• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Drift(Timestamp timestamp) const31 Timestamp DriftingClock::Drift(Timestamp timestamp) const {
32   return timestamp + Drift() / 1000.;
33 }
34 
Drift(NtpTime ntp_time) const35 NtpTime DriftingClock::Drift(NtpTime ntp_time) const {
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   uint64_t total_fractions = static_cast<uint64_t>(ntp_time);
40   total_fractions += Drift().us() * kNtpFracPerMicroSecond;
41   return NtpTime(total_fractions);
42 }
43 
44 }  // namespace test
45 }  // namespace webrtc
46