blob: 271291c214526a78419d9d6f8f72c5b858cecff5 [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
Niels Möller9308b7a2020-11-03 13:55:44 +010016#include <atomic>
kwibergbfefb032016-05-01 14:53:46 -070017#include <memory>
18
Sebastian Jansson4de31152019-06-11 08:52:11 +020019#include "api/units/timestamp.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() {}
Paul Hallak704d6e52021-04-08 14:57:45 +020035
Sebastian Jansson4de31152019-06-11 08:52:11 +020036 // Return a timestamp relative to an unspecified epoch.
Paul Hallak0de1ed02021-05-19 14:36:50 +020037 virtual Timestamp CurrentTime() = 0;
38 int64_t TimeInMilliseconds() { return CurrentTime().ms(); }
39 int64_t TimeInMicroseconds() { return CurrentTime().us(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000040
Paul Hallak704d6e52021-04-08 14:57:45 +020041 // Retrieve an NTP absolute timestamp (with an epoch of Jan 1, 1900).
Paul Hallakb59e9042021-05-20 17:21:49 +020042 // TODO(bugs.webrtc.org/11327): Make this non-virtual once
43 // "WebRTC-SystemIndependentNtpTimeKillSwitch" is removed.
44 virtual NtpTime CurrentNtpTime() {
45 return ConvertTimestampToNtpTime(CurrentTime());
46 }
Paul Hallak0de1ed02021-05-19 14:36:50 +020047 int64_t CurrentNtpInMilliseconds() { return CurrentNtpTime().ToMs(); }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000048
Paul Hallakb59e9042021-05-20 17:21:49 +020049 // Converts between a relative timestamp returned by this clock, to NTP time.
Paul Hallak2491dbd2021-05-21 07:31:52 +020050 virtual NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) = 0;
Paul Hallakb59e9042021-05-20 17:21:49 +020051 int64_t ConvertTimestampToNtpTimeInMilliseconds(int64_t timestamp_ms) {
52 return ConvertTimestampToNtpTime(Timestamp::Millis(timestamp_ms)).ToMs();
53 }
54
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000055 // Returns an instance of the real-time system clock implementation.
56 static Clock* GetRealTimeClock();
57};
58
59class SimulatedClock : public Clock {
60 public:
Paul Hallak704d6e52021-04-08 14:57:45 +020061 // The constructors assume an epoch of Jan 1, 1970.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000062 explicit SimulatedClock(int64_t initial_time_us);
Sebastian Jansson4de31152019-06-11 08:52:11 +020063 explicit SimulatedClock(Timestamp initial_time);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000065
Paul Hallak704d6e52021-04-08 14:57:45 +020066 // Return a timestamp with an epoch of Jan 1, 1970.
Sebastian Jansson4de31152019-06-11 08:52:11 +020067 Timestamp CurrentTime() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000068
Paul Hallakb59e9042021-05-20 17:21:49 +020069 NtpTime ConvertTimestampToNtpTime(Timestamp timestamp) override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000070
71 // Advance the simulated clock with a given number of milliseconds or
72 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000073 void AdvanceTimeMilliseconds(int64_t milliseconds);
74 void AdvanceTimeMicroseconds(int64_t microseconds);
Sebastian Jansson4de31152019-06-11 08:52:11 +020075 void AdvanceTime(TimeDelta delta);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000076
77 private:
Niels Möller9308b7a2020-11-03 13:55:44 +010078 // The time is read and incremented with relaxed order. Each thread will see
79 // monotonically increasing time, and when threads post tasks or messages to
80 // one another, the synchronization done as part of the message passing should
81 // ensure that any causual chain of events on multiple threads also
82 // corresponds to monotonically increasing time.
83 std::atomic<int64_t> time_us_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000084};
85
Nico Weber22f99252019-02-20 10:13:16 -050086} // namespace webrtc
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000087
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_