Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/pacing/alr_detector.h" | 11 #include "webrtc/modules/pacing/alr_detector.h" |
| 12 | 12 |
| 13 #include "webrtc/test/gtest.h" | 13 #include "webrtc/test/gtest.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 constexpr int kEstimatedBitrateBps = 300000; | 17 constexpr int kEstimatedBitrateBps = 300000; |
| 18 | 18 |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 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) { | |
|
philipel
2017/07/10 11:41:05
Return type should be SimulateOutgoingTrafficIn& r
tschumi
2017/07/11 09:33:31
Done.
| |
| 32 interval_ms_ = rtc::Optional<int>(time_ms); | |
|
philipel
2017/07/10 11:41:05
nit: emplace
tschumi
2017/07/11 09:33:31
Done.
| |
| 33 ProduceTraffic(); | |
| 34 return *this; | |
| 35 } | |
| 36 | |
| 37 SimulateOutgoingTrafficIn AtPercentOfEstimatedBitrate(int usage_percentage) { | |
|
philipel
2017/07/10 11:41:05
SimulateOutgoingTrafficIn&
tschumi
2017/07/11 09:33:31
Done.
| |
| 38 usage_percentage_ = rtc::Optional<int>(usage_percentage); | |
|
philipel
2017/07/10 11:41:05
nit: emplace
tschumi
2017/07/11 09:33:31
Done.
| |
| 39 ProduceTraffic(); | |
| 40 return *this; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 void ProduceTraffic() { | |
| 45 if (!interval_ms_ || !usage_percentage_) | |
| 46 return; | |
| 47 const int kTimeStepMs = 10; | |
| 48 for (int t = 0; t < *interval_ms_; t += kTimeStepMs) { | |
| 49 alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ * | |
| 50 kTimeStepMs / (8 * 100 * 1000), | |
| 51 kTimeStepMs); | |
| 52 } | |
| 53 int remainder_ms = *interval_ms_ % kTimeStepMs; | |
| 54 if (remainder_ms > 0) { | |
| 55 alr_detector_->OnBytesSent(kEstimatedBitrateBps * *usage_percentage_ * | |
| 56 remainder_ms / (8 * 100 * 1000), | |
| 57 kTimeStepMs); | |
| 58 } | |
| 59 } | |
| 60 AlrDetector* const alr_detector_; | |
| 61 rtc::Optional<int> interval_ms_; | |
| 62 rtc::Optional<int> usage_percentage_; | |
| 63 }; | |
| 64 } // namespace | |
| 65 | |
| 23 class AlrDetectorTest : public testing::Test { | 66 class AlrDetectorTest : public testing::Test { |
| 24 public: | 67 public: |
| 25 void SetUp() override { | 68 void SetUp() override { |
| 26 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps); | 69 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps); |
| 27 } | 70 } |
| 28 | 71 |
| 29 void SimulateOutgoingTraffic(int interval_ms, int usage_percentage) { | |
| 30 const int kTimeStepMs = 10; | |
| 31 for (int t = 0; t < interval_ms; t += kTimeStepMs) { | |
| 32 now_ms += kTimeStepMs; | |
| 33 alr_detector_.OnBytesSent(kEstimatedBitrateBps * usage_percentage * | |
| 34 kTimeStepMs / (8 * 100 * 1000), | |
| 35 now_ms); | |
| 36 } | |
| 37 | |
| 38 int remainder_ms = interval_ms % kTimeStepMs; | |
| 39 now_ms += remainder_ms; | |
| 40 if (remainder_ms > 0) { | |
| 41 alr_detector_.OnBytesSent(kEstimatedBitrateBps * usage_percentage * | |
| 42 remainder_ms / (8 * 100 * 1000), | |
| 43 remainder_ms); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 protected: | 72 protected: |
| 48 AlrDetector alr_detector_; | 73 AlrDetector alr_detector_; |
| 49 int64_t now_ms = 1; | |
| 50 }; | 74 }; |
| 51 | 75 |
| 52 TEST_F(AlrDetectorTest, AlrDetection) { | 76 TEST_F(AlrDetectorTest, AlrDetection) { |
| 53 // Start in non-ALR state. | 77 // Start in non-ALR state. |
| 54 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 78 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 55 | 79 |
| 56 // Stay in non-ALR state when usage is close to 100%. | 80 // Stay in non-ALR state when usage is close to 100%. |
| 57 SimulateOutgoingTraffic(500, 90); | 81 SimulateOutgoingTrafficIn(&alr_detector_) |
| 82 .ForTimeMs(1000) | |
| 83 .AtPercentOfEstimatedBitrate(90); | |
| 58 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 84 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 59 | 85 |
| 60 // Verify that we ALR starts when bitrate drops below 20%. | 86 // Verify that we ALR starts when bitrate drops below 20%. |
| 61 SimulateOutgoingTraffic(500, 20); | 87 SimulateOutgoingTrafficIn(&alr_detector_) |
| 88 .ForTimeMs(1000) | |
| 89 .AtPercentOfEstimatedBitrate(20); | |
| 62 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 90 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 63 | 91 |
| 64 // Verify that we remain in ALR state while usage is still below 70%. | 92 // Verify that ALR ends when usage is above 65%. |
| 65 SimulateOutgoingTraffic(500, 69); | 93 SimulateOutgoingTrafficIn(&alr_detector_) |
| 66 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 94 .ForTimeMs(1000) |
| 67 | 95 .AtPercentOfEstimatedBitrate(100); |
| 68 // Verify that ALR ends when usage is above 70%. | |
| 69 SimulateOutgoingTraffic(500, 75); | |
| 70 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 96 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 71 } | 97 } |
| 72 | 98 |
| 73 TEST_F(AlrDetectorTest, ShortSpike) { | 99 TEST_F(AlrDetectorTest, ShortSpike) { |
| 74 // Start in non-ALR state. | 100 // Start in non-ALR state. |
| 75 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 101 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 76 | 102 |
| 77 // Verify that we ALR starts when bitrate drops below 20%. | 103 // Verify that we ALR starts when bitrate drops below 20%. |
| 78 SimulateOutgoingTraffic(500, 20); | 104 SimulateOutgoingTrafficIn(&alr_detector_) |
| 105 .ForTimeMs(1000) | |
| 106 .AtPercentOfEstimatedBitrate(20); | |
| 79 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 107 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 80 | 108 |
| 81 // Verify that we stay in ALR region even after a short bitrate spike. | 109 // Verify that we stay in ALR region even after a short bitrate spike. |
| 82 SimulateOutgoingTraffic(100, 150); | 110 SimulateOutgoingTrafficIn(&alr_detector_) |
| 111 .ForTimeMs(200) | |
| 112 .AtPercentOfEstimatedBitrate(150); | |
| 83 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 113 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 84 | 114 |
| 85 SimulateOutgoingTraffic(200, 20); | 115 // ALR ends when usage is above 65%. |
| 86 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 116 SimulateOutgoingTrafficIn(&alr_detector_) |
| 87 | 117 .ForTimeMs(1000) |
| 88 // ALR ends when usage is above 70%. | 118 .AtPercentOfEstimatedBitrate(100); |
| 89 SimulateOutgoingTraffic(500, 75); | |
| 90 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 119 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 91 } | 120 } |
| 92 | 121 |
| 93 TEST_F(AlrDetectorTest, BandwidthEstimateChanges) { | 122 TEST_F(AlrDetectorTest, BandwidthEstimateChanges) { |
| 94 // Start in non-ALR state. | 123 // Start in non-ALR state. |
| 95 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 124 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 96 | 125 |
| 97 // ALR starts when bitrate drops below 20%. | 126 // ALR starts when bitrate drops below 20%. |
| 98 SimulateOutgoingTraffic(500, 20); | 127 SimulateOutgoingTrafficIn(&alr_detector_) |
| 128 .ForTimeMs(1000) | |
| 129 .AtPercentOfEstimatedBitrate(20); | |
| 99 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 130 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 100 | 131 |
| 101 // When bandwidth estimate drops the detector should stay in ALR mode and quit | 132 // When bandwidth estimate drops the detector should stay in ALR mode and quit |
| 102 // it shortly afterwards as the sender continues sending the same amount of | 133 // it shortly afterwards as the sender continues sending the same amount of |
| 103 // traffic. This is necessary to ensure that ProbeController can still react | 134 // traffic. This is necessary to ensure that ProbeController can still react |
| 104 // to the BWE drop by initiating a new probe. | 135 // to the BWE drop by initiating a new probe. |
| 105 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps / 5); | 136 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps / 5); |
| 106 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 137 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 107 SimulateOutgoingTraffic(10, 20); | 138 SimulateOutgoingTrafficIn(&alr_detector_) |
| 139 .ForTimeMs(1000) | |
| 140 .AtPercentOfEstimatedBitrate(50); | |
| 108 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); | 141 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
| 109 } | 142 } |
| 110 | 143 |
| 111 } // namespace webrtc | 144 } // namespace webrtc |
| OLD | NEW |