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