19858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org/* 29858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 39858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * 49858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * Use of this source code is governed by a BSD-style license 59858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * that can be found in the LICENSE file in the root of the source 69858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * tree. An additional intellectual property rights grant can be found 79858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * in the file PATENTS. All contributing project authors may 89858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org * be found in the AUTHORS file in the root of the source tree. 99858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org */ 109858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 119858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include "webrtc/system_wrappers/interface/clock.h" 129858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 139858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#if defined(_WIN32) 14c6d6fed3c0a82bb7a09095381b974e8e5eebcb35pbos@webrtc.org// Windows needs to be included before mmsystem.h 159858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include <Windows.h> 169858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include <WinSock.h> 179858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include <MMSystem.h> 189858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) 199858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include <sys/time.h> 209858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include <time.h> 219858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#endif 229858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2317d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h" 249858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#include "webrtc/system_wrappers/interface/tick_util.h" 259858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 269858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgnamespace webrtc { 279858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 289b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.orgconst double kNtpFracPerMs = 4.294967296E6; 299b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 309b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.orgint64_t Clock::NtpToMs(uint32_t ntp_secs, uint32_t ntp_frac) { 319b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org const double ntp_frac_ms = static_cast<double>(ntp_frac) / kNtpFracPerMs; 329b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org return 1000 * static_cast<int64_t>(ntp_secs) + 339b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org static_cast<int64_t>(ntp_frac_ms + 0.5); 349b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org} 359b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 366f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org#if defined(_WIN32) 376f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 386f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.orgstruct reference_point { 396f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org FILETIME file_time; 406f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org LARGE_INTEGER counterMS; 416f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org}; 426f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 436f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.orgstruct WindowsHelpTimer { 446f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org volatile LONG _timeInMs; 456f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org volatile LONG _numWrapTimeInMs; 466f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org reference_point _ref_point; 476f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 486f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org volatile LONG _sync_flag; 496f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org}; 506f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 516f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.orgvoid Synchronize(WindowsHelpTimer* help_timer) { 526f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org const LONG start_value = 0; 536f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org const LONG new_value = 1; 546f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org const LONG synchronized_value = 2; 556f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 566f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org LONG compare_flag = new_value; 576f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org while (help_timer->_sync_flag == start_value) { 586f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org const LONG new_value = 1; 596f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org compare_flag = InterlockedCompareExchange( 606f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org &help_timer->_sync_flag, new_value, start_value); 616f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org } 626f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org if (compare_flag != start_value) { 636f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // This thread was not the one that incremented the sync flag. 646f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Block until synchronization finishes. 656f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org while (compare_flag != synchronized_value) { 666f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ::Sleep(0); 676f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org } 686f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org return; 696f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org } 706f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Only the synchronizing thread gets here so this part can be 716f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // considered single threaded. 726f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 736f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // set timer accuracy to 1 ms 746f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org timeBeginPeriod(1); 756f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org FILETIME ft0 = { 0, 0 }, 766f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ft1 = { 0, 0 }; 776f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // 786f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Spin waiting for a change in system time. Get the matching 796f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // performance counter value for that time. 806f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // 816f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ::GetSystemTimeAsFileTime(&ft0); 826f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org do { 836f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ::GetSystemTimeAsFileTime(&ft1); 846f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 856f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_ref_point.counterMS.QuadPart = ::timeGetTime(); 866f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ::Sleep(0); 876f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) && 886f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org (ft0.dwLowDateTime == ft1.dwLowDateTime)); 896f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_ref_point.file_time = ft1; 906f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org timeEndPeriod(1); 916f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org} 926f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 936f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.orgvoid get_time(WindowsHelpTimer* help_timer, FILETIME& current_time) { 946f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // we can't use query performance counter due to speed stepping 956f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org DWORD t = timeGetTime(); 966f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // NOTE: we have a missmatch in sign between _timeInMs(LONG) and 976f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // (DWORD) however we only use it here without +- etc 986f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org volatile LONG* timeInMsPtr = &help_timer->_timeInMs; 996f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Make sure that we only inc wrapper once. 1006f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org DWORD old = InterlockedExchange(timeInMsPtr, t); 1016f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org if(old > t) { 1026f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // wrap 1036f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_numWrapTimeInMs++; 1046f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org } 1056f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org LARGE_INTEGER elapsedMS; 1066f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org elapsedMS.HighPart = help_timer->_numWrapTimeInMs; 1076f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org elapsedMS.LowPart = t; 1086f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 1096f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org elapsedMS.QuadPart = elapsedMS.QuadPart - 1106f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_ref_point.counterMS.QuadPart; 1116f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 1126f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Translate to 100-nanoseconds intervals (FILETIME resolution) 1136f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // and add to reference FILETIME to get current FILETIME. 1146f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org ULARGE_INTEGER filetime_ref_as_ul; 1156f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 1166f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org filetime_ref_as_ul.HighPart = 1176f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_ref_point.file_time.dwHighDateTime; 1186f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org filetime_ref_as_ul.LowPart = 1196f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org help_timer->_ref_point.file_time.dwLowDateTime; 1206f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org filetime_ref_as_ul.QuadPart += 1216f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org (ULONGLONG)((elapsedMS.QuadPart)*1000*10); 1226f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 1236f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // Copy to result 1246f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org current_time.dwHighDateTime = filetime_ref_as_ul.HighPart; 1256f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org current_time.dwLowDateTime = filetime_ref_as_ul.LowPart; 1266f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org} 1276f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org#endif 1286f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 1299858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgclass RealTimeClock : public Clock { 1309858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org // Return a timestamp in milliseconds relative to some arbitrary source; the 1319858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org // source is fixed for this clock. 132b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.org virtual int64_t TimeInMilliseconds() const OVERRIDE { 1339858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org return TickTime::MillisecondTimestamp(); 1349858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org } 1359858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1369858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org // Return a timestamp in microseconds relative to some arbitrary source; the 1379858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org // source is fixed for this clock. 138b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.org virtual int64_t TimeInMicroseconds() const OVERRIDE { 1399858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org return TickTime::MicrosecondTimestamp(); 1409858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org } 1419b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 1429b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org // Retrieve an NTP absolute timestamp in seconds and fractions of a second. 143b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.org virtual void CurrentNtp(uint32_t& seconds, 144b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.org uint32_t& fractions) const OVERRIDE { 1459b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org timeval tv = CurrentTimeVal(); 1469b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org double microseconds_in_seconds; 1479b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org Adjust(tv, &seconds, µseconds_in_seconds); 1489b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org fractions = static_cast<uint32_t>( 1499b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); 1509b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org } 1519b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 1529b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org // Retrieve an NTP absolute timestamp in milliseconds. 153b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.org virtual int64_t CurrentNtpInMilliseconds() const OVERRIDE { 1549b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org timeval tv = CurrentTimeVal(); 1559b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org uint32_t seconds; 1569b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org double microseconds_in_seconds; 1579b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org Adjust(tv, &seconds, µseconds_in_seconds); 1589b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org return 1000 * static_cast<int64_t>(seconds) + 1599b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); 1609b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org } 1619b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 1629b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org protected: 1639b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org virtual timeval CurrentTimeVal() const = 0; 1649b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 1659b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org static void Adjust(const timeval& tv, uint32_t* adjusted_s, 1669b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org double* adjusted_us_in_s) { 1679b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org *adjusted_s = tv.tv_sec + kNtpJan1970; 1689b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org *adjusted_us_in_s = tv.tv_usec / 1e6; 1699b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 1709b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org if (*adjusted_us_in_s >= 1) { 1719b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org *adjusted_us_in_s -= 1; 1729b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org ++*adjusted_s; 1739b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org } else if (*adjusted_us_in_s < -1) { 1749b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org *adjusted_us_in_s += 1; 1759b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org --*adjusted_s; 1769b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org } 1779b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org } 1789858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org}; 1799858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1809858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#if defined(_WIN32) 1819858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgclass WindowsRealTimeClock : public RealTimeClock { 1829858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org public: 1836f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org WindowsRealTimeClock(WindowsHelpTimer* helpTimer) 1846f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org : _helpTimer(helpTimer) {} 1859858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1869858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org virtual ~WindowsRealTimeClock() {} 1879858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1889b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org protected: 1897affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org virtual timeval CurrentTimeVal() const OVERRIDE { 190c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org const uint64_t FILETIME_1970 = 0x019db1ded53e8000; 1919858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1929858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org FILETIME StartTime; 193c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org uint64_t Time; 1949858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org struct timeval tv; 1959858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 1966f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // We can't use query performance counter since they can change depending on 1976f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org // speed stepping. 1986f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org get_time(_helpTimer, StartTime); 1999858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 200c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org Time = (((uint64_t) StartTime.dwHighDateTime) << 32) + 201c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org (uint64_t) StartTime.dwLowDateTime; 2029858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2039b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org // Convert the hecto-nano second time to tv format. 2049858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org Time -= FILETIME_1970; 2059858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 206c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); 207c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); 2089b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org return tv; 2099858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org } 2106f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 2116f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org WindowsHelpTimer* _helpTimer; 2129858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org}; 2139858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2149858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) 2159858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgclass UnixRealTimeClock : public RealTimeClock { 2169858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org public: 2179858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org UnixRealTimeClock() {} 2189858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2199858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org virtual ~UnixRealTimeClock() {} 2209858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2219b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org protected: 2227affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org virtual timeval CurrentTimeVal() const OVERRIDE { 2239858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org struct timeval tv; 2249858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org struct timezone tz; 2259b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org tz.tz_minuteswest = 0; 2269858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org tz.tz_dsttime = 0; 2279858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org gettimeofday(&tv, &tz); 2289b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org return tv; 2299858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org } 2309858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org}; 2319858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#endif 2329858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2336f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 2346f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org#if defined(_WIN32) 2356f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// Keeps the global state for the Windows implementation of RtpRtcpClock. 2366f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// Note that this is a POD. Only PODs are allowed to have static storage 2376f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// duration according to the Google Style guide. 2386f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// 2396f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// Note that on Windows, GetSystemTimeAsFileTime has poorer (up to 15 ms) 2406f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// resolution than the media timers, hence the WindowsHelpTimer context 2416f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// object and Synchronize API to sync the two. 2426f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// 2436f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// We only sync up once, which means that on Windows, our realtime clock 2446f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// wont respond to system time/date changes without a program restart. 2456f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// TODO(henrike): We should probably call sync more often to catch 2466f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org// drift and time changes for parity with other platforms. 2476f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 2486f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.orgstatic WindowsHelpTimer *SyncGlobalHelpTimer() { 2496f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org static WindowsHelpTimer global_help_timer = {0, 0, {{ 0, 0}, 0}, 0}; 2506f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org Synchronize(&global_help_timer); 2516f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org return &global_help_timer; 2526f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org} 2536f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org#endif 2546f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org 2559858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgClock* Clock::GetRealTimeClock() { 2569858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#if defined(_WIN32) 2576f3ce73db6cf7b6edd1c0c1fd4837d3435a2e0bfwu@webrtc.org static WindowsRealTimeClock clock(SyncGlobalHelpTimer()); 2581bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.org return &clock; 2599858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) 2601bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.org static UnixRealTimeClock clock; 2611bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.org return &clock; 2629858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#else 2639858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org return NULL; 2649858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org#endif 2659858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 2669858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2679858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.orgSimulatedClock::SimulatedClock(int64_t initial_time_us) 26817d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) { 26917d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org} 27017d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org 27117d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.orgSimulatedClock::~SimulatedClock() { 27217d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org} 2739858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 274b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.orgint64_t SimulatedClock::TimeInMilliseconds() const { 27517d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org ReadLockScoped synchronize(*lock_); 2769858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org return (time_us_ + 500) / 1000; 2779858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 2789858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 279b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.orgint64_t SimulatedClock::TimeInMicroseconds() const { 28017d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org ReadLockScoped synchronize(*lock_); 2819858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org return time_us_; 2829858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 2839858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 284b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.orgvoid SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) const { 28517d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org int64_t now_ms = TimeInMilliseconds(); 28617d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org seconds = (now_ms / 1000) + kNtpJan1970; 28717d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org fractions = 28817d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); 2899858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 2909858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 291b999e11cdb6031f2b56acff53dacdb5d1d72d984henrik.lundin@webrtc.orgint64_t SimulatedClock::CurrentNtpInMilliseconds() const { 2929b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); 2939b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org} 2949b5315258a9a931a4fb8b0806a79a4758a1ddc13stefan@webrtc.org 2951bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.orgvoid SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { 2961bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.org AdvanceTimeMicroseconds(1000 * milliseconds); 2979858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 2989858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 2991bb2146351979b6610107419b2a9c86cca2692a3stefan@webrtc.orgvoid SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { 30017d5ac4d612c69abe4dbc824601583f0e3273bc5henrik.lundin@webrtc.org WriteLockScoped synchronize(*lock_); 3019858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org time_us_ += microseconds; 3029858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org} 3039858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org 3049858fc8df331a4111dc996469bcf027c4f986556stefan@webrtc.org}; // namespace webrtc 305