danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/drifting_clock.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | namespace test { |
| 17 | const float DriftingClock::kDoubleSpeed = 2.0f; |
| 18 | const float DriftingClock::kNoDrift = 1.0f; |
| 19 | const float DriftingClock::kHalfSpeed = 0.5f; |
| 20 | |
| 21 | DriftingClock::DriftingClock(Clock* clock, float speed) |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 22 | : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) { |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 23 | RTC_CHECK(clock); |
| 24 | RTC_CHECK_GT(speed, 0.0f); |
| 25 | } |
| 26 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 27 | TimeDelta DriftingClock::Drift() const { |
| 28 | auto now = clock_->CurrentTime(); |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 29 | RTC_DCHECK_GE(now, start_time_); |
| 30 | return (now - start_time_) * drift_; |
| 31 | } |
| 32 | |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 33 | Timestamp DriftingClock::CurrentTime() { |
| 34 | return clock_->CurrentTime() + Drift() / 1000.; |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 37 | NtpTime DriftingClock::CurrentNtpTime() { |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 38 | // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second. |
| 39 | const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6 |
| 40 | |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 41 | NtpTime ntp = clock_->CurrentNtpTime(); |
| 42 | uint64_t total_fractions = static_cast<uint64_t>(ntp); |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 43 | total_fractions += Drift().us() * kNtpFracPerMicroSecond; |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 44 | return NtpTime(total_fractions); |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Sebastian Jansson | 2a96ab2 | 2019-01-30 20:44:45 +0100 | [diff] [blame] | 47 | int64_t DriftingClock::CurrentNtpInMilliseconds() { |
Sebastian Jansson | 4de3115 | 2019-06-11 08:52:11 +0200 | [diff] [blame] | 48 | return clock_->CurrentNtpInMilliseconds() + Drift().ms(); |
danilchap | 9c6a0c7 | 2016-02-10 10:54:47 -0800 | [diff] [blame] | 49 | } |
| 50 | } // namespace test |
| 51 | } // namespace webrtc |