| 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 |
| 11 | #ifndef RTC_BASE_BITRATE_TRACKER_H_ |
| 12 | #define RTC_BASE_BITRATE_TRACKER_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include "absl/types/optional.h" |
| 18 | #include "api/units/data_rate.h" |
| 19 | #include "api/units/data_size.h" |
| 20 | #include "api/units/time_delta.h" |
| 21 | #include "api/units/timestamp.h" |
| 22 | #include "rtc_base/rate_statistics.h" |
| 23 | #include "rtc_base/system/rtc_export.h" |
| 24 |
| 25 | namespace webrtc { |
| 26 | // Class to estimate bitrates over running window. |
| 27 | // Timestamps used in Update(), Rate() and SetWindowSize() must never |
| 28 | // decrease for two consecutive calls. |
| 29 | // This class is thread unsafe. |
| 30 | class RTC_EXPORT BitrateTracker { |
| 31 | public: |
| 32 | // max_window_sizes = Maximum window size for the rate estimation. |
| 33 | // Initial window size is set to this, but may be changed |
| 34 | // to something lower by calling SetWindowSize(). |
| 35 | explicit BitrateTracker(TimeDelta max_window_size); |
| 36 |
| 37 | BitrateTracker(const BitrateTracker&) = default; |
| 38 | BitrateTracker(BitrateTracker&&) = default; |
| 39 | BitrateTracker& operator=(const BitrateTracker&) = delete; |
| 40 | BitrateTracker& operator=(BitrateTracker&&) = delete; |
| 41 |
| 42 | ~BitrateTracker() = default; |
| 43 |
| 44 | // Resets instance to original state. |
| 45 | void Reset() { impl_.Reset(); } |
| 46 |
| 47 | // Updates bitrate with a new data point, moving averaging window as needed. |
| 48 | void Update(int64_t bytes, Timestamp now); |
| 49 | void Update(DataSize size, Timestamp now) { Update(size.bytes(), now); } |
| 50 |
| 51 | // Returns bitrate, moving averaging window as needed. |
| 52 | // Returns nullopt when bitrate can't be measured. |
| 53 | absl::optional<DataRate> Rate(Timestamp now) const; |
| 54 |
| 55 | // Update the size of the averaging window. The maximum allowed value for |
| 56 | // `window_size` is `max_window_size` as supplied in the constructor. |
| 57 | bool SetWindowSize(TimeDelta window_size, Timestamp now); |
| 58 |
| 59 | private: |
| 60 | RateStatistics impl_; |
| 61 | }; |
| 62 | } // namespace webrtc |
| 63 |
| 64 | #endif // RTC_BASE_BITRATE_TRACKER_H_ |
| 65 |