1 /* 2 * Copyright (c) 2018 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 "modules/congestion_controller/goog_cc/congestion_window_pushback_controller.h" 12 13 #include <inttypes.h> 14 #include <stdio.h> 15 16 #include <algorithm> 17 #include <string> 18 19 #include "absl/strings/match.h" 20 #include "rtc_base/checks.h" 21 #include "rtc_base/experiments/rate_control_settings.h" 22 23 namespace webrtc { 24 CongestionWindowPushbackController(const WebRtcKeyValueConfig * key_value_config)25CongestionWindowPushbackController::CongestionWindowPushbackController( 26 const WebRtcKeyValueConfig* key_value_config) 27 : add_pacing_( 28 absl::StartsWith(key_value_config->Lookup( 29 "WebRTC-AddPacingToCongestionWindowPushback"), 30 "Enabled")), 31 min_pushback_target_bitrate_bps_( 32 RateControlSettings::ParseFromKeyValueConfig(key_value_config) 33 .CongestionWindowMinPushbackTargetBitrateBps()), 34 current_data_window_( 35 RateControlSettings::ParseFromKeyValueConfig(key_value_config) 36 .CongestionWindowInitialDataWindow()) {} 37 UpdateOutstandingData(int64_t outstanding_bytes)38void CongestionWindowPushbackController::UpdateOutstandingData( 39 int64_t outstanding_bytes) { 40 outstanding_bytes_ = outstanding_bytes; 41 } UpdatePacingQueue(int64_t pacing_bytes)42void CongestionWindowPushbackController::UpdatePacingQueue( 43 int64_t pacing_bytes) { 44 pacing_bytes_ = pacing_bytes; 45 } 46 SetDataWindow(DataSize data_window)47void CongestionWindowPushbackController::SetDataWindow(DataSize data_window) { 48 current_data_window_ = data_window; 49 } 50 UpdateTargetBitrate(uint32_t bitrate_bps)51uint32_t CongestionWindowPushbackController::UpdateTargetBitrate( 52 uint32_t bitrate_bps) { 53 if (!current_data_window_ || current_data_window_->IsZero()) 54 return bitrate_bps; 55 int64_t total_bytes = outstanding_bytes_; 56 if (add_pacing_) 57 total_bytes += pacing_bytes_; 58 double fill_ratio = 59 total_bytes / static_cast<double>(current_data_window_->bytes()); 60 if (fill_ratio > 1.5) { 61 encoding_rate_ratio_ *= 0.9; 62 } else if (fill_ratio > 1) { 63 encoding_rate_ratio_ *= 0.95; 64 } else if (fill_ratio < 0.1) { 65 encoding_rate_ratio_ = 1.0; 66 } else { 67 encoding_rate_ratio_ *= 1.05; 68 encoding_rate_ratio_ = std::min(encoding_rate_ratio_, 1.0); 69 } 70 uint32_t adjusted_target_bitrate_bps = 71 static_cast<uint32_t>(bitrate_bps * encoding_rate_ratio_); 72 73 // Do not adjust below the minimum pushback bitrate but do obey if the 74 // original estimate is below it. 75 bitrate_bps = adjusted_target_bitrate_bps < min_pushback_target_bitrate_bps_ 76 ? std::min(bitrate_bps, min_pushback_target_bitrate_bps_) 77 : adjusted_target_bitrate_bps; 78 return bitrate_bps; 79 } 80 81 } // namespace webrtc 82