Å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() { |
| 26 | return {{320 * 240, 7}, {480 * 270, 10}, {640 * 480, 15}}; |
| 27 | } |
| 28 | |
| 29 | bool IsValid(const std::vector<BalancedDegradationSettings::Config>& configs) { |
| 30 | if (configs.size() <= 1) { |
| 31 | RTC_LOG(LS_WARNING) << "Unsupported size, value ignored."; |
| 32 | return false; |
| 33 | } |
| 34 | for (const auto& config : configs) { |
| 35 | if (config.fps < kMinFps || config.fps > kMaxFps) { |
| 36 | RTC_LOG(LS_WARNING) << "Unsupported fps setting, value ignored."; |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | for (size_t i = 1; i < configs.size(); ++i) { |
| 41 | if (configs[i].pixels < configs[i - 1].pixels || |
| 42 | configs[i].fps < configs[i - 1].fps) { |
| 43 | RTC_LOG(LS_WARNING) << "Invalid parameter value provided."; |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | std::vector<BalancedDegradationSettings::Config> GetValidOrDefault( |
| 51 | const std::vector<BalancedDegradationSettings::Config>& configs) { |
| 52 | if (IsValid(configs)) { |
| 53 | return configs; |
| 54 | } |
| 55 | return DefaultConfigs(); |
| 56 | } |
| 57 | } // namespace |
| 58 | |
| 59 | BalancedDegradationSettings::Config::Config() = default; |
| 60 | |
| 61 | BalancedDegradationSettings::Config::Config(int pixels, int fps) |
| 62 | : pixels(pixels), fps(fps) {} |
| 63 | |
| 64 | BalancedDegradationSettings::BalancedDegradationSettings() { |
| 65 | FieldTrialStructList<Config> configs( |
| 66 | {FieldTrialStructMember("pixels", [](Config* c) { return &c->pixels; }), |
| 67 | FieldTrialStructMember("fps", [](Config* c) { return &c->fps; })}, |
| 68 | {}); |
| 69 | |
| 70 | ParseFieldTrial({&configs}, field_trial::FindFullName(kFieldTrial)); |
| 71 | |
| 72 | configs_ = GetValidOrDefault(configs.Get()); |
| 73 | RTC_DCHECK_GT(configs_.size(), 1); |
| 74 | } |
| 75 | |
| 76 | BalancedDegradationSettings::~BalancedDegradationSettings() {} |
| 77 | |
| 78 | std::vector<BalancedDegradationSettings::Config> |
| 79 | BalancedDegradationSettings::GetConfigs() const { |
| 80 | return configs_; |
| 81 | } |
| 82 | |
| 83 | int BalancedDegradationSettings::MinFps(int pixels) const { |
| 84 | for (const auto& config : configs_) { |
| 85 | if (pixels <= config.pixels) |
| 86 | return config.fps; |
| 87 | } |
| 88 | return std::numeric_limits<int>::max(); |
| 89 | } |
| 90 | |
| 91 | int BalancedDegradationSettings::MaxFps(int pixels) const { |
| 92 | for (size_t i = 0; i < configs_.size() - 1; ++i) { |
| 93 | if (pixels <= configs_[i].pixels) |
| 94 | return configs_[i + 1].fps; |
| 95 | } |
| 96 | return std::numeric_limits<int>::max(); |
| 97 | } |
| 98 | |
| 99 | } // namespace webrtc |