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 
GetFallbackMinBpsFromFieldTrial(VideoCodecType type)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 
GetExperimentalMinVideoBitrate(VideoCodecType type)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::BitsPerSec(*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_av1("av1_br");
78     webrtc::FieldTrialOptional<webrtc::DataRate> min_bitrate_h264("h264_br");
79 
80     webrtc::ParseFieldTrial(
81         {&enabled, &min_video_bitrate, &min_bitrate_vp8, &min_bitrate_vp9,
82          &min_bitrate_av1, &min_bitrate_h264},
83         webrtc::field_trial::FindFullName(kMinVideoBitrateExperiment));
84 
85     if (min_video_bitrate) {
86       if (min_bitrate_vp8 || min_bitrate_vp9 || min_bitrate_av1 ||
87           min_bitrate_h264) {
88         // "br" is mutually-exclusive with the other configuration possibilites.
89         RTC_LOG(LS_WARNING) << "Self-contradictory experiment config.";
90       }
91       return *min_video_bitrate;
92     }
93 
94     switch (type) {
95       case kVideoCodecVP8:
96         return min_bitrate_vp8.GetOptional();
97       case kVideoCodecVP9:
98         return min_bitrate_vp9.GetOptional();
99       case kVideoCodecAV1:
100         return min_bitrate_av1.GetOptional();
101       case kVideoCodecH264:
102         return min_bitrate_h264.GetOptional();
103       case kVideoCodecGeneric:
104       case kVideoCodecMultiplex:
105         return absl::nullopt;
106     }
107 
108     RTC_NOTREACHED();
109   }
110 
111   return absl::nullopt;
112 }
113 
114 }  // namespace webrtc
115