| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/pacing/interval_budget.h" | 11 #include "webrtc/modules/pacing/interval_budget.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 namespace webrtc { | 15 namespace webrtc { |
| 16 namespace { | 16 namespace { |
| 17 constexpr int kWindowMs = 500; | 17 constexpr int kWindowMs = 500; |
| 18 constexpr int kDeltaTimeMs = 2000; |
| 18 } | 19 } |
| 19 | 20 |
| 20 IntervalBudget::IntervalBudget(int initial_target_rate_kbps) | 21 IntervalBudget::IntervalBudget(int initial_target_rate_kbps) |
| 21 : IntervalBudget(initial_target_rate_kbps, false) {} | 22 : IntervalBudget(initial_target_rate_kbps, false) {} |
| 22 | 23 |
| 23 IntervalBudget::IntervalBudget(int initial_target_rate_kbps, | 24 IntervalBudget::IntervalBudget(int initial_target_rate_kbps, |
| 24 bool can_build_up_underuse) | 25 bool can_build_up_underuse) |
| 25 : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) { | 26 : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) { |
| 26 set_target_rate_kbps(initial_target_rate_kbps); | 27 set_target_rate_kbps(initial_target_rate_kbps); |
| 27 } | 28 } |
| 28 | 29 |
| 29 void IntervalBudget::set_target_rate_kbps(int target_rate_kbps) { | 30 void IntervalBudget::set_target_rate_kbps(int target_rate_kbps) { |
| 30 target_rate_kbps_ = target_rate_kbps; | 31 target_rate_kbps_ = target_rate_kbps; |
| 31 max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) / 8; | 32 max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) / 8; |
| 32 bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_), | 33 bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_), |
| 33 max_bytes_in_budget_); | 34 max_bytes_in_budget_); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void IntervalBudget::IncreaseBudget(int64_t delta_time_ms) { | 37 void IntervalBudget::IncreaseBudget(int64_t delta_time_ms) { |
| 38 RTC_DCHECK_LT(delta_time_ms, kDeltaTimeMs); |
| 37 int bytes = target_rate_kbps_ * delta_time_ms / 8; | 39 int bytes = target_rate_kbps_ * delta_time_ms / 8; |
| 38 if (bytes_remaining_ < 0 || can_build_up_underuse_) { | 40 if (bytes_remaining_ < 0 || can_build_up_underuse_) { |
| 39 // We overused last interval, compensate this interval. | 41 // We overused last interval, compensate this interval. |
| 40 bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_); | 42 bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_); |
| 41 } else { | 43 } else { |
| 42 // If we underused last interval we can't use it this interval. | 44 // If we underused last interval we can't use it this interval. |
| 43 bytes_remaining_ = std::min(bytes, max_bytes_in_budget_); | 45 bytes_remaining_ = std::min(bytes, max_bytes_in_budget_); |
| 44 } | 46 } |
| 45 } | 47 } |
| 46 | 48 |
| 47 void IntervalBudget::UseBudget(size_t bytes) { | 49 void IntervalBudget::UseBudget(size_t bytes) { |
| 48 bytes_remaining_ = std::max(bytes_remaining_ - static_cast<int>(bytes), | 50 bytes_remaining_ = std::max(bytes_remaining_ - static_cast<int>(bytes), |
| 49 -max_bytes_in_budget_); | 51 -max_bytes_in_budget_); |
| 50 } | 52 } |
| 51 | 53 |
| 52 size_t IntervalBudget::bytes_remaining() const { | 54 size_t IntervalBudget::bytes_remaining() const { |
| 53 return static_cast<size_t>(std::max(0, bytes_remaining_)); | 55 return static_cast<size_t>(std::max(0, bytes_remaining_)); |
| 54 } | 56 } |
| 55 | 57 |
| 56 int IntervalBudget::budget_level_percent() const { | 58 int IntervalBudget::budget_level_percent() const { |
| 57 return bytes_remaining_ * 100 / max_bytes_in_budget_; | 59 return bytes_remaining_ * 100 / max_bytes_in_budget_; |
| 58 } | 60 } |
| 59 | 61 |
| 60 int IntervalBudget::target_rate_kbps() const { | 62 int IntervalBudget::target_rate_kbps() const { |
| 61 return target_rate_kbps_; | 63 return target_rate_kbps_; |
| 62 } | 64 } |
| 63 | 65 |
| 64 } // namespace webrtc | 66 } // namespace webrtc |
| OLD | NEW |