rtp_bitrate_configurator_unittest.cc (10562B)
1 /* 2 * Copyright (c) 2018 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 #include "call/rtp_bitrate_configurator.h" 11 12 #include <memory> 13 #include <optional> 14 15 #include "api/transport/bitrate_settings.h" 16 #include "test/gtest.h" 17 18 namespace webrtc { 19 using std::nullopt; 20 21 class RtpBitrateConfiguratorTest : public ::testing::Test { 22 public: 23 RtpBitrateConfiguratorTest() 24 : configurator_(new RtpBitrateConfigurator(BitrateConstraints())) {} 25 std::unique_ptr<RtpBitrateConfigurator> configurator_; 26 void UpdateConfigMatches(BitrateConstraints bitrate_config, 27 std::optional<int> min_bitrate_bps, 28 std::optional<int> start_bitrate_bps, 29 std::optional<int> max_bitrate_bps) { 30 std::optional<BitrateConstraints> result = 31 configurator_->UpdateWithSdpParameters(bitrate_config); 32 EXPECT_TRUE(result.has_value()); 33 if (start_bitrate_bps.has_value()) 34 EXPECT_EQ(result->start_bitrate_bps, start_bitrate_bps); 35 if (min_bitrate_bps.has_value()) 36 EXPECT_EQ(result->min_bitrate_bps, min_bitrate_bps); 37 if (max_bitrate_bps.has_value()) 38 EXPECT_EQ(result->max_bitrate_bps, max_bitrate_bps); 39 } 40 41 void UpdateMaskMatches(BitrateSettings bitrate_mask, 42 std::optional<int> min_bitrate_bps, 43 std::optional<int> start_bitrate_bps, 44 std::optional<int> max_bitrate_bps) { 45 std::optional<BitrateConstraints> result = 46 configurator_->UpdateWithClientPreferences(bitrate_mask); 47 EXPECT_TRUE(result.has_value()); 48 if (start_bitrate_bps.has_value()) 49 EXPECT_EQ(result->start_bitrate_bps, start_bitrate_bps); 50 if (min_bitrate_bps.has_value()) 51 EXPECT_EQ(result->min_bitrate_bps, min_bitrate_bps); 52 if (max_bitrate_bps.has_value()) 53 EXPECT_EQ(result->max_bitrate_bps, max_bitrate_bps); 54 } 55 }; 56 57 TEST_F(RtpBitrateConfiguratorTest, NewConfigWithValidConfigReturnsNewConfig) { 58 BitrateConstraints bitrate_config; 59 bitrate_config.min_bitrate_bps = 1; 60 bitrate_config.start_bitrate_bps = 2; 61 bitrate_config.max_bitrate_bps = 3; 62 63 UpdateConfigMatches(bitrate_config, 1, 2, 3); 64 } 65 66 TEST_F(RtpBitrateConfiguratorTest, NewConfigWithDifferentMinReturnsNewConfig) { 67 BitrateConstraints bitrate_config; 68 bitrate_config.min_bitrate_bps = 10; 69 bitrate_config.start_bitrate_bps = 20; 70 bitrate_config.max_bitrate_bps = 30; 71 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 72 73 bitrate_config.min_bitrate_bps = 11; 74 UpdateConfigMatches(bitrate_config, 11, -1, 30); 75 } 76 77 TEST_F(RtpBitrateConfiguratorTest, 78 NewConfigWithDifferentStartReturnsNewConfig) { 79 BitrateConstraints bitrate_config; 80 bitrate_config.min_bitrate_bps = 10; 81 bitrate_config.start_bitrate_bps = 20; 82 bitrate_config.max_bitrate_bps = 30; 83 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 84 85 bitrate_config.start_bitrate_bps = 21; 86 UpdateConfigMatches(bitrate_config, 10, 21, 30); 87 } 88 89 TEST_F(RtpBitrateConfiguratorTest, NewConfigWithDifferentMaxReturnsNewConfig) { 90 BitrateConstraints bitrate_config; 91 bitrate_config.min_bitrate_bps = 10; 92 bitrate_config.start_bitrate_bps = 20; 93 bitrate_config.max_bitrate_bps = 30; 94 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 95 96 bitrate_config.max_bitrate_bps = 31; 97 UpdateConfigMatches(bitrate_config, 10, -1, 31); 98 } 99 100 TEST_F(RtpBitrateConfiguratorTest, NewConfigWithSameConfigElidesSecondCall) { 101 BitrateConstraints bitrate_config; 102 bitrate_config.min_bitrate_bps = 1; 103 bitrate_config.start_bitrate_bps = 2; 104 bitrate_config.max_bitrate_bps = 3; 105 106 UpdateConfigMatches(bitrate_config, 1, 2, 3); 107 EXPECT_FALSE( 108 configurator_->UpdateWithSdpParameters(bitrate_config).has_value()); 109 } 110 111 TEST_F(RtpBitrateConfiguratorTest, 112 NewConfigWithSameMinMaxAndNegativeStartElidesSecondCall) { 113 BitrateConstraints bitrate_config; 114 bitrate_config.min_bitrate_bps = 1; 115 bitrate_config.start_bitrate_bps = 2; 116 bitrate_config.max_bitrate_bps = 3; 117 118 UpdateConfigMatches(bitrate_config, 1, 2, 3); 119 120 bitrate_config.start_bitrate_bps = -1; 121 EXPECT_FALSE( 122 configurator_->UpdateWithSdpParameters(bitrate_config).has_value()); 123 } 124 125 TEST_F(RtpBitrateConfiguratorTest, BiggerMaskMinUsed) { 126 BitrateSettings mask; 127 mask.min_bitrate_bps = 1234; 128 UpdateMaskMatches(mask, *mask.min_bitrate_bps, nullopt, nullopt); 129 } 130 131 TEST_F(RtpBitrateConfiguratorTest, BiggerConfigMinUsed) { 132 BitrateSettings mask; 133 mask.min_bitrate_bps = 1000; 134 UpdateMaskMatches(mask, 1000, nullopt, nullopt); 135 136 BitrateConstraints config; 137 config.min_bitrate_bps = 1234; 138 UpdateConfigMatches(config, 1234, nullopt, nullopt); 139 } 140 141 // The last call to set start should be used. 142 TEST_F(RtpBitrateConfiguratorTest, LatestStartMaskPreferred) { 143 BitrateSettings mask; 144 mask.start_bitrate_bps = 1300; 145 UpdateMaskMatches(mask, nullopt, *mask.start_bitrate_bps, nullopt); 146 147 BitrateConstraints bitrate_config; 148 bitrate_config.start_bitrate_bps = 1200; 149 150 UpdateConfigMatches(bitrate_config, nullopt, bitrate_config.start_bitrate_bps, 151 nullopt); 152 } 153 154 TEST_F(RtpBitrateConfiguratorTest, SmallerMaskMaxUsed) { 155 BitrateConstraints bitrate_config; 156 bitrate_config.max_bitrate_bps = bitrate_config.start_bitrate_bps + 2000; 157 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 158 159 BitrateSettings mask; 160 mask.max_bitrate_bps = bitrate_config.start_bitrate_bps + 1000; 161 162 UpdateMaskMatches(mask, nullopt, nullopt, *mask.max_bitrate_bps); 163 } 164 165 TEST_F(RtpBitrateConfiguratorTest, SmallerConfigMaxUsed) { 166 BitrateConstraints bitrate_config; 167 bitrate_config.max_bitrate_bps = bitrate_config.start_bitrate_bps + 1000; 168 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 169 170 BitrateSettings mask; 171 mask.max_bitrate_bps = bitrate_config.start_bitrate_bps + 2000; 172 173 // Expect no return because nothing changes 174 EXPECT_FALSE(configurator_->UpdateWithClientPreferences(mask).has_value()); 175 } 176 177 TEST_F(RtpBitrateConfiguratorTest, MaskStartLessThanConfigMinClamped) { 178 BitrateConstraints bitrate_config; 179 bitrate_config.min_bitrate_bps = 2000; 180 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 181 182 BitrateSettings mask; 183 mask.start_bitrate_bps = 1000; 184 UpdateMaskMatches(mask, 2000, 2000, nullopt); 185 } 186 187 TEST_F(RtpBitrateConfiguratorTest, MaskStartGreaterThanConfigMaxClamped) { 188 BitrateConstraints bitrate_config; 189 bitrate_config.start_bitrate_bps = 2000; 190 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 191 192 BitrateSettings mask; 193 mask.max_bitrate_bps = 1000; 194 195 UpdateMaskMatches(mask, nullopt, -1, 1000); 196 } 197 198 TEST_F(RtpBitrateConfiguratorTest, MaskMinGreaterThanConfigMaxClamped) { 199 BitrateConstraints bitrate_config; 200 bitrate_config.min_bitrate_bps = 2000; 201 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 202 203 BitrateSettings mask; 204 mask.max_bitrate_bps = 1000; 205 206 UpdateMaskMatches(mask, 1000, nullopt, 1000); 207 } 208 209 TEST_F(RtpBitrateConfiguratorTest, SettingMaskStartForcesUpdate) { 210 BitrateSettings mask; 211 mask.start_bitrate_bps = 1000; 212 213 // Config should be returned twice with the same params since 214 // start_bitrate_bps is set. 215 UpdateMaskMatches(mask, nullopt, 1000, nullopt); 216 UpdateMaskMatches(mask, nullopt, 1000, nullopt); 217 } 218 219 TEST_F(RtpBitrateConfiguratorTest, NewConfigWithNoChangesDoesNotCallNewConfig) { 220 BitrateConstraints config1; 221 config1.min_bitrate_bps = 0; 222 config1.start_bitrate_bps = 1000; 223 config1.max_bitrate_bps = -1; 224 225 BitrateConstraints config2; 226 config2.min_bitrate_bps = 0; 227 config2.start_bitrate_bps = -1; 228 config2.max_bitrate_bps = -1; 229 230 // The second call should not return anything because it doesn't 231 // change any values. 232 UpdateConfigMatches(config1, 0, 1000, -1); 233 EXPECT_FALSE(configurator_->UpdateWithSdpParameters(config2).has_value()); 234 } 235 236 // If config changes the max, but not the effective max, 237 // new config shouldn't be returned, to avoid unnecessary encoder 238 // reconfigurations. 239 TEST_F(RtpBitrateConfiguratorTest, 240 NewConfigNotReturnedWhenEffectiveMaxUnchanged) { 241 BitrateConstraints config; 242 config.min_bitrate_bps = 0; 243 config.start_bitrate_bps = -1; 244 config.max_bitrate_bps = 2000; 245 UpdateConfigMatches(config, nullopt, nullopt, 2000); 246 247 // Reduce effective max to 1000 with the mask. 248 BitrateSettings mask; 249 mask.max_bitrate_bps = 1000; 250 UpdateMaskMatches(mask, nullopt, nullopt, 1000); 251 252 // This leaves the effective max unchanged, so new config shouldn't be 253 // returned again. 254 config.max_bitrate_bps = 1000; 255 EXPECT_FALSE(configurator_->UpdateWithSdpParameters(config).has_value()); 256 } 257 258 // When the "start bitrate" mask is removed, new config shouldn't be returned 259 // again, since nothing's changing. 260 TEST_F(RtpBitrateConfiguratorTest, NewConfigNotReturnedWhenStartMaskRemoved) { 261 BitrateSettings mask; 262 mask.start_bitrate_bps = 1000; 263 UpdateMaskMatches(mask, 0, 1000, -1); 264 265 mask.start_bitrate_bps.reset(); 266 EXPECT_FALSE(configurator_->UpdateWithClientPreferences(mask).has_value()); 267 } 268 269 // Test that if a new config is returned after BitrateSettings applies a 270 // "start" value, the new config won't return that start value a 271 // second time. 272 TEST_F(RtpBitrateConfiguratorTest, NewConfigAfterBitrateConfigMaskWithStart) { 273 BitrateSettings mask; 274 mask.start_bitrate_bps = 1000; 275 UpdateMaskMatches(mask, 0, 1000, -1); 276 277 BitrateConstraints config; 278 config.min_bitrate_bps = 0; 279 config.start_bitrate_bps = -1; 280 config.max_bitrate_bps = 5000; 281 // The start value isn't changing, so new config should be returned with 282 // -1. 283 UpdateConfigMatches(config, 0, -1, 5000); 284 } 285 286 TEST_F(RtpBitrateConfiguratorTest, 287 NewConfigNotReturnedWhenClampedMinUnchanged) { 288 BitrateConstraints bitrate_config; 289 bitrate_config.start_bitrate_bps = 500; 290 bitrate_config.max_bitrate_bps = 1000; 291 configurator_.reset(new RtpBitrateConfigurator(bitrate_config)); 292 293 // Set min to 2000; it is clamped to the max (1000). 294 BitrateSettings mask; 295 mask.min_bitrate_bps = 2000; 296 UpdateMaskMatches(mask, 1000, -1, 1000); 297 298 // Set min to 3000; the clamped value stays the same so nothing happens. 299 mask.min_bitrate_bps = 3000; 300 EXPECT_FALSE(configurator_->UpdateWithClientPreferences(mask).has_value()); 301 } 302 } // namespace webrtc