| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/system_wrappers/include/clock.h" | 11 #include "webrtc/system_wrappers/include/clock.h" |
| 12 | 12 |
| 13 #if defined(_WIN32) | 13 #if defined(_WIN32) |
| 14 // Windows needs to be included before mmsystem.h | 14 // Windows needs to be included before mmsystem.h |
| 15 #include "webrtc/base/win32.h" | 15 #include "webrtc/base/win32.h" |
| 16 #include <MMSystem.h> | 16 #include <MMSystem.h> |
| 17 #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) | 17 #elif ((defined WEBRTC_LINUX) || (defined WEBRTC_MAC)) |
| 18 #include <sys/time.h> | 18 #include <sys/time.h> |
| 19 #include <time.h> | 19 #include <time.h> |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 #include "webrtc/base/criticalsection.h" | 22 #include "webrtc/base/criticalsection.h" |
| 23 #include "webrtc/base/timeutils.h" | 23 #include "webrtc/base/timeutils.h" |
| 24 #include "webrtc/system_wrappers/include/rw_lock_wrapper.h" | 24 #include "webrtc/system_wrappers/include/rw_lock_wrapper.h" |
| 25 | 25 |
| 26 namespace webrtc { | 26 namespace webrtc { |
| 27 | 27 |
| 28 NtpTime Clock::CurrentNtpTime() const { | |
| 29 uint32_t seconds; | |
| 30 uint32_t fractions; | |
| 31 CurrentNtp(seconds, fractions); | |
| 32 return NtpTime(seconds, fractions); | |
| 33 } | |
| 34 | |
| 35 class RealTimeClock : public Clock { | 28 class RealTimeClock : public Clock { |
| 36 // Return a timestamp in milliseconds relative to some arbitrary source; the | 29 // Return a timestamp in milliseconds relative to some arbitrary source; the |
| 37 // source is fixed for this clock. | 30 // source is fixed for this clock. |
| 38 int64_t TimeInMilliseconds() const override { | 31 int64_t TimeInMilliseconds() const override { |
| 39 return rtc::TimeMillis(); | 32 return rtc::TimeMillis(); |
| 40 } | 33 } |
| 41 | 34 |
| 42 // Return a timestamp in microseconds relative to some arbitrary source; the | 35 // Return a timestamp in microseconds relative to some arbitrary source; the |
| 43 // source is fixed for this clock. | 36 // source is fixed for this clock. |
| 44 int64_t TimeInMicroseconds() const override { | 37 int64_t TimeInMicroseconds() const override { |
| 45 return rtc::TimeMicros(); | 38 return rtc::TimeMicros(); |
| 46 } | 39 } |
| 47 | 40 |
| 48 // Retrieve an NTP absolute timestamp in seconds and fractions of a second. | 41 // Retrieve an NTP absolute timestamp. |
| 49 void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const override { | 42 NtpTime CurrentNtpTime() const override { |
| 50 timeval tv = CurrentTimeVal(); | 43 timeval tv = CurrentTimeVal(); |
| 51 double microseconds_in_seconds; | 44 double microseconds_in_seconds; |
| 45 uint32_t seconds; |
| 52 Adjust(tv, &seconds, µseconds_in_seconds); | 46 Adjust(tv, &seconds, µseconds_in_seconds); |
| 53 fractions = static_cast<uint32_t>( | 47 uint32_t fractions = static_cast<uint32_t>( |
| 54 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); | 48 microseconds_in_seconds * kMagicNtpFractionalUnit + 0.5); |
| 49 return NtpTime(seconds, fractions); |
| 55 } | 50 } |
| 56 | 51 |
| 57 // Retrieve an NTP absolute timestamp in milliseconds. | 52 // Retrieve an NTP absolute timestamp in milliseconds. |
| 58 int64_t CurrentNtpInMilliseconds() const override { | 53 int64_t CurrentNtpInMilliseconds() const override { |
| 59 timeval tv = CurrentTimeVal(); | 54 timeval tv = CurrentTimeVal(); |
| 60 uint32_t seconds; | 55 uint32_t seconds; |
| 61 double microseconds_in_seconds; | 56 double microseconds_in_seconds; |
| 62 Adjust(tv, &seconds, µseconds_in_seconds); | 57 Adjust(tv, &seconds, µseconds_in_seconds); |
| 63 return 1000 * static_cast<int64_t>(seconds) + | 58 return 1000 * static_cast<int64_t>(seconds) + |
| 64 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); | 59 static_cast<int64_t>(1000.0 * microseconds_in_seconds + 0.5); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 int64_t SimulatedClock::TimeInMilliseconds() const { | 235 int64_t SimulatedClock::TimeInMilliseconds() const { |
| 241 ReadLockScoped synchronize(*lock_); | 236 ReadLockScoped synchronize(*lock_); |
| 242 return (time_us_ + 500) / 1000; | 237 return (time_us_ + 500) / 1000; |
| 243 } | 238 } |
| 244 | 239 |
| 245 int64_t SimulatedClock::TimeInMicroseconds() const { | 240 int64_t SimulatedClock::TimeInMicroseconds() const { |
| 246 ReadLockScoped synchronize(*lock_); | 241 ReadLockScoped synchronize(*lock_); |
| 247 return time_us_; | 242 return time_us_; |
| 248 } | 243 } |
| 249 | 244 |
| 250 void SimulatedClock::CurrentNtp(uint32_t& seconds, uint32_t& fractions) const { | 245 NtpTime SimulatedClock::CurrentNtpTime() const { |
| 251 int64_t now_ms = TimeInMilliseconds(); | 246 int64_t now_ms = TimeInMilliseconds(); |
| 252 seconds = (now_ms / 1000) + kNtpJan1970; | 247 uint32_t seconds = (now_ms / 1000) + kNtpJan1970; |
| 253 fractions = | 248 uint32_t fractions = |
| 254 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); | 249 static_cast<uint32_t>((now_ms % 1000) * kMagicNtpFractionalUnit / 1000); |
| 250 return NtpTime(seconds, fractions); |
| 255 } | 251 } |
| 256 | 252 |
| 257 int64_t SimulatedClock::CurrentNtpInMilliseconds() const { | 253 int64_t SimulatedClock::CurrentNtpInMilliseconds() const { |
| 258 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); | 254 return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970); |
| 259 } | 255 } |
| 260 | 256 |
| 261 void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { | 257 void SimulatedClock::AdvanceTimeMilliseconds(int64_t milliseconds) { |
| 262 AdvanceTimeMicroseconds(1000 * milliseconds); | 258 AdvanceTimeMicroseconds(1000 * milliseconds); |
| 263 } | 259 } |
| 264 | 260 |
| 265 void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { | 261 void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { |
| 266 WriteLockScoped synchronize(*lock_); | 262 WriteLockScoped synchronize(*lock_); |
| 267 time_us_ += microseconds; | 263 time_us_ += microseconds; |
| 268 } | 264 } |
| 269 | 265 |
| 270 }; // namespace webrtc | 266 }; // namespace webrtc |
| OLD | NEW |