blob: 96f8a1ef266eb9e62ecf0f801ccc31d08c7eda8b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
15#include "modules/bitrate_controller/include/bitrate_controller.h"
16#include "modules/pacing/mock/mock_paced_sender.h"
17#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
18#include "test/field_trial.h"
19#include "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
Yves Gerey665174f2018-06-19 15:03:05 +020029uint8_t WeightedLoss(int num_packets1,
30 uint8_t fraction_loss1,
31 int num_packets2,
32 uint8_t fraction_loss2) {
33 int weighted_sum =
34 num_packets1 * fraction_loss1 + num_packets2 * fraction_loss2;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000035 int total_num_packets = num_packets1 + num_packets2;
36 return (weighted_sum + total_num_packets / 2) / total_num_packets;
37}
38
39webrtc::RTCPReportBlock CreateReportBlock(
Yves Gerey665174f2018-06-19 15:03:05 +020040 uint32_t remote_ssrc,
41 uint32_t source_ssrc,
42 uint8_t fraction_lost,
43 uint32_t extended_high_sequence_number) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000044 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
45 extended_high_sequence_number, 0, 0, 0);
46}
47
Yves Gerey665174f2018-06-19 15:03:05 +020048class TestBitrateObserver : public BitrateObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000049 public:
50 TestBitrateObserver()
Yves Gerey665174f2018-06-19 15:03:05 +020051 : last_bitrate_(0), last_fraction_loss_(0), last_rtt_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000052
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000053 virtual void OnNetworkChanged(uint32_t bitrate,
54 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000055 int64_t rtt) {
Stefan Holmere5904162015-03-26 11:11:06 +010056 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000057 last_fraction_loss_ = fraction_loss;
58 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000059 }
Stefan Holmere5904162015-03-26 11:11:06 +010060 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000061 uint8_t last_fraction_loss_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000062 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000063};
64
65class BitrateControllerTest : public ::testing::Test {
66 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000067 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000068 ~BitrateControllerTest() {}
69
70 virtual void SetUp() {
Stefan Holmer52200d02016-09-20 14:14:23 +020071 controller_.reset(BitrateController::CreateBitrateController(
72 &clock_, &bitrate_observer_, &event_log_));
Stefan Holmere5904162015-03-26 11:11:06 +010073 controller_->SetStartBitrate(kStartBitrateBps);
74 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
75 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
76 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +010077 bandwidth_observer_ = controller_.get();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000078 }
79
Yves Gerey665174f2018-06-19 15:03:05 +020080 virtual void TearDown() {}
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_;
Sebastian Jansson8d9c5402017-11-15 17:22:16 +010092 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);
michaeltf082c2a2016-11-07 04:17:14 -0800101 EXPECT_EQ(webrtc::congestion_controller::GetMinBitrateBps(),
102 bitrate_observer_.last_bitrate_);
Stefan Holmere5904162015-03-26 11:11:06 +0100103 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
104 clock_.AdvanceTimeMilliseconds(1000);
105 controller_->Process();
106 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
107}
108
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000109TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000110 // First REMB applies immediately.
111 int64_t time_ms = 1001;
112 webrtc::ReportBlockList report_blocks;
113 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
114 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100115 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000116 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
117 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000118 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
119 report_blocks.clear();
120 time_ms += 2000;
121
122 // Receive a high remb, test bitrate inc.
123 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000124
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000125 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000126 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
127 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100128 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
130 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000131 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000132
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000133 report_blocks.clear();
134 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100136 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000137 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
138 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000139 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000140
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000141 report_blocks.clear();
142 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000143 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100144 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000145 time_ms += 1000;
146
147 report_blocks.clear();
148 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
149 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100150 EXPECT_EQ(276604, 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
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000153 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700154 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100156 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000157 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000158
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000159 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000160 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700161 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
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_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000164 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000165
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000166 report_blocks.clear();
167 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000168 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100169 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000170
stefan32f81542016-01-20 07:13:58 -0800171 // Test that a low delay-based estimate limits the combined estimate.
Stefan Holmer280de9e2016-09-30 10:06:51 +0200172 webrtc::DelayBasedBwe::Result result(false, 280000);
173 controller_->OnDelayBasedBweResult(result);
stefan32f81542016-01-20 07:13:58 -0800174 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) {
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200187 const uint32_t kSenderSsrc1 = 1;
188 const uint32_t kSenderSsrc2 = 2;
189 const uint32_t kMediaSsrc1 = 3;
190 const uint32_t kMediaSsrc2 = 4;
191
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000192 int64_t time_ms = 1;
193 webrtc::ReportBlockList report_blocks;
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200194 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 1)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000195 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000196 time_ms += 500;
197
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100198 RtcpBandwidthObserver* second_bandwidth_observer = controller_.get();
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200199 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 21)};
Yves Gerey665174f2018-06-19 15:03:05 +0200200 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
201 time_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000202
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000203 // Test start bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200204 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000205 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
206 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000207 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000208
209 // Test bitrate increase 8% per second.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200210 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 21)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000211 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
212 time_ms += 500;
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200213
214 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 21)};
Yves Gerey665174f2018-06-19 15:03:05 +0200215 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
216 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200217 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000218 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
219 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000220 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221
222 // Extra report should not change estimate.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200223 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 31)};
Yves Gerey665174f2018-06-19 15:03:05 +0200224 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
225 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
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200229 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 41)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000230 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200231 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000232
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000233 // Second report should not change estimate.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200234 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 41)};
Yves Gerey665174f2018-06-19 15:03:05 +0200235 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
236 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200237 EXPECT_EQ(235360, 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.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200241 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 61)};
Yves Gerey665174f2018-06-19 15:03:05 +0200242 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
243 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200244 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
245 time_ms += 1000;
246
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200247 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 81)};
Yves Gerey665174f2018-06-19 15:03:05 +0200248 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
249 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100250 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000251 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000252
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200253 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 121)};
Yves Gerey665174f2018-06-19 15:03:05 +0200254 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
255 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100256 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000257 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000258
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000259 // Reach max cap.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200260 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 141)};
Yves Gerey665174f2018-06-19 15:03:05 +0200261 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
262 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100263 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000264
265 // Test that a low REMB trigger immediately.
266 // We don't care which bandwidth observer that delivers the REMB.
267 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100268 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000269 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
270 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000271
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000272 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000273 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100274 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000275}
276
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000277TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000278 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100279 const int kStartBitrate = 200000;
280 const int kMinBitrate = 100000;
281 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000282 controller_->SetStartBitrate(kStartBitrate);
283 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000284
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000285 // REMBs during the first 2 seconds apply immediately.
286 int64_t time_ms = 1001;
287 webrtc::ReportBlockList report_blocks;
288 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
289 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
290 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
291 report_blocks.clear();
292 time_ms += 2000;
293
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000294 // Receive a high REMB, test bitrate increase.
295 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
296
Stefan Holmere5904162015-03-26 11:11:06 +0100297 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000298 // Ramp up to max bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200299 for (int i = 0; i < 7; ++i) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000300 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
301 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
302 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
303 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000304 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
305 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
306 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
307 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000308 time_ms += 1000;
309 sequence_number[0] += 20;
310 sequence_number[1] += 1;
311 report_blocks.clear();
312 }
313
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000314 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000315
316 // Packet loss on the first stream. Verify that bitrate decreases.
317 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
318 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
319 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000320 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
321 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
322 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
323 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000324 sequence_number[0] += 20;
325 sequence_number[1] += 20;
326 time_ms += 1000;
327 report_blocks.clear();
328
329 // Packet loss on the second stream. Verify that bitrate decreases.
330 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
331 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
332 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000333 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
334 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
335 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
336 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000337 sequence_number[0] += 20;
338 sequence_number[1] += 1;
339 time_ms += 1000;
340 report_blocks.clear();
341
342 // All packets lost on stream with few packets, no back-off.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100343 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000344 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
345 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000346 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100347 EXPECT_EQ(WeightedLoss(20, 0, 1, 255), bitrate_observer_.last_fraction_loss_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000348 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
349 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000350 sequence_number[0] += 20;
351 sequence_number[1] += 1;
352 report_blocks.clear();
353}
354
Stefan Holmer52200d02016-09-20 14:14:23 +0200355TEST_F(BitrateControllerTest, TimeoutsWithoutFeedback) {
356 {
357 webrtc::test::ScopedFieldTrials override_field_trials(
Stefan Holmerdb158f92016-10-13 02:11:51 +0200358 "WebRTC-FeedbackTimeout/Enabled/");
Stefan Holmer52200d02016-09-20 14:14:23 +0200359 SetUp();
360 int expected_bitrate_bps = 300000;
361 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
362 kDefaultMaxBitrateBps);
363
364 webrtc::ReportBlockList report_blocks;
365 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
366 bandwidth_observer_->OnReceivedRtcpReceiverReport(
367 report_blocks, 50, clock_.TimeInMilliseconds());
368 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
369 clock_.AdvanceTimeMilliseconds(500);
370
371 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
372 bandwidth_observer_->OnReceivedRtcpReceiverReport(
373 report_blocks, 50, clock_.TimeInMilliseconds());
374 report_blocks.clear();
375 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
376 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
377 clock_.AdvanceTimeMilliseconds(1500);
378
379 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
380 bandwidth_observer_->OnReceivedRtcpReceiverReport(
381 report_blocks, 50, clock_.TimeInMilliseconds());
382 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
383 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700384 clock_.AdvanceTimeMilliseconds(4000);
Stefan Holmer52200d02016-09-20 14:14:23 +0200385
stefan5cb19822017-06-16 07:47:00 -0700386 // 4 seconds since feedback, expect increase.
Stefan Holmer52200d02016-09-20 14:14:23 +0200387 controller_->Process();
388 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
389 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700390 clock_.AdvanceTimeMilliseconds(2000);
Stefan Holmer52200d02016-09-20 14:14:23 +0200391
stefan5cb19822017-06-16 07:47:00 -0700392 // 6 seconds since feedback, expect no increase.
Stefan Holmer52200d02016-09-20 14:14:23 +0200393 controller_->Process();
394 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700395 clock_.AdvanceTimeMilliseconds(9001);
Stefan Holmer52200d02016-09-20 14:14:23 +0200396
stefan5cb19822017-06-16 07:47:00 -0700397 // More than 15 seconds since feedback, expect decrease.
Stefan Holmer52200d02016-09-20 14:14:23 +0200398 controller_->Process();
399 expected_bitrate_bps *= 0.8;
400 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
401 clock_.AdvanceTimeMilliseconds(500);
402
403 // Only one timeout every second.
404 controller_->Process();
405 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
406 clock_.AdvanceTimeMilliseconds(501);
407
408 // New timeout allowed.
409 controller_->Process();
410 expected_bitrate_bps *= 0.8;
411 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
412 }
413}
414
415TEST_F(BitrateControllerTest, StopIncreaseWithoutPacketReports) {
416 int expected_bitrate_bps = 300000;
417 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
418 kDefaultMaxBitrateBps);
419
420 webrtc::ReportBlockList report_blocks;
421 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
422 bandwidth_observer_->OnReceivedRtcpReceiverReport(
423 report_blocks, 50, clock_.TimeInMilliseconds());
424 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
425 clock_.AdvanceTimeMilliseconds(500);
426
427 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
428 bandwidth_observer_->OnReceivedRtcpReceiverReport(
429 report_blocks, 50, clock_.TimeInMilliseconds());
430 report_blocks.clear();
431 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
432 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
433 clock_.AdvanceTimeMilliseconds(1500);
434
435 // 1.2 seconds without packets reported as received, no increase.
436 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
437 bandwidth_observer_->OnReceivedRtcpReceiverReport(
438 report_blocks, 50, clock_.TimeInMilliseconds());
439 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
440 clock_.AdvanceTimeMilliseconds(1000);
441
442 // 5 packets reported as received since last, too few, no increase.
443 report_blocks.push_back(CreateReportBlock(1, 2, 0, 26));
444 bandwidth_observer_->OnReceivedRtcpReceiverReport(
445 report_blocks, 50, clock_.TimeInMilliseconds());
446 report_blocks.clear();
447 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
448 clock_.AdvanceTimeMilliseconds(100);
449
450 // 15 packets reported as received since last, enough to increase.
451 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
452 bandwidth_observer_->OnReceivedRtcpReceiverReport(
453 report_blocks, 50, clock_.TimeInMilliseconds());
454 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
455 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
456 clock_.AdvanceTimeMilliseconds(1000);
457}