Erik Språng | 7121564 | 2019-01-21 16:30:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 "rtc_base/experiments/rate_control_settings.h" |
| 12 | |
| 13 | #include <inttypes.h> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include <string> |
| 17 | |
| 18 | #include "api/transport/field_trial_based_config.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/numerics/safe_conversions.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const char* kCongestionWindowFieldTrialName = "WebRTC-CwndExperiment"; |
| 27 | const int kDefaultAcceptedQueueMs = 250; |
| 28 | |
| 29 | const char* kCongestionWindowPushbackFieldTrialName = |
| 30 | "WebRTC-CongestionWindowPushback"; |
| 31 | const int kDefaultMinPushbackTargetBitrateBps = 30000; |
| 32 | |
Erik Språng | 4b4266f | 2019-01-23 12:48:13 +0100 | [diff] [blame] | 33 | const char kVp8TrustedRateControllerFieldTrialName[] = |
| 34 | "WebRTC-LibvpxVp8TrustedRateController"; |
| 35 | const char kVp9TrustedRateControllerFieldTrialName[] = |
| 36 | "WebRTC-LibvpxVp9TrustedRateController"; |
| 37 | |
Erik Språng | 7121564 | 2019-01-21 16:30:55 +0100 | [diff] [blame] | 38 | absl::optional<int> MaybeReadCwndExperimentParameter( |
| 39 | const WebRtcKeyValueConfig* const key_value_config) { |
| 40 | int64_t accepted_queue_ms; |
| 41 | std::string experiment_string = |
| 42 | key_value_config->Lookup(kCongestionWindowFieldTrialName); |
| 43 | int parsed_values = |
| 44 | sscanf(experiment_string.c_str(), "Enabled-%" PRId64, &accepted_queue_ms); |
| 45 | if (parsed_values == 1) { |
| 46 | RTC_CHECK_GE(accepted_queue_ms, 0) |
| 47 | << "Accepted must be greater than or equal to 0."; |
| 48 | return rtc::checked_cast<int>(accepted_queue_ms); |
| 49 | } else if (experiment_string.find("Enabled") == 0) { |
| 50 | return kDefaultAcceptedQueueMs; |
| 51 | } |
| 52 | return absl::nullopt; |
| 53 | } |
| 54 | |
| 55 | absl::optional<int> MaybeReadCongestionWindowPushbackExperimentParameter( |
| 56 | const WebRtcKeyValueConfig* const key_value_config) { |
| 57 | uint32_t min_pushback_target_bitrate_bps; |
| 58 | std::string experiment_string = |
| 59 | key_value_config->Lookup(kCongestionWindowPushbackFieldTrialName); |
| 60 | int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%" PRIu32, |
| 61 | &min_pushback_target_bitrate_bps); |
| 62 | if (parsed_values == 1) { |
| 63 | RTC_CHECK_GE(min_pushback_target_bitrate_bps, 0) |
| 64 | << "Min pushback target bitrate must be greater than or equal to 0."; |
| 65 | return rtc::checked_cast<int>(min_pushback_target_bitrate_bps); |
| 66 | } else if (experiment_string.find("Enabled") == 0) { |
| 67 | return kDefaultMinPushbackTargetBitrateBps; |
| 68 | } |
| 69 | return absl::nullopt; |
| 70 | } |
| 71 | |
Erik Språng | 4b4266f | 2019-01-23 12:48:13 +0100 | [diff] [blame] | 72 | bool IsEnabled(const WebRtcKeyValueConfig* const key_value_config, |
| 73 | absl::string_view key) { |
| 74 | return key_value_config->Lookup(key).find("Enabled") == 0; |
| 75 | } |
| 76 | |
Erik Språng | 7121564 | 2019-01-21 16:30:55 +0100 | [diff] [blame] | 77 | } // namespace |
| 78 | |
| 79 | RateControlSettings::RateControlSettings( |
| 80 | const WebRtcKeyValueConfig* const key_value_config) |
| 81 | : congestion_window_("cwnd", |
| 82 | MaybeReadCwndExperimentParameter(key_value_config)), |
| 83 | congestion_window_pushback_( |
| 84 | "cwnd_pushback", |
| 85 | MaybeReadCongestionWindowPushbackExperimentParameter( |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 86 | key_value_config)), |
| 87 | pacing_factor_("pacing_factor"), |
Erik Språng | 4b4266f | 2019-01-23 12:48:13 +0100 | [diff] [blame] | 88 | alr_probing_("alr_probing", false), |
| 89 | trust_vp8_( |
| 90 | "trust_vp8", |
| 91 | IsEnabled(key_value_config, kVp8TrustedRateControllerFieldTrialName)), |
| 92 | trust_vp9_("trust_vp9", |
| 93 | IsEnabled(key_value_config, |
| 94 | kVp9TrustedRateControllerFieldTrialName)) { |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 95 | ParseFieldTrial({&congestion_window_, &congestion_window_pushback_, |
Erik Språng | 4b4266f | 2019-01-23 12:48:13 +0100 | [diff] [blame] | 96 | &pacing_factor_, &alr_probing_, &trust_vp8_, &trust_vp9_}, |
Erik Språng | 7121564 | 2019-01-21 16:30:55 +0100 | [diff] [blame] | 97 | key_value_config->Lookup("WebRTC-VideoRateControl")); |
| 98 | } |
| 99 | |
| 100 | RateControlSettings::~RateControlSettings() = default; |
| 101 | RateControlSettings::RateControlSettings(RateControlSettings&&) = default; |
| 102 | |
| 103 | RateControlSettings RateControlSettings::ParseFromFieldTrials() { |
| 104 | FieldTrialBasedConfig field_trial_config; |
| 105 | return RateControlSettings(&field_trial_config); |
| 106 | } |
| 107 | |
| 108 | RateControlSettings RateControlSettings::ParseFromKeyValueConfig( |
| 109 | const WebRtcKeyValueConfig* const key_value_config) { |
| 110 | FieldTrialBasedConfig field_trial_config; |
| 111 | return RateControlSettings(key_value_config ? key_value_config |
| 112 | : &field_trial_config); |
| 113 | } |
| 114 | |
| 115 | bool RateControlSettings::UseCongestionWindow() const { |
| 116 | return congestion_window_; |
| 117 | } |
| 118 | |
| 119 | int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const { |
| 120 | return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs); |
| 121 | } |
| 122 | |
| 123 | bool RateControlSettings::UseCongestionWindowPushback() const { |
| 124 | return congestion_window_ && congestion_window_pushback_; |
| 125 | } |
| 126 | |
| 127 | uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps() |
| 128 | const { |
| 129 | return congestion_window_pushback_.GetOptional().value_or( |
| 130 | kDefaultMinPushbackTargetBitrateBps); |
| 131 | } |
| 132 | |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 133 | absl::optional<double> RateControlSettings::GetPacingFactor() const { |
| 134 | return pacing_factor_.GetOptional(); |
| 135 | } |
| 136 | |
| 137 | bool RateControlSettings::UseAlrProbing() const { |
| 138 | return alr_probing_.Get(); |
| 139 | } |
| 140 | |
Erik Språng | 4b4266f | 2019-01-23 12:48:13 +0100 | [diff] [blame] | 141 | bool RateControlSettings::LibvpxVp8TrustedRateController() const { |
| 142 | return trust_vp8_.Get(); |
| 143 | } |
| 144 | |
| 145 | bool RateControlSettings::LibvpxVp9TrustedRateController() const { |
| 146 | return trust_vp9_.Get(); |
| 147 | } |
| 148 | |
Erik Språng | 7121564 | 2019-01-21 16:30:55 +0100 | [diff] [blame] | 149 | } // namespace webrtc |