blob: 81089b83ffa20225e12fdbe54f6a079049a9dec7 [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(
76 key_value_config)) {
77 ParseFieldTrial({&congestion_window_, &congestion_window_pushback_},
78 key_value_config->Lookup("WebRTC-VideoRateControl"));
79}
80
81RateControlSettings::~RateControlSettings() = default;
82RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
83
84RateControlSettings RateControlSettings::ParseFromFieldTrials() {
85 FieldTrialBasedConfig field_trial_config;
86 return RateControlSettings(&field_trial_config);
87}
88
89RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
90 const WebRtcKeyValueConfig* const key_value_config) {
91 FieldTrialBasedConfig field_trial_config;
92 return RateControlSettings(key_value_config ? key_value_config
93 : &field_trial_config);
94}
95
96bool RateControlSettings::UseCongestionWindow() const {
97 return congestion_window_;
98}
99
100int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
101 return congestion_window_.GetOptional().value_or(kDefaultAcceptedQueueMs);
102}
103
104bool RateControlSettings::UseCongestionWindowPushback() const {
105 return congestion_window_ && congestion_window_pushback_;
106}
107
108uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
109 const {
110 return congestion_window_pushback_.GetOptional().value_or(
111 kDefaultMinPushbackTargetBitrateBps);
112}
113
114} // namespace webrtc