blob: 2907ad0ec228e7a45b676fdb8e6621f01e96dafe [file] [log] [blame]
danilchap9c6a0c72016-02-10 10:54:47 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/drifting_clock.h"
12#include "rtc_base/checks.h"
danilchap9c6a0c72016-02-10 10:54:47 -080013
14namespace webrtc {
15namespace test {
16const float DriftingClock::kDoubleSpeed = 2.0f;
17const float DriftingClock::kNoDrift = 1.0f;
18const float DriftingClock::kHalfSpeed = 0.5f;
19
20DriftingClock::DriftingClock(Clock* clock, float speed)
Sebastian Jansson4de31152019-06-11 08:52:11 +020021 : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
danilchap9c6a0c72016-02-10 10:54:47 -080022 RTC_CHECK(clock);
23 RTC_CHECK_GT(speed, 0.0f);
24}
25
Sebastian Jansson4de31152019-06-11 08:52:11 +020026TimeDelta DriftingClock::Drift() const {
27 auto now = clock_->CurrentTime();
danilchap9c6a0c72016-02-10 10:54:47 -080028 RTC_DCHECK_GE(now, start_time_);
29 return (now - start_time_) * drift_;
30}
31
Sebastian Jansson4de31152019-06-11 08:52:11 +020032Timestamp DriftingClock::CurrentTime() {
33 return clock_->CurrentTime() + Drift() / 1000.;
danilchap9c6a0c72016-02-10 10:54:47 -080034}
35
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010036NtpTime DriftingClock::CurrentNtpTime() {
danilchap9c6a0c72016-02-10 10:54:47 -080037 // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second.
38 const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6
39
danilchap21dc1892017-03-07 02:51:09 -080040 NtpTime ntp = clock_->CurrentNtpTime();
41 uint64_t total_fractions = static_cast<uint64_t>(ntp);
Sebastian Jansson4de31152019-06-11 08:52:11 +020042 total_fractions += Drift().us() * kNtpFracPerMicroSecond;
danilchap21dc1892017-03-07 02:51:09 -080043 return NtpTime(total_fractions);
danilchap9c6a0c72016-02-10 10:54:47 -080044}
45
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010046int64_t DriftingClock::CurrentNtpInMilliseconds() {
Sebastian Jansson4de31152019-06-11 08:52:11 +020047 return clock_->CurrentNtpInMilliseconds() + Drift().ms();
danilchap9c6a0c72016-02-10 10:54:47 -080048}
49} // namespace test
50} // namespace webrtc