blob: f31556fc4ff38b1073877bdd2ea8ee264276dfec [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
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000015#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
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000022#include "webrtc/base/criticalsection.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020023#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
danilchap37953762017-02-09 11:15:25 -080028NtpTime Clock::CurrentNtpTime() const {
29 uint32_t seconds;
30 uint32_t fractions;
31 CurrentNtp(seconds, fractions);
32 return NtpTime(seconds, fractions);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000033}
34
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000035class RealTimeClock : public Clock {
36 // Return a timestamp in milliseconds relative to some arbitrary source; the
37 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000038 int64_t TimeInMilliseconds() const override {
Niels Möllerd28db7f2016-05-10 16:31:47 +020039 return rtc::TimeMillis();
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000040 }
41
42 // Return a timestamp in microseconds relative to some arbitrary source; the
43 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000044 int64_t TimeInMicroseconds() const override {
Niels Möllerd28db7f2016-05-10 16:31:47 +020045 return rtc::TimeMicros();
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000046 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000047
48 // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000049 void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000050 timeval tv = CurrentTimeVal();
51 double microseconds_in_seconds;
52 Adjust(tv, &seconds, &microseconds_in_seconds);
53 fractions = static_cast<uint32_t>(
54 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5);
55 }
56
57 // Retrieve an NTP absolute timestamp in milliseconds.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int64_t CurrentNtpInMilliseconds() const override {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000059 timeval tv = CurrentTimeVal();
60 uint32_t seconds;
61 double microseconds_in_seconds;
62 Adjust(tv, &seconds, &microseconds_in_seconds);
63 return 1000 * static_cast<int64_t>(seconds) +
64 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5);
65 }
66
67 protected:
68 virtual timeval CurrentTimeVal() const = 0;
69
70 static void Adjust(const timeval& tv, uint32_t* adjusted_s,
71 double* adjusted_us_in_s) {
72 *adjusted_s = tv.tv_sec + kNtpJan1970;
73 *adjusted_us_in_s = tv.tv_usec / 1e6;
74
75 if (*adjusted_us_in_s >= 1) {
76 *adjusted_us_in_s -= 1;
77 ++*adjusted_s;
78 } else if (*adjusted_us_in_s < -1) {
79 *adjusted_us_in_s += 1;
80 --*adjusted_s;
81 }
82 }
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000083};
84
85#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000086// TODO(pbos): Consider modifying the implementation to synchronize itself
87// against system time (update ref_point_, make it non-const) periodically to
88// prevent clock drift.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000089class WindowsRealTimeClock : public RealTimeClock {
90 public:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000091 WindowsRealTimeClock()
92 : last_time_ms_(0),
93 num_timer_wraps_(0),
94 ref_point_(GetSystemReferencePoint()) {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000095
96 virtual ~WindowsRealTimeClock() {}
97
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000098 protected:
pbos@webrtc.orgf9389222015-01-21 12:51:13 +000099 struct ReferencePoint {
100 FILETIME file_time;
101 LARGE_INTEGER counter_ms;
102 };
103
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 timeval CurrentTimeVal() const override {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000105 const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000106
107 FILETIME StartTime;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000108 uint64_t Time;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000109 struct timeval tv;
110
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000111 // We can't use query performance counter since they can change depending on
112 // speed stepping.
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000113 GetTime(&StartTime);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000114
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000115 Time = (((uint64_t) StartTime.dwHighDateTime) << 32) +
116 (uint64_t) StartTime.dwLowDateTime;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000117
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000118 // Convert the hecto-nano second time to tv format.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000119 Time -= FILETIME_1970;
120
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000121 tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000);
122 tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000123 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000124 }
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000125
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000126 void GetTime(FILETIME* current_time) const {
127 DWORD t;
128 LARGE_INTEGER elapsed_ms;
129 {
130 rtc::CritScope lock(&crit_);
131 // time MUST be fetched inside the critical section to avoid non-monotonic
132 // last_time_ms_ values that'll register as incorrect wraparounds due to
133 // concurrent calls to GetTime.
134 t = timeGetTime();
135 if (t < last_time_ms_)
136 num_timer_wraps_++;
137 last_time_ms_ = t;
138 elapsed_ms.HighPart = num_timer_wraps_;
139 }
140 elapsed_ms.LowPart = t;
141 elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart;
142
143 // Translate to 100-nanoseconds intervals (FILETIME resolution)
144 // and add to reference FILETIME to get current FILETIME.
145 ULARGE_INTEGER filetime_ref_as_ul;
146 filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime;
147 filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime;
148 filetime_ref_as_ul.QuadPart +=
149 static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10);
150
151 // Copy to result
152 current_time->dwHighDateTime = filetime_ref_as_ul.HighPart;
153 current_time->dwLowDateTime = filetime_ref_as_ul.LowPart;
154 }
155
156 static ReferencePoint GetSystemReferencePoint() {
dchenga771bf82015-07-01 17:52:10 -0700157 ReferencePoint ref = {};
158 FILETIME ft0 = {};
159 FILETIME ft1 = {};
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000160 // Spin waiting for a change in system time. As soon as this change happens,
161 // get the matching call for timeGetTime() as soon as possible. This is
162 // assumed to be the most accurate offset that we can get between
163 // timeGetTime() and system time.
164
165 // Set timer accuracy to 1 ms.
166 timeBeginPeriod(1);
167 GetSystemTimeAsFileTime(&ft0);
168 do {
169 GetSystemTimeAsFileTime(&ft1);
170
171 ref.counter_ms.QuadPart = timeGetTime();
172 Sleep(0);
173 } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) &&
174 (ft0.dwLowDateTime == ft1.dwLowDateTime));
175 ref.file_time = ft1;
176 timeEndPeriod(1);
177 return ref;
178 }
179
180 // mutable as time-accessing functions are const.
pbos5ad935c2016-01-25 03:52:44 -0800181 rtc::CriticalSection crit_;
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000182 mutable DWORD last_time_ms_;
183 mutable LONG num_timer_wraps_;
184 const ReferencePoint ref_point_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000185};
186
187#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
188class UnixRealTimeClock : public RealTimeClock {
189 public:
190 UnixRealTimeClock() {}
191
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 ~UnixRealTimeClock() override {}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000193
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000194 protected:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 timeval CurrentTimeVal() const override {
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000196 struct timeval tv;
197 struct timezone tz;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000198 tz.tz_minuteswest = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000199 tz.tz_dsttime = 0;
200 gettimeofday(&tv, &tz);
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000201 return tv;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000202 }
203};
204#endif
205
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000206#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000207static WindowsRealTimeClock* volatile g_shared_clock = nullptr;
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000208#endif
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000209Clock* Clock::GetRealTimeClock() {
210#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000211 // This read relies on volatile read being atomic-load-acquire. This is
212 // true in MSVC since at least 2005:
213 // "A read of a volatile object (volatile read) has Acquire semantics"
214 if (g_shared_clock != nullptr)
215 return g_shared_clock;
216 WindowsRealTimeClock* clock = new WindowsRealTimeClock;
217 if (InterlockedCompareExchangePointer(
218 reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) !=
219 nullptr) {
220 // g_shared_clock was assigned while we constructed/tried to assign our
221 // instance, delete our instance and use the existing one.
222 delete clock;
223 }
224 return g_shared_clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000225#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000226 static UnixRealTimeClock clock;
227 return &clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000228#else
229 return NULL;
230#endif
231}
232
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000233SimulatedClock::SimulatedClock(int64_t initial_time_us)
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000234 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {
235}
236
237SimulatedClock::~SimulatedClock() {
238}
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000239
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000240int64_t SimulatedClock::TimeInMilliseconds() 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_ + 500) / 1000;
243}
244
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000245int64_t SimulatedClock::TimeInMicroseconds() const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000246 ReadLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000247 return time_us_;
248}
249
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000250void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) const {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000251 int64_t now_ms = TimeInMilliseconds();
252 seconds = (now_ms / 1000) + kNtpJan1970;
253 fractions =
254 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000255}
256
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +0000257int64_t SimulatedClock::CurrentNtpInMilliseconds() const {
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000258 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
259}
260
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000261void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) {
262 AdvanceTimeMicroseconds(1000 * milliseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000263}
264
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000265void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) {
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000266 WriteLockScoped synchronize(*lock_);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000267 time_us_ += microseconds;
268}
269
270}; // namespace webrtc