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