blob: 11c8cee7af23621a9d81ed20d1fa66b6666c2458 [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
Harald Alvestranda846cef2020-01-15 14:02:12 +010024// Class to estimate rates based on counts in a sequence of 1-millisecond
25// intervals.
26
27// This class uses int64 for all its numbers because some rates can be very
28// high; for instance, a 20 Mbit/sec video stream can wrap a 32-bit byte
29// counter in 14 minutes.
30
Mirko Bonadeic66e0042019-10-18 09:52:22 +020031class RTC_EXPORT RateStatistics {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 public:
33 static constexpr float kBpsScale = 8000.0f;
34
35 // max_window_size_ms = Maximum window size in ms for the rate estimation.
36 // Initial window size is set to this, but may be changed
37 // to something lower by calling SetWindowSize().
38 // scale = coefficient to convert counts/ms to desired unit
39 // ex: kBpsScale (8000) for bits/s if count represents bytes.
40 RateStatistics(int64_t max_window_size_ms, float scale);
Sergey Silkin40b70502018-08-27 10:55:07 +020041
42 RateStatistics(const RateStatistics& other);
43
44 RateStatistics(RateStatistics&& other);
45
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046 ~RateStatistics();
47
48 // Reset instance to original state.
49 void Reset();
50
51 // Update rate with a new data point, moving averaging window as needed.
Harald Alvestranda846cef2020-01-15 14:02:12 +010052 void Update(int64_t count, int64_t now_ms);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053
54 // Note that despite this being a const method, it still updates the internal
55 // state (moves averaging window), but it doesn't make any alterations that
56 // are observable from the other methods, as long as supplied timestamps are
57 // from a monotonic clock. Ie, it doesn't matter if this call moves the
58 // window, since any subsequent call to Update or Rate would still have moved
59 // the window as much or more.
Harald Alvestranda846cef2020-01-15 14:02:12 +010060 absl::optional<int64_t> Rate(int64_t now_ms) const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061
62 // Update the size of the averaging window. The maximum allowed value for
63 // window_size_ms is max_window_size_ms as supplied in the constructor.
64 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);
65
66 private:
67 void EraseOld(int64_t now_ms);
68 bool IsInitialized() const;
69
70 // Counters are kept in buckets (circular buffer), with one bucket
71 // per millisecond.
72 struct Bucket {
Harald Alvestranda846cef2020-01-15 14:02:12 +010073 int64_t sum; // Sum of all samples in this bucket.
74 int samples; // Number of samples in this bucket.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075 };
76 std::unique_ptr<Bucket[]> buckets_;
77
78 // Total count recorded in buckets.
Harald Alvestranda846cef2020-01-15 14:02:12 +010079 int64_t accumulated_count_;
80
81 // True if accumulated_count_ has ever grown too large to be
82 // contained in its integer type.
83 bool overflow_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
85 // The total number of samples in the buckets.
Harald Alvestranda846cef2020-01-15 14:02:12 +010086 int num_samples_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
88 // Oldest time recorded in buckets.
89 int64_t oldest_time_;
90
91 // Bucket index of oldest counter recorded in buckets.
Harald Alvestranda846cef2020-01-15 14:02:12 +010092 int64_t oldest_index_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093
94 // To convert counts/ms to desired units
95 const float scale_;
96
97 // The window sizes, in ms, over which the rate is calculated.
98 const int64_t max_window_size_ms_;
99 int64_t current_window_size_ms_;
100};
101} // namespace webrtc
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000102
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200103#endif // RTC_BASE_RATE_STATISTICS_H_