Elad Alon | 80f53b7 | 2019-10-11 16:19:43 +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/min_video_bitrate_experiment.h" |
| 12 | |
| 13 | #include <string> |
| 14 | |
| 15 | #include "rtc_base/checks.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 | |
| 22 | const int kDefaultMinVideoBitrateBps = 30000; |
| 23 | |
| 24 | namespace { |
| 25 | const char kForcedFallbackFieldTrial[] = |
| 26 | "WebRTC-VP8-Forced-Fallback-Encoder-v2"; |
| 27 | const char kMinVideoBitrateExperiment[] = "WebRTC-Video-MinVideoBitrate"; |
| 28 | |
| 29 | absl::optional<int> GetFallbackMinBpsFromFieldTrial(VideoCodecType type) { |
| 30 | if (type != kVideoCodecVP8) { |
| 31 | return absl::nullopt; |
| 32 | } |
| 33 | |
| 34 | if (!webrtc::field_trial::IsEnabled(kForcedFallbackFieldTrial)) { |
| 35 | return absl::nullopt; |
| 36 | } |
| 37 | |
| 38 | const std::string group = |
| 39 | webrtc::field_trial::FindFullName(kForcedFallbackFieldTrial); |
| 40 | if (group.empty()) { |
| 41 | return absl::nullopt; |
| 42 | } |
| 43 | |
| 44 | int min_pixels; // Ignored. |
| 45 | int max_pixels; // Ignored. |
| 46 | int min_bps; |
| 47 | if (sscanf(group.c_str(), "Enabled-%d,%d,%d", &min_pixels, &max_pixels, |
| 48 | &min_bps) != 3) { |
| 49 | return absl::nullopt; |
| 50 | } |
| 51 | |
| 52 | if (min_bps <= 0) { |
| 53 | return absl::nullopt; |
| 54 | } |
| 55 | |
| 56 | return min_bps; |
| 57 | } |
| 58 | } // namespace |
| 59 | |
| 60 | absl::optional<DataRate> GetExperimentalMinVideoBitrate(VideoCodecType type) { |
| 61 | const absl::optional<int> fallback_min_bitrate_bps = |
| 62 | GetFallbackMinBpsFromFieldTrial(type); |
| 63 | if (fallback_min_bitrate_bps) { |
| 64 | return DataRate::bps(*fallback_min_bitrate_bps); |
| 65 | } |
| 66 | |
| 67 | if (webrtc::field_trial::IsEnabled(kMinVideoBitrateExperiment)) { |
| 68 | webrtc::FieldTrialFlag enabled("Enabled"); |
| 69 | |
| 70 | // Backwards-compatibility with an old experiment - a generic minimum which, |
| 71 | // if set, applies to all codecs. |
| 72 | webrtc::FieldTrialOptional<webrtc::DataRate> min_video_bitrate("br"); |
| 73 | |
| 74 | // New experiment - per-codec minimum bitrate. |
| 75 | webrtc::FieldTrialOptional<webrtc::DataRate> min_bitrate_vp8("vp8_br"); |
| 76 | webrtc::FieldTrialOptional<webrtc::DataRate> min_bitrate_vp9("vp9_br"); |
| 77 | webrtc::FieldTrialOptional<webrtc::DataRate> min_bitrate_h264("h264_br"); |
| 78 | |
| 79 | webrtc::ParseFieldTrial( |
| 80 | {&enabled, &min_video_bitrate, &min_bitrate_vp8, &min_bitrate_vp9, |
| 81 | &min_bitrate_h264}, |
| 82 | webrtc::field_trial::FindFullName(kMinVideoBitrateExperiment)); |
| 83 | |
| 84 | if (min_video_bitrate) { |
| 85 | if (min_bitrate_vp8 || min_bitrate_vp9 || min_bitrate_h264) { |
| 86 | // "br" is mutually-exclusive with the other configuration possibilites. |
| 87 | RTC_LOG(LS_WARNING) << "Self-contradictory experiment config."; |
| 88 | } |
| 89 | return *min_video_bitrate; |
| 90 | } |
| 91 | |
| 92 | switch (type) { |
| 93 | case kVideoCodecVP8: |
| 94 | return min_bitrate_vp8.GetOptional(); |
| 95 | case kVideoCodecVP9: |
| 96 | return min_bitrate_vp9.GetOptional(); |
| 97 | case kVideoCodecH264: |
| 98 | return min_bitrate_h264.GetOptional(); |
| 99 | case kVideoCodecGeneric: |
| 100 | case kVideoCodecMultiplex: |
| 101 | return absl::nullopt; |
| 102 | } |
| 103 | |
| 104 | RTC_NOTREACHED(); |
| 105 | } |
| 106 | |
| 107 | return absl::nullopt; |
| 108 | } |
| 109 | |
| 110 | } // namespace webrtc |