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