| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 "webrtc/modules/pacing/interval_budget.h" |
| 12 |
| 13 #include <algorithm> |
| 14 |
| 15 namespace webrtc { |
| 16 namespace { |
| 17 constexpr int kWindowMs = 500; |
| 18 } |
| 19 |
| 20 IntervalBudget::IntervalBudget(int initial_target_rate_kbps) |
| 21 : IntervalBudget(initial_target_rate_kbps, false) {} |
| 22 |
| 23 IntervalBudget::IntervalBudget(int initial_target_rate_kbps, |
| 24 bool can_build_up_underuse) |
| 25 : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) { |
| 26 set_target_rate_kbps(initial_target_rate_kbps); |
| 27 } |
| 28 |
| 29 void IntervalBudget::set_target_rate_kbps(int target_rate_kbps) { |
| 30 target_rate_kbps_ = target_rate_kbps; |
| 31 max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) / 8; |
| 32 bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_), |
| 33 max_bytes_in_budget_); |
| 34 } |
| 35 |
| 36 void IntervalBudget::IncreaseBudget(int64_t delta_time_ms) { |
| 37 int bytes = target_rate_kbps_ * delta_time_ms / 8; |
| 38 if (bytes_remaining_ < 0 || can_build_up_underuse_) { |
| 39 // We overused last interval, compensate this interval. |
| 40 bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_); |
| 41 } else { |
| 42 // If we underused last interval we can't use it this interval. |
| 43 bytes_remaining_ = std::min(bytes, max_bytes_in_budget_); |
| 44 } |
| 45 } |
| 46 |
| 47 void IntervalBudget::UseBudget(size_t bytes) { |
| 48 bytes_remaining_ = std::max(bytes_remaining_ - static_cast<int>(bytes), |
| 49 -max_bytes_in_budget_); |
| 50 } |
| 51 |
| 52 size_t IntervalBudget::bytes_remaining() const { |
| 53 return static_cast<size_t>(std::max(0, bytes_remaining_)); |
| 54 } |
| 55 |
| 56 int IntervalBudget::budget_level_percent() const { |
| 57 return bytes_remaining_ * 100 / max_bytes_in_budget_; |
| 58 } |
| 59 |
| 60 int IntervalBudget::target_rate_kbps() const { |
| 61 return target_rate_kbps_; |
| 62 } |
| 63 |
| 64 } // namespace webrtc |
| OLD | NEW |