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 | |
Niels Möller | 9308b7a | 2020-11-03 13:55:44 +0100 | [diff] [blame] | 16 | #include <atomic> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 19 | #include "api/units/timestamp.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() {} |
Paul Hallak | 704d6e5 | 2021-04-08 14:57:45 +0200 | [diff] [blame] | 35 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 36 | // Return a timestamp relative to an unspecified epoch. |
Paul Hallak | 0de1ed0 | 2021-05-19 14:36:50 +0200 | [diff] [blame] | 37 | virtual Timestamp CurrentTime() = 0; |
| 38 | int64_t TimeInMilliseconds() { return CurrentTime().ms(); } |
| 39 | int64_t TimeInMicroseconds() { return CurrentTime().us(); } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 40 | |
Paul Hallak | 704d6e5 | 2021-04-08 14:57:45 +0200 | [diff] [blame] | 41 | // Retrieve an NTP absolute timestamp (with an epoch of Jan 1, 1900). |
Paul Hallak | b59e904 | 2021-05-20 17:21:49 +0200 | [diff] [blame] | 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 | } |
Paul Hallak | 0de1ed0 | 2021-05-19 14:36:50 +0200 | [diff] [blame] | 47 | int64_t CurrentNtpInMilliseconds() { return CurrentNtpTime().ToMs(); } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 48 | |
Paul Hallak | b59e904 | 2021-05-20 17:21:49 +0200 | [diff] [blame] | 49 | // Converts between a relative timestamp returned by this clock, to NTP time. |
Paul Hallak | 2491dbd | 2021-05-21 07:31:52 +0200 | [diff] [blame] | 50 | virtual NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) = 0; |
Paul Hallak | b59e904 | 2021-05-20 17:21:49 +0200 | [diff] [blame] | 51 | int64_t ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms) { |
| 52 | return ConvertTimestampToNtpTime(Timestamp::Millis(timestamp_ms)).ToMs(); |
| 53 | } |
| 54 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 55 | // Returns an instance of the real-time system clock implementation. |
| 56 | static Clock* GetRealTimeClock(); |
| 57 | }; |
| 58 | |
| 59 | class SimulatedClock : public Clock { |
| 60 | public: |
Paul Hallak | 704d6e5 | 2021-04-08 14:57:45 +0200 | [diff] [blame] | 61 | // The constructors assume an epoch of Jan 1, 1970. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 62 | explicit SimulatedClock(int64_t initial_time_us); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 63 | explicit SimulatedClock(Timestamp initial_time); |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 64 | ~SimulatedClock() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 65 | |
Paul Hallak | 704d6e5 | 2021-04-08 14:57:45 +0200 | [diff] [blame] | 66 | // Return a timestamp with an epoch of Jan 1, 1970. |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 67 | Timestamp CurrentTime() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 68 | |
Paul Hallak | b59e904 | 2021-05-20 17:21:49 +0200 | [diff] [blame] | 69 | NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 70 | |
| 71 | // Advance the simulated clock with a given number of milliseconds or |
| 72 | // microseconds. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 73 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 74 | void AdvanceTimeMicroseconds(int64_t microseconds); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 75 | void AdvanceTime(TimeDelta delta); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
Niels Möller | 9308b7a | 2020-11-03 13:55:44 +0100 | [diff] [blame] | 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_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 86 | } // namespace webrtc |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 87 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 88 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |