blob: ce300ef8fa906d4397e6a60f68a4956202ed794f [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"
Evan Shrubsoleae68ea02019-04-09 09:59:00 +020019#include "rtc_base/experiments/field_trial_parser.h"
Erik Språng71215642019-01-21 16:30:55 +010020#include "rtc_base/logging.h"
21#include "rtc_base/numerics/safe_conversions.h"
22
23namespace webrtc {
24
25namespace {
26
Erik Språng71215642019-01-21 16:30:55 +010027const int kDefaultAcceptedQueueMs = 250;
28
Erik Språng71215642019-01-21 16:30:55 +010029const int kDefaultMinPushbackTargetBitrateBps = 30000;
30
Erik Språng4b4266f2019-01-23 12:48:13 +010031const char kVp8TrustedRateControllerFieldTrialName[] =
32 "WebRTC-LibvpxVp8TrustedRateController";
33const char kVp9TrustedRateControllerFieldTrialName[] =
34 "WebRTC-LibvpxVp9TrustedRateController";
35
Erik Språng2c58ba12019-01-23 16:53:18 +010036const char* kVideoHysteresisFieldTrialname =
37 "WebRTC-SimulcastUpswitchHysteresisPercent";
38const double kDefaultVideoHysteresisFactor = 1.0;
39const char* kScreenshareHysteresisFieldTrialname =
40 "WebRTC-SimulcastScreenshareUpswitchHysteresisPercent";
41// Default to 35% hysteresis for simulcast screenshare.
42const double kDefaultScreenshareHysteresisFactor = 1.35;
43
Erik Språng4b4266f2019-01-23 12:48:13 +010044bool IsEnabled(const WebRtcKeyValueConfig* const key_value_config,
45 absl::string_view key) {
46 return key_value_config->Lookup(key).find("Enabled") == 0;
47}
48
Erik Språng2c58ba12019-01-23 16:53:18 +010049double ParseHysteresisFactor(const WebRtcKeyValueConfig* const key_value_config,
50 absl::string_view key,
51 double default_value) {
52 std::string group_name = key_value_config->Lookup(key);
53 int percent = 0;
54 if (!group_name.empty() && sscanf(group_name.c_str(), "%d", &percent) == 1 &&
55 percent >= 0) {
56 return 1.0 + (percent / 100.0);
57 }
58 return default_value;
59}
60
Erik Språng71215642019-01-21 16:30:55 +010061} // namespace
62
63RateControlSettings::RateControlSettings(
64 const WebRtcKeyValueConfig* const key_value_config)
Evan Shrubsoleae68ea02019-04-09 09:59:00 +020065 : congestion_window_("QueueSize"),
66 congestion_window_pushback_("MinBitrate"),
Erik Språngcd76eab2019-01-21 18:06:46 +010067 pacing_factor_("pacing_factor"),
Erik Språng4b4266f2019-01-23 12:48:13 +010068 alr_probing_("alr_probing", false),
69 trust_vp8_(
70 "trust_vp8",
71 IsEnabled(key_value_config, kVp8TrustedRateControllerFieldTrialName)),
Erik Språng2c58ba12019-01-23 16:53:18 +010072 trust_vp9_(
73 "trust_vp9",
74 IsEnabled(key_value_config, kVp9TrustedRateControllerFieldTrialName)),
75 video_hysteresis_("video_hysteresis",
76 ParseHysteresisFactor(key_value_config,
77 kVideoHysteresisFieldTrialname,
78 kDefaultVideoHysteresisFactor)),
79 screenshare_hysteresis_(
80 "screenshare_hysteresis",
81 ParseHysteresisFactor(key_value_config,
82 kScreenshareHysteresisFieldTrialname,
Erik Språng5118bbc2019-01-29 18:28:06 +010083 kDefaultScreenshareHysteresisFactor)),
Erik Språng7ca375c2019-02-06 16:20:17 +010084 probe_max_allocation_("probe_max_allocation", true),
Erik Språng7f24fb92019-02-13 10:49:37 +010085 bitrate_adjuster_("bitrate_adjuster", false),
Erik Språng3d11e2f2019-04-15 14:48:30 +020086 adjuster_use_headroom_("adjuster_use_headroom", false),
Erik Språng7a3fe892019-04-15 12:22:55 +020087 vp8_s0_boost_("vp8_s0_boost", true),
88 vp8_dynamic_rate_("vp8_dynamic_rate", false),
89 vp9_dynamic_rate_("vp9_dynamic_rate", false) {
Evan Shrubsoleae68ea02019-04-09 09:59:00 +020090 ParseFieldTrial({&congestion_window_, &congestion_window_pushback_},
91 key_value_config->Lookup("WebRTC-CongestionWindow"));
Erik Språng3d11e2f2019-04-15 14:48:30 +020092 ParseFieldTrial(
93 {&pacing_factor_, &alr_probing_, &trust_vp8_, &trust_vp9_,
94 &video_hysteresis_, &screenshare_hysteresis_, &probe_max_allocation_,
95 &bitrate_adjuster_, &adjuster_use_headroom_, &vp8_s0_boost_,
96 &vp8_dynamic_rate_, &vp9_dynamic_rate_},
97 key_value_config->Lookup("WebRTC-VideoRateControl"));
Erik Språng71215642019-01-21 16:30:55 +010098}
99
100RateControlSettings::~RateControlSettings() = default;
101RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
102
103RateControlSettings RateControlSettings::ParseFromFieldTrials() {
104 FieldTrialBasedConfig field_trial_config;
105 return RateControlSettings(&field_trial_config);
106}
107
108RateControlSettings 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
115bool RateControlSettings::UseCongestionWindow() const {
Jonas Olssoncb968092019-03-12 13:57:15 +0100116 return static_cast<bool>(congestion_window_);
Erik Språng71215642019-01-21 16:30:55 +0100117}
118
119int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
120 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
121}
122
123bool RateControlSettings::UseCongestionWindowPushback() const {
124 return congestion_window_ && congestion_window_pushback_;
125}
126
127uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
128 const {
129 return congestion_window_pushback_.GetOptional().value_or(
130 kDefaultMinPushbackTargetBitrateBps);
131}
132
Erik Språngcd76eab2019-01-21 18:06:46 +0100133absl::optional<double> RateControlSettings::GetPacingFactor() const {
134 return pacing_factor_.GetOptional();
135}
136
137bool RateControlSettings::UseAlrProbing() const {
138 return alr_probing_.Get();
139}
140
Erik Språng4b4266f2019-01-23 12:48:13 +0100141bool RateControlSettings::LibvpxVp8TrustedRateController() const {
142 return trust_vp8_.Get();
143}
144
Erik Språng7f24fb92019-02-13 10:49:37 +0100145bool RateControlSettings::Vp8BoostBaseLayerQuality() const {
146 return vp8_s0_boost_.Get();
147}
148
Erik Språng7a3fe892019-04-15 12:22:55 +0200149bool RateControlSettings::Vp8DynamicRateSettings() const {
150 return vp8_dynamic_rate_.Get();
151}
152
Erik Språng4b4266f2019-01-23 12:48:13 +0100153bool RateControlSettings::LibvpxVp9TrustedRateController() const {
154 return trust_vp9_.Get();
155}
156
Erik Språng7a3fe892019-04-15 12:22:55 +0200157bool RateControlSettings::Vp9DynamicRateSettings() const {
158 return vp9_dynamic_rate_.Get();
159}
160
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100161double RateControlSettings::GetSimulcastHysteresisFactor(
162 VideoCodecMode mode) const {
163 if (mode == VideoCodecMode::kScreensharing) {
164 return GetSimulcastScreenshareHysteresisFactor();
165 }
166 return GetSimulcastVideoHysteresisFactor();
167}
168
169double RateControlSettings::GetSimulcastHysteresisFactor(
170 VideoEncoderConfig::ContentType content_type) const {
171 if (content_type == VideoEncoderConfig::ContentType::kScreen) {
172 return GetSimulcastScreenshareHysteresisFactor();
173 }
174 return GetSimulcastVideoHysteresisFactor();
175}
176
Erik Språng2c58ba12019-01-23 16:53:18 +0100177double RateControlSettings::GetSimulcastVideoHysteresisFactor() const {
178 return video_hysteresis_.Get();
179}
180
181double RateControlSettings::GetSimulcastScreenshareHysteresisFactor() const {
182 return screenshare_hysteresis_.Get();
183}
184
Erik Språng5118bbc2019-01-29 18:28:06 +0100185bool RateControlSettings::TriggerProbeOnMaxAllocatedBitrateChange() const {
186 return probe_max_allocation_.Get();
187}
188
Erik Språng7ca375c2019-02-06 16:20:17 +0100189bool RateControlSettings::UseEncoderBitrateAdjuster() const {
190 return bitrate_adjuster_.Get();
191}
192
Erik Språng3d11e2f2019-04-15 14:48:30 +0200193bool RateControlSettings::BitrateAdjusterCanUseNetworkHeadroom() const {
194 return adjuster_use_headroom_.Get();
195}
196
Erik Språng71215642019-01-21 16:30:55 +0100197} // namespace webrtc