blob: f69d13c1db329914d5eb325663707c53611408ad [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 {
35 // Return a timestamp in milliseconds relative to some arbitrary source; the
36 // source is fixed for this clock.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010037 int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000038
39 // Return a timestamp in microseconds relative to some arbitrary source; the
40 // source is fixed for this clock.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010041 int64_t TimeInMicroseconds() override { return rtc::TimeMicros(); }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000042
danilchap21dc1892017-03-07 02:51:09 -080043 // Retrieve an NTP absolute timestamp.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010044 NtpTime CurrentNtpTime() override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000045 timeval tv = CurrentTimeVal();
46 double microseconds_in_seconds;
danilchap21dc1892017-03-07 02:51:09 -080047 uint32_t seconds;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000048 Adjust(tv, &seconds, &microseconds_in_seconds);
danilchap21dc1892017-03-07 02:51:09 -080049 uint32_t fractions = static_cast<uint32_t>(
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000050 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
danilchap21dc1892017-03-07 02:51:09 -080051 return NtpTime(seconds, fractions);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000052 }
53
54 // Retrieve an NTP absolute timestamp in milliseconds.
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010055 int64_t CurrentNtpInMilliseconds() override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000056 timeval tv = CurrentTimeVal();
57 uint32_t seconds;
58 double microseconds_in_seconds;
59 Adjust(tv, &seconds, &microseconds_in_seconds);
60 return 1000 * static_cast<int64_t>(seconds) +
Karl Wiberg79eb1d92017-11-08 12:26:07 +010061 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000062 }
63
64 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010065 virtual timeval CurrentTimeVal() = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000066
Karl Wiberg79eb1d92017-11-08 12:26:07 +010067 static void Adjust(const timeval& tv,
68 uint32_t* adjusted_s,
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000069 double* adjusted_us_in_s) {
70 *adjusted_s = tv.tv_sec + kNtpJan1970;
71 *adjusted_us_in_s = tv.tv_usec / 1e6;
72
73 if (*adjusted_us_in_s >= 1) {
74 *adjusted_us_in_s -= 1;
75 ++*adjusted_s;
76 } else if (*adjusted_us_in_s < -1) {
77 *adjusted_us_in_s += 1;
78 --*adjusted_s;
79 }
80 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000081};
82
Robin Raymondce1b1402018-11-22 20:10:11 -050083#if defined(WINUWP)
84class WinUwpRealTimeClock final : public RealTimeClock {
85 public:
86 WinUwpRealTimeClock() = default;
87 ~WinUwpRealTimeClock() override {}
88
89 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010090 timeval CurrentTimeVal() override {
Robin Raymondce1b1402018-11-22 20:10:11 -050091 // The rtc::SystemTimeNanos() method is already time offset from a base
92 // epoch value and might as be synchronized against an NTP time server as
93 // an added bonus.
94 auto nanos = rtc::SystemTimeNanos();
95
96 struct timeval tv;
97
98 tv.tv_sec = rtc::dchecked_cast<long>(nanos / 1000000000);
99 tv.tv_usec = rtc::dchecked_cast<long>(nanos / 1000);
100
101 return tv;
102 }
103};
104
105#elif defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000106// TODO(pbos): Consider modifying the implementation to synchronize itself
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100107// against system time (update ref_point_) periodically to
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000108// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000109class WindowsRealTimeClock : public RealTimeClock {
110 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000111 WindowsRealTimeClock()
112 : last_time_ms_(0),
113 num_timer_wraps_(0),
114 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000115
Mirko Bonadeic14d9bb2018-07-16 15:44:28 +0200116 ~WindowsRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000117
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000118 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000119 struct ReferencePoint {
120 FILETIME file_time;
121 LARGE_INTEGER counter_ms;
122 };
123
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100124 timeval CurrentTimeVal() override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000125 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000126
127 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000128 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000129 struct timeval tv;
130
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000131 // We can't use query performance counter since they can change depending on
132 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000133 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000134
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100135 Time = (((uint64_t)StartTime.dwHighDateTime) << 32) +
136 (uint64_t)StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000137
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000138 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000139 Time -= FILETIME_1970;
140
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000141 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
142 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000143 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000144 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000145
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100146 void GetTime(FILETIME* current_time) {
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000147 DWORD t;
148 LARGE_INTEGER elapsed_ms;
149 {
150 rtc::CritScope lock(&crit_);
151 // time MUST be fetched inside the critical section to avoid non-monotonic
152 // last_time_ms_ values that'll register as incorrect wraparounds due to
153 // concurrent calls to GetTime.
154 t = timeGetTime();
155 if (t < last_time_ms_)
156 num_timer_wraps_++;
157 last_time_ms_ = t;
158 elapsed_ms.HighPart = num_timer_wraps_;
159 }
160 elapsed_ms.LowPart = t;
161 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
162
163 // Translate to 100-nanoseconds intervals (FILETIME resolution)
164 // and add to reference FILETIME to get current FILETIME.
165 ULARGE_INTEGER filetime_ref_as_ul;
166 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
167 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
168 filetime_ref_as_ul.QuadPart +=
169 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
170
171 // Copy to result
172 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
173 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
174 }
175
176 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700177 ReferencePoint ref = {};
178 FILETIME ft0 = {};
179 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000180 // Spin waiting for a change in system time. As soon as this change happens,
181 // get the matching call for timeGetTime() as soon as possible. This is
182 // assumed to be the most accurate offset that we can get between
183 // timeGetTime() and system time.
184
185 // Set timer accuracy to 1 ms.
186 timeBeginPeriod(1);
187 GetSystemTimeAsFileTime(&ft0);
188 do {
189 GetSystemTimeAsFileTime(&ft1);
190
191 ref.counter_ms.QuadPart = timeGetTime();
192 Sleep(0);
193 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
194 (ft0.dwLowDateTime == ft1.dwLowDateTime));
195 ref.file_time = ft1;
196 timeEndPeriod(1);
197 return ref;
198 }
199
pbos5ad935c2016-01-25 03:52:44 -0800200 rtc::CriticalSection crit_;
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100201 DWORD last_time_ms_;
202 LONG num_timer_wraps_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000203 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000204};
205
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800206#elif defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000207class UnixRealTimeClock : public RealTimeClock {
208 public:
209 UnixRealTimeClock() {}
210
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000211 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000212
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000213 protected:
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100214 timeval CurrentTimeVal() override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000215 struct timeval tv;
216 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000217 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000218 tz.tz_dsttime = 0;
219 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000220 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000221 }
222};
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800223#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000224
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000225Clock* Clock::GetRealTimeClock() {
Robin Raymondce1b1402018-11-22 20:10:11 -0500226#if defined(WINUWP)
227 static Clock* const clock = new WinUwpRealTimeClock();
228#elif defined(WEBRTC_WIN)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200229 static Clock* const clock = new WindowsRealTimeClock();
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800230#elif defined(WEBRTC_POSIX)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200231 static Clock* const clock = new UnixRealTimeClock();
232#else
233 static Clock* const clock = nullptr;
234#endif
235 return clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000236}
237
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000238SimulatedClock::SimulatedClock(int64_t initial_time_us)
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100239 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {}
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000240
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100241SimulatedClock::~SimulatedClock() {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000242
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100243int64_t SimulatedClock::TimeInMilliseconds() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000244 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000245 return (time_us_ + 500) / 1000;
246}
247
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100248int64_t SimulatedClock::TimeInMicroseconds() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000249 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000250 return time_us_;
251}
252
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100253NtpTime SimulatedClock::CurrentNtpTime() {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000254 int64_t now_ms = TimeInMilliseconds();
danilchap21dc1892017-03-07 02:51:09 -0800255 uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
256 uint32_t fractions =
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000257 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
danilchap21dc1892017-03-07 02:51:09 -0800258 return NtpTime(seconds, fractions);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000259}
260
Sebastian Jansson2a96ab22019-01-30 20:44:45 +0100261int64_t SimulatedClock::CurrentNtpInMilliseconds() {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000262 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
263}
264
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000265void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
266 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000267}
268
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000269void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000270 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000271 time_us_ += microseconds;
272}
273
Nico Weber22f99252019-02-20 10:13:16 -0500274} // namespace webrtc