blob: f465933e547b0929ba3a8b44f166a6ed624530b4 [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)),
124 probe_max_allocation_("probe_max_allocation", true) {
125 ParseFieldTrial(
126 {&congestion_window_, &congestion_window_pushback_, &pacing_factor_,
127 &alr_probing_, &trust_vp8_, &trust_vp9_, &video_hysteresis_,
128 &screenshare_hysteresis_, &probe_max_allocation_},
129 key_value_config->Lookup("WebRTC-VideoRateControl"));
Erik Språng71215642019-01-21 16:30:55 +0100130}
131
132RateControlSettings::~RateControlSettings() = default;
133RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
134
135RateControlSettings RateControlSettings::ParseFromFieldTrials() {
136 FieldTrialBasedConfig field_trial_config;
137 return RateControlSettings(&field_trial_config);
138}
139
140RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
141 const WebRtcKeyValueConfig* const key_value_config) {
142 FieldTrialBasedConfig field_trial_config;
143 return RateControlSettings(key_value_config ? key_value_config
144 : &field_trial_config);
145}
146
147bool RateControlSettings::UseCongestionWindow() const {
148 return congestion_window_;
149}
150
151int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
152 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
153}
154
155bool RateControlSettings::UseCongestionWindowPushback() const {
156 return congestion_window_ && congestion_window_pushback_;
157}
158
159uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
160 const {
161 return congestion_window_pushback_.GetOptional().value_or(
162 kDefaultMinPushbackTargetBitrateBps);
163}
164
Erik Språngcd76eab2019-01-21 18:06:46 +0100165absl::optional<double> RateControlSettings::GetPacingFactor() const {
166 return pacing_factor_.GetOptional();
167}
168
169bool RateControlSettings::UseAlrProbing() const {
170 return alr_probing_.Get();
171}
172
Erik Språng4b4266f2019-01-23 12:48:13 +0100173bool RateControlSettings::LibvpxVp8TrustedRateController() const {
174 return trust_vp8_.Get();
175}
176
177bool RateControlSettings::LibvpxVp9TrustedRateController() const {
178 return trust_vp9_.Get();
179}
180
Erik Språng2c58ba12019-01-23 16:53:18 +0100181double RateControlSettings::GetSimulcastVideoHysteresisFactor() const {
182 return video_hysteresis_.Get();
183}
184
185double RateControlSettings::GetSimulcastScreenshareHysteresisFactor() const {
186 return screenshare_hysteresis_.Get();
187}
188
Erik Språng5118bbc2019-01-29 18:28:06 +0100189bool RateControlSettings::TriggerProbeOnMaxAllocatedBitrateChange() const {
190 return probe_max_allocation_.Get();
191}
192
Erik Språng71215642019-01-21 16:30:55 +0100193} // namespace webrtc