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 
11 #include "test/drifting_clock.h"
12 
13 #include "rtc_base/checks.h"
14 
15 namespace webrtc {
16 namespace test {
17 constexpr float DriftingClock::kNoDrift;
18 
DriftingClock(Clock * clock,float speed)19 DriftingClock::DriftingClock(Clock* clock, float speed)
20     : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
21   RTC_CHECK(clock);
22   RTC_CHECK_GT(speed, 0.0f);
23 }
24 
Drift() const25 TimeDelta DriftingClock::Drift() const {
26   auto now = clock_->CurrentTime();
27   RTC_DCHECK_GE(now, start_time_);
28   return (now - start_time_) * drift_;
29 }
30 
CurrentTime()31 Timestamp DriftingClock::CurrentTime() {
32   return clock_->CurrentTime() + Drift() / 1000.;
33 }
34 
CurrentNtpTime()35 NtpTime DriftingClock::CurrentNtpTime() {
36   // 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 
39   NtpTime ntp = clock_->CurrentNtpTime();
40   uint64_t total_fractions = static_cast<uint64_t>(ntp);
41   total_fractions += Drift().us() * kNtpFracPerMicroSecond;
42   return NtpTime(total_fractions);
43 }
44 
CurrentNtpInMilliseconds()45 int64_t DriftingClock::CurrentNtpInMilliseconds() {
46   return clock_->CurrentNtpInMilliseconds() + Drift().ms();
47 }
48 }  // namespace test
49 }  // namespace webrtc
50