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