isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/pacing/alr_detector.h" |
| 12 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 13 | #include "webrtc/test/gtest.h" |
| 14 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 17 | constexpr int kEstimatedBitrateBps = 300000; |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | class SimulateOutgoingTrafficIn { |
| 25 | public: |
| 26 | explicit SimulateOutgoingTrafficIn(AlrDetector* alr_detector) |
| 27 | : alr_detector_(alr_detector) { |
| 28 | RTC_CHECK(alr_detector_); |
| 29 | } |
| 30 | |
| 31 | SimulateOutgoingTrafficIn& ForTimeMs(int time_ms) { |
| 32 | interval_ms_ = rtc::Optional<int>(time_ms); |
| 33 | interval_ms_.emplace(time_ms); |
| 34 | ProduceTraffic(); |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | SimulateOutgoingTrafficIn& AtPercentOfEstimatedBitrate(int usage_percentage) { |
| 39 | usage_percentage_.emplace(usage_percentage); |
| 40 | ProduceTraffic(); |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | void ProduceTraffic() { |
| 46 | if (!interval_ms_ || !usage_percentage_) |
| 47 | return; |
| 48 | const int kTimeStepMs = 10; |
| 49 | for (int t = 0; t < *interval_ms_; t += kTimeStepMs) { |
| 50 | alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ * |
| 51 | kTimeStepMs / (8 * 100 * 1000), |
| 52 | kTimeStepMs); |
| 53 | } |
| 54 | int remainder_ms = *interval_ms_ % kTimeStepMs; |
| 55 | if (remainder_ms > 0) { |
| 56 | alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ * |
| 57 | remainder_ms / (8 * 100 * 1000), |
| 58 | kTimeStepMs); |
| 59 | } |
| 60 | } |
| 61 | AlrDetector* const alr_detector_; |
| 62 | rtc::Optional<int> interval_ms_; |
| 63 | rtc::Optional<int> usage_percentage_; |
| 64 | }; |
| 65 | } // namespace |
| 66 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 67 | class AlrDetectorTest : public testing::Test { |
| 68 | public: |
| 69 | void SetUp() override { |
| 70 | alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps); |
| 71 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 72 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 73 | protected: |
| 74 | AlrDetector alr_detector_; |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | TEST_F(AlrDetectorTest, AlrDetection) { |
| 78 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 79 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 80 | |
| 81 | // Stay in non-ALR state when usage is close to 100%. |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 82 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 83 | .ForTimeMs(1000) |
| 84 | .AtPercentOfEstimatedBitrate(90); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 85 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 86 | |
| 87 | // Verify that we ALR starts when bitrate drops below 20%. |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 88 | SimulateOutgoingTrafficIn(&alr_detector_) |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 89 | .ForTimeMs(1500) |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 90 | .AtPercentOfEstimatedBitrate(20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 91 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 92 | |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 93 | // Verify that ALR ends when usage is above 65%. |
| 94 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 95 | .ForTimeMs(1000) |
| 96 | .AtPercentOfEstimatedBitrate(100); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 97 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 100 | TEST_F(AlrDetectorTest, ShortSpike) { |
| 101 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 102 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 103 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 104 | // Verify that we ALR starts when bitrate drops below 20%. |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 105 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 106 | .ForTimeMs(1000) |
| 107 | .AtPercentOfEstimatedBitrate(20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 108 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 109 | |
| 110 | // Verify that we stay in ALR region even after a short bitrate spike. |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 111 | SimulateOutgoingTrafficIn(&alr_detector_) |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 112 | .ForTimeMs(100) |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 113 | .AtPercentOfEstimatedBitrate(150); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 114 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 115 | |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 116 | // ALR ends when usage is above 65%. |
| 117 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 118 | .ForTimeMs(1000) |
| 119 | .AtPercentOfEstimatedBitrate(100); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 120 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(AlrDetectorTest, BandwidthEstimateChanges) { |
| 124 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 125 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 126 | |
| 127 | // ALR starts when bitrate drops below 20%. |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 128 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 129 | .ForTimeMs(1000) |
| 130 | .AtPercentOfEstimatedBitrate(20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 132 | |
| 133 | // When bandwidth estimate drops the detector should stay in ALR mode and quit |
| 134 | // it shortly afterwards as the sender continues sending the same amount of |
| 135 | // traffic. This is necessary to ensure that ProbeController can still react |
| 136 | // to the BWE drop by initiating a new probe. |
| 137 | alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps / 5); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 138 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 139 | SimulateOutgoingTrafficIn(&alr_detector_) |
| 140 | .ForTimeMs(1000) |
| 141 | .AtPercentOfEstimatedBitrate(50); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 142 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace webrtc |