xref: /aosp_15_r20/external/webrtc/rtc_base/rate_statistics.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_RATE_STATISTICS_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_RATE_STATISTICS_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include <deque>
18*d9f75844SAndroid Build Coastguard Worker #include <memory>
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
21*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker // Class to estimate rates based on counts in a sequence of 1-millisecond
26*d9f75844SAndroid Build Coastguard Worker // intervals.
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker // This class uses int64 for all its numbers because some rates can be very
29*d9f75844SAndroid Build Coastguard Worker // high; for instance, a 20 Mbit/sec video stream can wrap a 32-bit byte
30*d9f75844SAndroid Build Coastguard Worker // counter in 14 minutes.
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker // Note that timestamps used in Update(), Rate() and SetWindowSize() must never
33*d9f75844SAndroid Build Coastguard Worker // decrease for two consecutive calls.
34*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/11600): Migrate from int64_t to Timestamp.
35*d9f75844SAndroid Build Coastguard Worker 
36*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT RateStatistics {
37*d9f75844SAndroid Build Coastguard Worker  public:
38*d9f75844SAndroid Build Coastguard Worker   static constexpr float kBpsScale = 8000.0f;
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker   // max_window_size_ms = Maximum window size in ms for the rate estimation.
41*d9f75844SAndroid Build Coastguard Worker   //                      Initial window size is set to this, but may be changed
42*d9f75844SAndroid Build Coastguard Worker   //                      to something lower by calling SetWindowSize().
43*d9f75844SAndroid Build Coastguard Worker   // scale = coefficient to convert counts/ms to desired unit
44*d9f75844SAndroid Build Coastguard Worker   //         ex: kBpsScale (8000) for bits/s if count represents bytes.
45*d9f75844SAndroid Build Coastguard Worker   RateStatistics(int64_t max_window_size_ms, float scale);
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker   RateStatistics(const RateStatistics& other);
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker   RateStatistics(RateStatistics&& other);
50*d9f75844SAndroid Build Coastguard Worker 
51*d9f75844SAndroid Build Coastguard Worker   ~RateStatistics();
52*d9f75844SAndroid Build Coastguard Worker 
53*d9f75844SAndroid Build Coastguard Worker   // Reset instance to original state.
54*d9f75844SAndroid Build Coastguard Worker   void Reset();
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   // Update rate with a new data point, moving averaging window as needed.
57*d9f75844SAndroid Build Coastguard Worker   void Update(int64_t count, int64_t now_ms);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Note that despite this being a const method, it still updates the internal
60*d9f75844SAndroid Build Coastguard Worker   // state (moves averaging window), but it doesn't make any alterations that
61*d9f75844SAndroid Build Coastguard Worker   // are observable from the other methods, as long as supplied timestamps are
62*d9f75844SAndroid Build Coastguard Worker   // from a monotonic clock. Ie, it doesn't matter if this call moves the
63*d9f75844SAndroid Build Coastguard Worker   // window, since any subsequent call to Update or Rate would still have moved
64*d9f75844SAndroid Build Coastguard Worker   // the window as much or more.
65*d9f75844SAndroid Build Coastguard Worker   absl::optional<int64_t> Rate(int64_t now_ms) const;
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker   // Update the size of the averaging window. The maximum allowed value for
68*d9f75844SAndroid Build Coastguard Worker   // window_size_ms is max_window_size_ms as supplied in the constructor.
69*d9f75844SAndroid Build Coastguard Worker   bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker  private:
72*d9f75844SAndroid Build Coastguard Worker   void EraseOld(int64_t now_ms);
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker   struct Bucket {
75*d9f75844SAndroid Build Coastguard Worker     explicit Bucket(int64_t timestamp);
76*d9f75844SAndroid Build Coastguard Worker     int64_t sum;  // Sum of all samples in this bucket.
77*d9f75844SAndroid Build Coastguard Worker     int num_samples;          // Number of samples in this bucket.
78*d9f75844SAndroid Build Coastguard Worker     const int64_t timestamp;  // Timestamp this bucket corresponds to.
79*d9f75844SAndroid Build Coastguard Worker   };
80*d9f75844SAndroid Build Coastguard Worker   // All buckets within the time window, ordered by time.
81*d9f75844SAndroid Build Coastguard Worker   std::deque<Bucket> buckets_;
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   // Total count recorded in all buckets.
84*d9f75844SAndroid Build Coastguard Worker   int64_t accumulated_count_;
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker   // Timestamp of the first data point seen, or -1 of none seen.
87*d9f75844SAndroid Build Coastguard Worker   int64_t first_timestamp_;
88*d9f75844SAndroid Build Coastguard Worker 
89*d9f75844SAndroid Build Coastguard Worker   // True if accumulated_count_ has ever grown too large to be
90*d9f75844SAndroid Build Coastguard Worker   // contained in its integer type.
91*d9f75844SAndroid Build Coastguard Worker   bool overflow_ = false;
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker   // The total number of samples in the buckets.
94*d9f75844SAndroid Build Coastguard Worker   int num_samples_;
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker   // To convert counts/ms to desired units
97*d9f75844SAndroid Build Coastguard Worker   const float scale_;
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker   // The window sizes, in ms, over which the rate is calculated.
100*d9f75844SAndroid Build Coastguard Worker   const int64_t max_window_size_ms_;
101*d9f75844SAndroid Build Coastguard Worker   int64_t current_window_size_ms_;
102*d9f75844SAndroid Build Coastguard Worker };
103*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_RATE_STATISTICS_H_
106