Å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 | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 27 | 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 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 | } |
| 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 Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 81 | RTC_LOG(LS_WARNING) << "Invalid fps/pixel value provided."; |
| 82 | return false; |
| 83 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 84 | 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 Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | for (const auto& config : configs) { |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 92 | if (!IsValidConfig(config.vp8) || !IsValidConfig(config.vp9) || |
| 93 | !IsValidConfig(config.h264) || !IsValidConfig(config.generic)) { |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | std::vector<BalancedDegradationSettings::Config> GetValidOrDefault( |
| 101 | const std::vector<BalancedDegradationSettings::Config>& configs) { |
| 102 | if (IsValid(configs)) { |
| 103 | return configs; |
| 104 | } |
| 105 | return DefaultConfigs(); |
| 106 | } |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 107 | |
| 108 | absl::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 Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 116 | low = config.vp8.GetQpLow(); |
| 117 | high = config.vp8.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 118 | break; |
| 119 | case kVideoCodecVP9: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 120 | low = config.vp9.GetQpLow(); |
| 121 | high = config.vp9.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 122 | break; |
| 123 | case kVideoCodecH264: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 124 | low = config.h264.GetQpLow(); |
| 125 | high = config.h264.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 126 | break; |
| 127 | case kVideoCodecGeneric: |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 128 | low = config.generic.GetQpLow(); |
| 129 | high = config.generic.GetQpHigh(); |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 130 | 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 Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 142 | |
| 143 | int 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 Persson | 0c38a86 | 2019-08-14 10:10:12 +0200 | [diff] [blame^] | 167 | const int framerate = fps.value_or(config->fps); |
| 168 | |
| 169 | return (framerate == kMaxFps) ? std::numeric_limits<int>::max() : framerate; |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 170 | } |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 171 | } // namespace |
| 172 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 173 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpLow() |
| 174 | const { |
| 175 | return (qp_low > 0) ? absl::optional<int>(qp_low) : absl::nullopt; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 178 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetQpHigh() |
| 179 | const { |
| 180 | return (qp_high > 0) ? absl::optional<int>(qp_high) : absl::nullopt; |
| 181 | } |
| 182 | |
| 183 | absl::optional<int> BalancedDegradationSettings::CodecTypeSpecific::GetFps() |
| 184 | const { |
| 185 | return (fps > 0) ? absl::optional<int>(fps) : absl::nullopt; |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 188 | BalancedDegradationSettings::Config::Config() = default; |
| 189 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 190 | BalancedDegradationSettings::Config::Config(int pixels, |
| 191 | int fps, |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 192 | CodecTypeSpecific vp8, |
| 193 | CodecTypeSpecific vp9, |
| 194 | CodecTypeSpecific h264, |
| 195 | CodecTypeSpecific generic) |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 196 | : pixels(pixels), |
| 197 | fps(fps), |
| 198 | vp8(vp8), |
| 199 | vp9(vp9), |
| 200 | h264(h264), |
| 201 | generic(generic) {} |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 202 | |
| 203 | BalancedDegradationSettings::BalancedDegradationSettings() { |
| 204 | FieldTrialStructList<Config> configs( |
| 205 | {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 206 | FieldTrialStructMember("fps", [](Config* c) { return &c->fps; }), |
| 207 | FieldTrialStructMember("vp8_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 208 | [](Config* c) { return &c->vp8.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 209 | FieldTrialStructMember("vp8_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 210 | [](Config* c) { return &c->vp8.qp_high; }), |
| 211 | FieldTrialStructMember("vp8_fps", [](Config* c) { return &c->vp8.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 212 | FieldTrialStructMember("vp9_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 213 | [](Config* c) { return &c->vp9.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 214 | FieldTrialStructMember("vp9_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 215 | [](Config* c) { return &c->vp9.qp_high; }), |
| 216 | FieldTrialStructMember("vp9_fps", [](Config* c) { return &c->vp9.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 217 | FieldTrialStructMember("h264_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 218 | [](Config* c) { return &c->h264.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 219 | FieldTrialStructMember("h264_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 220 | [](Config* c) { return &c->h264.qp_high; }), |
| 221 | FieldTrialStructMember("h264_fps", |
| 222 | [](Config* c) { return &c->h264.fps; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 223 | FieldTrialStructMember("generic_qp_low", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 224 | [](Config* c) { return &c->generic.qp_low; }), |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 225 | FieldTrialStructMember("generic_qp_high", |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 226 | [](Config* c) { return &c->generic.qp_high; }), |
| 227 | FieldTrialStructMember("generic_fps", |
| 228 | [](Config* c) { return &c->generic.fps; })}, |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 229 | {}); |
| 230 | |
| 231 | ParseFieldTrial({&configs}, field_trial::FindFullName(kFieldTrial)); |
| 232 | |
| 233 | configs_ = GetValidOrDefault(configs.Get()); |
| 234 | RTC_DCHECK_GT(configs_.size(), 1); |
| 235 | } |
| 236 | |
| 237 | BalancedDegradationSettings::~BalancedDegradationSettings() {} |
| 238 | |
| 239 | std::vector<BalancedDegradationSettings::Config> |
| 240 | BalancedDegradationSettings::GetConfigs() const { |
| 241 | return configs_; |
| 242 | } |
| 243 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 244 | int BalancedDegradationSettings::MinFps(VideoCodecType type, int pixels) const { |
| 245 | return GetFps(type, GetMinFpsConfig(pixels)); |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 248 | absl::optional<BalancedDegradationSettings::Config> |
| 249 | BalancedDegradationSettings::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 | |
| 257 | int BalancedDegradationSettings::MaxFps(VideoCodecType type, int pixels) const { |
| 258 | return GetFps(type, GetMaxFpsConfig(pixels)); |
| 259 | } |
| 260 | |
| 261 | absl::optional<BalancedDegradationSettings::Config> |
| 262 | BalancedDegradationSettings::GetMaxFpsConfig(int pixels) const { |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 263 | for (size_t i = 0; i < configs_.size() - 1; ++i) { |
| 264 | if (pixels <= configs_[i].pixels) |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 265 | return configs_[i + 1]; |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 266 | } |
Åsa Persson | 48284b8 | 2019-07-08 10:01:12 +0200 | [diff] [blame] | 267 | return absl::nullopt; |
Åsa Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Åsa Persson | 1231419 | 2019-06-20 15:45:07 +0200 | [diff] [blame] | 270 | absl::optional<VideoEncoder::QpThresholds> |
| 271 | BalancedDegradationSettings::GetQpThresholds(VideoCodecType type, |
| 272 | int pixels) const { |
| 273 | return GetThresholds(type, GetConfig(pixels)); |
| 274 | } |
| 275 | |
| 276 | BalancedDegradationSettings::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 Persson | f3d828e | 2019-05-06 12:22:49 +0200 | [diff] [blame] | 285 | } // namespace webrtc |