blob: ce2258bf5e0a37ad5d2850d4039e063ea0482c2e [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::_;
21using testing::NiceMock;
22using testing::Return;
23using testing::SaveArg;
24using testing::StrictMock;
25
26namespace webrtc {
27namespace test {
28
29class CongestionControllerTest : public ::testing::Test {
30 protected:
31 CongestionControllerTest() : clock_(123456) {}
32 ~CongestionControllerTest() override {}
33
34 void SetUp() override {
35 pacer_ = new NiceMock<MockPacedSender>();
36 std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership.
37 std::unique_ptr<PacketRouter> packet_router(new PacketRouter());
ivoc14d5dbe2016-07-04 07:06:55 -070038 controller_.reset(new CongestionController(
39 &clock_, &observer_, &remote_bitrate_observer_, &event_log_,
40 std::move(packet_router), std::move(pacer)));
perkjec81bcd2016-05-11 06:01:13 -070041 bandwidth_observer_.reset(
42 controller_->GetBitrateController()->CreateRtcpBandwidthObserver());
43
44 // Set the initial bitrate estimate and expect the |observer| and |pacer_|
45 // to be updated.
46 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
47 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
48 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps);
49 }
50
51 SimulatedClock clock_;
52 StrictMock<MockCongestionObserver> observer_;
53 NiceMock<MockPacedSender>* pacer_;
54 NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020055 NiceMock<MockRtcEventLog> event_log_;
perkjec81bcd2016-05-11 06:01:13 -070056 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
57 std::unique_ptr<CongestionController> controller_;
58 const uint32_t kInitialBitrateBps = 60000;
59};
60
61TEST_F(CongestionControllerTest, OnNetworkChanged) {
62 // Test no change.
63 clock_.AdvanceTimeMilliseconds(25);
64 controller_->Process();
65
66 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
67 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
68 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
69 clock_.AdvanceTimeMilliseconds(25);
70 controller_->Process();
71
72 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
73 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps));
74 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps);
75 clock_.AdvanceTimeMilliseconds(25);
76 controller_->Process();
77}
78
79TEST_F(CongestionControllerTest, OnSendQueueFull) {
80 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
81 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
82
83 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
84 controller_->Process();
85
86 // Let the pacer not be full next time the controller checks.
87 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
88 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
89
90 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
91 controller_->Process();
92}
93
94TEST_F(CongestionControllerTest, OnSendQueueFullAndEstimateChange) {
95 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
96 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
97 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
98 controller_->Process();
99
100 // Receive new estimate but let the queue still be full.
101 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
102 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
103 .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1));
104 // The send pacer should get the new estimate though.
105 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
106 clock_.AdvanceTimeMilliseconds(25);
107 controller_->Process();
108
109 // Let the pacer not be full next time the controller checks.
110 // |OnNetworkChanged| should be called with the new estimate.
111 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
112 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
113 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
114 clock_.AdvanceTimeMilliseconds(25);
115 controller_->Process();
116}
117
perkjfea93092016-05-14 00:58:48 -0700118TEST_F(CongestionControllerTest, SignalNetworkState) {
119 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
120 controller_->SignalNetworkState(kNetworkDown);
121
122 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _));
123 controller_->SignalNetworkState(kNetworkUp);
124
125 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
126 controller_->SignalNetworkState(kNetworkDown);
127}
128
honghaiz059e1832016-06-24 11:03:55 -0700129TEST_F(CongestionControllerTest, ResetBweAndBitrates) {
130 int new_bitrate = 200000;
131 EXPECT_CALL(observer_, OnNetworkChanged(new_bitrate, _, _));
132 EXPECT_CALL(*pacer_, SetEstimatedBitrate(new_bitrate));
133 controller_->ResetBweAndBitrates(new_bitrate, -1, -1);
134
135 // If the bitrate is reset to -1, the new starting bitrate will be
136 // the minimum default bitrate 10000bps.
137 int min_default_bitrate = 10000;
138 EXPECT_CALL(observer_, OnNetworkChanged(min_default_bitrate, _, _));
139 EXPECT_CALL(*pacer_, SetEstimatedBitrate(min_default_bitrate));
140 controller_->ResetBweAndBitrates(-1, -1, -1);
141}
142
perkjfea93092016-05-14 00:58:48 -0700143TEST_F(CongestionControllerTest,
144 SignalNetworkStateAndQueueIsFullAndEstimateChange) {
145 // Send queue is full
146 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
147 .WillRepeatedly(Return(PacedSender::kMaxQueueLengthMs + 1));
148 EXPECT_CALL(observer_, OnNetworkChanged(0, _, _));
149 controller_->Process();
150
151 // Queue is full and network is down. Expect no bitrate change.
152 controller_->SignalNetworkState(kNetworkDown);
153 controller_->Process();
154
155 // Queue is full but network is up. Expect no bitrate change.
156 controller_->SignalNetworkState(kNetworkUp);
157 controller_->Process();
158
159 // Receive new estimate but let the queue still be full.
160 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2));
161 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2);
162 clock_.AdvanceTimeMilliseconds(25);
163 controller_->Process();
164
165 // Let the pacer not be full next time the controller checks.
166 EXPECT_CALL(*pacer_, ExpectedQueueTimeMs())
167 .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1));
168 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _));
169 controller_->Process();
170}
171
perkjec81bcd2016-05-11 06:01:13 -0700172} // namespace test
173} // namespace webrtc