blob: e0a040f3a1fa2852e45c9f9af5180ffcaab7bf6e [file] [log] [blame]
Irfan Sheriffb2540bb2016-09-12 12:28:54 -07001/*
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 <memory>
11
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070012#include "webrtc/modules/congestion_controller/probe_controller.h"
13#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020014#include "webrtc/rtc_base/logging.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070015#include "webrtc/system_wrappers/include/clock.h"
kwibergac9f8762016-09-30 22:29:43 -070016#include "webrtc/test/gmock.h"
17#include "webrtc/test/gtest.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070018
19using testing::_;
20using testing::AtLeast;
21using testing::NiceMock;
sergeyu80ed35e2016-11-28 13:11:13 -080022using testing::Return;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070023
24namespace webrtc {
25namespace test {
26
27namespace {
28
29constexpr int kMinBitrateBps = 100;
30constexpr int kStartBitrateBps = 300;
sergeyu07c147d2016-11-03 11:59:50 -070031constexpr int kMaxBitrateBps = 10000;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070032
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070033constexpr int kExponentialProbingTimeoutMs = 5000;
34
sergeyu80ed35e2016-11-28 13:11:13 -080035constexpr int kAlrProbeInterval = 5000;
terelius3376c842017-07-31 04:23:25 -070036constexpr int kAlrEndedTimeoutMs = 3000;
37constexpr int kBitrateDropTimeoutMs = 5000;
sergeyu80ed35e2016-11-28 13:11:13 -080038
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070039} // namespace
40
41class ProbeControllerTest : public ::testing::Test {
42 protected:
sergeyu80ed35e2016-11-28 13:11:13 -080043 ProbeControllerTest() : clock_(100000000L) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070044 probe_controller_.reset(new ProbeController(&pacer_, &clock_));
45 }
46 ~ProbeControllerTest() override {}
47
48 SimulatedClock clock_;
49 NiceMock<MockPacedSender> pacer_;
50 std::unique_ptr<ProbeController> probe_controller_;
51};
52
53TEST_F(ProbeControllerTest, InitiatesProbingAtStart) {
philipelfd58b612017-01-04 07:05:25 -080054 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070055 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
56 kMaxBitrateBps);
57}
58
Sergey Ulanove2b15012016-11-22 16:08:30 -080059TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) {
60 probe_controller_->OnNetworkStateChanged(kNetworkDown);
philipelfd58b612017-01-04 07:05:25 -080061 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
Sergey Ulanove2b15012016-11-22 16:08:30 -080062 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
63 kMaxBitrateBps);
64
65 testing::Mock::VerifyAndClearExpectations(&pacer_);
philipelfd58b612017-01-04 07:05:25 -080066 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Sergey Ulanove2b15012016-11-22 16:08:30 -080067 probe_controller_->OnNetworkStateChanged(kNetworkUp);
68}
69
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070070TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) {
philipelfd58b612017-01-04 07:05:25 -080071 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070072 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
73 kMaxBitrateBps);
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070074 // Long enough to time out exponential probing.
75 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070076 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
sergeyu9cef11b2016-12-02 11:03:01 -080077 probe_controller_->Process();
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070078
philipelfd58b612017-01-04 07:05:25 -080079 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070080 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
81 kMaxBitrateBps + 100);
82}
83
philipelda5e9d02017-01-17 02:08:28 -080084TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) {
85 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
86 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
87 kMaxBitrateBps);
88 // Long enough to time out exponential probing.
89 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
90 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
91 probe_controller_->Process();
92
93 probe_controller_->SetEstimatedBitrate(kMaxBitrateBps);
94 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
95 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
96 kMaxBitrateBps + 100);
97}
98
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070099TEST_F(ProbeControllerTest, TestExponentialProbing) {
100 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
101 kMaxBitrateBps);
sergeyu07c147d2016-11-03 11:59:50 -0700102
sergeyu5bc39452016-12-15 10:42:09 -0800103 // Repeated probe should only be sent when estimated bitrate climbs above
104 // 0.7 * 6 * kStartBitrateBps = 1260.
philipelfd58b612017-01-04 07:05:25 -0800105 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu9cef11b2016-12-02 11:03:01 -0800106 probe_controller_->SetEstimatedBitrate(1000);
107 testing::Mock::VerifyAndClearExpectations(&pacer_);
108
philipelfd58b612017-01-04 07:05:25 -0800109 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700110 probe_controller_->SetEstimatedBitrate(1800);
111}
112
113TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
114 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
115 kMaxBitrateBps);
116
117 // Advance far enough to cause a time out in waiting for probing result.
Irfan Sheriff9b7b7532016-09-16 11:30:44 -0700118 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
sergeyu9cef11b2016-12-02 11:03:01 -0800119 probe_controller_->Process();
120
philipelfd58b612017-01-04 07:05:25 -0800121 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700122 probe_controller_->SetEstimatedBitrate(1800);
123}
124
terelius3376c842017-07-31 04:23:25 -0700125TEST_F(ProbeControllerTest, RequestProbeInAlr) {
philipelfd58b612017-01-04 07:05:25 -0800126 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800127 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
128 kMaxBitrateBps);
129 probe_controller_->SetEstimatedBitrate(500);
130 testing::Mock::VerifyAndClearExpectations(&pacer_);
terelius3376c842017-07-31 04:23:25 -0700131 EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800132 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
133 .WillRepeatedly(
134 Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds())));
135 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
sergeyu9cef11b2016-12-02 11:03:01 -0800136 probe_controller_->Process();
terelius3376c842017-07-31 04:23:25 -0700137 probe_controller_->SetEstimatedBitrate(250);
138 probe_controller_->RequestProbe();
139}
140
141TEST_F(ProbeControllerTest, RequestProbeWhenAlrEndedRecently) {
142 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
143 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
144 kMaxBitrateBps);
145 probe_controller_->SetEstimatedBitrate(500);
146 testing::Mock::VerifyAndClearExpectations(&pacer_);
147 EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1);
148 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
149 .WillRepeatedly(Return(rtc::Optional<int64_t>()));
150 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
151 probe_controller_->Process();
152 probe_controller_->SetEstimatedBitrate(250);
153 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
154 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs - 1);
155 probe_controller_->RequestProbe();
156}
157
158TEST_F(ProbeControllerTest, RequestProbeWhenAlrNotEndedRecently) {
159 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
160 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
161 kMaxBitrateBps);
162 probe_controller_->SetEstimatedBitrate(500);
163 testing::Mock::VerifyAndClearExpectations(&pacer_);
164 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
165 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
166 .WillRepeatedly(Return(rtc::Optional<int64_t>()));
167 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
168 probe_controller_->Process();
169 probe_controller_->SetEstimatedBitrate(250);
170 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
171 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs + 1);
172 probe_controller_->RequestProbe();
173}
174
175TEST_F(ProbeControllerTest, RequestProbeWhenBweDropNotRecent) {
176 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
177 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
178 kMaxBitrateBps);
179 probe_controller_->SetEstimatedBitrate(500);
180 testing::Mock::VerifyAndClearExpectations(&pacer_);
181 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
182 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
183 .WillRepeatedly(
184 Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds())));
185 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
186 probe_controller_->Process();
187 probe_controller_->SetEstimatedBitrate(250);
188 clock_.AdvanceTimeMilliseconds(kBitrateDropTimeoutMs + 1);
189 probe_controller_->RequestProbe();
sergeyu80ed35e2016-11-28 13:11:13 -0800190}
191
192TEST_F(ProbeControllerTest, PeriodicProbing) {
philipelfd58b612017-01-04 07:05:25 -0800193 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800194 probe_controller_->EnablePeriodicAlrProbing(true);
195 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
196 kMaxBitrateBps);
197 probe_controller_->SetEstimatedBitrate(500);
198 testing::Mock::VerifyAndClearExpectations(&pacer_);
199
200 int64_t start_time = clock_.TimeInMilliseconds();
201
202 // Expect the controller to send a new probe after 5s has passed.
philipelfd58b612017-01-04 07:05:25 -0800203 EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800204 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
205 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
206 clock_.AdvanceTimeMilliseconds(5000);
207 probe_controller_->Process();
208 probe_controller_->SetEstimatedBitrate(500);
209 testing::Mock::VerifyAndClearExpectations(&pacer_);
210
211 // The following probe should be sent at 10s into ALR.
philipelfd58b612017-01-04 07:05:25 -0800212 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu80ed35e2016-11-28 13:11:13 -0800213 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
214 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
215 clock_.AdvanceTimeMilliseconds(4000);
216 probe_controller_->Process();
217 probe_controller_->SetEstimatedBitrate(500);
218 testing::Mock::VerifyAndClearExpectations(&pacer_);
219
philipelfd58b612017-01-04 07:05:25 -0800220 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800221 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
222 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
223 clock_.AdvanceTimeMilliseconds(1000);
224 probe_controller_->Process();
225 probe_controller_->SetEstimatedBitrate(500);
226 testing::Mock::VerifyAndClearExpectations(&pacer_);
227}
228
philipeld1d247f2017-05-04 08:35:52 -0700229TEST_F(ProbeControllerTest, PeriodicProbingAfterReset) {
230 testing::StrictMock<MockPacedSender> local_pacer;
231 probe_controller_.reset(new ProbeController(&local_pacer, &clock_));
232 int64_t alr_start_time = clock_.TimeInMilliseconds();
233 EXPECT_CALL(local_pacer, GetApplicationLimitedRegionStartTime())
234 .WillRepeatedly(
235 Return(rtc::Optional<int64_t>(alr_start_time)));
236
237 EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2);
238 probe_controller_->EnablePeriodicAlrProbing(true);
239 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
240 kMaxBitrateBps);
241 probe_controller_->Reset();
242
243 clock_.AdvanceTimeMilliseconds(10000);
244 probe_controller_->Process();
245
246 EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2);
247 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
248 kMaxBitrateBps);
249
250 // Make sure we use |kStartBitrateBps| as the estimated bitrate
251 // until SetEstimatedBitrate is called with an updated estimate.
252 clock_.AdvanceTimeMilliseconds(10000);
253 EXPECT_CALL(local_pacer, CreateProbeCluster(kStartBitrateBps*2));
254 probe_controller_->Process();
255}
256
sergeyu5bc39452016-12-15 10:42:09 -0800257TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) {
258 const int64_t kMbpsMultiplier = 1000000;
259 probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier,
260 100 * kMbpsMultiplier);
261
262 // Verify that probe bitrate is capped at the specified max bitrate
philipelfd58b612017-01-04 07:05:25 -0800263 EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier));
sergeyu5bc39452016-12-15 10:42:09 -0800264 probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier);
265 testing::Mock::VerifyAndClearExpectations(&pacer_);
266
267 // Verify that repeated probes aren't sent.
philipelfd58b612017-01-04 07:05:25 -0800268 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu5bc39452016-12-15 10:42:09 -0800269 probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier);
270}
271
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700272} // namespace test
273} // namespace webrtc