| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ | 10 #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |
| 11 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ | 11 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |
| 12 | 12 |
| 13 #include <stdint.h> | 13 #include <stdint.h> |
| 14 | 14 |
| 15 namespace webrtc { | 15 namespace webrtc { |
| 16 | 16 |
| 17 class NtpTime { | 17 class NtpTime { |
| 18 public: | 18 public: |
| 19 NtpTime() : seconds_(0), fractions_(0) {} | 19 static constexpr uint64_t kFractionsPerSecond = 0x100000000; |
| 20 NtpTime() : value_(0) {} |
| 21 explicit NtpTime(uint64_t value) : value_(value) {} |
| 20 NtpTime(uint32_t seconds, uint32_t fractions) | 22 NtpTime(uint32_t seconds, uint32_t fractions) |
| 21 : seconds_(seconds), fractions_(fractions) {} | 23 : value_(seconds * kFractionsPerSecond + fractions) {} |
| 22 | 24 |
| 23 NtpTime(const NtpTime&) = default; | 25 NtpTime(const NtpTime&) = default; |
| 24 NtpTime& operator=(const NtpTime&) = default; | 26 NtpTime& operator=(const NtpTime&) = default; |
| 27 explicit operator uint64_t() const { return value_; } |
| 25 | 28 |
| 26 void Set(uint32_t seconds, uint32_t fractions) { | 29 void Set(uint32_t seconds, uint32_t fractions) { |
| 27 seconds_ = seconds; | 30 value_ = seconds * kFractionsPerSecond + fractions; |
| 28 fractions_ = fractions; | |
| 29 } | 31 } |
| 30 void Reset() { | 32 void Reset() { value_ = 0; } |
| 31 seconds_ = 0; | |
| 32 fractions_ = 0; | |
| 33 } | |
| 34 | 33 |
| 35 int64_t ToMs() const { | 34 int64_t ToMs() const { |
| 36 static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000. | 35 static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000. |
| 37 const double frac_ms = static_cast<double>(fractions_) / kNtpFracPerMs; | 36 const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs; |
| 38 return 1000 * static_cast<int64_t>(seconds_) + | 37 return 1000 * static_cast<int64_t>(seconds()) + |
| 39 static_cast<int64_t>(frac_ms + 0.5); | 38 static_cast<int64_t>(frac_ms + 0.5); |
| 40 } | 39 } |
| 41 // NTP standard (RFC1305, section 3.1) explicitly state value 0/0 is invalid. | 40 // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid. |
| 42 bool Valid() const { return !(seconds_ == 0 && fractions_ == 0); } | 41 bool Valid() const { return value_ != 0; } |
| 43 | 42 |
| 44 uint32_t seconds() const { return seconds_; } | 43 uint32_t seconds() const { return value_ / kFractionsPerSecond; } |
| 45 uint32_t fractions() const { return fractions_; } | 44 uint32_t fractions() const { return value_ % kFractionsPerSecond; } |
| 46 | 45 |
| 47 private: | 46 private: |
| 48 uint32_t seconds_; | 47 uint64_t value_; |
| 49 uint32_t fractions_; | |
| 50 }; | 48 }; |
| 51 | 49 |
| 52 inline bool operator==(const NtpTime& n1, const NtpTime& n2) { | 50 inline bool operator==(const NtpTime& n1, const NtpTime& n2) { |
| 53 return n1.seconds() == n2.seconds() && n1.fractions() == n2.fractions(); | 51 return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2); |
| 54 } | 52 } |
| 55 inline bool operator!=(const NtpTime& n1, const NtpTime& n2) { | 53 inline bool operator!=(const NtpTime& n1, const NtpTime& n2) { |
| 56 return !(n1 == n2); | 54 return !(n1 == n2); |
| 57 } | 55 } |
| 58 | 56 |
| 59 } // namespace webrtc | 57 } // namespace webrtc |
| 60 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ | 58 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |
| OLD | NEW |