Per Kjellander | 898f091 | 2021-04-21 09:56:32 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 "modules/congestion_controller/remb_throttler.h" |
| 11 | |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "api/units/data_rate.h" |
| 15 | #include "api/units/time_delta.h" |
| 16 | #include "system_wrappers/include/clock.h" |
| 17 | #include "test/gmock.h" |
| 18 | #include "test/gtest.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | using ::testing::_; |
| 23 | using ::testing::MockFunction; |
| 24 | |
| 25 | TEST(RembThrottlerTest, CallRembSenderOnFirstReceiveBitrateChange) { |
| 26 | SimulatedClock clock(Timestamp::Zero()); |
| 27 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 28 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 29 | |
| 30 | EXPECT_CALL(remb_sender, Call(12345, std::vector<uint32_t>({1, 2, 3}))); |
| 31 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/12345); |
| 32 | } |
| 33 | |
| 34 | TEST(RembThrottlerTest, ThrottlesSmallReceiveBitrateDecrease) { |
| 35 | SimulatedClock clock(Timestamp::Zero()); |
| 36 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 37 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 38 | |
| 39 | EXPECT_CALL(remb_sender, Call); |
| 40 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/12346); |
| 41 | clock.AdvanceTime(TimeDelta::Millis(100)); |
| 42 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/12345); |
| 43 | |
| 44 | EXPECT_CALL(remb_sender, Call(12345, _)); |
| 45 | clock.AdvanceTime(TimeDelta::Millis(101)); |
| 46 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/12345); |
| 47 | } |
| 48 | |
| 49 | TEST(RembThrottlerTest, DoNotThrottleLargeReceiveBitrateDecrease) { |
| 50 | SimulatedClock clock(Timestamp::Zero()); |
| 51 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 52 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 53 | |
| 54 | EXPECT_CALL(remb_sender, Call(2345, _)); |
| 55 | EXPECT_CALL(remb_sender, Call(1234, _)); |
| 56 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/2345); |
| 57 | clock.AdvanceTime(TimeDelta::Millis(1)); |
| 58 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/1234); |
| 59 | } |
| 60 | |
| 61 | TEST(RembThrottlerTest, ThrottlesReceiveBitrateIncrease) { |
| 62 | SimulatedClock clock(Timestamp::Zero()); |
| 63 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 64 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 65 | |
| 66 | EXPECT_CALL(remb_sender, Call); |
| 67 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/1234); |
| 68 | clock.AdvanceTime(TimeDelta::Millis(100)); |
| 69 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/2345); |
| 70 | |
| 71 | // Updates 200ms after previous callback is not throttled. |
| 72 | EXPECT_CALL(remb_sender, Call(2345, _)); |
| 73 | clock.AdvanceTime(TimeDelta::Millis(101)); |
| 74 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/2345); |
| 75 | } |
| 76 | |
| 77 | TEST(RembThrottlerTest, CallRembSenderOnSetMaxDesiredReceiveBitrate) { |
| 78 | SimulatedClock clock(Timestamp::Zero()); |
| 79 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 80 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 81 | EXPECT_CALL(remb_sender, Call(1234, _)); |
| 82 | remb_throttler.SetMaxDesiredReceiveBitrate(DataRate::BitsPerSec(1234)); |
| 83 | } |
| 84 | |
| 85 | TEST(RembThrottlerTest, CallRembSenderWithMinOfMaxDesiredAndOnReceivedBitrate) { |
| 86 | SimulatedClock clock(Timestamp::Zero()); |
| 87 | MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender; |
| 88 | RembThrottler remb_throttler(remb_sender.AsStdFunction(), &clock); |
| 89 | |
| 90 | EXPECT_CALL(remb_sender, Call(1234, _)); |
| 91 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/1234); |
| 92 | clock.AdvanceTime(TimeDelta::Millis(1)); |
| 93 | remb_throttler.SetMaxDesiredReceiveBitrate(DataRate::BitsPerSec(4567)); |
| 94 | |
| 95 | clock.AdvanceTime(TimeDelta::Millis(200)); |
| 96 | EXPECT_CALL(remb_sender, Call(4567, _)); |
| 97 | remb_throttler.OnReceiveBitrateChanged({1, 2, 3}, /*bitrate_bps=*/5678); |
| 98 | } |
| 99 | |
| 100 | } // namespace webrtc |