blob: 53ccf3dda1d21c1c8f16be14522348181b4bf9b0 [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
11#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
ivoc14d5dbe2016-07-04 07:06:55 -070013#include "webrtc/call/mock/mock_rtc_event_log.h"
perkjec81bcd2016-05-11 06:01:13 -070014#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
15#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
16#include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h"
17#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_observer.h"
18#include "webrtc/system_wrappers/include/clock.h"
19
20using testing::_;
asapersson14f12502016-09-07 23:14:50 -070021using testing::AtLeast;
perkjec81bcd2016-05-11 06:01:13 -070022using testing::NiceMock;
23using testing::Return;
24using testing::SaveArg;
25using testing::StrictMock;
26
27namespace webrtc {
28namespace test {
29
30class CongestionControllerTest : public ::testing::Test {
31 protected:
32 CongestionControllerTest() : clock_(123456) {}
33 ~CongestionControllerTest() override {}
34
35 void SetUp() override {
36 pacer_ = new NiceMock<MockPacedSender>();
37 std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership.
38 std::unique_ptr<PacketRouter> packet_router(new PacketRouter());
ivoc14d5dbe2016-07-04 07:06:55 -070039 controller_.reset(new CongestionController(
40 &clock_, &observer_, &remote_bitrate_observer_, &event_log_,
41 std::move(packet_router), std::move(pacer)));
perkjec81bcd2016-05-11 06:01:13 -070042 bandwidth_observer_.reset(
43 controller_->GetBitrateController()->CreateRtcpBandwidthObserver());
44
45 // Set the initial bitrate estimate and expect the |observer| and |pacer_|
46 // to be updated.
47 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
48 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
49 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps);
50 }
51
52 SimulatedClock clock_;
53 StrictMock<MockCongestionObserver> observer_;
54 NiceMock<MockPacedSender>* pacer_;
55 NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020056 NiceMock<MockRtcEventLog> event_log_;
perkjec81bcd2016-05-11 06:01:13 -070057 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
58 std::unique_ptr<CongestionController> controller_;
59 const uint32_t kInitialBitrateBps = 60000;
60};
61
62TEST_F(CongestionControllerTest, OnNetworkChanged) {
63 // Test no change.
64 clock_.AdvanceTimeMilliseconds(25);
65 controller_->Process();
66
67 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
68 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
69 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
70 clock_.AdvanceTimeMilliseconds(25);
71 controller_->Process();
72
73 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
74 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
75 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps);
76 clock_.AdvanceTimeMilliseconds(25);
77 controller_->Process();
78}
79
80TEST_F(CongestionControllerTest, OnSendQueueFull) {
81 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
82 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
83
84 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
85 controller_->Process();
86
87 // Let the pacer not be full next time the controller checks.
88 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
89 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
90
91 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
92 controller_->Process();
93}
94
95TEST_F(CongestionControllerTest, OnSendQueueFullAndEstimateChange) {
96 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
97 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
98 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
99 controller_->Process();
100
101 // Receive new estimate but let the queue still be full.
102 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
103 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
104 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
105 // The send pacer should get the new estimate though.
106 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
107 clock_.AdvanceTimeMilliseconds(25);
108 controller_->Process();
109
110 // Let the pacer not be full next time the controller checks.
111 // |OnNetworkChanged| should be called with the new estimate.
112 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
113 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
114 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
115 clock_.AdvanceTimeMilliseconds(25);
116 controller_->Process();
117}
118
perkjfea93092016-05-14 00:58:48 -0700119TEST_F(CongestionControllerTest, SignalNetworkState) {
120 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
121 controller_->SignalNetworkState(kNetworkDown);
122
123 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
124 controller_->SignalNetworkState(kNetworkUp);
125
126 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
127 controller_->SignalNetworkState(kNetworkDown);
128}
129
honghaiz059e1832016-06-24 11:03:55 -0700130TEST_F(CongestionControllerTest, ResetBweAndBitrates) {
131 int new_bitrate = 200000;
132 EXPECT_CALL(observer_, OnNetworkChanged(new_bitrate, _, _));
133 EXPECT_CALL(*pacer_, SetEstimatedBitrate(new_bitrate));
134 controller_->ResetBweAndBitrates(new_bitrate, -1, -1);
135
136 // If the bitrate is reset to -1, the new starting bitrate will be
137 // the minimum default bitrate 10000bps.
138 int min_default_bitrate = 10000;
139 EXPECT_CALL(observer_, OnNetworkChanged(min_default_bitrate, _, _));
140 EXPECT_CALL(*pacer_, SetEstimatedBitrate(min_default_bitrate));
141 controller_->ResetBweAndBitrates(-1, -1, -1);
142}
143
perkjfea93092016-05-14 00:58:48 -0700144TEST_F(CongestionControllerTest,
145 SignalNetworkStateAndQueueIsFullAndEstimateChange) {
146 // Send queue is full
147 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
148 .WillRepeatedly(Return(PacedSender::kMaxQueueLengthMs + 1));
149 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
150 controller_->Process();
151
152 // Queue is full and network is down. Expect no bitrate change.
153 controller_->SignalNetworkState(kNetworkDown);
154 controller_->Process();
155
156 // Queue is full but network is up. Expect no bitrate change.
157 controller_->SignalNetworkState(kNetworkUp);
158 controller_->Process();
159
160 // Receive new estimate but let the queue still be full.
161 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
162 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
163 clock_.AdvanceTimeMilliseconds(25);
164 controller_->Process();
165
166 // Let the pacer not be full next time the controller checks.
167 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
168 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
169 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
170 controller_->Process();
171}
172
asapersson14f12502016-09-07 23:14:50 -0700173TEST_F(CongestionControllerTest, GetPacerQueuingDelayMs) {
174 EXPECT_CALL(observer_, OnNetworkChanged(_, _, _)).Times(AtLeast(1));
175
176 const int64_t kQueueTimeMs = 123;
177 EXPECT_CALL(*pacer_, QueueInMs()).WillRepeatedly(Return(kQueueTimeMs));
178 EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs());
179
180 // Expect zero pacer delay when network is down.
181 controller_->SignalNetworkState(kNetworkDown);
182 EXPECT_EQ(0, controller_->GetPacerQueuingDelayMs());
183
184 // Network is up, pacer delay should be reported.
185 controller_->SignalNetworkState(kNetworkUp);
186 EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs());
187}
188
perkjec81bcd2016-05-11 06:01:13 -0700189} // namespace test
190} // namespace webrtc