blob: 9f1998b617bb753a6aba57c99b26eb33c51f9786 [file] [log] [blame]
isheriff31687812016-10-04 08:43:09 -07001/*
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
isheriff31687812016-10-04 08:43:09 -070011#include "webrtc/modules/pacing/alr_detector.h"
12
Sergey Ulanov0182f852016-11-16 15:42:11 -080013#include "webrtc/test/gtest.h"
14
isheriff31687812016-10-04 08:43:09 -070015namespace {
16
isheriff31687812016-10-04 08:43:09 -070017constexpr int kEstimatedBitrateBps = 300000;
isheriff31687812016-10-04 08:43:09 -070018
19} // namespace
20
21namespace webrtc {
22
tschumim82c55932017-07-11 06:56:04 -070023namespace {
24class 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 Ulanov0182f852016-11-16 15:42:11 -080067class AlrDetectorTest : public testing::Test {
68 public:
69 void SetUp() override {
70 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps);
71 }
isheriff31687812016-10-04 08:43:09 -070072
Sergey Ulanov0182f852016-11-16 15:42:11 -080073 protected:
74 AlrDetector alr_detector_;
Sergey Ulanov0182f852016-11-16 15:42:11 -080075};
76
77TEST_F(AlrDetectorTest, AlrDetection) {
78 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -080079 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080080
81 // Stay in non-ALR state when usage is close to 100%.
tschumim82c55932017-07-11 06:56:04 -070082 SimulateOutgoingTrafficIn(&alr_detector_)
83 .ForTimeMs(1000)
84 .AtPercentOfEstimatedBitrate(90);
sergeyu80ed35e2016-11-28 13:11:13 -080085 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080086
87 // Verify that we ALR starts when bitrate drops below 20%.
tschumim82c55932017-07-11 06:56:04 -070088 SimulateOutgoingTrafficIn(&alr_detector_)
tschumim9d117642017-07-17 01:41:41 -070089 .ForTimeMs(1500)
tschumim82c55932017-07-11 06:56:04 -070090 .AtPercentOfEstimatedBitrate(20);
sergeyu80ed35e2016-11-28 13:11:13 -080091 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080092
tschumim82c55932017-07-11 06:56:04 -070093 // Verify that ALR ends when usage is above 65%.
94 SimulateOutgoingTrafficIn(&alr_detector_)
95 .ForTimeMs(1000)
96 .AtPercentOfEstimatedBitrate(100);
sergeyu80ed35e2016-11-28 13:11:13 -080097 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -070098}
99
Sergey Ulanov0182f852016-11-16 15:42:11 -0800100TEST_F(AlrDetectorTest, ShortSpike) {
101 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -0800102 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -0700103
Sergey Ulanov0182f852016-11-16 15:42:11 -0800104 // Verify that we ALR starts when bitrate drops below 20%.
tschumim82c55932017-07-11 06:56:04 -0700105 SimulateOutgoingTrafficIn(&alr_detector_)
106 .ForTimeMs(1000)
107 .AtPercentOfEstimatedBitrate(20);
sergeyu80ed35e2016-11-28 13:11:13 -0800108 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800109
110 // Verify that we stay in ALR region even after a short bitrate spike.
tschumim82c55932017-07-11 06:56:04 -0700111 SimulateOutgoingTrafficIn(&alr_detector_)
tschumim9d117642017-07-17 01:41:41 -0700112 .ForTimeMs(100)
tschumim82c55932017-07-11 06:56:04 -0700113 .AtPercentOfEstimatedBitrate(150);
sergeyu80ed35e2016-11-28 13:11:13 -0800114 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800115
tschumim82c55932017-07-11 06:56:04 -0700116 // ALR ends when usage is above 65%.
117 SimulateOutgoingTrafficIn(&alr_detector_)
118 .ForTimeMs(1000)
119 .AtPercentOfEstimatedBitrate(100);
sergeyu80ed35e2016-11-28 13:11:13 -0800120 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800121}
122
123TEST_F(AlrDetectorTest, BandwidthEstimateChanges) {
124 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -0800125 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800126
127 // ALR starts when bitrate drops below 20%.
tschumim82c55932017-07-11 06:56:04 -0700128 SimulateOutgoingTrafficIn(&alr_detector_)
129 .ForTimeMs(1000)
130 .AtPercentOfEstimatedBitrate(20);
sergeyu80ed35e2016-11-28 13:11:13 -0800131 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800132
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);
sergeyu80ed35e2016-11-28 13:11:13 -0800138 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
tschumim82c55932017-07-11 06:56:04 -0700139 SimulateOutgoingTrafficIn(&alr_detector_)
140 .ForTimeMs(1000)
141 .AtPercentOfEstimatedBitrate(50);
sergeyu80ed35e2016-11-28 13:11:13 -0800142 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -0700143}
144
145} // namespace webrtc