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 
13 #include <cmath>
14 #include <cstdint>
15 #include <limits>
16 
17 #include "rtc_base/numerics/safe_conversions.h"
18 
19 namespace webrtc {
20 
21 class NtpTime {
22  public:
23   static constexpr uint64_t kFractionsPerSecond = 0x100000000;
NtpTime()24   NtpTime() : value_(0) {}
NtpTime(uint64_t value)25   explicit NtpTime(uint64_t value) : value_(value) {}
NtpTime(uint32_t seconds,uint32_t fractions)26   NtpTime(uint32_t seconds, uint32_t fractions)
27       : value_(seconds * kFractionsPerSecond + fractions) {}
28 
29   NtpTime(const NtpTime&) = default;
30   NtpTime& operator=(const NtpTime&) = default;
uint64_t()31   explicit operator uint64_t() const { return value_; }
32 
Set(uint32_t seconds,uint32_t fractions)33   void Set(uint32_t seconds, uint32_t fractions) {
34     value_ = seconds * kFractionsPerSecond + fractions;
35   }
Reset()36   void Reset() { value_ = 0; }
37 
ToMs()38   int64_t ToMs() const {
39     static constexpr double kNtpFracPerMs = 4.294967296E6;  // 2^32 / 1000.
40     const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs;
41     return 1000 * static_cast<int64_t>(seconds()) +
42            static_cast<int64_t>(frac_ms + 0.5);
43   }
44   // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid.
Valid()45   bool Valid() const { return value_ != 0; }
46 
seconds()47   uint32_t seconds() const {
48     return rtc::dchecked_cast<uint32_t>(value_ / kFractionsPerSecond);
49   }
fractions()50   uint32_t fractions() const {
51     return rtc::dchecked_cast<uint32_t>(value_ % kFractionsPerSecond);
52   }
53 
54  private:
55   uint64_t value_;
56 };
57 
58 inline bool operator==(const NtpTime& n1, const NtpTime& n2) {
59   return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2);
60 }
61 inline bool operator!=(const NtpTime& n1, const NtpTime& n2) {
62   return !(n1 == n2);
63 }
64 
65 // Converts |int64_t| milliseconds to Q32.32-formatted fixed-point seconds.
66 // Performs clamping if the result overflows or underflows.
Int64MsToQ32x32(int64_t milliseconds)67 inline int64_t Int64MsToQ32x32(int64_t milliseconds) {
68   // TODO(bugs.webrtc.org/10893): Change to use |rtc::saturated_cast| once the
69   // bug has been fixed.
70   double result =
71       std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
72 
73   // Explicitly cast values to double to avoid implicit conversion warnings
74   // The conversion of the std::numeric_limits<int64_t>::max() triggers
75   // -Wimplicit-int-float-conversion warning in clang 10.0.0 without explicit
76   // cast
77   if (result <= static_cast<double>(std::numeric_limits<int64_t>::min())) {
78     return std::numeric_limits<int64_t>::min();
79   }
80 
81   if (result >= static_cast<double>(std::numeric_limits<int64_t>::max())) {
82     return std::numeric_limits<int64_t>::max();
83   }
84 
85   return rtc::dchecked_cast<int64_t>(result);
86 }
87 
88 // Converts |int64_t| milliseconds to UQ32.32-formatted fixed-point seconds.
89 // Performs clamping if the result overflows or underflows.
Int64MsToUQ32x32(int64_t milliseconds)90 inline uint64_t Int64MsToUQ32x32(int64_t milliseconds) {
91   // TODO(bugs.webrtc.org/10893): Change to use |rtc::saturated_cast| once the
92   // bug has been fixed.
93   double result =
94       std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
95 
96   // Explicitly cast values to double to avoid implicit conversion warnings
97   // The conversion of the std::numeric_limits<int64_t>::max() triggers
98   // -Wimplicit-int-float-conversion warning in clang 10.0.0 without explicit
99   // cast
100   if (result <= static_cast<double>(std::numeric_limits<uint64_t>::min())) {
101     return std::numeric_limits<uint64_t>::min();
102   }
103 
104   if (result >= static_cast<double>(std::numeric_limits<uint64_t>::max())) {
105     return std::numeric_limits<uint64_t>::max();
106   }
107 
108   return rtc::dchecked_cast<uint64_t>(result);
109 }
110 
111 // Converts Q32.32-formatted fixed-point seconds to |int64_t| milliseconds.
Q32x32ToInt64Ms(int64_t q32x32)112 inline int64_t Q32x32ToInt64Ms(int64_t q32x32) {
113   return rtc::dchecked_cast<int64_t>(
114       std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
115 }
116 
117 // Converts UQ32.32-formatted fixed-point seconds to |int64_t| milliseconds.
UQ32x32ToInt64Ms(uint64_t q32x32)118 inline int64_t UQ32x32ToInt64Ms(uint64_t q32x32) {
119   return rtc::dchecked_cast<int64_t>(
120       std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
121 }
122 
123 }  // namespace webrtc
124 #endif  // SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
125