blob: 0f19b788e08369179f3c09ad7272452e2e9f760f [file] [log] [blame]
Erik Språng71215642019-01-21 16:30:55 +01001/*
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
22namespace webrtc {
23
24namespace {
25
26const char* kCongestionWindowFieldTrialName = "WebRTC-CwndExperiment";
27const int kDefaultAcceptedQueueMs = 250;
28
29const char* kCongestionWindowPushbackFieldTrialName =
30 "WebRTC-CongestionWindowPushback";
31const int kDefaultMinPushbackTargetBitrateBps = 30000;
32
Erik Språng4b4266f2019-01-23 12:48:13 +010033const char kVp8TrustedRateControllerFieldTrialName[] =
34 "WebRTC-LibvpxVp8TrustedRateController";
35const char kVp9TrustedRateControllerFieldTrialName[] =
36 "WebRTC-LibvpxVp9TrustedRateController";
37
Erik Språng2c58ba12019-01-23 16:53:18 +010038const char* kVideoHysteresisFieldTrialname =
39 "WebRTC-SimulcastUpswitchHysteresisPercent";
40const double kDefaultVideoHysteresisFactor = 1.0;
41const char* kScreenshareHysteresisFieldTrialname =
42 "WebRTC-SimulcastScreenshareUpswitchHysteresisPercent";
43// Default to 35% hysteresis for simulcast screenshare.
44const double kDefaultScreenshareHysteresisFactor = 1.35;
45
Erik Språng71215642019-01-21 16:30:55 +010046absl::optional<int> MaybeReadCwndExperimentParameter(
47 const WebRtcKeyValueConfig* const key_value_config) {
48 int64_t accepted_queue_ms;
49 std::string experiment_string =
50 key_value_config->Lookup(kCongestionWindowFieldTrialName);
51 int parsed_values =
52 sscanf(experiment_string.c_str(), "Enabled-%" PRId64, &accepted_queue_ms);
53 if (parsed_values == 1) {
54 RTC_CHECK_GE(accepted_queue_ms, 0)
55 << "Accepted must be greater than or equal to 0.";
56 return rtc::checked_cast<int>(accepted_queue_ms);
57 } else if (experiment_string.find("Enabled") == 0) {
58 return kDefaultAcceptedQueueMs;
59 }
60 return absl::nullopt;
61}
62
63absl::optional<int> MaybeReadCongestionWindowPushbackExperimentParameter(
64 const WebRtcKeyValueConfig* const key_value_config) {
65 uint32_t min_pushback_target_bitrate_bps;
66 std::string experiment_string =
67 key_value_config->Lookup(kCongestionWindowPushbackFieldTrialName);
68 int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%" PRIu32,
69 &min_pushback_target_bitrate_bps);
70 if (parsed_values == 1) {
71 RTC_CHECK_GE(min_pushback_target_bitrate_bps, 0)
72 << "Min pushback target bitrate must be greater than or equal to 0.";
73 return rtc::checked_cast<int>(min_pushback_target_bitrate_bps);
74 } else if (experiment_string.find("Enabled") == 0) {
75 return kDefaultMinPushbackTargetBitrateBps;
76 }
77 return absl::nullopt;
78}
79
Erik Språng4b4266f2019-01-23 12:48:13 +010080bool IsEnabled(const WebRtcKeyValueConfig* const key_value_config,
81 absl::string_view key) {
82 return key_value_config->Lookup(key).find("Enabled") == 0;
83}
84
Erik Språng2c58ba12019-01-23 16:53:18 +010085double ParseHysteresisFactor(const WebRtcKeyValueConfig* const key_value_config,
86 absl::string_view key,
87 double default_value) {
88 std::string group_name = key_value_config->Lookup(key);
89 int percent = 0;
90 if (!group_name.empty() && sscanf(group_name.c_str(), "%d", &percent) == 1 &&
91 percent >= 0) {
92 return 1.0 + (percent / 100.0);
93 }
94 return default_value;
95}
96
Erik Språng71215642019-01-21 16:30:55 +010097} // namespace
98
99RateControlSettings::RateControlSettings(
100 const WebRtcKeyValueConfig* const key_value_config)
101 : congestion_window_("cwnd",
102 MaybeReadCwndExperimentParameter(key_value_config)),
103 congestion_window_pushback_(
104 "cwnd_pushback",
105 MaybeReadCongestionWindowPushbackExperimentParameter(
Erik Språngcd76eab2019-01-21 18:06:46 +0100106 key_value_config)),
107 pacing_factor_("pacing_factor"),
Erik Språng4b4266f2019-01-23 12:48:13 +0100108 alr_probing_("alr_probing", false),
109 trust_vp8_(
110 "trust_vp8",
111 IsEnabled(key_value_config, kVp8TrustedRateControllerFieldTrialName)),
Erik Språng2c58ba12019-01-23 16:53:18 +0100112 trust_vp9_(
113 "trust_vp9",
114 IsEnabled(key_value_config, kVp9TrustedRateControllerFieldTrialName)),
115 video_hysteresis_("video_hysteresis",
116 ParseHysteresisFactor(key_value_config,
117 kVideoHysteresisFieldTrialname,
118 kDefaultVideoHysteresisFactor)),
119 screenshare_hysteresis_(
120 "screenshare_hysteresis",
121 ParseHysteresisFactor(key_value_config,
122 kScreenshareHysteresisFieldTrialname,
Erik Språng5118bbc2019-01-29 18:28:06 +0100123 kDefaultScreenshareHysteresisFactor)),
Erik Språng7ca375c2019-02-06 16:20:17 +0100124 probe_max_allocation_("probe_max_allocation", true),
125 bitrate_adjuster_("bitrate_adjuster", false) {
Erik Språng5118bbc2019-01-29 18:28:06 +0100126 ParseFieldTrial(
127 {&congestion_window_, &congestion_window_pushback_, &pacing_factor_,
128 &alr_probing_, &trust_vp8_, &trust_vp9_, &video_hysteresis_,
Erik Språng7ca375c2019-02-06 16:20:17 +0100129 &screenshare_hysteresis_, &probe_max_allocation_, &bitrate_adjuster_},
Erik Språng5118bbc2019-01-29 18:28:06 +0100130 key_value_config->Lookup("WebRTC-VideoRateControl"));
Erik Språng71215642019-01-21 16:30:55 +0100131}
132
133RateControlSettings::~RateControlSettings() = default;
134RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
135
136RateControlSettings RateControlSettings::ParseFromFieldTrials() {
137 FieldTrialBasedConfig field_trial_config;
138 return RateControlSettings(&field_trial_config);
139}
140
141RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
142 const WebRtcKeyValueConfig* const key_value_config) {
143 FieldTrialBasedConfig field_trial_config;
144 return RateControlSettings(key_value_config ? key_value_config
145 : &field_trial_config);
146}
147
148bool RateControlSettings::UseCongestionWindow() const {
149 return congestion_window_;
150}
151
152int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
153 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
154}
155
156bool RateControlSettings::UseCongestionWindowPushback() const {
157 return congestion_window_ && congestion_window_pushback_;
158}
159
160uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
161 const {
162 return congestion_window_pushback_.GetOptional().value_or(
163 kDefaultMinPushbackTargetBitrateBps);
164}
165
Erik Språngcd76eab2019-01-21 18:06:46 +0100166absl::optional<double> RateControlSettings::GetPacingFactor() const {
167 return pacing_factor_.GetOptional();
168}
169
170bool RateControlSettings::UseAlrProbing() const {
171 return alr_probing_.Get();
172}
173
Erik Språng4b4266f2019-01-23 12:48:13 +0100174bool RateControlSettings::LibvpxVp8TrustedRateController() const {
175 return trust_vp8_.Get();
176}
177
178bool RateControlSettings::LibvpxVp9TrustedRateController() const {
179 return trust_vp9_.Get();
180}
181
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100182double RateControlSettings::GetSimulcastHysteresisFactor(
183 VideoCodecMode mode) const {
184 if (mode == VideoCodecMode::kScreensharing) {
185 return GetSimulcastScreenshareHysteresisFactor();
186 }
187 return GetSimulcastVideoHysteresisFactor();
188}
189
190double RateControlSettings::GetSimulcastHysteresisFactor(
191 VideoEncoderConfig::ContentType content_type) const {
192 if (content_type == VideoEncoderConfig::ContentType::kScreen) {
193 return GetSimulcastScreenshareHysteresisFactor();
194 }
195 return GetSimulcastVideoHysteresisFactor();
196}
197
Erik Språng2c58ba12019-01-23 16:53:18 +0100198double RateControlSettings::GetSimulcastVideoHysteresisFactor() const {
199 return video_hysteresis_.Get();
200}
201
202double RateControlSettings::GetSimulcastScreenshareHysteresisFactor() const {
203 return screenshare_hysteresis_.Get();
204}
205
Erik Språng5118bbc2019-01-29 18:28:06 +0100206bool RateControlSettings::TriggerProbeOnMaxAllocatedBitrateChange() const {
207 return probe_max_allocation_.Get();
208}
209
Erik Språng7ca375c2019-02-06 16:20:17 +0100210bool RateControlSettings::UseEncoderBitrateAdjuster() const {
211 return bitrate_adjuster_.Get();
212}
213
Erik Språng71215642019-01-21 16:30:55 +0100214} // namespace webrtc