clock.h 2.6 KB

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