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