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 |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 15 | #include "webrtc/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 | |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 22 | #include "webrtc/base/criticalsection.h" |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 23 | #include "webrtc/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 | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 28 | const double kNtpFracPerMs = 4.294967296E6; |
| 29 | |
| 30 | int64_t Clock::NtpToMs(uint32_t ntp_secs, uint32_t ntp_frac) { |
| 31 | const double ntp_frac_ms = static_cast<double>(ntp_frac) / kNtpFracPerMs; |
| 32 | return 1000 * static_cast<int64_t>(ntp_secs) + |
| 33 | static_cast<int64_t>(ntp_frac_ms + 0.5); |
| 34 | } |
| 35 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 36 | class RealTimeClock : public Clock { |
| 37 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 38 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 39 | int64_t TimeInMilliseconds() const override { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 40 | return rtc::TimeMillis(); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 44 | // source is fixed for this clock. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 45 | int64_t TimeInMicroseconds() const override { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame^] | 46 | return rtc::TimeMicros(); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 47 | } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 48 | |
| 49 | // Retrieve an NTP absolute timestamp in seconds and fractions of a second. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 50 | void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 51 | timeval tv = CurrentTimeVal(); |
| 52 | double microseconds_in_seconds; |
| 53 | Adjust(tv, &seconds, µseconds_in_seconds); |
| 54 | fractions = static_cast<uint32_t>( |
| 55 | microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); |
| 56 | } |
| 57 | |
| 58 | // Retrieve an NTP absolute timestamp in milliseconds. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 59 | int64_t CurrentNtpInMilliseconds() const override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 60 | timeval tv = CurrentTimeVal(); |
| 61 | uint32_t seconds; |
| 62 | double microseconds_in_seconds; |
| 63 | Adjust(tv, &seconds, µseconds_in_seconds); |
| 64 | return 1000 * static_cast<int64_t>(seconds) + |
| 65 | static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | virtual timeval CurrentTimeVal() const = 0; |
| 70 | |
| 71 | static void Adjust(const timeval& tv, uint32_t* adjusted_s, |
| 72 | double* adjusted_us_in_s) { |
| 73 | *adjusted_s = tv.tv_sec + kNtpJan1970; |
| 74 | *adjusted_us_in_s = tv.tv_usec / 1e6; |
| 75 | |
| 76 | if (*adjusted_us_in_s >= 1) { |
| 77 | *adjusted_us_in_s -= 1; |
| 78 | ++*adjusted_s; |
| 79 | } else if (*adjusted_us_in_s < -1) { |
| 80 | *adjusted_us_in_s += 1; |
| 81 | --*adjusted_s; |
| 82 | } |
| 83 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 87 | // TODO(pbos): Consider modifying the implementation to synchronize itself |
| 88 | // against system time (update ref_point_, make it non-const) periodically to |
| 89 | // prevent clock drift. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 90 | class WindowsRealTimeClock : public RealTimeClock { |
| 91 | public: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 92 | WindowsRealTimeClock() |
| 93 | : last_time_ms_(0), |
| 94 | num_timer_wraps_(0), |
| 95 | ref_point_(GetSystemReferencePoint()) {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 96 | |
| 97 | virtual ~WindowsRealTimeClock() {} |
| 98 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 99 | protected: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 100 | struct ReferencePoint { |
| 101 | FILETIME file_time; |
| 102 | LARGE_INTEGER counter_ms; |
| 103 | }; |
| 104 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 105 | timeval CurrentTimeVal() const override { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 106 | const uint64_t FILETIME_1970 = 0x019db1ded53e8000; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 107 | |
| 108 | FILETIME StartTime; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 109 | uint64_t Time; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 110 | struct timeval tv; |
| 111 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 112 | // We can't use query performance counter since they can change depending on |
| 113 | // speed stepping. |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 114 | GetTime(&StartTime); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 115 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 116 | Time = (((uint64_t) StartTime.dwHighDateTime) << 32) + |
| 117 | (uint64_t) StartTime.dwLowDateTime; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 118 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 119 | // Convert the hecto-nano second time to tv format. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 120 | Time -= FILETIME_1970; |
| 121 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 122 | tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); |
| 123 | tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 124 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 125 | } |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 126 | |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 127 | void GetTime(FILETIME* current_time) const { |
| 128 | DWORD t; |
| 129 | LARGE_INTEGER elapsed_ms; |
| 130 | { |
| 131 | rtc::CritScope lock(&crit_); |
| 132 | // time MUST be fetched inside the critical section to avoid non-monotonic |
| 133 | // last_time_ms_ values that'll register as incorrect wraparounds due to |
| 134 | // concurrent calls to GetTime. |
| 135 | t = timeGetTime(); |
| 136 | if (t < last_time_ms_) |
| 137 | num_timer_wraps_++; |
| 138 | last_time_ms_ = t; |
| 139 | elapsed_ms.HighPart = num_timer_wraps_; |
| 140 | } |
| 141 | elapsed_ms.LowPart = t; |
| 142 | elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart; |
| 143 | |
| 144 | // Translate to 100-nanoseconds intervals (FILETIME resolution) |
| 145 | // and add to reference FILETIME to get current FILETIME. |
| 146 | ULARGE_INTEGER filetime_ref_as_ul; |
| 147 | filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime; |
| 148 | filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime; |
| 149 | filetime_ref_as_ul.QuadPart += |
| 150 | static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10); |
| 151 | |
| 152 | // Copy to result |
| 153 | current_time->dwHighDateTime = filetime_ref_as_ul.HighPart; |
| 154 | current_time->dwLowDateTime = filetime_ref_as_ul.LowPart; |
| 155 | } |
| 156 | |
| 157 | static ReferencePoint GetSystemReferencePoint() { |
dcheng | a771bf8 | 2015-07-01 17:52:10 -0700 | [diff] [blame] | 158 | ReferencePoint ref = {}; |
| 159 | FILETIME ft0 = {}; |
| 160 | FILETIME ft1 = {}; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 161 | // Spin waiting for a change in system time. As soon as this change happens, |
| 162 | // get the matching call for timeGetTime() as soon as possible. This is |
| 163 | // assumed to be the most accurate offset that we can get between |
| 164 | // timeGetTime() and system time. |
| 165 | |
| 166 | // Set timer accuracy to 1 ms. |
| 167 | timeBeginPeriod(1); |
| 168 | GetSystemTimeAsFileTime(&ft0); |
| 169 | do { |
| 170 | GetSystemTimeAsFileTime(&ft1); |
| 171 | |
| 172 | ref.counter_ms.QuadPart = timeGetTime(); |
| 173 | Sleep(0); |
| 174 | } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) && |
| 175 | (ft0.dwLowDateTime == ft1.dwLowDateTime)); |
| 176 | ref.file_time = ft1; |
| 177 | timeEndPeriod(1); |
| 178 | return ref; |
| 179 | } |
| 180 | |
| 181 | // mutable as time-accessing functions are const. |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 182 | rtc::CriticalSection crit_; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 183 | mutable DWORD last_time_ms_; |
| 184 | mutable LONG num_timer_wraps_; |
| 185 | const ReferencePoint ref_point_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 189 | class UnixRealTimeClock : public RealTimeClock { |
| 190 | public: |
| 191 | UnixRealTimeClock() {} |
| 192 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 193 | ~UnixRealTimeClock() override {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 194 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 195 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 196 | timeval CurrentTimeVal() const override { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 197 | struct timeval tv; |
| 198 | struct timezone tz; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 199 | tz.tz_minuteswest = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 200 | tz.tz_dsttime = 0; |
| 201 | gettimeofday(&tv, &tz); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 202 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 203 | } |
| 204 | }; |
| 205 | #endif |
| 206 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 207 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 208 | static WindowsRealTimeClock* volatile g_shared_clock = nullptr; |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 209 | #endif |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 210 | Clock* Clock::GetRealTimeClock() { |
| 211 | #if defined(_WIN32) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 212 | // This read relies on volatile read being atomic-load-acquire. This is |
| 213 | // true in MSVC since at least 2005: |
| 214 | // "A read of a volatile object (volatile read) has Acquire semantics" |
| 215 | if (g_shared_clock != nullptr) |
| 216 | return g_shared_clock; |
| 217 | WindowsRealTimeClock* clock = new WindowsRealTimeClock; |
| 218 | if (InterlockedCompareExchangePointer( |
| 219 | reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) != |
| 220 | nullptr) { |
| 221 | // g_shared_clock was assigned while we constructed/tried to assign our |
| 222 | // instance, delete our instance and use the existing one. |
| 223 | delete clock; |
| 224 | } |
| 225 | return g_shared_clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 226 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 227 | static UnixRealTimeClock clock; |
| 228 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 229 | #else |
| 230 | return NULL; |
| 231 | #endif |
| 232 | } |
| 233 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 234 | SimulatedClock::SimulatedClock(int64_t initial_time_us) |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 235 | : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) { |
| 236 | } |
| 237 | |
| 238 | SimulatedClock::~SimulatedClock() { |
| 239 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 240 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 241 | int64_t SimulatedClock::TimeInMilliseconds() const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 242 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 243 | return (time_us_ + 500) / 1000; |
| 244 | } |
| 245 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 246 | int64_t SimulatedClock::TimeInMicroseconds() const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 247 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 248 | return time_us_; |
| 249 | } |
| 250 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 251 | void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) const { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 252 | int64_t now_ms = TimeInMilliseconds(); |
| 253 | seconds = (now_ms / 1000) + kNtpJan1970; |
| 254 | fractions = |
| 255 | static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 256 | } |
| 257 | |
henrik.lundin@webrtc.org | 190a32f | 2014-06-09 17:40:49 +0000 | [diff] [blame] | 258 | int64_t SimulatedClock::CurrentNtpInMilliseconds() const { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 259 | return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 260 | } |
| 261 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 262 | void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
| 263 | AdvanceTimeMicroseconds(1000 * milliseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 264 | } |
| 265 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 266 | void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 267 | WriteLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 268 | time_us_ += microseconds; |
| 269 | } |
| 270 | |
| 271 | }; // namespace webrtc |