blob: 1a5154557e4d1a5953ac0956efc3c6bc21763e14 [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"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
danilchap9c6a0c72016-02-10 10:54:47 -080014
15namespace webrtc {
16namespace test {
Danil Chapovalov01b7e922019-09-10 13:28:58 +020017constexpr float DriftingClock::kNoDrift;
danilchap9c6a0c72016-02-10 10:54:47 -080018
19DriftingClock::DriftingClock(Clock* clock, float speed)
Sebastian Jansson4de31152019-06-11 08:52:11 +020020 : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
danilchap9c6a0c72016-02-10 10:54:47 -080021 RTC_CHECK(clock);
22 RTC_CHECK_GT(speed, 0.0f);
23}
24
Sebastian Jansson4de31152019-06-11 08:52:11 +020025TimeDelta DriftingClock::Drift() const {
26 auto now = clock_->CurrentTime();
danilchap9c6a0c72016-02-10 10:54:47 -080027 RTC_DCHECK_GE(now, start_time_);
28 return (now - start_time_) * drift_;
29}
30
Sebastian Jansson4de31152019-06-11 08:52:11 +020031Timestamp DriftingClock::CurrentTime() {
32 return clock_->CurrentTime() + Drift() / 1000.;
danilchap9c6a0c72016-02-10 10:54:47 -080033}
34
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010035NtpTime DriftingClock::CurrentNtpTime() {
danilchap9c6a0c72016-02-10 10:54:47 -080036 // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second.
37 const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6
38
danilchap21dc1892017-03-07 02:51:09 -080039 NtpTime ntp = clock_->CurrentNtpTime();
40 uint64_t total_fractions = static_cast<uint64_t>(ntp);
Sebastian Jansson4de31152019-06-11 08:52:11 +020041 total_fractions += Drift().us() * kNtpFracPerMicroSecond;
danilchap21dc1892017-03-07 02:51:09 -080042 return NtpTime(total_fractions);
danilchap9c6a0c72016-02-10 10:54:47 -080043}
44
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010045int64_t DriftingClock::CurrentNtpInMilliseconds() {
Sebastian Jansson4de31152019-06-11 08:52:11 +020046 return clock_->CurrentNtpInMilliseconds() + Drift().ms();
danilchap9c6a0c72016-02-10 10:54:47 -080047}
48} // namespace test
49} // namespace webrtc