OLD | NEW |
---|---|
(Empty) | |
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 "webrtc/modules/video_coding/include/bitrate_adjuster.h" | |
12 | |
13 #include <cmath> | |
14 | |
15 #include "webrtc/base/checks.h" | |
16 #include "webrtc/base/logging.h" | |
17 #include "webrtc/system_wrappers/include/clock.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 = .1; | |
29 | |
30 const float BitrateAdjuster::kBytesPerMsToBitsPerSecond = 8 * 1000; | |
31 | |
32 BitrateAdjuster::BitrateAdjuster(Clock* clock) | |
33 : clock_(clock), | |
34 min_adjusted_bitrate_pct_(.5), | |
35 max_adjusted_bitrate_pct_(1), | |
36 bitrate_tracker_(1.5 * kBitrateUpdateIntervalMs, | |
37 kBytesPerMsToBitsPerSecond) { | |
38 Reset(); | |
39 } | |
40 | |
41 void BitrateAdjuster::SetTargetBitrateBps(uint32_t bitrate_bps) { | |
42 rtc::CritScope cs(&crit_); | |
43 float current_target_bitrate_bps = target_bitrate_bps_; | |
pbos-webrtc
2016/02/23 15:04:40
Just use directly, static_cast?
tkchin_webrtc
2016/02/23 17:03:20
Done.
| |
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, current_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 | |
71 void BitrateAdjuster::SetMinAdjustedBitratePct(float target_bitrate_pct) { | |
72 rtc::CritScope cs(&crit_); | |
pbos-webrtc
2016/02/23 15:04:40
Make const and set in the constructor instead.
tkchin_webrtc
2016/02/23 17:03:20
Done.
| |
73 min_adjusted_bitrate_pct_ = target_bitrate_pct; | |
74 } | |
75 | |
76 void BitrateAdjuster::SetMaxAdjustedBitratePct(float target_bitrate_pct) { | |
77 rtc::CritScope cs(&crit_); | |
78 max_adjusted_bitrate_pct_ = target_bitrate_pct; | |
79 } | |
80 | |
81 uint32_t BitrateAdjuster::GetEstimatedBitrateBps() { | |
82 rtc::CritScope cs(&crit_); | |
83 return bitrate_tracker_.Rate(clock_->TimeInMilliseconds()); | |
84 } | |
85 | |
86 void BitrateAdjuster::Update(size_t frame_size) { | |
87 rtc::CritScope cs(&crit_); | |
88 uint32_t current_time_ms = clock_->TimeInMilliseconds(); | |
89 bitrate_tracker_.Update(frame_size, current_time_ms); | |
90 UpdateBitrate(current_time_ms); | |
91 } | |
92 | |
93 bool BitrateAdjuster::IsWithinTolerance(uint32_t bitrate_bps, | |
94 uint32_t target_bitrate_bps) { | |
95 if (target_bitrate_bps == 0) { | |
96 return false; | |
97 } | |
98 float delta = std::abs(static_cast<float>(bitrate_bps) - | |
99 static_cast<float>(target_bitrate_bps)); | |
100 float delta_pct = delta / target_bitrate_bps; | |
101 return delta_pct < kBitrateTolerancePct; | |
102 } | |
103 | |
104 uint32_t BitrateAdjuster::GetMinAdjustedBitrateBps() const { | |
105 return min_adjusted_bitrate_pct_ * target_bitrate_bps_; | |
106 } | |
107 | |
108 uint32_t BitrateAdjuster::GetMaxAdjustedBitrateBps() const { | |
109 return max_adjusted_bitrate_pct_ * target_bitrate_bps_; | |
110 } | |
111 | |
112 // Only safe to call this after Update calls have stopped | |
113 void BitrateAdjuster::Reset() { | |
114 rtc::CritScope cs(&crit_); | |
115 target_bitrate_bps_ = 0; | |
116 adjusted_bitrate_bps_ = 0; | |
117 last_adjusted_target_bitrate_bps_ = 0; | |
118 last_bitrate_update_time_ms_ = 0; | |
119 frames_since_last_update_ = 0; | |
120 bitrate_tracker_.Reset(); | |
121 } | |
122 | |
123 void BitrateAdjuster::UpdateBitrate(uint32_t current_time_ms) { | |
124 uint32_t time_since_last_update_ms = | |
125 current_time_ms - last_bitrate_update_time_ms_; | |
126 // Don't attempt to update bitrate unless enough time and frames have passed. | |
127 ++frames_since_last_update_; | |
128 if (time_since_last_update_ms < kBitrateUpdateIntervalMs || | |
129 frames_since_last_update_ < kBitrateUpdateFrameInterval) { | |
130 return; | |
131 } | |
132 float estimated_bitrate_bps = bitrate_tracker_.Rate(current_time_ms); | |
133 float target_bitrate_bps = target_bitrate_bps_; | |
134 float error = target_bitrate_bps - estimated_bitrate_bps; | |
135 | |
136 // Adjust if we've overshot by any amount or if we've undershot too much. | |
137 if (estimated_bitrate_bps > target_bitrate_bps || | |
138 error > kBitrateTolerancePct * target_bitrate_bps) { | |
139 // Adjust the bitrate by a fraction of the error. | |
140 float adjustment = .5 * error; | |
141 float adjusted_bitrate_bps = target_bitrate_bps + adjustment; | |
142 | |
143 // Clamp the adjustment to [0.5 * target, 0.95 * target]. | |
144 float min_bitrate_bps = GetMinAdjustedBitrateBps(); | |
145 float max_bitrate_bps = GetMaxAdjustedBitrateBps(); | |
146 adjusted_bitrate_bps = std::max(adjusted_bitrate_bps, min_bitrate_bps); | |
147 adjusted_bitrate_bps = std::min(adjusted_bitrate_bps, max_bitrate_bps); | |
148 | |
149 // Set the adjustment if it's not already set. | |
150 float last_adjusted_bitrate_bps = adjusted_bitrate_bps_; | |
151 if (adjusted_bitrate_bps != last_adjusted_bitrate_bps) { | |
152 LOG(LS_VERBOSE) << "Adjusting encoder bitrate:" | |
153 << "\n target_bitrate:" | |
154 << static_cast<uint32_t>(target_bitrate_bps) | |
155 << "\n estimated_bitrate:" | |
156 << static_cast<uint32_t>(estimated_bitrate_bps) | |
157 << "\n last_adjusted_bitrate:" | |
158 << static_cast<uint32_t>(last_adjusted_bitrate_bps) | |
159 << "\n adjusted_bitrate:" | |
160 << static_cast<uint32_t>(adjusted_bitrate_bps); | |
161 adjusted_bitrate_bps_ = adjusted_bitrate_bps; | |
162 } | |
163 } | |
164 last_bitrate_update_time_ms_ = current_time_ms; | |
165 frames_since_last_update_ = 0; | |
166 last_adjusted_target_bitrate_bps_ = target_bitrate_bps_; | |
167 } | |
168 | |
169 } // namespace webrtc | |
OLD | NEW |