You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "test/drifting_clock.h"
  11. #include "rtc_base/checks.h"
  12. namespace webrtc {
  13. namespace test {
  14. constexpr float DriftingClock::kNoDrift;
  15. DriftingClock::DriftingClock(Clock* clock, float speed)
  16. : clock_(clock), drift_(speed - 1.0f), start_time_(clock_->CurrentTime()) {
  17. RTC_CHECK(clock);
  18. RTC_CHECK_GT(speed, 0.0f);
  19. }
  20. TimeDelta DriftingClock::Drift() const {
  21. auto now = clock_->CurrentTime();
  22. RTC_DCHECK_GE(now, start_time_);
  23. return (now - start_time_) * drift_;
  24. }
  25. Timestamp DriftingClock::CurrentTime() {
  26. return clock_->CurrentTime() + Drift() / 1000.;
  27. }
  28. NtpTime DriftingClock::CurrentNtpTime() {
  29. // NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second.
  30. const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6
  31. NtpTime ntp = clock_->CurrentNtpTime();
  32. uint64_t total_fractions = static_cast<uint64_t>(ntp);
  33. total_fractions += Drift().us() * kNtpFracPerMicroSecond;
  34. return NtpTime(total_fractions);
  35. }
  36. int64_t DriftingClock::CurrentNtpInMilliseconds() {
  37. return clock_->CurrentNtpInMilliseconds() + Drift().ms();
  38. }
  39. } // namespace test
  40. } // namespace webrtc