blob: 47c8e56916f7b8563beb5a6f372269b564e96a6f [file] [log] [blame]
danilchap9c6a0c72016-02-10 18:54:471/*
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 04:47:3111#include "test/drifting_clock.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/checks.h"
danilchap9c6a0c72016-02-10 18:54:4714
15namespace webrtc {
16namespace test {
Danil Chapovalov01b7e922019-09-10 11:28:5817constexpr float DriftingClock::kNoDrift;
danilchap9c6a0c72016-02-10 18:54:4718
19DriftingClock::DriftingClock(Clock* clock, float speed)
Sebastian Jansson4de31152019-06-11 06:52:1120 : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
danilchap9c6a0c72016-02-10 18:54:4721 RTC_CHECK(clock);
22 RTC_CHECK_GT(speed, 0.0f);
23}
24
Sebastian Jansson4de31152019-06-11 06:52:1125TimeDelta DriftingClock::Drift() const {
26 auto now = clock_->CurrentTime();
danilchap9c6a0c72016-02-10 18:54:4727 RTC_DCHECK_GE(now, start_time_);
28 return (now - start_time_) * drift_;
29}
30
Paul Hallakb59e9042021-05-20 15:21:4931Timestamp DriftingClock::Drift(Timestamp timestamp) const {
32 return timestamp + Drift() / 1000.;
danilchap9c6a0c72016-02-10 18:54:4733}
34
Paul Hallakb59e9042021-05-20 15:21:4935NtpTime DriftingClock::Drift(NtpTime ntp_time) const {
danilchap9c6a0c72016-02-10 18:54:4736 // 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
Paul Hallakb59e9042021-05-20 15:21:4939 uint64_t total_fractions = static_cast<uint64_t>(ntp_time);
Sebastian Jansson4de31152019-06-11 06:52:1140 total_fractions += Drift().us() * kNtpFracPerMicroSecond;
danilchap21dc1892017-03-07 10:51:0941 return NtpTime(total_fractions);
danilchap9c6a0c72016-02-10 18:54:4742}
43
danilchap9c6a0c72016-02-10 18:54:4744} // namespace test
45} // namespace webrtc