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 |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 15 | #include <Windows.h> |
| 16 | #include <WinSock.h> |
| 17 | #include <MMSystem.h> |
| 18 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 19 | #include <sys/time.h> |
| 20 | #include <time.h> |
| 21 | #endif |
| 22 | |
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 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 36 | #if defined(_WIN32) |
| 37 | |
| 38 | struct reference_point { |
| 39 | FILETIME file_time; |
| 40 | LARGE_INTEGER counterMS; |
| 41 | }; |
| 42 | |
| 43 | struct WindowsHelpTimer { |
| 44 | volatile LONG _timeInMs; |
| 45 | volatile LONG _numWrapTimeInMs; |
| 46 | reference_point _ref_point; |
| 47 | |
| 48 | volatile LONG _sync_flag; |
| 49 | }; |
| 50 | |
| 51 | void Synchronize(WindowsHelpTimer* help_timer) { |
| 52 | const LONG start_value = 0; |
| 53 | const LONG new_value = 1; |
| 54 | const LONG synchronized_value = 2; |
| 55 | |
| 56 | LONG compare_flag = new_value; |
| 57 | while (help_timer->_sync_flag == start_value) { |
| 58 | const LONG new_value = 1; |
| 59 | compare_flag = InterlockedCompareExchange( |
| 60 | &help_timer->_sync_flag, new_value, start_value); |
| 61 | } |
| 62 | if (compare_flag != start_value) { |
| 63 | // This thread was not the one that incremented the sync flag. |
| 64 | // Block until synchronization finishes. |
| 65 | while (compare_flag != synchronized_value) { |
| 66 | ::Sleep(0); |
| 67 | } |
| 68 | return; |
| 69 | } |
| 70 | // Only the synchronizing thread gets here so this part can be |
| 71 | // considered single threaded. |
| 72 | |
| 73 | // set timer accuracy to 1 ms |
| 74 | timeBeginPeriod(1); |
| 75 | FILETIME ft0 = { 0, 0 }, |
| 76 | ft1 = { 0, 0 }; |
| 77 | // |
| 78 | // Spin waiting for a change in system time. Get the matching |
| 79 | // performance counter value for that time. |
| 80 | // |
| 81 | ::GetSystemTimeAsFileTime(&ft0); |
| 82 | do { |
| 83 | ::GetSystemTimeAsFileTime(&ft1); |
| 84 | |
| 85 | help_timer->_ref_point.counterMS.QuadPart = ::timeGetTime(); |
| 86 | ::Sleep(0); |
| 87 | } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) && |
| 88 | (ft0.dwLowDateTime == ft1.dwLowDateTime)); |
| 89 | help_timer->_ref_point.file_time = ft1; |
| 90 | timeEndPeriod(1); |
| 91 | } |
| 92 | |
| 93 | void get_time(WindowsHelpTimer* help_timer, FILETIME& current_time) { |
| 94 | // we can't use query performance counter due to speed stepping |
| 95 | DWORD t = timeGetTime(); |
| 96 | // NOTE: we have a missmatch in sign between _timeInMs(LONG) and |
| 97 | // (DWORD) however we only use it here without +- etc |
| 98 | volatile LONG* timeInMsPtr = &help_timer->_timeInMs; |
| 99 | // Make sure that we only inc wrapper once. |
| 100 | DWORD old = InterlockedExchange(timeInMsPtr, t); |
| 101 | if(old > t) { |
| 102 | // wrap |
| 103 | help_timer->_numWrapTimeInMs++; |
| 104 | } |
| 105 | LARGE_INTEGER elapsedMS; |
| 106 | elapsedMS.HighPart = help_timer->_numWrapTimeInMs; |
| 107 | elapsedMS.LowPart = t; |
| 108 | |
| 109 | elapsedMS.QuadPart = elapsedMS.QuadPart - |
| 110 | help_timer->_ref_point.counterMS.QuadPart; |
| 111 | |
| 112 | // Translate to 100-nanoseconds intervals (FILETIME resolution) |
| 113 | // and add to reference FILETIME to get current FILETIME. |
| 114 | ULARGE_INTEGER filetime_ref_as_ul; |
| 115 | |
| 116 | filetime_ref_as_ul.HighPart = |
| 117 | help_timer->_ref_point.file_time.dwHighDateTime; |
| 118 | filetime_ref_as_ul.LowPart = |
| 119 | help_timer->_ref_point.file_time.dwLowDateTime; |
| 120 | filetime_ref_as_ul.QuadPart += |
| 121 | (ULONGLONG)((elapsedMS.QuadPart)*1000*10); |
| 122 | |
| 123 | // Copy to result |
| 124 | current_time.dwHighDateTime = filetime_ref_as_ul.HighPart; |
| 125 | current_time.dwLowDateTime = filetime_ref_as_ul.LowPart; |
| 126 | } |
| 127 | #endif |
| 128 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 129 | class RealTimeClock : public Clock { |
| 130 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 131 | // source is fixed for this clock. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 132 | virtual int64_t TimeInMilliseconds() OVERRIDE { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 133 | return TickTime::MillisecondTimestamp(); |
| 134 | } |
| 135 | |
| 136 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 137 | // source is fixed for this clock. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 138 | virtual int64_t TimeInMicroseconds() OVERRIDE { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 139 | return TickTime::MicrosecondTimestamp(); |
| 140 | } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 141 | |
| 142 | // Retrieve an NTP absolute timestamp in seconds and fractions of a second. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 143 | virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) OVERRIDE { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 144 | timeval tv = CurrentTimeVal(); |
| 145 | double microseconds_in_seconds; |
| 146 | Adjust(tv, &seconds, µseconds_in_seconds); |
| 147 | fractions = static_cast<uint32_t>( |
| 148 | microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); |
| 149 | } |
| 150 | |
| 151 | // Retrieve an NTP absolute timestamp in milliseconds. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 152 | virtual int64_t CurrentNtpInMilliseconds() OVERRIDE { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 153 | timeval tv = CurrentTimeVal(); |
| 154 | uint32_t seconds; |
| 155 | double microseconds_in_seconds; |
| 156 | Adjust(tv, &seconds, µseconds_in_seconds); |
| 157 | return 1000 * static_cast<int64_t>(seconds) + |
| 158 | static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); |
| 159 | } |
| 160 | |
| 161 | protected: |
| 162 | virtual timeval CurrentTimeVal() const = 0; |
| 163 | |
| 164 | static void Adjust(const timeval& tv, uint32_t* adjusted_s, |
| 165 | double* adjusted_us_in_s) { |
| 166 | *adjusted_s = tv.tv_sec + kNtpJan1970; |
| 167 | *adjusted_us_in_s = tv.tv_usec / 1e6; |
| 168 | |
| 169 | if (*adjusted_us_in_s >= 1) { |
| 170 | *adjusted_us_in_s -= 1; |
| 171 | ++*adjusted_s; |
| 172 | } else if (*adjusted_us_in_s < -1) { |
| 173 | *adjusted_us_in_s += 1; |
| 174 | --*adjusted_s; |
| 175 | } |
| 176 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | #if defined(_WIN32) |
| 180 | class WindowsRealTimeClock : public RealTimeClock { |
| 181 | public: |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 182 | WindowsRealTimeClock(WindowsHelpTimer* helpTimer) |
| 183 | : _helpTimer(helpTimer) {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 184 | |
| 185 | virtual ~WindowsRealTimeClock() {} |
| 186 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 187 | protected: |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 188 | virtual timeval CurrentTimeVal() const OVERRIDE { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 189 | const uint64_t FILETIME_1970 = 0x019db1ded53e8000; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 190 | |
| 191 | FILETIME StartTime; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 192 | uint64_t Time; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 193 | struct timeval tv; |
| 194 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 195 | // We can't use query performance counter since they can change depending on |
| 196 | // speed stepping. |
| 197 | get_time(_helpTimer, StartTime); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 198 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 199 | Time = (((uint64_t) StartTime.dwHighDateTime) << 32) + |
| 200 | (uint64_t) StartTime.dwLowDateTime; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 201 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 202 | // Convert the hecto-nano second time to tv format. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 203 | Time -= FILETIME_1970; |
| 204 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 205 | tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); |
| 206 | tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 207 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 208 | } |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 209 | |
| 210 | WindowsHelpTimer* _helpTimer; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 214 | class UnixRealTimeClock : public RealTimeClock { |
| 215 | public: |
| 216 | UnixRealTimeClock() {} |
| 217 | |
| 218 | virtual ~UnixRealTimeClock() {} |
| 219 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 220 | protected: |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 221 | virtual timeval CurrentTimeVal() const OVERRIDE { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 222 | struct timeval tv; |
| 223 | struct timezone tz; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 224 | tz.tz_minuteswest = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 225 | tz.tz_dsttime = 0; |
| 226 | gettimeofday(&tv, &tz); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 227 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 228 | } |
| 229 | }; |
| 230 | #endif |
| 231 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 232 | |
| 233 | #if defined(_WIN32) |
| 234 | // Keeps the global state for the Windows implementation of RtpRtcpClock. |
| 235 | // Note that this is a POD. Only PODs are allowed to have static storage |
| 236 | // duration according to the Google Style guide. |
| 237 | // |
| 238 | // Note that on Windows, GetSystemTimeAsFileTime has poorer (up to 15 ms) |
| 239 | // resolution than the media timers, hence the WindowsHelpTimer context |
| 240 | // object and Synchronize API to sync the two. |
| 241 | // |
| 242 | // We only sync up once, which means that on Windows, our realtime clock |
| 243 | // wont respond to system time/date changes without a program restart. |
| 244 | // TODO(henrike): We should probably call sync more often to catch |
| 245 | // drift and time changes for parity with other platforms. |
| 246 | |
| 247 | static WindowsHelpTimer *SyncGlobalHelpTimer() { |
| 248 | static WindowsHelpTimer global_help_timer = {0, 0, {{ 0, 0}, 0}, 0}; |
| 249 | Synchronize(&global_help_timer); |
| 250 | return &global_help_timer; |
| 251 | } |
| 252 | #endif |
| 253 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 254 | Clock* Clock::GetRealTimeClock() { |
| 255 | #if defined(_WIN32) |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 256 | static WindowsRealTimeClock clock(SyncGlobalHelpTimer()); |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 257 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 258 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 259 | static UnixRealTimeClock clock; |
| 260 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 261 | #else |
| 262 | return NULL; |
| 263 | #endif |
| 264 | } |
| 265 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 266 | SimulatedClock::SimulatedClock(int64_t initial_time_us) |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 267 | : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) { |
| 268 | } |
| 269 | |
| 270 | SimulatedClock::~SimulatedClock() { |
| 271 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 272 | |
| 273 | int64_t SimulatedClock::TimeInMilliseconds() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 274 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 275 | return (time_us_ + 500) / 1000; |
| 276 | } |
| 277 | |
| 278 | int64_t SimulatedClock::TimeInMicroseconds() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 279 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 280 | return time_us_; |
| 281 | } |
| 282 | |
| 283 | void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 284 | int64_t now_ms = TimeInMilliseconds(); |
| 285 | seconds = (now_ms / 1000) + kNtpJan1970; |
| 286 | fractions = |
| 287 | static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 288 | } |
| 289 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 290 | int64_t SimulatedClock::CurrentNtpInMilliseconds() { |
| 291 | return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 292 | } |
| 293 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 294 | void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
| 295 | AdvanceTimeMicroseconds(1000 * milliseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 296 | } |
| 297 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 298 | void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 299 | WriteLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 300 | time_us_ += microseconds; |
| 301 | } |
| 302 | |
| 303 | }; // namespace webrtc |