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.

86 lines
2.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 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. #ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
  11. #define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include "api/units/timestamp.h"
  15. #include "rtc_base/synchronization/rw_lock_wrapper.h"
  16. #include "system_wrappers/include/ntp_time.h"
  17. namespace webrtc {
  18. // January 1970, in NTP seconds.
  19. const uint32_t kNtpJan1970 = 2208988800UL;
  20. // Magic NTP fractional unit.
  21. const double kMagicNtpFractionalUnit = 4.294967296E+9;
  22. // A clock interface that allows reading of absolute and relative timestamps.
  23. class Clock {
  24. public:
  25. virtual ~Clock() {}
  26. // Return a timestamp relative to an unspecified epoch.
  27. virtual Timestamp CurrentTime() {
  28. return Timestamp::us(TimeInMicroseconds());
  29. }
  30. virtual int64_t TimeInMilliseconds() { return CurrentTime().ms(); }
  31. virtual int64_t TimeInMicroseconds() { return CurrentTime().us(); }
  32. // Retrieve an NTP absolute timestamp.
  33. virtual NtpTime CurrentNtpTime() = 0;
  34. // Retrieve an NTP absolute timestamp in milliseconds.
  35. virtual int64_t CurrentNtpInMilliseconds() = 0;
  36. // Converts an NTP timestamp to a millisecond timestamp.
  37. static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
  38. return NtpTime(seconds, fractions).ToMs();
  39. }
  40. // Returns an instance of the real-time system clock implementation.
  41. static Clock* GetRealTimeClock();
  42. };
  43. class SimulatedClock : public Clock {
  44. public:
  45. explicit SimulatedClock(int64_t initial_time_us);
  46. explicit SimulatedClock(Timestamp initial_time);
  47. ~SimulatedClock() override;
  48. // Return a timestamp relative to some arbitrary source; the source is fixed
  49. // for this clock.
  50. Timestamp CurrentTime() override;
  51. // Retrieve an NTP absolute timestamp.
  52. NtpTime CurrentNtpTime() override;
  53. // Converts an NTP timestamp to a millisecond timestamp.
  54. int64_t CurrentNtpInMilliseconds() override;
  55. // Advance the simulated clock with a given number of milliseconds or
  56. // microseconds.
  57. void AdvanceTimeMilliseconds(int64_t milliseconds);
  58. void AdvanceTimeMicroseconds(int64_t microseconds);
  59. void AdvanceTime(TimeDelta delta);
  60. private:
  61. Timestamp time_;
  62. std::unique_ptr<RWLockWrapper> lock_;
  63. };
  64. } // namespace webrtc
  65. #endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_