-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency_tracker.h
More file actions
65 lines (51 loc) · 1.81 KB
/
Copy pathfrequency_tracker.h
File metadata and controls
65 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* frequency_tracker.h - Frequency tracker
*
* Copyright (c) 2024 The WebRTC project authors. All Rights Reserved.
* Adapted for aspect video engine.
*/
#ifndef BASE_FREQUENCY_TRACKER_H_
#define BASE_FREQUENCY_TRACKER_H_
#include <cstddef>
#include <cstdint>
#include <optional>
#include "base/rate_statistics.h"
#include "base/units/frequency.h"
#include "base/units/timestamp.h"
namespace ave {
namespace base {
// Note: TimeDelta, Timestamp, and Frequency are already in the ave namespace,
// so no 'using' declarations are needed.
// Class to estimate frequency (e.g. frame rate) over running window.
// Timestamps used in Update() and Rate() must never decrease for two
// consecutive calls.
// This class is thread unsafe.
class FrequencyTracker {
public:
explicit FrequencyTracker(TimeDelta window_size)
: impl_(window_size.ms(), /*scale=*/1000) {}
FrequencyTracker(const FrequencyTracker&) = default;
FrequencyTracker(FrequencyTracker&&) = default;
FrequencyTracker& operator=(const FrequencyTracker&) = delete;
FrequencyTracker& operator=(FrequencyTracker&&) = delete;
~FrequencyTracker() = default;
// Reset instance to original state.
void Reset() { impl_.Reset(); }
// Update rate with a new data point, moving averaging window as needed.
void Update(int64_t count, Timestamp now) { impl_.Update(count, now.ms()); }
void Update(Timestamp now) { Update(1, now); }
// Returns rate, moving averaging window as needed.
// Returns nullopt when rate can't be measured.
::std::optional<Frequency> Rate(Timestamp now) const {
auto rate = impl_.Rate(now.ms());
if (rate.has_value()) {
return Frequency::Hertz(*rate);
}
return ::std::nullopt;
}
private:
mutable RateStatistics impl_;
};
} // namespace base
} // namespace ave
#endif // BASE_FREQUENCY_TRACKER_H_