pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <vector> |
| 13 | |
jbauch | f91e6d0 | 2016-01-24 23:05:21 -0800 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/pacing/mock/mock_paced_sender.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 19 | |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 20 | using ::testing::Exactly; |
| 21 | using ::testing::Return; |
| 22 | |
perkj | e30c272 | 2016-05-09 04:57:11 -0700 | [diff] [blame] | 23 | using webrtc::BitrateController; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 24 | using webrtc::BitrateObserver; |
| 25 | using webrtc::PacedSender; |
| 26 | using webrtc::RtcpBandwidthObserver; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 27 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 28 | uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1, |
| 29 | int num_packets2, uint8_t fraction_loss2) { |
| 30 | int weighted_sum = num_packets1 * fraction_loss1 + |
| 31 | num_packets2 * fraction_loss2; |
| 32 | int total_num_packets = num_packets1 + num_packets2; |
| 33 | return (weighted_sum + total_num_packets / 2) / total_num_packets; |
| 34 | } |
| 35 | |
| 36 | webrtc::RTCPReportBlock CreateReportBlock( |
| 37 | uint32_t remote_ssrc, uint32_t source_ssrc, |
| 38 | uint8_t fraction_lost, uint32_t extended_high_sequence_number) { |
| 39 | return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0, |
| 40 | extended_high_sequence_number, 0, 0, 0); |
| 41 | } |
| 42 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 43 | class TestBitrateObserver: public BitrateObserver { |
| 44 | public: |
| 45 | TestBitrateObserver() |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 46 | : last_bitrate_(0), |
| 47 | last_fraction_loss_(0), |
| 48 | last_rtt_(0) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 49 | } |
| 50 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 51 | virtual void OnNetworkChanged(uint32_t bitrate, |
| 52 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 53 | int64_t rtt) { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 54 | last_bitrate_ = static_cast<int>(bitrate); |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 55 | last_fraction_loss_ = fraction_loss; |
| 56 | last_rtt_ = rtt; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 57 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 58 | int last_bitrate_; |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 59 | uint8_t last_fraction_loss_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 60 | int64_t last_rtt_; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class BitrateControllerTest : public ::testing::Test { |
| 64 | protected: |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 65 | BitrateControllerTest() : clock_(0) {} |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 66 | ~BitrateControllerTest() {} |
| 67 | |
| 68 | virtual void SetUp() { |
ivoc | 9e03c3b | 2016-06-30 00:59:43 -0700 | [diff] [blame] | 69 | controller_ = |
| 70 | BitrateController::CreateBitrateController(&clock_, &bitrate_observer_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 71 | controller_->SetStartBitrate(kStartBitrateBps); |
| 72 | EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_); |
| 73 | controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps); |
| 74 | EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 75 | bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver(); |
| 76 | } |
| 77 | |
| 78 | virtual void TearDown() { |
| 79 | delete bandwidth_observer_; |
| 80 | delete controller_; |
| 81 | } |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 82 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 83 | const int kMinBitrateBps = 100000; |
| 84 | const int kStartBitrateBps = 200000; |
| 85 | const int kMaxBitrateBps = 300000; |
| 86 | |
| 87 | const int kDefaultMinBitrateBps = 10000; |
| 88 | const int kDefaultMaxBitrateBps = 1000000000; |
| 89 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 90 | webrtc::SimulatedClock clock_; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 91 | TestBitrateObserver bitrate_observer_; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 92 | BitrateController* controller_; |
| 93 | RtcpBandwidthObserver* bandwidth_observer_; |
| 94 | }; |
| 95 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 96 | TEST_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.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 108 | TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 109 | // 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 Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 114 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 115 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 116 | EXPECT_EQ(0, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 117 | 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.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 123 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 124 | // Test bitrate increase 8% per second. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 125 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
| 126 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 127 | EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 128 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 129 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 130 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 131 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 132 | report_blocks.clear(); |
| 133 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 134 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 135 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 136 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 137 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 138 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 139 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 140 | report_blocks.clear(); |
| 141 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 142 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 143 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 144 | 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 Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 149 | EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 150 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 151 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 152 | report_blocks.clear(); |
| 153 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 801)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 154 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 155 | EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 156 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 157 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 158 | // Reach max cap. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 159 | report_blocks.clear(); |
| 160 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 101)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 161 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 162 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 163 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 164 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 165 | report_blocks.clear(); |
| 166 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 167 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 168 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 169 | |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 170 | // Test that a low delay-based estimate limits the combined estimate. |
| 171 | controller_->UpdateDelayBasedEstimate(280000); |
| 172 | EXPECT_EQ(280000, bitrate_observer_.last_bitrate_); |
| 173 | |
| 174 | // Test that a low REMB limits the combined estimate. |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 175 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 176 | EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 177 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 178 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 179 | |
| 180 | bandwidth_observer_->OnReceivedEstimatedBitrate(1000); |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 181 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 185 | // REMBs during the first 2 seconds apply immediately. |
| 186 | int64_t time_ms = 1; |
| 187 | webrtc::ReportBlockList report_blocks; |
| 188 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 1)); |
| 189 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 190 | report_blocks.clear(); |
| 191 | time_ms += 500; |
| 192 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 193 | RtcpBandwidthObserver* second_bandwidth_observer = |
| 194 | controller_->CreateRtcpBandwidthObserver(); |
| 195 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 196 | // Test start bitrate. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 197 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 198 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 199 | report_blocks, 100, 1); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 200 | EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 201 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 202 | EXPECT_EQ(100, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 203 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 204 | |
| 205 | // Test bitrate increase 8% per second. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 206 | report_blocks.clear(); |
| 207 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 208 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 209 | time_ms += 500; |
| 210 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 211 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 212 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 213 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 214 | EXPECT_EQ(100, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 215 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 216 | |
| 217 | // Extra report should not change estimate. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 218 | report_blocks.clear(); |
| 219 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 31)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 220 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 221 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 222 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 223 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 224 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 225 | report_blocks.clear(); |
| 226 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 227 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 228 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 229 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 230 | // Second report should not change estimate. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 231 | report_blocks.clear(); |
| 232 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 233 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 234 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 235 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 236 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 237 | |
| 238 | // Reports from only one bandwidth observer is ok. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 239 | report_blocks.clear(); |
| 240 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 241 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 242 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 243 | EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 244 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 245 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 246 | report_blocks.clear(); |
| 247 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 81)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 248 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 249 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 250 | EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 251 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 252 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 253 | // Reach max cap. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 254 | report_blocks.clear(); |
| 255 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 121)); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 256 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 257 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 258 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 259 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 260 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 261 | report_blocks.clear(); |
| 262 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 263 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 264 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 265 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 266 | |
| 267 | // Test that a low REMB trigger immediately. |
| 268 | // We don't care which bandwidth observer that delivers the REMB. |
| 269 | second_bandwidth_observer->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 270 | EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 271 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 272 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 273 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 274 | // Min cap. |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 275 | bandwidth_observer_->OnReceivedEstimatedBitrate(1000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 276 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 277 | delete second_bandwidth_observer; |
| 278 | } |
| 279 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 280 | TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) { |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 281 | uint32_t sequence_number[2] = {0, 0xFF00}; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 282 | const int kStartBitrate = 200000; |
| 283 | const int kMinBitrate = 100000; |
| 284 | const int kMaxBitrate = 300000; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 285 | controller_->SetStartBitrate(kStartBitrate); |
| 286 | controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 287 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 288 | // REMBs during the first 2 seconds apply immediately. |
| 289 | int64_t time_ms = 1001; |
| 290 | webrtc::ReportBlockList report_blocks; |
| 291 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 292 | bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate); |
| 293 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 294 | report_blocks.clear(); |
| 295 | time_ms += 2000; |
| 296 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 297 | // Receive a high REMB, test bitrate increase. |
| 298 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
| 299 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 300 | int last_bitrate = 0; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 301 | // Ramp up to max bitrate. |
| 302 | for (int i = 0; i < 6; ++i) { |
| 303 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 304 | report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); |
| 305 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, |
| 306 | time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 307 | EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 308 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 309 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 310 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 311 | time_ms += 1000; |
| 312 | sequence_number[0] += 20; |
| 313 | sequence_number[1] += 1; |
| 314 | report_blocks.clear(); |
| 315 | } |
| 316 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 317 | EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 318 | |
| 319 | // Packet loss on the first stream. Verify that bitrate decreases. |
| 320 | report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0])); |
| 321 | report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); |
| 322 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 323 | EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 324 | EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_); |
| 325 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 326 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 327 | sequence_number[0] += 20; |
| 328 | sequence_number[1] += 20; |
| 329 | time_ms += 1000; |
| 330 | report_blocks.clear(); |
| 331 | |
| 332 | // Packet loss on the second stream. Verify that bitrate decreases. |
| 333 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 334 | report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1])); |
| 335 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 336 | EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 337 | EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_); |
| 338 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 339 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 340 | sequence_number[0] += 20; |
| 341 | sequence_number[1] += 1; |
| 342 | time_ms += 1000; |
| 343 | report_blocks.clear(); |
| 344 | |
| 345 | // All packets lost on stream with few packets, no back-off. |
| 346 | report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0])); |
| 347 | report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1])); |
| 348 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 349 | EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate); |
| 350 | EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_); |
| 351 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 352 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 353 | sequence_number[0] += 20; |
| 354 | sequence_number[1] += 1; |
| 355 | report_blocks.clear(); |
| 356 | } |
| 357 | |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 358 | TEST_F(BitrateControllerTest, SetReservedBitrate) { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 359 | // Receive successively lower REMBs, verify the reserved bitrate is deducted. |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 360 | controller_->SetReservedBitrate(0); |
| 361 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 362 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 363 | controller_->SetReservedBitrate(50000); |
| 364 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 365 | EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 366 | |
| 367 | controller_->SetReservedBitrate(0); |
| 368 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 369 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 370 | controller_->SetReservedBitrate(50000); |
| 371 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 372 | EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 373 | |
| 374 | controller_->SetReservedBitrate(0); |
| 375 | bandwidth_observer_->OnReceivedEstimatedBitrate(200000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 376 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 377 | controller_->SetReservedBitrate(30000); |
| 378 | bandwidth_observer_->OnReceivedEstimatedBitrate(200000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 379 | EXPECT_EQ(170000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 380 | |
| 381 | controller_->SetReservedBitrate(0); |
| 382 | bandwidth_observer_->OnReceivedEstimatedBitrate(160000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 383 | EXPECT_EQ(160000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 384 | controller_->SetReservedBitrate(30000); |
| 385 | bandwidth_observer_->OnReceivedEstimatedBitrate(160000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 386 | EXPECT_EQ(130000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 387 | |
| 388 | controller_->SetReservedBitrate(0); |
| 389 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 390 | EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 391 | controller_->SetReservedBitrate(10000); |
| 392 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 393 | EXPECT_EQ(110000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 394 | |
| 395 | controller_->SetReservedBitrate(0); |
| 396 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 397 | EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 398 | controller_->SetReservedBitrate(50000); |
| 399 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 400 | // Limited by min bitrate. |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 401 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 402 | |
| 403 | controller_->SetReservedBitrate(10000); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 404 | bandwidth_observer_->OnReceivedEstimatedBitrate(1); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 405 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 406 | } |