blob: 0e3d2a6c6e9e1f488bf822e983b6078e9d8f7f52 [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 {
17const float DriftingClock::kDoubleSpeed = 2.0f;
18const float DriftingClock::kNoDrift = 1.0f;
19const float DriftingClock::kHalfSpeed = 0.5f;
20
21DriftingClock::DriftingClock(Clock* clock, float speed)
Sebastian Jansson4de31152019-06-11 08:52:11 +020022 : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
danilchap9c6a0c72016-02-10 10:54:47 -080023 RTC_CHECK(clock);
24 RTC_CHECK_GT(speed, 0.0f);
25}
26
Sebastian Jansson4de31152019-06-11 08:52:11 +020027TimeDelta DriftingClock::Drift() const {
28 auto now = clock_->CurrentTime();
danilchap9c6a0c72016-02-10 10:54:47 -080029 RTC_DCHECK_GE(now, start_time_);
30 return (now - start_time_) * drift_;
31}
32
Sebastian Jansson4de31152019-06-11 08:52:11 +020033Timestamp DriftingClock::CurrentTime() {
34 return clock_->CurrentTime() + Drift() / 1000.;
danilchap9c6a0c72016-02-10 10:54:47 -080035}
36
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010037NtpTime DriftingClock::CurrentNtpTime() {
danilchap9c6a0c72016-02-10 10:54:47 -080038 // 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
danilchap21dc1892017-03-07 02:51:09 -080041 NtpTime ntp = clock_->CurrentNtpTime();
42 uint64_t total_fractions = static_cast<uint64_t>(ntp);
Sebastian Jansson4de31152019-06-11 08:52:11 +020043 total_fractions += Drift().us() * kNtpFracPerMicroSecond;
danilchap21dc1892017-03-07 02:51:09 -080044 return NtpTime(total_fractions);
danilchap9c6a0c72016-02-10 10:54:47 -080045}
46
Sebastian Jansson2a96ab22019-01-30 20:44:45 +010047int64_t DriftingClock::CurrentNtpInMilliseconds() {
Sebastian Jansson4de31152019-06-11 08:52:11 +020048 return clock_->CurrentNtpInMilliseconds() + Drift().ms();
danilchap9c6a0c72016-02-10 10:54:47 -080049}
50} // namespace test
51} // namespace webrtc