OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 11 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | 14 #include <memory> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/socket.h" | 20 #include "webrtc/base/socket.h" |
21 #include "webrtc/base/thread_annotations.h" | 21 #include "webrtc/base/thread_annotations.h" |
22 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 22 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 23 #include "webrtc/modules/pacing/paced_sender.h" |
23 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 24 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
24 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s
end_time.h" | 25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s
end_time.h" |
25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl
e_stream.h" | 26 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl
e_stream.h" |
26 #include "webrtc/modules/utility/include/process_thread.h" | 27 #include "webrtc/modules/utility/include/process_thread.h" |
27 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 28 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
28 #include "webrtc/video/payload_router.h" | 29 #include "webrtc/video/payload_router.h" |
29 | 30 |
30 namespace webrtc { | 31 namespace webrtc { |
31 namespace { | 32 namespace { |
32 | 33 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 uint32_t packets_since_absolute_send_time_; | 131 uint32_t packets_since_absolute_send_time_; |
131 int min_bitrate_bps_; | 132 int min_bitrate_bps_; |
132 | 133 |
133 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); | 134 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); |
134 }; | 135 }; |
135 | 136 |
136 } // namespace | 137 } // namespace |
137 | 138 |
138 CongestionController::CongestionController( | 139 CongestionController::CongestionController( |
139 Clock* clock, | 140 Clock* clock, |
140 Observer* observer, | 141 BitrateObserver* bitrate_observer, |
141 RemoteBitrateObserver* remote_bitrate_observer) | 142 RemoteBitrateObserver* remote_bitrate_observer) |
142 : clock_(clock), | 143 : clock_(clock), |
143 observer_(observer), | |
144 packet_router_(new PacketRouter()), | |
145 pacer_(new PacedSender(clock_, | 144 pacer_(new PacedSender(clock_, |
146 packet_router_.get(), | 145 &packet_router_, |
147 BitrateController::kDefaultStartBitratebps)), | 146 BitrateController::kDefaultStartBitrateKbps, |
148 remote_bitrate_estimator_( | 147 PacedSender::kDefaultPaceMultiplier * |
149 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), | 148 BitrateController::kDefaultStartBitrateKbps, |
150 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), | 149 0)), |
151 remote_estimator_proxy_(clock_, packet_router_.get()), | |
152 transport_feedback_adapter_(bitrate_controller_.get(), clock_), | |
153 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), | |
154 send_queue_is_full_(false) { | |
155 Init(); | |
156 } | |
157 | |
158 CongestionController::CongestionController( | |
159 Clock* clock, | |
160 Observer* observer, | |
161 RemoteBitrateObserver* remote_bitrate_observer, | |
162 std::unique_ptr<PacketRouter> packet_router, | |
163 std::unique_ptr<PacedSender> pacer) | |
164 : clock_(clock), | |
165 observer_(observer), | |
166 packet_router_(std::move(packet_router)), | |
167 pacer_(std::move(pacer)), | |
168 remote_bitrate_estimator_( | 150 remote_bitrate_estimator_( |
169 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), | 151 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
170 // Constructed last as this object calls the provided callback on | 152 // Constructed last as this object calls the provided callback on |
171 // construction. | 153 // construction. |
172 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), | 154 bitrate_controller_( |
173 remote_estimator_proxy_(clock_, packet_router_.get()), | 155 BitrateController::CreateBitrateController(clock_, bitrate_observer)), |
| 156 remote_estimator_proxy_(clock_, &packet_router_), |
174 transport_feedback_adapter_(bitrate_controller_.get(), clock_), | 157 transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
175 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), | 158 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) { |
176 send_queue_is_full_(false) { | |
177 Init(); | |
178 } | |
179 | |
180 CongestionController::~CongestionController() {} | |
181 | |
182 void CongestionController::Init() { | |
183 transport_feedback_adapter_.SetBitrateEstimator( | 159 transport_feedback_adapter_.SetBitrateEstimator( |
184 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_)); | 160 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_)); |
185 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( | 161 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
186 min_bitrate_bps_); | 162 min_bitrate_bps_); |
187 // This calls the observer_, which means that the observer provided by the | 163 } |
188 // user must be ready to accept a bitrate update when it constructs the | 164 |
189 // controller. We do this to avoid having to keep synchronized initial values | 165 CongestionController::~CongestionController() { |
190 // in both the controller and the allocator. | |
191 MaybeTriggerOnNetworkChanged(); | |
192 } | 166 } |
193 | 167 |
194 | 168 |
195 void CongestionController::SetBweBitrates(int min_bitrate_bps, | 169 void CongestionController::SetBweBitrates(int min_bitrate_bps, |
196 int start_bitrate_bps, | 170 int start_bitrate_bps, |
197 int max_bitrate_bps) { | 171 int max_bitrate_bps) { |
198 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, | 172 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, |
199 // and that we don't try to set the min bitrate to 0 from any applications. | 173 // and that we don't try to set the min bitrate to 0 from any applications. |
200 // The congestion controller should allow a min bitrate of 0. | 174 // The congestion controller should allow a min bitrate of 0. |
201 const int kMinBitrateBps = 10000; | 175 const int kMinBitrateBps = 10000; |
202 if (min_bitrate_bps < kMinBitrateBps) | 176 if (min_bitrate_bps < kMinBitrateBps) |
203 min_bitrate_bps = kMinBitrateBps; | 177 min_bitrate_bps = kMinBitrateBps; |
204 if (max_bitrate_bps > 0) | 178 if (max_bitrate_bps > 0) |
205 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); | 179 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); |
206 if (start_bitrate_bps > 0) | 180 if (start_bitrate_bps > 0) |
207 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); | 181 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); |
208 | 182 |
209 bitrate_controller_->SetBitrates(start_bitrate_bps, | 183 bitrate_controller_->SetBitrates(start_bitrate_bps, |
210 min_bitrate_bps, | 184 min_bitrate_bps, |
211 max_bitrate_bps); | 185 max_bitrate_bps); |
212 | 186 |
213 if (remote_bitrate_estimator_) | 187 if (remote_bitrate_estimator_) |
214 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); | 188 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
215 min_bitrate_bps_ = min_bitrate_bps; | 189 min_bitrate_bps_ = min_bitrate_bps; |
216 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( | 190 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
217 min_bitrate_bps_); | 191 min_bitrate_bps_); |
218 MaybeTriggerOnNetworkChanged(); | |
219 } | 192 } |
220 | 193 |
221 BitrateController* CongestionController::GetBitrateController() const { | 194 BitrateController* CongestionController::GetBitrateController() const { |
222 return bitrate_controller_.get(); | 195 return bitrate_controller_.get(); |
223 } | 196 } |
224 | 197 |
225 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( | 198 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( |
226 bool send_side_bwe) { | 199 bool send_side_bwe) { |
227 if (send_side_bwe) { | 200 if (send_side_bwe) { |
228 return &remote_estimator_proxy_; | 201 return &remote_estimator_proxy_; |
229 } else { | 202 } else { |
230 return remote_bitrate_estimator_.get(); | 203 return remote_bitrate_estimator_.get(); |
231 } | 204 } |
232 } | 205 } |
233 | 206 |
234 TransportFeedbackObserver* | 207 TransportFeedbackObserver* |
235 CongestionController::GetTransportFeedbackObserver() { | 208 CongestionController::GetTransportFeedbackObserver() { |
236 return &transport_feedback_adapter_; | 209 return &transport_feedback_adapter_; |
237 } | 210 } |
238 | 211 |
239 void CongestionController::SetAllocatedSendBitrate(int allocated_bitrate_bps, | 212 void CongestionController::UpdatePacerBitrate(int bitrate_kbps, |
240 int padding_bitrate_bps) { | 213 int max_bitrate_kbps, |
241 pacer_->SetAllocatedSendBitrate(allocated_bitrate_bps, padding_bitrate_bps); | 214 int min_bitrate_kbps) { |
| 215 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps); |
242 } | 216 } |
243 | 217 |
244 int64_t CongestionController::GetPacerQueuingDelayMs() const { | 218 int64_t CongestionController::GetPacerQueuingDelayMs() const { |
245 return pacer_->QueueInMs(); | 219 return pacer_->QueueInMs(); |
246 } | 220 } |
247 | 221 |
248 void CongestionController::SignalNetworkState(NetworkState state) { | 222 void CongestionController::SignalNetworkState(NetworkState state) { |
249 if (state == kNetworkUp) { | 223 if (state == kNetworkUp) { |
250 pacer_->Resume(); | 224 pacer_->Resume(); |
251 } else { | 225 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
264 } | 238 } |
265 | 239 |
266 int64_t CongestionController::TimeUntilNextProcess() { | 240 int64_t CongestionController::TimeUntilNextProcess() { |
267 return std::min(bitrate_controller_->TimeUntilNextProcess(), | 241 return std::min(bitrate_controller_->TimeUntilNextProcess(), |
268 remote_bitrate_estimator_->TimeUntilNextProcess()); | 242 remote_bitrate_estimator_->TimeUntilNextProcess()); |
269 } | 243 } |
270 | 244 |
271 void CongestionController::Process() { | 245 void CongestionController::Process() { |
272 bitrate_controller_->Process(); | 246 bitrate_controller_->Process(); |
273 remote_bitrate_estimator_->Process(); | 247 remote_bitrate_estimator_->Process(); |
274 MaybeTriggerOnNetworkChanged(); | |
275 } | |
276 | |
277 void CongestionController::MaybeTriggerOnNetworkChanged() { | |
278 uint32_t bitrate_bps; | |
279 uint8_t fraction_loss; | |
280 int64_t rtt; | |
281 bool network_changed = bitrate_controller_->GetNetworkParameters( | |
282 &bitrate_bps, &fraction_loss, &rtt); | |
283 if (network_changed) | |
284 pacer_->SetEstimatedBitrate(bitrate_bps); | |
285 bool send_queue_is_full = | |
286 pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs; | |
287 bitrate_bps = send_queue_is_full ? 0 : bitrate_bps; | |
288 if ((network_changed && !send_queue_is_full) || | |
289 UpdateSendQueueStatus(send_queue_is_full)) { | |
290 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt); | |
291 } | |
292 } | |
293 | |
294 bool CongestionController::UpdateSendQueueStatus(bool send_queue_is_full) { | |
295 rtc::CritScope cs(&critsect_); | |
296 bool result = send_queue_is_full_ != send_queue_is_full; | |
297 send_queue_is_full_ = send_queue_is_full; | |
298 return result; | |
299 } | 248 } |
300 | 249 |
301 } // namespace webrtc | 250 } // namespace webrtc |
OLD | NEW |