blob: c026a4e7d98d8f5e9b5e2ea1aa5915c93c3e352f [file] [log] [blame]
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
12#define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
Sebastian Jansson4de31152019-06-11 08:52:11 +020018#include "api/units/timestamp.h"
Karl Wiberg2b857922018-03-23 14:53:54 +010019#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/ntp_time.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000021
22namespace webrtc {
23
24// January 1970, in NTP seconds.
25const uint32_t kNtpJan1970 = 2208988800UL;
26
27// Magic NTP fractional unit.
28const double kMagicNtpFractionalUnit = 4.294967296E+9;
29
30// A clock interface that allows reading of absolute and relative timestamps.
31class Clock {
32 public:
33 virtual ~Clock() {}
Sebastian Jansson4de31152019-06-11 08:52:11 +020034 // Return a timestamp relative to an unspecified epoch.
35 virtual Timestamp CurrentTime() {
36 return Timestamp::us(TimeInMicroseconds());
37 }
38 virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); }
39 virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000040
danilchap21dc1892017-03-07 02:51:09 -080041 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010042 virtual NtpTime CurrentNtpTime() = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000043
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000044 // Retrieve an NTP absolute timestamp in milliseconds.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010045 virtual int64_t CurrentNtpInMilliseconds() = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000046
47 // Converts an NTP timestamp to a millisecond timestamp.
danilchap37953762017-02-09 11:15:25 -080048 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
49 return NtpTime(seconds, fractions).ToMs();
50 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000051
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000052 // Returns an instance of the real-time system clock implementation.
53 static Clock* GetRealTimeClock();
54};
55
56class SimulatedClock : public Clock {
57 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000058 explicit SimulatedClock(int64_t initial_time_us);
Sebastian Jansson4de31152019-06-11 08:52:11 +020059 explicit SimulatedClock(Timestamp initial_time);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000060
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000062
Sebastian Jansson4de31152019-06-11 08:52:11 +020063 // Return a timestamp relative to some arbitrary source; the source is fixed
64 // for this clock.
65 Timestamp CurrentTime() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000066
danilchap21dc1892017-03-07 02:51:09 -080067 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010068 NtpTime CurrentNtpTime() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000069
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000070 // Converts an NTP timestamp to a millisecond timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010071 int64_t CurrentNtpInMilliseconds() override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000072
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000073 // Advance the simulated clock with a given number of milliseconds or
74 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000075 void AdvanceTimeMilliseconds(int64_t milliseconds);
76 void AdvanceTimeMicroseconds(int64_t microseconds);
Sebastian Jansson4de31152019-06-11 08:52:11 +020077 void AdvanceTime(TimeDelta delta);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000078
79 private:
Sebastian Jansson4de31152019-06-11 08:52:11 +020080 Timestamp time_;
kwibergbfefb032016-05-01 14:53:46 -070081 std::unique_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000082};
83
Nico Weber22f99252019-02-20 10:13:16 -050084} // namespace webrtc
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000085
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_