blob: 19a9039adafa111aee4591281b978f09ce80d915 [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/base/logging.h"
13#include "webrtc/modules/congestion_controller/probe_controller.h"
14#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
15#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;
36
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070037} // namespace
38
39class ProbeControllerTest : public ::testing::Test {
40 protected:
sergeyu80ed35e2016-11-28 13:11:13 -080041 ProbeControllerTest() : clock_(100000000L) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070042 probe_controller_.reset(new ProbeController(&pacer_, &clock_));
43 }
44 ~ProbeControllerTest() override {}
45
46 SimulatedClock clock_;
47 NiceMock<MockPacedSender> pacer_;
48 std::unique_ptr<ProbeController> probe_controller_;
49};
50
51TEST_F(ProbeControllerTest, InitiatesProbingAtStart) {
philipelfd58b612017-01-04 07:05:25 -080052 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070053 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
54 kMaxBitrateBps);
55}
56
Sergey Ulanove2b15012016-11-22 16:08:30 -080057TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) {
58 probe_controller_->OnNetworkStateChanged(kNetworkDown);
philipelfd58b612017-01-04 07:05:25 -080059 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
Sergey Ulanove2b15012016-11-22 16:08:30 -080060 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
61 kMaxBitrateBps);
62
63 testing::Mock::VerifyAndClearExpectations(&pacer_);
philipelfd58b612017-01-04 07:05:25 -080064 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Sergey Ulanove2b15012016-11-22 16:08:30 -080065 probe_controller_->OnNetworkStateChanged(kNetworkUp);
66}
67
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070068TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) {
philipelfd58b612017-01-04 07:05:25 -080069 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070070 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
71 kMaxBitrateBps);
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070072 // Long enough to time out exponential probing.
73 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070074 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
sergeyu9cef11b2016-12-02 11:03:01 -080075 probe_controller_->Process();
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070076
philipelfd58b612017-01-04 07:05:25 -080077 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070078 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
79 kMaxBitrateBps + 100);
80}
81
philipelda5e9d02017-01-17 02:08:28 -080082TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) {
83 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
84 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
85 kMaxBitrateBps);
86 // Long enough to time out exponential probing.
87 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
88 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
89 probe_controller_->Process();
90
91 probe_controller_->SetEstimatedBitrate(kMaxBitrateBps);
92 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
93 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
94 kMaxBitrateBps + 100);
95}
96
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070097TEST_F(ProbeControllerTest, TestExponentialProbing) {
98 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
99 kMaxBitrateBps);
sergeyu07c147d2016-11-03 11:59:50 -0700100
sergeyu5bc39452016-12-15 10:42:09 -0800101 // Repeated probe should only be sent when estimated bitrate climbs above
102 // 0.7 * 6 * kStartBitrateBps = 1260.
philipelfd58b612017-01-04 07:05:25 -0800103 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu9cef11b2016-12-02 11:03:01 -0800104 probe_controller_->SetEstimatedBitrate(1000);
105 testing::Mock::VerifyAndClearExpectations(&pacer_);
106
philipelfd58b612017-01-04 07:05:25 -0800107 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700108 probe_controller_->SetEstimatedBitrate(1800);
109}
110
111TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
112 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
113 kMaxBitrateBps);
114
115 // Advance far enough to cause a time out in waiting for probing result.
Irfan Sheriff9b7b7532016-09-16 11:30:44 -0700116 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
sergeyu9cef11b2016-12-02 11:03:01 -0800117 probe_controller_->Process();
118
philipelfd58b612017-01-04 07:05:25 -0800119 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700120 probe_controller_->SetEstimatedBitrate(1800);
121}
122
sergeyu80ed35e2016-11-28 13:11:13 -0800123TEST_F(ProbeControllerTest, ProbeAfterEstimateDropInAlr) {
philipelfd58b612017-01-04 07:05:25 -0800124 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800125 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
126 kMaxBitrateBps);
127 probe_controller_->SetEstimatedBitrate(500);
128 testing::Mock::VerifyAndClearExpectations(&pacer_);
129
130 // When bandwidth estimate drops the controller should send a probe at the
131 // previous bitrate.
philipelfd58b612017-01-04 07:05:25 -0800132 EXPECT_CALL(pacer_, CreateProbeCluster(500)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800133 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
134 .WillRepeatedly(
135 Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds())));
136 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
sergeyu9cef11b2016-12-02 11:03:01 -0800137 probe_controller_->Process();
sergeyu80ed35e2016-11-28 13:11:13 -0800138 probe_controller_->SetEstimatedBitrate(50);
139}
140
141TEST_F(ProbeControllerTest, PeriodicProbing) {
philipelfd58b612017-01-04 07:05:25 -0800142 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800143 probe_controller_->EnablePeriodicAlrProbing(true);
144 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
145 kMaxBitrateBps);
146 probe_controller_->SetEstimatedBitrate(500);
147 testing::Mock::VerifyAndClearExpectations(&pacer_);
148
149 int64_t start_time = clock_.TimeInMilliseconds();
150
151 // Expect the controller to send a new probe after 5s has passed.
philipelfd58b612017-01-04 07:05:25 -0800152 EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800153 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
154 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
155 clock_.AdvanceTimeMilliseconds(5000);
156 probe_controller_->Process();
157 probe_controller_->SetEstimatedBitrate(500);
158 testing::Mock::VerifyAndClearExpectations(&pacer_);
159
160 // The following probe should be sent at 10s into ALR.
philipelfd58b612017-01-04 07:05:25 -0800161 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu80ed35e2016-11-28 13:11:13 -0800162 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
163 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
164 clock_.AdvanceTimeMilliseconds(4000);
165 probe_controller_->Process();
166 probe_controller_->SetEstimatedBitrate(500);
167 testing::Mock::VerifyAndClearExpectations(&pacer_);
168
philipelfd58b612017-01-04 07:05:25 -0800169 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1);
sergeyu80ed35e2016-11-28 13:11:13 -0800170 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
171 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
172 clock_.AdvanceTimeMilliseconds(1000);
173 probe_controller_->Process();
174 probe_controller_->SetEstimatedBitrate(500);
175 testing::Mock::VerifyAndClearExpectations(&pacer_);
176}
177
sergeyu5bc39452016-12-15 10:42:09 -0800178TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) {
179 const int64_t kMbpsMultiplier = 1000000;
180 probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier,
181 100 * kMbpsMultiplier);
182
183 // Verify that probe bitrate is capped at the specified max bitrate
philipelfd58b612017-01-04 07:05:25 -0800184 EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier));
sergeyu5bc39452016-12-15 10:42:09 -0800185 probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier);
186 testing::Mock::VerifyAndClearExpectations(&pacer_);
187
188 // Verify that repeated probes aren't sent.
philipelfd58b612017-01-04 07:05:25 -0800189 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
sergeyu5bc39452016-12-15 10:42:09 -0800190 probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier);
191}
192
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700193} // namespace test
194} // namespace webrtc