| 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 <memory> |
| 17 |
| 18 | #include "api/units/timestamp.h" |
| 19 | #include "rtc_base/synchronization/rw_lock_wrapper.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 | // Return a timestamp relative to an unspecified epoch. |
| 36 | virtual Timestamp CurrentTime() { |
| 37 | return Timestamp::Micros(TimeInMicroseconds()); |
| 38 | } |
| 39 | virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); } |
| 40 | virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); } |
| 41 |
| 42 | // Retrieve an NTP absolute timestamp. |
| 43 | virtual NtpTime CurrentNtpTime() = 0; |
| 44 |
| 45 | // Retrieve an NTP absolute timestamp in milliseconds. |
| 46 | virtual int64_t CurrentNtpInMilliseconds() = 0; |
| 47 |
| 48 | // Converts an NTP timestamp to a millisecond timestamp. |
| 49 | static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) { |
| 50 | return NtpTime(seconds, fractions).ToMs(); |
| 51 | } |
| 52 |
| 53 | // Returns an instance of the real-time system clock implementation. |
| 54 | static Clock* GetRealTimeClock(); |
| 55 | }; |
| 56 |
| 57 | class SimulatedClock : public Clock { |
| 58 | public: |
| 59 | explicit SimulatedClock(int64_t initial_time_us); |
| 60 | explicit SimulatedClock(Timestamp initial_time); |
| 61 |
| 62 | ~SimulatedClock() override; |
| 63 |
| 64 | // Return a timestamp relative to some arbitrary source; the source is fixed |
| 65 | // for this clock. |
| 66 | Timestamp CurrentTime() override; |
| 67 |
| 68 | // Retrieve an NTP absolute timestamp. |
| 69 | NtpTime CurrentNtpTime() override; |
| 70 |
| 71 | // Converts an NTP timestamp to a millisecond timestamp. |
| 72 | int64_t CurrentNtpInMilliseconds() override; |
| 73 |
| 74 | // Advance the simulated clock with a given number of milliseconds or |
| 75 | // microseconds. |
| 76 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 77 | void AdvanceTimeMicroseconds(int64_t microseconds); |
| 78 | void AdvanceTime(TimeDelta delta); |
| 79 |
| 80 | private: |
| 81 | Timestamp time_; |
| 82 | std::unique_ptr<RWLockWrapper> lock_; |
| 83 | }; |
| 84 |
| 85 | } // namespace webrtc |
| 86 |
| 87 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 88 |