blob: 3eea1557110292eb323d0e9b0462513d5bc52943 [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#include "system_wrappers/include/clock.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000012
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080013#if defined(WEBRTC_WIN)
Karl Wiberg79eb1d92017-11-08 12:26:07 +010014
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000015// Windows needs to be included before mmsystem.h
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/win32.h"
Karl Wiberg79eb1d92017-11-08 12:26:07 +010017
Mirko Bonadei01914412018-03-23 16:30:58 +010018#include <mmsystem.h>
Karl Wiberg79eb1d92017-11-08 12:26:07 +010019
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/critical_section.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080022#elif defined(WEBRTC_POSIX)
Karl Wiberg79eb1d92017-11-08 12:26:07 +010023
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000024#include <sys/time.h>
25#include <time.h>
Karl Wiberg79eb1d92017-11-08 12:26:07 +010026
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080027#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000028
Karl Wiberg2b857922018-03-23 14:53:54 +010029#include "rtc_base/synchronization/rw_lock_wrapper.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000031
32namespace webrtc {
33
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000034class RealTimeClock : public Clock {
Sebastian Jansson4de31152019-06-11 08:52:11 +020035 Timestamp CurrentTime() override { return Timestamp::us(rtc::TimeMicros()); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000036 // Return a timestamp in milliseconds relative to some arbitrary source; the
37 // source is fixed for this clock.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010038 int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000039
40 // Return a timestamp in microseconds relative to some arbitrary source; the
41 // source is fixed for this clock.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010042 int64_t TimeInMicroseconds() override { return rtc::TimeMicros(); }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000043
danilchap21dc1892017-03-07 02:51:09 -080044 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010045 NtpTime CurrentNtpTime() override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000046 timeval tv = CurrentTimeVal();
47 double microseconds_in_seconds;
danilchap21dc1892017-03-07 02:51:09 -080048 uint32_t seconds;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000049 Adjust(tv, &seconds, &microseconds_in_seconds);
danilchap21dc1892017-03-07 02:51:09 -080050 uint32_t fractions = static_cast<uint32_t>(
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000051 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
danilchap21dc1892017-03-07 02:51:09 -080052 return NtpTime(seconds, fractions);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000053 }
54
55 // Retrieve an NTP absolute timestamp in milliseconds.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010056 int64_t CurrentNtpInMilliseconds() override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000057 timeval tv = CurrentTimeVal();
58 uint32_t seconds;
59 double microseconds_in_seconds;
60 Adjust(tv, &seconds, &microseconds_in_seconds);
61 return 1000 * static_cast<int64_t>(seconds) +
Karl Wiberg79eb1d92017-11-08 12:26:07 +010062 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000063 }
64
65 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010066 virtual timeval CurrentTimeVal() = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000067
Karl Wiberg79eb1d92017-11-08 12:26:07 +010068 static void Adjust(const timeval& tv,
69 uint32_t* adjusted_s,
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000070 double* adjusted_us_in_s) {
71 *adjusted_s = tv.tv_sec + kNtpJan1970;
72 *adjusted_us_in_s = tv.tv_usec / 1e6;
73
74 if (*adjusted_us_in_s >= 1) {
75 *adjusted_us_in_s -= 1;
76 ++*adjusted_s;
77 } else if (*adjusted_us_in_s < -1) {
78 *adjusted_us_in_s += 1;
79 --*adjusted_s;
80 }
81 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000082};
83
Robin Raymondce1b1402018-11-22 20:10:11 -050084#if defined(WINUWP)
85class WinUwpRealTimeClock final : public RealTimeClock {
86 public:
87 WinUwpRealTimeClock() = default;
88 ~WinUwpRealTimeClock() override {}
89
90 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010091 timeval CurrentTimeVal() override {
Robin Raymondce1b1402018-11-22 20:10:11 -050092 // The rtc::SystemTimeNanos() method is already time offset from a base
93 // epoch value and might as be synchronized against an NTP time server as
94 // an added bonus.
95 auto nanos = rtc::SystemTimeNanos();
96
97 struct timeval tv;
98
99 tv.tv_sec = rtc::dchecked_cast<long>(nanos / 1000000000);
100 tv.tv_usec = rtc::dchecked_cast<long>(nanos / 1000);
101
102 return tv;
103 }
104};
105
106#elif defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000107// TODO(pbos): Consider modifying the implementation to synchronize itself
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100108// against system time (update ref_point_) periodically to
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000109// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000110class WindowsRealTimeClock : public RealTimeClock {
111 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000112 WindowsRealTimeClock()
113 : last_time_ms_(0),
114 num_timer_wraps_(0),
115 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000116
Mirko Bonadeic14d9bb2018-07-16 15:44:28 +0200117 ~WindowsRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000118
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000119 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000120 struct ReferencePoint {
121 FILETIME file_time;
122 LARGE_INTEGER counter_ms;
123 };
124
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100125 timeval CurrentTimeVal() override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000126 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000127
128 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000129 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000130 struct timeval tv;
131
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000132 // We can't use query performance counter since they can change depending on
133 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000134 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000135
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100136 Time = (((uint64_t)StartTime.dwHighDateTime) << 32) +
137 (uint64_t)StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000138
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000139 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000140 Time -= FILETIME_1970;
141
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000142 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
143 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000144 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000145 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000146
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100147 void GetTime(FILETIME* current_time) {
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000148 DWORD t;
149 LARGE_INTEGER elapsed_ms;
150 {
151 rtc::CritScope lock(&crit_);
152 // time MUST be fetched inside the critical section to avoid non-monotonic
153 // last_time_ms_ values that'll register as incorrect wraparounds due to
154 // concurrent calls to GetTime.
155 t = timeGetTime();
156 if (t < last_time_ms_)
157 num_timer_wraps_++;
158 last_time_ms_ = t;
159 elapsed_ms.HighPart = num_timer_wraps_;
160 }
161 elapsed_ms.LowPart = t;
162 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
163
164 // Translate to 100-nanoseconds intervals (FILETIME resolution)
165 // and add to reference FILETIME to get current FILETIME.
166 ULARGE_INTEGER filetime_ref_as_ul;
167 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
168 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
169 filetime_ref_as_ul.QuadPart +=
170 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
171
172 // Copy to result
173 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
174 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
175 }
176
177 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700178 ReferencePoint ref = {};
179 FILETIME ft0 = {};
180 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000181 // Spin waiting for a change in system time. As soon as this change happens,
182 // get the matching call for timeGetTime() as soon as possible. This is
183 // assumed to be the most accurate offset that we can get between
184 // timeGetTime() and system time.
185
186 // Set timer accuracy to 1 ms.
187 timeBeginPeriod(1);
188 GetSystemTimeAsFileTime(&ft0);
189 do {
190 GetSystemTimeAsFileTime(&ft1);
191
192 ref.counter_ms.QuadPart = timeGetTime();
193 Sleep(0);
194 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
195 (ft0.dwLowDateTime == ft1.dwLowDateTime));
196 ref.file_time = ft1;
197 timeEndPeriod(1);
198 return ref;
199 }
200
pbos5ad935c2016-01-25 03:52:44 -0800201 rtc::CriticalSection crit_;
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100202 DWORD last_time_ms_;
203 LONG num_timer_wraps_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000204 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000205};
206
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800207#elif defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000208class UnixRealTimeClock : public RealTimeClock {
209 public:
210 UnixRealTimeClock() {}
211
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000212 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000213
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000214 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100215 timeval CurrentTimeVal() override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000216 struct timeval tv;
217 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000218 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000219 tz.tz_dsttime = 0;
220 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000221 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000222 }
223};
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800224#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000225
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000226Clock* Clock::GetRealTimeClock() {
Robin Raymondce1b1402018-11-22 20:10:11 -0500227#if defined(WINUWP)
228 static Clock* const clock = new WinUwpRealTimeClock();
229#elif defined(WEBRTC_WIN)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200230 static Clock* const clock = new WindowsRealTimeClock();
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800231#elif defined(WEBRTC_POSIX)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200232 static Clock* const clock = new UnixRealTimeClock();
233#else
234 static Clock* const clock = nullptr;
235#endif
236 return clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000237}
238
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000239SimulatedClock::SimulatedClock(int64_t initial_time_us)
Sebastian Jansson4de31152019-06-11 08:52:11 +0200240 : SimulatedClock(Timestamp::us(initial_time_us)) {}
241
242SimulatedClock::SimulatedClock(Timestamp initial_time)
243 : time_(initial_time), lock_(RWLockWrapper::CreateRWLock()) {}
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000244
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100245SimulatedClock::~SimulatedClock() {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000246
Sebastian Jansson4de31152019-06-11 08:52:11 +0200247Timestamp SimulatedClock::CurrentTime() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000248 ReadLockScoped synchronize(*lock_);
Sebastian Jansson4de31152019-06-11 08:52:11 +0200249 return time_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000250}
251
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100252NtpTime SimulatedClock::CurrentNtpTime() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000253 int64_t now_ms = TimeInMilliseconds();
danilchap21dc1892017-03-07 02:51:09 -0800254 uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
255 uint32_t fractions =
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000256 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
danilchap21dc1892017-03-07 02:51:09 -0800257 return NtpTime(seconds, fractions);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000258}
259
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100260int64_t SimulatedClock::CurrentNtpInMilliseconds() {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000261 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
262}
263
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000264void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
Sebastian Jansson4de31152019-06-11 08:52:11 +0200265 AdvanceTime(TimeDelta::ms(milliseconds));
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000266}
267
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000268void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
Sebastian Jansson4de31152019-06-11 08:52:11 +0200269 AdvanceTime(TimeDelta::us(microseconds));
270}
271
272void SimulatedClock::AdvanceTime(TimeDelta delta) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000273 WriteLockScoped synchronize(*lock_);
Sebastian Jansson4de31152019-06-11 08:52:11 +0200274 time_ += delta;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000275}
276
Nico Weber22f99252019-02-20 10:13:16 -0500277} // namespace webrtc