tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/pacing/interval_budget.h" |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | namespace webrtc { |
| 16 | namespace { |
| 17 | constexpr int kWindowMs = 500; |
tschumim | 5afa3f2 | 2017-08-18 03:38:49 -0700 | [diff] [blame] | 18 | constexpr int kDeltaTimeMs = 2000; |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | IntervalBudget::IntervalBudget(int initial_target_rate_kbps) |
| 22 | : IntervalBudget(initial_target_rate_kbps, false) {} |
| 23 | |
| 24 | IntervalBudget::IntervalBudget(int initial_target_rate_kbps, |
| 25 | bool can_build_up_underuse) |
| 26 | : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) { |
| 27 | set_target_rate_kbps(initial_target_rate_kbps); |
| 28 | } |
| 29 | |
| 30 | void IntervalBudget::set_target_rate_kbps(int target_rate_kbps) { |
| 31 | target_rate_kbps_ = target_rate_kbps; |
| 32 | max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) / 8; |
| 33 | bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_), |
| 34 | max_bytes_in_budget_); |
| 35 | } |
| 36 | |
| 37 | void IntervalBudget::IncreaseBudget(int64_t delta_time_ms) { |
tschumim | 5afa3f2 | 2017-08-18 03:38:49 -0700 | [diff] [blame] | 38 | RTC_DCHECK_LT(delta_time_ms, kDeltaTimeMs); |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 39 | int bytes = target_rate_kbps_ * delta_time_ms / 8; |
| 40 | if (bytes_remaining_ < 0 || can_build_up_underuse_) { |
| 41 | // We overused last interval, compensate this interval. |
| 42 | bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_); |
| 43 | } else { |
| 44 | // If we underused last interval we can't use it this interval. |
| 45 | bytes_remaining_ = std::min(bytes, max_bytes_in_budget_); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void IntervalBudget::UseBudget(size_t bytes) { |
| 50 | bytes_remaining_ = std::max(bytes_remaining_ - static_cast<int>(bytes), |
| 51 | -max_bytes_in_budget_); |
| 52 | } |
| 53 | |
| 54 | size_t IntervalBudget::bytes_remaining() const { |
| 55 | return static_cast<size_t>(std::max(0, bytes_remaining_)); |
| 56 | } |
| 57 | |
| 58 | int IntervalBudget::budget_level_percent() const { |
| 59 | return bytes_remaining_ * 100 / max_bytes_in_budget_; |
| 60 | } |
| 61 | |
| 62 | int IntervalBudget::target_rate_kbps() const { |
| 63 | return target_rate_kbps_; |
| 64 | } |
| 65 | |
| 66 | } // namespace webrtc |