| 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
| 11 | #ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 12 | #define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <atomic> |
| 17 | #include <memory> |
| 18 | |
| 19 | #include "api/units/timestamp.h" |
| 20 | #include "rtc_base/system/rtc_export.h" |
| 21 | #include "system_wrappers/include/ntp_time.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // January 1970, in NTP seconds. |
| 26 | const uint32_t kNtpJan1970 = 2208988800UL; |
| 27 | |
| 28 | // Magic NTP fractional unit. |
| 29 | const double kMagicNtpFractionalUnit = 4.294967296E+9; |
| 30 | |
| 31 | // A clock interface that allows reading of absolute and relative timestamps. |
| 32 | class RTC_EXPORT Clock { |
| 33 | public: |
| 34 | virtual ~Clock() {} |
| 35 | |
| 36 | // Return a timestamp relative to an unspecified epoch. |
| 37 | virtual Timestamp CurrentTime() = 0; |
| 38 | int64_t TimeInMilliseconds() { return CurrentTime().ms(); } |
| 39 | int64_t TimeInMicroseconds() { return CurrentTime().us(); } |
| 40 | |
| 41 | // Retrieve an NTP absolute timestamp (with an epoch of Jan 1, 1900). |
| 42 | // TODO(bugs.webrtc.org/11327): Make this non-virtual once |
| 43 | // "WebRTC-SystemIndependentNtpTimeKillSwitch" is removed. |
| 44 | virtual NtpTime CurrentNtpTime() { |
| 45 | return ConvertTimestampToNtpTime(CurrentTime()); |
| 46 | } |
| 47 | int64_t CurrentNtpInMilliseconds() { return CurrentNtpTime().ToMs(); } |
| 48 | |
| 49 | // Converts between a relative timestamp returned by this clock, to NTP time. |
| 50 | virtual NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) = 0; |
| 51 | int64_t ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms) { |
| 52 | return ConvertTimestampToNtpTime(Timestamp::Millis(timestamp_ms)).ToMs(); |
| 53 | } |
| 54 | |
| 55 | // Returns an instance of the real-time system clock implementation. |
| 56 | static Clock* GetRealTimeClock(); |
| 57 | }; |
| 58 | |
| 59 | class SimulatedClock : public Clock { |
| 60 | public: |
| 61 | // The constructors assume an epoch of Jan 1, 1970. |
| 62 | explicit SimulatedClock(int64_t initial_time_us); |
| 63 | explicit SimulatedClock(Timestamp initial_time); |
| 64 | ~SimulatedClock() override; |
| 65 | |
| 66 | // Return a timestamp with an epoch of Jan 1, 1970. |
| 67 | Timestamp CurrentTime() override; |
| 68 | |
| 69 | NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override; |
| 70 | |
| 71 | // Advance the simulated clock with a given number of milliseconds or |
| 72 | // microseconds. |
| 73 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 74 | void AdvanceTimeMicroseconds(int64_t microseconds); |
| 75 | void AdvanceTime(TimeDelta delta); |
| 76 | |
| 77 | private: |
| 78 | // The time is read and incremented with relaxed order. Each thread will see |
| 79 | // monotonically increasing time, and when threads post tasks or messages to |
| 80 | // one another, the synchronization done as part of the message passing should |
| 81 | // ensure that any causual chain of events on multiple threads also |
| 82 | // corresponds to monotonically increasing time. |
| 83 | std::atomic<int64_t> time_us_; |
| 84 | }; |
| 85 | |
| 86 | } // namespace webrtc |
| 87 | |
| 88 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 89 | |