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