blob: 520099e346624b5575293585dc9d7ca7d3cc4d60 [file] [log] [blame]
henrik.lundin8053f792016-04-22 13:21:43 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_
12#define MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_
henrik.lundin8053f792016-04-22 13:21:43 -070013
14#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/constructormagic.h"
henrik.lundin8053f792016-04-22 13:21:43 -070018
19namespace 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.
26class 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 const int ms_per_tick = ticktimer_.ms_per_tick();
42 return elapsed_ticks < UINT64_MAX / ms_per_tick
43 ? elapsed_ticks * ms_per_tick
44 : UINT64_MAX;
45 }
46
47 private:
48 const TickTimer& ticktimer_;
49 const uint64_t starttick_;
50 };
51
52 // Countdown counts down from a given start value with each tick of the
53 // associated TickTimer, until zero is reached. The Finished() method will
54 // return true if zero has been reached, false otherwise. The intended use is
55 // to request a new Countdown object from a TickTimer object with the
56 // GetNewCountdown() method. Note: since the Countdown object contains a
57 // reference to the TickTimer it is associated with, it cannot outlive the
58 // TickTimer.
59 class Countdown {
60 public:
61 Countdown(const TickTimer& ticktimer, uint64_t ticks_to_count);
62
63 ~Countdown();
64
65 bool Finished() const {
66 return stopwatch_->ElapsedTicks() >= ticks_to_count_;
67 }
68
69 private:
70 const std::unique_ptr<Stopwatch> stopwatch_;
71 const uint64_t ticks_to_count_;
72 };
73
74 TickTimer() : TickTimer(10) {}
75 explicit TickTimer(int ms_per_tick) : ms_per_tick_(ms_per_tick) {
76 RTC_DCHECK_GT(ms_per_tick_, 0);
77 }
78
79 void Increment() { ++ticks_; }
80
81 // Mainly intended for testing.
82 void Increment(uint64_t x) { ticks_ += x; }
83
84 uint64_t ticks() const { return ticks_; }
85
86 int ms_per_tick() const { return ms_per_tick_; }
87
88 // Returns a new Stopwatch object, based on the current TickTimer. Note that
89 // the new Stopwatch object contains a reference to the current TickTimer,
90 // and must therefore not outlive the TickTimer.
91 std::unique_ptr<Stopwatch> GetNewStopwatch() const {
92 return std::unique_ptr<Stopwatch>(new Stopwatch(*this));
93 }
94
95 // Returns a new Countdown object, based on the current TickTimer. Note that
96 // the new Countdown object contains a reference to the current TickTimer,
97 // and must therefore not outlive the TickTimer.
98 std::unique_ptr<Countdown> GetNewCountdown(uint64_t ticks_to_count) const {
99 return std::unique_ptr<Countdown>(new Countdown(*this, ticks_to_count));
100 }
101
102 private:
103 uint64_t ticks_ = 0;
104 const int ms_per_tick_;
105 RTC_DISALLOW_COPY_AND_ASSIGN(TickTimer);
106};
107
108} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200109#endif // MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_