stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
| 12 | #define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 18 | #include "api/units/timestamp.h" |
Karl Wiberg | 2b85792 | 2018-03-23 14:53:54 +0100 | [diff] [blame] | 19 | #include "rtc_base/synchronization/rw_lock_wrapper.h" |
Johannes Kron | 6d2dfeb | 2020-03-09 12:58:47 +0100 | [diff] [blame^] | 20 | #include "rtc_base/system/rtc_export.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/ntp_time.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 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. |
Johannes Kron | 6d2dfeb | 2020-03-09 12:58:47 +0100 | [diff] [blame^] | 32 | class RTC_EXPORT Clock { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 33 | public: |
| 34 | virtual ~Clock() {} |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 35 | // Return a timestamp relative to an unspecified epoch. |
| 36 | virtual Timestamp CurrentTime() { |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 37 | return Timestamp::Micros(TimeInMicroseconds()); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 38 | } |
| 39 | virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); } |
| 40 | virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 41 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 42 | // Retrieve an NTP absolute timestamp. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 43 | virtual NtpTime CurrentNtpTime() = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 44 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 45 | // Retrieve an NTP absolute timestamp in milliseconds. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 46 | virtual int64_t CurrentNtpInMilliseconds() = 0; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 47 | |
| 48 | // Converts an NTP timestamp to a millisecond timestamp. |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 49 | static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) { |
| 50 | return NtpTime(seconds, fractions).ToMs(); |
| 51 | } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 52 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 53 | // Returns an instance of the real-time system clock implementation. |
| 54 | static Clock* GetRealTimeClock(); |
| 55 | }; |
| 56 | |
| 57 | class SimulatedClock : public Clock { |
| 58 | public: |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 59 | explicit SimulatedClock(int64_t initial_time_us); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 60 | explicit SimulatedClock(Timestamp initial_time); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 61 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 62 | ~SimulatedClock() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 63 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 64 | // Return a timestamp relative to some arbitrary source; the source is fixed |
| 65 | // for this clock. |
| 66 | Timestamp CurrentTime() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 67 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 68 | // Retrieve an NTP absolute timestamp. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 69 | NtpTime CurrentNtpTime() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 70 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 71 | // Converts an NTP timestamp to a millisecond timestamp. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 72 | int64_t CurrentNtpInMilliseconds() override; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 73 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 74 | // Advance the simulated clock with a given number of milliseconds or |
| 75 | // microseconds. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 76 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 77 | void AdvanceTimeMicroseconds(int64_t microseconds); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 78 | void AdvanceTime(TimeDelta delta); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 81 | Timestamp time_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 82 | std::unique_ptr<RWLockWrapper> lock_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 85 | } // namespace webrtc |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 86 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 87 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |