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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/clock.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 12 | |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 13 | #if defined(WEBRTC_WIN) |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 14 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 15 | // Windows needs to be included before mmsystem.h |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/win32.h" |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 17 | |
Mirko Bonadei | 0191441 | 2018-03-23 16:30:58 +0100 | [diff] [blame] | 18 | #include <mmsystem.h> |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 19 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/critical_section.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 22 | #elif defined(WEBRTC_POSIX) |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 23 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 24 | #include <sys/time.h> |
| 25 | #include <time.h> |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 26 | |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 27 | #endif // defined(WEBRTC_POSIX) |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 28 | |
Karl Wiberg | 2b85792 | 2018-03-23 14:53:54 +0100 | [diff] [blame] | 29 | #include "rtc_base/synchronization/rw_lock_wrapper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "rtc_base/time_utils.h" |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 34 | class RealTimeClock : public Clock { |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 35 | Timestamp CurrentTime() override { return Timestamp::us(rtc::TimeMicros()); } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 36 | // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 37 | // source is fixed for this clock. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 38 | int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 39 | |
| 40 | // Return a timestamp in microseconds relative to some arbitrary source; the |
| 41 | // source is fixed for this clock. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 42 | int64_t TimeInMicroseconds() override { return rtc::TimeMicros(); } |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 43 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 44 | // Retrieve an NTP absolute timestamp. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 45 | NtpTime CurrentNtpTime() override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 46 | timeval tv = CurrentTimeVal(); |
| 47 | double microseconds_in_seconds; |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 48 | uint32_t seconds; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 49 | Adjust(tv, &seconds, µseconds_in_seconds); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 50 | uint32_t fractions = static_cast<uint32_t>( |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 51 | microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 52 | return NtpTime(seconds, fractions); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Retrieve an NTP absolute timestamp in milliseconds. |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 56 | int64_t CurrentNtpInMilliseconds() override { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 57 | timeval tv = CurrentTimeVal(); |
| 58 | uint32_t seconds; |
| 59 | double microseconds_in_seconds; |
| 60 | Adjust(tv, &seconds, µseconds_in_seconds); |
| 61 | return 1000 * static_cast<int64_t>(seconds) + |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 62 | static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | protected: |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 66 | virtual timeval CurrentTimeVal() = 0; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 67 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 68 | static void Adjust(const timeval& tv, |
| 69 | uint32_t* adjusted_s, |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 70 | double* adjusted_us_in_s) { |
| 71 | *adjusted_s = tv.tv_sec + kNtpJan1970; |
| 72 | *adjusted_us_in_s = tv.tv_usec / 1e6; |
| 73 | |
| 74 | if (*adjusted_us_in_s >= 1) { |
| 75 | *adjusted_us_in_s -= 1; |
| 76 | ++*adjusted_s; |
| 77 | } else if (*adjusted_us_in_s < -1) { |
| 78 | *adjusted_us_in_s += 1; |
| 79 | --*adjusted_s; |
| 80 | } |
| 81 | } |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 84 | #if defined(WINUWP) |
| 85 | class WinUwpRealTimeClock final : public RealTimeClock { |
| 86 | public: |
| 87 | WinUwpRealTimeClock() = default; |
| 88 | ~WinUwpRealTimeClock() override {} |
| 89 | |
| 90 | protected: |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 91 | timeval CurrentTimeVal() override { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 92 | // The rtc::SystemTimeNanos() method is already time offset from a base |
| 93 | // epoch value and might as be synchronized against an NTP time server as |
| 94 | // an added bonus. |
| 95 | auto nanos = rtc::SystemTimeNanos(); |
| 96 | |
| 97 | struct timeval tv; |
| 98 | |
| 99 | tv.tv_sec = rtc::dchecked_cast<long>(nanos / 1000000000); |
| 100 | tv.tv_usec = rtc::dchecked_cast<long>(nanos / 1000); |
| 101 | |
| 102 | return tv; |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | #elif defined(WEBRTC_WIN) |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 107 | // TODO(pbos): Consider modifying the implementation to synchronize itself |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 108 | // against system time (update ref_point_) periodically to |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 109 | // prevent clock drift. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 110 | class WindowsRealTimeClock : public RealTimeClock { |
| 111 | public: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 112 | WindowsRealTimeClock() |
| 113 | : last_time_ms_(0), |
| 114 | num_timer_wraps_(0), |
| 115 | ref_point_(GetSystemReferencePoint()) {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 116 | |
Mirko Bonadei | c14d9bb | 2018-07-16 15:44:28 +0200 | [diff] [blame] | 117 | ~WindowsRealTimeClock() override {} |
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 | protected: |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 120 | struct ReferencePoint { |
| 121 | FILETIME file_time; |
| 122 | LARGE_INTEGER counter_ms; |
| 123 | }; |
| 124 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 125 | timeval CurrentTimeVal() override { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 126 | const uint64_t FILETIME_1970 = 0x019db1ded53e8000; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 127 | |
| 128 | FILETIME StartTime; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 129 | uint64_t Time; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 130 | struct timeval tv; |
| 131 | |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 132 | // We can't use query performance counter since they can change depending on |
| 133 | // speed stepping. |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 134 | GetTime(&StartTime); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 135 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 136 | Time = (((uint64_t)StartTime.dwHighDateTime) << 32) + |
| 137 | (uint64_t)StartTime.dwLowDateTime; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 138 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 139 | // Convert the hecto-nano second time to tv format. |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 140 | Time -= FILETIME_1970; |
| 141 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 142 | tv.tv_sec = (uint32_t)(Time / (uint64_t)10000000); |
| 143 | tv.tv_usec = (uint32_t)((Time % (uint64_t)10000000) / 10); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 144 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 145 | } |
wu@webrtc.org | 7a9a3b7 | 2014-05-29 19:40:28 +0000 | [diff] [blame] | 146 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 147 | void GetTime(FILETIME* current_time) { |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 148 | DWORD t; |
| 149 | LARGE_INTEGER elapsed_ms; |
| 150 | { |
| 151 | rtc::CritScope lock(&crit_); |
| 152 | // time MUST be fetched inside the critical section to avoid non-monotonic |
| 153 | // last_time_ms_ values that'll register as incorrect wraparounds due to |
| 154 | // concurrent calls to GetTime. |
| 155 | t = timeGetTime(); |
| 156 | if (t < last_time_ms_) |
| 157 | num_timer_wraps_++; |
| 158 | last_time_ms_ = t; |
| 159 | elapsed_ms.HighPart = num_timer_wraps_; |
| 160 | } |
| 161 | elapsed_ms.LowPart = t; |
| 162 | elapsed_ms.QuadPart = elapsed_ms.QuadPart - ref_point_.counter_ms.QuadPart; |
| 163 | |
| 164 | // Translate to 100-nanoseconds intervals (FILETIME resolution) |
| 165 | // and add to reference FILETIME to get current FILETIME. |
| 166 | ULARGE_INTEGER filetime_ref_as_ul; |
| 167 | filetime_ref_as_ul.HighPart = ref_point_.file_time.dwHighDateTime; |
| 168 | filetime_ref_as_ul.LowPart = ref_point_.file_time.dwLowDateTime; |
| 169 | filetime_ref_as_ul.QuadPart += |
| 170 | static_cast<ULONGLONG>((elapsed_ms.QuadPart) * 1000 * 10); |
| 171 | |
| 172 | // Copy to result |
| 173 | current_time->dwHighDateTime = filetime_ref_as_ul.HighPart; |
| 174 | current_time->dwLowDateTime = filetime_ref_as_ul.LowPart; |
| 175 | } |
| 176 | |
| 177 | static ReferencePoint GetSystemReferencePoint() { |
dcheng | a771bf8 | 2015-07-01 17:52:10 -0700 | [diff] [blame] | 178 | ReferencePoint ref = {}; |
| 179 | FILETIME ft0 = {}; |
| 180 | FILETIME ft1 = {}; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 181 | // Spin waiting for a change in system time. As soon as this change happens, |
| 182 | // get the matching call for timeGetTime() as soon as possible. This is |
| 183 | // assumed to be the most accurate offset that we can get between |
| 184 | // timeGetTime() and system time. |
| 185 | |
| 186 | // Set timer accuracy to 1 ms. |
| 187 | timeBeginPeriod(1); |
| 188 | GetSystemTimeAsFileTime(&ft0); |
| 189 | do { |
| 190 | GetSystemTimeAsFileTime(&ft1); |
| 191 | |
| 192 | ref.counter_ms.QuadPart = timeGetTime(); |
| 193 | Sleep(0); |
| 194 | } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) && |
| 195 | (ft0.dwLowDateTime == ft1.dwLowDateTime)); |
| 196 | ref.file_time = ft1; |
| 197 | timeEndPeriod(1); |
| 198 | return ref; |
| 199 | } |
| 200 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 201 | rtc::CriticalSection crit_; |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 202 | DWORD last_time_ms_; |
| 203 | LONG num_timer_wraps_; |
pbos@webrtc.org | f938922 | 2015-01-21 12:51:13 +0000 | [diff] [blame] | 204 | const ReferencePoint ref_point_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 207 | #elif defined(WEBRTC_POSIX) |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 208 | class UnixRealTimeClock : public RealTimeClock { |
| 209 | public: |
| 210 | UnixRealTimeClock() {} |
| 211 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 212 | ~UnixRealTimeClock() override {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 213 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 214 | protected: |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 215 | timeval CurrentTimeVal() override { |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 216 | struct timeval tv; |
| 217 | struct timezone tz; |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 218 | tz.tz_minuteswest = 0; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 219 | tz.tz_dsttime = 0; |
| 220 | gettimeofday(&tv, &tz); |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 221 | return tv; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 222 | } |
| 223 | }; |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 224 | #endif // defined(WEBRTC_POSIX) |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 225 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 226 | Clock* Clock::GetRealTimeClock() { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 227 | #if defined(WINUWP) |
| 228 | static Clock* const clock = new WinUwpRealTimeClock(); |
| 229 | #elif defined(WEBRTC_WIN) |
Mirko Bonadei | 6c092d2 | 2018-09-10 13:27:11 +0200 | [diff] [blame] | 230 | static Clock* const clock = new WindowsRealTimeClock(); |
Sergey Ulanov | 6acefdb | 2017-12-11 17:38:13 -0800 | [diff] [blame] | 231 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | 6c092d2 | 2018-09-10 13:27:11 +0200 | [diff] [blame] | 232 | static Clock* const clock = new UnixRealTimeClock(); |
| 233 | #else |
| 234 | static Clock* const clock = nullptr; |
| 235 | #endif |
| 236 | return clock; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 237 | } |
| 238 | |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 239 | SimulatedClock::SimulatedClock(int64_t initial_time_us) |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 240 | : SimulatedClock(Timestamp::us(initial_time_us)) {} |
| 241 | |
| 242 | SimulatedClock::SimulatedClock(Timestamp initial_time) |
| 243 | : time_(initial_time), lock_(RWLockWrapper::CreateRWLock()) {} |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 244 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 245 | SimulatedClock::~SimulatedClock() {} |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 246 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 247 | Timestamp SimulatedClock::CurrentTime() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 248 | ReadLockScoped synchronize(*lock_); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 249 | return time_; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 252 | NtpTime SimulatedClock::CurrentNtpTime() { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 253 | int64_t now_ms = TimeInMilliseconds(); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 254 | uint32_t seconds = (now_ms / 1000) + kNtpJan1970; |
| 255 | uint32_t fractions = |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 256 | static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 257 | return NtpTime(seconds, fractions); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 260 | int64_t SimulatedClock::CurrentNtpInMilliseconds() { |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 261 | return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 262 | } |
| 263 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 264 | void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 265 | AdvanceTime(TimeDelta::ms(milliseconds)); |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 266 | } |
| 267 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 268 | void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 269 | AdvanceTime(TimeDelta::us(microseconds)); |
| 270 | } |
| 271 | |
| 272 | void SimulatedClock::AdvanceTime(TimeDelta delta) { |
henrik.lundin@webrtc.org | 59336e8 | 2014-05-27 09:34:58 +0000 | [diff] [blame] | 273 | WriteLockScoped synchronize(*lock_); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame^] | 274 | time_ += delta; |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 277 | } // namespace webrtc |