blob: 2b9e589fbdb99eaf6f0cd8385a85ed862bb9d15d [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"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/rtp_rtcp/include/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) {
Stefan Holmere5904162015-03-26 11:11:06 +010049 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000050 last_fraction_loss_ = fraction_loss;
51 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000052 }
Stefan Holmere5904162015-03-26 11:11:06 +010053 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000054 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_);
Stefan Holmere5904162015-03-26 11:11:06 +010066 controller_->SetStartBitrate(kStartBitrateBps);
67 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
68 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
69 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000070 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
71 }
72
73 virtual void TearDown() {
74 delete bandwidth_observer_;
75 delete controller_;
76 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000077
Stefan Holmere5904162015-03-26 11:11:06 +010078 const int kMinBitrateBps = 100000;
79 const int kStartBitrateBps = 200000;
80 const int kMaxBitrateBps = 300000;
81
82 const int kDefaultMinBitrateBps = 10000;
83 const int kDefaultMaxBitrateBps = 1000000000;
84
andresp@webrtc.org44caf012014-03-26 21:00:21 +000085 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000086 TestBitrateObserver bitrate_observer_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000087 BitrateController* controller_;
88 RtcpBandwidthObserver* bandwidth_observer_;
89};
90
Stefan Holmere5904162015-03-26 11:11:06 +010091TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
92 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
93 controller_->SetMinMaxBitrate(0, 0);
94 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
95 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
96 EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_);
97 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
98 clock_.AdvanceTimeMilliseconds(1000);
99 controller_->Process();
100 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
101}
102
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000103TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000104 // First REMB applies immediately.
105 int64_t time_ms = 1001;
106 webrtc::ReportBlockList report_blocks;
107 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
108 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100109 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000110 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
111 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000112 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
113 report_blocks.clear();
114 time_ms += 2000;
115
116 // Receive a high remb, test bitrate inc.
117 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000118
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000119 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000120 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
121 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100122 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000123 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
124 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000126
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000127 report_blocks.clear();
128 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000129 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100130 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000131 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
132 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000133 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000134
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000135 report_blocks.clear();
136 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000137 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100138 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000139 time_ms += 1000;
140
141 report_blocks.clear();
142 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
143 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100144 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000145 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000146
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000147 report_blocks.clear();
148 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000149 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100150 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000151 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000152
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000153 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000154 report_blocks.clear();
155 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000156 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100157 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000158 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000159
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000160 report_blocks.clear();
161 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000162 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100163 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000164
165 // Test that a low REMB trigger immediately.
166 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100167 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000168 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
169 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000170
171 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100172 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000173}
174
175TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000176 // REMBs during the first 2 seconds apply immediately.
177 int64_t time_ms = 1;
178 webrtc::ReportBlockList report_blocks;
179 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
180 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
181 report_blocks.clear();
182 time_ms += 500;
183
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000184 RtcpBandwidthObserver* second_bandwidth_observer =
185 controller_->CreateRtcpBandwidthObserver();
186
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000187 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000188 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000189 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
190 report_blocks, 100, 1);
Stefan Holmere5904162015-03-26 11:11:06 +0100191 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
193 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000194 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000195
196 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000197 report_blocks.clear();
198 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000199 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
200 time_ms += 500;
201 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
202 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100203 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000204 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
205 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000206 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000207
208 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000209 report_blocks.clear();
210 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000211 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
212 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100213 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000214 time_ms += 500;
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, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000218 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100219 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000220
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000222 report_blocks.clear();
223 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000224 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
225 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100226 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000228
229 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000230 report_blocks.clear();
231 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000232 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
233 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100234 EXPECT_EQ(276604, 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
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000237 report_blocks.clear();
238 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000239 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
240 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100241 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000242 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000243
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000244 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000245 report_blocks.clear();
246 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000247 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000248 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100249 EXPECT_EQ(300000, 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
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000252 report_blocks.clear();
253 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000254 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000255 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100256 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000257
258 // Test that a low REMB trigger immediately.
259 // We don't care which bandwidth observer that delivers the REMB.
260 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100261 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000262 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
263 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000264
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000265 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000266 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100267 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000268 delete second_bandwidth_observer;
269}
270
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000271TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000272 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100273 const int kStartBitrate = 200000;
274 const int kMinBitrate = 100000;
275 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000276 controller_->SetStartBitrate(kStartBitrate);
277 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000278
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000279 // REMBs during the first 2 seconds apply immediately.
280 int64_t time_ms = 1001;
281 webrtc::ReportBlockList report_blocks;
282 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
283 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
284 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
285 report_blocks.clear();
286 time_ms += 2000;
287
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000288 // Receive a high REMB, test bitrate increase.
289 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
290
Stefan Holmere5904162015-03-26 11:11:06 +0100291 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000292 // Ramp up to max bitrate.
293 for (int i = 0; i < 6; ++i) {
294 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
295 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
296 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
297 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000298 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
299 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
300 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
301 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000302 time_ms += 1000;
303 sequence_number[0] += 20;
304 sequence_number[1] += 1;
305 report_blocks.clear();
306 }
307
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000308 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000309
310 // Packet loss on the first stream. Verify that bitrate decreases.
311 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
312 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
313 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000314 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
315 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
316 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
317 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000318 sequence_number[0] += 20;
319 sequence_number[1] += 20;
320 time_ms += 1000;
321 report_blocks.clear();
322
323 // Packet loss on the second stream. Verify that bitrate decreases.
324 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
325 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
326 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000327 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
328 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
329 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
330 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000331 sequence_number[0] += 20;
332 sequence_number[1] += 1;
333 time_ms += 1000;
334 report_blocks.clear();
335
336 // All packets lost on stream with few packets, no back-off.
337 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
338 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
339 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000340 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
341 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
342 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
343 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000344 sequence_number[0] += 20;
345 sequence_number[1] += 1;
346 report_blocks.clear();
347}
348
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000349TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000350 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000351 controller_->SetReservedBitrate(0);
352 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100353 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000354 controller_->SetReservedBitrate(50000);
355 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100356 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000357
358 controller_->SetReservedBitrate(0);
359 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100360 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000361 controller_->SetReservedBitrate(50000);
362 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100363 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000364
365 controller_->SetReservedBitrate(0);
366 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100367 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000368 controller_->SetReservedBitrate(30000);
369 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100370 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000371
372 controller_->SetReservedBitrate(0);
373 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100374 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000375 controller_->SetReservedBitrate(30000);
376 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100377 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000378
379 controller_->SetReservedBitrate(0);
380 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100381 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000382 controller_->SetReservedBitrate(10000);
383 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100384 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000385
386 controller_->SetReservedBitrate(0);
387 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100388 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000389 controller_->SetReservedBitrate(50000);
390 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000391 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100392 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000393
394 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000395 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100396 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000397}