blob: 0e080eedf4938c0c43e4b10cbbcaf6542c61d19d [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
ivoc14d5dbe2016-07-04 07:06:55 -070014#include "webrtc/call/mock/mock_rtc_event_log.h"
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000015#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkjec81bcd2016-05-11 06:01:13 -070016#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Stefan Holmer52200d02016-09-20 14:14:23 +020018#include "webrtc/test/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/test/gtest.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() {
Stefan Holmer52200d02016-09-20 14:14:23 +020070 controller_.reset(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_);
Stefan Holmer52200d02016-09-20 14:14:23 +020076 bandwidth_observer_.reset(controller_->CreateRtcpBandwidthObserver());
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000077 }
78
79 virtual void TearDown() {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000080 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000081
Stefan Holmere5904162015-03-26 11:11:06 +010082 const int kMinBitrateBps = 100000;
83 const int kStartBitrateBps = 200000;
84 const int kMaxBitrateBps = 300000;
85
86 const int kDefaultMinBitrateBps = 10000;
87 const int kDefaultMaxBitrateBps = 1000000000;
88
andresp@webrtc.org44caf012014-03-26 21:00:21 +000089 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000090 TestBitrateObserver bitrate_observer_;
Stefan Holmer52200d02016-09-20 14:14:23 +020091 std::unique_ptr<BitrateController> controller_;
92 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020093 testing::NiceMock<webrtc::MockRtcEventLog> event_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000094};
95
Stefan Holmere5904162015-03-26 11:11:06 +010096TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
97 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
98 controller_->SetMinMaxBitrate(0, 0);
99 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
100 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
101 EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_);
102 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
103 clock_.AdvanceTimeMilliseconds(1000);
104 controller_->Process();
105 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
106}
107
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000108TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000109 // First REMB applies immediately.
110 int64_t time_ms = 1001;
111 webrtc::ReportBlockList report_blocks;
112 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
113 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100114 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000115 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
116 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000117 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
118 report_blocks.clear();
119 time_ms += 2000;
120
121 // Receive a high remb, test bitrate inc.
122 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000123
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000124 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
126 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100127 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000128 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
129 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000131
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000132 report_blocks.clear();
133 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000134 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100135 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000136 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
137 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000138 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000139
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000140 report_blocks.clear();
141 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000142 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100143 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000144 time_ms += 1000;
145
146 report_blocks.clear();
147 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
148 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100149 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000150 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000151
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000152 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700153 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000154 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100155 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000156 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000157
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000158 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000159 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700160 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000161 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100162 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000163 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000164
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000165 report_blocks.clear();
166 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000167 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100168 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000169
stefan32f81542016-01-20 07:13:58 -0800170 // Test that a low delay-based estimate limits the combined estimate.
Stefan Holmer280de9e2016-09-30 10:06:51 +0200171 webrtc::DelayBasedBwe::Result result(false, 280000);
172 controller_->OnDelayBasedBweResult(result);
stefan32f81542016-01-20 07:13:58 -0800173 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
174
175 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000176 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100177 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000178 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
179 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000180
181 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800182 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000183}
184
185TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
Stefan Holmer52200d02016-09-20 14:14:23 +0200186 const uint32_t kSsrc1 = 1;
187 const uint32_t kSsrc2 = 2;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000188 // REMBs during the first 2 seconds apply immediately.
189 int64_t time_ms = 1;
190 webrtc::ReportBlockList report_blocks;
Stefan Holmer52200d02016-09-20 14:14:23 +0200191 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 1));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000192 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
193 report_blocks.clear();
194 time_ms += 500;
195
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000196 RtcpBandwidthObserver* second_bandwidth_observer =
197 controller_->CreateRtcpBandwidthObserver();
198
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000199 // Test start bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200200 report_blocks.push_back(CreateReportBlock(2, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000201 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
202 report_blocks, 100, 1);
Stefan Holmer52200d02016-09-20 14:14:23 +0200203 EXPECT_EQ(200000, 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 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000209 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200210 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000211 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
212 time_ms += 500;
Stefan Holmer52200d02016-09-20 14:14:23 +0200213 report_blocks.front().remoteSSRC = kSsrc2;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000214 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
215 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200216 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000217 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
218 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000219 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000220
221 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000222 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200223 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000224 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
225 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200226 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000228
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000229 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200230 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000231 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200232 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000233
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000234 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000235 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200236 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000237 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
238 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200239 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000240 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000241
242 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000243 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200244 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 61));
245 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
246 report_blocks, 50, time_ms);
247 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
248 time_ms += 1000;
249
250 report_blocks.clear();
251 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000252 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
253 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100254 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000255 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000256
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000257 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200258 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000259 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
260 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100261 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000262 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000263
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000264 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000265 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200266 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000267 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000268 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100269 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000270
271 // Test that a low REMB trigger immediately.
272 // We don't care which bandwidth observer that delivers the REMB.
273 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100274 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000275 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
276 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000278 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000279 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100280 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000281 delete second_bandwidth_observer;
282}
283
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000284TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000285 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100286 const int kStartBitrate = 200000;
287 const int kMinBitrate = 100000;
288 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000289 controller_->SetStartBitrate(kStartBitrate);
290 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000291
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000292 // REMBs during the first 2 seconds apply immediately.
293 int64_t time_ms = 1001;
294 webrtc::ReportBlockList report_blocks;
295 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
296 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
297 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
298 report_blocks.clear();
299 time_ms += 2000;
300
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000301 // Receive a high REMB, test bitrate increase.
302 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
303
Stefan Holmere5904162015-03-26 11:11:06 +0100304 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000305 // Ramp up to max bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200306 for (int i = 0; i < 7; ++i) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000307 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
308 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
309 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
310 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000311 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
312 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
313 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
314 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000315 time_ms += 1000;
316 sequence_number[0] += 20;
317 sequence_number[1] += 1;
318 report_blocks.clear();
319 }
320
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000321 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000322
323 // Packet loss on the first stream. Verify that bitrate decreases.
324 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
325 report_blocks.push_back(CreateReportBlock(1, 3, 0, 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, 50, 1, 0), 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] += 20;
333 time_ms += 1000;
334 report_blocks.clear();
335
336 // Packet loss on the second stream. Verify that bitrate decreases.
337 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
338 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
339 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000340 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
341 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), 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 time_ms += 1000;
347 report_blocks.clear();
348
349 // All packets lost on stream with few packets, no back-off.
350 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
351 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
352 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000353 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
354 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
355 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
356 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000357 sequence_number[0] += 20;
358 sequence_number[1] += 1;
359 report_blocks.clear();
360}
361
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000362TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000363 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000364 controller_->SetReservedBitrate(0);
365 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100366 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000367 controller_->SetReservedBitrate(50000);
368 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100369 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000370
371 controller_->SetReservedBitrate(0);
372 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100373 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000374 controller_->SetReservedBitrate(50000);
375 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100376 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000377
378 controller_->SetReservedBitrate(0);
379 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100380 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000381 controller_->SetReservedBitrate(30000);
382 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100383 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000384
385 controller_->SetReservedBitrate(0);
386 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100387 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000388 controller_->SetReservedBitrate(30000);
389 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100390 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000391
392 controller_->SetReservedBitrate(0);
393 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100394 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000395 controller_->SetReservedBitrate(10000);
396 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100397 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000398
399 controller_->SetReservedBitrate(0);
400 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100401 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000402 controller_->SetReservedBitrate(50000);
403 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000404 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100405 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000406
407 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000408 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100409 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000410}
Stefan Holmer52200d02016-09-20 14:14:23 +0200411
412TEST_F(BitrateControllerTest, TimeoutsWithoutFeedback) {
413 {
414 webrtc::test::ScopedFieldTrials override_field_trials(
Stefan Holmerdb158f92016-10-13 02:11:51 +0200415 "WebRTC-FeedbackTimeout/Enabled/");
Stefan Holmer52200d02016-09-20 14:14:23 +0200416 SetUp();
417 int expected_bitrate_bps = 300000;
418 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
419 kDefaultMaxBitrateBps);
420
421 webrtc::ReportBlockList report_blocks;
422 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
423 bandwidth_observer_->OnReceivedRtcpReceiverReport(
424 report_blocks, 50, clock_.TimeInMilliseconds());
425 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
426 clock_.AdvanceTimeMilliseconds(500);
427
428 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
429 bandwidth_observer_->OnReceivedRtcpReceiverReport(
430 report_blocks, 50, clock_.TimeInMilliseconds());
431 report_blocks.clear();
432 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
433 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
434 clock_.AdvanceTimeMilliseconds(1500);
435
436 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
437 bandwidth_observer_->OnReceivedRtcpReceiverReport(
438 report_blocks, 50, clock_.TimeInMilliseconds());
439 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
440 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
441 clock_.AdvanceTimeMilliseconds(1000);
442
443 // 1 seconds since feedback, expect increase.
444 controller_->Process();
445 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
446 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
447 clock_.AdvanceTimeMilliseconds(800);
448
449 // 1.8 seconds since feedback, expect no increase.
450 controller_->Process();
451 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
452 clock_.AdvanceTimeMilliseconds(3701);
453
454 // More than 4.5 seconds since feedback, expect decrease.
455 controller_->Process();
456 expected_bitrate_bps *= 0.8;
457 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
458 clock_.AdvanceTimeMilliseconds(500);
459
460 // Only one timeout every second.
461 controller_->Process();
462 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
463 clock_.AdvanceTimeMilliseconds(501);
464
465 // New timeout allowed.
466 controller_->Process();
467 expected_bitrate_bps *= 0.8;
468 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
469 }
470}
471
472TEST_F(BitrateControllerTest, StopIncreaseWithoutPacketReports) {
473 int expected_bitrate_bps = 300000;
474 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
475 kDefaultMaxBitrateBps);
476
477 webrtc::ReportBlockList report_blocks;
478 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
479 bandwidth_observer_->OnReceivedRtcpReceiverReport(
480 report_blocks, 50, clock_.TimeInMilliseconds());
481 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
482 clock_.AdvanceTimeMilliseconds(500);
483
484 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
485 bandwidth_observer_->OnReceivedRtcpReceiverReport(
486 report_blocks, 50, clock_.TimeInMilliseconds());
487 report_blocks.clear();
488 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
489 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
490 clock_.AdvanceTimeMilliseconds(1500);
491
492 // 1.2 seconds without packets reported as received, no increase.
493 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
494 bandwidth_observer_->OnReceivedRtcpReceiverReport(
495 report_blocks, 50, clock_.TimeInMilliseconds());
496 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
497 clock_.AdvanceTimeMilliseconds(1000);
498
499 // 5 packets reported as received since last, too few, no increase.
500 report_blocks.push_back(CreateReportBlock(1, 2, 0, 26));
501 bandwidth_observer_->OnReceivedRtcpReceiverReport(
502 report_blocks, 50, clock_.TimeInMilliseconds());
503 report_blocks.clear();
504 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
505 clock_.AdvanceTimeMilliseconds(100);
506
507 // 15 packets reported as received since last, enough to increase.
508 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
509 bandwidth_observer_->OnReceivedRtcpReceiverReport(
510 report_blocks, 50, clock_.TimeInMilliseconds());
511 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
512 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
513 clock_.AdvanceTimeMilliseconds(1000);
514}