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/system_wrappers/interface/scoped_ptr.h" 15 #include "webrtc/system_wrappers/interface/thread_annotations.h" 16 #include "webrtc/typedefs.h" 17 18 namespace webrtc { 19 20 class RWLockWrapper; 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. 35 virtual int64_t TimeInMilliseconds() const = 0; 36 37 // Return a timestamp in microseconds relative to some arbitrary source; the 38 // source is fixed for this clock. 39 virtual int64_t TimeInMicroseconds() const = 0; 40 41 // Retrieve an NTP absolute timestamp in seconds and fractions of a second. 42 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const = 0; 43 44 // Retrieve an NTP absolute timestamp in milliseconds. 45 virtual int64_t CurrentNtpInMilliseconds() const = 0; 46 47 // Converts an NTP timestamp to a millisecond timestamp. 48 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions); 49 50 // Returns an instance of the real-time system clock implementation. 51 static Clock* GetRealTimeClock(); 52 }; 53 54 class SimulatedClock : public Clock { 55 public: 56 explicit SimulatedClock(int64_t initial_time_us); 57 58 virtual ~SimulatedClock(); 59 60 // Return a timestamp in milliseconds relative to some arbitrary source; the 61 // source is fixed for this clock. 62 virtual int64_t TimeInMilliseconds() const OVERRIDE; 63 64 // Return a timestamp in microseconds relative to some arbitrary source; the 65 // source is fixed for this clock. 66 virtual int64_t TimeInMicroseconds() const OVERRIDE; 67 68 // Retrieve an NTP absolute timestamp in milliseconds. 69 virtual void CurrentNtp(uint32_t& seconds, 70 uint32_t& fractions) const OVERRIDE; 71 72 // Converts an NTP timestamp to a millisecond timestamp. 73 virtual int64_t CurrentNtpInMilliseconds() const OVERRIDE; 74 75 // Advance the simulated clock with a given number of milliseconds or 76 // microseconds. 77 void AdvanceTimeMilliseconds(int64_t milliseconds); 78 void AdvanceTimeMicroseconds(int64_t microseconds); 79 80 private: 81 int64_t time_us_ GUARDED_BY(lock_); 82 scoped_ptr<RWLockWrapper> lock_; 83 }; 84 85 }; // namespace webrtc 86 87 #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_ 88