Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: webrtc/modules/congestion_controller/congestion_controller.cc

Issue 2061423003: Refactor NACK bitrate allocation (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Moved rate limiter and addressed comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/congestion_controller/delay_based_bwe.h" 23 #include "webrtc/modules/congestion_controller/delay_based_bwe.h"
24 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" 24 #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" 25 #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" 26 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl e_stream.h"
27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
27 #include "webrtc/modules/utility/include/process_thread.h" 28 #include "webrtc/modules/utility/include/process_thread.h"
28 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" 29 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
29 #include "webrtc/video/payload_router.h" 30 #include "webrtc/video/payload_router.h"
30 31
31 namespace webrtc { 32 namespace webrtc {
32 namespace { 33 namespace {
33 34
34 static const uint32_t kTimeOffsetSwitchThreshold = 30; 35 static const uint32_t kTimeOffsetSwitchThreshold = 30;
36 static const int64_t kMinRetransmitWindowSizeMs = 30;
37 static const int64_t kMaxRetransmitWindowSizeMs = 1000;
35 38
36 class WrappingBitrateEstimator : public RemoteBitrateEstimator { 39 class WrappingBitrateEstimator : public RemoteBitrateEstimator {
37 public: 40 public:
38 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) 41 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
39 : observer_(observer), 42 : observer_(observer),
40 clock_(clock), 43 clock_(clock),
41 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), 44 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
42 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)), 45 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
43 using_absolute_send_time_(false), 46 using_absolute_send_time_(false),
44 packets_since_absolute_send_time_(0), 47 packets_since_absolute_send_time_(0),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 std::unique_ptr<RemoteBitrateEstimator> rbe_; 131 std::unique_ptr<RemoteBitrateEstimator> rbe_;
129 bool using_absolute_send_time_; 132 bool using_absolute_send_time_;
130 uint32_t packets_since_absolute_send_time_; 133 uint32_t packets_since_absolute_send_time_;
131 int min_bitrate_bps_; 134 int min_bitrate_bps_;
132 135
133 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); 136 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
134 }; 137 };
135 138
136 } // namespace 139 } // namespace
137 140
141 class RateLimiter : public NackRateLimiter {
142 public:
143 RateLimiter(Clock* clock, int64_t max_window_size_ms, uint32_t max_rate_bps)
144 : clock_(clock),
145 current_rate_(max_window_size_ms, RateStatistics::kBpsScale),
146 window_size_ms_(max_window_size_ms),
147 max_rate_bps_(max_rate_bps) {}
148 virtual ~RateLimiter() {}
149
150 // Try to use rate to send bytes. Returns true on success and if so updates
151 // current rate.
152 bool TryUseRate(size_t bytes) override {
153 rtc::CritScope cs(&lock_);
154 rtc::Optional<uint32_t> current_rate =
155 current_rate_.Rate(clock_->TimeInMilliseconds());
156 if (current_rate) {
157 // If there is a current rate, check if adding bytes would cause maximum
158 // bitrate target to be exceeded. If there is NOT a valid current rate,
159 // allow allocating rate even if target is exceeded. This prevents
160 // problems
161 // at very low rates, where for instance retransmissions would never be
162 // allowed due to too high bitrate caused by a single packet.
163
164 uint32_t bitrate_addition_bps = (bytes * 8 * 1000) / window_size_ms_;
165 if (*current_rate + bitrate_addition_bps > max_rate_bps_)
166 return false;
167 }
168
169 current_rate_.Update(bytes, clock_->TimeInMilliseconds());
170 return true;
171 }
172
173 // Set the maximum bitrate, in bps, that this limiter allows to send.
174 void SetMaxRate(uint32_t max_bitrate_bps) {
175 rtc::CritScope cs(&lock_);
176 max_rate_bps_ = max_bitrate_bps;
danilchap 2016/06/28 14:26:17 maybe rename max_rate_bps_ to max_bitrate_bps_ or
sprang_webrtc 2016/07/04 09:33:03 Done.
177 }
178
179 // Set the window size over which to measure the current bitrate.
180 // For retransmissions, this is typically the RTT.
181 void SetWindowSize(int64_t window_size_ms) {
182 rtc::CritScope cs(&lock_);
183 window_size_ms_ = window_size_ms;
184 current_rate_.SetWindowSize(window_size_ms, clock_->TimeInMilliseconds());
185 }
186
187 private:
188 Clock* const clock_;
189 rtc::CriticalSection lock_;
190 RateStatistics current_rate_ GUARDED_BY(lock_);
191 int64_t window_size_ms_ GUARDED_BY(lock_);
192 uint32_t max_rate_bps_ GUARDED_BY(lock_);
193
194 RTC_DISALLOW_COPY_AND_ASSIGN(RateLimiter);
danilchap 2016/06/28 14:26:17 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RateLimiter) si
sprang_webrtc 2016/07/04 09:33:03 Done.
195 };
196
138 CongestionController::CongestionController( 197 CongestionController::CongestionController(
139 Clock* clock, 198 Clock* clock,
140 BitrateObserver* bitrate_observer, 199 BitrateObserver* bitrate_observer,
141 RemoteBitrateObserver* remote_bitrate_observer) 200 RemoteBitrateObserver* remote_bitrate_observer)
142 : clock_(clock), 201 : clock_(clock),
143 observer_(nullptr), 202 observer_(nullptr),
144 packet_router_(new PacketRouter()), 203 packet_router_(new PacketRouter()),
145 pacer_(new PacedSender(clock_, packet_router_.get())), 204 pacer_(new PacedSender(clock_, packet_router_.get())),
146 remote_bitrate_estimator_( 205 remote_bitrate_estimator_(
147 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), 206 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
148 bitrate_controller_( 207 bitrate_controller_(
149 BitrateController::CreateBitrateController(clock_, bitrate_observer)), 208 BitrateController::CreateBitrateController(clock_, bitrate_observer)),
209 nack_rate_limiter_(
210 new RateLimiter(clock,
211 kMaxRetransmitWindowSizeMs,
212 RemoteBitrateEstimator::kDefaultMinBitrateBps)),
150 remote_estimator_proxy_(clock_, packet_router_.get()), 213 remote_estimator_proxy_(clock_, packet_router_.get()),
151 transport_feedback_adapter_(bitrate_controller_.get(), clock_), 214 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
152 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), 215 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
153 last_reported_bitrate_bps_(0), 216 last_reported_bitrate_bps_(0),
154 last_reported_fraction_loss_(0), 217 last_reported_fraction_loss_(0),
155 last_reported_rtt_(0), 218 last_reported_rtt_(0),
156 network_state_(kNetworkUp) { 219 network_state_(kNetworkUp) {
157 Init(); 220 Init();
158 } 221 }
159 222
160 CongestionController::CongestionController( 223 CongestionController::CongestionController(
161 Clock* clock, 224 Clock* clock,
162 Observer* observer, 225 Observer* observer,
163 RemoteBitrateObserver* remote_bitrate_observer) 226 RemoteBitrateObserver* remote_bitrate_observer)
164 : clock_(clock), 227 : clock_(clock),
165 observer_(observer), 228 observer_(observer),
166 packet_router_(new PacketRouter()), 229 packet_router_(new PacketRouter()),
167 pacer_(new PacedSender(clock_, packet_router_.get())), 230 pacer_(new PacedSender(clock_, packet_router_.get())),
168 remote_bitrate_estimator_( 231 remote_bitrate_estimator_(
169 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), 232 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
170 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), 233 bitrate_controller_(BitrateController::CreateBitrateController(clock_)),
234 nack_rate_limiter_(
235 new RateLimiter(clock,
236 kMaxRetransmitWindowSizeMs,
danilchap 2016/06/28 14:26:17 RateLimiter is local class, and window size/bitrat
sprang_webrtc 2016/07/04 09:33:03 True. This was a leftover from a partial refactori
237 RemoteBitrateEstimator::kDefaultMinBitrateBps)),
171 remote_estimator_proxy_(clock_, packet_router_.get()), 238 remote_estimator_proxy_(clock_, packet_router_.get()),
172 transport_feedback_adapter_(bitrate_controller_.get(), clock_), 239 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
173 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), 240 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
174 last_reported_bitrate_bps_(0), 241 last_reported_bitrate_bps_(0),
175 last_reported_fraction_loss_(0), 242 last_reported_fraction_loss_(0),
176 last_reported_rtt_(0), 243 last_reported_rtt_(0),
177 network_state_(kNetworkUp) { 244 network_state_(kNetworkUp) {
178 Init(); 245 Init();
179 } 246 }
180 247
181 CongestionController::CongestionController( 248 CongestionController::CongestionController(
182 Clock* clock, 249 Clock* clock,
183 Observer* observer, 250 Observer* observer,
184 RemoteBitrateObserver* remote_bitrate_observer, 251 RemoteBitrateObserver* remote_bitrate_observer,
185 std::unique_ptr<PacketRouter> packet_router, 252 std::unique_ptr<PacketRouter> packet_router,
186 std::unique_ptr<PacedSender> pacer) 253 std::unique_ptr<PacedSender> pacer)
187 : clock_(clock), 254 : clock_(clock),
188 observer_(observer), 255 observer_(observer),
189 packet_router_(std::move(packet_router)), 256 packet_router_(std::move(packet_router)),
190 pacer_(std::move(pacer)), 257 pacer_(std::move(pacer)),
191 remote_bitrate_estimator_( 258 remote_bitrate_estimator_(
192 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), 259 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
193 // Constructed last as this object calls the provided callback on 260 // Constructed last as this object calls the provided callback on
194 // construction. 261 // construction.
195 bitrate_controller_(BitrateController::CreateBitrateController(clock_)), 262 bitrate_controller_(BitrateController::CreateBitrateController(clock_)),
263 nack_rate_limiter_(
264 new RateLimiter(clock,
265 kMaxRetransmitWindowSizeMs,
266 RemoteBitrateEstimator::kDefaultMinBitrateBps)),
196 remote_estimator_proxy_(clock_, packet_router_.get()), 267 remote_estimator_proxy_(clock_, packet_router_.get()),
197 transport_feedback_adapter_(bitrate_controller_.get(), clock_), 268 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
198 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), 269 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
199 last_reported_bitrate_bps_(0), 270 last_reported_bitrate_bps_(0),
200 last_reported_fraction_loss_(0), 271 last_reported_fraction_loss_(0),
201 last_reported_rtt_(0), 272 last_reported_rtt_(0),
202 network_state_(kNetworkUp) { 273 network_state_(kNetworkUp) {
203 Init(); 274 Init();
204 } 275 }
205 276
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 } else { 321 } else {
251 return remote_bitrate_estimator_.get(); 322 return remote_bitrate_estimator_.get();
252 } 323 }
253 } 324 }
254 325
255 TransportFeedbackObserver* 326 TransportFeedbackObserver*
256 CongestionController::GetTransportFeedbackObserver() { 327 CongestionController::GetTransportFeedbackObserver() {
257 return &transport_feedback_adapter_; 328 return &transport_feedback_adapter_;
258 } 329 }
259 330
331 NackRateLimiter* CongestionController::GetNackRateLimiter() {
332 return nack_rate_limiter_.get();
333 }
334
260 void CongestionController::SetAllocatedSendBitrateLimits( 335 void CongestionController::SetAllocatedSendBitrateLimits(
261 int min_send_bitrate_bps, 336 int min_send_bitrate_bps,
262 int max_padding_bitrate_bps) { 337 int max_padding_bitrate_bps) {
263 pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps); 338 pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps);
264 } 339 }
265 340
266 int64_t CongestionController::GetPacerQueuingDelayMs() const { 341 int64_t CongestionController::GetPacerQueuingDelayMs() const {
267 return pacer_->QueueInMs(); 342 return pacer_->QueueInMs();
268 } 343 }
269 344
(...skipping 11 matching lines...) Expand all
281 } 356 }
282 357
283 void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) { 358 void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
284 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id, 359 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
285 sent_packet.send_time_ms); 360 sent_packet.send_time_ms);
286 } 361 }
287 362
288 void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { 363 void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
289 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); 364 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
290 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms); 365 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
366
367 int64_t nack_window_size = max_rtt_ms;
danilchap 2016/06/28 14:26:17 nack_window_size_ms
sprang_webrtc 2016/07/04 09:33:03 Done.
368 if (nack_window_size > kMaxRetransmitWindowSizeMs) {
369 nack_window_size = kMaxRetransmitWindowSizeMs;
370 } else if (nack_window_size < kMinRetransmitWindowSizeMs) {
371 nack_window_size = kMinRetransmitWindowSizeMs;
372 }
373 nack_rate_limiter_->SetWindowSize(nack_window_size);
291 } 374 }
292 375
293 int64_t CongestionController::TimeUntilNextProcess() { 376 int64_t CongestionController::TimeUntilNextProcess() {
294 return std::min(bitrate_controller_->TimeUntilNextProcess(), 377 return std::min(bitrate_controller_->TimeUntilNextProcess(),
295 remote_bitrate_estimator_->TimeUntilNextProcess()); 378 remote_bitrate_estimator_->TimeUntilNextProcess());
296 } 379 }
297 380
298 void CongestionController::Process() { 381 void CongestionController::Process() {
299 bitrate_controller_->Process(); 382 bitrate_controller_->Process();
300 remote_bitrate_estimator_->Process(); 383 remote_bitrate_estimator_->Process();
301 MaybeTriggerOnNetworkChanged(); 384 MaybeTriggerOnNetworkChanged();
302 } 385 }
303 386
304 void CongestionController::MaybeTriggerOnNetworkChanged() { 387 void CongestionController::MaybeTriggerOnNetworkChanged() {
305 // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a 388 // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a
306 // BitrateObserver is used. Remove this check once the ctor is removed. 389 // BitrateObserver is used. Remove this check once the ctor is removed.
307 if (!observer_) 390 if (!observer_)
308 return; 391 return;
309 392
310 uint32_t bitrate_bps; 393 uint32_t bitrate_bps;
311 uint8_t fraction_loss; 394 uint8_t fraction_loss;
312 int64_t rtt; 395 int64_t rtt;
313 bool estimate_changed = bitrate_controller_->GetNetworkParameters( 396 bool estimate_changed = bitrate_controller_->GetNetworkParameters(
314 &bitrate_bps, &fraction_loss, &rtt); 397 &bitrate_bps, &fraction_loss, &rtt);
315 if (estimate_changed) 398 if (estimate_changed) {
316 pacer_->SetEstimatedBitrate(bitrate_bps); 399 pacer_->SetEstimatedBitrate(bitrate_bps);
400 nack_rate_limiter_->SetMaxRate(bitrate_bps);
401 }
317 402
318 bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps; 403 bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps;
319 404
320 if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) { 405 if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) {
321 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt); 406 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
322 } 407 }
323 } 408 }
324 409
325 bool CongestionController::HasNetworkParametersToReportChanged( 410 bool CongestionController::HasNetworkParametersToReportChanged(
326 uint32_t bitrate_bps, 411 uint32_t bitrate_bps,
(...skipping 13 matching lines...) Expand all
340 bool CongestionController::IsSendQueueFull() const { 425 bool CongestionController::IsSendQueueFull() const {
341 return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs; 426 return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs;
342 } 427 }
343 428
344 bool CongestionController::IsNetworkDown() const { 429 bool CongestionController::IsNetworkDown() const {
345 rtc::CritScope cs(&critsect_); 430 rtc::CritScope cs(&critsect_);
346 return network_state_ == kNetworkDown; 431 return network_state_ == kNetworkDown;
347 } 432 }
348 433
349 } // namespace webrtc 434 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698