tick_timer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef API_NETEQ_TICK_TIMER_H_
  11. #define API_NETEQ_TICK_TIMER_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. // Implements a time counter. The counter is advanced with the Increment()
  17. // methods, and is queried with the ticks() accessor. It is assumed that one
  18. // "tick" of the counter corresponds to 10 ms.
  19. // A TickTimer object can provide two types of associated time-measuring
  20. // objects: Stopwatch and Countdown.
  21. class TickTimer {
  22. public:
  23. // Stopwatch measures time elapsed since it was started, by querying the
  24. // associated TickTimer for the current time. The intended use is to request a
  25. // new Stopwatch object from a TickTimer object with the GetNewStopwatch()
  26. // method. Note: since the Stopwatch object contains a reference to the
  27. // TickTimer it is associated with, it cannot outlive the TickTimer.
  28. class Stopwatch {
  29. public:
  30. explicit Stopwatch(const TickTimer& ticktimer);
  31. uint64_t ElapsedTicks() const { return ticktimer_.ticks() - starttick_; }
  32. uint64_t ElapsedMs() const {
  33. const uint64_t elapsed_ticks = ticktimer_.ticks() - starttick_;
  34. const int ms_per_tick = ticktimer_.ms_per_tick();
  35. return elapsed_ticks < UINT64_MAX / ms_per_tick
  36. ? elapsed_ticks * ms_per_tick
  37. : UINT64_MAX;
  38. }
  39. private:
  40. const TickTimer& ticktimer_;
  41. const uint64_t starttick_;
  42. };
  43. // Countdown counts down from a given start value with each tick of the
  44. // associated TickTimer, until zero is reached. The Finished() method will
  45. // return true if zero has been reached, false otherwise. The intended use is
  46. // to request a new Countdown object from a TickTimer object with the
  47. // GetNewCountdown() method. Note: since the Countdown object contains a
  48. // reference to the TickTimer it is associated with, it cannot outlive the
  49. // TickTimer.
  50. class Countdown {
  51. public:
  52. Countdown(const TickTimer& ticktimer, uint64_t ticks_to_count);
  53. ~Countdown();
  54. bool Finished() const {
  55. return stopwatch_->ElapsedTicks() >= ticks_to_count_;
  56. }
  57. private:
  58. const std::unique_ptr<Stopwatch> stopwatch_;
  59. const uint64_t ticks_to_count_;
  60. };
  61. TickTimer() : TickTimer(10) {}
  62. explicit TickTimer(int ms_per_tick) : ms_per_tick_(ms_per_tick) {
  63. RTC_DCHECK_GT(ms_per_tick_, 0);
  64. }
  65. TickTimer(const TickTimer&) = delete;
  66. TickTimer& operator=(const TickTimer&) = delete;
  67. void Increment() { ++ticks_; }
  68. // Mainly intended for testing.
  69. void Increment(uint64_t x) { ticks_ += x; }
  70. uint64_t ticks() const { return ticks_; }
  71. int ms_per_tick() const { return ms_per_tick_; }
  72. // Returns a new Stopwatch object, based on the current TickTimer. Note that
  73. // the new Stopwatch object contains a reference to the current TickTimer,
  74. // and must therefore not outlive the TickTimer.
  75. std::unique_ptr<Stopwatch> GetNewStopwatch() const {
  76. return std::unique_ptr<Stopwatch>(new Stopwatch(*this));
  77. }
  78. // Returns a new Countdown object, based on the current TickTimer. Note that
  79. // the new Countdown object contains a reference to the current TickTimer,
  80. // and must therefore not outlive the TickTimer.
  81. std::unique_ptr<Countdown> GetNewCountdown(uint64_t ticks_to_count) const {
  82. return std::unique_ptr<Countdown>(new Countdown(*this, ticks_to_count));
  83. }
  84. private:
  85. uint64_t ticks_ = 0;
  86. const int ms_per_tick_;
  87. };
  88. } // namespace webrtc
  89. #endif // API_NETEQ_TICK_TIMER_H_