blob: eac19d0081ae85412bd266738821a0b90922d221 [file] [log] [blame]
Sebastian Jansson6bcd7f62018-02-27 16:07:021/*
2 * Copyright (c) 2016 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
Sebastian Janssonfc7ec8e2018-02-28 15:48:0011#include "modules/congestion_controller/goog_cc/alr_detector.h"
Sebastian Jansson6bcd7f62018-02-27 16:07:0212
Per Kjellander5b698732019-04-15 10:36:3313#include "api/transport/field_trial_based_config.h"
Yves Gerey988cc082018-10-23 10:03:0114#include "rtc_base/checks.h"
Sebastian Jansson6bcd7f62018-02-27 16:07:0215#include "rtc_base/experiments/alr_experiment.h"
16#include "test/field_trial.h"
17#include "test/gtest.h"
18
19namespace {
20
21constexpr int kEstimatedBitrateBps = 300000;
22
23} // namespace
24
25namespace webrtc {
Sebastian Jansson6bcd7f62018-02-27 16:07:0226namespace {
27class SimulateOutgoingTrafficIn {
28 public:
29 explicit SimulateOutgoingTrafficIn(AlrDetector* alr_detector,
30 int64_t* timestamp_ms)
31 : alr_detector_(alr_detector), timestamp_ms_(timestamp_ms) {
32 RTC_CHECK(alr_detector_);
33 }
34
35 SimulateOutgoingTrafficIn& ForTimeMs(int time_ms) {
36 interval_ms_ = time_ms;
37 ProduceTraffic();
38 return *this;
39 }
40
41 SimulateOutgoingTrafficIn& AtPercentOfEstimatedBitrate(int usage_percentage) {
42 usage_percentage_.emplace(usage_percentage);
43 ProduceTraffic();
44 return *this;
45 }
46
47 private:
48 void ProduceTraffic() {
49 if (!interval_ms_ || !usage_percentage_)
50 return;
51 const int kTimeStepMs = 10;
52 for (int t = 0; t < *interval_ms_; t += kTimeStepMs) {
53 *timestamp_ms_ += kTimeStepMs;
54 alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ *
55 kTimeStepMs / (8 * 100 * 1000),
56 *timestamp_ms_);
57 }
58 int remainder_ms = *interval_ms_ % kTimeStepMs;
59 if (remainder_ms > 0) {
60 *timestamp_ms_ += kTimeStepMs;
61 alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ *
62 remainder_ms / (8 * 100 * 1000),
63 *timestamp_ms_);
64 }
65 }
66 AlrDetector* const alr_detector_;
67 int64_t* timestamp_ms_;
Danil Chapovalov0040b662018-06-18 08:48:1668 absl::optional<int> interval_ms_;
69 absl::optional<int> usage_percentage_;
Sebastian Jansson6bcd7f62018-02-27 16:07:0270};
71} // namespace
72
Per Kjellandera7caaf02019-04-29 11:51:5273TEST(AlrDetectorTest, AlrDetection) {
74 FieldTrialBasedConfig field_trials;
75 int64_t timestamp_ms = 1000;
76 AlrDetector alr_detector(&field_trials);
77 alr_detector.SetEstimatedBitrate(kEstimatedBitrateBps);
Sebastian Jansson6bcd7f62018-02-27 16:07:0278
Sebastian Jansson6bcd7f62018-02-27 16:07:0279 // Start in non-ALR state.
Per Kjellandera7caaf02019-04-29 11:51:5280 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:0281
82 // Stay in non-ALR state when usage is close to 100%.
Per Kjellandera7caaf02019-04-29 11:51:5283 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:0284 .ForTimeMs(1000)
85 .AtPercentOfEstimatedBitrate(90);
Per Kjellandera7caaf02019-04-29 11:51:5286 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:0287
88 // Verify that we ALR starts when bitrate drops below 20%.
Per Kjellandera7caaf02019-04-29 11:51:5289 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:0290 .ForTimeMs(1500)
91 .AtPercentOfEstimatedBitrate(20);
Per Kjellandera7caaf02019-04-29 11:51:5292 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:0293
94 // Verify that ALR ends when usage is above 65%.
Per Kjellandera7caaf02019-04-29 11:51:5295 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:0296 .ForTimeMs(4000)
97 .AtPercentOfEstimatedBitrate(100);
Per Kjellandera7caaf02019-04-29 11:51:5298 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:0299}
100
Per Kjellandera7caaf02019-04-29 11:51:52101TEST(AlrDetectorTest, ShortSpike) {
102 FieldTrialBasedConfig field_trials;
103 int64_t timestamp_ms = 1000;
104 AlrDetector alr_detector(&field_trials);
105 alr_detector.SetEstimatedBitrate(kEstimatedBitrateBps);
Sebastian Jansson6bcd7f62018-02-27 16:07:02106 // Start in non-ALR state.
Per Kjellandera7caaf02019-04-29 11:51:52107 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02108
109 // Verify that we ALR starts when bitrate drops below 20%.
Per Kjellandera7caaf02019-04-29 11:51:52110 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:02111 .ForTimeMs(1000)
112 .AtPercentOfEstimatedBitrate(20);
Per Kjellandera7caaf02019-04-29 11:51:52113 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02114
115 // Verify that we stay in ALR region even after a short bitrate spike.
Per Kjellandera7caaf02019-04-29 11:51:52116 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:02117 .ForTimeMs(100)
118 .AtPercentOfEstimatedBitrate(150);
Per Kjellandera7caaf02019-04-29 11:51:52119 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02120
121 // ALR ends when usage is above 65%.
Per Kjellandera7caaf02019-04-29 11:51:52122 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:02123 .ForTimeMs(3000)
124 .AtPercentOfEstimatedBitrate(100);
Per Kjellandera7caaf02019-04-29 11:51:52125 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02126}
127
Per Kjellandera7caaf02019-04-29 11:51:52128TEST(AlrDetectorTest, BandwidthEstimateChanges) {
129 FieldTrialBasedConfig field_trials;
130 int64_t timestamp_ms = 1000;
131 AlrDetector alr_detector(&field_trials);
132 alr_detector.SetEstimatedBitrate(kEstimatedBitrateBps);
133
Sebastian Jansson6bcd7f62018-02-27 16:07:02134 // Start in non-ALR state.
Per Kjellandera7caaf02019-04-29 11:51:52135 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02136
137 // ALR starts when bitrate drops below 20%.
Per Kjellandera7caaf02019-04-29 11:51:52138 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:02139 .ForTimeMs(1000)
140 .AtPercentOfEstimatedBitrate(20);
Per Kjellandera7caaf02019-04-29 11:51:52141 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02142
143 // When bandwidth estimate drops the detector should stay in ALR mode and quit
144 // it shortly afterwards as the sender continues sending the same amount of
145 // traffic. This is necessary to ensure that ProbeController can still react
146 // to the BWE drop by initiating a new probe.
Per Kjellandera7caaf02019-04-29 11:51:52147 alr_detector.SetEstimatedBitrate(kEstimatedBitrateBps / 5);
148 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
149 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
Sebastian Jansson6bcd7f62018-02-27 16:07:02150 .ForTimeMs(1000)
151 .AtPercentOfEstimatedBitrate(50);
Per Kjellandera7caaf02019-04-29 11:51:52152 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
Sebastian Jansson6bcd7f62018-02-27 16:07:02153}
154
Per Kjellandera7caaf02019-04-29 11:51:52155TEST(AlrDetectorTest, ParseControlFieldTrial) {
156 webrtc::test::ScopedFieldTrials scoped_field_trial(
Sebastian Jansson6bcd7f62018-02-27 16:07:02157 "WebRTC-ProbingScreenshareBwe/Control/");
Danil Chapovalov0040b662018-06-18 08:48:16158 absl::optional<AlrExperimentSettings> parsed_params =
Sebastian Jansson6bcd7f62018-02-27 16:07:02159 AlrExperimentSettings::CreateFromFieldTrial(
Per Kjellander5b698732019-04-15 10:36:33160 FieldTrialBasedConfig(), "WebRTC-ProbingScreenshareBwe");
Sebastian Jansson6bcd7f62018-02-27 16:07:02161 EXPECT_FALSE(static_cast<bool>(parsed_params));
162}
163
Per Kjellandera7caaf02019-04-29 11:51:52164TEST(AlrDetectorTest, ParseActiveFieldTrial) {
165 webrtc::test::ScopedFieldTrials scoped_field_trial(
Sebastian Jansson6bcd7f62018-02-27 16:07:02166 "WebRTC-ProbingScreenshareBwe/1.1,2875,85,20,-20,1/");
Danil Chapovalov0040b662018-06-18 08:48:16167 absl::optional<AlrExperimentSettings> parsed_params =
Sebastian Jansson6bcd7f62018-02-27 16:07:02168 AlrExperimentSettings::CreateFromFieldTrial(
Per Kjellander5b698732019-04-15 10:36:33169 FieldTrialBasedConfig(), "WebRTC-ProbingScreenshareBwe");
Sebastian Jansson6bcd7f62018-02-27 16:07:02170 ASSERT_TRUE(static_cast<bool>(parsed_params));
171 EXPECT_EQ(1.1f, parsed_params->pacing_factor);
172 EXPECT_EQ(2875, parsed_params->max_paced_queue_time);
173 EXPECT_EQ(85, parsed_params->alr_bandwidth_usage_percent);
174 EXPECT_EQ(20, parsed_params->alr_start_budget_level_percent);
175 EXPECT_EQ(-20, parsed_params->alr_stop_budget_level_percent);
176 EXPECT_EQ(1, parsed_params->group_id);
177}
178
Per Kjellandera7caaf02019-04-29 11:51:52179TEST(AlrDetectorTest, ParseAlrSpecificFieldTrial) {
180 webrtc::test::ScopedFieldTrials scoped_field_trial(
181 "WebRTC-AlrDetectorParameters/"
182 "bw_usage:90%,start:0%,stop:-10%/");
183 FieldTrialBasedConfig field_trials;
184 AlrDetector alr_detector(&field_trials);
185 int64_t timestamp_ms = 1000;
186 alr_detector.SetEstimatedBitrate(kEstimatedBitrateBps);
187
188 // Start in non-ALR state.
189 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
190
191 // ALR does not start at 100% utilization.
192 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
193 .ForTimeMs(1000)
194 .AtPercentOfEstimatedBitrate(100);
195 EXPECT_FALSE(alr_detector.GetApplicationLimitedRegionStartTime());
196
197 // ALR does start at 85% utilization.
198 // Overused 10% above so it should take about 2s to reach a budget level of
199 // 0%.
200 SimulateOutgoingTrafficIn(&alr_detector, &timestamp_ms)
201 .ForTimeMs(2100)
202 .AtPercentOfEstimatedBitrate(85);
203 EXPECT_TRUE(alr_detector.GetApplicationLimitedRegionStartTime());
204}
205
Sebastian Jansson6bcd7f62018-02-27 16:07:02206} // namespace webrtc