blob: fd9636a88d803ee3695de2ccb7dea249c47d2dd4 [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
stefan32f81542016-01-20 07:13:58 -0800165 // Test that a low delay-based estimate limits the combined estimate.
166 controller_->UpdateDelayBasedEstimate(280000);
167 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
168
169 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000170 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100171 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000172 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
173 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000174
175 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800176 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177}
178
179TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000180 // REMBs during the first 2 seconds apply immediately.
181 int64_t time_ms = 1;
182 webrtc::ReportBlockList report_blocks;
183 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
184 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
185 report_blocks.clear();
186 time_ms += 500;
187
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000188 RtcpBandwidthObserver* second_bandwidth_observer =
189 controller_->CreateRtcpBandwidthObserver();
190
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000191 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000192 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000193 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
194 report_blocks, 100, 1);
Stefan Holmere5904162015-03-26 11:11:06 +0100195 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000196 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
197 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000198 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000199
200 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000201 report_blocks.clear();
202 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000203 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
204 time_ms += 500;
205 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
206 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100207 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000208 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
209 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000210 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000211
212 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000213 report_blocks.clear();
214 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000215 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
216 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100217 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000218 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000219
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000220 report_blocks.clear();
221 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000222 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100223 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000224
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000225 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000226 report_blocks.clear();
227 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000228 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
229 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100230 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000231 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000232
233 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000234 report_blocks.clear();
235 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000236 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
237 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100238 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000239 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000240
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000241 report_blocks.clear();
242 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000243 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
244 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100245 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000246 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000247
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000248 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000249 report_blocks.clear();
250 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000251 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000252 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100253 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000254 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000255
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000256 report_blocks.clear();
257 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000258 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000259 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100260 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000261
262 // Test that a low REMB trigger immediately.
263 // We don't care which bandwidth observer that delivers the REMB.
264 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100265 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000266 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
267 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000268
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000269 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000270 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100271 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000272 delete second_bandwidth_observer;
273}
274
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000275TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000276 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100277 const int kStartBitrate = 200000;
278 const int kMinBitrate = 100000;
279 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000280 controller_->SetStartBitrate(kStartBitrate);
281 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000282
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000283 // REMBs during the first 2 seconds apply immediately.
284 int64_t time_ms = 1001;
285 webrtc::ReportBlockList report_blocks;
286 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
287 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
288 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
289 report_blocks.clear();
290 time_ms += 2000;
291
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000292 // Receive a high REMB, test bitrate increase.
293 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
294
Stefan Holmere5904162015-03-26 11:11:06 +0100295 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000296 // Ramp up to max bitrate.
297 for (int i = 0; i < 6; ++i) {
298 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
299 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
300 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
301 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000302 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
303 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
304 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
305 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000306 time_ms += 1000;
307 sequence_number[0] += 20;
308 sequence_number[1] += 1;
309 report_blocks.clear();
310 }
311
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000312 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000313
314 // Packet loss on the first stream. Verify that bitrate decreases.
315 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
316 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
317 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000318 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
319 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
320 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
321 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000322 sequence_number[0] += 20;
323 sequence_number[1] += 20;
324 time_ms += 1000;
325 report_blocks.clear();
326
327 // Packet loss on the second stream. Verify that bitrate decreases.
328 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
329 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
330 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000331 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
332 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
333 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
334 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000335 sequence_number[0] += 20;
336 sequence_number[1] += 1;
337 time_ms += 1000;
338 report_blocks.clear();
339
340 // All packets lost on stream with few packets, no back-off.
341 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
342 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
343 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000344 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
345 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
346 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
347 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000348 sequence_number[0] += 20;
349 sequence_number[1] += 1;
350 report_blocks.clear();
351}
352
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000353TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000354 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000355 controller_->SetReservedBitrate(0);
356 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100357 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000358 controller_->SetReservedBitrate(50000);
359 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100360 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000361
362 controller_->SetReservedBitrate(0);
363 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100364 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000365 controller_->SetReservedBitrate(50000);
366 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100367 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000368
369 controller_->SetReservedBitrate(0);
370 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100371 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000372 controller_->SetReservedBitrate(30000);
373 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100374 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000375
376 controller_->SetReservedBitrate(0);
377 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100378 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000379 controller_->SetReservedBitrate(30000);
380 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100381 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000382
383 controller_->SetReservedBitrate(0);
384 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100385 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000386 controller_->SetReservedBitrate(10000);
387 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100388 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000389
390 controller_->SetReservedBitrate(0);
391 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100392 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000393 controller_->SetReservedBitrate(50000);
394 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000395 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100396 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000397
398 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000399 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100400 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000401}