stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 1 | /* |
| 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 Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/include/clock.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(_WIN32) |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 14 | // Windows needs to be included before mmsystem.h |
kjellander | c3771cc | 2017-06-30 13:42:44 -0700 | [diff] [blame^] | 15 | #include "webrtc/rtc_base/win32.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 16 | #include <MMSystem.h> |
| 17 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 18 | #include <sys/time.h> |
| 19 | #include <time.h> |
| 20 | #endif |
| 21 | |
kjellander | c3771cc | 2017-06-30 13:42:44 -0700 | [diff] [blame^] | 22 | #include "webrtc/rtc_base/criticalsection.h" |
| 23 | #include "webrtc/rtc_base/timeutils.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/rw_lock_wrapper.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 28 | class RealTimeClock : public Clock { |
| 29 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 30 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 31 | int64_t TimeInMilliseconds() const override { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 32 | return rtc::TimeMillis(); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 36 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 37 | int64_t TimeInMicroseconds() const override { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 38 | return rtc::TimeMicros(); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 39 | } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 40 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 41 | // Retrieve an NTP absolute timestamp. |
| 42 | NtpTime CurrentNtpTime() const override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 43 | timeval tv = CurrentTimeVal(); |
| 44 | double microseconds_in_seconds; |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 45 | uint32_t seconds; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 46 | Adjust(tv, &seconds, µseconds_in_seconds); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 47 | uint32_t fractions = static_cast<uint32_t>( |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 48 | microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 49 | return NtpTime(seconds, fractions); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Retrieve an NTP absolute timestamp in milliseconds. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 53 | int64_t CurrentNtpInMilliseconds() const override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 54 | timeval tv = CurrentTimeVal(); |
| 55 | uint32_t seconds; |
| 56 | double microseconds_in_seconds; |
| 57 | Adjust(tv, &seconds, µseconds_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.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 81 | // 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.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 84 | class WindowsRealTimeClock : public RealTimeClock { |
| 85 | public: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 86 | WindowsRealTimeClock() |
| 87 | : last_time_ms_(0), |
| 88 | num_timer_wraps_(0), |
| 89 | ref_point_(GetSystemReferencePoint()) {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 90 | |
| 91 | virtual ~WindowsRealTimeClock() {} |
| 92 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 93 | protected: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 94 | struct ReferencePoint { |
| 95 | FILETIME file_time; |
| 96 | LARGE_INTEGER counter_ms; |
| 97 | }; |
| 98 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 99 | timeval CurrentTimeVal() const override { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 100 | const uint64_t FILETIME_1970 = 0x019db1ded53e8000; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 101 | |
| 102 | FILETIME StartTime; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 103 | uint64_t Time; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 104 | struct timeval tv; |
| 105 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 106 | // We can't use query performance counter since they can change depending on |
| 107 | // speed stepping. |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 108 | GetTime(&StartTime); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 109 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 110 | Time = (((uint64_t) StartTime.dwHighDateTime) << 32) + |
| 111 | (uint64_t) StartTime.dwLowDateTime; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 112 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 113 | // Convert the hecto-nano second time to tv format. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 114 | Time -= FILETIME_1970; |
| 115 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 116 | tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); |
| 117 | tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 118 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 119 | } |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 120 | |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 121 | 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() { |
dcheng | a771bf8 | 2015-07-01 17:52:10 -0700 | [diff] [blame] | 152 | ReferencePoint ref = {}; |
| 153 | FILETIME ft0 = {}; |
| 154 | FILETIME ft1 = {}; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 155 | // 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. |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 176 | rtc::CriticalSection crit_; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 177 | mutable DWORD last_time_ms_; |
| 178 | mutable LONG num_timer_wraps_; |
| 179 | const ReferencePoint ref_point_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 183 | class UnixRealTimeClock : public RealTimeClock { |
| 184 | public: |
| 185 | UnixRealTimeClock() {} |
| 186 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 187 | ~UnixRealTimeClock() override {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 188 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 189 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 190 | timeval CurrentTimeVal() const override { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 191 | struct timeval tv; |
| 192 | struct timezone tz; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 193 | tz.tz_minuteswest = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 194 | tz.tz_dsttime = 0; |
| 195 | gettimeofday(&tv, &tz); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 196 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | #endif |
| 200 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 201 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 202 | static WindowsRealTimeClock* volatile g_shared_clock = nullptr; |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 203 | #endif |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 204 | Clock* Clock::GetRealTimeClock() { |
| 205 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 206 | // 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.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 220 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 221 | static UnixRealTimeClock clock; |
| 222 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 223 | #else |
| 224 | return NULL; |
| 225 | #endif |
| 226 | } |
| 227 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 228 | SimulatedClock::SimulatedClock(int64_t initial_time_us) |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 229 | : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) { |
| 230 | } |
| 231 | |
| 232 | SimulatedClock::~SimulatedClock() { |
| 233 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 234 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 235 | int64_t SimulatedClock::TimeInMilliseconds() const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 236 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 237 | return (time_us_ + 500) / 1000; |
| 238 | } |
| 239 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 240 | int64_t SimulatedClock::TimeInMicroseconds() const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 241 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 242 | return time_us_; |
| 243 | } |
| 244 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 245 | NtpTime SimulatedClock::CurrentNtpTime() const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 246 | int64_t now_ms = TimeInMilliseconds(); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 247 | uint32_t seconds = (now_ms / 1000) + kNtpJan1970; |
| 248 | uint32_t fractions = |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 249 | static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 250 | return NtpTime(seconds, fractions); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 251 | } |
| 252 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 253 | int64_t SimulatedClock::CurrentNtpInMilliseconds() const { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 254 | return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 255 | } |
| 256 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 257 | void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
| 258 | AdvanceTimeMicroseconds(1000 * milliseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 259 | } |
| 260 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 261 | void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 262 | WriteLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 263 | time_us_ += microseconds; |
| 264 | } |
| 265 | |
| 266 | }; // namespace webrtc |