blob: 018ef1a9ff4d06480f2b058ea940b9ccd74c9c71 [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
33absl::optional<int> MaybeReadCwndExperimentParameter(
34 const WebRtcKeyValueConfig* const key_value_config) {
35 int64_t accepted_queue_ms;
36 std::string experiment_string =
37 key_value_config->Lookup(kCongestionWindowFieldTrialName);
38 int parsed_values =
39 sscanf(experiment_string.c_str(), "Enabled-%" PRId64, &accepted_queue_ms);
40 if (parsed_values == 1) {
41 RTC_CHECK_GE(accepted_queue_ms, 0)
42 << "Accepted must be greater than or equal to 0.";
43 return rtc::checked_cast<int>(accepted_queue_ms);
44 } else if (experiment_string.find("Enabled") == 0) {
45 return kDefaultAcceptedQueueMs;
46 }
47 return absl::nullopt;
48}
49
50absl::optional<int> MaybeReadCongestionWindowPushbackExperimentParameter(
51 const WebRtcKeyValueConfig* const key_value_config) {
52 uint32_t min_pushback_target_bitrate_bps;
53 std::string experiment_string =
54 key_value_config->Lookup(kCongestionWindowPushbackFieldTrialName);
55 int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%" PRIu32,
56 &min_pushback_target_bitrate_bps);
57 if (parsed_values == 1) {
58 RTC_CHECK_GE(min_pushback_target_bitrate_bps, 0)
59 << "Min pushback target bitrate must be greater than or equal to 0.";
60 return rtc::checked_cast<int>(min_pushback_target_bitrate_bps);
61 } else if (experiment_string.find("Enabled") == 0) {
62 return kDefaultMinPushbackTargetBitrateBps;
63 }
64 return absl::nullopt;
65}
66
67} // namespace
68
69RateControlSettings::RateControlSettings(
70 const WebRtcKeyValueConfig* const key_value_config)
71 : congestion_window_("cwnd",
72 MaybeReadCwndExperimentParameter(key_value_config)),
73 congestion_window_pushback_(
74 "cwnd_pushback",
75 MaybeReadCongestionWindowPushbackExperimentParameter(
Erik Språngcd76eab2019-01-21 18:06:46 +010076 key_value_config)),
77 pacing_factor_("pacing_factor"),
78 alr_probing_("alr_probing", false) {
79 ParseFieldTrial({&congestion_window_, &congestion_window_pushback_,
80 &pacing_factor_, &alr_probing_},
Erik Språng71215642019-01-21 16:30:55 +010081 key_value_config->Lookup("WebRTC-VideoRateControl"));
82}
83
84RateControlSettings::~RateControlSettings() = default;
85RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
86
87RateControlSettings RateControlSettings::ParseFromFieldTrials() {
88 FieldTrialBasedConfig field_trial_config;
89 return RateControlSettings(&field_trial_config);
90}
91
92RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
93 const WebRtcKeyValueConfig* const key_value_config) {
94 FieldTrialBasedConfig field_trial_config;
95 return RateControlSettings(key_value_config ? key_value_config
96 : &field_trial_config);
97}
98
99bool RateControlSettings::UseCongestionWindow() const {
100 return congestion_window_;
101}
102
103int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
104 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
105}
106
107bool RateControlSettings::UseCongestionWindowPushback() const {
108 return congestion_window_ && congestion_window_pushback_;
109}
110
111uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
112 const {
113 return congestion_window_pushback_.GetOptional().value_or(
114 kDefaultMinPushbackTargetBitrateBps);
115}
116
Erik Språngcd76eab2019-01-21 18:06:46 +0100117absl::optional<double> RateControlSettings::GetPacingFactor() const {
118 return pacing_factor_.GetOptional();
119}
120
121bool RateControlSettings::UseAlrProbing() const {
122 return alr_probing_.Get();
123}
124
Erik Språng71215642019-01-21 16:30:55 +0100125} // namespace webrtc