xref: /aosp_15_r20/external/webrtc/rtc_base/experiments/balanced_degradation_settings.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #ifndef RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_
12 #define RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_
13 
14 #include <vector>
15 
16 #include "absl/types/optional.h"
17 #include "api/field_trials_view.h"
18 #include "api/video_codecs/video_encoder.h"
19 
20 namespace webrtc {
21 
22 class BalancedDegradationSettings {
23  public:
24   static constexpr int kNoFpsDiff = -100;
25 
26   BalancedDegradationSettings(const FieldTrialsView& field_trials);
27   ~BalancedDegradationSettings();
28 
29   struct CodecTypeSpecific {
CodecTypeSpecificCodecTypeSpecific30     CodecTypeSpecific() {}
CodecTypeSpecificCodecTypeSpecific31     CodecTypeSpecific(int qp_low, int qp_high, int fps, int kbps, int kbps_res)
32         : qp_low(qp_low),
33           qp_high(qp_high),
34           fps(fps),
35           kbps(kbps),
36           kbps_res(kbps_res) {}
37 
38     bool operator==(const CodecTypeSpecific& o) const {
39       return qp_low == o.qp_low && qp_high == o.qp_high && fps == o.fps &&
40              kbps == o.kbps && kbps_res == o.kbps_res;
41     }
42 
43     absl::optional<int> GetQpLow() const;
44     absl::optional<int> GetQpHigh() const;
45     absl::optional<int> GetFps() const;
46     absl::optional<int> GetKbps() const;
47     absl::optional<int> GetKbpsRes() const;
48 
49     // Optional settings.
50     int qp_low = 0;
51     int qp_high = 0;
52     int fps = 0;       // If unset, defaults to `fps` in Config.
53     int kbps = 0;      // If unset, defaults to `kbps` in Config.
54     int kbps_res = 0;  // If unset, defaults to `kbps_res` in Config.
55   };
56 
57   struct Config {
58     Config();
59     Config(int pixels,
60            int fps,
61            int kbps,
62            int kbps_res,
63            int fps_diff,
64            CodecTypeSpecific vp8,
65            CodecTypeSpecific vp9,
66            CodecTypeSpecific h264,
67            CodecTypeSpecific av1,
68            CodecTypeSpecific generic);
69 
70     bool operator==(const Config& o) const {
71       return pixels == o.pixels && fps == o.fps && kbps == o.kbps &&
72              kbps_res == o.kbps_res && fps_diff == o.fps_diff && vp8 == o.vp8 &&
73              vp9 == o.vp9 && h264 == o.h264 && av1 == o.av1 &&
74              generic == o.generic;
75     }
76 
77     // Example:
78     // WebRTC-Video-BalancedDegradationSettings/pixels:100|200|300,fps:5|15|25/
79     // pixels <= 100 -> min framerate: 5 fps
80     // pixels <= 200 -> min framerate: 15 fps
81     // pixels <= 300 -> min framerate: 25 fps
82     //
83     // WebRTC-Video-BalancedDegradationSettings/pixels:100|200|300,
84     // fps:5|15|25,       // Min framerate.
85     // kbps:0|60|70,      // Min bitrate needed to adapt up.
86     // kbps_res:0|65|75/  // Min bitrate needed to adapt up in resolution.
87     //
88     // pixels: fps:  kbps:     kbps_res:
89     // 300     30    -         -
90     // 300     25    70 kbps   75 kbps
91     // 200     25    70 kbps   -
92     // 200     15    60 kbps   65 kbps
93     // 100     15    60 kbps   -
94     // 100      5
95     //               optional  optional
96 
97     int pixels = 0;  // Video frame size.
98     // If the frame size is less than or equal to `pixels`:
99     int fps = 0;   // Min framerate to be used.
100     int kbps = 0;  // Min bitrate needed to adapt up (resolution/fps).
101     int kbps_res = 0;           // Min bitrate needed to adapt up in resolution.
102     int fps_diff = kNoFpsDiff;  // Min fps reduction needed (input fps - `fps`)
103                                 // w/o triggering a new subsequent downgrade
104                                 // check.
105     CodecTypeSpecific vp8;
106     CodecTypeSpecific vp9;
107     CodecTypeSpecific h264;
108     CodecTypeSpecific av1;
109     CodecTypeSpecific generic;
110   };
111 
112   // Returns configurations from field trial on success (default on failure).
113   std::vector<Config> GetConfigs() const;
114 
115   // Gets the min/max framerate from `configs_` based on `pixels`.
116   int MinFps(VideoCodecType type, int pixels) const;
117   int MaxFps(VideoCodecType type, int pixels) const;
118 
119   // Checks if quality can be increased based on `pixels` and `bitrate_bps`.
120   bool CanAdaptUp(VideoCodecType type, int pixels, uint32_t bitrate_bps) const;
121   bool CanAdaptUpResolution(VideoCodecType type,
122                             int pixels,
123                             uint32_t bitrate_bps) const;
124 
125   // Gets the min framerate diff from `configs_` based on `pixels`.
126   absl::optional<int> MinFpsDiff(int pixels) const;
127 
128   // Gets QpThresholds for the codec `type` based on `pixels`.
129   absl::optional<VideoEncoder::QpThresholds> GetQpThresholds(
130       VideoCodecType type,
131       int pixels) const;
132 
133  private:
134   absl::optional<Config> GetMinFpsConfig(int pixels) const;
135   absl::optional<Config> GetMaxFpsConfig(int pixels) const;
136   Config GetConfig(int pixels) const;
137 
138   std::vector<Config> configs_;
139 };
140 
141 }  // namespace webrtc
142 
143 #endif  // RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_
144