tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 "common_video/include/bitrate_adjuster.h" |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 12 | |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 13 | #include <algorithm> |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 14 | #include <cmath> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 18 | #include "rtc_base/timeutils.h" |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Update bitrate at most once every second. |
| 23 | const uint32_t BitrateAdjuster::kBitrateUpdateIntervalMs = 1000; |
| 24 | |
| 25 | // Update bitrate at most once every 30 frames. |
| 26 | const uint32_t BitrateAdjuster::kBitrateUpdateFrameInterval = 30; |
| 27 | |
| 28 | // 10 percent of original. |
| 29 | const float BitrateAdjuster::kBitrateTolerancePct = .1f; |
| 30 | |
| 31 | const float BitrateAdjuster::kBytesPerMsToBitsPerSecond = 8 * 1000; |
| 32 | |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 33 | BitrateAdjuster::BitrateAdjuster(float min_adjusted_bitrate_pct, |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 34 | float max_adjusted_bitrate_pct) |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 35 | : min_adjusted_bitrate_pct_(min_adjusted_bitrate_pct), |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 36 | max_adjusted_bitrate_pct_(max_adjusted_bitrate_pct), |
| 37 | bitrate_tracker_(1.5 * kBitrateUpdateIntervalMs, |
| 38 | kBytesPerMsToBitsPerSecond) { |
| 39 | Reset(); |
| 40 | } |
| 41 | |
| 42 | void BitrateAdjuster::SetTargetBitrateBps(uint32_t bitrate_bps) { |
| 43 | rtc::CritScope cs(&crit_); |
| 44 | // If the change in target bitrate is large, update the adjusted bitrate |
| 45 | // immediately since it's likely we have gained or lost a sizeable amount of |
| 46 | // bandwidth and we'll want to respond quickly. |
| 47 | // If the change in target bitrate fits within the existing tolerance of |
| 48 | // encoder output, wait for the next adjustment time to preserve |
| 49 | // existing penalties and not forcibly reset the adjusted bitrate to target. |
| 50 | // However, if we received many small deltas within an update time |
| 51 | // window and one of them exceeds the tolerance when compared to the last |
| 52 | // target we updated against, treat it as a large change in target bitrate. |
| 53 | if (!IsWithinTolerance(bitrate_bps, target_bitrate_bps_) || |
| 54 | !IsWithinTolerance(bitrate_bps, last_adjusted_target_bitrate_bps_)) { |
| 55 | adjusted_bitrate_bps_ = bitrate_bps; |
| 56 | last_adjusted_target_bitrate_bps_ = bitrate_bps; |
| 57 | } |
| 58 | target_bitrate_bps_ = bitrate_bps; |
| 59 | } |
| 60 | |
| 61 | uint32_t BitrateAdjuster::GetTargetBitrateBps() const { |
| 62 | rtc::CritScope cs(&crit_); |
| 63 | return target_bitrate_bps_; |
| 64 | } |
| 65 | |
| 66 | uint32_t BitrateAdjuster::GetAdjustedBitrateBps() const { |
| 67 | rtc::CritScope cs(&crit_); |
| 68 | return adjusted_bitrate_bps_; |
| 69 | } |
| 70 | |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 71 | rtc::Optional<uint32_t> BitrateAdjuster::GetEstimatedBitrateBps() { |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 72 | rtc::CritScope cs(&crit_); |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 73 | return bitrate_tracker_.Rate(rtc::TimeMillis()); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void BitrateAdjuster::Update(size_t frame_size) { |
| 77 | rtc::CritScope cs(&crit_); |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 78 | uint32_t current_time_ms = rtc::TimeMillis(); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 79 | bitrate_tracker_.Update(frame_size, current_time_ms); |
| 80 | UpdateBitrate(current_time_ms); |
| 81 | } |
| 82 | |
| 83 | bool BitrateAdjuster::IsWithinTolerance(uint32_t bitrate_bps, |
| 84 | uint32_t target_bitrate_bps) { |
| 85 | if (target_bitrate_bps == 0) { |
| 86 | return false; |
| 87 | } |
| 88 | float delta = std::abs(static_cast<float>(bitrate_bps) - |
| 89 | static_cast<float>(target_bitrate_bps)); |
| 90 | float delta_pct = delta / target_bitrate_bps; |
| 91 | return delta_pct < kBitrateTolerancePct; |
| 92 | } |
| 93 | |
| 94 | uint32_t BitrateAdjuster::GetMinAdjustedBitrateBps() const { |
| 95 | return min_adjusted_bitrate_pct_ * target_bitrate_bps_; |
| 96 | } |
| 97 | |
| 98 | uint32_t BitrateAdjuster::GetMaxAdjustedBitrateBps() const { |
| 99 | return max_adjusted_bitrate_pct_ * target_bitrate_bps_; |
| 100 | } |
| 101 | |
| 102 | // Only safe to call this after Update calls have stopped |
| 103 | void BitrateAdjuster::Reset() { |
| 104 | rtc::CritScope cs(&crit_); |
| 105 | target_bitrate_bps_ = 0; |
| 106 | adjusted_bitrate_bps_ = 0; |
| 107 | last_adjusted_target_bitrate_bps_ = 0; |
| 108 | last_bitrate_update_time_ms_ = 0; |
| 109 | frames_since_last_update_ = 0; |
| 110 | bitrate_tracker_.Reset(); |
| 111 | } |
| 112 | |
| 113 | void BitrateAdjuster::UpdateBitrate(uint32_t current_time_ms) { |
| 114 | uint32_t time_since_last_update_ms = |
| 115 | current_time_ms - last_bitrate_update_time_ms_; |
| 116 | // Don't attempt to update bitrate unless enough time and frames have passed. |
| 117 | ++frames_since_last_update_; |
| 118 | if (time_since_last_update_ms < kBitrateUpdateIntervalMs || |
| 119 | frames_since_last_update_ < kBitrateUpdateFrameInterval) { |
| 120 | return; |
| 121 | } |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 122 | float target_bitrate_bps = target_bitrate_bps_; |
Erik Språng | 51e6030 | 2016-06-10 22:13:21 +0200 | [diff] [blame] | 123 | float estimated_bitrate_bps = |
| 124 | bitrate_tracker_.Rate(current_time_ms).value_or(target_bitrate_bps); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 125 | float error = target_bitrate_bps - estimated_bitrate_bps; |
| 126 | |
| 127 | // Adjust if we've overshot by any amount or if we've undershot too much. |
| 128 | if (estimated_bitrate_bps > target_bitrate_bps || |
| 129 | error > kBitrateTolerancePct * target_bitrate_bps) { |
| 130 | // Adjust the bitrate by a fraction of the error. |
| 131 | float adjustment = .5 * error; |
| 132 | float adjusted_bitrate_bps = target_bitrate_bps + adjustment; |
| 133 | |
| 134 | // Clamp the adjustment. |
| 135 | float min_bitrate_bps = GetMinAdjustedBitrateBps(); |
| 136 | float max_bitrate_bps = GetMaxAdjustedBitrateBps(); |
| 137 | adjusted_bitrate_bps = std::max(adjusted_bitrate_bps, min_bitrate_bps); |
| 138 | adjusted_bitrate_bps = std::min(adjusted_bitrate_bps, max_bitrate_bps); |
| 139 | |
| 140 | // Set the adjustment if it's not already set. |
| 141 | float last_adjusted_bitrate_bps = adjusted_bitrate_bps_; |
| 142 | if (adjusted_bitrate_bps != last_adjusted_bitrate_bps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 143 | RTC_LOG(LS_VERBOSE) << "Adjusting encoder bitrate:" |
| 144 | << "\n target_bitrate:" |
| 145 | << static_cast<uint32_t>(target_bitrate_bps) |
| 146 | << "\n estimated_bitrate:" |
| 147 | << static_cast<uint32_t>(estimated_bitrate_bps) |
| 148 | << "\n last_adjusted_bitrate:" |
| 149 | << static_cast<uint32_t>(last_adjusted_bitrate_bps) |
| 150 | << "\n adjusted_bitrate:" |
| 151 | << static_cast<uint32_t>(adjusted_bitrate_bps); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 152 | adjusted_bitrate_bps_ = adjusted_bitrate_bps; |
| 153 | } |
| 154 | } |
| 155 | last_bitrate_update_time_ms_ = current_time_ms; |
| 156 | frames_since_last_update_ = 0; |
| 157 | last_adjusted_target_bitrate_bps_ = target_bitrate_bps_; |
| 158 | } |
| 159 | |
| 160 | } // namespace webrtc |