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