mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "api/video/i420_buffer.h" |
| 14 | #include "common_video/include/video_frame.h" |
| 15 | #include "modules/video_coding/utility/quality_scaler.h" |
| 16 | #include "rtc_base/event.h" |
| 17 | #include "rtc_base/fakeclock.h" |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 18 | #include "rtc_base/random.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gmock.h" |
| 20 | #include "test/gtest.h" |
| 21 | #include "video/overuse_frame_detector.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 22 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 23 | namespace webrtc { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 24 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 25 | using ::testing::InvokeWithoutArgs; |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 26 | using ::testing::_; |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 27 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 28 | namespace { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | const int kWidth = 640; |
| 30 | const int kHeight = 480; |
| 31 | // Corresponds to load of 15% |
| 32 | const int kFrameIntervalUs = 33 * rtc::kNumMicrosecsPerMillisec; |
| 33 | const int kProcessTimeUs = 5 * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 34 | } // namespace |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 35 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 36 | class MockCpuOveruseObserver : public AdaptationObserverInterface { |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 37 | public: |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 38 | MockCpuOveruseObserver() {} |
| 39 | virtual ~MockCpuOveruseObserver() {} |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 40 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 41 | MOCK_METHOD1(AdaptUp, void(AdaptReason)); |
| 42 | MOCK_METHOD1(AdaptDown, void(AdaptReason)); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 45 | class CpuOveruseObserverImpl : public AdaptationObserverInterface { |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 46 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | CpuOveruseObserverImpl() : overuse_(0), normaluse_(0) {} |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 48 | virtual ~CpuOveruseObserverImpl() {} |
| 49 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 50 | void AdaptDown(AdaptReason) { ++overuse_; } |
| 51 | void AdaptUp(AdaptReason) { ++normaluse_; } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 52 | |
| 53 | int overuse_; |
| 54 | int normaluse_; |
| 55 | }; |
| 56 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 57 | class OveruseFrameDetectorUnderTest : public OveruseFrameDetector { |
| 58 | public: |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 59 | explicit OveruseFrameDetectorUnderTest( |
| 60 | CpuOveruseMetricsObserver* metrics_observer) |
| 61 | : OveruseFrameDetector(metrics_observer) {} |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 62 | ~OveruseFrameDetectorUnderTest() {} |
| 63 | |
| 64 | using OveruseFrameDetector::CheckForOveruse; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 65 | using OveruseFrameDetector::SetOptions; |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 68 | class OveruseFrameDetectorTest : public ::testing::Test, |
| 69 | public CpuOveruseMetricsObserver { |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 70 | protected: |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 71 | void SetUp() override { |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 72 | observer_ = &mock_observer_; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 73 | options_.min_process_count = 0; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 74 | overuse_detector_ = absl::make_unique<OveruseFrameDetectorUnderTest>(this); |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 75 | // Unfortunately, we can't call SetOptions here, since that would break |
| 76 | // single-threading requirements in the RunOnTqNormalUsage test. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 77 | } |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 78 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 79 | void OnEncodedFrameTimeMeasured(int encode_time_ms, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 80 | int encode_usage_percent) override { |
| 81 | encode_usage_percent_ = encode_usage_percent; |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 84 | int InitialUsage() { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 85 | return ((options_.low_encode_usage_threshold_percent + |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 86 | options_.high_encode_usage_threshold_percent) / |
| 87 | 2.0f) + |
| 88 | 0.5; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 91 | virtual void InsertAndSendFramesWithInterval(int num_frames, |
| 92 | int interval_us, |
| 93 | int width, |
| 94 | int height, |
| 95 | int delay_us) { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 96 | VideoFrame frame(I420Buffer::Create(width, height), |
| 97 | webrtc::kVideoRotation_0, 0); |
| 98 | uint32_t timestamp = 0; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 99 | while (num_frames-- > 0) { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 100 | frame.set_timestamp(timestamp); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 101 | int64_t capture_time_us = rtc::TimeMicros(); |
| 102 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 103 | clock_.AdvanceTimeMicros(delay_us); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 104 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), |
| 105 | capture_time_us, delay_us); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 106 | clock_.AdvanceTimeMicros(interval_us - delay_us); |
| 107 | timestamp += interval_us * 90 / 1000; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 111 | virtual void InsertAndSendFramesWithRandomInterval(int num_frames, |
| 112 | int min_interval_us, |
| 113 | int max_interval_us, |
| 114 | int width, |
| 115 | int height, |
| 116 | int delay_us) { |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 117 | webrtc::Random random(17); |
| 118 | |
| 119 | VideoFrame frame(I420Buffer::Create(width, height), |
| 120 | webrtc::kVideoRotation_0, 0); |
| 121 | uint32_t timestamp = 0; |
| 122 | while (num_frames-- > 0) { |
| 123 | frame.set_timestamp(timestamp); |
| 124 | int interval_us = random.Rand(min_interval_us, max_interval_us); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 125 | int64_t capture_time_us = rtc::TimeMicros(); |
| 126 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 127 | clock_.AdvanceTimeMicros(delay_us); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 128 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), |
| 129 | capture_time_us, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 130 | absl::optional<int>(delay_us)); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 131 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 132 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 133 | // Avoid turning clock backwards. |
| 134 | if (interval_us > delay_us) |
| 135 | clock_.AdvanceTimeMicros(interval_us - delay_us); |
| 136 | |
| 137 | timestamp += interval_us * 90 / 1000; |
| 138 | } |
| 139 | } |
| 140 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 141 | virtual void ForceUpdate(int width, int height) { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 142 | // Insert one frame, wait a second and then put in another to force update |
| 143 | // the usage. From the tests where these are used, adding another sample |
| 144 | // doesn't affect the expected outcome (this is mainly to check initial |
| 145 | // values and whether the overuse detector has been reset or not). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | InsertAndSendFramesWithInterval(2, rtc::kNumMicrosecsPerSec, width, height, |
| 147 | kFrameIntervalUs); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 148 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 149 | void TriggerOveruse(int num_times) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 150 | const int kDelayUs = 32 * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 151 | for (int i = 0; i < num_times; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 152 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 153 | kDelayUs); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 154 | overuse_detector_->CheckForOveruse(observer_); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 158 | void TriggerUnderuse() { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 159 | const int kDelayUs1 = 5000; |
| 160 | const int kDelayUs2 = 6000; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight, |
| 162 | kDelayUs1); |
| 163 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 164 | kDelayUs2); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 165 | overuse_detector_->CheckForOveruse(observer_); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 168 | int UsagePercent() { return encode_usage_percent_; } |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 169 | |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 170 | int64_t OveruseProcessingTimeLimitForFramerate(int fps) const { |
| 171 | int64_t frame_interval = rtc::kNumMicrosecsPerSec / fps; |
| 172 | int64_t max_processing_time_us = |
| 173 | (frame_interval * options_.high_encode_usage_threshold_percent) / 100; |
| 174 | return max_processing_time_us; |
| 175 | } |
| 176 | |
| 177 | int64_t UnderuseProcessingTimeLimitForFramerate(int fps) const { |
| 178 | int64_t frame_interval = rtc::kNumMicrosecsPerSec / fps; |
| 179 | int64_t max_processing_time_us = |
| 180 | (frame_interval * options_.low_encode_usage_threshold_percent) / 100; |
| 181 | return max_processing_time_us; |
| 182 | } |
| 183 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 184 | CpuOveruseOptions options_; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 185 | rtc::ScopedFakeClock clock_; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 186 | MockCpuOveruseObserver mock_observer_; |
| 187 | AdaptationObserverInterface* observer_; |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 188 | std::unique_ptr<OveruseFrameDetectorUnderTest> overuse_detector_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 189 | int encode_usage_percent_ = -1; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 190 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 191 | static const auto reason_ = AdaptationObserverInterface::AdaptReason::kCpu; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 194 | // UsagePercent() > high_encode_usage_threshold_percent => overuse. |
| 195 | // UsagePercent() < low_encode_usage_threshold_percent => underuse. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 196 | TEST_F(OveruseFrameDetectorTest, TriggerOveruse) { |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 197 | // usage > high => overuse |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 198 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 199 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 200 | TriggerOveruse(options_.high_threshold_consecutive_count); |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST_F(OveruseFrameDetectorTest, OveruseAndRecover) { |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 204 | // usage > high => overuse |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 205 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 206 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 207 | TriggerOveruse(options_.high_threshold_consecutive_count); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 208 | // usage < low => underuse |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 209 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 210 | TriggerUnderuse(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 211 | } |
| 212 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 213 | TEST_F(OveruseFrameDetectorTest, DoubleOveruseAndRecover) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 214 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 215 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(2); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 216 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 217 | TriggerOveruse(options_.high_threshold_consecutive_count); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 218 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 219 | TriggerUnderuse(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 220 | } |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 221 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 222 | TEST_F(OveruseFrameDetectorTest, TriggerUnderuseWithMinProcessCount) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 223 | const int kProcessIntervalUs = 5 * rtc::kNumMicrosecsPerSec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 224 | options_.min_process_count = 1; |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 225 | CpuOveruseObserverImpl overuse_observer; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 226 | observer_ = nullptr; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 227 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 228 | InsertAndSendFramesWithInterval(1200, kFrameIntervalUs, kWidth, kHeight, |
| 229 | kProcessTimeUs); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 230 | overuse_detector_->CheckForOveruse(&overuse_observer); |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 231 | EXPECT_EQ(0, overuse_observer.normaluse_); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 232 | clock_.AdvanceTimeMicros(kProcessIntervalUs); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 233 | overuse_detector_->CheckForOveruse(&overuse_observer); |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 234 | EXPECT_EQ(1, overuse_observer.normaluse_); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 235 | } |
| 236 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 237 | TEST_F(OveruseFrameDetectorTest, ConstantOveruseGivesNoNormalUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 238 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 239 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(0); |
| 240 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(64); |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 241 | for (size_t i = 0; i < 64; ++i) { |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 242 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 243 | } |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 244 | } |
| 245 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 246 | TEST_F(OveruseFrameDetectorTest, ConsecutiveCountTriggersOveruse) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 247 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 248 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 249 | options_.high_threshold_consecutive_count = 2; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 250 | overuse_detector_->SetOptions(options_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 251 | TriggerOveruse(2); |
| 252 | } |
| 253 | |
| 254 | TEST_F(OveruseFrameDetectorTest, IncorrectConsecutiveCountTriggersNoOveruse) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 255 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 256 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(0); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 257 | options_.high_threshold_consecutive_count = 2; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 258 | overuse_detector_->SetOptions(options_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 259 | TriggerOveruse(1); |
| 260 | } |
| 261 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 262 | TEST_F(OveruseFrameDetectorTest, ProcessingUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 263 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 264 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 265 | kProcessTimeUs); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 266 | EXPECT_EQ(kProcessTimeUs * 100 / kFrameIntervalUs, UsagePercent()); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 269 | TEST_F(OveruseFrameDetectorTest, ResetAfterResolutionChange) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 270 | overuse_detector_->SetOptions(options_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 271 | ForceUpdate(kWidth, kHeight); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 272 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 273 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 274 | kProcessTimeUs); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 275 | EXPECT_NE(InitialUsage(), UsagePercent()); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 276 | // Verify reset (with new width/height). |
| 277 | ForceUpdate(kWidth, kHeight + 1); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 278 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 281 | TEST_F(OveruseFrameDetectorTest, ResetAfterFrameTimeout) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 282 | overuse_detector_->SetOptions(options_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 283 | ForceUpdate(kWidth, kHeight); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 284 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 285 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 286 | kProcessTimeUs); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 287 | EXPECT_NE(InitialUsage(), UsagePercent()); |
| 288 | InsertAndSendFramesWithInterval( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 289 | 2, options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec, |
| 290 | kWidth, kHeight, kProcessTimeUs); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 291 | EXPECT_NE(InitialUsage(), UsagePercent()); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 292 | // Verify reset. |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 293 | InsertAndSendFramesWithInterval( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 294 | 2, |
| 295 | (options_.frame_timeout_interval_ms + 1) * rtc::kNumMicrosecsPerMillisec, |
| 296 | kWidth, kHeight, kProcessTimeUs); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 297 | ForceUpdate(kWidth, kHeight); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 298 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 301 | TEST_F(OveruseFrameDetectorTest, MinFrameSamplesBeforeUpdating) { |
| 302 | options_.min_frame_samples = 40; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 303 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 304 | InsertAndSendFramesWithInterval(40, kFrameIntervalUs, kWidth, kHeight, |
| 305 | kProcessTimeUs); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 306 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 307 | // Pass time far enough to digest all previous samples. |
| 308 | clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec); |
| 309 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 310 | kProcessTimeUs); |
| 311 | // The last sample has not been processed here. |
| 312 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 313 | |
| 314 | // Pass time far enough to digest all previous samples, 41 in total. |
| 315 | clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 316 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 317 | kProcessTimeUs); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 318 | EXPECT_NE(InitialUsage(), UsagePercent()); |
| 319 | } |
| 320 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 321 | TEST_F(OveruseFrameDetectorTest, InitialProcessingUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 322 | overuse_detector_->SetOptions(options_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 323 | ForceUpdate(kWidth, kHeight); |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 324 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 327 | TEST_F(OveruseFrameDetectorTest, MeasuresMultipleConcurrentSamples) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 328 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 329 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 330 | static const int kIntervalUs = 33 * rtc::kNumMicrosecsPerMillisec; |
| 331 | static const size_t kNumFramesEncodingDelay = 3; |
| 332 | VideoFrame frame(I420Buffer::Create(kWidth, kHeight), |
| 333 | webrtc::kVideoRotation_0, 0); |
| 334 | for (size_t i = 0; i < 1000; ++i) { |
| 335 | // Unique timestamps. |
| 336 | frame.set_timestamp(static_cast<uint32_t>(i)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 337 | int64_t capture_time_us = rtc::TimeMicros(); |
| 338 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 339 | clock_.AdvanceTimeMicros(kIntervalUs); |
| 340 | if (i > kNumFramesEncodingDelay) { |
| 341 | overuse_detector_->FrameSent( |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 342 | static_cast<uint32_t>(i - kNumFramesEncodingDelay), rtc::TimeMicros(), |
| 343 | capture_time_us, kIntervalUs); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 344 | } |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 345 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | TEST_F(OveruseFrameDetectorTest, UpdatesExistingSamples) { |
| 350 | // >85% encoding time should trigger overuse. |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 351 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 352 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 353 | static const int kIntervalUs = 33 * rtc::kNumMicrosecsPerMillisec; |
| 354 | static const int kDelayUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 355 | VideoFrame frame(I420Buffer::Create(kWidth, kHeight), |
| 356 | webrtc::kVideoRotation_0, 0); |
| 357 | uint32_t timestamp = 0; |
| 358 | for (size_t i = 0; i < 1000; ++i) { |
| 359 | frame.set_timestamp(timestamp); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 360 | int64_t capture_time_us = rtc::TimeMicros(); |
| 361 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 362 | // Encode and send first parts almost instantly. |
| 363 | clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerMillisec); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 364 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), capture_time_us, |
| 365 | rtc::kNumMicrosecsPerMillisec); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 366 | // Encode heavier part, resulting in >85% usage total. |
| 367 | clock_.AdvanceTimeMicros(kDelayUs - rtc::kNumMicrosecsPerMillisec); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 368 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), capture_time_us, |
| 369 | kDelayUs); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 370 | clock_.AdvanceTimeMicros(kIntervalUs - kDelayUs); |
| 371 | timestamp += kIntervalUs * 90 / 1000; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 372 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 376 | TEST_F(OveruseFrameDetectorTest, RunOnTqNormalUsage) { |
| 377 | rtc::TaskQueue queue("OveruseFrameDetectorTestQueue"); |
| 378 | |
| 379 | rtc::Event event(false, false); |
| 380 | queue.PostTask([this, &event] { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 381 | overuse_detector_->StartCheckForOveruse(options_, observer_); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 382 | event.Set(); |
| 383 | }); |
| 384 | event.Wait(rtc::Event::kForever); |
| 385 | |
| 386 | // Expect NormalUsage(). When called, stop the |overuse_detector_| and then |
| 387 | // set |event| to end the test. |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 388 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)) |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 389 | .WillOnce(InvokeWithoutArgs([this, &event] { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 390 | overuse_detector_->StopCheckForOveruse(); |
| 391 | event.Set(); |
| 392 | })); |
| 393 | |
eladalon | 1cc5fc3 | 2017-08-23 04:15:18 -0700 | [diff] [blame] | 394 | queue.PostTask([this] { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 395 | const int kDelayUs1 = 5 * rtc::kNumMicrosecsPerMillisec; |
| 396 | const int kDelayUs2 = 6 * rtc::kNumMicrosecsPerMillisec; |
| 397 | InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight, |
| 398 | kDelayUs1); |
| 399 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 400 | kDelayUs2); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 401 | }); |
| 402 | |
| 403 | EXPECT_TRUE(event.Wait(10000)); |
| 404 | } |
| 405 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 406 | TEST_F(OveruseFrameDetectorTest, MaxIntervalScalesWithFramerate) { |
| 407 | const int kCapturerMaxFrameRate = 30; |
| 408 | const int kEncodeMaxFrameRate = 20; // Maximum fps the encoder can sustain. |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 409 | |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 410 | overuse_detector_->SetOptions(options_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 411 | // Trigger overuse. |
| 412 | int64_t frame_interval_us = rtc::kNumMicrosecsPerSec / kCapturerMaxFrameRate; |
| 413 | // Processing time just below over use limit given kEncodeMaxFrameRate. |
| 414 | int64_t processing_time_us = |
| 415 | (98 * OveruseProcessingTimeLimitForFramerate(kEncodeMaxFrameRate)) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 416 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 417 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 418 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 419 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 420 | overuse_detector_->CheckForOveruse(observer_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 421 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 422 | |
| 423 | // Simulate frame rate reduction and normal usage. |
| 424 | frame_interval_us = rtc::kNumMicrosecsPerSec / kEncodeMaxFrameRate; |
| 425 | overuse_detector_->OnTargetFramerateUpdated(kEncodeMaxFrameRate); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 426 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(0); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 427 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 428 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 429 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 430 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Reduce processing time to trigger underuse. |
| 434 | processing_time_us = |
| 435 | (98 * UnderuseProcessingTimeLimitForFramerate(kEncodeMaxFrameRate)) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 436 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 437 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 438 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 439 | overuse_detector_->CheckForOveruse(observer_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 442 | TEST_F(OveruseFrameDetectorTest, RespectsMinFramerate) { |
| 443 | const int kMinFrameRate = 7; // Minimum fps allowed by current detector impl. |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 444 | overuse_detector_->SetOptions(options_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 445 | overuse_detector_->OnTargetFramerateUpdated(kMinFrameRate); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 446 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 447 | // Normal usage just at the limit. |
| 448 | int64_t frame_interval_us = rtc::kNumMicrosecsPerSec / kMinFrameRate; |
| 449 | // Processing time just below over use limit given kEncodeMaxFrameRate. |
| 450 | int64_t processing_time_us = |
| 451 | (98 * OveruseProcessingTimeLimitForFramerate(kMinFrameRate)) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 452 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(0); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 453 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 454 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 455 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 456 | overuse_detector_->CheckForOveruse(observer_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 457 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 458 | |
| 459 | // Over the limit to overuse. |
| 460 | processing_time_us = |
| 461 | (102 * OveruseProcessingTimeLimitForFramerate(kMinFrameRate)) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 462 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 463 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 464 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 465 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 466 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // Reduce input frame rate. Should still trigger overuse. |
| 470 | overuse_detector_->OnTargetFramerateUpdated(kMinFrameRate - 1); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 471 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 472 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 473 | InsertAndSendFramesWithInterval(1200, frame_interval_us, kWidth, kHeight, |
| 474 | processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 475 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | TEST_F(OveruseFrameDetectorTest, LimitsMaxFrameInterval) { |
| 480 | const int kMaxFrameRate = 20; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 481 | overuse_detector_->SetOptions(options_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 482 | overuse_detector_->OnTargetFramerateUpdated(kMaxFrameRate); |
| 483 | int64_t frame_interval_us = rtc::kNumMicrosecsPerSec / kMaxFrameRate; |
| 484 | // Maximum frame interval allowed is 35% above ideal. |
| 485 | int64_t max_frame_interval_us = (135 * frame_interval_us) / 100; |
| 486 | // Maximum processing time, without triggering overuse, allowed with the above |
| 487 | // frame interval. |
| 488 | int64_t max_processing_time_us = |
| 489 | (max_frame_interval_us * options_.high_encode_usage_threshold_percent) / |
| 490 | 100; |
| 491 | |
| 492 | // Processing time just below overuse limit given kMaxFrameRate. |
| 493 | int64_t processing_time_us = (98 * max_processing_time_us) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 494 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(0); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 495 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 496 | InsertAndSendFramesWithInterval(1200, max_frame_interval_us, kWidth, |
| 497 | kHeight, processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 498 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Go above limit, trigger overuse. |
| 502 | processing_time_us = (102 * max_processing_time_us) / 100; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 503 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 504 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 505 | InsertAndSendFramesWithInterval(1200, max_frame_interval_us, kWidth, |
| 506 | kHeight, processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 507 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // Increase frame interval, should still trigger overuse. |
| 511 | max_frame_interval_us *= 2; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 512 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 513 | for (int i = 0; i < options_.high_threshold_consecutive_count; ++i) { |
| 514 | InsertAndSendFramesWithInterval(1200, max_frame_interval_us, kWidth, |
| 515 | kHeight, processing_time_us); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 516 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 517 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 520 | // Models screencast, with irregular arrival of frames which are heavy |
| 521 | // to encode. |
| 522 | TEST_F(OveruseFrameDetectorTest, NoOveruseForLargeRandomFrameInterval) { |
| 523 | // TODO(bugs.webrtc.org/8504): When new estimator is relanded, |
| 524 | // behavior is improved in this scenario, with only AdaptUp events, |
| 525 | // and estimated load closer to the true average. |
| 526 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 527 | // EXPECT_CALL(mock_observer_, AdaptDown(_)).Times(0); |
| 528 | // EXPECT_CALL(mock_observer_, AdaptUp(reason_)) |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 529 | // .Times(testing::AtLeast(1)); |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 530 | overuse_detector_->SetOptions(options_); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 531 | |
| 532 | const int kNumFrames = 500; |
| 533 | const int kEncodeTimeUs = 100 * rtc::kNumMicrosecsPerMillisec; |
| 534 | |
| 535 | const int kMinIntervalUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 536 | const int kMaxIntervalUs = 1000 * rtc::kNumMicrosecsPerMillisec; |
| 537 | |
| 538 | const int kTargetFramerate = 5; |
| 539 | |
| 540 | overuse_detector_->OnTargetFramerateUpdated(kTargetFramerate); |
| 541 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 542 | InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs, |
| 543 | kMaxIntervalUs, kWidth, kHeight, |
| 544 | kEncodeTimeUs); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 545 | // Average usage 19%. Check that estimate is in the right ball park. |
| 546 | // EXPECT_NEAR(UsagePercent(), 20, 10); |
| 547 | EXPECT_NEAR(UsagePercent(), 20, 35); |
| 548 | } |
| 549 | |
| 550 | // Models screencast, with irregular arrival of frames, often |
| 551 | // exceeding the timeout interval. |
| 552 | TEST_F(OveruseFrameDetectorTest, NoOveruseForRandomFrameIntervalWithReset) { |
| 553 | // TODO(bugs.webrtc.org/8504): When new estimator is relanded, |
| 554 | // behavior is improved in this scenario, and we get AdaptUp events. |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 555 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 556 | EXPECT_CALL(mock_observer_, AdaptDown(_)).Times(0); |
| 557 | // EXPECT_CALL(mock_observer_, AdaptUp(reason_)) |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 558 | // .Times(testing::AtLeast(1)); |
| 559 | |
| 560 | const int kNumFrames = 500; |
| 561 | const int kEncodeTimeUs = 100 * rtc::kNumMicrosecsPerMillisec; |
| 562 | |
| 563 | const int kMinIntervalUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 564 | const int kMaxIntervalUs = 3000 * rtc::kNumMicrosecsPerMillisec; |
| 565 | |
| 566 | const int kTargetFramerate = 5; |
| 567 | |
| 568 | overuse_detector_->OnTargetFramerateUpdated(kTargetFramerate); |
| 569 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 570 | InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs, |
| 571 | kMaxIntervalUs, kWidth, kHeight, |
| 572 | kEncodeTimeUs); |
Niels Möller | e541be7 | 2017-12-13 13:03:10 +0100 | [diff] [blame] | 573 | |
| 574 | // Average usage 6.6%, but since the frame_timeout_interval_ms is |
| 575 | // only 1500 ms, we often reset the estimate to the initial value. |
| 576 | // Check that estimate is in the right ball park. |
| 577 | EXPECT_GE(UsagePercent(), 1); |
| 578 | EXPECT_LE(UsagePercent(), InitialUsage() + 5); |
| 579 | } |
| 580 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 581 | // Tests using new cpu load estimator |
| 582 | class OveruseFrameDetectorTest2 : public OveruseFrameDetectorTest { |
| 583 | protected: |
| 584 | void SetUp() override { |
| 585 | options_.filter_time_ms = 5 * rtc::kNumMillisecsPerSec; |
| 586 | OveruseFrameDetectorTest::SetUp(); |
| 587 | } |
| 588 | |
| 589 | void InsertAndSendFramesWithInterval(int num_frames, |
| 590 | int interval_us, |
| 591 | int width, |
| 592 | int height, |
| 593 | int delay_us) override { |
| 594 | VideoFrame frame(I420Buffer::Create(width, height), |
| 595 | webrtc::kVideoRotation_0, 0); |
| 596 | while (num_frames-- > 0) { |
| 597 | int64_t capture_time_us = rtc::TimeMicros(); |
| 598 | overuse_detector_->FrameCaptured(frame, capture_time_us /* ignored */); |
| 599 | overuse_detector_->FrameSent(0 /* ignored timestamp */, |
| 600 | 0 /* ignored send_time_us */, |
| 601 | capture_time_us, delay_us); |
| 602 | clock_.AdvanceTimeMicros(interval_us); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | void InsertAndSendFramesWithRandomInterval(int num_frames, |
| 607 | int min_interval_us, |
| 608 | int max_interval_us, |
| 609 | int width, |
| 610 | int height, |
| 611 | int delay_us) override { |
| 612 | webrtc::Random random(17); |
| 613 | |
| 614 | VideoFrame frame(I420Buffer::Create(width, height), |
| 615 | webrtc::kVideoRotation_0, 0); |
| 616 | for (int i = 0; i < num_frames; i++) { |
| 617 | int interval_us = random.Rand(min_interval_us, max_interval_us); |
| 618 | int64_t capture_time_us = rtc::TimeMicros(); |
| 619 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
| 620 | overuse_detector_->FrameSent(0 /* ignored timestamp */, |
| 621 | 0 /* ignored send_time_us */, |
| 622 | capture_time_us, delay_us); |
| 623 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 624 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 625 | clock_.AdvanceTimeMicros(interval_us); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | void ForceUpdate(int width, int height) override { |
| 630 | // This is mainly to check initial values and whether the overuse |
| 631 | // detector has been reset or not. |
| 632 | InsertAndSendFramesWithInterval(1, rtc::kNumMicrosecsPerSec, width, height, |
| 633 | kFrameIntervalUs); |
| 634 | } |
| 635 | }; |
| 636 | |
| 637 | // UsagePercent() > high_encode_usage_threshold_percent => overuse. |
| 638 | // UsagePercent() < low_encode_usage_threshold_percent => underuse. |
| 639 | TEST_F(OveruseFrameDetectorTest2, TriggerOveruse) { |
| 640 | // usage > high => overuse |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 641 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 642 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 643 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 644 | } |
| 645 | |
| 646 | TEST_F(OveruseFrameDetectorTest2, OveruseAndRecover) { |
| 647 | // usage > high => overuse |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 648 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 649 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 650 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 651 | // usage < low => underuse |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 652 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 653 | TriggerUnderuse(); |
| 654 | } |
| 655 | |
| 656 | TEST_F(OveruseFrameDetectorTest2, DoubleOveruseAndRecover) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 657 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 658 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(2); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 659 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 660 | TriggerOveruse(options_.high_threshold_consecutive_count); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 661 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 662 | TriggerUnderuse(); |
| 663 | } |
| 664 | |
| 665 | TEST_F(OveruseFrameDetectorTest2, TriggerUnderuseWithMinProcessCount) { |
| 666 | const int kProcessIntervalUs = 5 * rtc::kNumMicrosecsPerSec; |
| 667 | options_.min_process_count = 1; |
| 668 | CpuOveruseObserverImpl overuse_observer; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 669 | observer_ = nullptr; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 670 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 671 | InsertAndSendFramesWithInterval(1200, kFrameIntervalUs, kWidth, kHeight, |
| 672 | kProcessTimeUs); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 673 | overuse_detector_->CheckForOveruse(&overuse_observer); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 674 | EXPECT_EQ(0, overuse_observer.normaluse_); |
| 675 | clock_.AdvanceTimeMicros(kProcessIntervalUs); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 676 | overuse_detector_->CheckForOveruse(&overuse_observer); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 677 | EXPECT_EQ(1, overuse_observer.normaluse_); |
| 678 | } |
| 679 | |
| 680 | TEST_F(OveruseFrameDetectorTest2, ConstantOveruseGivesNoNormalUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 681 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 682 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(0); |
| 683 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(64); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 684 | for (size_t i = 0; i < 64; ++i) { |
| 685 | TriggerOveruse(options_.high_threshold_consecutive_count); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | TEST_F(OveruseFrameDetectorTest2, ConsecutiveCountTriggersOveruse) { |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 690 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(1); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 691 | options_.high_threshold_consecutive_count = 2; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 692 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 693 | TriggerOveruse(2); |
| 694 | } |
| 695 | |
| 696 | TEST_F(OveruseFrameDetectorTest2, IncorrectConsecutiveCountTriggersNoOveruse) { |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 697 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(0); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 698 | options_.high_threshold_consecutive_count = 2; |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 699 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 700 | TriggerOveruse(1); |
| 701 | } |
| 702 | |
| 703 | TEST_F(OveruseFrameDetectorTest2, ProcessingUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 704 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 705 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 706 | kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 707 | EXPECT_EQ(kProcessTimeUs * 100 / kFrameIntervalUs, UsagePercent()); |
| 708 | } |
| 709 | |
| 710 | TEST_F(OveruseFrameDetectorTest2, ResetAfterResolutionChange) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 711 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 712 | ForceUpdate(kWidth, kHeight); |
| 713 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 714 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 715 | kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 716 | EXPECT_NE(InitialUsage(), UsagePercent()); |
| 717 | // Verify reset (with new width/height). |
| 718 | ForceUpdate(kWidth, kHeight + 1); |
| 719 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 720 | } |
| 721 | |
| 722 | TEST_F(OveruseFrameDetectorTest2, ResetAfterFrameTimeout) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 723 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 724 | ForceUpdate(kWidth, kHeight); |
| 725 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 726 | InsertAndSendFramesWithInterval(1000, kFrameIntervalUs, kWidth, kHeight, |
| 727 | kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 728 | EXPECT_NE(InitialUsage(), UsagePercent()); |
| 729 | InsertAndSendFramesWithInterval( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 730 | 2, options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec, |
| 731 | kWidth, kHeight, kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 732 | EXPECT_NE(InitialUsage(), UsagePercent()); |
| 733 | // Verify reset. |
| 734 | InsertAndSendFramesWithInterval( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 735 | 2, |
| 736 | (options_.frame_timeout_interval_ms + 1) * rtc::kNumMicrosecsPerMillisec, |
| 737 | kWidth, kHeight, kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 738 | ForceUpdate(kWidth, kHeight); |
| 739 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 740 | } |
| 741 | |
| 742 | TEST_F(OveruseFrameDetectorTest2, ConvergesSlowly) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 743 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 744 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 745 | kProcessTimeUs); |
| 746 | // No update for the first sample. |
| 747 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 748 | |
| 749 | // Total time approximately 40 * 33ms = 1.3s, significantly less |
| 750 | // than the 5s time constant. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 751 | InsertAndSendFramesWithInterval(40, kFrameIntervalUs, kWidth, kHeight, |
| 752 | kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 753 | |
| 754 | // Should have started to approach correct load of 15%, but not very far. |
| 755 | EXPECT_LT(UsagePercent(), InitialUsage()); |
| 756 | EXPECT_GT(UsagePercent(), (InitialUsage() * 3 + 15) / 4); |
| 757 | |
| 758 | // Run for roughly 10s more, should now be closer. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 759 | InsertAndSendFramesWithInterval(300, kFrameIntervalUs, kWidth, kHeight, |
| 760 | kProcessTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 761 | EXPECT_NEAR(UsagePercent(), 20, 5); |
| 762 | } |
| 763 | |
| 764 | TEST_F(OveruseFrameDetectorTest2, InitialProcessingUsage) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 765 | overuse_detector_->SetOptions(options_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 766 | ForceUpdate(kWidth, kHeight); |
| 767 | EXPECT_EQ(InitialUsage(), UsagePercent()); |
| 768 | } |
| 769 | |
| 770 | TEST_F(OveruseFrameDetectorTest2, MeasuresMultipleConcurrentSamples) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 771 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 772 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 773 | static const int kIntervalUs = 33 * rtc::kNumMicrosecsPerMillisec; |
| 774 | static const size_t kNumFramesEncodingDelay = 3; |
| 775 | VideoFrame frame(I420Buffer::Create(kWidth, kHeight), |
| 776 | webrtc::kVideoRotation_0, 0); |
| 777 | for (size_t i = 0; i < 1000; ++i) { |
| 778 | // Unique timestamps. |
| 779 | frame.set_timestamp(static_cast<uint32_t>(i)); |
| 780 | int64_t capture_time_us = rtc::TimeMicros(); |
| 781 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
| 782 | clock_.AdvanceTimeMicros(kIntervalUs); |
| 783 | if (i > kNumFramesEncodingDelay) { |
| 784 | overuse_detector_->FrameSent( |
| 785 | static_cast<uint32_t>(i - kNumFramesEncodingDelay), rtc::TimeMicros(), |
| 786 | capture_time_us, kIntervalUs); |
| 787 | } |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 788 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | TEST_F(OveruseFrameDetectorTest2, UpdatesExistingSamples) { |
| 793 | // >85% encoding time should trigger overuse. |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 794 | overuse_detector_->SetOptions(options_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 795 | EXPECT_CALL(mock_observer_, AdaptDown(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 796 | static const int kIntervalUs = 33 * rtc::kNumMicrosecsPerMillisec; |
| 797 | static const int kDelayUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 798 | VideoFrame frame(I420Buffer::Create(kWidth, kHeight), |
| 799 | webrtc::kVideoRotation_0, 0); |
| 800 | uint32_t timestamp = 0; |
| 801 | for (size_t i = 0; i < 1000; ++i) { |
| 802 | frame.set_timestamp(timestamp); |
| 803 | int64_t capture_time_us = rtc::TimeMicros(); |
| 804 | overuse_detector_->FrameCaptured(frame, capture_time_us); |
| 805 | // Encode and send first parts almost instantly. |
| 806 | clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerMillisec); |
| 807 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), capture_time_us, |
| 808 | rtc::kNumMicrosecsPerMillisec); |
| 809 | // Encode heavier part, resulting in >85% usage total. |
| 810 | clock_.AdvanceTimeMicros(kDelayUs - rtc::kNumMicrosecsPerMillisec); |
| 811 | overuse_detector_->FrameSent(timestamp, rtc::TimeMicros(), capture_time_us, |
| 812 | kDelayUs); |
| 813 | clock_.AdvanceTimeMicros(kIntervalUs - kDelayUs); |
| 814 | timestamp += kIntervalUs * 90 / 1000; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 815 | overuse_detector_->CheckForOveruse(observer_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | TEST_F(OveruseFrameDetectorTest2, RunOnTqNormalUsage) { |
| 820 | rtc::TaskQueue queue("OveruseFrameDetectorTestQueue"); |
| 821 | |
| 822 | rtc::Event event(false, false); |
| 823 | queue.PostTask([this, &event] { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 824 | overuse_detector_->StartCheckForOveruse(options_, observer_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 825 | event.Set(); |
| 826 | }); |
| 827 | event.Wait(rtc::Event::kForever); |
| 828 | |
| 829 | // Expect NormalUsage(). When called, stop the |overuse_detector_| and then |
| 830 | // set |event| to end the test. |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 831 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)) |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 832 | .WillOnce(InvokeWithoutArgs([this, &event] { |
| 833 | overuse_detector_->StopCheckForOveruse(); |
| 834 | event.Set(); |
| 835 | })); |
| 836 | |
| 837 | queue.PostTask([this] { |
| 838 | const int kDelayUs1 = 5 * rtc::kNumMicrosecsPerMillisec; |
| 839 | const int kDelayUs2 = 6 * rtc::kNumMicrosecsPerMillisec; |
| 840 | InsertAndSendFramesWithInterval(1300, kFrameIntervalUs, kWidth, kHeight, |
| 841 | kDelayUs1); |
| 842 | InsertAndSendFramesWithInterval(1, kFrameIntervalUs, kWidth, kHeight, |
| 843 | kDelayUs2); |
| 844 | }); |
| 845 | |
| 846 | EXPECT_TRUE(event.Wait(10000)); |
| 847 | } |
| 848 | |
| 849 | // Models screencast, with irregular arrival of frames which are heavy |
| 850 | // to encode. |
| 851 | TEST_F(OveruseFrameDetectorTest2, NoOveruseForLargeRandomFrameInterval) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 852 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 853 | EXPECT_CALL(mock_observer_, AdaptDown(_)).Times(0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 854 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 855 | |
| 856 | const int kNumFrames = 500; |
| 857 | const int kEncodeTimeUs = 100 * rtc::kNumMicrosecsPerMillisec; |
| 858 | |
| 859 | const int kMinIntervalUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 860 | const int kMaxIntervalUs = 1000 * rtc::kNumMicrosecsPerMillisec; |
| 861 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 862 | InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs, |
| 863 | kMaxIntervalUs, kWidth, kHeight, |
| 864 | kEncodeTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 865 | // Average usage 19%. Check that estimate is in the right ball park. |
| 866 | EXPECT_NEAR(UsagePercent(), 20, 10); |
| 867 | } |
| 868 | |
| 869 | // Models screencast, with irregular arrival of frames, often |
| 870 | // exceeding the timeout interval. |
| 871 | TEST_F(OveruseFrameDetectorTest2, NoOveruseForRandomFrameIntervalWithReset) { |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 872 | overuse_detector_->SetOptions(options_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 873 | EXPECT_CALL(mock_observer_, AdaptDown(_)).Times(0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 874 | EXPECT_CALL(mock_observer_, AdaptUp(reason_)).Times(testing::AtLeast(1)); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 875 | |
| 876 | const int kNumFrames = 500; |
| 877 | const int kEncodeTimeUs = 100 * rtc::kNumMicrosecsPerMillisec; |
| 878 | |
| 879 | const int kMinIntervalUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 880 | const int kMaxIntervalUs = 3000 * rtc::kNumMicrosecsPerMillisec; |
| 881 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 882 | InsertAndSendFramesWithRandomInterval(kNumFrames, kMinIntervalUs, |
| 883 | kMaxIntervalUs, kWidth, kHeight, |
| 884 | kEncodeTimeUs); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 885 | |
| 886 | // Average usage 6.6%, but since the frame_timeout_interval_ms is |
| 887 | // only 1500 ms, we often reset the estimate to the initial value. |
| 888 | // Check that estimate is in the right ball park. |
| 889 | EXPECT_GE(UsagePercent(), 1); |
| 890 | EXPECT_LE(UsagePercent(), InitialUsage() + 5); |
| 891 | } |
| 892 | |
Niels Möller | 58d2a5e | 2018-08-07 16:37:18 +0200 | [diff] [blame^] | 893 | TEST_F(OveruseFrameDetectorTest2, ToleratesOutOfOrderFrames) { |
| 894 | overuse_detector_->SetOptions(options_); |
| 895 | // Represents a cpu utilization close to 100%. First input frame results in |
| 896 | // three encoded frames, and the last of those isn't finished until after the |
| 897 | // first encoded frame corresponding to the next input frame. |
| 898 | const int kEncodeTimeUs = 30 * rtc::kNumMicrosecsPerMillisec; |
| 899 | const int kCaptureTimesMs[] = { 33, 33, 66, 33 }; |
| 900 | |
| 901 | for (int capture_time_ms : kCaptureTimesMs) { |
| 902 | overuse_detector_->FrameSent( |
| 903 | 0, 0, capture_time_ms * rtc::kNumMicrosecsPerMillisec, kEncodeTimeUs); |
| 904 | } |
| 905 | EXPECT_GE(UsagePercent(), InitialUsage()); |
| 906 | } |
| 907 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 908 | } // namespace webrtc |