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/quality_rampup_experiment.h"
12 
13 #include "test/field_trial.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 namespace {
18 
19 class QualityRampupExperimentTest : public ::testing::Test {
20  protected:
NowMs() const21   int64_t NowMs() const { return current_ms_; }
AdvanceMs(int64_t delta_ms)22   int64_t AdvanceMs(int64_t delta_ms) {
23     current_ms_ += delta_ms;
24     return current_ms_;
25   }
26   int64_t current_ms_ = 2345;
27 };
28 
TEST_F(QualityRampupExperimentTest,ValuesNotSetByDefault)29 TEST_F(QualityRampupExperimentTest, ValuesNotSetByDefault) {
30   const auto settings = QualityRampupExperiment::ParseSettings();
31   EXPECT_FALSE(settings.MinPixels());
32   EXPECT_FALSE(settings.MinDurationMs());
33   EXPECT_FALSE(settings.MaxBitrateFactor());
34 }
35 
TEST_F(QualityRampupExperimentTest,ParseMinPixels)36 TEST_F(QualityRampupExperimentTest, ParseMinPixels) {
37   test::ScopedFieldTrials field_trials(
38       "WebRTC-Video-QualityRampupSettings/min_pixels:10000/");
39   EXPECT_EQ(10000, QualityRampupExperiment::ParseSettings().MinPixels());
40 }
41 
TEST_F(QualityRampupExperimentTest,ParseMinDuration)42 TEST_F(QualityRampupExperimentTest, ParseMinDuration) {
43   test::ScopedFieldTrials field_trials(
44       "WebRTC-Video-QualityRampupSettings/min_duration_ms:987/");
45   EXPECT_EQ(987, QualityRampupExperiment::ParseSettings().MinDurationMs());
46 }
47 
TEST_F(QualityRampupExperimentTest,ParseMaxBitrateFactor)48 TEST_F(QualityRampupExperimentTest, ParseMaxBitrateFactor) {
49   test::ScopedFieldTrials field_trials(
50       "WebRTC-Video-QualityRampupSettings/max_bitrate_factor:1.23/");
51   EXPECT_EQ(1.23, QualityRampupExperiment::ParseSettings().MaxBitrateFactor());
52 }
53 
TEST_F(QualityRampupExperimentTest,ReportsBwHighWhenDurationPassed)54 TEST_F(QualityRampupExperimentTest, ReportsBwHighWhenDurationPassed) {
55   test::ScopedFieldTrials field_trials(
56       "WebRTC-Video-QualityRampupSettings/"
57       "min_pixels:10000,min_duration_ms:2000/");
58   auto exp = QualityRampupExperiment::ParseSettings();
59   EXPECT_EQ(10000, exp.MinPixels());
60   EXPECT_EQ(2000, exp.MinDurationMs());
61 
62   const uint32_t kMaxKbps = 800;
63   exp.SetMaxBitrate(/*pixels*/ 10000, kMaxKbps);
64 
65   const uint32_t kAvailableKbps = kMaxKbps;
66   EXPECT_FALSE(exp.BwHigh(NowMs(), kAvailableKbps));
67   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000 - 1), kAvailableKbps));
68   EXPECT_TRUE(exp.BwHigh(AdvanceMs(1), kAvailableKbps));
69 }
70 
TEST_F(QualityRampupExperimentTest,UsesMaxSetBitrate)71 TEST_F(QualityRampupExperimentTest, UsesMaxSetBitrate) {
72   test::ScopedFieldTrials field_trials(
73       "WebRTC-Video-QualityRampupSettings/"
74       "min_pixels:10000,min_duration_ms:2000/");
75   auto exp = QualityRampupExperiment::ParseSettings();
76 
77   const uint32_t kMaxKbps = 800;
78   exp.SetMaxBitrate(/*pixels*/ 10000, kMaxKbps);
79   exp.SetMaxBitrate(/*pixels*/ 10000, kMaxKbps - 1);
80 
81   EXPECT_FALSE(exp.BwHigh(NowMs(), kMaxKbps - 1));
82   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000), kMaxKbps - 1));
83   EXPECT_FALSE(exp.BwHigh(AdvanceMs(1), kMaxKbps));
84   EXPECT_TRUE(exp.BwHigh(AdvanceMs(2000), kMaxKbps));
85 }
86 
TEST_F(QualityRampupExperimentTest,DoesNotReportBwHighIfBelowMinPixels)87 TEST_F(QualityRampupExperimentTest, DoesNotReportBwHighIfBelowMinPixels) {
88   test::ScopedFieldTrials field_trials(
89       "WebRTC-Video-QualityRampupSettings/"
90       "min_pixels:10000,min_duration_ms:2000/");
91   auto exp = QualityRampupExperiment::ParseSettings();
92 
93   const uint32_t kMaxKbps = 800;
94   exp.SetMaxBitrate(/*pixels*/ 9999, kMaxKbps);
95 
96   const uint32_t kAvailableKbps = kMaxKbps;
97   EXPECT_FALSE(exp.BwHigh(NowMs(), kAvailableKbps));
98   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000), kAvailableKbps));
99 }
100 
TEST_F(QualityRampupExperimentTest,ReportsBwHighWithMaxBitrateFactor)101 TEST_F(QualityRampupExperimentTest, ReportsBwHighWithMaxBitrateFactor) {
102   test::ScopedFieldTrials field_trials(
103       "WebRTC-Video-QualityRampupSettings/"
104       "min_pixels:10000,min_duration_ms:2000,max_bitrate_factor:1.5/");
105   auto exp = QualityRampupExperiment::ParseSettings();
106   EXPECT_EQ(10000, exp.MinPixels());
107   EXPECT_EQ(2000, exp.MinDurationMs());
108   EXPECT_EQ(1.5, exp.MaxBitrateFactor());
109 
110   const uint32_t kMaxKbps = 800;
111   exp.SetMaxBitrate(/*pixels*/ 10000, kMaxKbps);
112 
113   const uint32_t kAvailableKbps = kMaxKbps * 1.5;
114   EXPECT_FALSE(exp.BwHigh(NowMs(), kAvailableKbps - 1));
115   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000), kAvailableKbps - 1));
116   EXPECT_FALSE(exp.BwHigh(AdvanceMs(1), kAvailableKbps));
117   EXPECT_TRUE(exp.BwHigh(AdvanceMs(2000), kAvailableKbps));
118 }
119 
TEST_F(QualityRampupExperimentTest,ReportsBwHigh)120 TEST_F(QualityRampupExperimentTest, ReportsBwHigh) {
121   test::ScopedFieldTrials field_trials(
122       "WebRTC-Video-QualityRampupSettings/"
123       "min_pixels:10000,min_duration_ms:2000/");
124   auto exp = QualityRampupExperiment::ParseSettings();
125 
126   const uint32_t kMaxKbps = 800;
127   exp.SetMaxBitrate(/*pixels*/ 10000, kMaxKbps);
128 
129   const uint32_t kAvailableKbps = kMaxKbps;
130   EXPECT_FALSE(exp.BwHigh(NowMs(), kAvailableKbps));
131   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000 - 1), kAvailableKbps));
132   EXPECT_FALSE(exp.BwHigh(AdvanceMs(1), kAvailableKbps - 1));  // Below, reset.
133   EXPECT_FALSE(exp.BwHigh(AdvanceMs(1), kAvailableKbps));
134   EXPECT_FALSE(exp.BwHigh(AdvanceMs(2000 - 1), kAvailableKbps));
135   EXPECT_TRUE(exp.BwHigh(AdvanceMs(1), kAvailableKbps));
136 }
137 
138 }  // namespace
139 }  // namespace webrtc
140