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 | |
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. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 39 | virtual int64_t TimeInMilliseconds() 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. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 45 | virtual int64_t TimeInMicroseconds() 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. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 50 | virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) 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. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 59 | virtual int64_t CurrentNtpInMilliseconds() 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) |
| 87 | class WindowsRealTimeClock : public RealTimeClock { |
| 88 | public: |
wu@webrtc.org | 75718cf | 2014-05-15 23:54:14 +0000 | [diff] [blame] | 89 | WindowsRealTimeClock() {} |
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 | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 94 | virtual timeval CurrentTimeVal() const OVERRIDE { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 95 | const uint64_t FILETIME_1970 = 0x019db1ded53e8000; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 96 | |
| 97 | FILETIME StartTime; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 98 | uint64_t Time; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 99 | struct timeval tv; |
| 100 | |
wu@webrtc.org | 75718cf | 2014-05-15 23:54:14 +0000 | [diff] [blame] | 101 | GetSystemTimeAsFileTime(&StartTime); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 102 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 103 | Time = (((uint64_t) StartTime.dwHighDateTime) << 32) + |
| 104 | (uint64_t) StartTime.dwLowDateTime; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 105 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 106 | // Convert the hecto-nano second time to tv format. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 107 | Time -= FILETIME_1970; |
| 108 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 109 | tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); |
| 110 | tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 111 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 112 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 116 | class UnixRealTimeClock : public RealTimeClock { |
| 117 | public: |
| 118 | UnixRealTimeClock() {} |
| 119 | |
| 120 | virtual ~UnixRealTimeClock() {} |
| 121 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 122 | protected: |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 123 | virtual timeval CurrentTimeVal() const OVERRIDE { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 124 | struct timeval tv; |
| 125 | struct timezone tz; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 126 | tz.tz_minuteswest = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 127 | tz.tz_dsttime = 0; |
| 128 | gettimeofday(&tv, &tz); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 129 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | #endif |
| 133 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 134 | Clock* Clock::GetRealTimeClock() { |
| 135 | #if defined(_WIN32) |
wu@webrtc.org | 75718cf | 2014-05-15 23:54:14 +0000 | [diff] [blame] | 136 | static WindowsRealTimeClock clock; |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 137 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 138 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 139 | static UnixRealTimeClock clock; |
| 140 | return &clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 141 | #else |
| 142 | return NULL; |
| 143 | #endif |
| 144 | } |
| 145 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 146 | SimulatedClock::SimulatedClock(int64_t initial_time_us) |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 147 | : time_us_(initial_time_us), lock_(RWLockWrapper::CreateRWLock()) { |
| 148 | } |
| 149 | |
| 150 | SimulatedClock::~SimulatedClock() { |
| 151 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 152 | |
| 153 | int64_t SimulatedClock::TimeInMilliseconds() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 154 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 155 | return (time_us_ + 500) / 1000; |
| 156 | } |
| 157 | |
| 158 | int64_t SimulatedClock::TimeInMicroseconds() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 159 | ReadLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 160 | return time_us_; |
| 161 | } |
| 162 | |
| 163 | void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 164 | int64_t now_ms = TimeInMilliseconds(); |
| 165 | seconds = (now_ms / 1000) + kNtpJan1970; |
| 166 | fractions = |
| 167 | static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 168 | } |
| 169 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 170 | int64_t SimulatedClock::CurrentNtpInMilliseconds() { |
| 171 | return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 172 | } |
| 173 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 174 | void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
| 175 | AdvanceTimeMicroseconds(1000 * milliseconds); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 176 | } |
| 177 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 178 | void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 179 | WriteLockScoped synchronize(*lock_); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 180 | time_us_ += microseconds; |
| 181 | } |
| 182 | |
| 183 | }; // namespace webrtc |