blob: a70eecf2415b48cb8bf0e7aae666dd7b2a3b8c10 [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
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000011#include <algorithm>
12#include <vector>
13
jbauchf91e6d02016-01-24 23:05:21 -080014#include "testing/gtest/include/gtest/gtest.h"
15
ivoc14d5dbe2016-07-04 07:06:55 -070016#include "webrtc/call/mock/mock_rtc_event_log.h"
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000017#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkjec81bcd2016-05-11 06:01:13 -070018#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010019#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000020
perkjec81bcd2016-05-11 06:01:13 -070021using ::testing::Exactly;
22using ::testing::Return;
23
perkje30c2722016-05-09 04:57:11 -070024using webrtc::BitrateController;
perkjec81bcd2016-05-11 06:01:13 -070025using webrtc::BitrateObserver;
26using webrtc::PacedSender;
27using webrtc::RtcpBandwidthObserver;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000028
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000029uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1,
30 int num_packets2, uint8_t fraction_loss2) {
31 int weighted_sum = num_packets1 * fraction_loss1 +
32 num_packets2 * fraction_loss2;
33 int total_num_packets = num_packets1 + num_packets2;
34 return (weighted_sum + total_num_packets / 2) / total_num_packets;
35}
36
37webrtc::RTCPReportBlock CreateReportBlock(
38 uint32_t remote_ssrc, uint32_t source_ssrc,
39 uint8_t fraction_lost, uint32_t extended_high_sequence_number) {
40 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
41 extended_high_sequence_number, 0, 0, 0);
42}
43
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000044class TestBitrateObserver: public BitrateObserver {
45 public:
46 TestBitrateObserver()
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000047 : last_bitrate_(0),
48 last_fraction_loss_(0),
49 last_rtt_(0) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000050 }
51
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000052 virtual void OnNetworkChanged(uint32_t bitrate,
53 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000054 int64_t rtt) {
Stefan Holmere5904162015-03-26 11:11:06 +010055 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000056 last_fraction_loss_ = fraction_loss;
57 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000058 }
Stefan Holmere5904162015-03-26 11:11:06 +010059 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000060 uint8_t last_fraction_loss_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000061 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000062};
63
64class BitrateControllerTest : public ::testing::Test {
65 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000066 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000067 ~BitrateControllerTest() {}
68
69 virtual void SetUp() {
ivoc14d5dbe2016-07-04 07:06:55 -070070 controller_ = BitrateController::CreateBitrateController(
71 &clock_, &bitrate_observer_, &event_log_);
Stefan Holmere5904162015-03-26 11:11:06 +010072 controller_->SetStartBitrate(kStartBitrateBps);
73 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
74 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
75 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000076 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
77 }
78
79 virtual void TearDown() {
80 delete bandwidth_observer_;
81 delete controller_;
82 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000083
Stefan Holmere5904162015-03-26 11:11:06 +010084 const int kMinBitrateBps = 100000;
85 const int kStartBitrateBps = 200000;
86 const int kMaxBitrateBps = 300000;
87
88 const int kDefaultMinBitrateBps = 10000;
89 const int kDefaultMaxBitrateBps = 1000000000;
90
andresp@webrtc.org44caf012014-03-26 21:00:21 +000091 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000092 TestBitrateObserver bitrate_observer_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000093 BitrateController* controller_;
94 RtcpBandwidthObserver* bandwidth_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020095 testing::NiceMock<webrtc::MockRtcEventLog> event_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000096};
97
Stefan Holmere5904162015-03-26 11:11:06 +010098TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
99 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
100 controller_->SetMinMaxBitrate(0, 0);
101 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
102 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
103 EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_);
104 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
105 clock_.AdvanceTimeMilliseconds(1000);
106 controller_->Process();
107 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
108}
109
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000110TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000111 // First REMB applies immediately.
112 int64_t time_ms = 1001;
113 webrtc::ReportBlockList report_blocks;
114 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
115 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100116 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000117 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
118 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000119 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
120 report_blocks.clear();
121 time_ms += 2000;
122
123 // Receive a high remb, test bitrate inc.
124 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000125
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000126 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000127 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
128 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100129 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000130 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
131 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000132 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000133
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000134 report_blocks.clear();
135 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000136 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100137 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000138 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
139 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000140 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000141
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000142 report_blocks.clear();
143 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000144 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100145 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000146 time_ms += 1000;
147
148 report_blocks.clear();
149 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
150 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100151 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000152 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000153
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000154 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700155 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(299732, 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
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000160 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000161 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700162 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000163 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100164 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000165 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000166
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000167 report_blocks.clear();
168 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000169 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100170 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000171
stefan32f81542016-01-20 07:13:58 -0800172 // Test that a low delay-based estimate limits the combined estimate.
173 controller_->UpdateDelayBasedEstimate(280000);
174 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
175
176 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100178 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000179 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
180 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000181
182 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800183 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000184}
185
186TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000187 // REMBs during the first 2 seconds apply immediately.
188 int64_t time_ms = 1;
189 webrtc::ReportBlockList report_blocks;
190 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
191 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
192 report_blocks.clear();
193 time_ms += 500;
194
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000195 RtcpBandwidthObserver* second_bandwidth_observer =
196 controller_->CreateRtcpBandwidthObserver();
197
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000198 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000199 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000200 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
201 report_blocks, 100, 1);
Stefan Holmere5904162015-03-26 11:11:06 +0100202 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000203 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
204 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000205 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000206
207 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000208 report_blocks.clear();
209 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000210 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
211 time_ms += 500;
212 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
213 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100214 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000215 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
216 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000217 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000218
219 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000220 report_blocks.clear();
221 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000222 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
223 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100224 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000225 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000226
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000227 report_blocks.clear();
228 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000229 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100230 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000231
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000232 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000233 report_blocks.clear();
234 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000235 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
236 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100237 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000238 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000239
240 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000241 report_blocks.clear();
242 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
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(276604, 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
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000248 report_blocks.clear();
249 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000250 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
251 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100252 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000253 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000254
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000255 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000256 report_blocks.clear();
257 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
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_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000261 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000262
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000263 report_blocks.clear();
264 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000265 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000266 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100267 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000268
269 // Test that a low REMB trigger immediately.
270 // We don't care which bandwidth observer that delivers the REMB.
271 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100272 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000273 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
274 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000275
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000276 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100278 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000279 delete second_bandwidth_observer;
280}
281
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000282TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000283 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100284 const int kStartBitrate = 200000;
285 const int kMinBitrate = 100000;
286 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000287 controller_->SetStartBitrate(kStartBitrate);
288 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000289
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000290 // REMBs during the first 2 seconds apply immediately.
291 int64_t time_ms = 1001;
292 webrtc::ReportBlockList report_blocks;
293 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
294 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
295 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
296 report_blocks.clear();
297 time_ms += 2000;
298
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000299 // Receive a high REMB, test bitrate increase.
300 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
301
Stefan Holmere5904162015-03-26 11:11:06 +0100302 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000303 // Ramp up to max bitrate.
304 for (int i = 0; i < 6; ++i) {
305 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
306 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
307 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
308 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000309 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
310 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
311 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
312 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000313 time_ms += 1000;
314 sequence_number[0] += 20;
315 sequence_number[1] += 1;
316 report_blocks.clear();
317 }
318
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000319 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000320
321 // Packet loss on the first stream. Verify that bitrate decreases.
322 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
323 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
324 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000325 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
326 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
327 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
328 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000329 sequence_number[0] += 20;
330 sequence_number[1] += 20;
331 time_ms += 1000;
332 report_blocks.clear();
333
334 // Packet loss on the second stream. Verify that bitrate decreases.
335 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
336 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
337 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000338 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
339 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
340 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
341 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000342 sequence_number[0] += 20;
343 sequence_number[1] += 1;
344 time_ms += 1000;
345 report_blocks.clear();
346
347 // All packets lost on stream with few packets, no back-off.
348 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
349 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
350 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000351 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
352 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
353 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
354 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000355 sequence_number[0] += 20;
356 sequence_number[1] += 1;
357 report_blocks.clear();
358}
359
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000360TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000361 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000362 controller_->SetReservedBitrate(0);
363 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
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(400000);
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(250000);
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(50000);
373 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100374 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000375
376 controller_->SetReservedBitrate(0);
377 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100378 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000379 controller_->SetReservedBitrate(30000);
380 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100381 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000382
383 controller_->SetReservedBitrate(0);
384 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100385 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000386 controller_->SetReservedBitrate(30000);
387 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100388 EXPECT_EQ(130000, 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(10000);
394 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100395 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000396
397 controller_->SetReservedBitrate(0);
398 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100399 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000400 controller_->SetReservedBitrate(50000);
401 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000402 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100403 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000404
405 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000406 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100407 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000408}