Ying Wang | e1d7b23 | 2018-07-17 16:01:25 +0200 | [diff] [blame] | 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 <string> |
| 12 | #include <algorithm> |
| 13 | |
| 14 | #include "modules/congestion_controller/congestion_window_pushback_controller.h" |
| 15 | #include "system_wrappers/include/field_trial.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // When CongestionWindowPushback is enabled, the pacer is oblivious to |
| 20 | // the congestion window. The relation between outstanding data and |
| 21 | // the congestion window affects encoder allocations directly. |
| 22 | // This experiment is build on top of congestion window experiment. |
| 23 | const char kCongestionPushbackExperiment[] = "WebRTC-CongestionWindowPushback"; |
| 24 | const uint32_t kDefaultMinPushbackTargetBitrateBps = 30000; |
| 25 | |
| 26 | bool ReadCongestionWindowPushbackExperimentParameter( |
| 27 | uint32_t* min_pushback_target_bitrate_bps) { |
| 28 | RTC_DCHECK(min_pushback_target_bitrate_bps); |
| 29 | std::string experiment_string = |
| 30 | webrtc::field_trial::FindFullName(kCongestionPushbackExperiment); |
| 31 | int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%" PRIu32, |
| 32 | min_pushback_target_bitrate_bps); |
| 33 | if (parsed_values == 1) { |
| 34 | RTC_CHECK_GE(*min_pushback_target_bitrate_bps, 0) |
| 35 | << "Min pushback target bitrate must be greater than or equal to 0."; |
| 36 | return true; |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | CongestionWindowPushbackController::CongestionWindowPushbackController() { |
| 42 | if (!ReadCongestionWindowPushbackExperimentParameter( |
| 43 | &min_pushback_target_bitrate_bps_)) { |
| 44 | min_pushback_target_bitrate_bps_ = kDefaultMinPushbackTargetBitrateBps; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void CongestionWindowPushbackController::UpdateOutstandingData( |
| 49 | size_t outstanding_bytes) { |
| 50 | outstanding_bytes_ = outstanding_bytes; |
| 51 | } |
| 52 | |
| 53 | void CongestionWindowPushbackController::UpdateMaxOutstandingData( |
| 54 | size_t max_outstanding_bytes) { |
| 55 | DataSize data_window = DataSize::bytes(max_outstanding_bytes); |
| 56 | if (current_data_window_) { |
| 57 | data_window = (data_window + current_data_window_.value()) / 2; |
| 58 | } |
| 59 | current_data_window_ = data_window; |
| 60 | } |
| 61 | |
| 62 | void CongestionWindowPushbackController::SetDataWindow(DataSize data_window) { |
| 63 | current_data_window_ = data_window; |
| 64 | } |
| 65 | |
| 66 | uint32_t CongestionWindowPushbackController::UpdateTargetBitrate( |
| 67 | uint32_t bitrate_bps) { |
| 68 | if (!current_data_window_ || current_data_window_->IsZero()) |
| 69 | return bitrate_bps; |
| 70 | double fill_ratio = |
| 71 | outstanding_bytes_ / static_cast<double>(current_data_window_->bytes()); |
| 72 | if (fill_ratio > 1.5) { |
| 73 | encoding_rate_ratio_ *= 0.9; |
| 74 | } else if (fill_ratio > 1) { |
| 75 | encoding_rate_ratio_ *= 0.95; |
| 76 | } else if (fill_ratio < 0.1) { |
| 77 | encoding_rate_ratio_ = 1.0; |
| 78 | } else { |
| 79 | encoding_rate_ratio_ *= 1.05; |
| 80 | encoding_rate_ratio_ = std::min(encoding_rate_ratio_, 1.0); |
| 81 | } |
| 82 | uint32_t adjusted_target_bitrate_bps = |
| 83 | static_cast<uint32_t>(bitrate_bps * encoding_rate_ratio_); |
| 84 | |
| 85 | // Do not adjust below the minimum pushback bitrate but do obey if the |
| 86 | // original estimate is below it. |
| 87 | bitrate_bps = adjusted_target_bitrate_bps < min_pushback_target_bitrate_bps_ |
| 88 | ? std::min(bitrate_bps, min_pushback_target_bitrate_bps_) |
| 89 | : adjusted_target_bitrate_bps; |
| 90 | return bitrate_bps; |
| 91 | } |
| 92 | |
| 93 | } // namespace webrtc |