blob: b181432e887057805253b0ab796acac369573c49 [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ång7a3fe892019-04-15 12:22:55 +020086 vp8_s0_boost_("vp8_s0_boost", true),
87 vp8_dynamic_rate_("vp8_dynamic_rate", false),
88 vp9_dynamic_rate_("vp9_dynamic_rate", false) {
Evan Shrubsoleae68ea02019-04-09 09:59:00 +020089 ParseFieldTrial({&congestion_window_, &congestion_window_pushback_},
90 key_value_config->Lookup("WebRTC-CongestionWindow"));
91 ParseFieldTrial({&pacing_factor_, &alr_probing_, &trust_vp8_, &trust_vp9_,
Erik Språng7f24fb92019-02-13 10:49:37 +010092 &video_hysteresis_, &screenshare_hysteresis_,
Erik Språng7a3fe892019-04-15 12:22:55 +020093 &probe_max_allocation_, &bitrate_adjuster_, &vp8_s0_boost_,
94 &vp8_dynamic_rate_, &vp9_dynamic_rate_},
Erik Språng7f24fb92019-02-13 10:49:37 +010095 key_value_config->Lookup("WebRTC-VideoRateControl"));
Erik Språng71215642019-01-21 16:30:55 +010096}
97
98RateControlSettings::~RateControlSettings() = default;
99RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
100
101RateControlSettings RateControlSettings::ParseFromFieldTrials() {
102 FieldTrialBasedConfig field_trial_config;
103 return RateControlSettings(&field_trial_config);
104}
105
106RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
107 const WebRtcKeyValueConfig* const key_value_config) {
108 FieldTrialBasedConfig field_trial_config;
109 return RateControlSettings(key_value_config ? key_value_config
110 : &field_trial_config);
111}
112
113bool RateControlSettings::UseCongestionWindow() const {
Jonas Olssoncb968092019-03-12 13:57:15 +0100114 return static_cast<bool>(congestion_window_);
Erik Språng71215642019-01-21 16:30:55 +0100115}
116
117int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
118 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
119}
120
121bool RateControlSettings::UseCongestionWindowPushback() const {
122 return congestion_window_ && congestion_window_pushback_;
123}
124
125uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
126 const {
127 return congestion_window_pushback_.GetOptional().value_or(
128 kDefaultMinPushbackTargetBitrateBps);
129}
130
Erik Språngcd76eab2019-01-21 18:06:46 +0100131absl::optional<double> RateControlSettings::GetPacingFactor() const {
132 return pacing_factor_.GetOptional();
133}
134
135bool RateControlSettings::UseAlrProbing() const {
136 return alr_probing_.Get();
137}
138
Erik Språng4b4266f2019-01-23 12:48:13 +0100139bool RateControlSettings::LibvpxVp8TrustedRateController() const {
140 return trust_vp8_.Get();
141}
142
Erik Språng7f24fb92019-02-13 10:49:37 +0100143bool RateControlSettings::Vp8BoostBaseLayerQuality() const {
144 return vp8_s0_boost_.Get();
145}
146
Erik Språng7a3fe892019-04-15 12:22:55 +0200147bool RateControlSettings::Vp8DynamicRateSettings() const {
148 return vp8_dynamic_rate_.Get();
149}
150
Erik Språng4b4266f2019-01-23 12:48:13 +0100151bool RateControlSettings::LibvpxVp9TrustedRateController() const {
152 return trust_vp9_.Get();
153}
154
Erik Språng7a3fe892019-04-15 12:22:55 +0200155bool RateControlSettings::Vp9DynamicRateSettings() const {
156 return vp9_dynamic_rate_.Get();
157}
158
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100159double RateControlSettings::GetSimulcastHysteresisFactor(
160 VideoCodecMode mode) const {
161 if (mode == VideoCodecMode::kScreensharing) {
162 return GetSimulcastScreenshareHysteresisFactor();
163 }
164 return GetSimulcastVideoHysteresisFactor();
165}
166
167double RateControlSettings::GetSimulcastHysteresisFactor(
168 VideoEncoderConfig::ContentType content_type) const {
169 if (content_type == VideoEncoderConfig::ContentType::kScreen) {
170 return GetSimulcastScreenshareHysteresisFactor();
171 }
172 return GetSimulcastVideoHysteresisFactor();
173}
174
Erik Språng2c58ba12019-01-23 16:53:18 +0100175double RateControlSettings::GetSimulcastVideoHysteresisFactor() const {
176 return video_hysteresis_.Get();
177}
178
179double RateControlSettings::GetSimulcastScreenshareHysteresisFactor() const {
180 return screenshare_hysteresis_.Get();
181}
182
Erik Språng5118bbc2019-01-29 18:28:06 +0100183bool RateControlSettings::TriggerProbeOnMaxAllocatedBitrateChange() const {
184 return probe_max_allocation_.Get();
185}
186
Erik Språng7ca375c2019-02-06 16:20:17 +0100187bool RateControlSettings::UseEncoderBitrateAdjuster() const {
188 return bitrate_adjuster_.Get();
189}
190
Erik Språng71215642019-01-21 16:30:55 +0100191} // namespace webrtc