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 
13 #include <limits>
14 
15 #include "rtc_base/gunit.h"
16 #include "test/field_trial.h"
17 #include "test/gmock.h"
18 
19 namespace webrtc {
20 namespace {
21 
VerifyIsDefault(const std::vector<BalancedDegradationSettings::Config> & config)22 void VerifyIsDefault(
23     const std::vector<BalancedDegradationSettings::Config>& config) {
24   EXPECT_THAT(config, ::testing::ElementsAre(
25                           BalancedDegradationSettings::Config{
26                               320 * 240,
27                               7,
28                               0,
29                               0,
30                               BalancedDegradationSettings::kNoFpsDiff,
31                               {0, 0, 0, 0, 0},
32                               {0, 0, 0, 0, 0},
33                               {0, 0, 0, 0, 0},
34                               {0, 0, 0, 0, 0},
35                               {0, 0, 0, 0, 0}},
36                           BalancedDegradationSettings::Config{
37                               480 * 270,
38                               10,
39                               0,
40                               0,
41                               BalancedDegradationSettings::kNoFpsDiff,
42                               {0, 0, 0, 0, 0},
43                               {0, 0, 0, 0, 0},
44                               {0, 0, 0, 0, 0},
45                               {0, 0, 0, 0, 0},
46                               {0, 0, 0, 0, 0}},
47                           BalancedDegradationSettings::Config{
48                               640 * 480,
49                               15,
50                               0,
51                               0,
52                               BalancedDegradationSettings::kNoFpsDiff,
53                               {0, 0, 0, 0, 0},
54                               {0, 0, 0, 0, 0},
55                               {0, 0, 0, 0, 0},
56                               {0, 0, 0, 0, 0},
57                               {0, 0, 0, 0, 0}}));
58 }
59 }  // namespace
60 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfNoList)61 TEST(BalancedDegradationSettings, GetsDefaultConfigIfNoList) {
62   webrtc::test::ScopedFieldTrials field_trials("");
63   BalancedDegradationSettings settings;
64   VerifyIsDefault(settings.GetConfigs());
65   EXPECT_TRUE(settings.CanAdaptUp(kVideoCodecVP8, 1, /*bitrate_bps*/ 1));
66   EXPECT_TRUE(
67       settings.CanAdaptUpResolution(kVideoCodecVP8, 1, /*bitrate_bps*/ 1));
68   EXPECT_FALSE(settings.MinFpsDiff(1));
69   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecVP8, 1));
70   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecVP9, 1));
71   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecH264, 1));
72   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecAV1, 1));
73   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecGeneric, 1));
74   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecMultiplex, 1));
75 }
76 
TEST(BalancedDegradationSettings,GetsConfig)77 TEST(BalancedDegradationSettings, GetsConfig) {
78   webrtc::test::ScopedFieldTrials field_trials(
79       "WebRTC-Video-BalancedDegradationSettings/"
80       "pixels:11|22|33,fps:5|15|25,other:4|5|6/");
81   BalancedDegradationSettings settings;
82   EXPECT_THAT(settings.GetConfigs(),
83               ::testing::ElementsAre(
84                   BalancedDegradationSettings::Config{
85                       11,
86                       5,
87                       0,
88                       0,
89                       BalancedDegradationSettings::kNoFpsDiff,
90                       {0, 0, 0, 0, 0},
91                       {0, 0, 0, 0, 0},
92                       {0, 0, 0, 0, 0},
93                       {0, 0, 0, 0, 0},
94                       {0, 0, 0, 0, 0}},
95                   BalancedDegradationSettings::Config{
96                       22,
97                       15,
98                       0,
99                       0,
100                       BalancedDegradationSettings::kNoFpsDiff,
101                       {0, 0, 0, 0, 0},
102                       {0, 0, 0, 0, 0},
103                       {0, 0, 0, 0, 0},
104                       {0, 0, 0, 0, 0},
105                       {0, 0, 0, 0, 0}},
106                   BalancedDegradationSettings::Config{
107                       33,
108                       25,
109                       0,
110                       0,
111                       BalancedDegradationSettings::kNoFpsDiff,
112                       {0, 0, 0, 0, 0},
113                       {0, 0, 0, 0, 0},
114                       {0, 0, 0, 0, 0},
115                       {0, 0, 0, 0, 0},
116                       {0, 0, 0, 0, 0}}));
117 }
118 
TEST(BalancedDegradationSettings,GetsDefaultConfigForZeroFpsValue)119 TEST(BalancedDegradationSettings, GetsDefaultConfigForZeroFpsValue) {
120   webrtc::test::ScopedFieldTrials field_trials(
121       "WebRTC-Video-BalancedDegradationSettings/"
122       "pixels:1000|2000|3000,fps:0|15|25/");
123   BalancedDegradationSettings settings;
124   VerifyIsDefault(settings.GetConfigs());
125 }
126 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfPixelsDecreases)127 TEST(BalancedDegradationSettings, GetsDefaultConfigIfPixelsDecreases) {
128   webrtc::test::ScopedFieldTrials field_trials(
129       "WebRTC-Video-BalancedDegradationSettings/"
130       "pixels:1000|999|3000,fps:5|15|25/");
131   BalancedDegradationSettings settings;
132   VerifyIsDefault(settings.GetConfigs());
133 }
134 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfFramerateDecreases)135 TEST(BalancedDegradationSettings, GetsDefaultConfigIfFramerateDecreases) {
136   webrtc::test::ScopedFieldTrials field_trials(
137       "WebRTC-Video-BalancedDegradationSettings/"
138       "pixels:1000|2000|3000,fps:5|4|25/");
139   BalancedDegradationSettings settings;
140   VerifyIsDefault(settings.GetConfigs());
141 }
142 
TEST(BalancedDegradationSettings,GetsConfigWithSpecificFps)143 TEST(BalancedDegradationSettings, GetsConfigWithSpecificFps) {
144   webrtc::test::ScopedFieldTrials field_trials(
145       "WebRTC-Video-BalancedDegradationSettings/"
146       "pixels:1000|2000|3000,fps:5|15|25,vp8_fps:7|8|9,vp9_fps:9|10|11,"
147       "h264_fps:11|12|13,av1_fps:1|2|3,generic_fps:13|14|15/");
148   BalancedDegradationSettings settings;
149   EXPECT_THAT(settings.GetConfigs(),
150               ::testing::ElementsAre(
151                   BalancedDegradationSettings::Config{
152                       1000,
153                       5,
154                       0,
155                       0,
156                       BalancedDegradationSettings::kNoFpsDiff,
157                       {0, 0, 7, 0, 0},
158                       {0, 0, 9, 0, 0},
159                       {0, 0, 11, 0, 0},
160                       {0, 0, 1, 0, 0},
161                       {0, 0, 13, 0, 0}},
162                   BalancedDegradationSettings::Config{
163                       2000,
164                       15,
165                       0,
166                       0,
167                       BalancedDegradationSettings::kNoFpsDiff,
168                       {0, 0, 8, 0, 0},
169                       {0, 0, 10, 0, 0},
170                       {0, 0, 12, 0, 0},
171                       {0, 0, 2, 0, 0},
172                       {0, 0, 14, 0, 0}},
173                   BalancedDegradationSettings::Config{
174                       3000,
175                       25,
176                       0,
177                       0,
178                       BalancedDegradationSettings::kNoFpsDiff,
179                       {0, 0, 9, 0, 0},
180                       {0, 0, 11, 0, 0},
181                       {0, 0, 13, 0, 0},
182                       {0, 0, 3, 0, 0},
183                       {0, 0, 15, 0, 0}}));
184 }
185 
TEST(BalancedDegradationSettings,GetsDefaultConfigForZeroVp8FpsValue)186 TEST(BalancedDegradationSettings, GetsDefaultConfigForZeroVp8FpsValue) {
187   webrtc::test::ScopedFieldTrials field_trials(
188       "WebRTC-Video-BalancedDegradationSettings/"
189       "pixels:1000|2000|3000,fps:7|15|25,vp8_fps:0|15|25/");
190   BalancedDegradationSettings settings;
191   VerifyIsDefault(settings.GetConfigs());
192 }
193 
TEST(BalancedDegradationSettings,GetsDefaultConfigForInvalidFpsValue)194 TEST(BalancedDegradationSettings, GetsDefaultConfigForInvalidFpsValue) {
195   webrtc::test::ScopedFieldTrials field_trials(
196       "WebRTC-Video-BalancedDegradationSettings/"
197       "pixels:1000|2000|3000,fps:7|15|25,vp8_fps:10|15|2000/");
198   BalancedDegradationSettings settings;
199   VerifyIsDefault(settings.GetConfigs());
200 }
201 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfVp8FramerateDecreases)202 TEST(BalancedDegradationSettings, GetsDefaultConfigIfVp8FramerateDecreases) {
203   webrtc::test::ScopedFieldTrials field_trials(
204       "WebRTC-Video-BalancedDegradationSettings/"
205       "pixels:1000|2000|3000,fps:4|5|25,vp8_fps:5|4|25/");
206   BalancedDegradationSettings settings;
207   VerifyIsDefault(settings.GetConfigs());
208 }
209 
TEST(BalancedDegradationSettings,GetsMinFps)210 TEST(BalancedDegradationSettings, GetsMinFps) {
211   webrtc::test::ScopedFieldTrials field_trials(
212       "WebRTC-Video-BalancedDegradationSettings/"
213       "pixels:1000|2000|3000,fps:5|15|25/");
214   BalancedDegradationSettings settings;
215   EXPECT_EQ(5, settings.MinFps(kVideoCodecVP8, 1));
216   EXPECT_EQ(5, settings.MinFps(kVideoCodecVP8, 1000));
217   EXPECT_EQ(15, settings.MinFps(kVideoCodecVP8, 1001));
218   EXPECT_EQ(15, settings.MinFps(kVideoCodecVP8, 2000));
219   EXPECT_EQ(25, settings.MinFps(kVideoCodecVP8, 2001));
220   EXPECT_EQ(25, settings.MinFps(kVideoCodecVP8, 3000));
221   EXPECT_EQ(std::numeric_limits<int>::max(),
222             settings.MinFps(kVideoCodecVP8, 3001));
223 }
224 
TEST(BalancedDegradationSettings,GetsVp8MinFps)225 TEST(BalancedDegradationSettings, GetsVp8MinFps) {
226   webrtc::test::ScopedFieldTrials field_trials(
227       "WebRTC-Video-BalancedDegradationSettings/"
228       "pixels:1000|2000|3000,fps:5|15|25,vp8_fps:7|10|12/");
229   BalancedDegradationSettings settings;
230   EXPECT_EQ(7, settings.MinFps(kVideoCodecVP8, 1));
231   EXPECT_EQ(7, settings.MinFps(kVideoCodecVP8, 1000));
232   EXPECT_EQ(10, settings.MinFps(kVideoCodecVP8, 1001));
233   EXPECT_EQ(10, settings.MinFps(kVideoCodecVP8, 2000));
234   EXPECT_EQ(12, settings.MinFps(kVideoCodecVP8, 2001));
235   EXPECT_EQ(12, settings.MinFps(kVideoCodecVP8, 3000));
236   EXPECT_EQ(std::numeric_limits<int>::max(),
237             settings.MinFps(kVideoCodecVP8, 3001));
238 }
239 
TEST(BalancedDegradationSettings,GetsMaxFps)240 TEST(BalancedDegradationSettings, GetsMaxFps) {
241   webrtc::test::ScopedFieldTrials field_trials(
242       "WebRTC-Video-BalancedDegradationSettings/"
243       "pixels:1000|2000|3000,fps:5|15|25/");
244   BalancedDegradationSettings settings;
245   EXPECT_EQ(15, settings.MaxFps(kVideoCodecVP8, 1));
246   EXPECT_EQ(15, settings.MaxFps(kVideoCodecVP8, 1000));
247   EXPECT_EQ(25, settings.MaxFps(kVideoCodecVP8, 1001));
248   EXPECT_EQ(25, settings.MaxFps(kVideoCodecVP8, 2000));
249   EXPECT_EQ(std::numeric_limits<int>::max(),
250             settings.MaxFps(kVideoCodecVP8, 2001));
251 }
252 
TEST(BalancedDegradationSettings,GetsVp8MaxFps)253 TEST(BalancedDegradationSettings, GetsVp8MaxFps) {
254   webrtc::test::ScopedFieldTrials field_trials(
255       "WebRTC-Video-BalancedDegradationSettings/"
256       "pixels:1000|2000|3000,fps:5|15|25,vp8_fps:7|10|12/");
257   BalancedDegradationSettings settings;
258   EXPECT_EQ(10, settings.MaxFps(kVideoCodecVP8, 1));
259   EXPECT_EQ(10, settings.MaxFps(kVideoCodecVP8, 1000));
260   EXPECT_EQ(12, settings.MaxFps(kVideoCodecVP8, 1001));
261   EXPECT_EQ(12, settings.MaxFps(kVideoCodecVP8, 2000));
262   EXPECT_EQ(std::numeric_limits<int>::max(),
263             settings.MaxFps(kVideoCodecVP8, 2001));
264 }
265 
TEST(BalancedDegradationSettings,GetsVp9Fps)266 TEST(BalancedDegradationSettings, GetsVp9Fps) {
267   webrtc::test::ScopedFieldTrials field_trials(
268       "WebRTC-Video-BalancedDegradationSettings/"
269       "pixels:1000|2000|3000,fps:5|15|25,vp9_fps:7|10|12/");
270   BalancedDegradationSettings settings;
271   EXPECT_EQ(7, settings.MinFps(kVideoCodecVP9, 1000));
272   EXPECT_EQ(10, settings.MaxFps(kVideoCodecVP9, 1000));
273 }
274 
TEST(BalancedDegradationSettings,GetsH264Fps)275 TEST(BalancedDegradationSettings, GetsH264Fps) {
276   webrtc::test::ScopedFieldTrials field_trials(
277       "WebRTC-Video-BalancedDegradationSettings/"
278       "pixels:1000|2000|3000,fps:5|15|25,h264_fps:8|11|13/");
279   BalancedDegradationSettings settings;
280   EXPECT_EQ(11, settings.MinFps(kVideoCodecH264, 2000));
281   EXPECT_EQ(13, settings.MaxFps(kVideoCodecH264, 2000));
282 }
283 
TEST(BalancedDegradationSettings,GetsGenericFps)284 TEST(BalancedDegradationSettings, GetsGenericFps) {
285   webrtc::test::ScopedFieldTrials field_trials(
286       "WebRTC-Video-BalancedDegradationSettings/"
287       "pixels:1000|2000|3000,fps:5|15|25,generic_fps:9|12|14/");
288   BalancedDegradationSettings settings;
289   EXPECT_EQ(14, settings.MinFps(kVideoCodecGeneric, 3000));
290   EXPECT_EQ(std::numeric_limits<int>::max(),
291             settings.MaxFps(kVideoCodecGeneric, 3000));
292 }
293 
TEST(BalancedDegradationSettings,GetsUnlimitedForMaxValidFps)294 TEST(BalancedDegradationSettings, GetsUnlimitedForMaxValidFps) {
295   webrtc::test::ScopedFieldTrials field_trials(
296       "WebRTC-Video-BalancedDegradationSettings/"
297       "pixels:1000|2000|3000,fps:5|15|100,vp8_fps:30|100|100/");
298   const int kUnlimitedFps = std::numeric_limits<int>::max();
299   BalancedDegradationSettings settings;
300   EXPECT_EQ(15, settings.MinFps(kVideoCodecH264, 2000));
301   EXPECT_EQ(kUnlimitedFps, settings.MinFps(kVideoCodecH264, 2001));
302   EXPECT_EQ(30, settings.MinFps(kVideoCodecVP8, 1000));
303   EXPECT_EQ(kUnlimitedFps, settings.MinFps(kVideoCodecVP8, 1001));
304 }
305 
TEST(BalancedDegradationSettings,GetsConfigWithBitrate)306 TEST(BalancedDegradationSettings, GetsConfigWithBitrate) {
307   webrtc::test::ScopedFieldTrials field_trials(
308       "WebRTC-Video-BalancedDegradationSettings/"
309       "pixels:11|22|33,fps:5|15|25,kbps:44|88|99,kbps_res:55|111|222,"
310       "vp8_kbps:11|12|13,vp8_kbps_res:14|15|16,"
311       "vp9_kbps:21|22|23,vp9_kbps_res:24|25|26,"
312       "h264_kbps:31|32|33,h264_kbps_res:34|35|36,"
313       "av1_kbps:41|42|43,av1_kbps_res:44|45|46,"
314       "generic_kbps:51|52|53,generic_kbps_res:54|55|56/");
315   BalancedDegradationSettings settings;
316   EXPECT_THAT(settings.GetConfigs(),
317               ::testing::ElementsAre(
318                   BalancedDegradationSettings::Config{
319                       11,
320                       5,
321                       44,
322                       55,
323                       BalancedDegradationSettings::kNoFpsDiff,
324                       {0, 0, 0, 11, 14},
325                       {0, 0, 0, 21, 24},
326                       {0, 0, 0, 31, 34},
327                       {0, 0, 0, 41, 44},
328                       {0, 0, 0, 51, 54}},
329                   BalancedDegradationSettings::Config{
330                       22,
331                       15,
332                       88,
333                       111,
334                       BalancedDegradationSettings::kNoFpsDiff,
335                       {0, 0, 0, 12, 15},
336                       {0, 0, 0, 22, 25},
337                       {0, 0, 0, 32, 35},
338                       {0, 0, 0, 42, 45},
339                       {0, 0, 0, 52, 55}},
340                   BalancedDegradationSettings::Config{
341                       33,
342                       25,
343                       99,
344                       222,
345                       BalancedDegradationSettings::kNoFpsDiff,
346                       {0, 0, 0, 13, 16},
347                       {0, 0, 0, 23, 26},
348                       {0, 0, 0, 33, 36},
349                       {0, 0, 0, 43, 46},
350                       {0, 0, 0, 53, 56}}));
351 }
352 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfBitrateDecreases)353 TEST(BalancedDegradationSettings, GetsDefaultConfigIfBitrateDecreases) {
354   webrtc::test::ScopedFieldTrials field_trials(
355       "WebRTC-Video-BalancedDegradationSettings/"
356       "pixels:11|22|33,fps:5|15|25,kbps:44|43|99/");
357   BalancedDegradationSettings settings;
358   VerifyIsDefault(settings.GetConfigs());
359 }
360 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfBitrateDecreasesWithUnsetValue)361 TEST(BalancedDegradationSettings,
362      GetsDefaultConfigIfBitrateDecreasesWithUnsetValue) {
363   webrtc::test::ScopedFieldTrials field_trials(
364       "WebRTC-Video-BalancedDegradationSettings/"
365       "pixels:11|22|33,fps:5|15|25,kbps:44|0|43/");
366   BalancedDegradationSettings settings;
367   VerifyIsDefault(settings.GetConfigs());
368 }
369 
TEST(BalancedDegradationSettings,CanAdaptUp)370 TEST(BalancedDegradationSettings, CanAdaptUp) {
371   VideoCodecType vp8 = kVideoCodecVP8;
372   webrtc::test::ScopedFieldTrials field_trials(
373       "WebRTC-Video-BalancedDegradationSettings/"
374       "pixels:1000|2000|3000|4000,fps:5|15|25|30,kbps:0|80|0|90,"
375       "vp9_kbps:40|50|60|70/");
376   BalancedDegradationSettings s;
377   EXPECT_TRUE(s.CanAdaptUp(vp8, 1000, 0));  // No bitrate provided.
378   EXPECT_FALSE(s.CanAdaptUp(vp8, 1000, 79000));
379   EXPECT_TRUE(s.CanAdaptUp(vp8, 1000, 80000));
380   EXPECT_TRUE(s.CanAdaptUp(vp8, 1001, 1));  // No limit configured.
381   EXPECT_FALSE(s.CanAdaptUp(vp8, 3000, 89000));
382   EXPECT_TRUE(s.CanAdaptUp(vp8, 3000, 90000));
383   EXPECT_TRUE(s.CanAdaptUp(vp8, 3001, 1));  // No limit.
384 }
385 
TEST(BalancedDegradationSettings,CanAdaptUpWithCodecType)386 TEST(BalancedDegradationSettings, CanAdaptUpWithCodecType) {
387   webrtc::test::ScopedFieldTrials field_trials(
388       "WebRTC-Video-BalancedDegradationSettings/"
389       "pixels:1000|2000|3000|4000,fps:5|15|25|30,vp8_kbps:0|30|40|50,"
390       "vp9_kbps:0|60|70|80,h264_kbps:0|55|65|75,av1_kbps:0|77|88|99,"
391       "generic_kbps:0|25|35|45/");
392   BalancedDegradationSettings s;
393   EXPECT_FALSE(s.CanAdaptUp(kVideoCodecVP8, 1000, 29000));
394   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecVP8, 1000, 30000));
395   EXPECT_FALSE(s.CanAdaptUp(kVideoCodecVP9, 1000, 59000));
396   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecVP9, 1000, 60000));
397   EXPECT_FALSE(s.CanAdaptUp(kVideoCodecH264, 1000, 54000));
398   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecH264, 1000, 55000));
399   EXPECT_FALSE(s.CanAdaptUp(kVideoCodecAV1, 1000, 76000));
400   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecAV1, 1000, 77000));
401   EXPECT_FALSE(s.CanAdaptUp(kVideoCodecGeneric, 1000, 24000));
402   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecGeneric, 1000, 25000));
403   EXPECT_TRUE(s.CanAdaptUp(kVideoCodecMultiplex, 1000, 1));  // Not configured.
404 }
405 
TEST(BalancedDegradationSettings,CanAdaptUpResolution)406 TEST(BalancedDegradationSettings, CanAdaptUpResolution) {
407   VideoCodecType vp8 = kVideoCodecVP8;
408   webrtc::test::ScopedFieldTrials field_trials(
409       "WebRTC-Video-BalancedDegradationSettings/"
410       "pixels:1000|2000|3000|4000,fps:5|15|25|30,kbps_res:0|80|0|90,"
411       "vp9_kbps_res:40|50|60|70/");
412   BalancedDegradationSettings s;
413   EXPECT_TRUE(s.CanAdaptUpResolution(vp8, 1000, 0));  // No bitrate provided.
414   EXPECT_FALSE(s.CanAdaptUpResolution(vp8, 1000, 79000));
415   EXPECT_TRUE(s.CanAdaptUpResolution(vp8, 1000, 80000));
416   EXPECT_TRUE(s.CanAdaptUpResolution(vp8, 1001, 1));  // No limit configured.
417   EXPECT_FALSE(s.CanAdaptUpResolution(vp8, 3000, 89000));
418   EXPECT_TRUE(s.CanAdaptUpResolution(vp8, 3000, 90000));
419   EXPECT_TRUE(s.CanAdaptUpResolution(vp8, 3001, 1));  // No limit.
420 }
421 
TEST(BalancedDegradationSettings,CanAdaptUpResolutionWithCodecType)422 TEST(BalancedDegradationSettings, CanAdaptUpResolutionWithCodecType) {
423   webrtc::test::ScopedFieldTrials field_trials(
424       "WebRTC-Video-BalancedDegradationSettings/"
425       "pixels:1000|2000|3000|4000,fps:5|15|25|30,vp8_kbps_res:0|30|40|50,"
426       "vp9_kbps_res:0|60|70|80,h264_kbps_res:0|55|65|75,"
427       "av1_kbps_res:0|77|88|99,generic_kbps_res:0|25|35|45/");
428   BalancedDegradationSettings s;
429   EXPECT_FALSE(s.CanAdaptUpResolution(kVideoCodecVP8, 1000, 29000));
430   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecVP8, 1000, 30000));
431   EXPECT_FALSE(s.CanAdaptUpResolution(kVideoCodecVP9, 1000, 59000));
432   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecVP9, 1000, 60000));
433   EXPECT_FALSE(s.CanAdaptUpResolution(kVideoCodecH264, 1000, 54000));
434   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecH264, 1000, 55000));
435   EXPECT_FALSE(s.CanAdaptUpResolution(kVideoCodecAV1, 1000, 76000));
436   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecAV1, 1000, 77000));
437   EXPECT_FALSE(s.CanAdaptUpResolution(kVideoCodecGeneric, 1000, 24000));
438   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecGeneric, 1000, 25000));
439   EXPECT_TRUE(s.CanAdaptUpResolution(kVideoCodecMultiplex, 1000,
440                                      1));  // Not configured.
441 }
442 
TEST(BalancedDegradationSettings,GetsFpsDiff)443 TEST(BalancedDegradationSettings, GetsFpsDiff) {
444   webrtc::test::ScopedFieldTrials field_trials(
445       "WebRTC-Video-BalancedDegradationSettings/"
446       "pixels:1000|2000|3000,fps:5|15|25,fps_diff:0|-2|3/");
447   BalancedDegradationSettings settings;
448   EXPECT_EQ(0, settings.MinFpsDiff(1));
449   EXPECT_EQ(0, settings.MinFpsDiff(1000));
450   EXPECT_EQ(-2, settings.MinFpsDiff(1001));
451   EXPECT_EQ(-2, settings.MinFpsDiff(2000));
452   EXPECT_EQ(3, settings.MinFpsDiff(2001));
453   EXPECT_EQ(3, settings.MinFpsDiff(3000));
454   EXPECT_FALSE(settings.MinFpsDiff(3001));
455 }
456 
TEST(BalancedDegradationSettings,GetsNoFpsDiffIfValueBelowMinSetting)457 TEST(BalancedDegradationSettings, GetsNoFpsDiffIfValueBelowMinSetting) {
458   webrtc::test::ScopedFieldTrials field_trials(
459       "WebRTC-Video-BalancedDegradationSettings/"
460       "pixels:1000|2000|3000,fps:5|15|25,fps_diff:-100|-99|-101/");
461   // Min valid fps_diff setting: -99.
462   BalancedDegradationSettings settings;
463   EXPECT_FALSE(settings.MinFpsDiff(1000));
464   EXPECT_EQ(-99, settings.MinFpsDiff(2000));
465   EXPECT_FALSE(settings.MinFpsDiff(3000));
466 }
467 
TEST(BalancedDegradationSettings,QpThresholdsNotSetByDefault)468 TEST(BalancedDegradationSettings, QpThresholdsNotSetByDefault) {
469   webrtc::test::ScopedFieldTrials field_trials(
470       "WebRTC-Video-BalancedDegradationSettings/"
471       "pixels:1000|2000|3000,fps:5|15|25/");
472   BalancedDegradationSettings settings;
473   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecVP8, 1));
474   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecVP9, 1));
475   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecH264, 1));
476   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecAV1, 1));
477   EXPECT_FALSE(settings.GetQpThresholds(kVideoCodecGeneric, 1));
478 }
479 
TEST(BalancedDegradationSettings,GetsConfigWithQpThresholds)480 TEST(BalancedDegradationSettings, GetsConfigWithQpThresholds) {
481   webrtc::test::ScopedFieldTrials field_trials(
482       "WebRTC-Video-BalancedDegradationSettings/"
483       "pixels:1000|2000|3000,fps:5|15|25,vp8_qp_low:89|90|88,"
484       "vp8_qp_high:90|91|92,vp9_qp_low:27|28|29,vp9_qp_high:120|130|140,"
485       "h264_qp_low:12|13|14,h264_qp_high:20|30|40,av1_qp_low:2|3|4,"
486       "av1_qp_high:11|33|44,generic_qp_low:7|6|5,generic_qp_high:22|23|24/");
487   BalancedDegradationSettings settings;
488   EXPECT_THAT(settings.GetConfigs(),
489               ::testing::ElementsAre(
490                   BalancedDegradationSettings::Config{
491                       1000,
492                       5,
493                       0,
494                       0,
495                       BalancedDegradationSettings::kNoFpsDiff,
496                       {89, 90, 0, 0, 0},
497                       {27, 120, 0, 0, 0},
498                       {12, 20, 0, 0, 0},
499                       {2, 11, 0, 0, 0},
500                       {7, 22, 0, 0, 0}},
501                   BalancedDegradationSettings::Config{
502                       2000,
503                       15,
504                       0,
505                       0,
506                       BalancedDegradationSettings::kNoFpsDiff,
507                       {90, 91, 0, 0, 0},
508                       {28, 130, 0, 0, 0},
509                       {13, 30, 0, 0, 0},
510                       {3, 33, 0, 0, 0},
511                       {6, 23, 0, 0, 0}},
512                   BalancedDegradationSettings::Config{
513                       3000,
514                       25,
515                       0,
516                       0,
517                       BalancedDegradationSettings::kNoFpsDiff,
518                       {88, 92, 0, 0, 0},
519                       {29, 140, 0, 0, 0},
520                       {14, 40, 0, 0, 0},
521                       {4, 44, 0, 0, 0},
522                       {5, 24, 0, 0, 0}}));
523 }
524 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfOnlyHasLowThreshold)525 TEST(BalancedDegradationSettings, GetsDefaultConfigIfOnlyHasLowThreshold) {
526   webrtc::test::ScopedFieldTrials field_trials(
527       "WebRTC-Video-BalancedDegradationSettings/"
528       "pixels:1000|2000|3000,fps:5|15|25,vp8_qp_low:89|90|88/");
529   BalancedDegradationSettings settings;
530   VerifyIsDefault(settings.GetConfigs());
531 }
532 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfOnlyHasHighThreshold)533 TEST(BalancedDegradationSettings, GetsDefaultConfigIfOnlyHasHighThreshold) {
534   webrtc::test::ScopedFieldTrials field_trials(
535       "WebRTC-Video-BalancedDegradationSettings/"
536       "pixels:1000|2000|3000,fps:5|15|25,vp8_qp_high:90|91|92/");
537   BalancedDegradationSettings settings;
538   VerifyIsDefault(settings.GetConfigs());
539 }
540 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfLowEqualsHigh)541 TEST(BalancedDegradationSettings, GetsDefaultConfigIfLowEqualsHigh) {
542   webrtc::test::ScopedFieldTrials field_trials(
543       "WebRTC-Video-BalancedDegradationSettings/"
544       "pixels:1000|2000|3000,fps:5|15|25,"
545       "vp8_qp_low:89|90|88,vp8_qp_high:90|91|88/");
546   BalancedDegradationSettings settings;
547   VerifyIsDefault(settings.GetConfigs());
548 }
549 
TEST(BalancedDegradationSettings,GetsDefaultConfigIfLowGreaterThanHigh)550 TEST(BalancedDegradationSettings, GetsDefaultConfigIfLowGreaterThanHigh) {
551   webrtc::test::ScopedFieldTrials field_trials(
552       "WebRTC-Video-BalancedDegradationSettings/"
553       "pixels:1000|2000|3000,fps:5|15|25,"
554       "vp8_qp_low:89|90|88,vp8_qp_high:90|91|87/");
555   BalancedDegradationSettings settings;
556   VerifyIsDefault(settings.GetConfigs());
557 }
558 
TEST(BalancedDegradationSettings,GetsDefaultConfigForZeroQpValue)559 TEST(BalancedDegradationSettings, GetsDefaultConfigForZeroQpValue) {
560   webrtc::test::ScopedFieldTrials field_trials(
561       "WebRTC-Video-BalancedDegradationSettings/"
562       "pixels:1000|2000|3000,fps:5|15|25,"
563       "vp8_qp_low:89|0|88,vp8_qp_high:90|91|92/");
564   BalancedDegradationSettings settings;
565   VerifyIsDefault(settings.GetConfigs());
566 }
567 
TEST(BalancedDegradationSettings,GetsVp8QpThresholds)568 TEST(BalancedDegradationSettings, GetsVp8QpThresholds) {
569   webrtc::test::ScopedFieldTrials field_trials(
570       "WebRTC-Video-BalancedDegradationSettings/"
571       "pixels:1000|2000|3000,fps:5|15|25,"
572       "vp8_qp_low:89|90|88,vp8_qp_high:90|91|92/");
573   BalancedDegradationSettings settings;
574   EXPECT_EQ(89, settings.GetQpThresholds(kVideoCodecVP8, 1)->low);
575   EXPECT_EQ(90, settings.GetQpThresholds(kVideoCodecVP8, 1)->high);
576   EXPECT_EQ(90, settings.GetQpThresholds(kVideoCodecVP8, 1000)->high);
577   EXPECT_EQ(91, settings.GetQpThresholds(kVideoCodecVP8, 1001)->high);
578   EXPECT_EQ(91, settings.GetQpThresholds(kVideoCodecVP8, 2000)->high);
579   EXPECT_EQ(92, settings.GetQpThresholds(kVideoCodecVP8, 2001)->high);
580   EXPECT_EQ(92, settings.GetQpThresholds(kVideoCodecVP8, 3000)->high);
581   EXPECT_EQ(92, settings.GetQpThresholds(kVideoCodecVP8, 3001)->high);
582 }
583 
TEST(BalancedDegradationSettings,GetsVp9QpThresholds)584 TEST(BalancedDegradationSettings, GetsVp9QpThresholds) {
585   webrtc::test::ScopedFieldTrials field_trials(
586       "WebRTC-Video-BalancedDegradationSettings/"
587       "pixels:1000|2000|3000,fps:5|15|25,"
588       "vp9_qp_low:55|56|57,vp9_qp_high:155|156|157/");
589   BalancedDegradationSettings settings;
590   const auto thresholds = settings.GetQpThresholds(kVideoCodecVP9, 1000);
591   EXPECT_TRUE(thresholds);
592   EXPECT_EQ(55, thresholds->low);
593   EXPECT_EQ(155, thresholds->high);
594 }
595 
TEST(BalancedDegradationSettings,GetsH264QpThresholds)596 TEST(BalancedDegradationSettings, GetsH264QpThresholds) {
597   webrtc::test::ScopedFieldTrials field_trials(
598       "WebRTC-Video-BalancedDegradationSettings/"
599       "pixels:1000|2000|3000,fps:5|15|25,"
600       "h264_qp_low:21|22|23,h264_qp_high:41|43|42/");
601   BalancedDegradationSettings settings;
602   const auto thresholds = settings.GetQpThresholds(kVideoCodecH264, 2000);
603   EXPECT_TRUE(thresholds);
604   EXPECT_EQ(22, thresholds->low);
605   EXPECT_EQ(43, thresholds->high);
606 }
607 
TEST(BalancedDegradationSettings,GetsGenericQpThresholds)608 TEST(BalancedDegradationSettings, GetsGenericQpThresholds) {
609   webrtc::test::ScopedFieldTrials field_trials(
610       "WebRTC-Video-BalancedDegradationSettings/"
611       "pixels:1000|2000|3000,fps:5|15|25,"
612       "generic_qp_low:2|3|4,generic_qp_high:22|23|24/");
613   BalancedDegradationSettings settings;
614   const auto thresholds = settings.GetQpThresholds(kVideoCodecGeneric, 3000);
615   EXPECT_TRUE(thresholds);
616   EXPECT_EQ(4, thresholds->low);
617   EXPECT_EQ(24, thresholds->high);
618 }
619 
620 }  // namespace webrtc
621