blob: fc863552dea1d704df62048dcdf73a233f89a2b8 [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
13#if defined(_WIN32)
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
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000018#include <MMSystem.h>
Karl Wiberg79eb1d92017-11-08 12:26:07 +010019
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000020#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
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
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000025#endif
26
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/criticalsection.h"
28#include "rtc_base/timeutils.h"
29#include "system_wrappers/include/rw_lock_wrapper.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
82#if defined(_WIN32)
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
93 virtual ~WindowsRealTimeClock() {}
94
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
184#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC))
185class 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};
201#endif
202
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000203#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000204static WindowsRealTimeClock* volatile g_shared_clock = nullptr;
wu@webrtc.org7a9a3b72014-05-29 19:40:28 +0000205#endif
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000206Clock* Clock::GetRealTimeClock() {
207#if defined(_WIN32)
pbos@webrtc.orgf9389222015-01-21 12:51:13 +0000208 // This read relies on volatile read being atomic-load-acquire. This is
209 // true in MSVC since at least 2005:
210 // "A read of a volatile object (volatile read) has Acquire semantics"
211 if (g_shared_clock != nullptr)
212 return g_shared_clock;
213 WindowsRealTimeClock* clock = new WindowsRealTimeClock;
214 if (InterlockedCompareExchangePointer(
215 reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) !=
216 nullptr) {
217 // g_shared_clock was assigned while we constructed/tried to assign our
218 // instance, delete our instance and use the existing one.
219 delete clock;
220 }
221 return g_shared_clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000222#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000223 static UnixRealTimeClock clock;
224 return &clock;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000225#else
226 return NULL;
227#endif
228}
229
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000230SimulatedClock::SimulatedClock(int64_t initial_time_us)
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100231 : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) {}
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +0000232
Karl Wiberg79eb1d92017-11-08 12:26:07 +0100233SimulatedClock::~SimulatedClock() {}
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