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 | |
| 14 | #include "webrtc/typedefs.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | // January 1970, in NTP seconds. |
| 19 | const uint32_t kNtpJan1970 = 2208988800UL; |
| 20 | |
| 21 | // Magic NTP fractional unit. |
| 22 | const double kMagicNtpFractionalUnit = 4.294967296E+9; |
| 23 | |
| 24 | // A clock interface that allows reading of absolute and relative timestamps. |
| 25 | class Clock { |
| 26 | public: |
| 27 | virtual ~Clock() {} |
| 28 | |
| 29 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 30 | // source is fixed for this clock. |
| 31 | virtual int64_t TimeInMilliseconds() = 0; |
| 32 | |
| 33 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 34 | // source is fixed for this clock. |
| 35 | virtual int64_t TimeInMicroseconds() = 0; |
| 36 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 37 | // Retrieve an NTP absolute timestamp in seconds and fractions of a second. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 38 | virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) = 0; |
| 39 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 40 | // Retrieve an NTP absolute timestamp in milliseconds. |
| 41 | virtual int64_t CurrentNtpInMilliseconds() = 0; |
| 42 | |
| 43 | // Converts an NTP timestamp to a millisecond timestamp. |
| 44 | static int64_t NtpToMs(uint32_t seconds, uint32_t fractions); |
| 45 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 46 | // Returns an instance of the real-time system clock implementation. |
| 47 | static Clock* GetRealTimeClock(); |
| 48 | }; |
| 49 | |
| 50 | class SimulatedClock : public Clock { |
| 51 | public: |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 52 | explicit SimulatedClock(int64_t initial_time_us); |
| 53 | |
| 54 | virtual ~SimulatedClock() {} |
| 55 | |
| 56 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 57 | // source is fixed for this clock. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 58 | virtual int64_t TimeInMilliseconds() OVERRIDE; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 59 | |
| 60 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 61 | // source is fixed for this clock. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 62 | virtual int64_t TimeInMicroseconds() OVERRIDE; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 63 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 64 | // Retrieve an NTP absolute timestamp in milliseconds. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 65 | virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) 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 | // Converts an NTP timestamp to a millisecond timestamp. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 68 | virtual int64_t CurrentNtpInMilliseconds() OVERRIDE; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 69 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 70 | // Advance the simulated clock with a given number of milliseconds or |
| 71 | // microseconds. |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 72 | void AdvanceTimeMilliseconds(int64_t milliseconds); |
| 73 | void AdvanceTimeMicroseconds(int64_t microseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | int64_t time_us_; |
| 77 | }; |
| 78 | |
| 79 | }; // namespace webrtc |
| 80 | |
| 81 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_ |