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 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Karl Wiberg | 2b85792 | 2018-03-23 14:53:54 +0100 | [diff] [blame^] | 16 | #include "rtc_base/synchronization/rw_lock_wrapper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "system_wrappers/include/ntp_time.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "typedefs.h" // NOLINT(build/include) |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // January 1970, in NTP seconds. |
| 23 | const uint32_t kNtpJan1970 = 2208988800UL; |
| 24 | |
| 25 | // Magic NTP fractional unit. |
| 26 | const double kMagicNtpFractionalUnit = 4.294967296E+9; |
| 27 | |
| 28 | // A clock interface that allows reading of absolute and relative timestamps. |
| 29 | class Clock { |
| 30 | public: |
| 31 | virtual ~Clock() {} |
| 32 | |
| 33 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 34 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 35 | virtual int64_t TimeInMilliseconds() const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 36 | |
| 37 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 38 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 39 | virtual int64_t TimeInMicroseconds() const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 40 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 41 | // Retrieve an NTP absolute timestamp. |
| 42 | virtual NtpTime CurrentNtpTime() const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 43 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 44 | // Retrieve an NTP absolute timestamp in milliseconds. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 45 | virtual int64_t CurrentNtpInMilliseconds() const = 0; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 46 | |
| 47 | // Converts an NTP timestamp to a millisecond timestamp. |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 48 | static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) { |
| 49 | return NtpTime(seconds, fractions).ToMs(); |
| 50 | } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 51 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 52 | // Returns an instance of the real-time system clock implementation. |
| 53 | static Clock* GetRealTimeClock(); |
| 54 | }; |
| 55 | |
| 56 | class SimulatedClock : public Clock { |
| 57 | public: |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 58 | explicit SimulatedClock(int64_t initial_time_us); |
| 59 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 60 | ~SimulatedClock() override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 61 | |
| 62 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 63 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 64 | int64_t TimeInMilliseconds() const override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 65 | |
| 66 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 67 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 68 | int64_t TimeInMicroseconds() const override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 69 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 70 | // Retrieve an NTP absolute timestamp. |
| 71 | NtpTime CurrentNtpTime() const override; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 72 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 73 | // Converts an NTP timestamp to a millisecond timestamp. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 74 | int64_t CurrentNtpInMilliseconds() const override; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 75 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 76 | // Advance the simulated clock with a given number of milliseconds or |
| 77 | // microseconds. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 78 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 79 | void AdvanceTimeMicroseconds(int64_t microseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 80 | |
| 81 | private: |
henrike@webrtc.org | 7ea71de | 2014-06-26 16:13:58 +0000 | [diff] [blame] | 82 | int64_t time_us_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 83 | std::unique_ptr<RWLockWrapper> lock_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | }; // namespace webrtc |
| 87 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 88 | #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_ |