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