blob: 317eb90640d9a24a7e5acddd605d86e4d1ac749c [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ång71215642019-01-21 16:30:55 +010038absl::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
55absl::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ång4b4266f2019-01-23 12:48:13 +010072bool 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ång71215642019-01-21 16:30:55 +010077} // namespace
78
79RateControlSettings::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ångcd76eab2019-01-21 18:06:46 +010086 key_value_config)),
87 pacing_factor_("pacing_factor"),
Erik Språng4b4266f2019-01-23 12:48:13 +010088 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ångcd76eab2019-01-21 18:06:46 +010095 ParseFieldTrial({&congestion_window_, &congestion_window_pushback_,
Erik Språng4b4266f2019-01-23 12:48:13 +010096 &pacing_factor_, &alr_probing_, &trust_vp8_, &trust_vp9_},
Erik Språng71215642019-01-21 16:30:55 +010097 key_value_config->Lookup("WebRTC-VideoRateControl"));
98}
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 {
116 return congestion_window_;
117}
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
145bool RateControlSettings::LibvpxVp9TrustedRateController() const {
146 return trust_vp9_.Get();
147}
148
Erik Språng71215642019-01-21 16:30:55 +0100149} // namespace webrtc