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