blob: 4f5d9cff1f3a3a73c39abb32adf7713da9b0e586 [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
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/criticalsection.h"
21
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/timeutils.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.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010037 int64_t TimeInMilliseconds() const 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.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010041 int64_t TimeInMicroseconds() const 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.
44 NtpTime CurrentNtpTime() const 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.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000055 int64_t CurrentNtpInMilliseconds() const 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:
65 virtual timeval CurrentTimeVal() const = 0;
66
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
Sergey Ulanov6acefdb2017-12-11 17:38:13 -080083#if defined(WEBRTC_WIN)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000084// TODO(pbos): Consider modifying the implementation to synchronize itself
85// against system time (update ref_point_, make it non-const) periodically to
86// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000087class WindowsRealTimeClock : public RealTimeClock {
88 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000089 WindowsRealTimeClock()
90 : last_time_ms_(0),
91 num_timer_wraps_(0),
92 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000093
Mirko Bonadeic14d9bb2018-07-16 15:44:28 +020094 ~WindowsRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000095
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000096 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000097 struct ReferencePoint {
98 FILETIME file_time;
99 LARGE_INTEGER counter_ms;
100 };
101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 timeval CurrentTimeVal() const override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000103 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000104
105 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000106 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000107 struct timeval tv;
108
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000109 // We can't use query performance counter since they can change depending on
110 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000111 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000112
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100113 Time = (((uint64_t)StartTime.dwHighDateTime) << 32) +
114 (uint64_t)StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000115
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000116 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000117 Time -= FILETIME_1970;
118
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000119 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
120 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000121 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000122 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000123
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000124 void GetTime(FILETIME* current_time) const {
125 DWORD t;
126 LARGE_INTEGER elapsed_ms;
127 {
128 rtc::CritScope lock(&crit_);
129 // time MUST be fetched inside the critical section to avoid non-monotonic
130 // last_time_ms_ values that'll register as incorrect wraparounds due to
131 // concurrent calls to GetTime.
132 t = timeGetTime();
133 if (t < last_time_ms_)
134 num_timer_wraps_++;
135 last_time_ms_ = t;
136 elapsed_ms.HighPart = num_timer_wraps_;
137 }
138 elapsed_ms.LowPart = t;
139 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
140
141 // Translate to 100-nanoseconds intervals (FILETIME resolution)
142 // and add to reference FILETIME to get current FILETIME.
143 ULARGE_INTEGER filetime_ref_as_ul;
144 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
145 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
146 filetime_ref_as_ul.QuadPart +=
147 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
148
149 // Copy to result
150 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
151 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
152 }
153
154 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700155 ReferencePoint ref = {};
156 FILETIME ft0 = {};
157 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000158 // Spin waiting for a change in system time. As soon as this change happens,
159 // get the matching call for timeGetTime() as soon as possible. This is
160 // assumed to be the most accurate offset that we can get between
161 // timeGetTime() and system time.
162
163 // Set timer accuracy to 1 ms.
164 timeBeginPeriod(1);
165 GetSystemTimeAsFileTime(&ft0);
166 do {
167 GetSystemTimeAsFileTime(&ft1);
168
169 ref.counter_ms.QuadPart = timeGetTime();
170 Sleep(0);
171 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
172 (ft0.dwLowDateTime == ft1.dwLowDateTime));
173 ref.file_time = ft1;
174 timeEndPeriod(1);
175 return ref;
176 }
177
178 // mutable as time-accessing functions are const.
pbos5ad935c2016-01-25 03:52:44 -0800179 rtc::CriticalSection crit_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000180 mutable DWORD last_time_ms_;
181 mutable LONG num_timer_wraps_;
182 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000183};
184
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800185#elif defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000186class UnixRealTimeClock : public RealTimeClock {
187 public:
188 UnixRealTimeClock() {}
189
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000191
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000192 protected:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 timeval CurrentTimeVal() const override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000194 struct timeval tv;
195 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000196 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000197 tz.tz_dsttime = 0;
198 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000199 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000200 }
201};
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800202#endif // defined(WEBRTC_POSIX)
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000203
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000204Clock* Clock::GetRealTimeClock() {
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800205#if defined(WEBRTC_WIN)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200206 static Clock* const clock = new WindowsRealTimeClock();
Sergey Ulanov6acefdb2017-12-11 17:38:13 -0800207#elif defined(WEBRTC_POSIX)
Mirko Bonadei6c092d22018-09-10 13:27:11 +0200208 static Clock* const clock = new UnixRealTimeClock();
209#else
210 static Clock* const clock = nullptr;
211#endif
212 return clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000213}
214
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000215SimulatedClock::SimulatedClock(int64_t initial_time_us)
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100216 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {}
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000217
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100218SimulatedClock::~SimulatedClock() {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000219
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000220int64_t SimulatedClock::TimeInMilliseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000221 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000222 return (time_us_ + 500) / 1000;
223}
224
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000225int64_t SimulatedClock::TimeInMicroseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000226 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000227 return time_us_;
228}
229
danilchap21dc1892017-03-07 02:51:09 -0800230NtpTime SimulatedClock::CurrentNtpTime() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000231 int64_t now_ms = TimeInMilliseconds();
danilchap21dc1892017-03-07 02:51:09 -0800232 uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
233 uint32_t fractions =
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000234 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
danilchap21dc1892017-03-07 02:51:09 -0800235 return NtpTime(seconds, fractions);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000236}
237
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000238int64_t SimulatedClock::CurrentNtpInMilliseconds() const {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000239 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
240}
241
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000242void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
243 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000244}
245
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000246void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000247 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000248 time_us_ += microseconds;
249}
250
251}; // namespace webrtc