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" | |
24 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 23 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s
end_time.h" | 24 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s
end_time.h" |
26 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl
e_stream.h" | 25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl
e_stream.h" |
27 #include "webrtc/modules/utility/include/process_thread.h" | 26 #include "webrtc/modules/utility/include/process_thread.h" |
28 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 27 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
29 #include "webrtc/video/payload_router.h" | 28 #include "webrtc/video/payload_router.h" |
30 | 29 |
31 namespace webrtc { | 30 namespace webrtc { |
32 namespace { | 31 namespace { |
33 | 32 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 uint32_t packets_since_absolute_send_time_; | 130 uint32_t packets_since_absolute_send_time_; |
132 int min_bitrate_bps_; | 131 int min_bitrate_bps_; |
133 | 132 |
134 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); | 133 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); |
135 }; | 134 }; |
136 | 135 |
137 } // namespace | 136 } // namespace |
138 | 137 |
139 CongestionController::CongestionController( | 138 CongestionController::CongestionController( |
140 Clock* clock, | 139 Clock* clock, |
141 BitrateObserver* bitrate_observer, | 140 Observer* observer, |
142 RemoteBitrateObserver* remote_bitrate_observer) | 141 RemoteBitrateObserver* remote_bitrate_observer) |
143 : clock_(clock), | 142 : clock_(clock), |
| 143 observer_(observer), |
| 144 packet_router_(new PacketRouter()), |
144 pacer_(new PacedSender(clock_, | 145 pacer_(new PacedSender(clock_, |
145 &packet_router_, | 146 packet_router_.get(), |
146 BitrateController::kDefaultStartBitrateKbps, | 147 BitrateController::kDefaultStartBitratebps)), |
147 PacedSender::kDefaultPaceMultiplier * | 148 remote_bitrate_estimator_( |
148 BitrateController::kDefaultStartBitrateKbps, | 149 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
149 0)), | 150 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), |
| 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)), |
150 remote_bitrate_estimator_( | 168 remote_bitrate_estimator_( |
151 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), | 169 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
152 // Constructed last as this object calls the provided callback on | 170 // Constructed last as this object calls the provided callback on |
153 // construction. | 171 // construction. |
154 bitrate_controller_( | 172 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), |
155 BitrateController::CreateBitrateController(clock_, bitrate_observer)), | 173 remote_estimator_proxy_(clock_, packet_router_.get()), |
156 remote_estimator_proxy_(clock_, &packet_router_), | |
157 transport_feedback_adapter_(bitrate_controller_.get(), clock_), | 174 transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
158 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) { | 175 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
| 176 send_queue_is_full_(false) { |
| 177 Init(); |
| 178 } |
| 179 |
| 180 CongestionController::~CongestionController() {} |
| 181 |
| 182 void CongestionController::Init() { |
159 transport_feedback_adapter_.SetBitrateEstimator( | 183 transport_feedback_adapter_.SetBitrateEstimator( |
160 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_)); | 184 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_)); |
161 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( | 185 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
162 min_bitrate_bps_); | 186 min_bitrate_bps_); |
163 } | 187 // This calls the observer_, which means that the observer provided by the |
164 | 188 // user must be ready to accept a bitrate update when it constructs the |
165 CongestionController::~CongestionController() { | 189 // controller. We do this to avoid having to keep synchronized initial values |
| 190 // in both the controller and the allocator. |
| 191 MaybeTriggerOnNetworkChanged(); |
166 } | 192 } |
167 | 193 |
168 | 194 |
169 void CongestionController::SetBweBitrates(int min_bitrate_bps, | 195 void CongestionController::SetBweBitrates(int min_bitrate_bps, |
170 int start_bitrate_bps, | 196 int start_bitrate_bps, |
171 int max_bitrate_bps) { | 197 int max_bitrate_bps) { |
172 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, | 198 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, |
173 // and that we don't try to set the min bitrate to 0 from any applications. | 199 // and that we don't try to set the min bitrate to 0 from any applications. |
174 // The congestion controller should allow a min bitrate of 0. | 200 // The congestion controller should allow a min bitrate of 0. |
175 const int kMinBitrateBps = 10000; | 201 const int kMinBitrateBps = 10000; |
176 if (min_bitrate_bps < kMinBitrateBps) | 202 if (min_bitrate_bps < kMinBitrateBps) |
177 min_bitrate_bps = kMinBitrateBps; | 203 min_bitrate_bps = kMinBitrateBps; |
178 if (max_bitrate_bps > 0) | 204 if (max_bitrate_bps > 0) |
179 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); | 205 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); |
180 if (start_bitrate_bps > 0) | 206 if (start_bitrate_bps > 0) |
181 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); | 207 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); |
182 | 208 |
183 bitrate_controller_->SetBitrates(start_bitrate_bps, | 209 bitrate_controller_->SetBitrates(start_bitrate_bps, |
184 min_bitrate_bps, | 210 min_bitrate_bps, |
185 max_bitrate_bps); | 211 max_bitrate_bps); |
186 | 212 |
187 if (remote_bitrate_estimator_) | 213 if (remote_bitrate_estimator_) |
188 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); | 214 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
189 min_bitrate_bps_ = min_bitrate_bps; | 215 min_bitrate_bps_ = min_bitrate_bps; |
190 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( | 216 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
191 min_bitrate_bps_); | 217 min_bitrate_bps_); |
| 218 MaybeTriggerOnNetworkChanged(); |
192 } | 219 } |
193 | 220 |
194 BitrateController* CongestionController::GetBitrateController() const { | 221 BitrateController* CongestionController::GetBitrateController() const { |
195 return bitrate_controller_.get(); | 222 return bitrate_controller_.get(); |
196 } | 223 } |
197 | 224 |
198 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( | 225 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( |
199 bool send_side_bwe) { | 226 bool send_side_bwe) { |
200 if (send_side_bwe) { | 227 if (send_side_bwe) { |
201 return &remote_estimator_proxy_; | 228 return &remote_estimator_proxy_; |
202 } else { | 229 } else { |
203 return remote_bitrate_estimator_.get(); | 230 return remote_bitrate_estimator_.get(); |
204 } | 231 } |
205 } | 232 } |
206 | 233 |
207 TransportFeedbackObserver* | 234 TransportFeedbackObserver* |
208 CongestionController::GetTransportFeedbackObserver() { | 235 CongestionController::GetTransportFeedbackObserver() { |
209 return &transport_feedback_adapter_; | 236 return &transport_feedback_adapter_; |
210 } | 237 } |
211 | 238 |
212 void CongestionController::UpdatePacerBitrate(int bitrate_kbps, | 239 void CongestionController::SetAllocatedSendBitrate(int allocated_bitrate_bps, |
213 int max_bitrate_kbps, | 240 int padding_bitrate_bps) { |
214 int min_bitrate_kbps) { | 241 pacer_->SetAllocatedSendBitrate(allocated_bitrate_bps, padding_bitrate_bps); |
215 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps); | |
216 } | 242 } |
217 | 243 |
218 int64_t CongestionController::GetPacerQueuingDelayMs() const { | 244 int64_t CongestionController::GetPacerQueuingDelayMs() const { |
219 return pacer_->QueueInMs(); | 245 return pacer_->QueueInMs(); |
220 } | 246 } |
221 | 247 |
222 void CongestionController::SignalNetworkState(NetworkState state) { | 248 void CongestionController::SignalNetworkState(NetworkState state) { |
223 if (state == kNetworkUp) { | 249 if (state == kNetworkUp) { |
224 pacer_->Resume(); | 250 pacer_->Resume(); |
225 } else { | 251 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
238 } | 264 } |
239 | 265 |
240 int64_t CongestionController::TimeUntilNextProcess() { | 266 int64_t CongestionController::TimeUntilNextProcess() { |
241 return std::min(bitrate_controller_->TimeUntilNextProcess(), | 267 return std::min(bitrate_controller_->TimeUntilNextProcess(), |
242 remote_bitrate_estimator_->TimeUntilNextProcess()); | 268 remote_bitrate_estimator_->TimeUntilNextProcess()); |
243 } | 269 } |
244 | 270 |
245 void CongestionController::Process() { | 271 void CongestionController::Process() { |
246 bitrate_controller_->Process(); | 272 bitrate_controller_->Process(); |
247 remote_bitrate_estimator_->Process(); | 273 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; |
248 } | 299 } |
249 | 300 |
250 } // namespace webrtc | 301 } // namespace webrtc |
OLD | NEW |