Danil Chapovalov | e546ff9 | 2023-07-21 12:06:20 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 11 | #include "rtc_base/bitrate_tracker.h" |
| 12 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 13 | #include <optional> |
| 14 | |
Danil Chapovalov | e546ff9 | 2023-07-21 12:06:20 | [diff] [blame] | 15 | #include "api/units/data_rate.h" |
| 16 | #include "api/units/timestamp.h" |
| 17 | #include "rtc_base/rate_statistics.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | BitrateTracker::BitrateTracker(TimeDelta max_window_size) |
| 22 | : impl_(max_window_size.ms(), RateStatistics::kBpsScale) {} |
| 23 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 24 | std::optional<DataRate> BitrateTracker::Rate(Timestamp now) const { |
| 25 | if (std::optional<int64_t> rate = impl_.Rate(now.ms())) { |
Danil Chapovalov | e546ff9 | 2023-07-21 12:06:20 | [diff] [blame] | 26 | return DataRate::BitsPerSec(*rate); |
| 27 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 28 | return std::nullopt; |
Danil Chapovalov | e546ff9 | 2023-07-21 12:06:20 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool BitrateTracker::SetWindowSize(TimeDelta window_size, Timestamp now) { |
| 32 | return impl_.SetWindowSize(window_size.ms(), now.ms()); |
| 33 | } |
| 34 | |
| 35 | void BitrateTracker::Update(int64_t bytes, Timestamp now) { |
| 36 | impl_.Update(bytes, now.ms()); |
| 37 | } |
| 38 | |
| 39 | } // namespace webrtc |