blob: 65b5fa10d942dcf33a2faac69628d46ae9553ed3 [file] [log] [blame]
sprang@webrtc.org37968a92013-12-03 10:31:59 +00001/*
2 * Copyright (c) 2013 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 RTC_BASE_RATE_STATISTICS_H_
12#define RTC_BASE_RATE_STATISTICS_H_
sprang@webrtc.org37968a92013-12-03 10:31:59 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <memory>
jbauch555604a2016-04-26 03:13:22 -070018
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020019#include "absl/types/optional.h"
Mirko Bonadeic66e0042019-10-18 09:52:22 +020020#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021
22namespace webrtc {
23
Mirko Bonadeic66e0042019-10-18 09:52:22 +020024class RTC_EXPORT RateStatistics {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025 public:
26 static constexpr float kBpsScale = 8000.0f;
27
28 // max_window_size_ms = Maximum window size in ms for the rate estimation.
29 // Initial window size is set to this, but may be changed
30 // to something lower by calling SetWindowSize().
31 // scale = coefficient to convert counts/ms to desired unit
32 // ex: kBpsScale (8000) for bits/s if count represents bytes.
33 RateStatistics(int64_t max_window_size_ms, float scale);
Sergey Silkin40b70502018-08-27 10:55:07 +020034
35 RateStatistics(const RateStatistics& other);
36
37 RateStatistics(RateStatistics&& other);
38
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 ~RateStatistics();
40
41 // Reset instance to original state.
42 void Reset();
43
44 // Update rate with a new data point, moving averaging window as needed.
45 void Update(size_t count, int64_t now_ms);
46
47 // Note that despite this being a const method, it still updates the internal
48 // state (moves averaging window), but it doesn't make any alterations that
49 // are observable from the other methods, as long as supplied timestamps are
50 // from a monotonic clock. Ie, it doesn't matter if this call moves the
51 // window, since any subsequent call to Update or Rate would still have moved
52 // the window as much or more.
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020053 absl::optional<uint32_t> Rate(int64_t now_ms) const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
55 // Update the size of the averaging window. The maximum allowed value for
56 // window_size_ms is max_window_size_ms as supplied in the constructor.
57 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);
58
59 private:
60 void EraseOld(int64_t now_ms);
61 bool IsInitialized() const;
62
63 // Counters are kept in buckets (circular buffer), with one bucket
64 // per millisecond.
65 struct Bucket {
66 size_t sum; // Sum of all samples in this bucket.
67 size_t samples; // Number of samples in this bucket.
68 };
69 std::unique_ptr<Bucket[]> buckets_;
70
71 // Total count recorded in buckets.
72 size_t accumulated_count_;
73
74 // The total number of samples in the buckets.
75 size_t num_samples_;
76
77 // Oldest time recorded in buckets.
78 int64_t oldest_time_;
79
80 // Bucket index of oldest counter recorded in buckets.
81 uint32_t oldest_index_;
82
83 // To convert counts/ms to desired units
84 const float scale_;
85
86 // The window sizes, in ms, over which the rate is calculated.
87 const int64_t max_window_size_ms_;
88 int64_t current_window_size_ms_;
89};
90} // namespace webrtc
sprang@webrtc.org37968a92013-12-03 10:31:59 +000091
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020092#endif // RTC_BASE_RATE_STATISTICS_H_