ntp_time.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Explicitly cast values to double to avoid implicit conversion warnings
  62. // The conversion of the std::numeric_limits<int64_t>::max() triggers
  63. // -Wimplicit-int-float-conversion warning in clang 10.0.0 without explicit
  64. // cast
  65. if (result <= static_cast<double>(std::numeric_limits<int64_t>::min())) {
  66. return std::numeric_limits<int64_t>::min();
  67. }
  68. if (result >= static_cast<double>(std::numeric_limits<int64_t>::max())) {
  69. return std::numeric_limits<int64_t>::max();
  70. }
  71. return rtc::dchecked_cast<int64_t>(result);
  72. }
  73. // Converts |int64_t| milliseconds to UQ32.32-formatted fixed-point seconds.
  74. // Performs clamping if the result overflows or underflows.
  75. inline uint64_t Int64MsToUQ32x32(int64_t milliseconds) {
  76. // TODO(bugs.webrtc.org/10893): Change to use |rtc::saturated_cast| once the
  77. // bug has been fixed.
  78. double result =
  79. std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
  80. // Explicitly cast values to double to avoid implicit conversion warnings
  81. // The conversion of the std::numeric_limits<int64_t>::max() triggers
  82. // -Wimplicit-int-float-conversion warning in clang 10.0.0 without explicit
  83. // cast
  84. if (result <= static_cast<double>(std::numeric_limits<uint64_t>::min())) {
  85. return std::numeric_limits<uint64_t>::min();
  86. }
  87. if (result >= static_cast<double>(std::numeric_limits<uint64_t>::max())) {
  88. return std::numeric_limits<uint64_t>::max();
  89. }
  90. return rtc::dchecked_cast<uint64_t>(result);
  91. }
  92. // Converts Q32.32-formatted fixed-point seconds to |int64_t| milliseconds.
  93. inline int64_t Q32x32ToInt64Ms(int64_t q32x32) {
  94. return rtc::dchecked_cast<int64_t>(
  95. std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
  96. }
  97. // Converts UQ32.32-formatted fixed-point seconds to |int64_t| milliseconds.
  98. inline int64_t UQ32x32ToInt64Ms(uint64_t q32x32) {
  99. return rtc::dchecked_cast<int64_t>(
  100. std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
  101. }
  102. } // namespace webrtc
  103. #endif // SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_