You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
  11. #define SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
  12. #include <cmath>
  13. #include <cstdint>
  14. #include <limits>
  15. #include "rtc_base/numerics/safe_conversions.h"
  16. namespace webrtc {
  17. class NtpTime {
  18. public:
  19. static constexpr uint64_t kFractionsPerSecond = 0x100000000;
  20. NtpTime() : value_(0) {}
  21. explicit NtpTime(uint64_t value) : value_(value) {}
  22. NtpTime(uint32_t seconds, uint32_t fractions)
  23. : value_(seconds * kFractionsPerSecond + fractions) {}
  24. NtpTime(const NtpTime&) = default;
  25. NtpTime& operator=(const NtpTime&) = default;
  26. explicit operator uint64_t() const { return value_; }
  27. void Set(uint32_t seconds, uint32_t fractions) {
  28. value_ = seconds * kFractionsPerSecond + fractions;
  29. }
  30. void Reset() { value_ = 0; }
  31. int64_t ToMs() const {
  32. static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000.
  33. const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs;
  34. return 1000 * static_cast<int64_t>(seconds()) +
  35. static_cast<int64_t>(frac_ms + 0.5);
  36. }
  37. // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid.
  38. bool Valid() const { return value_ != 0; }
  39. uint32_t seconds() const {
  40. return rtc::dchecked_cast<uint32_t>(value_ / kFractionsPerSecond);
  41. }
  42. uint32_t fractions() const {
  43. return rtc::dchecked_cast<uint32_t>(value_ % kFractionsPerSecond);
  44. }
  45. private:
  46. uint64_t value_;
  47. };
  48. inline bool operator==(const NtpTime& n1, const NtpTime& n2) {
  49. return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2);
  50. }
  51. inline bool operator!=(const NtpTime& n1, const NtpTime& n2) {
  52. return !(n1 == n2);
  53. }
  54. // Converts |int64_t| milliseconds to Q32.32-formatted fixed-point seconds.
  55. // Performs clamping if the result overflows or underflows.
  56. inline int64_t Int64MsToQ32x32(int64_t milliseconds) {
  57. // TODO(bugs.webrtc.org/10893): Change to use |rtc::saturated_cast| once the
  58. // bug has been fixed.
  59. double result =
  60. std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
  61. if (result <= std::numeric_limits<int64_t>::min()) {
  62. return std::numeric_limits<int64_t>::min();
  63. }
  64. if (result >= std::numeric_limits<int64_t>::max()) {
  65. return std::numeric_limits<int64_t>::max();
  66. }
  67. return rtc::dchecked_cast<int64_t>(result);
  68. }
  69. // Converts |int64_t| milliseconds to UQ32.32-formatted fixed-point seconds.
  70. // Performs clamping if the result overflows or underflows.
  71. inline uint64_t Int64MsToUQ32x32(int64_t milliseconds) {
  72. // TODO(bugs.webrtc.org/10893): Change to use |rtc::saturated_cast| once the
  73. // bug has been fixed.
  74. double result =
  75. std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
  76. if (result <= std::numeric_limits<uint64_t>::min()) {
  77. return std::numeric_limits<uint64_t>::min();
  78. }
  79. if (result >= std::numeric_limits<uint64_t>::max()) {
  80. return std::numeric_limits<uint64_t>::max();
  81. }
  82. return rtc::dchecked_cast<uint64_t>(result);
  83. }
  84. // Converts Q32.32-formatted fixed-point seconds to |int64_t| milliseconds.
  85. inline int64_t Q32x32ToInt64Ms(int64_t q32x32) {
  86. return rtc::dchecked_cast<int64_t>(
  87. std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
  88. }
  89. // Converts UQ32.32-formatted fixed-point seconds to |int64_t| milliseconds.
  90. inline int64_t UQ32x32ToInt64Ms(uint64_t q32x32) {
  91. return rtc::dchecked_cast<int64_t>(
  92. std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
  93. }
  94. } // namespace webrtc
  95. #endif // SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_