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 SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
12 #define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
13 
14 #include <stdint.h>
15 
16 #include <memory>
17 
18 #include "api/units/timestamp.h"
19 #include "rtc_base/synchronization/rw_lock_wrapper.h"
20 #include "rtc_base/system/rtc_export.h"
21 #include "system_wrappers/include/ntp_time.h"
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.
32 class RTC_EXPORT Clock {
33  public:
~Clock()34   virtual ~Clock() {}
35 
36   // Return a timestamp relative to an unspecified epoch.
37   virtual Timestamp CurrentTime() = 0;
TimeInMilliseconds()38   int64_t TimeInMilliseconds() { return CurrentTime().ms(); }
TimeInMicroseconds()39   int64_t TimeInMicroseconds() { return CurrentTime().us(); }
40 
41   // Retrieve an NTP absolute timestamp (with an epoch of Jan 1, 1900).
42   // TODO(bugs.webrtc.org/11327): Make this non-virtual once
43   // "WebRTC-SystemIndependentNtpTimeKillSwitch" is removed.
CurrentNtpTime()44   virtual NtpTime CurrentNtpTime() {
45     return ConvertTimestampToNtpTime(CurrentTime());
46   }
CurrentNtpInMilliseconds()47   int64_t CurrentNtpInMilliseconds() { return CurrentNtpTime().ToMs(); }
48 
49   // Converts between a relative timestamp returned by this clock, to NTP time.
50   virtual NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) = 0;
ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms)51   int64_t ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms) {
52     return ConvertTimestampToNtpTime(Timestamp::Millis(timestamp_ms)).ToMs();
53   }
54 
55   // Returns an instance of the real-time system clock implementation.
56   static Clock* GetRealTimeClockRaw();
57 };
58 
59 class SimulatedClock : public Clock {
60  public:
61   // The constructors assume an epoch of Jan 1, 1970.
62   explicit SimulatedClock(int64_t initial_time_us);
63   explicit SimulatedClock(Timestamp initial_time);
64   ~SimulatedClock() override;
65 
66   // Return a timestamp with an epoch of Jan 1, 1970.
67   Timestamp CurrentTime() override;
68 
69   NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override;
70 
71   // Advance the simulated clock with a given number of milliseconds or
72   // microseconds.
73   void AdvanceTimeMilliseconds(int64_t milliseconds);
74   void AdvanceTimeMicroseconds(int64_t microseconds);
75   void AdvanceTime(TimeDelta delta);
76 
77  private:
78   Timestamp time_;
79   std::unique_ptr<RWLockWrapper> lock_;
80 };
81 
82 }  // namespace webrtc
83 
84 #endif  // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
85