1 /*
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 #include "modules/congestion_controller/goog_cc/probe_controller.h"
11
12 #include <memory>
13
14 #include "api/transport/field_trial_based_config.h"
15 #include "api/transport/network_types.h"
16 #include "api/units/data_rate.h"
17 #include "api/units/timestamp.h"
18 #include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
19 #include "rtc_base/logging.h"
20 #include "system_wrappers/include/clock.h"
21 #include "test/field_trial.h"
22 #include "test/gmock.h"
23 #include "test/gtest.h"
24
25 using ::testing::_;
26 using ::testing::AtLeast;
27 using ::testing::Field;
28 using ::testing::Matcher;
29 using ::testing::NiceMock;
30 using ::testing::Return;
31
32 namespace webrtc {
33 namespace test {
34
35 namespace {
36
37 constexpr int kMinBitrateBps = 100;
38 constexpr int kStartBitrateBps = 300;
39 constexpr int kMaxBitrateBps = 10000;
40
41 constexpr int kExponentialProbingTimeoutMs = 5000;
42
43 constexpr int kAlrProbeInterval = 5000;
44 constexpr int kAlrEndedTimeoutMs = 3000;
45 constexpr int kBitrateDropTimeoutMs = 5000;
46 } // namespace
47
48 class ProbeControllerTest : public ::testing::Test {
49 protected:
ProbeControllerTest()50 ProbeControllerTest() : clock_(100000000L) {
51 probe_controller_.reset(
52 new ProbeController(&field_trial_config_, &mock_rtc_event_log));
53 }
~ProbeControllerTest()54 ~ProbeControllerTest() override {}
55
SetNetworkAvailable(bool available)56 std::vector<ProbeClusterConfig> SetNetworkAvailable(bool available) {
57 NetworkAvailability msg;
58 msg.at_time = Timestamp::Millis(NowMs());
59 msg.network_available = available;
60 return probe_controller_->OnNetworkAvailability(msg);
61 }
62
NowMs()63 int64_t NowMs() { return clock_.TimeInMilliseconds(); }
64
65 FieldTrialBasedConfig field_trial_config_;
66 SimulatedClock clock_;
67 NiceMock<MockRtcEventLog> mock_rtc_event_log;
68 std::unique_ptr<ProbeController> probe_controller_;
69 };
70
TEST_F(ProbeControllerTest,InitiatesProbingAtStart)71 TEST_F(ProbeControllerTest, InitiatesProbingAtStart) {
72 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
73 kMaxBitrateBps, NowMs());
74 EXPECT_GE(probes.size(), 2u);
75 }
76
TEST_F(ProbeControllerTest,ProbeOnlyWhenNetworkIsUp)77 TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) {
78 SetNetworkAvailable(false);
79 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
80 kMaxBitrateBps, NowMs());
81 EXPECT_EQ(probes.size(), 0u);
82 probes = SetNetworkAvailable(true);
83 EXPECT_GE(probes.size(), 2u);
84 }
85
TEST_F(ProbeControllerTest,InitiatesProbingOnMaxBitrateIncrease)86 TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) {
87 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
88 kMaxBitrateBps, NowMs());
89 // Long enough to time out exponential probing.
90 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
91 probes = probe_controller_->SetEstimatedBitrate(kStartBitrateBps, NowMs());
92 probes = probe_controller_->Process(NowMs());
93 probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
94 kMaxBitrateBps + 100, NowMs());
95 EXPECT_EQ(probes.size(), 1u);
96 EXPECT_EQ(probes[0].target_data_rate.bps(), kMaxBitrateBps + 100);
97 }
98
TEST_F(ProbeControllerTest,ProbesOnMaxBitrateIncreaseOnlyWhenInAlr)99 TEST_F(ProbeControllerTest, ProbesOnMaxBitrateIncreaseOnlyWhenInAlr) {
100 probe_controller_.reset(
101 new ProbeController(&field_trial_config_, &mock_rtc_event_log));
102 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
103 kMaxBitrateBps, NowMs());
104 probes = probe_controller_->SetEstimatedBitrate(kMaxBitrateBps - 1, NowMs());
105
106 // Wait long enough to time out exponential probing.
107 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
108 probes = probe_controller_->Process(NowMs());
109 EXPECT_EQ(probes.size(), 0u);
110
111 // Probe when in alr.
112 probe_controller_->SetAlrStartTimeMs(clock_.TimeInMilliseconds());
113 probes = probe_controller_->OnMaxTotalAllocatedBitrate(kMaxBitrateBps + 1,
114 NowMs());
115 EXPECT_EQ(probes.size(), 2u);
116
117 // Do not probe when not in alr.
118 probe_controller_->SetAlrStartTimeMs(absl::nullopt);
119 probes = probe_controller_->OnMaxTotalAllocatedBitrate(kMaxBitrateBps + 2,
120 NowMs());
121 EXPECT_TRUE(probes.empty());
122 }
123
TEST_F(ProbeControllerTest,InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate)124 TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) {
125 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
126 kMaxBitrateBps, NowMs());
127 // Long enough to time out exponential probing.
128 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
129 probes = probe_controller_->SetEstimatedBitrate(kStartBitrateBps, NowMs());
130 probes = probe_controller_->Process(NowMs());
131 probes = probe_controller_->SetEstimatedBitrate(kMaxBitrateBps, NowMs());
132 probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
133 kMaxBitrateBps + 100, NowMs());
134 EXPECT_EQ(probes.size(), 1u);
135 EXPECT_EQ(probes[0].target_data_rate.bps(), kMaxBitrateBps + 100);
136 }
137
TEST_F(ProbeControllerTest,TestExponentialProbing)138 TEST_F(ProbeControllerTest, TestExponentialProbing) {
139 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
140 kMaxBitrateBps, NowMs());
141
142 // Repeated probe should only be sent when estimated bitrate climbs above
143 // 0.7 * 6 * kStartBitrateBps = 1260.
144 probes = probe_controller_->SetEstimatedBitrate(1000, NowMs());
145 EXPECT_EQ(probes.size(), 0u);
146
147 probes = probe_controller_->SetEstimatedBitrate(1800, NowMs());
148 EXPECT_EQ(probes.size(), 1u);
149 EXPECT_EQ(probes[0].target_data_rate.bps(), 2 * 1800);
150 }
151
TEST_F(ProbeControllerTest,TestExponentialProbingTimeout)152 TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
153 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
154 kMaxBitrateBps, NowMs());
155 // Advance far enough to cause a time out in waiting for probing result.
156 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
157 probes = probe_controller_->Process(NowMs());
158
159 probes = probe_controller_->SetEstimatedBitrate(1800, NowMs());
160 EXPECT_EQ(probes.size(), 0u);
161 }
162
TEST_F(ProbeControllerTest,RequestProbeInAlr)163 TEST_F(ProbeControllerTest, RequestProbeInAlr) {
164 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
165 kMaxBitrateBps, NowMs());
166 EXPECT_GE(probes.size(), 2u);
167 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
168
169 probe_controller_->SetAlrStartTimeMs(clock_.TimeInMilliseconds());
170 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
171 probes = probe_controller_->Process(NowMs());
172 probes = probe_controller_->SetEstimatedBitrate(250, NowMs());
173 probes = probe_controller_->RequestProbe(NowMs());
174
175 EXPECT_EQ(probes.size(), 1u);
176 EXPECT_EQ(probes[0].target_data_rate.bps(), 0.85 * 500);
177 }
178
TEST_F(ProbeControllerTest,RequestProbeWhenAlrEndedRecently)179 TEST_F(ProbeControllerTest, RequestProbeWhenAlrEndedRecently) {
180 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
181 kMaxBitrateBps, NowMs());
182 EXPECT_EQ(probes.size(), 2u);
183 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
184
185 probe_controller_->SetAlrStartTimeMs(absl::nullopt);
186 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
187 probes = probe_controller_->Process(NowMs());
188 probes = probe_controller_->SetEstimatedBitrate(250, NowMs());
189 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
190 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs - 1);
191 probes = probe_controller_->RequestProbe(NowMs());
192
193 EXPECT_EQ(probes.size(), 1u);
194 EXPECT_EQ(probes[0].target_data_rate.bps(), 0.85 * 500);
195 }
196
TEST_F(ProbeControllerTest,RequestProbeWhenAlrNotEndedRecently)197 TEST_F(ProbeControllerTest, RequestProbeWhenAlrNotEndedRecently) {
198 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
199 kMaxBitrateBps, NowMs());
200 EXPECT_EQ(probes.size(), 2u);
201 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
202
203 probe_controller_->SetAlrStartTimeMs(absl::nullopt);
204 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
205 probes = probe_controller_->Process(NowMs());
206 probes = probe_controller_->SetEstimatedBitrate(250, NowMs());
207 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
208 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs + 1);
209 probes = probe_controller_->RequestProbe(NowMs());
210 EXPECT_EQ(probes.size(), 0u);
211 }
212
TEST_F(ProbeControllerTest,RequestProbeWhenBweDropNotRecent)213 TEST_F(ProbeControllerTest, RequestProbeWhenBweDropNotRecent) {
214 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
215 kMaxBitrateBps, NowMs());
216 EXPECT_EQ(probes.size(), 2u);
217 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
218
219 probe_controller_->SetAlrStartTimeMs(clock_.TimeInMilliseconds());
220 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
221 probes = probe_controller_->Process(NowMs());
222 probes = probe_controller_->SetEstimatedBitrate(250, NowMs());
223 clock_.AdvanceTimeMilliseconds(kBitrateDropTimeoutMs + 1);
224 probes = probe_controller_->RequestProbe(NowMs());
225 EXPECT_EQ(probes.size(), 0u);
226 }
227
TEST_F(ProbeControllerTest,PeriodicProbing)228 TEST_F(ProbeControllerTest, PeriodicProbing) {
229 probe_controller_->EnablePeriodicAlrProbing(true);
230 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
231 kMaxBitrateBps, NowMs());
232 EXPECT_EQ(probes.size(), 2u);
233 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
234
235 int64_t start_time = clock_.TimeInMilliseconds();
236
237 // Expect the controller to send a new probe after 5s has passed.
238 probe_controller_->SetAlrStartTimeMs(start_time);
239 clock_.AdvanceTimeMilliseconds(5000);
240 probes = probe_controller_->Process(NowMs());
241 EXPECT_EQ(probes.size(), 1u);
242 EXPECT_EQ(probes[0].target_data_rate.bps(), 1000);
243
244 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
245
246 // The following probe should be sent at 10s into ALR.
247 probe_controller_->SetAlrStartTimeMs(start_time);
248 clock_.AdvanceTimeMilliseconds(4000);
249 probes = probe_controller_->Process(NowMs());
250 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
251 EXPECT_EQ(probes.size(), 0u);
252
253 probe_controller_->SetAlrStartTimeMs(start_time);
254 clock_.AdvanceTimeMilliseconds(1000);
255 probes = probe_controller_->Process(NowMs());
256 EXPECT_EQ(probes.size(), 1u);
257 probes = probe_controller_->SetEstimatedBitrate(500, NowMs());
258 EXPECT_EQ(probes.size(), 0u);
259 }
260
TEST_F(ProbeControllerTest,PeriodicProbingAfterReset)261 TEST_F(ProbeControllerTest, PeriodicProbingAfterReset) {
262 probe_controller_.reset(
263 new ProbeController(&field_trial_config_, &mock_rtc_event_log));
264 int64_t alr_start_time = clock_.TimeInMilliseconds();
265
266 probe_controller_->SetAlrStartTimeMs(alr_start_time);
267 probe_controller_->EnablePeriodicAlrProbing(true);
268 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
269 kMaxBitrateBps, NowMs());
270 probe_controller_->Reset(NowMs());
271
272 clock_.AdvanceTimeMilliseconds(10000);
273 probes = probe_controller_->Process(NowMs());
274 // Since bitrates are not yet set, no probe is sent event though we are in ALR
275 // mode.
276 EXPECT_EQ(probes.size(), 0u);
277
278 probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
279 kMaxBitrateBps, NowMs());
280 EXPECT_EQ(probes.size(), 2u);
281
282 // Make sure we use |kStartBitrateBps| as the estimated bitrate
283 // until SetEstimatedBitrate is called with an updated estimate.
284 clock_.AdvanceTimeMilliseconds(10000);
285 probes = probe_controller_->Process(NowMs());
286 EXPECT_EQ(probes.size(), 1u);
287 EXPECT_EQ(probes[0].target_data_rate.bps(), kStartBitrateBps * 2);
288 }
289
TEST_F(ProbeControllerTest,TestExponentialProbingOverflow)290 TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) {
291 const int64_t kMbpsMultiplier = 1000000;
292 auto probes = probe_controller_->SetBitrates(
293 kMinBitrateBps, 10 * kMbpsMultiplier, 100 * kMbpsMultiplier, NowMs());
294 // Verify that probe bitrate is capped at the specified max bitrate.
295 probes =
296 probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier, NowMs());
297 EXPECT_EQ(probes.size(), 1u);
298 EXPECT_EQ(probes[0].target_data_rate.bps(), 100 * kMbpsMultiplier);
299 // Verify that repeated probes aren't sent.
300 probes =
301 probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier, NowMs());
302 EXPECT_EQ(probes.size(), 0u);
303 }
304
TEST_F(ProbeControllerTest,TestAllocatedBitrateCap)305 TEST_F(ProbeControllerTest, TestAllocatedBitrateCap) {
306 const int64_t kMbpsMultiplier = 1000000;
307 const int64_t kMaxBitrateBps = 100 * kMbpsMultiplier;
308 auto probes = probe_controller_->SetBitrates(
309 kMinBitrateBps, 10 * kMbpsMultiplier, kMaxBitrateBps, NowMs());
310
311 // Configure ALR for periodic probing.
312 probe_controller_->EnablePeriodicAlrProbing(true);
313 int64_t alr_start_time = clock_.TimeInMilliseconds();
314 probe_controller_->SetAlrStartTimeMs(alr_start_time);
315
316 int64_t estimated_bitrate_bps = kMaxBitrateBps / 10;
317 probes =
318 probe_controller_->SetEstimatedBitrate(estimated_bitrate_bps, NowMs());
319
320 // Set a max allocated bitrate below the current estimate.
321 int64_t max_allocated_bps = estimated_bitrate_bps - 1 * kMbpsMultiplier;
322 probes =
323 probe_controller_->OnMaxTotalAllocatedBitrate(max_allocated_bps, NowMs());
324 EXPECT_TRUE(probes.empty()); // No probe since lower than current max.
325
326 // Probes such as ALR capped at 2x the max allocation limit.
327 clock_.AdvanceTimeMilliseconds(5000);
328 probes = probe_controller_->Process(NowMs());
329 EXPECT_EQ(probes.size(), 1u);
330 EXPECT_EQ(probes[0].target_data_rate.bps(), 2 * max_allocated_bps);
331
332 // Remove allocation limit.
333 EXPECT_TRUE(
334 probe_controller_->OnMaxTotalAllocatedBitrate(0, NowMs()).empty());
335 clock_.AdvanceTimeMilliseconds(5000);
336 probes = probe_controller_->Process(NowMs());
337 EXPECT_EQ(probes.size(), 1u);
338 EXPECT_EQ(probes[0].target_data_rate.bps(), estimated_bitrate_bps * 2);
339 }
340
TEST_F(ProbeControllerTest,ConfigurableProbingFieldTrial)341 TEST_F(ProbeControllerTest, ConfigurableProbingFieldTrial) {
342 test::ScopedFieldTrials trials(
343 "WebRTC-Bwe-ProbingConfiguration/"
344 "p1:2,p2:5,step_size:3,further_probe_threshold:0.8,"
345 "alloc_p1:2,alloc_p2/");
346 probe_controller_.reset(
347 new ProbeController(&field_trial_config_, &mock_rtc_event_log));
348 auto probes = probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
349 5000000, NowMs());
350 EXPECT_EQ(probes.size(), 2u);
351 EXPECT_EQ(probes[0].target_data_rate.bps(), 600);
352 EXPECT_EQ(probes[1].target_data_rate.bps(), 1500);
353
354 // Repeated probe should only be sent when estimated bitrate climbs above
355 // 0.8 * 5 * kStartBitrateBps = 1200.
356 probes = probe_controller_->SetEstimatedBitrate(1100, NowMs());
357 EXPECT_EQ(probes.size(), 0u);
358
359 probes = probe_controller_->SetEstimatedBitrate(1250, NowMs());
360 EXPECT_EQ(probes.size(), 1u);
361 EXPECT_EQ(probes[0].target_data_rate.bps(), 3 * 1250);
362
363 clock_.AdvanceTimeMilliseconds(5000);
364 probes = probe_controller_->Process(NowMs());
365
366 probe_controller_->SetAlrStartTimeMs(NowMs());
367 probes = probe_controller_->OnMaxTotalAllocatedBitrate(200000, NowMs());
368 EXPECT_EQ(probes.size(), 1u);
369 EXPECT_EQ(probes[0].target_data_rate.bps(), 400000);
370 }
371
372 } // namespace test
373 } // namespace webrtc
374