blob: b912bc8a0cc4a597f145248a7f921fcd6b3d9b0b [file] [log] [blame]
danilchapb1ac2032015-11-26 09:01:10 -08001/*
Karl Wiberg79eb1d92017-11-08 12:26:07 +01002 * 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
11#define SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
danilchapb1ac2032015-11-26 09:01:10 -080012
Chen Xing60949532019-08-16 16:00:30 +020013#include <cmath>
14#include <cstdint>
15#include <limits>
pbosc7c26a02017-01-02 08:42:32 -080016
Patrik Höglund3e113432017-12-15 14:40:10 +010017#include "rtc_base/numerics/safe_conversions.h"
18
danilchapb1ac2032015-11-26 09:01:10 -080019namespace webrtc {
20
21class NtpTime {
22 public:
danilchap27260ce2017-02-15 01:18:15 -080023 static constexpr uint64_t kFractionsPerSecond = 0x100000000;
24 NtpTime() : value_(0) {}
25 explicit NtpTime(uint64_t value) : value_(value) {}
danilchapb1ac2032015-11-26 09:01:10 -080026 NtpTime(uint32_t seconds, uint32_t fractions)
danilchap27260ce2017-02-15 01:18:15 -080027 : value_(seconds * kFractionsPerSecond + fractions) {}
danilchapb1ac2032015-11-26 09:01:10 -080028
29 NtpTime(const NtpTime&) = default;
30 NtpTime& operator=(const NtpTime&) = default;
danilchap27260ce2017-02-15 01:18:15 -080031 explicit operator uint64_t() const { return value_; }
danilchapb1ac2032015-11-26 09:01:10 -080032
danilchapb1ac2032015-11-26 09:01:10 -080033 void Set(uint32_t seconds, uint32_t fractions) {
danilchap27260ce2017-02-15 01:18:15 -080034 value_ = seconds * kFractionsPerSecond + fractions;
danilchapb1ac2032015-11-26 09:01:10 -080035 }
danilchap27260ce2017-02-15 01:18:15 -080036 void Reset() { value_ = 0; }
danilchapb1ac2032015-11-26 09:01:10 -080037
danilchap37953762017-02-09 11:15:25 -080038 int64_t ToMs() const {
39 static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000.
danilchap27260ce2017-02-15 01:18:15 -080040 const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs;
41 return 1000 * static_cast<int64_t>(seconds()) +
danilchap37953762017-02-09 11:15:25 -080042 static_cast<int64_t>(frac_ms + 0.5);
43 }
danilchap27260ce2017-02-15 01:18:15 -080044 // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid.
45 bool Valid() const { return value_ != 0; }
danilchapb1ac2032015-11-26 09:01:10 -080046
Patrik Höglund3e113432017-12-15 14:40:10 +010047 uint32_t seconds() const {
48 return rtc::dchecked_cast<uint32_t>(value_ / kFractionsPerSecond);
49 }
50 uint32_t fractions() const {
51 return rtc::dchecked_cast<uint32_t>(value_ % kFractionsPerSecond);
52 }
danilchapb1ac2032015-11-26 09:01:10 -080053
54 private:
danilchap27260ce2017-02-15 01:18:15 -080055 uint64_t value_;
danilchapb1ac2032015-11-26 09:01:10 -080056};
57
58inline bool operator==(const NtpTime& n1, const NtpTime& n2) {
danilchap27260ce2017-02-15 01:18:15 -080059 return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2);
danilchapb1ac2032015-11-26 09:01:10 -080060}
61inline bool operator!=(const NtpTime& n1, const NtpTime& n2) {
62 return !(n1 == n2);
63}
64
Artem Titovf0671922021-07-27 12:40:17 +020065// Converts `int64_t` milliseconds to Q32.32-formatted fixed-point seconds.
Chen Xing60949532019-08-16 16:00:30 +020066// Performs clamping if the result overflows or underflows.
67inline int64_t Int64MsToQ32x32(int64_t milliseconds) {
Artem Titovcfea2182021-08-10 01:22:31 +020068 // TODO(bugs.webrtc.org/10893): Change to use `rtc::saturated_cast` once the
Chen Xing60949532019-08-16 16:00:30 +020069 // bug has been fixed.
70 double result =
71 std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
72
mmorrison9f6ff832020-04-07 15:45:22 -060073 // 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())) {
Chen Xing60949532019-08-16 16:00:30 +020078 return std::numeric_limits<int64_t>::min();
79 }
80
mmorrison9f6ff832020-04-07 15:45:22 -060081 if (result >= static_cast<double>(std::numeric_limits<int64_t>::max())) {
Chen Xing60949532019-08-16 16:00:30 +020082 return std::numeric_limits<int64_t>::max();
83 }
84
85 return rtc::dchecked_cast<int64_t>(result);
86}
87
Artem Titovf0671922021-07-27 12:40:17 +020088// Converts `int64_t` milliseconds to UQ32.32-formatted fixed-point seconds.
Chen Xing60949532019-08-16 16:00:30 +020089// Performs clamping if the result overflows or underflows.
90inline uint64_t Int64MsToUQ32x32(int64_t milliseconds) {
Artem Titovcfea2182021-08-10 01:22:31 +020091 // TODO(bugs.webrtc.org/10893): Change to use `rtc::saturated_cast` once the
Chen Xing60949532019-08-16 16:00:30 +020092 // bug has been fixed.
93 double result =
94 std::round(milliseconds * (NtpTime::kFractionsPerSecond / 1000.0));
95
mmorrison9f6ff832020-04-07 15:45:22 -060096 // 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())) {
Chen Xing60949532019-08-16 16:00:30 +0200101 return std::numeric_limits<uint64_t>::min();
102 }
103
mmorrison9f6ff832020-04-07 15:45:22 -0600104 if (result >= static_cast<double>(std::numeric_limits<uint64_t>::max())) {
Chen Xing60949532019-08-16 16:00:30 +0200105 return std::numeric_limits<uint64_t>::max();
106 }
107
108 return rtc::dchecked_cast<uint64_t>(result);
109}
110
Artem Titovf0671922021-07-27 12:40:17 +0200111// Converts Q32.32-formatted fixed-point seconds to `int64_t` milliseconds.
Chen Xing60949532019-08-16 16:00:30 +0200112inline int64_t Q32x32ToInt64Ms(int64_t q32x32) {
113 return rtc::dchecked_cast<int64_t>(
114 std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
115}
116
Artem Titovf0671922021-07-27 12:40:17 +0200117// Converts UQ32.32-formatted fixed-point seconds to `int64_t` milliseconds.
Chen Xing60949532019-08-16 16:00:30 +0200118inline int64_t UQ32x32ToInt64Ms(uint64_t q32x32) {
119 return rtc::dchecked_cast<int64_t>(
120 std::round(q32x32 * (1000.0 / NtpTime::kFractionsPerSecond)));
121}
122
danilchapb1ac2032015-11-26 09:01:10 -0800123} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_