1 /*
2  *  Copyright 2018 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/quality_scaling_experiment.h"
12 
13 #include "test/field_trial.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 namespace {
ExpectEqualSettings(QualityScalingExperiment::Settings a,QualityScalingExperiment::Settings b)18 void ExpectEqualSettings(QualityScalingExperiment::Settings a,
19                          QualityScalingExperiment::Settings b) {
20   EXPECT_EQ(a.vp8_low, b.vp8_low);
21   EXPECT_EQ(a.vp8_high, b.vp8_high);
22   EXPECT_EQ(a.vp9_low, b.vp9_low);
23   EXPECT_EQ(a.vp9_high, b.vp9_high);
24   EXPECT_EQ(a.h264_low, b.h264_low);
25   EXPECT_EQ(a.h264_high, b.h264_high);
26   EXPECT_EQ(a.generic_low, b.generic_low);
27   EXPECT_EQ(a.generic_high, b.generic_high);
28   EXPECT_EQ(a.alpha_high, b.alpha_high);
29   EXPECT_EQ(a.alpha_low, b.alpha_low);
30   EXPECT_EQ(a.drop, b.drop);
31 }
32 
ExpectEqualConfig(QualityScalingExperiment::Config a,QualityScalingExperiment::Config b)33 void ExpectEqualConfig(QualityScalingExperiment::Config a,
34                        QualityScalingExperiment::Config b) {
35   EXPECT_EQ(a.alpha_high, b.alpha_high);
36   EXPECT_EQ(a.alpha_low, b.alpha_low);
37   EXPECT_EQ(a.use_all_drop_reasons, b.use_all_drop_reasons);
38 }
39 }  // namespace
40 
41 #if !defined(WEBRTC_IOS)
42 // TODO(bugs.webrtc.org/12401): investigate why QualityScaler kicks in on iOS.
TEST(QualityScalingExperimentTest,DefaultEnabledWithoutFieldTrial)43 TEST(QualityScalingExperimentTest, DefaultEnabledWithoutFieldTrial) {
44   webrtc::test::ScopedFieldTrials field_trials("");
45   EXPECT_TRUE(QualityScalingExperiment::Enabled());
46 }
47 #else
TEST(QualityScalingExperimentTest,DefaultDisabledWithoutFieldTrialIOS)48 TEST(QualityScalingExperimentTest, DefaultDisabledWithoutFieldTrialIOS) {
49   webrtc::test::ScopedFieldTrials field_trials("");
50   EXPECT_FALSE(QualityScalingExperiment::Enabled());
51 }
52 #endif
53 
TEST(QualityScalingExperimentTest,EnabledWithFieldTrial)54 TEST(QualityScalingExperimentTest, EnabledWithFieldTrial) {
55   webrtc::test::ScopedFieldTrials field_trials(
56       "WebRTC-Video-QualityScaling/Enabled/");
57   EXPECT_TRUE(QualityScalingExperiment::Enabled());
58 }
59 
TEST(QualityScalingExperimentTest,ParseSettings)60 TEST(QualityScalingExperimentTest, ParseSettings) {
61   const QualityScalingExperiment::Settings kExpected = {1, 2, 3,    4,     5, 6,
62                                                         7, 8, 0.9f, 0.99f, 1};
63   webrtc::test::ScopedFieldTrials field_trials(
64       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,7,8,0.9,0.99,1/");
65   const auto settings = QualityScalingExperiment::ParseSettings();
66   EXPECT_TRUE(settings);
67   ExpectEqualSettings(kExpected, *settings);
68 }
69 
70 #if !defined(WEBRTC_IOS)
71 // TODO(bugs.webrtc.org/12401): investigate why QualityScaler kicks in on iOS.
TEST(QualityScalingExperimentTest,ParseSettingsUsesDefaultsWithoutFieldTrial)72 TEST(QualityScalingExperimentTest, ParseSettingsUsesDefaultsWithoutFieldTrial) {
73   webrtc::test::ScopedFieldTrials field_trials("");
74   // Uses some default hard coded values.
75   EXPECT_TRUE(QualityScalingExperiment::ParseSettings());
76 }
77 #else
TEST(QualityScalingExperimentTest,ParseSettingsFailsWithoutFieldTrial)78 TEST(QualityScalingExperimentTest, ParseSettingsFailsWithoutFieldTrial) {
79   webrtc::test::ScopedFieldTrials field_trials("");
80   EXPECT_FALSE(QualityScalingExperiment::ParseSettings());
81 }
82 #endif
83 
TEST(QualityScalingExperimentTest,ParseSettingsFailsWithInvalidFieldTrial)84 TEST(QualityScalingExperimentTest, ParseSettingsFailsWithInvalidFieldTrial) {
85   webrtc::test::ScopedFieldTrials field_trials(
86       "WebRTC-Video-QualityScaling/Enabled-invalid/");
87   EXPECT_FALSE(QualityScalingExperiment::ParseSettings());
88 }
89 
TEST(QualityScalingExperimentTest,GetConfig)90 TEST(QualityScalingExperimentTest, GetConfig) {
91   webrtc::test::ScopedFieldTrials field_trials(
92       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,7,8,0.9,0.99,0/");
93   const auto config = QualityScalingExperiment::GetConfig();
94   EXPECT_EQ(0.9f, config.alpha_high);
95   EXPECT_EQ(0.99f, config.alpha_low);
96   EXPECT_FALSE(config.use_all_drop_reasons);
97 }
98 
TEST(QualityScalingExperimentTest,GetsDefaultConfigForInvalidFieldTrial)99 TEST(QualityScalingExperimentTest, GetsDefaultConfigForInvalidFieldTrial) {
100   webrtc::test::ScopedFieldTrials field_trials(
101       "WebRTC-Video-QualityScaling/Enabled-invalid/");
102   const auto config = QualityScalingExperiment::GetConfig();
103   ExpectEqualConfig(config, QualityScalingExperiment::Config());
104 }
105 
TEST(QualityScalingExperimentTest,GetsDefaultAlphaForInvalidValue)106 TEST(QualityScalingExperimentTest, GetsDefaultAlphaForInvalidValue) {
107   QualityScalingExperiment::Config expected_config;
108   expected_config.use_all_drop_reasons = true;
109   webrtc::test::ScopedFieldTrials field_trials(
110       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,7,8,0.99,0.9,1/");
111   const auto config = QualityScalingExperiment::GetConfig();
112   ExpectEqualConfig(config, expected_config);
113 }
114 
TEST(QualityScalingExperimentTest,GetVp8Thresholds)115 TEST(QualityScalingExperimentTest, GetVp8Thresholds) {
116   webrtc::test::ScopedFieldTrials field_trials(
117       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,0,0,0.9,0.99,1/");
118   const auto thresholds =
119       QualityScalingExperiment::GetQpThresholds(kVideoCodecVP8);
120   EXPECT_TRUE(thresholds);
121   EXPECT_EQ(1, thresholds->low);
122   EXPECT_EQ(2, thresholds->high);
123 }
124 
TEST(QualityScalingExperimentTest,GetThresholdsFailsForInvalidVp8Value)125 TEST(QualityScalingExperimentTest, GetThresholdsFailsForInvalidVp8Value) {
126   webrtc::test::ScopedFieldTrials field_trials(
127       "WebRTC-Video-QualityScaling/Enabled-0,0,3,4,5,6,7,8,0.9,0.99,1/");
128   const auto thresholds =
129       QualityScalingExperiment::GetQpThresholds(kVideoCodecVP8);
130   EXPECT_FALSE(thresholds);
131 }
132 
TEST(QualityScalingExperimentTest,GetVp9Thresholds)133 TEST(QualityScalingExperimentTest, GetVp9Thresholds) {
134   webrtc::test::ScopedFieldTrials field_trials(
135       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,0,0,0.9,0.99,1/");
136   const auto thresholds =
137       QualityScalingExperiment::GetQpThresholds(kVideoCodecVP9);
138   EXPECT_TRUE(thresholds);
139   EXPECT_EQ(3, thresholds->low);
140   EXPECT_EQ(4, thresholds->high);
141 }
142 
TEST(QualityScalingExperimentTest,GetThresholdsFailsForInvalidVp9Value)143 TEST(QualityScalingExperimentTest, GetThresholdsFailsForInvalidVp9Value) {
144   webrtc::test::ScopedFieldTrials field_trials(
145       "WebRTC-Video-QualityScaling/Enabled-1,2,0,0,5,6,7,8,0.9,0.99,1/");
146   const auto thresholds =
147       QualityScalingExperiment::GetQpThresholds(kVideoCodecVP9);
148   EXPECT_FALSE(thresholds);
149 }
150 
TEST(QualityScalingExperimentTest,GetH264Thresholds)151 TEST(QualityScalingExperimentTest, GetH264Thresholds) {
152   webrtc::test::ScopedFieldTrials field_trials(
153       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,0,0,0.9,0.99,1/");
154   const auto thresholds =
155       QualityScalingExperiment::GetQpThresholds(kVideoCodecH264);
156   EXPECT_TRUE(thresholds);
157   EXPECT_EQ(5, thresholds->low);
158   EXPECT_EQ(6, thresholds->high);
159 }
160 
TEST(QualityScalingExperimentTest,GetThresholdsFailsForInvalidH264Value)161 TEST(QualityScalingExperimentTest, GetThresholdsFailsForInvalidH264Value) {
162   webrtc::test::ScopedFieldTrials field_trials(
163       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,0,0,7,8,0.9,0.99,1/");
164   const auto thresholds =
165       QualityScalingExperiment::GetQpThresholds(kVideoCodecH264);
166   EXPECT_FALSE(thresholds);
167 }
168 
TEST(QualityScalingExperimentTest,GetGenericThresholds)169 TEST(QualityScalingExperimentTest, GetGenericThresholds) {
170   webrtc::test::ScopedFieldTrials field_trials(
171       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,0,0,7,8,0.9,0.99,1/");
172   const auto thresholds =
173       QualityScalingExperiment::GetQpThresholds(kVideoCodecGeneric);
174   EXPECT_TRUE(thresholds);
175   EXPECT_EQ(7, thresholds->low);
176   EXPECT_EQ(8, thresholds->high);
177 }
178 
TEST(QualityScalingExperimentTest,GetThresholdsFailsForInvalidGenericValue)179 TEST(QualityScalingExperimentTest, GetThresholdsFailsForInvalidGenericValue) {
180   webrtc::test::ScopedFieldTrials field_trials(
181       "WebRTC-Video-QualityScaling/Enabled-1,2,3,4,5,6,0,0,0.9,0.99,1/");
182   const auto thresholds =
183       QualityScalingExperiment::GetQpThresholds(kVideoCodecGeneric);
184   EXPECT_FALSE(thresholds);
185 }
186 }  // namespace webrtc
187