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