danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 04:47:31 | [diff] [blame] | 11 | #include "test/drifting_clock.h" |
| 12 | #include "rtc_base/checks.h" |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | namespace test { |
| 16 | const float DriftingClock::kDoubleSpeed = 2.0f; |
| 17 | const float DriftingClock::kNoDrift = 1.0f; |
| 18 | const float DriftingClock::kHalfSpeed = 0.5f; |
| 19 | |
| 20 | DriftingClock::DriftingClock(Clock* clock, float speed) |
| 21 | : clock_(clock), |
| 22 | drift_(speed - 1.0f), |
| 23 | start_time_(clock_->TimeInMicroseconds()) { |
| 24 | RTC_CHECK(clock); |
| 25 | RTC_CHECK_GT(speed, 0.0f); |
| 26 | } |
| 27 | |
| 28 | float DriftingClock::Drift() const { |
| 29 | int64_t now = clock_->TimeInMicroseconds(); |
| 30 | RTC_DCHECK_GE(now, start_time_); |
| 31 | return (now - start_time_) * drift_; |
| 32 | } |
| 33 | |
| 34 | int64_t DriftingClock::TimeInMilliseconds() const { |
| 35 | return clock_->TimeInMilliseconds() + Drift() / 1000.; |
| 36 | } |
| 37 | |
| 38 | int64_t DriftingClock::TimeInMicroseconds() const { |
| 39 | return clock_->TimeInMicroseconds() + Drift(); |
| 40 | } |
| 41 | |
danilchap | 21dc189 | 2017-03-07 10:51:09 | [diff] [blame] | 42 | NtpTime DriftingClock::CurrentNtpTime() const { |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 43 | // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second. |
| 44 | const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6 |
| 45 | |
danilchap | 21dc189 | 2017-03-07 10:51:09 | [diff] [blame] | 46 | NtpTime ntp = clock_->CurrentNtpTime(); |
| 47 | uint64_t total_fractions = static_cast<uint64_t>(ntp); |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 48 | total_fractions += Drift() * kNtpFracPerMicroSecond; |
danilchap | 21dc189 | 2017-03-07 10:51:09 | [diff] [blame] | 49 | return NtpTime(total_fractions); |
danilchap | 9c6a0c7 | 2016-02-10 18:54:47 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int64_t DriftingClock::CurrentNtpInMilliseconds() const { |
| 53 | return clock_->CurrentNtpInMilliseconds() + Drift() / 1000.; |
| 54 | } |
| 55 | } // namespace test |
| 56 | } // namespace webrtc |