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 | |
| 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_ |
| 13 | |
henrik.lundin@webrtc.org | 19db3e3 | 2014-06-26 14:01:18 +0000 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h" |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 16 | #include "webrtc/system_wrappers/interface/thread_annotations.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // January 1970, in NTP seconds. |
| 22 | const uint32_t kNtpJan1970 = 2208988800UL; |
| 23 | |
| 24 | // Magic NTP fractional unit. |
| 25 | const double kMagicNtpFractionalUnit = 4.294967296E+9; |
| 26 | |
| 27 | // A clock interface that allows reading of absolute and relative timestamps. |
| 28 | class Clock { |
| 29 | public: |
| 30 | virtual ~Clock() {} |
| 31 | |
| 32 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 33 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 34 | virtual int64_t TimeInMilliseconds() const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 35 | |
| 36 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 37 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 38 | virtual int64_t TimeInMicroseconds() const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 39 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 40 | // Retrieve an NTP absolute timestamp in seconds and fractions of a second. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 41 | virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 42 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 43 | // Retrieve an NTP absolute timestamp in milliseconds. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 44 | virtual int64_t CurrentNtpInMilliseconds() const = 0; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 45 | |
| 46 | // Converts an NTP timestamp to a millisecond timestamp. |
| 47 | static int64_t NtpToMs(uint32_t seconds, uint32_t fractions); |
| 48 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 49 | // Returns an instance of the real-time system clock implementation. |
| 50 | static Clock* GetRealTimeClock(); |
| 51 | }; |
| 52 | |
| 53 | class SimulatedClock : public Clock { |
| 54 | public: |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 55 | explicit SimulatedClock(int64_t initial_time_us); |
| 56 | |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 57 | virtual ~SimulatedClock(); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 58 | |
| 59 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 60 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 61 | virtual int64_t TimeInMilliseconds() const OVERRIDE; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 62 | |
| 63 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 64 | // source is fixed for this clock. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 65 | virtual int64_t TimeInMicroseconds() const OVERRIDE; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 66 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 67 | // Retrieve an NTP absolute timestamp in milliseconds. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 68 | virtual void CurrentNtp(uint32_t& seconds, |
| 69 | uint32_t& fractions) const 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. |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 72 | virtual int64_t CurrentNtpInMilliseconds() const 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); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 80 | int64_t time_us_ GUARDED_BY(lock_); |
| 81 | scoped_ptr<RWLockWrapper> lock_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | }; // namespace webrtc |
| 85 | |
| 86 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_ |