| 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 | NtpTime CurrentNtpTime() { return ConvertTimestampToNtpTime(CurrentTime()); } |
| 43 | int64_t CurrentNtpInMilliseconds() { return CurrentNtpTime().ToMs(); } |
| 44 |
| 45 | // Converts between a relative timestamp returned by this clock, to NTP time. |
| 46 | virtual NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) = 0; |
| 47 | int64_t ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms) { |
| 48 | return ConvertTimestampToNtpTime(Timestamp::Millis(timestamp_ms)).ToMs(); |
| 49 | } |
| 50 |
| 51 | // Returns an instance of the real-time system clock implementation. |
| 52 | static Clock* GetRealTimeClock(); |
| 53 | }; |
| 54 |
| 55 | class SimulatedClock : public Clock { |
| 56 | public: |
| 57 | // The constructors assume an epoch of Jan 1, 1970. |
| 58 | explicit SimulatedClock(int64_t initial_time_us); |
| 59 | explicit SimulatedClock(Timestamp initial_time); |
| 60 | ~SimulatedClock() override; |
| 61 |
| 62 | // Return a timestamp with an epoch of Jan 1, 1970. |
| 63 | Timestamp CurrentTime() override; |
| 64 |
| 65 | NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override; |
| 66 |
| 67 | // Advance the simulated clock with a given number of milliseconds or |
| 68 | // microseconds. |
| 69 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 70 | void AdvanceTimeMicroseconds(int64_t microseconds); |
| 71 | void AdvanceTime(TimeDelta delta); |
| 72 |
| 73 | private: |
| 74 | // The time is read and incremented with relaxed order. Each thread will see |
| 75 | // monotonically increasing time, and when threads post tasks or messages to |
| 76 | // one another, the synchronization done as part of the message passing should |
| 77 | // ensure that any causual chain of events on multiple threads also |
| 78 | // corresponds to monotonically increasing time. |
| 79 | std::atomic<int64_t> time_us_; |
| 80 | }; |
| 81 |
| 82 | } // namespace webrtc |
| 83 |
| 84 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 85 |