blob: 8eac3d18b3f475f6c7f395c5b5f8ae959c2be0fb [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"
Johannes Kron6d2dfeb2020-03-09 12:58:47 +010020#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "system_wrappers/include/ntp_time.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000022
23namespace webrtc {
24
25// January 1970, in NTP seconds.
26const uint32_t kNtpJan1970 = 2208988800UL;
27
28// Magic NTP fractional unit.
29const double kMagicNtpFractionalUnit = 4.294967296E+9;
30
31// A clock interface that allows reading of absolute and relative timestamps.
Johannes Kron6d2dfeb2020-03-09 12:58:47 +010032class RTC_EXPORT Clock {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000033 public:
34 virtual ~Clock() {}
Sebastian Jansson4de31152019-06-11 08:52:11 +020035 // Return a timestamp relative to an unspecified epoch.
36 virtual Timestamp CurrentTime() {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010037 return Timestamp::Micros(TimeInMicroseconds());
Sebastian Jansson4de31152019-06-11 08:52:11 +020038 }
39 virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); }
40 virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000041
danilchap21dc1892017-03-07 02:51:09 -080042 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010043 virtual NtpTime CurrentNtpTime() = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000044
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000045 // Retrieve an NTP absolute timestamp in milliseconds.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010046 virtual int64_t CurrentNtpInMilliseconds() = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000047
48 // Converts an NTP timestamp to a millisecond timestamp.
danilchap37953762017-02-09 11:15:25 -080049 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
50 return NtpTime(seconds, fractions).ToMs();
51 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000052
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000053 // Returns an instance of the real-time system clock implementation.
54 static Clock* GetRealTimeClock();
55};
56
57class SimulatedClock : public Clock {
58 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000059 explicit SimulatedClock(int64_t initial_time_us);
Sebastian Jansson4de31152019-06-11 08:52:11 +020060 explicit SimulatedClock(Timestamp initial_time);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000061
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000063
Sebastian Jansson4de31152019-06-11 08:52:11 +020064 // Return a timestamp relative to some arbitrary source; the source is fixed
65 // for this clock.
66 Timestamp CurrentTime() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000067
danilchap21dc1892017-03-07 02:51:09 -080068 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010069 NtpTime CurrentNtpTime() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000070
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000071 // Converts an NTP timestamp to a millisecond timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010072 int64_t CurrentNtpInMilliseconds() override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000073
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000074 // Advance the simulated clock with a given number of milliseconds or
75 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000076 void AdvanceTimeMilliseconds(int64_t milliseconds);
77 void AdvanceTimeMicroseconds(int64_t microseconds);
Sebastian Jansson4de31152019-06-11 08:52:11 +020078 void AdvanceTime(TimeDelta delta);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000079
80 private:
Sebastian Jansson4de31152019-06-11 08:52:11 +020081 Timestamp time_;
kwibergbfefb032016-05-01 14:53:46 -070082 std::unique_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000083};
84
Nico Weber22f99252019-02-20 10:13:16 -050085} // namespace webrtc
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000086
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020087#endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_