blob: e1ed1bc9452dde053406d817fc9ab4ba3473add3 [file] [log] [blame]
Åsa Perssonf3d828e2019-05-06 12:22:49 +02001/*
2 * Copyright 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/balanced_degradation_settings.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Åsa Perssonf3d828e2019-05-06 12:22:49 +020013#include <limits>
14
15#include "rtc_base/experiments/field_trial_list.h"
16#include "rtc_base/experiments/field_trial_parser.h"
17#include "rtc_base/logging.h"
18#include "system_wrappers/include/field_trial.h"
19
20namespace webrtc {
21namespace {
22constexpr char kFieldTrial[] = "WebRTC-Video-BalancedDegradationSettings";
23constexpr int kMinFps = 1;
Åsa Persson0c38a862019-08-14 10:10:12 +020024constexpr int kMaxFps = 100; // 100 means unlimited fps.
Åsa Perssonf3d828e2019-05-06 12:22:49 +020025
26std::vector<BalancedDegradationSettings::Config> DefaultConfigs() {
Åsa Persson48284b82019-07-08 10:01:12 +020027 return {{320 * 240, 7, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
28 {480 * 270, 10, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
29 {640 * 480, 15, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}};
Åsa Persson12314192019-06-20 15:45:07 +020030}
31
Åsa Persson48284b82019-07-08 10:01:12 +020032bool IsValidConfig(
33 const BalancedDegradationSettings::CodecTypeSpecific& config) {
34 if (config.GetQpLow().has_value() != config.GetQpHigh().has_value()) {
35 RTC_LOG(LS_WARNING) << "Neither or both thresholds should be set.";
Åsa Persson12314192019-06-20 15:45:07 +020036 return false;
37 }
Åsa Persson48284b82019-07-08 10:01:12 +020038 if (config.GetQpLow().has_value() && config.GetQpHigh().has_value() &&
39 config.GetQpLow().value() >= config.GetQpHigh().value()) {
Åsa Persson12314192019-06-20 15:45:07 +020040 RTC_LOG(LS_WARNING) << "Invalid threshold value, low >= high threshold.";
41 return false;
42 }
Åsa Persson48284b82019-07-08 10:01:12 +020043 if (config.GetFps().has_value() && (config.GetFps().value() < kMinFps ||
44 config.GetFps().value() > kMaxFps)) {
45 RTC_LOG(LS_WARNING) << "Unsupported fps setting, value ignored.";
46 return false;
47 }
48 return true;
49}
50
51bool IsValid(const BalancedDegradationSettings::CodecTypeSpecific& config1,
52 const BalancedDegradationSettings::CodecTypeSpecific& config2) {
53 bool both_or_none_set = ((config1.qp_low > 0) == (config2.qp_low > 0) &&
54 (config1.qp_high > 0) == (config2.qp_high > 0) &&
55 (config1.fps > 0) == (config2.fps > 0));
56 if (!both_or_none_set) {
57 RTC_LOG(LS_WARNING) << "Invalid value, all/none should be set.";
58 return false;
59 }
60 if (config1.fps > 0 && config1.fps < config2.fps) {
61 RTC_LOG(LS_WARNING) << "Invalid fps/pixel value provided.";
62 return false;
63 }
Åsa Persson12314192019-06-20 15:45:07 +020064 return true;
Åsa Perssonf3d828e2019-05-06 12:22:49 +020065}
66
67bool IsValid(const std::vector<BalancedDegradationSettings::Config>& configs) {
68 if (configs.size() <= 1) {
69 RTC_LOG(LS_WARNING) << "Unsupported size, value ignored.";
70 return false;
71 }
72 for (const auto& config : configs) {
73 if (config.fps < kMinFps || config.fps > kMaxFps) {
74 RTC_LOG(LS_WARNING) << "Unsupported fps setting, value ignored.";
75 return false;
76 }
77 }
78 for (size_t i = 1; i < configs.size(); ++i) {
79 if (configs[i].pixels < configs[i - 1].pixels ||
80 configs[i].fps < configs[i - 1].fps) {
Åsa Persson12314192019-06-20 15:45:07 +020081 RTC_LOG(LS_WARNING) << "Invalid fps/pixel value provided.";
82 return false;
83 }
Åsa Persson48284b82019-07-08 10:01:12 +020084 if (!IsValid(configs[i].vp8, configs[i - 1].vp8) ||
85 !IsValid(configs[i].vp9, configs[i - 1].vp9) ||
86 !IsValid(configs[i].h264, configs[i - 1].h264) ||
87 !IsValid(configs[i].generic, configs[i - 1].generic)) {
Åsa Persson12314192019-06-20 15:45:07 +020088 return false;
89 }
90 }
91 for (const auto& config : configs) {
Åsa Persson48284b82019-07-08 10:01:12 +020092 if (!IsValidConfig(config.vp8) || !IsValidConfig(config.vp9) ||
93 !IsValidConfig(config.h264) || !IsValidConfig(config.generic)) {
Åsa Perssonf3d828e2019-05-06 12:22:49 +020094 return false;
95 }
96 }
97 return true;
98}
99
100std::vector<BalancedDegradationSettings::Config> GetValidOrDefault(
101 const std::vector<BalancedDegradationSettings::Config>& configs) {
102 if (IsValid(configs)) {
103 return configs;
104 }
105 return DefaultConfigs();
106}
Åsa Persson12314192019-06-20 15:45:07 +0200107
108absl::optional<VideoEncoder::QpThresholds> GetThresholds(
109 VideoCodecType type,
110 const BalancedDegradationSettings::Config& config) {
111 absl::optional<int> low;
112 absl::optional<int> high;
113
114 switch (type) {
115 case kVideoCodecVP8:
Åsa Persson48284b82019-07-08 10:01:12 +0200116 low = config.vp8.GetQpLow();
117 high = config.vp8.GetQpHigh();
Åsa Persson12314192019-06-20 15:45:07 +0200118 break;
119 case kVideoCodecVP9:
Åsa Persson48284b82019-07-08 10:01:12 +0200120 low = config.vp9.GetQpLow();
121 high = config.vp9.GetQpHigh();
Åsa Persson12314192019-06-20 15:45:07 +0200122 break;
123 case kVideoCodecH264:
Åsa Persson48284b82019-07-08 10:01:12 +0200124 low = config.h264.GetQpLow();
125 high = config.h264.GetQpHigh();
Åsa Persson12314192019-06-20 15:45:07 +0200126 break;
127 case kVideoCodecGeneric:
Åsa Persson48284b82019-07-08 10:01:12 +0200128 low = config.generic.GetQpLow();
129 high = config.generic.GetQpHigh();
Åsa Persson12314192019-06-20 15:45:07 +0200130 break;
131 default:
132 break;
133 }
134
135 if (low && high) {
136 RTC_LOG(LS_INFO) << "QP thresholds: low: " << *low << ", high: " << *high;
137 return absl::optional<VideoEncoder::QpThresholds>(
138 VideoEncoder::QpThresholds(*low, *high));
139 }
140 return absl::nullopt;
141}
Åsa Persson48284b82019-07-08 10:01:12 +0200142
143int GetFps(VideoCodecType type,
144 const absl::optional<BalancedDegradationSettings::Config>& config) {
145 if (!config.has_value()) {
146 return std::numeric_limits<int>::max();
147 }
148
149 absl::optional<int> fps;
150 switch (type) {
151 case kVideoCodecVP8:
152 fps = config->vp8.GetFps();
153 break;
154 case kVideoCodecVP9:
155 fps = config->vp9.GetFps();
156 break;
157 case kVideoCodecH264:
158 fps = config->h264.GetFps();
159 break;
160 case kVideoCodecGeneric:
161 fps = config->generic.GetFps();
162 break;
163 default:
164 break;
165 }
166
Åsa Persson0c38a862019-08-14 10:10:12 +0200167 const int framerate = fps.value_or(config->fps);
168
169 return (framerate == kMaxFps) ? std::numeric_limits<int>::max() : framerate;
Åsa Persson48284b82019-07-08 10:01:12 +0200170}
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200171} // namespace
172
Åsa Persson48284b82019-07-08 10:01:12 +0200173absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpLow()
174 const {
175 return (qp_low > 0) ? absl::optional<int>(qp_low) : absl::nullopt;
Åsa Persson12314192019-06-20 15:45:07 +0200176}
177
Åsa Persson48284b82019-07-08 10:01:12 +0200178absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpHigh()
179 const {
180 return (qp_high > 0) ? absl::optional<int>(qp_high) : absl::nullopt;
181}
182
183absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetFps()
184 const {
185 return (fps > 0) ? absl::optional<int>(fps) : absl::nullopt;
Åsa Persson12314192019-06-20 15:45:07 +0200186}
187
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200188BalancedDegradationSettings::Config::Config() = default;
189
Åsa Persson12314192019-06-20 15:45:07 +0200190BalancedDegradationSettings::Config::Config(int pixels,
191 int fps,
Åsa Persson48284b82019-07-08 10:01:12 +0200192 CodecTypeSpecific vp8,
193 CodecTypeSpecific vp9,
194 CodecTypeSpecific h264,
195 CodecTypeSpecific generic)
Åsa Persson12314192019-06-20 15:45:07 +0200196 : pixels(pixels),
197 fps(fps),
198 vp8(vp8),
199 vp9(vp9),
200 h264(h264),
201 generic(generic) {}
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200202
203BalancedDegradationSettings::BalancedDegradationSettings() {
204 FieldTrialStructList<Config> configs(
205 {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }),
Åsa Persson12314192019-06-20 15:45:07 +0200206 FieldTrialStructMember("fps", [](Config* c) { return &c->fps; }),
207 FieldTrialStructMember("vp8_qp_low",
Åsa Persson48284b82019-07-08 10:01:12 +0200208 [](Config* c) { return &c->vp8.qp_low; }),
Åsa Persson12314192019-06-20 15:45:07 +0200209 FieldTrialStructMember("vp8_qp_high",
Åsa Persson48284b82019-07-08 10:01:12 +0200210 [](Config* c) { return &c->vp8.qp_high; }),
211 FieldTrialStructMember("vp8_fps", [](Config* c) { return &c->vp8.fps; }),
Åsa Persson12314192019-06-20 15:45:07 +0200212 FieldTrialStructMember("vp9_qp_low",
Åsa Persson48284b82019-07-08 10:01:12 +0200213 [](Config* c) { return &c->vp9.qp_low; }),
Åsa Persson12314192019-06-20 15:45:07 +0200214 FieldTrialStructMember("vp9_qp_high",
Åsa Persson48284b82019-07-08 10:01:12 +0200215 [](Config* c) { return &c->vp9.qp_high; }),
216 FieldTrialStructMember("vp9_fps", [](Config* c) { return &c->vp9.fps; }),
Åsa Persson12314192019-06-20 15:45:07 +0200217 FieldTrialStructMember("h264_qp_low",
Åsa Persson48284b82019-07-08 10:01:12 +0200218 [](Config* c) { return &c->h264.qp_low; }),
Åsa Persson12314192019-06-20 15:45:07 +0200219 FieldTrialStructMember("h264_qp_high",
Åsa Persson48284b82019-07-08 10:01:12 +0200220 [](Config* c) { return &c->h264.qp_high; }),
221 FieldTrialStructMember("h264_fps",
222 [](Config* c) { return &c->h264.fps; }),
Åsa Persson12314192019-06-20 15:45:07 +0200223 FieldTrialStructMember("generic_qp_low",
Åsa Persson48284b82019-07-08 10:01:12 +0200224 [](Config* c) { return &c->generic.qp_low; }),
Åsa Persson12314192019-06-20 15:45:07 +0200225 FieldTrialStructMember("generic_qp_high",
Åsa Persson48284b82019-07-08 10:01:12 +0200226 [](Config* c) { return &c->generic.qp_high; }),
227 FieldTrialStructMember("generic_fps",
228 [](Config* c) { return &c->generic.fps; })},
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200229 {});
230
231 ParseFieldTrial({&configs}, field_trial::FindFullName(kFieldTrial));
232
233 configs_ = GetValidOrDefault(configs.Get());
234 RTC_DCHECK_GT(configs_.size(), 1);
235}
236
237BalancedDegradationSettings::~BalancedDegradationSettings() {}
238
239std::vector<BalancedDegradationSettings::Config>
240BalancedDegradationSettings::GetConfigs() const {
241 return configs_;
242}
243
Åsa Persson48284b82019-07-08 10:01:12 +0200244int BalancedDegradationSettings::MinFps(VideoCodecType type, int pixels) const {
245 return GetFps(type, GetMinFpsConfig(pixels));
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200246}
247
Åsa Persson48284b82019-07-08 10:01:12 +0200248absl::optional<BalancedDegradationSettings::Config>
249BalancedDegradationSettings::GetMinFpsConfig(int pixels) const {
250 for (const auto& config : configs_) {
251 if (pixels <= config.pixels)
252 return config;
253 }
254 return absl::nullopt;
255}
256
257int BalancedDegradationSettings::MaxFps(VideoCodecType type, int pixels) const {
258 return GetFps(type, GetMaxFpsConfig(pixels));
259}
260
261absl::optional<BalancedDegradationSettings::Config>
262BalancedDegradationSettings::GetMaxFpsConfig(int pixels) const {
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200263 for (size_t i = 0; i < configs_.size() - 1; ++i) {
264 if (pixels <= configs_[i].pixels)
Åsa Persson48284b82019-07-08 10:01:12 +0200265 return configs_[i + 1];
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200266 }
Åsa Persson48284b82019-07-08 10:01:12 +0200267 return absl::nullopt;
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200268}
269
Åsa Persson12314192019-06-20 15:45:07 +0200270absl::optional<VideoEncoder::QpThresholds>
271BalancedDegradationSettings::GetQpThresholds(VideoCodecType type,
272 int pixels) const {
273 return GetThresholds(type, GetConfig(pixels));
274}
275
276BalancedDegradationSettings::Config BalancedDegradationSettings::GetConfig(
277 int pixels) const {
278 for (const auto& config : configs_) {
279 if (pixels <= config.pixels)
280 return config;
281 }
282 return configs_.back(); // Use last above highest pixels.
283}
284
Åsa Perssonf3d828e2019-05-06 12:22:49 +0200285} // namespace webrtc