blob: f65adfcbb18ae8438fa14b0a6e10fd98eba15803 [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010011#include "webrtc/system_wrappers/include/clock.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000012
13#if defined(_WIN32)
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000014// Windows needs to be included before mmsystem.h
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020015#include "webrtc/base/win32.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000016#include <MMSystem.h>
17#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
18#include <sys/time.h>
19#include <time.h>
20#endif
21
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020022#include "webrtc/base/criticalsection.h"
23#include "webrtc/base/timeutils.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010024#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000025
26namespace webrtc {
27
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000028class RealTimeClock : public Clock {
29 // Return a timestamp in milliseconds relative to some arbitrary source; the
30 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000031 int64_t TimeInMilliseconds() const override {
Niels Möllerd28db7f2016-05-10 16:31:47 +020032 return rtc::TimeMillis();
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000033 }
34
35 // Return a timestamp in microseconds relative to some arbitrary source; the
36 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 int64_t TimeInMicroseconds() const override {
Niels Möllerd28db7f2016-05-10 16:31:47 +020038 return rtc::TimeMicros();
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000039 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000040
danilchap21dc1892017-03-07 02:51:09 -080041 // Retrieve an NTP absolute timestamp.
42 NtpTime CurrentNtpTime() const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000043 timeval tv = CurrentTimeVal();
44 double microseconds_in_seconds;
danilchap21dc1892017-03-07 02:51:09 -080045 uint32_t seconds;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000046 Adjust(tv, &seconds, &microseconds_in_seconds);
danilchap21dc1892017-03-07 02:51:09 -080047 uint32_t fractions = static_cast<uint32_t>(
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000048 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
danilchap21dc1892017-03-07 02:51:09 -080049 return NtpTime(seconds, fractions);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000050 }
51
52 // Retrieve an NTP absolute timestamp in milliseconds.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000053 int64_t CurrentNtpInMilliseconds() const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000054 timeval tv = CurrentTimeVal();
55 uint32_t seconds;
56 double microseconds_in_seconds;
57 Adjust(tv, &seconds, &microseconds_in_seconds);
58 return 1000 * static_cast<int64_t>(seconds) +
59 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
60 }
61
62 protected:
63 virtual timeval CurrentTimeVal() const = 0;
64
65 static void Adjust(const timeval& tv, uint32_t* adjusted_s,
66 double* adjusted_us_in_s) {
67 *adjusted_s = tv.tv_sec + kNtpJan1970;
68 *adjusted_us_in_s = tv.tv_usec / 1e6;
69
70 if (*adjusted_us_in_s >= 1) {
71 *adjusted_us_in_s -= 1;
72 ++*adjusted_s;
73 } else if (*adjusted_us_in_s < -1) {
74 *adjusted_us_in_s += 1;
75 --*adjusted_s;
76 }
77 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000078};
79
80#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000081// TODO(pbos): Consider modifying the implementation to synchronize itself
82// against system time (update ref_point_, make it non-const) periodically to
83// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000084class WindowsRealTimeClock : public RealTimeClock {
85 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000086 WindowsRealTimeClock()
87 : last_time_ms_(0),
88 num_timer_wraps_(0),
89 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000090
91 virtual ~WindowsRealTimeClock() {}
92
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000093 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000094 struct ReferencePoint {
95 FILETIME file_time;
96 LARGE_INTEGER counter_ms;
97 };
98
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 timeval CurrentTimeVal() const override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000100 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000101
102 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000103 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000104 struct timeval tv;
105
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000106 // We can't use query performance counter since they can change depending on
107 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000108 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000109
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000110 Time = (((uint64_t) StartTime.dwHighDateTime) << 32) +
111 (uint64_t) StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000112
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000113 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000114 Time -= FILETIME_1970;
115
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000116 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
117 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000118 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000119 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000120
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000121 void GetTime(FILETIME* current_time) const {
122 DWORD t;
123 LARGE_INTEGER elapsed_ms;
124 {
125 rtc::CritScope lock(&crit_);
126 // time MUST be fetched inside the critical section to avoid non-monotonic
127 // last_time_ms_ values that'll register as incorrect wraparounds due to
128 // concurrent calls to GetTime.
129 t = timeGetTime();
130 if (t < last_time_ms_)
131 num_timer_wraps_++;
132 last_time_ms_ = t;
133 elapsed_ms.HighPart = num_timer_wraps_;
134 }
135 elapsed_ms.LowPart = t;
136 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
137
138 // Translate to 100-nanoseconds intervals (FILETIME resolution)
139 // and add to reference FILETIME to get current FILETIME.
140 ULARGE_INTEGER filetime_ref_as_ul;
141 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
142 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
143 filetime_ref_as_ul.QuadPart +=
144 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
145
146 // Copy to result
147 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
148 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
149 }
150
151 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700152 ReferencePoint ref = {};
153 FILETIME ft0 = {};
154 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000155 // Spin waiting for a change in system time. As soon as this change happens,
156 // get the matching call for timeGetTime() as soon as possible. This is
157 // assumed to be the most accurate offset that we can get between
158 // timeGetTime() and system time.
159
160 // Set timer accuracy to 1 ms.
161 timeBeginPeriod(1);
162 GetSystemTimeAsFileTime(&ft0);
163 do {
164 GetSystemTimeAsFileTime(&ft1);
165
166 ref.counter_ms.QuadPart = timeGetTime();
167 Sleep(0);
168 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
169 (ft0.dwLowDateTime == ft1.dwLowDateTime));
170 ref.file_time = ft1;
171 timeEndPeriod(1);
172 return ref;
173 }
174
175 // mutable as time-accessing functions are const.
pbos5ad935c2016-01-25 03:52:44 -0800176 rtc::CriticalSection crit_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000177 mutable DWORD last_time_ms_;
178 mutable LONG num_timer_wraps_;
179 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000180};
181
182#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
183class UnixRealTimeClock : public RealTimeClock {
184 public:
185 UnixRealTimeClock() {}
186
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000187 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000188
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000189 protected:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 timeval CurrentTimeVal() const override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000191 struct timeval tv;
192 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000193 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000194 tz.tz_dsttime = 0;
195 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000196 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000197 }
198};
199#endif
200
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000201#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000202static WindowsRealTimeClock* volatile g_shared_clock = nullptr;
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000203#endif
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000204Clock* Clock::GetRealTimeClock() {
205#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000206 // This read relies on volatile read being atomic-load-acquire. This is
207 // true in MSVC since at least 2005:
208 // "A read of a volatile object (volatile read) has Acquire semantics"
209 if (g_shared_clock != nullptr)
210 return g_shared_clock;
211 WindowsRealTimeClock* clock = new WindowsRealTimeClock;
212 if (InterlockedCompareExchangePointer(
213 reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) !=
214 nullptr) {
215 // g_shared_clock was assigned while we constructed/tried to assign our
216 // instance, delete our instance and use the existing one.
217 delete clock;
218 }
219 return g_shared_clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000220#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000221 static UnixRealTimeClock clock;
222 return &clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000223#else
224 return NULL;
225#endif
226}
227
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000228SimulatedClock::SimulatedClock(int64_t initial_time_us)
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000229 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {
230}
231
232SimulatedClock::~SimulatedClock() {
233}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000234
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000235int64_t SimulatedClock::TimeInMilliseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000236 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000237 return (time_us_ + 500) / 1000;
238}
239
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000240int64_t SimulatedClock::TimeInMicroseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000241 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000242 return time_us_;
243}
244
danilchap21dc1892017-03-07 02:51:09 -0800245NtpTime SimulatedClock::CurrentNtpTime() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000246 int64_t now_ms = TimeInMilliseconds();
danilchap21dc1892017-03-07 02:51:09 -0800247 uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
248 uint32_t fractions =
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000249 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
danilchap21dc1892017-03-07 02:51:09 -0800250 return NtpTime(seconds, fractions);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000251}
252
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000253int64_t SimulatedClock::CurrentNtpInMilliseconds() const {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000254 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
255}
256
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000257void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
258 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000259}
260
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000261void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000262 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000263 time_us_ += microseconds;
264}
265
266}; // namespace webrtc