blob: e8d49c6924fdd179b7611315105d8d8505febd79 [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
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000046 virtual void OnNetworkChanged(uint32_t bitrate,
47 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000048 int64_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_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000055 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000056};
57
58class BitrateControllerTest : public ::testing::Test {
59 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000060 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000061 ~BitrateControllerTest() {}
62
63 virtual void SetUp() {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000064 controller_ =
65 BitrateController::CreateBitrateController(&clock_, &bitrate_observer_);
66 controller_->SetStartBitrate(200000);
67 controller_->SetMinMaxBitrate(100000, 300000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000068 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
69 }
70
71 virtual void TearDown() {
72 delete bandwidth_observer_;
73 delete controller_;
74 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000075
76 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000077 TestBitrateObserver bitrate_observer_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000078 BitrateController* controller_;
79 RtcpBandwidthObserver* bandwidth_observer_;
80};
81
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000082TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000083 // First REMB applies immediately.
84 int64_t time_ms = 1001;
85 webrtc::ReportBlockList report_blocks;
86 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
87 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000088 EXPECT_EQ(200000u, bitrate_observer_.last_bitrate_);
89 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
90 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000091 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
92 report_blocks.clear();
93 time_ms += 2000;
94
95 // Receive a high remb, test bitrate inc.
96 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000097
andresp@webrtc.org44caf012014-03-26 21:00:21 +000098 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000099 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
100 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000101 EXPECT_EQ(217000u, bitrate_observer_.last_bitrate_);
102 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
103 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000104 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000105
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000106 report_blocks.clear();
107 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000108 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000109 EXPECT_EQ(235360u, bitrate_observer_.last_bitrate_);
110 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
111 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000112 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000113
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000114 report_blocks.clear();
115 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000116 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000117 EXPECT_EQ(255189u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000118 time_ms += 1000;
119
120 report_blocks.clear();
121 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
122 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000123 EXPECT_EQ(276604u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000124 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000125
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000126 report_blocks.clear();
127 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000128 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129 EXPECT_EQ(299732u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000131
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000132 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000133 report_blocks.clear();
134 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000136 EXPECT_EQ(300000u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000137 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, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000141 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000142 EXPECT_EQ(300000u, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000143
144 // Test that a low REMB trigger immediately.
145 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000146 EXPECT_EQ(250000u, bitrate_observer_.last_bitrate_);
147 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
148 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000149
150 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000151 EXPECT_EQ(100000u, bitrate_observer_.last_bitrate_); // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000152}
153
154TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 // REMBs during the first 2 seconds apply immediately.
156 int64_t time_ms = 1;
157 webrtc::ReportBlockList report_blocks;
158 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
159 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
160 report_blocks.clear();
161 time_ms += 500;
162
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000163 RtcpBandwidthObserver* second_bandwidth_observer =
164 controller_->CreateRtcpBandwidthObserver();
165
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000166 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000167 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000168 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
169 report_blocks, 100, 1);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000170 EXPECT_EQ(217000u, bitrate_observer_.last_bitrate_);
171 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
172 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000173 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000174
175 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000176 report_blocks.clear();
177 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000178 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
179 time_ms += 500;
180 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
181 report_blocks, 100, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182 EXPECT_EQ(235360u, bitrate_observer_.last_bitrate_);
183 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
184 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000185 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000186
187 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000188 report_blocks.clear();
189 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000190 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
191 report_blocks, 100, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192 EXPECT_EQ(235360u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000193 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000194
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000195 report_blocks.clear();
196 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000197 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000198 EXPECT_EQ(255189u, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000199
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000200 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000201 report_blocks.clear();
202 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000203 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
204 report_blocks, 100, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000205 EXPECT_EQ(255189u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000206 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000207
208 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000209 report_blocks.clear();
210 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000211 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
212 report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000213 EXPECT_EQ(276604u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000214 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000215
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000216 report_blocks.clear();
217 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000218 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
219 report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000220 EXPECT_EQ(299732u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000221 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000222
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000223 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000224 report_blocks.clear();
225 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000226 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227 report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000228 EXPECT_EQ(300000u, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000229 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000230
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000231 report_blocks.clear();
232 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000233 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000234 report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000235 EXPECT_EQ(300000u, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000236
237 // Test that a low REMB trigger immediately.
238 // We don't care which bandwidth observer that delivers the REMB.
239 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000240 EXPECT_EQ(250000u, bitrate_observer_.last_bitrate_);
241 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
242 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000243
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000244 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000245 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000246 EXPECT_EQ(100000u, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000247 delete second_bandwidth_observer;
248}
249
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000250TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000251 uint32_t sequence_number[2] = {0, 0xFF00};
252 const uint32_t kStartBitrate = 200000;
253 const uint32_t kMinBitrate = 100000;
254 const uint32_t kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000255 controller_->SetStartBitrate(kStartBitrate);
256 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000257
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000258 // REMBs during the first 2 seconds apply immediately.
259 int64_t time_ms = 1001;
260 webrtc::ReportBlockList report_blocks;
261 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
262 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
263 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
264 report_blocks.clear();
265 time_ms += 2000;
266
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000267 // Receive a high REMB, test bitrate increase.
268 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
269
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000270 uint32_t last_bitrate = 0;
271 // Ramp up to max bitrate.
272 for (int i = 0; i < 6; ++i) {
273 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
274 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
275 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
276 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000277 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
278 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
279 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
280 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000281 time_ms += 1000;
282 sequence_number[0] += 20;
283 sequence_number[1] += 1;
284 report_blocks.clear();
285 }
286
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000287 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000288
289 // Packet loss on the first stream. Verify that bitrate decreases.
290 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
291 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
292 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000293 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
294 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
295 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
296 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000297 sequence_number[0] += 20;
298 sequence_number[1] += 20;
299 time_ms += 1000;
300 report_blocks.clear();
301
302 // Packet loss on the second stream. Verify that bitrate decreases.
303 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
304 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
305 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000306 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
307 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
308 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
309 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000310 sequence_number[0] += 20;
311 sequence_number[1] += 1;
312 time_ms += 1000;
313 report_blocks.clear();
314
315 // All packets lost on stream with few packets, no back-off.
316 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
317 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
318 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000319 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
320 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
321 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
322 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000323 sequence_number[0] += 20;
324 sequence_number[1] += 1;
325 report_blocks.clear();
326}
327
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000328TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000329 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000330 controller_->SetReservedBitrate(0);
331 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000332 EXPECT_EQ(200000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000333 controller_->SetReservedBitrate(50000);
334 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000335 EXPECT_EQ(150000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000336
337 controller_->SetReservedBitrate(0);
338 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000339 EXPECT_EQ(200000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000340 controller_->SetReservedBitrate(50000);
341 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000342 EXPECT_EQ(150000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000343
344 controller_->SetReservedBitrate(0);
345 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000346 EXPECT_EQ(200000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000347 controller_->SetReservedBitrate(30000);
348 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000349 EXPECT_EQ(170000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000350
351 controller_->SetReservedBitrate(0);
352 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000353 EXPECT_EQ(160000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000354 controller_->SetReservedBitrate(30000);
355 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000356 EXPECT_EQ(130000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000357
358 controller_->SetReservedBitrate(0);
359 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000360 EXPECT_EQ(120000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000361 controller_->SetReservedBitrate(10000);
362 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000363 EXPECT_EQ(110000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000364
365 controller_->SetReservedBitrate(0);
366 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000367 EXPECT_EQ(120000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000368 controller_->SetReservedBitrate(50000);
369 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000370 // Limited by min bitrate.
371 EXPECT_EQ(100000u, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000372
373 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000374 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
375 EXPECT_EQ(100000u, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000376}