blob: 040cfec38474f63a49a2ebcc92f92494afbbc73a [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
2 * Copyright (c) 2012 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
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000011#include "testing/gtest/include/gtest/gtest.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000012
13#include <algorithm>
14#include <vector>
15
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000016#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
17#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000018
19using webrtc::RtcpBandwidthObserver;
20using webrtc::BitrateObserver;
21using webrtc::BitrateController;
22
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000023uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1,
24 int num_packets2, uint8_t fraction_loss2) {
25 int weighted_sum = num_packets1 * fraction_loss1 +
26 num_packets2 * fraction_loss2;
27 int total_num_packets = num_packets1 + num_packets2;
28 return (weighted_sum + total_num_packets / 2) / total_num_packets;
29}
30
31webrtc::RTCPReportBlock CreateReportBlock(
32 uint32_t remote_ssrc, uint32_t source_ssrc,
33 uint8_t fraction_lost, uint32_t extended_high_sequence_number) {
34 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
35 extended_high_sequence_number, 0, 0, 0);
36}
37
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000038class TestBitrateObserver: public BitrateObserver {
39 public:
40 TestBitrateObserver()
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000041 : last_bitrate_(0),
42 last_fraction_loss_(0),
43 last_rtt_(0) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000044 }
45
46 virtual void OnNetworkChanged(const uint32_t bitrate,
47 const uint8_t fraction_loss,
48 const uint32_t rtt) {
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000049 last_bitrate_ = bitrate;
50 last_fraction_loss_ = fraction_loss;
51 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000052 }
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000053 uint32_t last_bitrate_;
54 uint8_t last_fraction_loss_;
55 uint32_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000056};
57
58class BitrateControllerTest : public ::testing::Test {
59 protected:
andresp@webrtc.org44caf012014-03-26 21:00:21 +000060 BitrateControllerTest() : clock_(0), enforce_min_bitrate_(true) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000061 ~BitrateControllerTest() {}
62
63 virtual void SetUp() {
andresp@webrtc.org44caf012014-03-26 21:00:21 +000064 controller_ = BitrateController::CreateBitrateController(
65 &clock_, enforce_min_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000066 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
67 }
68
69 virtual void TearDown() {
70 delete bandwidth_observer_;
71 delete controller_;
72 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000073
74 webrtc::SimulatedClock clock_;
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000075 bool enforce_min_bitrate_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000076 BitrateController* controller_;
77 RtcpBandwidthObserver* bandwidth_observer_;
78};
79
80TEST_F(BitrateControllerTest, Basic) {
81 TestBitrateObserver bitrate_observer;
82 controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
83 controller_->RemoveBitrateObserver(&bitrate_observer);
84}
85
stefan@webrtc.org077593b2014-06-19 12:13:00 +000086TEST_F(BitrateControllerTest, UpdatingBitrateObserver) {
87 TestBitrateObserver bitrate_observer;
88 controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 1500000);
89 clock_.AdvanceTimeMilliseconds(25);
90 controller_->Process();
91 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
92
93 controller_->SetBitrateObserver(&bitrate_observer, 1500000, 100000, 1500000);
94 clock_.AdvanceTimeMilliseconds(25);
95 controller_->Process();
96 EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_);
97
98 controller_->SetBitrateObserver(&bitrate_observer, 500000, 100000, 1500000);
99 clock_.AdvanceTimeMilliseconds(25);
100 controller_->Process();
101 EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_);
102}
103
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000104TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
105 TestBitrateObserver bitrate_observer;
106 controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
107
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000108 // First REMB applies immediately.
109 int64_t time_ms = 1001;
110 webrtc::ReportBlockList report_blocks;
111 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
112 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000113 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
114 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
115 EXPECT_EQ(0u, bitrate_observer.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000116 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
117 report_blocks.clear();
118 time_ms += 2000;
119
120 // Receive a high remb, test bitrate inc.
121 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000122
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000123 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000124 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
125 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000126 EXPECT_EQ(217000u, bitrate_observer.last_bitrate_);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000127 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000128 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000129 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000130
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000131 report_blocks.clear();
132 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000133 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
134 EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
135 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
136 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
137 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000138
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000139 report_blocks.clear();
140 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000141 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
142 EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
143 time_ms += 1000;
144
145 report_blocks.clear();
146 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
147 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000148 EXPECT_EQ(276604u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000149 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000150
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000151 report_blocks.clear();
152 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000153 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000154 EXPECT_EQ(299732u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000156
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000157 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000158 report_blocks.clear();
159 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000160 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000161 EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000162 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000163
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000164 report_blocks.clear();
165 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000166 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000167 EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000168
169 // Test that a low REMB trigger immediately.
170 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000171 EXPECT_EQ(250000u, bitrate_observer.last_bitrate_);
172 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
173 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000174
175 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000176 EXPECT_EQ(100000u, bitrate_observer.last_bitrate_); // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 controller_->RemoveBitrateObserver(&bitrate_observer);
178}
179
180TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
181 TestBitrateObserver bitrate_observer;
182 controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
183
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000184 // REMBs during the first 2 seconds apply immediately.
185 int64_t time_ms = 1;
186 webrtc::ReportBlockList report_blocks;
187 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
188 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
189 report_blocks.clear();
190 time_ms += 500;
191
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000192 RtcpBandwidthObserver* second_bandwidth_observer =
193 controller_->CreateRtcpBandwidthObserver();
194
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000195 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000196 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000197 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
198 report_blocks, 100, 1);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000199 EXPECT_EQ(217000u, bitrate_observer.last_bitrate_);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000200 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000201 EXPECT_EQ(100u, bitrate_observer.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000202 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000203
204 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000205 report_blocks.clear();
206 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000207 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
208 time_ms += 500;
209 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
210 report_blocks, 100, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000211 EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000212 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
213 EXPECT_EQ(100u, bitrate_observer.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000214 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000215
216 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000217 report_blocks.clear();
218 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000219 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
220 report_blocks, 100, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000221 EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000222 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000223
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000224 report_blocks.clear();
225 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000226 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000227 EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000228
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000229 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000230 report_blocks.clear();
231 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000232 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
233 report_blocks, 100, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000234 EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000235 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000236
237 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000238 report_blocks.clear();
239 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000240 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
241 report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000242 EXPECT_EQ(276604u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000243 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000244
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000245 report_blocks.clear();
246 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000247 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
248 report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000249 EXPECT_EQ(299732u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000250 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000251
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000252 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000253 report_blocks.clear();
254 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000255 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000256 report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000257 EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000258 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000259
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000260 report_blocks.clear();
261 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000262 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000263 report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000264 EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000265
266 // Test that a low REMB trigger immediately.
267 // We don't care which bandwidth observer that delivers the REMB.
268 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000269 EXPECT_EQ(250000u, bitrate_observer.last_bitrate_);
270 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
271 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000272
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000273 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000274 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000275 EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000276 controller_->RemoveBitrateObserver(&bitrate_observer);
277 delete second_bandwidth_observer;
278}
279
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000280TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
281 TestBitrateObserver bitrate_observer;
282 uint32_t sequence_number[2] = {0, 0xFF00};
283 const uint32_t kStartBitrate = 200000;
284 const uint32_t kMinBitrate = 100000;
285 const uint32_t kMaxBitrate = 300000;
286 controller_->SetBitrateObserver(&bitrate_observer, kStartBitrate, kMinBitrate,
287 kMaxBitrate);
288
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000289 // REMBs during the first 2 seconds apply immediately.
290 int64_t time_ms = 1001;
291 webrtc::ReportBlockList report_blocks;
292 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
293 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
294 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
295 report_blocks.clear();
296 time_ms += 2000;
297
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000298 // Receive a high REMB, test bitrate increase.
299 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
300
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000301 uint32_t last_bitrate = 0;
302 // Ramp up to max bitrate.
303 for (int i = 0; i < 6; ++i) {
304 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
305 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
306 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
307 time_ms);
308 EXPECT_GT(bitrate_observer.last_bitrate_, last_bitrate);
309 EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
310 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
311 last_bitrate = bitrate_observer.last_bitrate_;
312 time_ms += 1000;
313 sequence_number[0] += 20;
314 sequence_number[1] += 1;
315 report_blocks.clear();
316 }
317
318 EXPECT_EQ(kMaxBitrate, bitrate_observer.last_bitrate_);
319
320 // Packet loss on the first stream. Verify that bitrate decreases.
321 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
322 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
323 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
324 EXPECT_LT(bitrate_observer.last_bitrate_, last_bitrate);
325 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer.last_fraction_loss_);
326 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
327 last_bitrate = bitrate_observer.last_bitrate_;
328 sequence_number[0] += 20;
329 sequence_number[1] += 20;
330 time_ms += 1000;
331 report_blocks.clear();
332
333 // Packet loss on the second stream. Verify that bitrate decreases.
334 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
335 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
336 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
337 EXPECT_LT(bitrate_observer.last_bitrate_, last_bitrate);
338 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer.last_fraction_loss_);
339 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
340 last_bitrate = bitrate_observer.last_bitrate_;
341 sequence_number[0] += 20;
342 sequence_number[1] += 1;
343 time_ms += 1000;
344 report_blocks.clear();
345
346 // All packets lost on stream with few packets, no back-off.
347 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
348 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
349 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
350 EXPECT_EQ(bitrate_observer.last_bitrate_, last_bitrate);
351 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer.last_fraction_loss_);
352 EXPECT_EQ(50u, bitrate_observer.last_rtt_);
353 last_bitrate = bitrate_observer.last_bitrate_;
354 sequence_number[0] += 20;
355 sequence_number[1] += 1;
356 report_blocks.clear();
357}
358
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000359TEST_F(BitrateControllerTest, TwoBitrateObserversOneRtcpObserver) {
360 TestBitrateObserver bitrate_observer_1;
361 TestBitrateObserver bitrate_observer_2;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000362 controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 300000);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000363 controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 300000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000364
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000365 // REMBs during the first 2 seconds apply immediately.
366 int64_t time_ms = 1001;
367 webrtc::ReportBlockList report_blocks;
368 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
369 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000370 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_);
371 EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
372 EXPECT_EQ(0u, bitrate_observer_1.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000373 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
374 report_blocks.clear();
375 time_ms += 2000;
376
377 // Receive a high remb, test bitrate inc.
378 // Test too low start bitrate, hence lower than sum of min.
379 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000380
381 // Test bitrate increase 8% per second, distributed equally.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000382 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
383 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000384 EXPECT_EQ(112500u, bitrate_observer_1.last_bitrate_);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000385 EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
386 EXPECT_EQ(50u, bitrate_observer_1.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000387 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000388
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000389 EXPECT_EQ(212500u, bitrate_observer_2.last_bitrate_);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000390 EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
391 EXPECT_EQ(50u, bitrate_observer_2.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000392
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000393 report_blocks.clear();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000394 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000395 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000396 EXPECT_EQ(126000u, bitrate_observer_1.last_bitrate_);
397 EXPECT_EQ(226000u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000398 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000399
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000400 report_blocks.clear();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000401 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000402 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000403 EXPECT_EQ(140580u, bitrate_observer_1.last_bitrate_);
404 EXPECT_EQ(240580u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000405 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000406
407 // Check that the bitrate sum honor our REMB.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000408 report_blocks.clear();
409 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000410 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000411 EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_);
412 EXPECT_EQ(250000u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000413 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000414
415 // Remove REMB cap, higher than sum of max.
416 bandwidth_observer_->OnReceivedEstimatedBitrate(700000);
417
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000418 report_blocks.clear();
419 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000420 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000421 EXPECT_EQ(166500u, bitrate_observer_1.last_bitrate_);
422 EXPECT_EQ(266500u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000423 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000424
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000425 report_blocks.clear();
426 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000427 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000428 EXPECT_EQ(184320u, bitrate_observer_1.last_bitrate_);
429 EXPECT_EQ(284320u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000430 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000431
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000432 report_blocks.clear();
433 report_blocks.push_back(CreateReportBlock(1, 2, 0, 161));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000434 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000435 EXPECT_EQ(207130u, bitrate_observer_1.last_bitrate_);
436 EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_); // Max cap.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000437 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000438
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000439 report_blocks.clear();
440 report_blocks.push_back(CreateReportBlock(1, 2, 0, 181));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000441 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000442 EXPECT_EQ(248700u, bitrate_observer_1.last_bitrate_);
443 EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000444 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000445
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000446 report_blocks.clear();
447 report_blocks.push_back(CreateReportBlock(1, 2, 0, 201));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000448 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000449 EXPECT_EQ(293596u, bitrate_observer_1.last_bitrate_);
450 EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000451 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000452
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000453 report_blocks.clear();
454 report_blocks.push_back(CreateReportBlock(1, 2, 0, 221));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000455 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000456 EXPECT_EQ(300000u, bitrate_observer_1.last_bitrate_); // Max cap.
457 EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000458
459 // Test that a low REMB trigger immediately.
460 bandwidth_observer_->OnReceivedEstimatedBitrate(350000);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000461 EXPECT_EQ(125000u, bitrate_observer_1.last_bitrate_);
462 EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
463 EXPECT_EQ(50u, bitrate_observer_1.last_rtt_);
464 EXPECT_EQ(225000u, bitrate_observer_2.last_bitrate_);
465 EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
466 EXPECT_EQ(50u, bitrate_observer_2.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000467
468 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +0000469 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); // Min cap.
470 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000471 controller_->RemoveBitrateObserver(&bitrate_observer_1);
472 controller_->RemoveBitrateObserver(&bitrate_observer_2);
473}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000474
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000475TEST_F(BitrateControllerTest, SetReservedBitrate) {
476 TestBitrateObserver bitrate_observer;
477 controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
478
479 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
480
481 controller_->SetReservedBitrate(0);
482 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
483 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
484 controller_->SetReservedBitrate(50000);
485 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
486 EXPECT_EQ(150000u, bitrate_observer.last_bitrate_);
487
488 controller_->SetReservedBitrate(0);
489 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
490 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
491 controller_->SetReservedBitrate(50000);
492 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
493 EXPECT_EQ(150000u, bitrate_observer.last_bitrate_);
494
495 controller_->SetReservedBitrate(0);
496 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
497 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
498 controller_->SetReservedBitrate(30000);
499 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
500 EXPECT_EQ(170000u, bitrate_observer.last_bitrate_);
501
502 controller_->SetReservedBitrate(0);
503 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
504 EXPECT_EQ(160000u, bitrate_observer.last_bitrate_);
505 controller_->SetReservedBitrate(30000);
506 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
507 EXPECT_EQ(130000u, bitrate_observer.last_bitrate_);
508
509 controller_->SetReservedBitrate(0);
510 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
511 EXPECT_EQ(120000u, bitrate_observer.last_bitrate_);
512 controller_->SetReservedBitrate(10000);
513 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
514 EXPECT_EQ(110000u, bitrate_observer.last_bitrate_);
515
516 controller_->SetReservedBitrate(0);
517 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
518 EXPECT_EQ(120000u, bitrate_observer.last_bitrate_);
519 controller_->SetReservedBitrate(50000);
520 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
521 EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
522
523 controller_->SetReservedBitrate(10000);
524 bandwidth_observer_->OnReceivedEstimatedBitrate(0);
525 EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
526
527 controller_->RemoveBitrateObserver(&bitrate_observer);
528}
529
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000530class BitrateControllerTestNoEnforceMin : public BitrateControllerTest {
531 protected:
532 BitrateControllerTestNoEnforceMin() : BitrateControllerTest() {
533 enforce_min_bitrate_ = false;
534 }
535};
536
537// The following three tests verify that the EnforceMinBitrate() method works
538// as intended.
539TEST_F(BitrateControllerTestNoEnforceMin, OneBitrateObserver) {
540 TestBitrateObserver bitrate_observer_1;
541 controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 400000);
542
543 // High REMB.
544 bandwidth_observer_->OnReceivedEstimatedBitrate(150000);
545 EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_);
546
547 // Low REMB.
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000548 bandwidth_observer_->OnReceivedEstimatedBitrate(10000);
549 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
550
551 // Keeps at least 10 kbps.
552 bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
553 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000554
555 controller_->RemoveBitrateObserver(&bitrate_observer_1);
556}
557
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000558TEST_F(BitrateControllerTestNoEnforceMin, SetReservedBitrate) {
559 TestBitrateObserver bitrate_observer_1;
560 controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 400000);
561 controller_->SetReservedBitrate(10000);
562
563 // High REMB.
564 bandwidth_observer_->OnReceivedEstimatedBitrate(150000);
565 EXPECT_EQ(140000u, bitrate_observer_1.last_bitrate_);
566
567 // Low REMB.
568 bandwidth_observer_->OnReceivedEstimatedBitrate(15000);
569 EXPECT_EQ(5000u, bitrate_observer_1.last_bitrate_);
570
571 // Keeps at least 10 kbps.
572 bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
573 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_);
574
575 controller_->RemoveBitrateObserver(&bitrate_observer_1);
576}
577
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000578TEST_F(BitrateControllerTestNoEnforceMin, ThreeBitrateObservers) {
579 TestBitrateObserver bitrate_observer_1;
580 TestBitrateObserver bitrate_observer_2;
581 TestBitrateObserver bitrate_observer_3;
582 // Set up the observers with min bitrates at 100000, 200000, and 300000.
583 // Note: The start bitrate of bitrate_observer_1 (700000) is used as the
584 // overall start bitrate.
585 controller_->SetBitrateObserver(&bitrate_observer_1, 700000, 100000, 400000);
586 controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 400000);
587 controller_->SetBitrateObserver(&bitrate_observer_3, 200000, 300000, 400000);
588
589 // High REMB. Make sure the controllers get a fair share of the surplus
590 // (i.e., what is left after each controller gets its min rate).
591 bandwidth_observer_->OnReceivedEstimatedBitrate(690000);
592 // Verify that each observer gets its min rate (sum of min rates is 600000),
593 // and that the remaining 90000 is divided equally among the three.
594 EXPECT_EQ(130000u, bitrate_observer_1.last_bitrate_);
595 EXPECT_EQ(230000u, bitrate_observer_2.last_bitrate_);
596 EXPECT_EQ(330000u, bitrate_observer_3.last_bitrate_);
597
598 // High REMB, but below the sum of min bitrates.
599 bandwidth_observer_->OnReceivedEstimatedBitrate(500000);
600 // Verify that the first and second observers get their min bitrates, and the
601 // third gets the remainder.
602 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); // Min bitrate.
603 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); // Min bitrate.
604 EXPECT_EQ(200000u, bitrate_observer_3.last_bitrate_); // Remainder.
605
606 // Low REMB.
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000607 bandwidth_observer_->OnReceivedEstimatedBitrate(10000);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000608 // Verify that the first observer gets all the rate, and the rest get zero.
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000609 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
610 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_);
611 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_);
612
613 // Verify it keeps an estimate of at least 10kbps.
614 bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
615 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000616 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_);
617 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_);
618
619 controller_->RemoveBitrateObserver(&bitrate_observer_1);
620 controller_->RemoveBitrateObserver(&bitrate_observer_2);
621 controller_->RemoveBitrateObserver(&bitrate_observer_3);
622}
623
624TEST_F(BitrateControllerTest, ThreeBitrateObserversLowRembEnforceMin) {
625 TestBitrateObserver bitrate_observer_1;
626 TestBitrateObserver bitrate_observer_2;
627 TestBitrateObserver bitrate_observer_3;
628 controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 300000);
629 controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 300000);
630 controller_->SetBitrateObserver(&bitrate_observer_3, 200000, 300000, 300000);
631
632 // Low REMB. Verify that all observers still get their respective min bitrate.
633 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
634 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); // Min cap.
635 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); // Min cap.
636 EXPECT_EQ(300000u, bitrate_observer_3.last_bitrate_); // Min cap.
637
638 controller_->RemoveBitrateObserver(&bitrate_observer_1);
639 controller_->RemoveBitrateObserver(&bitrate_observer_2);
640 controller_->RemoveBitrateObserver(&bitrate_observer_3);
641}