Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 1 | /* |
| 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 Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 13 | #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 | |
| 20 | namespace webrtc { |
| 21 | namespace { |
| 22 | constexpr char kFieldTrial[] = "WebRTC-Video-BalancedDegradationSettings"; |
| 23 | constexpr int kMinFps = 1; |
Åsa Persson | 0c38a86 | 2019-08-14 10:10:12 +0200 | [diff] [blame] | 24 | constexpr int kMaxFps = 100; // 100 means unlimited fps. |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 25 | |
| 26 | std::vector<BalancedDegradationSettings::Config> DefaultConfigs() { |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 27 | return {{320 * 240, 7, 0, {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, 0}}, |
| 29 | {640 * 480, 15, 0, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 32 | bool 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 Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 36 | return false; |
| 37 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 38 | if (config.GetQpLow().has_value() && config.GetQpHigh().has_value() && |
| 39 | config.GetQpLow().value() >= config.GetQpHigh().value()) { |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 40 | RTC_LOG(LS_WARNING) << "Invalid threshold value, low >= high threshold."; |
| 41 | return false; |
| 42 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 43 | 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 | |
| 51 | bool 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 Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 64 | return true; |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool 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 | } |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 78 | int last_kbps = configs[0].kbps; |
| 79 | for (size_t i = 1; i < configs.size(); ++i) { |
| 80 | if (configs[i].kbps > 0) { |
| 81 | if (configs[i].kbps < last_kbps) { |
| 82 | RTC_LOG(LS_WARNING) << "Invalid bitrate value provided."; |
| 83 | return false; |
| 84 | } |
| 85 | last_kbps = configs[i].kbps; |
| 86 | } |
| 87 | } |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 88 | for (size_t i = 1; i < configs.size(); ++i) { |
| 89 | if (configs[i].pixels < configs[i - 1].pixels || |
| 90 | configs[i].fps < configs[i - 1].fps) { |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 91 | RTC_LOG(LS_WARNING) << "Invalid fps/pixel value provided."; |
| 92 | return false; |
| 93 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 94 | if (!IsValid(configs[i].vp8, configs[i - 1].vp8) || |
| 95 | !IsValid(configs[i].vp9, configs[i - 1].vp9) || |
| 96 | !IsValid(configs[i].h264, configs[i - 1].h264) || |
| 97 | !IsValid(configs[i].generic, configs[i - 1].generic)) { |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | for (const auto& config : configs) { |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 102 | if (!IsValidConfig(config.vp8) || !IsValidConfig(config.vp9) || |
| 103 | !IsValidConfig(config.h264) || !IsValidConfig(config.generic)) { |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | std::vector<BalancedDegradationSettings::Config> GetValidOrDefault( |
| 111 | const std::vector<BalancedDegradationSettings::Config>& configs) { |
| 112 | if (IsValid(configs)) { |
| 113 | return configs; |
| 114 | } |
| 115 | return DefaultConfigs(); |
| 116 | } |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 117 | |
| 118 | absl::optional<VideoEncoder::QpThresholds> GetThresholds( |
| 119 | VideoCodecType type, |
| 120 | const BalancedDegradationSettings::Config& config) { |
| 121 | absl::optional<int> low; |
| 122 | absl::optional<int> high; |
| 123 | |
| 124 | switch (type) { |
| 125 | case kVideoCodecVP8: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 126 | low = config.vp8.GetQpLow(); |
| 127 | high = config.vp8.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 128 | break; |
| 129 | case kVideoCodecVP9: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 130 | low = config.vp9.GetQpLow(); |
| 131 | high = config.vp9.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 132 | break; |
| 133 | case kVideoCodecH264: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 134 | low = config.h264.GetQpLow(); |
| 135 | high = config.h264.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 136 | break; |
| 137 | case kVideoCodecGeneric: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 138 | low = config.generic.GetQpLow(); |
| 139 | high = config.generic.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (low && high) { |
| 146 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << *low << ", high: " << *high; |
| 147 | return absl::optional<VideoEncoder::QpThresholds>( |
| 148 | VideoEncoder::QpThresholds(*low, *high)); |
| 149 | } |
| 150 | return absl::nullopt; |
| 151 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 152 | |
| 153 | int GetFps(VideoCodecType type, |
| 154 | const absl::optional<BalancedDegradationSettings::Config>& config) { |
| 155 | if (!config.has_value()) { |
| 156 | return std::numeric_limits<int>::max(); |
| 157 | } |
| 158 | |
| 159 | absl::optional<int> fps; |
| 160 | switch (type) { |
| 161 | case kVideoCodecVP8: |
| 162 | fps = config->vp8.GetFps(); |
| 163 | break; |
| 164 | case kVideoCodecVP9: |
| 165 | fps = config->vp9.GetFps(); |
| 166 | break; |
| 167 | case kVideoCodecH264: |
| 168 | fps = config->h264.GetFps(); |
| 169 | break; |
| 170 | case kVideoCodecGeneric: |
| 171 | fps = config->generic.GetFps(); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | |
Åsa Persson | 0c38a86 | 2019-08-14 10:10:12 +0200 | [diff] [blame] | 177 | const int framerate = fps.value_or(config->fps); |
| 178 | |
| 179 | return (framerate == kMaxFps) ? std::numeric_limits<int>::max() : framerate; |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 180 | } |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 181 | } // namespace |
| 182 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 183 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpLow() |
| 184 | const { |
| 185 | return (qp_low > 0) ? absl::optional<int>(qp_low) : absl::nullopt; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 188 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpHigh() |
| 189 | const { |
| 190 | return (qp_high > 0) ? absl::optional<int>(qp_high) : absl::nullopt; |
| 191 | } |
| 192 | |
| 193 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetFps() |
| 194 | const { |
| 195 | return (fps > 0) ? absl::optional<int>(fps) : absl::nullopt; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 198 | BalancedDegradationSettings::Config::Config() = default; |
| 199 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 200 | BalancedDegradationSettings::Config::Config(int pixels, |
| 201 | int fps, |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 202 | int kbps, |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 203 | CodecTypeSpecific vp8, |
| 204 | CodecTypeSpecific vp9, |
| 205 | CodecTypeSpecific h264, |
| 206 | CodecTypeSpecific generic) |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 207 | : pixels(pixels), |
| 208 | fps(fps), |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 209 | kbps(kbps), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 210 | vp8(vp8), |
| 211 | vp9(vp9), |
| 212 | h264(h264), |
| 213 | generic(generic) {} |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 214 | |
| 215 | BalancedDegradationSettings::BalancedDegradationSettings() { |
| 216 | FieldTrialStructList<Config> configs( |
| 217 | {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 218 | FieldTrialStructMember("fps", [](Config* c) { return &c->fps; }), |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 219 | FieldTrialStructMember("kbps", [](Config* c) { return &c->kbps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 220 | FieldTrialStructMember("vp8_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 221 | [](Config* c) { return &c->vp8.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 222 | FieldTrialStructMember("vp8_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 223 | [](Config* c) { return &c->vp8.qp_high; }), |
| 224 | FieldTrialStructMember("vp8_fps", [](Config* c) { return &c->vp8.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 225 | FieldTrialStructMember("vp9_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 226 | [](Config* c) { return &c->vp9.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 227 | FieldTrialStructMember("vp9_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 228 | [](Config* c) { return &c->vp9.qp_high; }), |
| 229 | FieldTrialStructMember("vp9_fps", [](Config* c) { return &c->vp9.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 230 | FieldTrialStructMember("h264_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 231 | [](Config* c) { return &c->h264.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 232 | FieldTrialStructMember("h264_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 233 | [](Config* c) { return &c->h264.qp_high; }), |
| 234 | FieldTrialStructMember("h264_fps", |
| 235 | [](Config* c) { return &c->h264.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 236 | FieldTrialStructMember("generic_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 237 | [](Config* c) { return &c->generic.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 238 | FieldTrialStructMember("generic_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 239 | [](Config* c) { return &c->generic.qp_high; }), |
| 240 | FieldTrialStructMember("generic_fps", |
| 241 | [](Config* c) { return &c->generic.fps; })}, |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 242 | {}); |
| 243 | |
| 244 | ParseFieldTrial({&configs}, field_trial::FindFullName(kFieldTrial)); |
| 245 | |
| 246 | configs_ = GetValidOrDefault(configs.Get()); |
| 247 | RTC_DCHECK_GT(configs_.size(), 1); |
| 248 | } |
| 249 | |
| 250 | BalancedDegradationSettings::~BalancedDegradationSettings() {} |
| 251 | |
| 252 | std::vector<BalancedDegradationSettings::Config> |
| 253 | BalancedDegradationSettings::GetConfigs() const { |
| 254 | return configs_; |
| 255 | } |
| 256 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 257 | int BalancedDegradationSettings::MinFps(VideoCodecType type, int pixels) const { |
| 258 | return GetFps(type, GetMinFpsConfig(pixels)); |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 261 | absl::optional<BalancedDegradationSettings::Config> |
| 262 | BalancedDegradationSettings::GetMinFpsConfig(int pixels) const { |
| 263 | for (const auto& config : configs_) { |
| 264 | if (pixels <= config.pixels) |
| 265 | return config; |
| 266 | } |
| 267 | return absl::nullopt; |
| 268 | } |
| 269 | |
| 270 | int BalancedDegradationSettings::MaxFps(VideoCodecType type, int pixels) const { |
| 271 | return GetFps(type, GetMaxFpsConfig(pixels)); |
| 272 | } |
| 273 | |
| 274 | absl::optional<BalancedDegradationSettings::Config> |
| 275 | BalancedDegradationSettings::GetMaxFpsConfig(int pixels) const { |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 276 | for (size_t i = 0; i < configs_.size() - 1; ++i) { |
| 277 | if (pixels <= configs_[i].pixels) |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 278 | return configs_[i + 1]; |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 279 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 280 | return absl::nullopt; |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Åsa Persson | 1b247f1 | 2019-08-14 17:26:39 +0200 | [diff] [blame^] | 283 | absl::optional<int> BalancedDegradationSettings::NextHigherBitrateKbps( |
| 284 | int pixels) const { |
| 285 | for (size_t i = 0; i < configs_.size() - 1; ++i) { |
| 286 | if (pixels <= configs_[i].pixels) { |
| 287 | return (configs_[i + 1].kbps > 0) |
| 288 | ? absl::optional<int>(configs_[i + 1].kbps) |
| 289 | : absl::nullopt; |
| 290 | } |
| 291 | } |
| 292 | return absl::nullopt; |
| 293 | } |
| 294 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 295 | absl::optional<VideoEncoder::QpThresholds> |
| 296 | BalancedDegradationSettings::GetQpThresholds(VideoCodecType type, |
| 297 | int pixels) const { |
| 298 | return GetThresholds(type, GetConfig(pixels)); |
| 299 | } |
| 300 | |
| 301 | BalancedDegradationSettings::Config BalancedDegradationSettings::GetConfig( |
| 302 | int pixels) const { |
| 303 | for (const auto& config : configs_) { |
| 304 | if (pixels <= config.pixels) |
| 305 | return config; |
| 306 | } |
| 307 | return configs_.back(); // Use last above highest pixels. |
| 308 | } |
| 309 | |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 310 | } // namespace webrtc |