blob: ea5f90ab393f11683cb08458134193bf4462e461 [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
Mirko Bonadei06d35592020-04-01 13:43:08 +020018#include "absl/strings/match.h"
Erik Språng71215642019-01-21 16:30:55 +010019#include "api/transport/field_trial_based_config.h"
20#include "rtc_base/logging.h"
21#include "rtc_base/numerics/safe_conversions.h"
22
23namespace webrtc {
24
25namespace {
26
Ying Wang45930472021-02-05 11:07:00 +010027const int kDefaultAcceptedQueueMs = 350;
Erik Språng71215642019-01-21 16:30:55 +010028
Erik Språng71215642019-01-21 16:30:55 +010029const int kDefaultMinPushbackTargetBitrateBps = 30000;
30
Ying Wang45930472021-02-05 11:07:00 +010031const char kCongestionWindowDefaultFieldTrialString[] =
32 "QueueSize:350,MinBitrate:30000,DropFrame:true";
33
Rasmus Brandt2b9317a2019-10-30 13:01:46 +010034const char kUseBaseHeavyVp8Tl3RateAllocationFieldTrialName[] =
35 "WebRTC-UseBaseHeavyVP8TL3RateAllocation";
36
Jonas Orelande62c2f22022-03-29 11:04:48 +020037bool IsEnabled(const FieldTrialsView* const key_value_config,
Erik Språng4b4266f2019-01-23 12:48:13 +010038 absl::string_view key) {
Mirko Bonadei06d35592020-04-01 13:43:08 +020039 return absl::StartsWith(key_value_config->Lookup(key), "Enabled");
Erik Språng4b4266f2019-01-23 12:48:13 +010040}
41
Erik Språng71215642019-01-21 16:30:55 +010042} // namespace
43
Sebastian Jansson0ee80082019-08-14 13:16:26 +020044constexpr char CongestionWindowConfig::kKey[];
45
46std::unique_ptr<StructParametersParser> CongestionWindowConfig::Parser() {
47 return StructParametersParser::Create("QueueSize", &queue_size_ms, //
Christoffer Rodbro034f7672019-12-06 13:13:40 +010048 "MinBitrate", &min_bitrate_bps,
Ying Wang9b881ab2020-02-07 14:29:32 +010049 "InitWin", &initial_data_window,
50 "DropFrame", &drop_frame_only);
Sebastian Jansson0ee80082019-08-14 13:16:26 +020051}
52
53// static
54CongestionWindowConfig CongestionWindowConfig::Parse(absl::string_view config) {
55 CongestionWindowConfig res;
56 res.Parser()->Parse(config);
57 return res;
58}
59
60constexpr char VideoRateControlConfig::kKey[];
61
62std::unique_ptr<StructParametersParser> VideoRateControlConfig::Parser() {
63 // The empty comments ensures that each pair is on a separate line.
64 return StructParametersParser::Create(
Erik Språng0c1a9342022-07-27 16:49:25 +020065 "pacing_factor", &pacing_factor, //
66 "alr_probing", &alr_probing, //
67 "vp8_qp_max", &vp8_qp_max, //
68 "vp8_min_pixels", &vp8_min_pixels, //
69 "trust_vp8", &trust_vp8, //
70 "trust_vp9", &trust_vp9, //
71 "probe_max_allocation", &probe_max_allocation, //
72 "bitrate_adjuster", &bitrate_adjuster, //
73 "adjuster_use_headroom", &adjuster_use_headroom, //
74 "vp8_s0_boost", &vp8_s0_boost, //
Erik Språng9d69cbe2020-10-22 17:44:42 +020075 "vp8_base_heavy_tl3_alloc", &vp8_base_heavy_tl3_alloc);
Sebastian Jansson0ee80082019-08-14 13:16:26 +020076}
77
Erik Språng71215642019-01-21 16:30:55 +010078RateControlSettings::RateControlSettings(
Jonas Orelande62c2f22022-03-29 11:04:48 +020079 const FieldTrialsView* const key_value_config) {
Ying Wang45930472021-02-05 11:07:00 +010080 std::string congestion_window_config =
81 key_value_config->Lookup(CongestionWindowConfig::kKey).empty()
82 ? kCongestionWindowDefaultFieldTrialString
83 : key_value_config->Lookup(CongestionWindowConfig::kKey);
84 congestion_window_config_ =
85 CongestionWindowConfig::Parse(congestion_window_config);
Rasmus Brandt2b9317a2019-10-30 13:01:46 +010086 video_config_.vp8_base_heavy_tl3_alloc = IsEnabled(
87 key_value_config, kUseBaseHeavyVp8Tl3RateAllocationFieldTrialName);
Sebastian Jansson0ee80082019-08-14 13:16:26 +020088 video_config_.Parser()->Parse(
89 key_value_config->Lookup(VideoRateControlConfig::kKey));
Erik Språng71215642019-01-21 16:30:55 +010090}
91
92RateControlSettings::~RateControlSettings() = default;
93RateControlSettings::RateControlSettings(RateControlSettings&&) = default;
94
95RateControlSettings RateControlSettings::ParseFromFieldTrials() {
96 FieldTrialBasedConfig field_trial_config;
97 return RateControlSettings(&field_trial_config);
98}
99
100RateControlSettings RateControlSettings::ParseFromKeyValueConfig(
Jonas Orelande62c2f22022-03-29 11:04:48 +0200101 const FieldTrialsView* const key_value_config) {
Erik Språng71215642019-01-21 16:30:55 +0100102 FieldTrialBasedConfig field_trial_config;
103 return RateControlSettings(key_value_config ? key_value_config
104 : &field_trial_config);
105}
106
107bool RateControlSettings::UseCongestionWindow() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200108 return static_cast<bool>(congestion_window_config_.queue_size_ms);
Erik Språng71215642019-01-21 16:30:55 +0100109}
110
111int64_t RateControlSettings::GetCongestionWindowAdditionalTimeMs() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200112 return congestion_window_config_.queue_size_ms.value_or(
113 kDefaultAcceptedQueueMs);
Erik Språng71215642019-01-21 16:30:55 +0100114}
115
116bool RateControlSettings::UseCongestionWindowPushback() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200117 return congestion_window_config_.queue_size_ms &&
118 congestion_window_config_.min_bitrate_bps;
Erik Språng71215642019-01-21 16:30:55 +0100119}
120
Ying Wang9b881ab2020-02-07 14:29:32 +0100121bool RateControlSettings::UseCongestionWindowDropFrameOnly() const {
122 return congestion_window_config_.drop_frame_only;
123}
124
Erik Språng71215642019-01-21 16:30:55 +0100125uint32_t RateControlSettings::CongestionWindowMinPushbackTargetBitrateBps()
126 const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200127 return congestion_window_config_.min_bitrate_bps.value_or(
Erik Språng71215642019-01-21 16:30:55 +0100128 kDefaultMinPushbackTargetBitrateBps);
129}
130
Christoffer Rodbro034f7672019-12-06 13:13:40 +0100131absl::optional<DataSize>
132RateControlSettings::CongestionWindowInitialDataWindow() const {
133 return congestion_window_config_.initial_data_window;
134}
135
Erik Språngcd76eab2019-01-21 18:06:46 +0100136absl::optional<double> RateControlSettings::GetPacingFactor() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200137 return video_config_.pacing_factor;
Erik Språngcd76eab2019-01-21 18:06:46 +0100138}
139
140bool RateControlSettings::UseAlrProbing() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200141 return video_config_.alr_probing;
Erik Språngcd76eab2019-01-21 18:06:46 +0100142}
143
Åsa Perssond7dd49f2019-05-08 14:44:12 +0200144absl::optional<int> RateControlSettings::LibvpxVp8QpMax() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200145 if (video_config_.vp8_qp_max &&
146 (*video_config_.vp8_qp_max < 0 || *video_config_.vp8_qp_max > 63)) {
Åsa Perssond7dd49f2019-05-08 14:44:12 +0200147 RTC_LOG(LS_WARNING) << "Unsupported vp8_qp_max_ value, ignored.";
148 return absl::nullopt;
149 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200150 return video_config_.vp8_qp_max;
Åsa Perssond7dd49f2019-05-08 14:44:12 +0200151}
152
Åsa Perssona0948492019-06-27 13:44:30 +0200153absl::optional<int> RateControlSettings::LibvpxVp8MinPixels() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200154 if (video_config_.vp8_min_pixels && *video_config_.vp8_min_pixels < 1) {
Åsa Perssona0948492019-06-27 13:44:30 +0200155 return absl::nullopt;
156 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200157 return video_config_.vp8_min_pixels;
Åsa Perssona0948492019-06-27 13:44:30 +0200158}
159
Erik Språng4b4266f2019-01-23 12:48:13 +0100160bool RateControlSettings::LibvpxVp8TrustedRateController() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200161 return video_config_.trust_vp8;
Erik Språng4b4266f2019-01-23 12:48:13 +0100162}
163
Erik Språng7f24fb92019-02-13 10:49:37 +0100164bool RateControlSettings::Vp8BoostBaseLayerQuality() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200165 return video_config_.vp8_s0_boost;
Erik Språng7f24fb92019-02-13 10:49:37 +0100166}
167
Erik Språng4b4266f2019-01-23 12:48:13 +0100168bool RateControlSettings::LibvpxVp9TrustedRateController() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200169 return video_config_.trust_vp9;
Erik Språng4b4266f2019-01-23 12:48:13 +0100170}
171
Rasmus Brandt2b9317a2019-10-30 13:01:46 +0100172bool RateControlSettings::Vp8BaseHeavyTl3RateAllocation() const {
173 return video_config_.vp8_base_heavy_tl3_alloc;
174}
175
Erik Språng5118bbc2019-01-29 18:28:06 +0100176bool RateControlSettings::TriggerProbeOnMaxAllocatedBitrateChange() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200177 return video_config_.probe_max_allocation;
Erik Språng5118bbc2019-01-29 18:28:06 +0100178}
179
Erik Språng7ca375c2019-02-06 16:20:17 +0100180bool RateControlSettings::UseEncoderBitrateAdjuster() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200181 return video_config_.bitrate_adjuster;
Erik Språng7ca375c2019-02-06 16:20:17 +0100182}
183
Erik Språng3d11e2f2019-04-15 14:48:30 +0200184bool RateControlSettings::BitrateAdjusterCanUseNetworkHeadroom() const {
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200185 return video_config_.adjuster_use_headroom;
Erik Språng3d11e2f2019-04-15 14:48:30 +0200186}
187
Erik Språng71215642019-01-21 16:30:55 +0100188} // namespace webrtc