blob: bdbaaf1cce8d39bf9e1a3ce559c278ea04beaf59 [file] [log] [blame]
perkjec81bcd2016-05-11 06:01:13 -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
terelius2d81eb32016-10-25 07:04:37 -070011#include "webrtc/logging/rtc_event_log/mock/mock_rtc_event_log.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070012#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkjec81bcd2016-05-11 06:01:13 -070013#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
14#include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h"
kwibergac9f8762016-09-30 22:29:43 -070015#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
michaeltf082c2a2016-11-07 04:17:14 -080016#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
perkjec81bcd2016-05-11 06:01:13 -070017#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_observer.h"
18#include "webrtc/system_wrappers/include/clock.h"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/test/gmock.h"
20#include "webrtc/test/gtest.h"
perkjec81bcd2016-05-11 06:01:13 -070021
22using testing::_;
asapersson14f12502016-09-07 23:14:50 -070023using testing::AtLeast;
perkjec81bcd2016-05-11 06:01:13 -070024using testing::NiceMock;
25using testing::Return;
26using testing::SaveArg;
27using testing::StrictMock;
28
29namespace webrtc {
30namespace test {
31
32class CongestionControllerTest : public ::testing::Test {
33 protected:
34 CongestionControllerTest() : clock_(123456) {}
35 ~CongestionControllerTest() override {}
36
37 void SetUp() override {
38 pacer_ = new NiceMock<MockPacedSender>();
39 std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership.
ivoc14d5dbe2016-07-04 07:06:55 -070040 controller_.reset(new CongestionController(
41 &clock_, &observer_, &remote_bitrate_observer_, &event_log_,
nisse0245da02016-11-30 03:35:20 -080042 &packet_router_, std::move(pacer)));
perkjec81bcd2016-05-11 06:01:13 -070043 bandwidth_observer_.reset(
44 controller_->GetBitrateController()->CreateRtcpBandwidthObserver());
45
46 // Set the initial bitrate estimate and expect the |observer| and |pacer_|
47 // to be updated.
minyue78b4d562016-11-30 04:47:39 -080048 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -070049 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
50 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps);
51 }
52
53 SimulatedClock clock_;
54 StrictMock<MockCongestionObserver> observer_;
55 NiceMock<MockPacedSender>* pacer_;
56 NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020057 NiceMock<MockRtcEventLog> event_log_;
perkjec81bcd2016-05-11 06:01:13 -070058 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
nisse0245da02016-11-30 03:35:20 -080059 PacketRouter packet_router_;
perkjec81bcd2016-05-11 06:01:13 -070060 std::unique_ptr<CongestionController> controller_;
61 const uint32_t kInitialBitrateBps = 60000;
62};
63
64TEST_F(CongestionControllerTest, OnNetworkChanged) {
65 // Test no change.
66 clock_.AdvanceTimeMilliseconds(25);
67 controller_->Process();
68
minyue78b4d562016-11-30 04:47:39 -080069 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -070070 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
71 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
72 clock_.AdvanceTimeMilliseconds(25);
73 controller_->Process();
74
minyue78b4d562016-11-30 04:47:39 -080075 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -070076 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
77 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps);
78 clock_.AdvanceTimeMilliseconds(25);
79 controller_->Process();
80}
81
82TEST_F(CongestionControllerTest, OnSendQueueFull) {
83 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
84 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
85
minyue78b4d562016-11-30 04:47:39 -080086 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -070087 controller_->Process();
88
89 // Let the pacer not be full next time the controller checks.
90 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
91 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
92
minyue78b4d562016-11-30 04:47:39 -080093 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -070094 controller_->Process();
95}
96
97TEST_F(CongestionControllerTest, OnSendQueueFullAndEstimateChange) {
98 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
99 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
minyue78b4d562016-11-30 04:47:39 -0800100 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -0700101 controller_->Process();
102
103 // Receive new estimate but let the queue still be full.
104 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
105 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
106 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
107 // The send pacer should get the new estimate though.
108 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
109 clock_.AdvanceTimeMilliseconds(25);
110 controller_->Process();
111
112 // Let the pacer not be full next time the controller checks.
113 // |OnNetworkChanged| should be called with the new estimate.
114 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
115 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
minyue78b4d562016-11-30 04:47:39 -0800116 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _));
perkjec81bcd2016-05-11 06:01:13 -0700117 clock_.AdvanceTimeMilliseconds(25);
118 controller_->Process();
119}
120
perkjfea93092016-05-14 00:58:48 -0700121TEST_F(CongestionControllerTest, SignalNetworkState) {
minyue78b4d562016-11-30 04:47:39 -0800122 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _));
perkjfea93092016-05-14 00:58:48 -0700123 controller_->SignalNetworkState(kNetworkDown);
124
minyue78b4d562016-11-30 04:47:39 -0800125 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _));
perkjfea93092016-05-14 00:58:48 -0700126 controller_->SignalNetworkState(kNetworkUp);
127
minyue78b4d562016-11-30 04:47:39 -0800128 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _));
perkjfea93092016-05-14 00:58:48 -0700129 controller_->SignalNetworkState(kNetworkDown);
130}
131
honghaiz059e1832016-06-24 11:03:55 -0700132TEST_F(CongestionControllerTest, ResetBweAndBitrates) {
133 int new_bitrate = 200000;
minyue78b4d562016-11-30 04:47:39 -0800134 EXPECT_CALL(observer_, OnNetworkChanged(new_bitrate, _, _, _));
honghaiz059e1832016-06-24 11:03:55 -0700135 EXPECT_CALL(*pacer_, SetEstimatedBitrate(new_bitrate));
136 controller_->ResetBweAndBitrates(new_bitrate, -1, -1);
137
138 // If the bitrate is reset to -1, the new starting bitrate will be
michaeltf082c2a2016-11-07 04:17:14 -0800139 // the minimum default bitrate kMinBitrateBps.
minyue78b4d562016-11-30 04:47:39 -0800140 EXPECT_CALL(
141 observer_,
142 OnNetworkChanged(congestion_controller::GetMinBitrateBps(), _, _, _));
michaeltf082c2a2016-11-07 04:17:14 -0800143 EXPECT_CALL(*pacer_,
144 SetEstimatedBitrate(congestion_controller::GetMinBitrateBps()));
honghaiz059e1832016-06-24 11:03:55 -0700145 controller_->ResetBweAndBitrates(-1, -1, -1);
146}
147
perkjfea93092016-05-14 00:58:48 -0700148TEST_F(CongestionControllerTest,
149 SignalNetworkStateAndQueueIsFullAndEstimateChange) {
150 // Send queue is full
151 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
152 .WillRepeatedly(Return(PacedSender::kMaxQueueLengthMs + 1));
minyue78b4d562016-11-30 04:47:39 -0800153 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _));
perkjfea93092016-05-14 00:58:48 -0700154 controller_->Process();
155
156 // Queue is full and network is down. Expect no bitrate change.
157 controller_->SignalNetworkState(kNetworkDown);
158 controller_->Process();
159
160 // Queue is full but network is up. Expect no bitrate change.
161 controller_->SignalNetworkState(kNetworkUp);
162 controller_->Process();
163
164 // Receive new estimate but let the queue still be full.
165 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
166 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
167 clock_.AdvanceTimeMilliseconds(25);
168 controller_->Process();
169
170 // Let the pacer not be full next time the controller checks.
171 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
172 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
minyue78b4d562016-11-30 04:47:39 -0800173 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _));
perkjfea93092016-05-14 00:58:48 -0700174 controller_->Process();
175}
176
asapersson14f12502016-09-07 23:14:50 -0700177TEST_F(CongestionControllerTest, GetPacerQueuingDelayMs) {
minyue78b4d562016-11-30 04:47:39 -0800178 EXPECT_CALL(observer_, OnNetworkChanged(_, _, _, _)).Times(AtLeast(1));
asapersson14f12502016-09-07 23:14:50 -0700179
180 const int64_t kQueueTimeMs = 123;
181 EXPECT_CALL(*pacer_, QueueInMs()).WillRepeatedly(Return(kQueueTimeMs));
182 EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs());
183
184 // Expect zero pacer delay when network is down.
185 controller_->SignalNetworkState(kNetworkDown);
186 EXPECT_EQ(0, controller_->GetPacerQueuingDelayMs());
187
188 // Network is up, pacer delay should be reported.
189 controller_->SignalNetworkState(kNetworkUp);
190 EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs());
191}
192
minyue78b4d562016-11-30 04:47:39 -0800193TEST_F(CongestionControllerTest, GetProbingInterval) {
194 clock_.AdvanceTimeMilliseconds(25);
195 controller_->Process();
196
197 EXPECT_CALL(observer_, OnNetworkChanged(_, _, _, testing::Ne(0)));
198 EXPECT_CALL(*pacer_, SetEstimatedBitrate(_));
199 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
200 clock_.AdvanceTimeMilliseconds(25);
201 controller_->Process();
202}
203
perkjec81bcd2016-05-11 06:01:13 -0700204} // namespace test
205} // namespace webrtc