xref: /aosp_15_r20/external/webrtc/test/drifting_clock.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "test/drifting_clock.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
16*d9f75844SAndroid Build Coastguard Worker namespace test {
17*d9f75844SAndroid Build Coastguard Worker constexpr float DriftingClock::kNoDrift;
18*d9f75844SAndroid Build Coastguard Worker 
DriftingClock(Clock * clock,float speed)19*d9f75844SAndroid Build Coastguard Worker DriftingClock::DriftingClock(Clock* clock, float speed)
20*d9f75844SAndroid Build Coastguard Worker     : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
21*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(clock);
22*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK_GT(speed, 0.0f);
23*d9f75844SAndroid Build Coastguard Worker }
24*d9f75844SAndroid Build Coastguard Worker 
Drift() const25*d9f75844SAndroid Build Coastguard Worker TimeDelta DriftingClock::Drift() const {
26*d9f75844SAndroid Build Coastguard Worker   auto now = clock_->CurrentTime();
27*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(now, start_time_);
28*d9f75844SAndroid Build Coastguard Worker   return (now - start_time_) * drift_;
29*d9f75844SAndroid Build Coastguard Worker }
30*d9f75844SAndroid Build Coastguard Worker 
Drift(Timestamp timestamp) const31*d9f75844SAndroid Build Coastguard Worker Timestamp DriftingClock::Drift(Timestamp timestamp) const {
32*d9f75844SAndroid Build Coastguard Worker   return timestamp + Drift() / 1000.;
33*d9f75844SAndroid Build Coastguard Worker }
34*d9f75844SAndroid Build Coastguard Worker 
Drift(NtpTime ntp_time) const35*d9f75844SAndroid Build Coastguard Worker NtpTime DriftingClock::Drift(NtpTime ntp_time) const {
36*d9f75844SAndroid Build Coastguard Worker   // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second.
37*d9f75844SAndroid Build Coastguard Worker   const double kNtpFracPerMicroSecond = 4294.967296;  // = 2^32 / 10^6
38*d9f75844SAndroid Build Coastguard Worker 
39*d9f75844SAndroid Build Coastguard Worker   uint64_t total_fractions = static_cast<uint64_t>(ntp_time);
40*d9f75844SAndroid Build Coastguard Worker   total_fractions += Drift().us() * kNtpFracPerMicroSecond;
41*d9f75844SAndroid Build Coastguard Worker   return NtpTime(total_fractions);
42*d9f75844SAndroid Build Coastguard Worker }
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker }  // namespace test
45*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
46