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/rate_control_settings.h"
12 
13 #include "api/video_codecs/video_codec.h"
14 #include "api/video_codecs/video_encoder_config.h"
15 #include "test/field_trial.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 
20 namespace {
21 
TEST(RateControlSettingsTest,CongestionWindow)22 TEST(RateControlSettingsTest, CongestionWindow) {
23   EXPECT_FALSE(
24       RateControlSettings::ParseFromFieldTrials().UseCongestionWindow());
25 
26   test::ScopedFieldTrials field_trials(
27       "WebRTC-CongestionWindow/QueueSize:100/");
28   const RateControlSettings settings_after =
29       RateControlSettings::ParseFromFieldTrials();
30   EXPECT_TRUE(settings_after.UseCongestionWindow());
31   EXPECT_EQ(settings_after.GetCongestionWindowAdditionalTimeMs(), 100);
32 }
33 
TEST(RateControlSettingsTest,CongestionWindowPushback)34 TEST(RateControlSettingsTest, CongestionWindowPushback) {
35   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials()
36                    .UseCongestionWindowPushback());
37 
38   test::ScopedFieldTrials field_trials(
39       "WebRTC-CongestionWindow/QueueSize:100,MinBitrate:100000/");
40   const RateControlSettings settings_after =
41       RateControlSettings::ParseFromFieldTrials();
42   EXPECT_TRUE(settings_after.UseCongestionWindowPushback());
43   EXPECT_EQ(settings_after.CongestionWindowMinPushbackTargetBitrateBps(),
44             100000u);
45 }
46 
TEST(RateControlSettingsTest,PacingFactor)47 TEST(RateControlSettingsTest, PacingFactor) {
48   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials().GetPacingFactor());
49 
50   test::ScopedFieldTrials field_trials(
51       "WebRTC-VideoRateControl/pacing_factor:1.2/");
52   const RateControlSettings settings_after =
53       RateControlSettings::ParseFromFieldTrials();
54   // Need to explicitly dereference the absl::optional
55   // for the EXPECT_DOUBLE_EQ to compile.
56   ASSERT_TRUE(settings_after.GetPacingFactor());
57   EXPECT_DOUBLE_EQ(*settings_after.GetPacingFactor(), 1.2);
58 }
59 
TEST(RateControlSettingsTest,AlrProbing)60 TEST(RateControlSettingsTest, AlrProbing) {
61   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials().UseAlrProbing());
62 
63   test::ScopedFieldTrials field_trials(
64       "WebRTC-VideoRateControl/alr_probing:1/");
65   EXPECT_TRUE(RateControlSettings::ParseFromFieldTrials().UseAlrProbing());
66 }
67 
TEST(RateControlSettingsTest,LibvpxVp8QpMax)68 TEST(RateControlSettingsTest, LibvpxVp8QpMax) {
69   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials().LibvpxVp8QpMax());
70 
71   test::ScopedFieldTrials field_trials(
72       "WebRTC-VideoRateControl/vp8_qp_max:50/");
73   EXPECT_EQ(RateControlSettings::ParseFromFieldTrials().LibvpxVp8QpMax(), 50);
74 }
75 
TEST(RateControlSettingsTest,DoesNotGetTooLargeLibvpxVp8QpMaxValue)76 TEST(RateControlSettingsTest, DoesNotGetTooLargeLibvpxVp8QpMaxValue) {
77   test::ScopedFieldTrials field_trials(
78       "WebRTC-VideoRateControl/vp8_qp_max:70/");
79   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials().LibvpxVp8QpMax());
80 }
81 
TEST(RateControlSettingsTest,LibvpxVp8MinPixels)82 TEST(RateControlSettingsTest, LibvpxVp8MinPixels) {
83   EXPECT_FALSE(
84       RateControlSettings::ParseFromFieldTrials().LibvpxVp8MinPixels());
85 
86   test::ScopedFieldTrials field_trials(
87       "WebRTC-VideoRateControl/vp8_min_pixels:50000/");
88   EXPECT_EQ(RateControlSettings::ParseFromFieldTrials().LibvpxVp8MinPixels(),
89             50000);
90 }
91 
TEST(RateControlSettingsTest,DoesNotGetTooSmallLibvpxVp8MinPixelValue)92 TEST(RateControlSettingsTest, DoesNotGetTooSmallLibvpxVp8MinPixelValue) {
93   test::ScopedFieldTrials field_trials(
94       "WebRTC-VideoRateControl/vp8_min_pixels:0/");
95   EXPECT_FALSE(
96       RateControlSettings::ParseFromFieldTrials().LibvpxVp8MinPixels());
97 }
98 
TEST(RateControlSettingsTest,LibvpxTrustedRateController)99 TEST(RateControlSettingsTest, LibvpxTrustedRateController) {
100   const RateControlSettings settings_before =
101       RateControlSettings::ParseFromFieldTrials();
102   EXPECT_FALSE(settings_before.LibvpxVp8TrustedRateController());
103   EXPECT_FALSE(settings_before.LibvpxVp9TrustedRateController());
104 
105   test::ScopedFieldTrials field_trials(
106       "WebRTC-VideoRateControl/trust_vp8:1,trust_vp9:1/");
107   const RateControlSettings settings_after =
108       RateControlSettings::ParseFromFieldTrials();
109   EXPECT_TRUE(settings_after.LibvpxVp8TrustedRateController());
110   EXPECT_TRUE(settings_after.LibvpxVp9TrustedRateController());
111 }
112 
TEST(RateControlSettingsTest,Vp8BaseHeavyTl3RateAllocationLegacyKey)113 TEST(RateControlSettingsTest, Vp8BaseHeavyTl3RateAllocationLegacyKey) {
114   const RateControlSettings settings_before =
115       RateControlSettings::ParseFromFieldTrials();
116   EXPECT_FALSE(settings_before.Vp8BaseHeavyTl3RateAllocation());
117 
118   test::ScopedFieldTrials field_trials(
119       "WebRTC-UseBaseHeavyVP8TL3RateAllocation/Enabled/");
120   const RateControlSettings settings_after =
121       RateControlSettings::ParseFromFieldTrials();
122   EXPECT_TRUE(settings_after.Vp8BaseHeavyTl3RateAllocation());
123 }
124 
TEST(RateControlSettingsTest,Vp8BaseHeavyTl3RateAllocationVideoRateControlKey)125 TEST(RateControlSettingsTest,
126      Vp8BaseHeavyTl3RateAllocationVideoRateControlKey) {
127   const RateControlSettings settings_before =
128       RateControlSettings::ParseFromFieldTrials();
129   EXPECT_FALSE(settings_before.Vp8BaseHeavyTl3RateAllocation());
130 
131   test::ScopedFieldTrials field_trials(
132       "WebRTC-VideoRateControl/vp8_base_heavy_tl3_alloc:1/");
133   const RateControlSettings settings_after =
134       RateControlSettings::ParseFromFieldTrials();
135   EXPECT_TRUE(settings_after.Vp8BaseHeavyTl3RateAllocation());
136 }
137 
TEST(RateControlSettingsTest,Vp8BaseHeavyTl3RateAllocationVideoRateControlKeyOverridesLegacyKey)138 TEST(RateControlSettingsTest,
139      Vp8BaseHeavyTl3RateAllocationVideoRateControlKeyOverridesLegacyKey) {
140   const RateControlSettings settings_before =
141       RateControlSettings::ParseFromFieldTrials();
142   EXPECT_FALSE(settings_before.Vp8BaseHeavyTl3RateAllocation());
143 
144   test::ScopedFieldTrials field_trials(
145       "WebRTC-UseBaseHeavyVP8TL3RateAllocation/Enabled/WebRTC-VideoRateControl/"
146       "vp8_base_heavy_tl3_alloc:0/");
147   const RateControlSettings settings_after =
148       RateControlSettings::ParseFromFieldTrials();
149   EXPECT_FALSE(settings_after.Vp8BaseHeavyTl3RateAllocation());
150 }
151 
TEST(RateControlSettingsTest,GetSimulcastHysteresisFactor)152 TEST(RateControlSettingsTest, GetSimulcastHysteresisFactor) {
153   const RateControlSettings settings_before =
154       RateControlSettings::ParseFromFieldTrials();
155   EXPECT_DOUBLE_EQ(settings_before.GetSimulcastHysteresisFactor(
156                        VideoCodecMode::kRealtimeVideo),
157                    1.0);
158   EXPECT_DOUBLE_EQ(settings_before.GetSimulcastHysteresisFactor(
159                        VideoEncoderConfig::ContentType::kRealtimeVideo),
160                    1.0);
161   EXPECT_DOUBLE_EQ(settings_before.GetSimulcastHysteresisFactor(
162                        VideoCodecMode::kScreensharing),
163                    1.35);
164   EXPECT_DOUBLE_EQ(settings_before.GetSimulcastHysteresisFactor(
165                        VideoEncoderConfig::ContentType::kScreen),
166                    1.35);
167 
168   test::ScopedFieldTrials field_trials(
169       "WebRTC-VideoRateControl/"
170       "video_hysteresis:1.2,screenshare_hysteresis:1.4/");
171   const RateControlSettings settings_after =
172       RateControlSettings::ParseFromFieldTrials();
173 
174   EXPECT_DOUBLE_EQ(settings_after.GetSimulcastHysteresisFactor(
175                        VideoCodecMode::kRealtimeVideo),
176                    1.2);
177   EXPECT_DOUBLE_EQ(settings_after.GetSimulcastHysteresisFactor(
178                        VideoEncoderConfig::ContentType::kRealtimeVideo),
179                    1.2);
180   EXPECT_DOUBLE_EQ(settings_after.GetSimulcastHysteresisFactor(
181                        VideoCodecMode::kScreensharing),
182                    1.4);
183   EXPECT_DOUBLE_EQ(settings_after.GetSimulcastHysteresisFactor(
184                        VideoEncoderConfig::ContentType::kScreen),
185                    1.4);
186 }
187 
TEST(RateControlSettingsTest,TriggerProbeOnMaxAllocatedBitrateChange)188 TEST(RateControlSettingsTest, TriggerProbeOnMaxAllocatedBitrateChange) {
189   EXPECT_TRUE(RateControlSettings::ParseFromFieldTrials()
190                   .TriggerProbeOnMaxAllocatedBitrateChange());
191 
192   test::ScopedFieldTrials field_trials(
193       "WebRTC-VideoRateControl/probe_max_allocation:0/");
194   EXPECT_FALSE(RateControlSettings::ParseFromFieldTrials()
195                    .TriggerProbeOnMaxAllocatedBitrateChange());
196 }
197 
TEST(RateControlSettingsTest,UseEncoderBitrateAdjuster)198 TEST(RateControlSettingsTest, UseEncoderBitrateAdjuster) {
199   // Should be off by default.
200   EXPECT_FALSE(
201       RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster());
202 
203   {
204     // Can be turned on via field trial.
205     test::ScopedFieldTrials field_trials(
206         "WebRTC-VideoRateControl/bitrate_adjuster:true/");
207     EXPECT_TRUE(RateControlSettings::ParseFromFieldTrials()
208                     .UseEncoderBitrateAdjuster());
209   }
210 }
211 
212 }  // namespace
213 
214 }  // namespace webrtc
215