1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "call/bitrate_allocator.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "api/call/bitrate_allocation.h"
#include "api/transport/network_types.h"
#include "api/units/data_rate.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "test/explicit_key_value_config.h"
#include "test/gmock.h"
#include "test/gtest.h"
using ::testing::_;
using ::testing::AllOf;
using ::testing::Field;
using ::testing::NiceMock;
namespace webrtc {
namespace {
auto AllocationLimitsEq(uint32_t min_allocatable_rate_bps,
uint32_t max_padding_rate_bps,
uint32_t max_allocatable_rate_bps) {
return AllOf(Field(&BitrateAllocationLimits::min_allocatable_rate,
DataRate::BitsPerSec(min_allocatable_rate_bps)),
Field(&BitrateAllocationLimits::max_allocatable_rate,
DataRate::BitsPerSec(max_allocatable_rate_bps)),
Field(&BitrateAllocationLimits::max_padding_rate,
DataRate::BitsPerSec(max_padding_rate_bps)));
}
auto AllocationLimitsEq(uint32_t min_allocatable_rate_bps,
uint32_t max_padding_rate_bps) {
return AllOf(Field(&BitrateAllocationLimits::min_allocatable_rate,
DataRate::BitsPerSec(min_allocatable_rate_bps)),
Field(&BitrateAllocationLimits::max_padding_rate,
DataRate::BitsPerSec(max_padding_rate_bps)));
}
class MockLimitObserver : public BitrateAllocator::LimitObserver {
public:
MOCK_METHOD(void,
OnAllocationLimitsChanged,
(BitrateAllocationLimits),
(override));
};
class TestBitrateObserver : public BitrateAllocatorObserver {
public:
TestBitrateObserver()
: last_bitrate_bps_(0),
last_fraction_loss_(0),
last_rtt_ms_(0),
last_probing_interval_ms_(0),
protection_ratio_(0.0) {}
void SetBitrateProtectionRatio(double protection_ratio) {
protection_ratio_ = protection_ratio;
}
uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override {
last_bitrate_bps_ = update.target_bitrate.bps();
last_fraction_loss_ =
dchecked_cast<uint8_t>(update.packet_loss_ratio * 256);
last_rtt_ms_ = update.round_trip_time.ms();
last_probing_interval_ms_ = update.bwe_period.ms();
return update.target_bitrate.bps() * protection_ratio_;
}
std::optional<DataRate> GetUsedRate() const override { return std::nullopt; }
uint32_t last_bitrate_bps_;
uint8_t last_fraction_loss_;
int64_t last_rtt_ms_;
int last_probing_interval_ms_;
double protection_ratio_;
};
class TestContributingBitrateObserver : public TestBitrateObserver {
public:
TestContributingBitrateObserver() : rate_usage_(DataRate::Zero()) {}
std::optional<DataRate> GetUsedRate() const override { return rate_usage_; }
DataRate rate_usage_;
};
constexpr int64_t kDefaultProbingIntervalMs = 3000;
const double kDefaultBitratePriority = 1.0;
TargetTransferRate CreateTargetRateMessage(uint32_t target_bitrate_bps,
uint8_t fraction_loss,
int64_t rtt_ms,
int64_t bwe_period_ms) {
TargetTransferRate msg;
// The timestamp is just for log output, keeping it fixed just means fewer log
// messages in the test.
msg.at_time = Timestamp::Seconds(10000);
msg.target_rate = DataRate::BitsPerSec(target_bitrate_bps);
msg.stable_target_rate = msg.target_rate;
msg.network_estimate.bandwidth = msg.target_rate;
msg.network_estimate.loss_rate_ratio = fraction_loss / 255.0;
msg.network_estimate.round_trip_time = TimeDelta::Millis(rtt_ms);
msg.network_estimate.bwe_period = TimeDelta::Millis(bwe_period_ms);
return msg;
}
} // namespace
class BitrateAllocatorTest : public ::testing::Test {
protected:
BitrateAllocatorTest()
: allocator_(new BitrateAllocator(&limit_observer_, DataRate::Zero())) {
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000u, 0, 0, kDefaultProbingIntervalMs));
}
~BitrateAllocatorTest() {}
void AddObserver(
BitrateAllocatorObserver* observer,
uint32_t min_bitrate_bps,
uint32_t max_bitrate_bps,
uint32_t pad_up_bitrate_bps,
bool enforce_min_bitrate,
double bitrate_priority,
std::optional<TrackRateElasticity> rate_elasticity = std::nullopt) {
allocator_->AddObserver(
observer, {min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
/* priority_bitrate */ 0, enforce_min_bitrate,
bitrate_priority, rate_elasticity});
}
MediaStreamAllocationConfig DefaultConfig() const {
MediaStreamAllocationConfig default_config;
default_config.min_bitrate_bps = 0;
default_config.max_bitrate_bps = 1500000;
default_config.pad_up_bitrate_bps = 0;
default_config.priority_bitrate_bps = 0;
default_config.enforce_min_bitrate = true;
default_config.bitrate_priority = kDefaultBitratePriority;
return default_config;
}
void ReconfigureAllocator(DataRate elastic_rate_upper_limit) {
allocator_.reset(
new BitrateAllocator(&limit_observer_, elastic_rate_upper_limit));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000u, 0, 0, kDefaultProbingIntervalMs));
}
NiceMock<MockLimitObserver> limit_observer_;
std::unique_ptr<BitrateAllocator> allocator_;
};
TEST_F(BitrateAllocatorTest, RespectsPriorityBitrate) {
TestBitrateObserver stream_a;
auto config_a = DefaultConfig();
config_a.min_bitrate_bps = 100000;
config_a.priority_bitrate_bps = 0;
allocator_->AddObserver(&stream_a, config_a);
TestBitrateObserver stream_b;
auto config_b = DefaultConfig();
config_b.min_bitrate_bps = 100000;
config_b.max_bitrate_bps = 300000;
config_b.priority_bitrate_bps = 300000;
allocator_->AddObserver(&stream_b, config_b);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(100000, 0, 0, 0));
EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
EXPECT_EQ(stream_b.last_bitrate_bps_, 100000u);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000, 0, 0, 0));
EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
EXPECT_EQ(stream_b.last_bitrate_bps_, 100000u);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 0, 0));
EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
EXPECT_EQ(stream_b.last_bitrate_bps_, 200000u);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(400000, 0, 0, 0));
EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
EXPECT_EQ(stream_b.last_bitrate_bps_, 300000u);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(800000, 0, 0, 0));
EXPECT_EQ(stream_a.last_bitrate_bps_, 500000u);
EXPECT_EQ(stream_b.last_bitrate_bps_, 300000u);
}
TEST_F(BitrateAllocatorTest, UpdatingBitrateObserver) {
TestBitrateObserver bitrate_observer;
const uint32_t kMinSendBitrateBps = 100000;
const uint32_t kPadUpToBitrateBps = 50000;
const uint32_t kMaxBitrateBps = 1500000;
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(
kMinSendBitrateBps, kPadUpToBitrateBps, kMaxBitrateBps)));
AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps,
kPadUpToBitrateBps, true, kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer));
// TODO(pbos): Expect capping to 1.5M instead of 3M when not boosting the max
// bitrate for FEC/retransmissions (see todo in BitrateAllocator).
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(4000000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(3000000, allocator_->GetStartBitrate(&bitrate_observer));
// Expect `max_padding_bitrate_bps` to change to 0 if the observer is updated.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
AllocationLimitsEq(kMinSendBitrateBps, 0)));
AddObserver(&bitrate_observer, kMinSendBitrateBps, 4000000, 0, true,
kDefaultBitratePriority);
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
AllocationLimitsEq(kMinSendBitrateBps, 0)));
EXPECT_EQ(4000000, allocator_->GetStartBitrate(&bitrate_observer));
AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(3000000, allocator_->GetStartBitrate(&bitrate_observer));
EXPECT_EQ(3000000u, bitrate_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(kMaxBitrateBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_bps_);
}
TEST_F(BitrateAllocatorTest, TwoBitrateObserversOneRtcpObserver) {
TestBitrateObserver bitrate_observer_1;
TestBitrateObserver bitrate_observer_2;
const uint32_t kObs1StartBitrateBps = 100000;
const uint32_t kObs2StartBitrateBps = 200000;
const uint32_t kObs1MaxBitrateBps = 300000;
const uint32_t kObs2MaxBitrateBps = 300000;
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(
kObs1StartBitrateBps, 0, kObs1MaxBitrateBps)));
AddObserver(&bitrate_observer_1, kObs1StartBitrateBps, kObs1MaxBitrateBps, 0,
true, kDefaultBitratePriority);
EXPECT_EQ(static_cast<int>(kObs1MaxBitrateBps),
allocator_->GetStartBitrate(&bitrate_observer_1));
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(
kObs1StartBitrateBps + kObs2StartBitrateBps, 0,
kObs1MaxBitrateBps + kObs2MaxBitrateBps)));
AddObserver(&bitrate_observer_2, kObs2StartBitrateBps, kObs2MaxBitrateBps, 0,
true, kDefaultBitratePriority);
EXPECT_EQ(static_cast<int>(kObs2StartBitrateBps),
allocator_->GetStartBitrate(&bitrate_observer_2));
// Test too low start bitrate, hence lower than sum of min. Min bitrates
// will
// be allocated to all observers.
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kObs2StartBitrateBps, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
EXPECT_EQ(50, bitrate_observer_1.last_rtt_ms_);
EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
EXPECT_EQ(50, bitrate_observer_2.last_rtt_ms_);
// Test a bitrate which should be distributed equally.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(500000, 0, 50, kDefaultProbingIntervalMs));
const uint32_t kBitrateToShare =
500000 - kObs2StartBitrateBps - kObs1StartBitrateBps;
EXPECT_EQ(100000u + kBitrateToShare / 2,
bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(200000u + kBitrateToShare / 2,
bitrate_observer_2.last_bitrate_bps_);
// Limited by 2x max bitrates since we leave room for FEC and
// retransmissions.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(1500000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(600000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(600000u, bitrate_observer_2.last_bitrate_bps_);
// Verify that if the bandwidth estimate is set to zero, the allocated
// rate is
// zero.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(0, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
}
TEST_F(BitrateAllocatorTest, RemoveObserverTriggersLimitObserver) {
TestBitrateObserver bitrate_observer;
const uint32_t kMinSendBitrateBps = 100000;
const uint32_t kPadUpToBitrateBps = 50000;
const uint32_t kMaxBitrateBps = 1500000;
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(
kMinSendBitrateBps, kPadUpToBitrateBps, kMaxBitrateBps)));
AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps,
kPadUpToBitrateBps, true, kDefaultBitratePriority);
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
allocator_->RemoveObserver(&bitrate_observer);
}
class BitrateAllocatorTestNoEnforceMin : public ::testing::Test {
protected:
BitrateAllocatorTestNoEnforceMin()
: allocator_(new BitrateAllocator(&limit_observer_, DataRate::Zero())) {
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000u, 0, 0, kDefaultProbingIntervalMs));
}
~BitrateAllocatorTestNoEnforceMin() {}
void AddObserver(BitrateAllocatorObserver* observer,
uint32_t min_bitrate_bps,
uint32_t max_bitrate_bps,
uint32_t pad_up_bitrate_bps,
bool enforce_min_bitrate,
absl::string_view /* track_id */,
double bitrate_priority) {
allocator_->AddObserver(
observer, {min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps, 0,
enforce_min_bitrate, bitrate_priority});
}
NiceMock<MockLimitObserver> limit_observer_;
std::unique_ptr<BitrateAllocator> allocator_;
};
// The following three tests verify enforcing a minimum bitrate works as
// intended.
TEST_F(BitrateAllocatorTestNoEnforceMin, OneBitrateObserver) {
TestBitrateObserver bitrate_observer_1;
// Expect OnAllocationLimitsChanged with `min_send_bitrate_bps` = 0 since
// AddObserver is called with `enforce_min_bitrate` = false.
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(0, 120000)));
AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
// High BWE.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(150000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_bps_);
// Low BWE.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(10000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
allocator_->RemoveObserver(&bitrate_observer_1);
}
TEST_F(BitrateAllocatorTestNoEnforceMin, ThreeBitrateObservers) {
TestBitrateObserver bitrate_observer_1;
TestBitrateObserver bitrate_observer_2;
TestBitrateObserver bitrate_observer_3;
// Set up the observers with min bitrates at 100000, 200000, and 300000.
AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
AddObserver(&bitrate_observer_2, 200000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
AddObserver(&bitrate_observer_3, 300000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(0, allocator_->GetStartBitrate(&bitrate_observer_3));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
// High BWE. Make sure the controllers get a fair share of the surplus (i.e.,
// what is left after each controller gets its min rate).
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(690000, 0, 0, kDefaultProbingIntervalMs));
// Verify that each observer gets its min rate (sum of min rates is 600000),
// and that the remaining 90000 is divided equally among the three.
uint32_t bitrate_to_share = 690000u - 100000u - 200000u - 300000u;
EXPECT_EQ(100000u + bitrate_to_share / 3,
bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(200000u + bitrate_to_share / 3,
bitrate_observer_2.last_bitrate_bps_);
EXPECT_EQ(300000u + bitrate_to_share / 3,
bitrate_observer_3.last_bitrate_bps_);
// BWE below the sum of observer's min bitrate.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_); // Min bitrate.
EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_); // Min bitrate.
EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_); // Nothing.
// Increased BWE, but still below the sum of configured min bitrates for all
// observers and too little for observer 3. 1 and 2 will share the rest.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(500000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(200000u, bitrate_observer_1.last_bitrate_bps_); // Min + split.
EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_bps_); // Min + split.
EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_); // Nothing.
// Below min for all.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(10000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_);
// Verify that zero estimated bandwidth, means that that all gets zero,
// regardless of set min bitrate.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(0, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_);
allocator_->RemoveObserver(&bitrate_observer_1);
allocator_->RemoveObserver(&bitrate_observer_2);
allocator_->RemoveObserver(&bitrate_observer_3);
}
TEST_F(BitrateAllocatorTestNoEnforceMin, OneBitrateObserverWithPacketLoss) {
const uint32_t kMinBitrateBps = 100000;
const uint32_t kMaxBitrateBps = 400000;
// Hysteresis adds another 10% or 20kbps to min bitrate.
const uint32_t kMinStartBitrateBps =
kMinBitrateBps + std::max(20000u, kMinBitrateBps / 10);
// Expect OnAllocationLimitsChanged with `min_send_bitrate_bps` = 0 since
// AddObserver is called with `enforce_min_bitrate` = false.
TestBitrateObserver bitrate_observer;
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
AllocationLimitsEq(0, 0, kMaxBitrateBps)));
AddObserver(&bitrate_observer, kMinBitrateBps, kMaxBitrateBps, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer));
// High BWE.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(150000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(150000u, bitrate_observer.last_bitrate_bps_);
// Add loss and use a part of the bitrate for protection.
const double kProtectionRatio = 0.4;
const uint8_t fraction_loss = kProtectionRatio * 256;
bitrate_observer.SetBitrateProtectionRatio(kProtectionRatio);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
200000, 0, fraction_loss, kDefaultProbingIntervalMs));
EXPECT_EQ(200000u, bitrate_observer.last_bitrate_bps_);
// Above the min threshold, but not enough given the protection used.
// Limits changed, as we will video is now off and we need to pad up to the
// start bitrate.
// Verify the hysteresis is added for the protection.
const uint32_t kMinStartBitrateWithProtectionBps =
static_cast<uint32_t>(kMinStartBitrateBps * (1 + kProtectionRatio));
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(
0, kMinStartBitrateWithProtectionBps, kMaxBitrateBps)));
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kMinStartBitrateBps + 1000, 0, fraction_loss, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(kMinStartBitrateWithProtectionBps - 1000, 0,
fraction_loss, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer.last_bitrate_bps_);
// Just enough to enable video again.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
AllocationLimitsEq(0, 0, kMaxBitrateBps)));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(kMinStartBitrateWithProtectionBps, 0,
fraction_loss, kDefaultProbingIntervalMs));
EXPECT_EQ(kMinStartBitrateWithProtectionBps,
bitrate_observer.last_bitrate_bps_);
// Remove all protection and make sure video is not paused as earlier.
bitrate_observer.SetBitrateProtectionRatio(0.0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(kMinStartBitrateWithProtectionBps - 1000, 0, 0,
kDefaultProbingIntervalMs));
EXPECT_EQ(kMinStartBitrateWithProtectionBps - 1000,
bitrate_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kMinStartBitrateBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(kMinStartBitrateBps, bitrate_observer.last_bitrate_bps_);
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(0, 0, 0)));
allocator_->RemoveObserver(&bitrate_observer);
}
TEST_F(BitrateAllocatorTest,
TotalAllocationLimitsAreUnaffectedByProtectionRatio) {
TestBitrateObserver bitrate_observer;
const uint32_t kMinBitrateBps = 100000;
const uint32_t kMaxBitrateBps = 400000;
// Register `bitrate_observer` and expect total allocation limits to change.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(AllocationLimitsEq(
kMinBitrateBps, 0, kMaxBitrateBps)))
.Times(1);
MediaStreamAllocationConfig allocation_config = DefaultConfig();
allocation_config.min_bitrate_bps = kMinBitrateBps;
allocation_config.max_bitrate_bps = kMaxBitrateBps;
allocator_->AddObserver(&bitrate_observer, allocation_config);
// Observer uses 20% of it's allocated bitrate for protection.
bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.2);
// Total allocation limits are unaffected by the protection rate change.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
// Observer uses 0% of it's allocated bitrate for protection.
bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.0);
// Total allocation limits are unaffected by the protection rate change.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
// Observer again uses 20% of it's allocated bitrate for protection.
bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.2);
// Total allocation limits are unaffected by the protection rate change.
EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
}
TEST_F(BitrateAllocatorTestNoEnforceMin, TwoBitrateObserverWithPacketLoss) {
TestBitrateObserver bitrate_observer_1;
TestBitrateObserver bitrate_observer_2;
AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
AddObserver(&bitrate_observer_2, 200000, 400000, 0, false, "",
kDefaultBitratePriority);
EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
// Enough bitrate for both.
bitrate_observer_2.SetBitrateProtectionRatio(0.5);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
// Above min for observer 2, but too little given the protection used.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(330000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(330000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(100000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(99999, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(119000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(120000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(120000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
// Verify the protection is accounted for before resuming observer 2.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(429000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(400000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(430000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(330000u, bitrate_observer_2.last_bitrate_bps_);
allocator_->RemoveObserver(&bitrate_observer_1);
allocator_->RemoveObserver(&bitrate_observer_2);
}
TEST_F(BitrateAllocatorTest, ThreeBitrateObserversLowBweEnforceMin) {
TestBitrateObserver bitrate_observer_1;
TestBitrateObserver bitrate_observer_2;
TestBitrateObserver bitrate_observer_3;
AddObserver(&bitrate_observer_1, 100000, 400000, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
AddObserver(&bitrate_observer_2, 200000, 400000, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
AddObserver(&bitrate_observer_3, 300000, 400000, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_3));
EXPECT_EQ(100000, static_cast<int>(bitrate_observer_1.last_bitrate_bps_));
EXPECT_EQ(200000, static_cast<int>(bitrate_observer_2.last_bitrate_bps_));
// Low BWE. Verify that all observers still get their respective min
// bitrate.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(1000, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_); // Min cap.
EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_); // Min cap.
EXPECT_EQ(300000u, bitrate_observer_3.last_bitrate_bps_); // Min cap.
allocator_->RemoveObserver(&bitrate_observer_1);
allocator_->RemoveObserver(&bitrate_observer_2);
allocator_->RemoveObserver(&bitrate_observer_3);
}
TEST_F(BitrateAllocatorTest, AddObserverWhileNetworkDown) {
TestBitrateObserver bitrate_observer_1;
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(50000, 0)));
AddObserver(&bitrate_observer_1, 50000, 400000, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
// Set network down, ie, no available bitrate.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(0, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
TestBitrateObserver bitrate_observer_2;
// Adding an observer while the network is down should not affect the limits.
EXPECT_CALL(limit_observer_,
OnAllocationLimitsChanged(AllocationLimitsEq(50000 + 50000, 0)));
AddObserver(&bitrate_observer_2, 50000, 400000, 0, true,
kDefaultBitratePriority);
// Expect the start_bitrate to be set as if the network was still up but that
// the new observer have been notified that the network is down.
EXPECT_EQ(300000 / 2, allocator_->GetStartBitrate(&bitrate_observer_2));
EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
// Set network back up.
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(1500000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(750000u, bitrate_observer_1.last_bitrate_bps_);
EXPECT_EQ(750000u, bitrate_observer_2.last_bitrate_bps_);
}
TEST_F(BitrateAllocatorTest, MixedEnforecedConfigs) {
TestBitrateObserver enforced_observer;
AddObserver(&enforced_observer, 6000, 30000, 0, true,
kDefaultBitratePriority);
EXPECT_EQ(60000, allocator_->GetStartBitrate(&enforced_observer));
TestBitrateObserver not_enforced_observer;
AddObserver(¬_enforced_observer, 30000, 2500000, 0, false,
kDefaultBitratePriority);
EXPECT_EQ(270000, allocator_->GetStartBitrate(¬_enforced_observer));
EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(36000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(30000u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(35000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(5000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(36000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(55000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(56000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(50000u, not_enforced_observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(56000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(16000u, enforced_observer.last_bitrate_bps_);
EXPECT_EQ(40000u, not_enforced_observer.last_bitrate_bps_);
allocator_->RemoveObserver(&enforced_observer);
allocator_->RemoveObserver(¬_enforced_observer);
}
TEST_F(BitrateAllocatorTest, AvoidToggleAbsolute) {
TestBitrateObserver observer;
AddObserver(&observer, 30000, 300000, 0, false, kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(30000u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(20000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(49000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(50000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(50000u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(30000u, observer.last_bitrate_bps_);
allocator_->RemoveObserver(&observer);
}
TEST_F(BitrateAllocatorTest, AvoidTogglePercent) {
TestBitrateObserver observer;
AddObserver(&observer, 300000, 600000, 0, false, kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(300000u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(329000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(0u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(330000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(330000u, observer.last_bitrate_bps_);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
EXPECT_EQ(300000u, observer.last_bitrate_bps_);
allocator_->RemoveObserver(&observer);
}
TEST_F(BitrateAllocatorTest, PassProbingInterval) {
TestBitrateObserver observer;
AddObserver(&observer, 300000, 600000, 0, false, kDefaultBitratePriority);
EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(300000, 0, 50, 5000));
EXPECT_EQ(5000, observer.last_probing_interval_ms_);
allocator_->RemoveObserver(&observer);
}
TEST_F(BitrateAllocatorTest, PriorityRateOneObserverBasic) {
TestBitrateObserver observer;
const uint32_t kMinSendBitrateBps = 10;
const uint32_t kMaxSendBitrateBps = 60;
const uint32_t kNetworkBandwidthBps = 30;
AddObserver(&observer, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true, 2.0);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(kNetworkBandwidthBps, observer.last_bitrate_bps_);
allocator_->RemoveObserver(&observer);
}
// Tests that two observers with the same bitrate priority are allocated
// their bitrate evenly.
TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBasic) {
TestBitrateObserver observer_low_1;
TestBitrateObserver observer_low_2;
const uint32_t kMinSendBitrateBps = 10;
const uint32_t kMaxSendBitrateBps = 60;
const uint32_t kNetworkBandwidthBps = 60;
AddObserver(&observer_low_1, kMinSendBitrateBps, kMaxSendBitrateBps, 0, false,
2.0);
AddObserver(&observer_low_2, kMinSendBitrateBps, kMaxSendBitrateBps, 0, false,
2.0);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_1.last_bitrate_bps_);
EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_2.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low_1);
allocator_->RemoveObserver(&observer_low_2);
}
// Tests that there is no difference in functionality when the min bitrate is
// enforced.
TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBasicMinEnforced) {
TestBitrateObserver observer_low_1;
TestBitrateObserver observer_low_2;
const uint32_t kMinSendBitrateBps = 0;
const uint32_t kMaxSendBitrateBps = 60;
const uint32_t kNetworkBandwidthBps = 60;
AddObserver(&observer_low_1, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
2.0);
AddObserver(&observer_low_2, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
2.0);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_1.last_bitrate_bps_);
EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_2.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low_1);
allocator_->RemoveObserver(&observer_low_2);
}
// Tests that if the available bandwidth is the sum of the max bitrate
// of all observers, they will be allocated their max.
TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBothAllocatedMax) {
TestBitrateObserver observer_low;
TestBitrateObserver observer_mid;
const uint32_t kMinSendBitrateBps = 0;
const uint32_t kMaxSendBitrateBps = 60;
const uint32_t kNetworkBandwidthBps = kMaxSendBitrateBps * 2;
AddObserver(&observer_low, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
2.0);
AddObserver(&observer_mid, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
4.0);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(kMaxSendBitrateBps, observer_low.last_bitrate_bps_);
EXPECT_EQ(kMaxSendBitrateBps, observer_mid.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
}
// Tests that after a higher bitrate priority observer has been allocated its
// max bitrate the lower priority observer will then be allocated the remaining
// bitrate.
TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversOneAllocatedToMax) {
TestBitrateObserver observer_low;
TestBitrateObserver observer_mid;
AddObserver(&observer_low, 10, 50, 0, false, 2.0);
AddObserver(&observer_mid, 10, 50, 0, false, 4.0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(90, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(40u, observer_low.last_bitrate_bps_);
EXPECT_EQ(50u, observer_mid.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
}
// Tests that three observers with three different bitrate priorities will all
// be allocated bitrate according to their relative bitrate priority.
TEST_F(BitrateAllocatorTest,
PriorityRateThreeObserversAllocatedRelativeAmounts) {
TestBitrateObserver observer_low;
TestBitrateObserver observer_mid;
TestBitrateObserver observer_high;
const uint32_t kMaxBitrate = 100;
// Not enough bandwidth to fill any observer's max bitrate.
const uint32_t kNetworkBandwidthBps = 70;
const double kLowBitratePriority = 2.0;
const double kMidBitratePriority = 4.0;
const double kHighBitratePriority = 8.0;
const double kTotalBitratePriority =
kLowBitratePriority + kMidBitratePriority + kHighBitratePriority;
AddObserver(&observer_low, 0, kMaxBitrate, 0, false, kLowBitratePriority);
AddObserver(&observer_mid, 0, kMaxBitrate, 0, false, kMidBitratePriority);
AddObserver(&observer_high, 0, kMaxBitrate, 0, false, kHighBitratePriority);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
const double kLowFractionAllocated =
kLowBitratePriority / kTotalBitratePriority;
const double kMidFractionAllocated =
kMidBitratePriority / kTotalBitratePriority;
const double kHighFractionAllocated =
kHighBitratePriority / kTotalBitratePriority;
EXPECT_EQ(kLowFractionAllocated * kNetworkBandwidthBps,
observer_low.last_bitrate_bps_);
EXPECT_EQ(kMidFractionAllocated * kNetworkBandwidthBps,
observer_mid.last_bitrate_bps_);
EXPECT_EQ(kHighFractionAllocated * kNetworkBandwidthBps,
observer_high.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
allocator_->RemoveObserver(&observer_high);
}
// Tests that after the high priority observer has been allocated its maximum
// bitrate, the other two observers are still allocated bitrate according to
// their relative bitrate priority.
TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversHighAllocatedToMax) {
TestBitrateObserver observer_low;
const double kLowBitratePriority = 2.0;
TestBitrateObserver observer_mid;
const double kMidBitratePriority = 4.0;
TestBitrateObserver observer_high;
const double kHighBitratePriority = 8.0;
const uint32_t kAvailableBitrate = 90;
const uint32_t kMaxBitrate = 40;
const uint32_t kMinBitrate = 10;
// Remaining bitrate after allocating to all mins and knowing that the high
// priority observer will have its max bitrate allocated.
const uint32_t kRemainingBitrate =
kAvailableBitrate - kMaxBitrate - (2 * kMinBitrate);
AddObserver(&observer_low, kMinBitrate, kMaxBitrate, 0, false,
kLowBitratePriority);
AddObserver(&observer_mid, kMinBitrate, kMaxBitrate, 0, false,
kMidBitratePriority);
AddObserver(&observer_high, kMinBitrate, kMaxBitrate, 0, false,
kHighBitratePriority);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kAvailableBitrate, 0, 0, kDefaultProbingIntervalMs));
const double kLowFractionAllocated =
kLowBitratePriority / (kLowBitratePriority + kMidBitratePriority);
const double kMidFractionAllocated =
kMidBitratePriority / (kLowBitratePriority + kMidBitratePriority);
EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kLowFractionAllocated),
observer_low.last_bitrate_bps_);
EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kMidFractionAllocated),
observer_mid.last_bitrate_bps_);
EXPECT_EQ(40u, observer_high.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
allocator_->RemoveObserver(&observer_high);
}
// Tests that after the low priority observer has been allocated its maximum
// bitrate, the other two observers are still allocated bitrate according to
// their relative bitrate priority.
TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversLowAllocatedToMax) {
TestBitrateObserver observer_low;
const double kLowBitratePriority = 2.0;
const uint32_t kLowMaxBitrate = 10;
TestBitrateObserver observer_mid;
const double kMidBitratePriority = 4.0;
TestBitrateObserver observer_high;
const double kHighBitratePriority = 8.0;
const uint32_t kMinBitrate = 0;
const uint32_t kMaxBitrate = 60;
const uint32_t kAvailableBitrate = 100;
// Remaining bitrate knowing that the low priority observer is allocated its
// max bitrate. We know this because it is allocated 2.0/14.0 (1/7) of the
// available bitrate, so 70 bps would be sufficient network bandwidth.
const uint32_t kRemainingBitrate = kAvailableBitrate - kLowMaxBitrate;
AddObserver(&observer_low, kMinBitrate, kLowMaxBitrate, 0, false,
kLowBitratePriority);
AddObserver(&observer_mid, kMinBitrate, kMaxBitrate, 0, false,
kMidBitratePriority);
AddObserver(&observer_high, kMinBitrate, kMaxBitrate, 0, false,
kHighBitratePriority);
allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
kAvailableBitrate, 0, 0, kDefaultProbingIntervalMs));
const double kMidFractionAllocated =
kMidBitratePriority / (kMidBitratePriority + kHighBitratePriority);
const double kHighFractionAllocated =
kHighBitratePriority / (kMidBitratePriority + kHighBitratePriority);
EXPECT_EQ(kLowMaxBitrate, observer_low.last_bitrate_bps_);
EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kMidFractionAllocated),
observer_mid.last_bitrate_bps_);
EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kHighFractionAllocated),
observer_high.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
allocator_->RemoveObserver(&observer_high);
}
// Tests that after two observers are allocated bitrate to their max, the
// the remaining observer is allocated what's left appropriately. This test
// handles an edge case where the medium and high observer reach their
// "relative" max allocation at the same time. The high has 40 to allocate
// above its min, and the mid has 20 to allocate above its min, which scaled
// by their bitrate priority is the same for each.
TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversTwoAllocatedToMax) {
TestBitrateObserver observer_low;
TestBitrateObserver observer_mid;
TestBitrateObserver observer_high;
AddObserver(&observer_low, 10, 40, 0, false, 2.0);
// Scaled allocation above the min allocation is the same for these two,
// meaning they will get allocated their max at the same time.
// Scaled (target allocation) = (max - min) / bitrate priority
AddObserver(&observer_mid, 10, 30, 0, false, 4.0);
AddObserver(&observer_high, 10, 50, 0, false, 8.0);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(110, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(30u, observer_low.last_bitrate_bps_);
EXPECT_EQ(30u, observer_mid.last_bitrate_bps_);
EXPECT_EQ(50u, observer_high.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_low);
allocator_->RemoveObserver(&observer_mid);
allocator_->RemoveObserver(&observer_high);
}
TEST_F(BitrateAllocatorTest, ElasticRateAllocationCanBorrowUnsedRate) {
test::ExplicitKeyValueConfig field_trials(
"WebRTC-ElasticBitrateAllocation/upper_limit:200bps/");
ReconfigureAllocator(
GetElasticRateAllocationFieldTrialParameter(field_trials));
TestBitrateObserver observer_consume;
TestContributingBitrateObserver observer_contribute;
AddObserver(&observer_consume, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanConsumeExtraRate);
AddObserver(&observer_contribute, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanContributeUnusedRate);
observer_contribute.rate_usage_ = DataRate::BitsPerSec(20);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(100, 0, 0, kDefaultProbingIntervalMs));
// observer_contribute is allocated 50 but only used 20, so 30 is borrowed to
// observer_consume who gets 50+30=80.
EXPECT_EQ(80u, observer_consume.last_bitrate_bps_);
EXPECT_EQ(50u, observer_contribute.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_consume);
allocator_->RemoveObserver(&observer_contribute);
}
TEST_F(BitrateAllocatorTest, ElasticRateAllocationDefaultsInactive) {
test::ExplicitKeyValueConfig field_trials("");
ReconfigureAllocator(
GetElasticRateAllocationFieldTrialParameter(field_trials));
TestBitrateObserver observer_consume;
TestContributingBitrateObserver observer_contribute;
AddObserver(&observer_consume, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanConsumeExtraRate);
AddObserver(&observer_contribute, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanContributeUnusedRate);
observer_contribute.rate_usage_ = DataRate::BitsPerSec(20);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(100, 0, 0, kDefaultProbingIntervalMs));
EXPECT_EQ(50u, observer_consume.last_bitrate_bps_);
EXPECT_EQ(50u, observer_contribute.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_consume);
allocator_->RemoveObserver(&observer_contribute);
}
TEST_F(BitrateAllocatorTest, ElasticRateAllocationDontExceedMaxBitrate) {
test::ExplicitKeyValueConfig field_trials(
"WebRTC-ElasticBitrateAllocation/upper_limit:200bps/");
ReconfigureAllocator(
GetElasticRateAllocationFieldTrialParameter(field_trials));
TestBitrateObserver observer_consume;
TestContributingBitrateObserver observer_contribute;
AddObserver(&observer_consume, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanConsumeExtraRate);
AddObserver(&observer_contribute, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanContributeUnusedRate);
observer_contribute.rate_usage_ = DataRate::BitsPerSec(20);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(140, 0, 0, kDefaultProbingIntervalMs));
// observer_contribute is allocated 70 but only used 20, so 50 is borrowed to
// observer_consume who could get 70+50=120, but is capped by max-bitrate to
// 100.
EXPECT_EQ(100u, observer_consume.last_bitrate_bps_);
EXPECT_EQ(70u, observer_contribute.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_consume);
allocator_->RemoveObserver(&observer_contribute);
}
TEST_F(BitrateAllocatorTest, ElasticRateAllocationStayWithinUpperLimit) {
uint32_t upper_limit = 70;
test::ExplicitKeyValueConfig field_trials(
"WebRTC-ElasticBitrateAllocation/upper_limit:" +
std::to_string(upper_limit) + "bps/");
ReconfigureAllocator(
GetElasticRateAllocationFieldTrialParameter(field_trials));
TestBitrateObserver observer_consume;
TestContributingBitrateObserver observer_contribute;
AddObserver(&observer_consume, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanConsumeExtraRate);
AddObserver(&observer_contribute, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanContributeUnusedRate);
observer_contribute.rate_usage_ = DataRate::BitsPerSec(20);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(100, 0, 0, kDefaultProbingIntervalMs));
// observer_contribute is allocated 50 but only used 20, so 30 is borrowed to
// observer_consume who could get 30+50=80, but is capped by upper_limit.
EXPECT_EQ(upper_limit, observer_consume.last_bitrate_bps_);
EXPECT_EQ(50u, observer_contribute.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_consume);
allocator_->RemoveObserver(&observer_contribute);
}
TEST_F(BitrateAllocatorTest, ElasticRateAllocationDontReduceAllocation) {
uint32_t upper_limit = 70;
test::ExplicitKeyValueConfig field_trials(
"WebRTC-ElasticBitrateAllocation/upper_limit:" +
std::to_string(upper_limit) + "bps/");
ReconfigureAllocator(
GetElasticRateAllocationFieldTrialParameter(field_trials));
TestBitrateObserver observer_consume;
TestContributingBitrateObserver observer_contribute;
AddObserver(&observer_consume, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanConsumeExtraRate);
AddObserver(&observer_contribute, 10, 100, 0, false, 1.0,
TrackRateElasticity::kCanContributeUnusedRate);
observer_contribute.rate_usage_ = DataRate::BitsPerSec(20);
allocator_->OnNetworkEstimateChanged(
CreateTargetRateMessage(200, 0, 0, kDefaultProbingIntervalMs));
// observer_contribute is allocated 100 but only used 20, so 80 can be
// borrowed to observer_consume. But observer_consume already has 100
// (above upper_limit), so no bitrate is borrowed.
EXPECT_EQ(100u, observer_consume.last_bitrate_bps_);
EXPECT_EQ(100u, observer_contribute.last_bitrate_bps_);
allocator_->RemoveObserver(&observer_consume);
allocator_->RemoveObserver(&observer_contribute);
}
} // namespace webrtc
|