blob: c9940fbe99e439ddebc8c2d3dfada9657dfe861a [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
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080020#elif defined(WEBRTC_POSIX)
Karl Wiberg79eb1d92017-11-08 12:26:07 +010021
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000022#include <sys/time.h>
23#include <time.h>
Karl Wiberg79eb1d92017-11-08 12:26:07 +010024
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080025#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000026
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/criticalsection.h"
Karl Wiberg2b857922018-03-23 14:53:54 +010028#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/timeutils.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000030
31namespace webrtc {
32
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000033class RealTimeClock : public Clock {
34 // Return a timestamp in milliseconds relative to some arbitrary source; the
35 // source is fixed for this clock.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010036 int64_t TimeInMilliseconds() const override { return rtc::TimeMillis(); }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000037
38 // Return a timestamp in microseconds relative to some arbitrary source; the
39 // source is fixed for this clock.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010040 int64_t TimeInMicroseconds() const override { return rtc::TimeMicros(); }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000041
danilchap21dc1892017-03-07 02:51:09 -080042 // Retrieve an NTP absolute timestamp.
43 NtpTime CurrentNtpTime() const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000044 timeval tv = CurrentTimeVal();
45 double microseconds_in_seconds;
danilchap21dc1892017-03-07 02:51:09 -080046 uint32_t seconds;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000047 Adjust(tv, &seconds, &microseconds_in_seconds);
danilchap21dc1892017-03-07 02:51:09 -080048 uint32_t fractions = static_cast<uint32_t>(
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000049 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
danilchap21dc1892017-03-07 02:51:09 -080050 return NtpTime(seconds, fractions);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000051 }
52
53 // Retrieve an NTP absolute timestamp in milliseconds.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000054 int64_t CurrentNtpInMilliseconds() const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000055 timeval tv = CurrentTimeVal();
56 uint32_t seconds;
57 double microseconds_in_seconds;
58 Adjust(tv, &seconds, &microseconds_in_seconds);
59 return 1000 * static_cast<int64_t>(seconds) +
Karl Wiberg79eb1d92017-11-08 12:26:07 +010060 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000061 }
62
63 protected:
64 virtual timeval CurrentTimeVal() const = 0;
65
Karl Wiberg79eb1d92017-11-08 12:26:07 +010066 static void Adjust(const timeval& tv,
67 uint32_t* adjusted_s,
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000068 double* adjusted_us_in_s) {
69 *adjusted_s = tv.tv_sec + kNtpJan1970;
70 *adjusted_us_in_s = tv.tv_usec / 1e6;
71
72 if (*adjusted_us_in_s >= 1) {
73 *adjusted_us_in_s -= 1;
74 ++*adjusted_s;
75 } else if (*adjusted_us_in_s < -1) {
76 *adjusted_us_in_s += 1;
77 --*adjusted_s;
78 }
79 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000080};
81
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080082#if defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000083// TODO(pbos): Consider modifying the implementation to synchronize itself
84// against system time (update ref_point_, make it non-const) periodically to
85// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000086class WindowsRealTimeClock : public RealTimeClock {
87 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000088 WindowsRealTimeClock()
89 : last_time_ms_(0),
90 num_timer_wraps_(0),
91 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000092
Mirko Bonadeic14d9bb2018-07-16 15:44:28 +020093 ~WindowsRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000094
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000095 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000096 struct ReferencePoint {
97 FILETIME file_time;
98 LARGE_INTEGER counter_ms;
99 };
100
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 timeval CurrentTimeVal() const override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000102 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000103
104 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000105 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000106 struct timeval tv;
107
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000108 // We can't use query performance counter since they can change depending on
109 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000110 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000111
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100112 Time = (((uint64_t)StartTime.dwHighDateTime) << 32) +
113 (uint64_t)StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000114
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000115 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000116 Time -= FILETIME_1970;
117
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000118 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
119 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000120 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000121 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000122
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000123 void GetTime(FILETIME* current_time) const {
124 DWORD t;
125 LARGE_INTEGER elapsed_ms;
126 {
127 rtc::CritScope lock(&crit_);
128 // time MUST be fetched inside the critical section to avoid non-monotonic
129 // last_time_ms_ values that'll register as incorrect wraparounds due to
130 // concurrent calls to GetTime.
131 t = timeGetTime();
132 if (t < last_time_ms_)
133 num_timer_wraps_++;
134 last_time_ms_ = t;
135 elapsed_ms.HighPart = num_timer_wraps_;
136 }
137 elapsed_ms.LowPart = t;
138 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
139
140 // Translate to 100-nanoseconds intervals (FILETIME resolution)
141 // and add to reference FILETIME to get current FILETIME.
142 ULARGE_INTEGER filetime_ref_as_ul;
143 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
144 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
145 filetime_ref_as_ul.QuadPart +=
146 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
147
148 // Copy to result
149 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
150 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
151 }
152
153 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700154 ReferencePoint ref = {};
155 FILETIME ft0 = {};
156 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000157 // Spin waiting for a change in system time. As soon as this change happens,
158 // get the matching call for timeGetTime() as soon as possible. This is
159 // assumed to be the most accurate offset that we can get between
160 // timeGetTime() and system time.
161
162 // Set timer accuracy to 1 ms.
163 timeBeginPeriod(1);
164 GetSystemTimeAsFileTime(&ft0);
165 do {
166 GetSystemTimeAsFileTime(&ft1);
167
168 ref.counter_ms.QuadPart = timeGetTime();
169 Sleep(0);
170 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
171 (ft0.dwLowDateTime == ft1.dwLowDateTime));
172 ref.file_time = ft1;
173 timeEndPeriod(1);
174 return ref;
175 }
176
177 // mutable as time-accessing functions are const.
pbos5ad935c2016-01-25 03:52:44 -0800178 rtc::CriticalSection crit_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000179 mutable DWORD last_time_ms_;
180 mutable LONG num_timer_wraps_;
181 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000182};
183
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800184#elif defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000185class UnixRealTimeClock : public RealTimeClock {
186 public:
187 UnixRealTimeClock() {}
188
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000189 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000190
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000191 protected:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 timeval CurrentTimeVal() const override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000193 struct timeval tv;
194 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000195 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000196 tz.tz_dsttime = 0;
197 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000198 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000199 }
200};
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800201#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000202
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800203#if defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000204static WindowsRealTimeClock* volatile g_shared_clock = nullptr;
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800205#endif // defined(WEBRTC_WIN)
206
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000207Clock* Clock::GetRealTimeClock() {
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800208#if defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000209 // This read relies on volatile read being atomic-load-acquire. This is
210 // true in MSVC since at least 2005:
211 // "A read of a volatile object (volatile read) has Acquire semantics"
212 if (g_shared_clock != nullptr)
213 return g_shared_clock;
214 WindowsRealTimeClock* clock = new WindowsRealTimeClock;
215 if (InterlockedCompareExchangePointer(
216 reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) !=
217 nullptr) {
218 // g_shared_clock was assigned while we constructed/tried to assign our
219 // instance, delete our instance and use the existing one.
220 delete clock;
221 }
222 return g_shared_clock;
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800223#elif defined(WEBRTC_POSIX)
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000224 static UnixRealTimeClock clock;
225 return &clock;
Yves Gerey665174f2018-06-19 15:03:05 +0200226#else // defined(WEBRTC_POSIX)
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800227 return nullptr;
228#endif // !defined(WEBRTC_WIN) || defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000229}
230
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000231SimulatedClock::SimulatedClock(int64_t initial_time_us)
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100232 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {}
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000233
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100234SimulatedClock::~SimulatedClock() {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000235
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000236int64_t SimulatedClock::TimeInMilliseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000237 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000238 return (time_us_ + 500) / 1000;
239}
240
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000241int64_t SimulatedClock::TimeInMicroseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000242 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000243 return time_us_;
244}
245
danilchap21dc1892017-03-07 02:51:09 -0800246NtpTime SimulatedClock::CurrentNtpTime() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000247 int64_t now_ms = TimeInMilliseconds();
danilchap21dc1892017-03-07 02:51:09 -0800248 uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
249 uint32_t fractions =
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000250 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
danilchap21dc1892017-03-07 02:51:09 -0800251 return NtpTime(seconds, fractions);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000252}
253
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000254int64_t SimulatedClock::CurrentNtpInMilliseconds() const {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000255 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
256}
257
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000258void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
259 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000260}
261
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000262void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000263 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000264 time_us_ += microseconds;
265}
266
267}; // namespace webrtc