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