perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 11 | #include "video/video_stream_encoder.h" |
| 12 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 14 | #include <limits> |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 17 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/video/i420_buffer.h" |
Erik Språng | f93eda1 | 2019-01-16 17:10:57 +0100 | [diff] [blame] | 19 | #include "api/video/video_bitrate_allocation.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 20 | #include "api/video_codecs/create_vp8_temporal_layers.h" |
| 21 | #include "api/video_codecs/vp8_temporal_layers.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "media/base/video_adapter.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 28 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "system_wrappers/include/sleep.h" |
| 30 | #include "test/encoder_settings.h" |
| 31 | #include "test/fake_encoder.h" |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 32 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "test/frame_generator.h" |
| 34 | #include "test/gmock.h" |
| 35 | #include "test/gtest.h" |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 36 | #include "test/video_encoder_proxy_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "video/send_statistics_proxy.h" |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 38 | |
| 39 | namespace webrtc { |
| 40 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 41 | using ScaleReason = AdaptationObserverInterface::AdaptReason; |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 42 | using ::testing::_; |
| 43 | using ::testing::Return; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 44 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 45 | namespace { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 46 | const int kMinPixelsPerFrame = 320 * 180; |
| 47 | const int kMinFramerateFps = 2; |
| 48 | const int kMinBalancedFramerateFps = 7; |
| 49 | const int64_t kFrameTimeoutMs = 100; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 50 | const size_t kMaxPayloadLength = 1440; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 51 | const int kTargetBitrateBps = 1000000; |
| 52 | const int kLowTargetBitrateBps = kTargetBitrateBps / 10; |
| 53 | const int kMaxInitialFramedrop = 4; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 54 | const int kDefaultFramerate = 30; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 55 | const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 56 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 57 | class TestBuffer : public webrtc::I420Buffer { |
| 58 | public: |
| 59 | TestBuffer(rtc::Event* event, int width, int height) |
| 60 | : I420Buffer(width, height), event_(event) {} |
| 61 | |
| 62 | private: |
| 63 | friend class rtc::RefCountedObject<TestBuffer>; |
| 64 | ~TestBuffer() override { |
| 65 | if (event_) |
| 66 | event_->Set(); |
| 67 | } |
| 68 | rtc::Event* const event_; |
| 69 | }; |
| 70 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 71 | class CpuOveruseDetectorProxy : public OveruseFrameDetector { |
| 72 | public: |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 73 | explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer) |
| 74 | : OveruseFrameDetector(metrics_observer), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 75 | last_target_framerate_fps_(-1) {} |
| 76 | virtual ~CpuOveruseDetectorProxy() {} |
| 77 | |
| 78 | void OnTargetFramerateUpdated(int framerate_fps) override { |
| 79 | rtc::CritScope cs(&lock_); |
| 80 | last_target_framerate_fps_ = framerate_fps; |
| 81 | OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps); |
| 82 | } |
| 83 | |
| 84 | int GetLastTargetFramerate() { |
| 85 | rtc::CritScope cs(&lock_); |
| 86 | return last_target_framerate_fps_; |
| 87 | } |
| 88 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 89 | CpuOveruseOptions GetOptions() { return options_; } |
| 90 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 91 | private: |
| 92 | rtc::CriticalSection lock_; |
| 93 | int last_target_framerate_fps_ RTC_GUARDED_BY(lock_); |
| 94 | }; |
| 95 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 96 | class VideoStreamEncoderUnderTest : public VideoStreamEncoder { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 97 | public: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 98 | VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy, |
| 99 | const VideoStreamEncoderSettings& settings) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | : VideoStreamEncoder(1 /* number_of_cores */, |
| 101 | stats_proxy, |
| 102 | settings, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | std::unique_ptr<OveruseFrameDetector>( |
| 104 | overuse_detector_proxy_ = |
| 105 | new CpuOveruseDetectorProxy(stats_proxy))) {} |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 106 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 107 | void PostTaskAndWait(bool down, AdaptReason reason) { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 108 | rtc::Event event; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 109 | encoder_queue()->PostTask([this, &event, reason, down] { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 110 | down ? AdaptDown(reason) : AdaptUp(reason); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 111 | event.Set(); |
| 112 | }); |
perkj | 070ba85 | 2017-02-16 15:46:27 -0800 | [diff] [blame] | 113 | ASSERT_TRUE(event.Wait(5000)); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 116 | // This is used as a synchronisation mechanism, to make sure that the |
| 117 | // encoder queue is not blocked before we start sending it frames. |
| 118 | void WaitUntilTaskQueueIsIdle() { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 119 | rtc::Event event; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | encoder_queue()->PostTask([&event] { event.Set(); }); |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 121 | ASSERT_TRUE(event.Wait(5000)); |
| 122 | } |
| 123 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 124 | void TriggerCpuOveruse() { PostTaskAndWait(true, AdaptReason::kCpu); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 125 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 126 | void TriggerCpuNormalUsage() { PostTaskAndWait(false, AdaptReason::kCpu); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 127 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 128 | void TriggerQualityLow() { PostTaskAndWait(true, AdaptReason::kQuality); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 129 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 130 | void TriggerQualityHigh() { PostTaskAndWait(false, AdaptReason::kQuality); } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 131 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 132 | CpuOveruseDetectorProxy* overuse_detector_proxy_; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 135 | class VideoStreamFactory |
| 136 | : public VideoEncoderConfig::VideoStreamFactoryInterface { |
| 137 | public: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 138 | explicit VideoStreamFactory(size_t num_temporal_layers, int framerate) |
| 139 | : num_temporal_layers_(num_temporal_layers), framerate_(framerate) { |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 140 | EXPECT_GT(num_temporal_layers, 0u); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 141 | EXPECT_GT(framerate, 0); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
| 145 | std::vector<VideoStream> CreateEncoderStreams( |
| 146 | int width, |
| 147 | int height, |
| 148 | const VideoEncoderConfig& encoder_config) override { |
| 149 | std::vector<VideoStream> streams = |
| 150 | test::CreateVideoStreams(width, height, encoder_config); |
| 151 | for (VideoStream& stream : streams) { |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 152 | stream.num_temporal_layers = num_temporal_layers_; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 153 | stream.max_framerate = framerate_; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 154 | } |
| 155 | return streams; |
| 156 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 157 | |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 158 | const size_t num_temporal_layers_; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 159 | const int framerate_; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 162 | class AdaptingFrameForwarder : public test::FrameForwarder { |
| 163 | public: |
| 164 | AdaptingFrameForwarder() : adaptation_enabled_(false) {} |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 165 | ~AdaptingFrameForwarder() override {} |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 166 | |
| 167 | void set_adaptation_enabled(bool enabled) { |
| 168 | rtc::CritScope cs(&crit_); |
| 169 | adaptation_enabled_ = enabled; |
| 170 | } |
| 171 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 172 | bool adaption_enabled() const { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 173 | rtc::CritScope cs(&crit_); |
| 174 | return adaptation_enabled_; |
| 175 | } |
| 176 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 177 | rtc::VideoSinkWants last_wants() const { |
| 178 | rtc::CritScope cs(&crit_); |
| 179 | return last_wants_; |
| 180 | } |
| 181 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 182 | absl::optional<int> last_sent_width() const { return last_width_; } |
| 183 | absl::optional<int> last_sent_height() const { return last_height_; } |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 184 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 185 | void IncomingCapturedFrame(const VideoFrame& video_frame) override { |
| 186 | int cropped_width = 0; |
| 187 | int cropped_height = 0; |
| 188 | int out_width = 0; |
| 189 | int out_height = 0; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 190 | if (adaption_enabled()) { |
| 191 | if (adapter_.AdaptFrameResolution( |
| 192 | video_frame.width(), video_frame.height(), |
| 193 | video_frame.timestamp_us() * 1000, &cropped_width, |
| 194 | &cropped_height, &out_width, &out_height)) { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 195 | VideoFrame adapted_frame = |
| 196 | VideoFrame::Builder() |
| 197 | .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>( |
| 198 | nullptr, out_width, out_height)) |
| 199 | .set_timestamp_rtp(99) |
| 200 | .set_timestamp_ms(99) |
| 201 | .set_rotation(kVideoRotation_0) |
| 202 | .build(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 203 | adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); |
| 204 | test::FrameForwarder::IncomingCapturedFrame(adapted_frame); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 205 | last_width_.emplace(adapted_frame.width()); |
| 206 | last_height_.emplace(adapted_frame.height()); |
| 207 | } else { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 208 | last_width_ = absl::nullopt; |
| 209 | last_height_ = absl::nullopt; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 210 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 211 | } else { |
| 212 | test::FrameForwarder::IncomingCapturedFrame(video_frame); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 213 | last_width_.emplace(video_frame.width()); |
| 214 | last_height_.emplace(video_frame.height()); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 219 | const rtc::VideoSinkWants& wants) override { |
| 220 | rtc::CritScope cs(&crit_); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 221 | last_wants_ = sink_wants(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 222 | adapter_.OnResolutionFramerateRequest(wants.target_pixel_count, |
| 223 | wants.max_pixel_count, |
| 224 | wants.max_framerate_fps); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 225 | test::FrameForwarder::AddOrUpdateSink(sink, wants); |
| 226 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 227 | cricket::VideoAdapter adapter_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 228 | bool adaptation_enabled_ RTC_GUARDED_BY(crit_); |
| 229 | rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 230 | absl::optional<int> last_width_; |
| 231 | absl::optional<int> last_height_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 232 | }; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 233 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 234 | // TODO(nisse): Mock only VideoStreamEncoderObserver. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 235 | class MockableSendStatisticsProxy : public SendStatisticsProxy { |
| 236 | public: |
| 237 | MockableSendStatisticsProxy(Clock* clock, |
| 238 | const VideoSendStream::Config& config, |
| 239 | VideoEncoderConfig::ContentType content_type) |
| 240 | : SendStatisticsProxy(clock, config, content_type) {} |
| 241 | |
| 242 | VideoSendStream::Stats GetStats() override { |
| 243 | rtc::CritScope cs(&lock_); |
| 244 | if (mock_stats_) |
| 245 | return *mock_stats_; |
| 246 | return SendStatisticsProxy::GetStats(); |
| 247 | } |
| 248 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 249 | int GetInputFrameRate() const override { |
| 250 | rtc::CritScope cs(&lock_); |
| 251 | if (mock_stats_) |
| 252 | return mock_stats_->input_frame_rate; |
| 253 | return SendStatisticsProxy::GetInputFrameRate(); |
| 254 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 255 | void SetMockStats(const VideoSendStream::Stats& stats) { |
| 256 | rtc::CritScope cs(&lock_); |
| 257 | mock_stats_.emplace(stats); |
| 258 | } |
| 259 | |
| 260 | void ResetMockStats() { |
| 261 | rtc::CritScope cs(&lock_); |
| 262 | mock_stats_.reset(); |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | rtc::CriticalSection lock_; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 267 | absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 270 | class MockBitrateObserver : public VideoBitrateAllocationObserver { |
| 271 | public: |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 272 | MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 275 | } // namespace |
| 276 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 277 | class VideoStreamEncoderTest : public ::testing::Test { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 278 | public: |
| 279 | static const int kDefaultTimeoutMs = 30 * 1000; |
| 280 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 281 | VideoStreamEncoderTest() |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 282 | : video_send_config_(VideoSendStream::Config(nullptr)), |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 283 | codec_width_(320), |
| 284 | codec_height_(240), |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 285 | max_framerate_(kDefaultFramerate), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 286 | fake_encoder_(), |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 287 | encoder_factory_(&fake_encoder_), |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 288 | bitrate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 289 | stats_proxy_(new MockableSendStatisticsProxy( |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 290 | Clock::GetRealTimeClock(), |
| 291 | video_send_config_, |
| 292 | webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 293 | sink_(&fake_encoder_) {} |
| 294 | |
| 295 | void SetUp() override { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 296 | metrics::Reset(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 297 | video_send_config_ = VideoSendStream::Config(nullptr); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 298 | video_send_config_.encoder_settings.encoder_factory = &encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 299 | video_send_config_.encoder_settings.bitrate_allocator_factory = |
| 300 | bitrate_allocator_factory_.get(); |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 301 | video_send_config_.rtp.payload_name = "FAKE"; |
| 302 | video_send_config_.rtp.payload_type = 125; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 303 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 304 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 305 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 306 | video_encoder_config.video_stream_factory = |
| 307 | new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 308 | video_encoder_config_ = video_encoder_config.Copy(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 309 | |
| 310 | // Framerate limit is specified by the VideoStreamFactory. |
| 311 | std::vector<VideoStream> streams = |
| 312 | video_encoder_config.video_stream_factory->CreateEncoderStreams( |
| 313 | codec_width_, codec_height_, video_encoder_config); |
| 314 | max_framerate_ = streams[0].max_framerate; |
| 315 | fake_clock_.SetTimeMicros(1234); |
| 316 | |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 317 | ConfigureEncoder(std::move(video_encoder_config)); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 320 | void ConfigureEncoder(VideoEncoderConfig video_encoder_config) { |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 321 | if (video_stream_encoder_) |
| 322 | video_stream_encoder_->Stop(); |
| 323 | video_stream_encoder_.reset(new VideoStreamEncoderUnderTest( |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 324 | stats_proxy_.get(), video_send_config_.encoder_settings)); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 325 | video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */); |
| 326 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 327 | &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 328 | video_stream_encoder_->SetStartBitrate(kTargetBitrateBps); |
| 329 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 330 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 331 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void ResetEncoder(const std::string& payload_name, |
| 335 | size_t num_streams, |
| 336 | size_t num_temporal_layers, |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 337 | unsigned char num_spatial_layers, |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 338 | bool screenshare) { |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 339 | video_send_config_.rtp.payload_name = payload_name; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 340 | |
| 341 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 342 | video_encoder_config.codec_type = PayloadStringToCodecType(payload_name); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 343 | video_encoder_config.number_of_streams = num_streams; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 344 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 345 | video_encoder_config.video_stream_factory = |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 346 | new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers, |
| 347 | kDefaultFramerate); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 348 | video_encoder_config.content_type = |
| 349 | screenshare ? VideoEncoderConfig::ContentType::kScreen |
| 350 | : VideoEncoderConfig::ContentType::kRealtimeVideo; |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 351 | if (payload_name == "VP9") { |
| 352 | VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings(); |
| 353 | vp9_settings.numberOfSpatialLayers = num_spatial_layers; |
| 354 | video_encoder_config.encoder_specific_settings = |
| 355 | new rtc::RefCountedObject< |
| 356 | VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
| 357 | } |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 358 | ConfigureEncoder(std::move(video_encoder_config)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 359 | } |
| 360 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 361 | VideoFrame CreateFrame(int64_t ntp_time_ms, |
| 362 | rtc::Event* destruction_event) const { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 363 | VideoFrame frame = |
| 364 | VideoFrame::Builder() |
| 365 | .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>( |
| 366 | destruction_event, codec_width_, codec_height_)) |
| 367 | .set_timestamp_rtp(99) |
| 368 | .set_timestamp_ms(99) |
| 369 | .set_rotation(kVideoRotation_0) |
| 370 | .build(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 371 | frame.set_ntp_time_ms(ntp_time_ms); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 372 | return frame; |
| 373 | } |
| 374 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 375 | VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 376 | VideoFrame frame = |
| 377 | VideoFrame::Builder() |
| 378 | .set_video_frame_buffer( |
| 379 | new rtc::RefCountedObject<TestBuffer>(nullptr, width, height)) |
| 380 | .set_timestamp_rtp(99) |
| 381 | .set_timestamp_ms(99) |
| 382 | .set_rotation(kVideoRotation_0) |
| 383 | .build(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 384 | frame.set_ntp_time_ms(ntp_time_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 385 | frame.set_timestamp_us(ntp_time_ms * 1000); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 386 | return frame; |
| 387 | } |
| 388 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 389 | void VerifyNoLimitation(const rtc::VideoSinkWants& wants) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 390 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 391 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 392 | EXPECT_FALSE(wants.target_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 393 | } |
| 394 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 395 | void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1, |
| 396 | const rtc::VideoSinkWants& wants2) { |
| 397 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 398 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 399 | } |
| 400 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 401 | void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) { |
| 402 | EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps); |
| 403 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 404 | EXPECT_FALSE(wants.target_pixel_count); |
| 405 | } |
| 406 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 407 | void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1, |
| 408 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 409 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 410 | EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 411 | EXPECT_GT(wants1.max_pixel_count, 0); |
| 412 | } |
| 413 | |
| 414 | void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1, |
| 415 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 416 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 417 | EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 418 | } |
| 419 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 420 | void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1, |
| 421 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 422 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 423 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 424 | } |
| 425 | |
| 426 | void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1, |
| 427 | const rtc::VideoSinkWants& wants2) { |
| 428 | EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 429 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 430 | } |
| 431 | |
| 432 | void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1, |
| 433 | const rtc::VideoSinkWants& wants2) { |
| 434 | EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 435 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 436 | } |
| 437 | |
| 438 | void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1, |
| 439 | const rtc::VideoSinkWants& wants2) { |
| 440 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 441 | EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 442 | EXPECT_GT(wants1.max_pixel_count, 0); |
| 443 | } |
| 444 | |
| 445 | void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1, |
| 446 | const rtc::VideoSinkWants& wants2) { |
| 447 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 448 | EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 449 | } |
| 450 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 451 | void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants, |
| 452 | int pixel_count) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 453 | EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 454 | EXPECT_LT(wants.max_pixel_count, pixel_count); |
| 455 | EXPECT_GT(wants.max_pixel_count, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) { |
| 459 | EXPECT_LT(wants.max_framerate_fps, fps); |
| 460 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 461 | EXPECT_FALSE(wants.target_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 462 | } |
| 463 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 464 | void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants, |
| 465 | int expected_fps) { |
| 466 | EXPECT_EQ(expected_fps, wants.max_framerate_fps); |
| 467 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 468 | EXPECT_FALSE(wants.target_pixel_count); |
| 469 | } |
| 470 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 471 | void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants, |
| 472 | int last_frame_pixels) { |
| 473 | // Balanced mode should always scale FPS to the desired range before |
| 474 | // attempting to scale resolution. |
| 475 | int fps_limit = wants.max_framerate_fps; |
| 476 | if (last_frame_pixels <= 320 * 240) { |
| 477 | EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10); |
| 478 | } else if (last_frame_pixels <= 480 * 270) { |
| 479 | EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15); |
| 480 | } else if (last_frame_pixels <= 640 * 480) { |
| 481 | EXPECT_LE(15, fps_limit); |
| 482 | } else { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 483 | EXPECT_EQ(kDefaultFramerate, fps_limit); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 487 | void WaitForEncodedFrame(int64_t expected_ntp_time) { |
| 488 | sink_.WaitForEncodedFrame(expected_ntp_time); |
| 489 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_); |
| 490 | } |
| 491 | |
| 492 | bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) { |
| 493 | bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms); |
| 494 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_); |
| 495 | return ok; |
| 496 | } |
| 497 | |
| 498 | void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) { |
| 499 | sink_.WaitForEncodedFrame(expected_width, expected_height); |
| 500 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_); |
| 501 | } |
| 502 | |
| 503 | void ExpectDroppedFrame() { |
| 504 | sink_.ExpectDroppedFrame(); |
| 505 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_); |
| 506 | } |
| 507 | |
| 508 | bool WaitForFrame(int64_t timeout_ms) { |
| 509 | bool ok = sink_.WaitForFrame(timeout_ms); |
| 510 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_); |
| 511 | return ok; |
| 512 | } |
| 513 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 514 | class TestEncoder : public test::FakeEncoder { |
| 515 | public: |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 516 | TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 517 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 518 | VideoCodec codec_config() const { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 519 | rtc::CritScope lock(&crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 520 | return config_; |
| 521 | } |
| 522 | |
| 523 | void BlockNextEncode() { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 524 | rtc::CritScope lock(&local_crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 525 | block_next_encode_ = true; |
| 526 | } |
| 527 | |
Erik Språng | aed3070 | 2018-11-05 12:57:17 +0100 | [diff] [blame] | 528 | VideoEncoder::EncoderInfo GetEncoderInfo() const override { |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 529 | rtc::CritScope lock(&local_crit_sect_); |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 530 | EncoderInfo info; |
| 531 | if (initialized_) { |
| 532 | if (quality_scaling_) { |
| 533 | info.scaling_settings = |
| 534 | VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame); |
| 535 | } |
| 536 | info.is_hardware_accelerated = is_hardware_accelerated_; |
Erik Språng | aed3070 | 2018-11-05 12:57:17 +0100 | [diff] [blame] | 537 | } |
| 538 | return info; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 539 | } |
| 540 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 541 | void ContinueEncode() { continue_encode_event_.Set(); } |
| 542 | |
| 543 | void CheckLastTimeStampsMatch(int64_t ntp_time_ms, |
| 544 | uint32_t timestamp) const { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 545 | rtc::CritScope lock(&local_crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 546 | EXPECT_EQ(timestamp_, timestamp); |
| 547 | EXPECT_EQ(ntp_time_ms_, ntp_time_ms); |
| 548 | } |
| 549 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 550 | void SetQualityScaling(bool b) { |
| 551 | rtc::CritScope lock(&local_crit_sect_); |
| 552 | quality_scaling_ = b; |
| 553 | } |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 554 | |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 555 | void SetIsHardwareAccelerated(bool is_hardware_accelerated) { |
| 556 | rtc::CritScope lock(&local_crit_sect_); |
| 557 | is_hardware_accelerated_ = is_hardware_accelerated; |
| 558 | } |
| 559 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 560 | void ForceInitEncodeFailure(bool force_failure) { |
| 561 | rtc::CritScope lock(&local_crit_sect_); |
| 562 | force_init_encode_failed_ = force_failure; |
| 563 | } |
| 564 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 565 | void SimulateOvershoot(double rate_factor) { |
| 566 | rtc::CritScope lock(&local_crit_sect_); |
| 567 | rate_factor_ = rate_factor; |
| 568 | } |
| 569 | |
| 570 | uint32_t GetLastFramerate() { |
| 571 | rtc::CritScope lock(&local_crit_sect_); |
| 572 | return last_framerate_; |
| 573 | } |
| 574 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 575 | private: |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 576 | int32_t Encode(const VideoFrame& input_image, |
| 577 | const CodecSpecificInfo* codec_specific_info, |
| 578 | const std::vector<FrameType>* frame_types) override { |
| 579 | bool block_encode; |
| 580 | { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 581 | rtc::CritScope lock(&local_crit_sect_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 582 | EXPECT_GT(input_image.timestamp(), timestamp_); |
| 583 | EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); |
| 584 | EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); |
| 585 | |
| 586 | timestamp_ = input_image.timestamp(); |
| 587 | ntp_time_ms_ = input_image.ntp_time_ms(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 588 | last_input_width_ = input_image.width(); |
| 589 | last_input_height_ = input_image.height(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 590 | block_encode = block_next_encode_; |
| 591 | block_next_encode_ = false; |
| 592 | } |
| 593 | int32_t result = |
| 594 | FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
| 595 | if (block_encode) |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 596 | EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 597 | return result; |
| 598 | } |
| 599 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 600 | int32_t InitEncode(const VideoCodec* config, |
| 601 | int32_t number_of_cores, |
| 602 | size_t max_payload_size) override { |
| 603 | int res = |
| 604 | FakeEncoder::InitEncode(config, number_of_cores, max_payload_size); |
| 605 | rtc::CritScope lock(&local_crit_sect_); |
Erik Språng | 82fad3d | 2018-03-21 09:57:23 +0100 | [diff] [blame] | 606 | if (config->codecType == kVideoCodecVP8) { |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 607 | // Simulate setting up temporal layers, in order to validate the life |
| 608 | // cycle of these objects. |
| 609 | int num_streams = std::max<int>(1, config->numberOfSimulcastStreams); |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 610 | for (int i = 0; i < num_streams; ++i) { |
| 611 | allocated_temporal_layers_.emplace_back( |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 612 | CreateVp8TemporalLayers(Vp8TemporalLayersType::kFixedPattern, |
| 613 | config->VP8().numberOfTemporalLayers)); |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | if (force_init_encode_failed_) |
| 617 | return -1; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 618 | |
| 619 | initialized_ = true; |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 620 | return res; |
| 621 | } |
| 622 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 623 | int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation, |
| 624 | uint32_t framerate) { |
| 625 | rtc::CritScope lock(&local_crit_sect_); |
| 626 | VideoBitrateAllocation adjusted_rate_allocation; |
| 627 | for (size_t si = 0; si < kMaxSpatialLayers; ++si) { |
| 628 | for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) { |
| 629 | if (rate_allocation.HasBitrate(si, ti)) { |
| 630 | adjusted_rate_allocation.SetBitrate( |
| 631 | si, ti, |
| 632 | static_cast<uint32_t>(rate_allocation.GetBitrate(si, ti) * |
| 633 | rate_factor_)); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | last_framerate_ = framerate; |
| 638 | return FakeEncoder::SetRateAllocation(adjusted_rate_allocation, |
| 639 | framerate); |
| 640 | } |
| 641 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 642 | rtc::CriticalSection local_crit_sect_; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 643 | bool initialized_ RTC_GUARDED_BY(local_crit_sect_) = false; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 644 | bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 645 | rtc::Event continue_encode_event_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 646 | uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 647 | int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 648 | int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 649 | int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 650 | bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 651 | bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false; |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 652 | std::vector<std::unique_ptr<Vp8TemporalLayers>> allocated_temporal_layers_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 653 | RTC_GUARDED_BY(local_crit_sect_); |
| 654 | bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false; |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 655 | double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0; |
| 656 | uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 657 | }; |
| 658 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 659 | class TestSink : public VideoStreamEncoder::EncoderSink { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 660 | public: |
| 661 | explicit TestSink(TestEncoder* test_encoder) |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 662 | : test_encoder_(test_encoder) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 663 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 664 | void WaitForEncodedFrame(int64_t expected_ntp_time) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 665 | EXPECT_TRUE( |
| 666 | TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs)); |
| 667 | } |
| 668 | |
| 669 | bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, |
| 670 | int64_t timeout_ms) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 671 | uint32_t timestamp = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 672 | if (!encoded_frame_event_.Wait(timeout_ms)) |
| 673 | return false; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 674 | { |
| 675 | rtc::CritScope lock(&crit_); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 676 | timestamp = last_timestamp_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 677 | } |
| 678 | test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 679 | return true; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 680 | } |
| 681 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 682 | void WaitForEncodedFrame(uint32_t expected_width, |
| 683 | uint32_t expected_height) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 684 | EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 685 | CheckLastFrameSizeMatches(expected_width, expected_height); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 688 | void CheckLastFrameSizeMatches(uint32_t expected_width, |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 689 | uint32_t expected_height) { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 690 | uint32_t width = 0; |
| 691 | uint32_t height = 0; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 692 | { |
| 693 | rtc::CritScope lock(&crit_); |
| 694 | width = last_width_; |
| 695 | height = last_height_; |
| 696 | } |
| 697 | EXPECT_EQ(expected_height, height); |
| 698 | EXPECT_EQ(expected_width, width); |
| 699 | } |
| 700 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 701 | void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); } |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 702 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 703 | bool WaitForFrame(int64_t timeout_ms) { |
| 704 | return encoded_frame_event_.Wait(timeout_ms); |
| 705 | } |
| 706 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 707 | void SetExpectNoFrames() { |
| 708 | rtc::CritScope lock(&crit_); |
| 709 | expect_frames_ = false; |
| 710 | } |
| 711 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 712 | int number_of_reconfigurations() const { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 713 | rtc::CritScope lock(&crit_); |
| 714 | return number_of_reconfigurations_; |
| 715 | } |
| 716 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 717 | int last_min_transmit_bitrate() const { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 718 | rtc::CritScope lock(&crit_); |
| 719 | return min_transmit_bitrate_bps_; |
| 720 | } |
| 721 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 722 | private: |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 723 | Result OnEncodedImage( |
| 724 | const EncodedImage& encoded_image, |
| 725 | const CodecSpecificInfo* codec_specific_info, |
| 726 | const RTPFragmentationHeader* fragmentation) override { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 727 | rtc::CritScope lock(&crit_); |
| 728 | EXPECT_TRUE(expect_frames_); |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 729 | last_timestamp_ = encoded_image.Timestamp(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 730 | last_width_ = encoded_image._encodedWidth; |
| 731 | last_height_ = encoded_image._encodedHeight; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 732 | encoded_frame_event_.Set(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 733 | return Result(Result::OK, last_timestamp_); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void OnEncoderConfigurationChanged(std::vector<VideoStream> streams, |
| 737 | int min_transmit_bitrate_bps) override { |
| 738 | rtc::CriticalSection crit_; |
| 739 | ++number_of_reconfigurations_; |
| 740 | min_transmit_bitrate_bps_ = min_transmit_bitrate_bps; |
| 741 | } |
| 742 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 743 | rtc::CriticalSection crit_; |
| 744 | TestEncoder* test_encoder_; |
| 745 | rtc::Event encoded_frame_event_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 746 | uint32_t last_timestamp_ = 0; |
| 747 | uint32_t last_height_ = 0; |
| 748 | uint32_t last_width_ = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 749 | bool expect_frames_ = true; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 750 | int number_of_reconfigurations_ = 0; |
| 751 | int min_transmit_bitrate_bps_ = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | VideoSendStream::Config video_send_config_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 755 | VideoEncoderConfig video_encoder_config_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 756 | int codec_width_; |
| 757 | int codec_height_; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 758 | int max_framerate_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 759 | TestEncoder fake_encoder_; |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 760 | test::VideoEncoderProxyFactory encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 761 | std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 762 | std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 763 | TestSink sink_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 764 | AdaptingFrameForwarder video_source_; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 765 | std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 766 | rtc::ScopedFakeClock fake_clock_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 769 | TEST_F(VideoStreamEncoderTest, EncodeOneFrame) { |
| 770 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 771 | rtc::Event frame_destroyed_event; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 772 | video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 773 | WaitForEncodedFrame(1); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 774 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 775 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 776 | } |
| 777 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 778 | TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 779 | // Dropped since no target bitrate has been set. |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 780 | rtc::Event frame_destroyed_event; |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 781 | // The encoder will cache up to one frame for a short duration. Adding two |
| 782 | // frames means that the first frame will be dropped and the second frame will |
| 783 | // be sent when the encoder is enabled. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 784 | video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 785 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 786 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 787 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 788 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 789 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 790 | // The pending frame should be received. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 791 | WaitForEncodedFrame(2); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 792 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
| 793 | |
| 794 | WaitForEncodedFrame(3); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 795 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 796 | } |
| 797 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 798 | TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) { |
| 799 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 800 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 801 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 802 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 803 | video_stream_encoder_->OnBitrateUpdated(0, 0, 0); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 804 | // The encoder will cache up to one frame for a short duration. Adding two |
| 805 | // frames means that the first frame will be dropped and the second frame will |
| 806 | // be sent when the encoder is resumed. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 807 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 808 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 809 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 810 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 811 | WaitForEncodedFrame(3); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 812 | video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr)); |
| 813 | WaitForEncodedFrame(4); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 814 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 815 | } |
| 816 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 817 | TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) { |
| 818 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 819 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 820 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 821 | |
| 822 | // This frame will be dropped since it has the same ntp timestamp. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 823 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 824 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 825 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 826 | WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 827 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 828 | } |
| 829 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 830 | TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) { |
| 831 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 832 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 833 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 834 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 835 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 836 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 837 | sink_.SetExpectNoFrames(); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 838 | rtc::Event frame_destroyed_event; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 839 | video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event)); |
| 840 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 841 | } |
| 842 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 843 | TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) { |
| 844 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 845 | |
| 846 | fake_encoder_.BlockNextEncode(); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 847 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 848 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 849 | // Here, the encoder thread will be blocked in the TestEncoder waiting for a |
| 850 | // call to ContinueEncode. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 851 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 852 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 853 | fake_encoder_.ContinueEncode(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 854 | WaitForEncodedFrame(3); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 855 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 856 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 857 | } |
| 858 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 859 | TEST_F(VideoStreamEncoderTest, |
| 860 | ConfigureEncoderTriggersOnEncoderConfigurationChanged) { |
| 861 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 862 | EXPECT_EQ(0, sink_.number_of_reconfigurations()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 863 | |
| 864 | // Capture a frame and wait for it to synchronize with the encoder thread. |
Per | f8c5f2b | 2016-09-23 16:24:55 +0200 | [diff] [blame] | 865 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 866 | WaitForEncodedFrame(1); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 867 | // The encoder will have been configured once when the first frame is |
| 868 | // received. |
| 869 | EXPECT_EQ(1, sink_.number_of_reconfigurations()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 870 | |
| 871 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 872 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 873 | video_encoder_config.min_transmit_bitrate_bps = 9999; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 874 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 875 | kMaxPayloadLength); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 876 | |
| 877 | // Capture a frame and wait for it to synchronize with the encoder thread. |
Per | f8c5f2b | 2016-09-23 16:24:55 +0200 | [diff] [blame] | 878 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 879 | WaitForEncodedFrame(2); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 880 | EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
perkj | 3b703ed | 2016-09-29 23:25:40 -0700 | [diff] [blame] | 881 | EXPECT_EQ(9999, sink_.last_min_transmit_bitrate()); |
perkj | 26105b4 | 2016-09-29 22:39:10 -0700 | [diff] [blame] | 882 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 883 | video_stream_encoder_->Stop(); |
perkj | 26105b4 | 2016-09-29 22:39:10 -0700 | [diff] [blame] | 884 | } |
| 885 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 886 | TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) { |
| 887 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 888 | |
| 889 | // Capture a frame and wait for it to synchronize with the encoder thread. |
| 890 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 891 | WaitForEncodedFrame(1); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 892 | // The encoder will have been configured once. |
| 893 | EXPECT_EQ(1, sink_.number_of_reconfigurations()); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 894 | EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 895 | EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
| 896 | |
| 897 | codec_width_ *= 2; |
| 898 | codec_height_ *= 2; |
| 899 | // Capture a frame with a higher resolution and wait for it to synchronize |
| 900 | // with the encoder thread. |
| 901 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 902 | WaitForEncodedFrame(2); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 903 | EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 904 | EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 905 | EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 906 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 907 | video_stream_encoder_->Stop(); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 908 | } |
| 909 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 910 | TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 911 | EXPECT_TRUE(video_source_.has_sinks()); |
| 912 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 913 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 914 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 915 | EXPECT_FALSE(video_source_.has_sinks()); |
| 916 | EXPECT_TRUE(new_video_source.has_sinks()); |
| 917 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 918 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 919 | } |
| 920 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 921 | TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 922 | EXPECT_FALSE(video_source_.sink_wants().rotation_applied); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 923 | video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 924 | EXPECT_TRUE(video_source_.sink_wants().rotation_applied); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 925 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 928 | TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) { |
| 929 | const int kFramerateFps = 30; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 930 | const int kWidth = 1280; |
| 931 | const int kHeight = 720; |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 932 | |
| 933 | // We rely on the automatic resolution adaptation, but we handle framerate |
| 934 | // adaptation manually by mocking the stats proxy. |
| 935 | video_source_.set_adaptation_enabled(true); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 936 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 937 | // Enable BALANCED preference, no initial limitation. |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 938 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 939 | video_stream_encoder_->SetSource(&video_source_, |
| 940 | webrtc::DegradationPreference::BALANCED); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 941 | VerifyNoLimitation(video_source_.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 942 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 943 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 944 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 945 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 946 | // Adapt down as far as possible. |
| 947 | rtc::VideoSinkWants last_wants; |
| 948 | int64_t t = 1; |
| 949 | int loop_count = 0; |
| 950 | do { |
| 951 | ++loop_count; |
| 952 | last_wants = video_source_.sink_wants(); |
| 953 | |
| 954 | // Simulate the framerate we've been asked to adapt to. |
| 955 | const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps); |
| 956 | const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps; |
| 957 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 958 | mock_stats.input_frame_rate = fps; |
| 959 | stats_proxy_->SetMockStats(mock_stats); |
| 960 | |
| 961 | video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight)); |
| 962 | sink_.WaitForEncodedFrame(t); |
| 963 | t += frame_interval_ms; |
| 964 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 965 | video_stream_encoder_->TriggerCpuOveruse(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 966 | VerifyBalancedModeFpsRange( |
| 967 | video_source_.sink_wants(), |
| 968 | *video_source_.last_sent_width() * *video_source_.last_sent_height()); |
| 969 | } while (video_source_.sink_wants().max_pixel_count < |
| 970 | last_wants.max_pixel_count || |
| 971 | video_source_.sink_wants().max_framerate_fps < |
| 972 | last_wants.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 973 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 974 | // Verify that we've adapted all the way down. |
| 975 | stats_proxy_->ResetMockStats(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 976 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 977 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 978 | EXPECT_EQ(loop_count - 1, |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 979 | stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 980 | EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() * |
| 981 | *video_source_.last_sent_height()); |
| 982 | EXPECT_EQ(kMinBalancedFramerateFps, |
| 983 | video_source_.sink_wants().max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 984 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 985 | // Adapt back up the same number of times we adapted down. |
| 986 | for (int i = 0; i < loop_count - 1; ++i) { |
| 987 | last_wants = video_source_.sink_wants(); |
| 988 | |
| 989 | // Simulate the framerate we've been asked to adapt to. |
| 990 | const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps); |
| 991 | const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps; |
| 992 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 993 | mock_stats.input_frame_rate = fps; |
| 994 | stats_proxy_->SetMockStats(mock_stats); |
| 995 | |
| 996 | video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight)); |
| 997 | sink_.WaitForEncodedFrame(t); |
| 998 | t += frame_interval_ms; |
| 999 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1000 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1001 | VerifyBalancedModeFpsRange( |
| 1002 | video_source_.sink_wants(), |
| 1003 | *video_source_.last_sent_width() * *video_source_.last_sent_height()); |
| 1004 | EXPECT_TRUE(video_source_.sink_wants().max_pixel_count > |
| 1005 | last_wants.max_pixel_count || |
| 1006 | video_source_.sink_wants().max_framerate_fps > |
| 1007 | last_wants.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1010 | VerifyFpsMaxResolutionMax(video_source_.sink_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1011 | stats_proxy_->ResetMockStats(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1012 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1013 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1014 | EXPECT_EQ((loop_count - 1) * 2, |
| 1015 | stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1016 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1017 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1018 | } |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1019 | TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) { |
| 1020 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1021 | VerifyNoLimitation(video_source_.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1022 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1023 | const int kFrameWidth = 1280; |
| 1024 | const int kFrameHeight = 720; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1025 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1026 | int64_t frame_timestamp = 1; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1027 | |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1028 | video_source_.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1029 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1030 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1031 | frame_timestamp += kFrameIntervalMs; |
| 1032 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1033 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1034 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1035 | video_source_.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1036 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1037 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1038 | frame_timestamp += kFrameIntervalMs; |
sprang | 3ea3c77 | 2017-03-30 07:23:48 -0700 | [diff] [blame] | 1039 | |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 1040 | // Default degradation preference is maintain-framerate, so will lower max |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1041 | // wanted resolution. |
| 1042 | EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); |
| 1043 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, |
| 1044 | kFrameWidth * kFrameHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1045 | EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1046 | |
| 1047 | // Set new source, switch to maintain-resolution. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1048 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1049 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1050 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1051 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1052 | // Initially no degradation registered. |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1053 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1054 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1055 | // Force an input frame rate to be available, or the adaptation call won't |
| 1056 | // know what framerate to adapt form. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1057 | const int kInputFps = 30; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1058 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1059 | stats.input_frame_rate = kInputFps; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1060 | stats_proxy_->SetMockStats(stats); |
| 1061 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1062 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1063 | new_video_source.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1064 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1065 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1066 | frame_timestamp += kFrameIntervalMs; |
| 1067 | |
| 1068 | // Some framerate constraint should be set. |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1069 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1070 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1071 | new_video_source.sink_wants().max_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1072 | EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1073 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1074 | // Turn off degradation completely. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1075 | video_stream_encoder_->SetSource(&new_video_source, |
| 1076 | webrtc::DegradationPreference::DISABLED); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1077 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1078 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1079 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1080 | new_video_source.IncomingCapturedFrame( |
| 1081 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1082 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1083 | frame_timestamp += kFrameIntervalMs; |
| 1084 | |
| 1085 | // Still no degradation. |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1086 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1087 | |
| 1088 | // Calling SetSource with resolution scaling enabled apply the old SinkWants. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1089 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1090 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1091 | EXPECT_LT(new_video_source.sink_wants().max_pixel_count, |
| 1092 | kFrameWidth * kFrameHeight); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1093 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1094 | EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1095 | |
| 1096 | // Calling SetSource with framerate scaling enabled apply the old SinkWants. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1097 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1098 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1099 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
| 1100 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1101 | new_video_source.sink_wants().max_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1102 | EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1103 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1104 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1107 | TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) { |
| 1108 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1109 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1110 | const int kWidth = 1280; |
| 1111 | const int kHeight = 720; |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1112 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1113 | WaitForEncodedFrame(1); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1114 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1115 | EXPECT_FALSE(stats.bw_limited_resolution); |
| 1116 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
| 1117 | |
| 1118 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1119 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1120 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1121 | WaitForEncodedFrame(2); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1122 | |
| 1123 | stats = stats_proxy_->GetStats(); |
| 1124 | EXPECT_TRUE(stats.bw_limited_resolution); |
| 1125 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
| 1126 | |
| 1127 | // Trigger adapt up. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1128 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1129 | video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1130 | WaitForEncodedFrame(3); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1131 | |
| 1132 | stats = stats_proxy_->GetStats(); |
| 1133 | EXPECT_FALSE(stats.bw_limited_resolution); |
| 1134 | EXPECT_EQ(2, stats.number_of_quality_adapt_changes); |
| 1135 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1136 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1137 | video_stream_encoder_->Stop(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1140 | TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) { |
| 1141 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1142 | |
| 1143 | const int kWidth = 1280; |
| 1144 | const int kHeight = 720; |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1145 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1146 | WaitForEncodedFrame(1); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1147 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1148 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1149 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1150 | |
| 1151 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1152 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1153 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1154 | WaitForEncodedFrame(2); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1155 | |
| 1156 | stats = stats_proxy_->GetStats(); |
| 1157 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1158 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1159 | |
| 1160 | // Trigger CPU normal use. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1161 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1162 | video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1163 | WaitForEncodedFrame(3); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1164 | |
| 1165 | stats = stats_proxy_->GetStats(); |
| 1166 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1167 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1168 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1169 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1170 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1173 | TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) { |
| 1174 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1175 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1176 | const int kWidth = 1280; |
| 1177 | const int kHeight = 720; |
| 1178 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1179 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1180 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1181 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1182 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1183 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1184 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1185 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1186 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1187 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1188 | WaitForEncodedFrame(2); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1189 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1190 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1191 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1192 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1193 | |
| 1194 | // Set new source with adaptation still enabled. |
| 1195 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1196 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1197 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1198 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1199 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1200 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1201 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1202 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1203 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1204 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1205 | |
| 1206 | // Set adaptation disabled. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1207 | video_stream_encoder_->SetSource(&new_video_source, |
| 1208 | webrtc::DegradationPreference::DISABLED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1209 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1210 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1211 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1212 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1213 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1214 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1215 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1216 | |
| 1217 | // Set adaptation back to enabled. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1218 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1219 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1220 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1221 | new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1222 | WaitForEncodedFrame(5); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1223 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1224 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1225 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1226 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1227 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1228 | // Trigger CPU normal use. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1229 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1230 | new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1231 | WaitForEncodedFrame(6); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1232 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1233 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1234 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1235 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1236 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1237 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1238 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1241 | TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) { |
| 1242 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1243 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1244 | const int kWidth = 1280; |
| 1245 | const int kHeight = 720; |
| 1246 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1247 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1248 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1249 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1250 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1251 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1252 | |
| 1253 | // Set new source with adaptation still enabled. |
| 1254 | test::FrameForwarder new_video_source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1255 | video_stream_encoder_->SetSource(&new_video_source, |
| 1256 | webrtc::DegradationPreference::BALANCED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1257 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1258 | new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1259 | WaitForEncodedFrame(2); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1260 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1261 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1262 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1263 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1264 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1265 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1266 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1267 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1268 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1269 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1270 | EXPECT_TRUE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1271 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1272 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1273 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1274 | // Set new source with adaptation still enabled. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1275 | video_stream_encoder_->SetSource(&new_video_source, |
| 1276 | webrtc::DegradationPreference::BALANCED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1277 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1278 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1279 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1280 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1281 | EXPECT_TRUE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1282 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1283 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1284 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1285 | // Disable resolution scaling. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1286 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1287 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1288 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1289 | new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1290 | WaitForEncodedFrame(5); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1291 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1292 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1293 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1294 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
| 1295 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1296 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1297 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1300 | TEST_F(VideoStreamEncoderTest, |
| 1301 | QualityAdaptationStatsAreResetWhenScalerIsDisabled) { |
| 1302 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1303 | |
| 1304 | const int kWidth = 1280; |
| 1305 | const int kHeight = 720; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1306 | int64_t timestamp_ms = kFrameIntervalMs; |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1307 | video_source_.set_adaptation_enabled(true); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1308 | video_source_.IncomingCapturedFrame( |
| 1309 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1310 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1311 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1312 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1313 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1314 | |
| 1315 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1316 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1317 | timestamp_ms += kFrameIntervalMs; |
| 1318 | video_source_.IncomingCapturedFrame( |
| 1319 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1320 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1321 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1322 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1323 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1324 | |
| 1325 | // Trigger overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1326 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1327 | timestamp_ms += kFrameIntervalMs; |
| 1328 | video_source_.IncomingCapturedFrame( |
| 1329 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1330 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1331 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1332 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1333 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1334 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 1335 | // Leave source unchanged, but disable quality scaler. |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1336 | fake_encoder_.SetQualityScaling(false); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 1337 | |
| 1338 | VideoEncoderConfig video_encoder_config; |
| 1339 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
| 1340 | // Make format different, to force recreation of encoder. |
| 1341 | video_encoder_config.video_format.parameters["foo"] = "foo"; |
| 1342 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 1343 | kMaxPayloadLength); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1344 | timestamp_ms += kFrameIntervalMs; |
| 1345 | video_source_.IncomingCapturedFrame( |
| 1346 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1347 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1348 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1349 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1350 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1351 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1352 | video_stream_encoder_->Stop(); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1355 | TEST_F(VideoStreamEncoderTest, |
| 1356 | StatsTracksCpuAdaptationStatsWhenSwitchingSource) { |
| 1357 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1358 | |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 1359 | const int kWidth = 1280; |
| 1360 | const int kHeight = 720; |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1361 | int sequence = 1; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1362 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1363 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1364 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1365 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1366 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1367 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1368 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1369 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1370 | // Trigger CPU overuse, should now adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1371 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1372 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1373 | WaitForEncodedFrame(sequence++); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1374 | stats = stats_proxy_->GetStats(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1375 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1376 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1377 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1378 | |
| 1379 | // Set new source with adaptation still enabled. |
| 1380 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1381 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1382 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1383 | |
| 1384 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1385 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1386 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1387 | stats = stats_proxy_->GetStats(); |
| 1388 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1389 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1390 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1391 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1392 | // Set cpu adaptation by frame dropping. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1393 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1394 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1395 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1396 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1397 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1398 | stats = stats_proxy_->GetStats(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1399 | // Not adapted at first. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1400 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1401 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1402 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1403 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1404 | // Force an input frame rate to be available, or the adaptation call won't |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1405 | // know what framerate to adapt from. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1406 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 1407 | mock_stats.input_frame_rate = 30; |
| 1408 | stats_proxy_->SetMockStats(mock_stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1409 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1410 | stats_proxy_->ResetMockStats(); |
| 1411 | |
| 1412 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1413 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1414 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1415 | |
| 1416 | // Framerate now adapted. |
| 1417 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1418 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1419 | EXPECT_TRUE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1420 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1421 | |
| 1422 | // Disable CPU adaptation. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1423 | video_stream_encoder_->SetSource(&new_video_source, |
| 1424 | webrtc::DegradationPreference::DISABLED); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1425 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1426 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1427 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1428 | |
| 1429 | stats = stats_proxy_->GetStats(); |
| 1430 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1431 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1432 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1433 | |
| 1434 | // Try to trigger overuse. Should not succeed. |
| 1435 | stats_proxy_->SetMockStats(mock_stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1436 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1437 | stats_proxy_->ResetMockStats(); |
| 1438 | |
| 1439 | stats = stats_proxy_->GetStats(); |
| 1440 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1441 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1442 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1443 | |
| 1444 | // Switch back the source with resolution adaptation enabled. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1445 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1446 | &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1447 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1448 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1449 | stats = stats_proxy_->GetStats(); |
| 1450 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1451 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1452 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1453 | |
| 1454 | // Trigger CPU normal usage. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1455 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1456 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1457 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1458 | stats = stats_proxy_->GetStats(); |
| 1459 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1460 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1461 | EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); |
| 1462 | |
| 1463 | // Back to the source with adaptation off, set it back to maintain-resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1464 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1465 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1466 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1467 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1468 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1469 | stats = stats_proxy_->GetStats(); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1470 | // Disabled, since we previously switched the source to disabled. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1471 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1472 | EXPECT_TRUE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1473 | EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); |
| 1474 | |
| 1475 | // Trigger CPU normal usage. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1476 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1477 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1478 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1479 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1480 | stats = stats_proxy_->GetStats(); |
| 1481 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1482 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1483 | EXPECT_EQ(4, stats.number_of_cpu_adapt_changes); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1484 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1485 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1486 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1489 | TEST_F(VideoStreamEncoderTest, |
| 1490 | ScalingUpAndDownDoesNothingWithMaintainResolution) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1491 | const int kWidth = 1280; |
| 1492 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1493 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1494 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1495 | // Expect no scaling to begin with. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1496 | VerifyNoLimitation(video_source_.sink_wants()); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1497 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1498 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1499 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1500 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1501 | // Trigger scale down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1502 | video_stream_encoder_->TriggerQualityLow(); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1503 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1504 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1505 | WaitForEncodedFrame(2); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1506 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1507 | // Expect a scale down. |
| 1508 | EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1509 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1510 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1511 | // Set resolution scaling disabled. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1512 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1513 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1514 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1515 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1516 | // Trigger scale down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1517 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1518 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1519 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1520 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1521 | // Expect no scaling. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1522 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1523 | new_video_source.sink_wants().max_pixel_count); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1524 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1525 | // Trigger scale up. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1526 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1527 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1528 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1529 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1530 | // Expect nothing to change, still no scaling. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1531 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1532 | new_video_source.sink_wants().max_pixel_count); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1533 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1534 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1537 | TEST_F(VideoStreamEncoderTest, |
| 1538 | SkipsSameAdaptDownRequest_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1539 | const int kWidth = 1280; |
| 1540 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1541 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1542 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1543 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1544 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1545 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1546 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1547 | |
| 1548 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1549 | WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1550 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1551 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1552 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1553 | |
| 1554 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1555 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1556 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1557 | const int kLastMaxPixelCount = source.sink_wants().max_pixel_count; |
| 1558 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1559 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1560 | |
| 1561 | // Trigger adapt down for same input resolution, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1562 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1563 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1564 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1565 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1566 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1567 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1570 | TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1571 | const int kWidth = 1280; |
| 1572 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1573 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1574 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1575 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1576 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1577 | video_stream_encoder_->SetSource(&source, |
| 1578 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1579 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1580 | sink_.WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1581 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1582 | |
| 1583 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1584 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1585 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 1586 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1587 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1588 | const int kLastMaxPixelCount = source.sink_wants().max_pixel_count; |
| 1589 | |
| 1590 | // Trigger adapt down for same input resolution, expect no change. |
| 1591 | source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 1592 | sink_.WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1593 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1594 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1595 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1596 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1597 | |
| 1598 | // Trigger adapt down for larger input resolution, expect no change. |
| 1599 | source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1)); |
| 1600 | sink_.WaitForEncodedFrame(3); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1601 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1602 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1603 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1604 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1605 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1606 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1609 | TEST_F(VideoStreamEncoderTest, |
| 1610 | NoChangeForInitialNormalUsage_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1611 | const int kWidth = 1280; |
| 1612 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1613 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1614 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1615 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1616 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1617 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1618 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1619 | |
| 1620 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1621 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1622 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1623 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1624 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1625 | |
| 1626 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1627 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1628 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1629 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1630 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1631 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1632 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1635 | TEST_F(VideoStreamEncoderTest, |
| 1636 | NoChangeForInitialNormalUsage_MaintainResolutionMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1637 | const int kWidth = 1280; |
| 1638 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1639 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1640 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1641 | // Enable MAINTAIN_RESOLUTION preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1642 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1643 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1644 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1645 | |
| 1646 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1647 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1648 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1649 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1650 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1651 | |
| 1652 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1653 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1654 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1655 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1656 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1657 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1658 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1661 | TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1662 | const int kWidth = 1280; |
| 1663 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1664 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1665 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1666 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1667 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1668 | video_stream_encoder_->SetSource(&source, |
| 1669 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1670 | |
| 1671 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1672 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1673 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1674 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1675 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1676 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1677 | |
| 1678 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1679 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1680 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1681 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1682 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1683 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1684 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1685 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1688 | TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1689 | const int kWidth = 1280; |
| 1690 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1691 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1692 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1693 | // Enable DISABLED preference, no initial limitation. |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1694 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1695 | video_stream_encoder_->SetSource(&source, |
| 1696 | webrtc::DegradationPreference::DISABLED); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1697 | |
| 1698 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1699 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1700 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1701 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1702 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1703 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1704 | |
| 1705 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1706 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1707 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1708 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1709 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1710 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1711 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1712 | video_stream_encoder_->Stop(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1715 | TEST_F(VideoStreamEncoderTest, |
| 1716 | AdaptsResolutionForLowQuality_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1717 | const int kWidth = 1280; |
| 1718 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1719 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1720 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1721 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1722 | AdaptingFrameForwarder source; |
| 1723 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1724 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1725 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1726 | |
| 1727 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1728 | WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1729 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1730 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1731 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1732 | |
| 1733 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1734 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1735 | source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1736 | WaitForEncodedFrame(2); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1737 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1738 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1739 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1740 | |
| 1741 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1742 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1743 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1744 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1745 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1746 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1747 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1748 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1751 | TEST_F(VideoStreamEncoderTest, |
| 1752 | AdaptsFramerateForLowQuality_MaintainResolutionMode) { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1753 | const int kWidth = 1280; |
| 1754 | const int kHeight = 720; |
| 1755 | const int kInputFps = 30; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1756 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1757 | |
| 1758 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1759 | stats.input_frame_rate = kInputFps; |
| 1760 | stats_proxy_->SetMockStats(stats); |
| 1761 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1762 | // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE). |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1763 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1764 | sink_.WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1765 | VerifyFpsMaxResolutionMax(video_source_.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1766 | |
| 1767 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1768 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1769 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 1770 | sink_.WaitForEncodedFrame(2); |
| 1771 | VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight); |
| 1772 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1773 | // Enable MAINTAIN_RESOLUTION preference. |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1774 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1775 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1776 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1777 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1778 | |
| 1779 | // Trigger adapt down, expect reduced framerate. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1780 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1781 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
| 1782 | sink_.WaitForEncodedFrame(3); |
| 1783 | VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps); |
| 1784 | |
| 1785 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1786 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1787 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1788 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1789 | video_stream_encoder_->Stop(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1792 | TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) { |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1793 | const int kWidth = 1280; |
| 1794 | const int kHeight = 720; |
| 1795 | const size_t kNumFrames = 10; |
| 1796 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1797 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1798 | |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1799 | // Enable adapter, expected input resolutions when downscaling: |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 1800 | // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame) |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1801 | video_source_.set_adaptation_enabled(true); |
| 1802 | |
| 1803 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1804 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1805 | |
| 1806 | int downscales = 0; |
| 1807 | for (size_t i = 1; i <= kNumFrames; i++) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1808 | video_source_.IncomingCapturedFrame( |
| 1809 | CreateFrame(i * kFrameIntervalMs, kWidth, kHeight)); |
| 1810 | WaitForEncodedFrame(i * kFrameIntervalMs); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1811 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1812 | // Trigger scale down. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1813 | rtc::VideoSinkWants last_wants = video_source_.sink_wants(); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1814 | video_stream_encoder_->TriggerQualityLow(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1815 | EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1816 | |
| 1817 | if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count) |
| 1818 | ++downscales; |
| 1819 | |
| 1820 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1821 | EXPECT_EQ(downscales, |
| 1822 | stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1823 | EXPECT_GT(downscales, 0); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1824 | } |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1825 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1828 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1829 | AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) { |
| 1830 | const int kWidth = 1280; |
| 1831 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1832 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1833 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1834 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1835 | AdaptingFrameForwarder source; |
| 1836 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1837 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1838 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1839 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1840 | int64_t timestamp_ms = kFrameIntervalMs; |
| 1841 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1842 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1843 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1844 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1845 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1846 | |
| 1847 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1848 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1849 | timestamp_ms += kFrameIntervalMs; |
| 1850 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1851 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1852 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1853 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1854 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1855 | |
| 1856 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1857 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1858 | timestamp_ms += kFrameIntervalMs; |
| 1859 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1860 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1861 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1862 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1863 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1864 | |
| 1865 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1866 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1867 | timestamp_ms += kFrameIntervalMs; |
| 1868 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1869 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1870 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1871 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1872 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1873 | |
| 1874 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1875 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1876 | timestamp_ms += kFrameIntervalMs; |
| 1877 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1878 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1879 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1880 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1881 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1882 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1883 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1886 | TEST_F(VideoStreamEncoderTest, |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1887 | AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) { |
| 1888 | const int kWidth = 1280; |
| 1889 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1890 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1891 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1892 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1893 | AdaptingFrameForwarder source; |
| 1894 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1895 | video_stream_encoder_->SetSource(&source, |
| 1896 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1897 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1898 | int64_t timestamp_ms = kFrameIntervalMs; |
| 1899 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1900 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1901 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1902 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1903 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1904 | |
| 1905 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1906 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1907 | timestamp_ms += kFrameIntervalMs; |
| 1908 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1909 | sink_.WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1910 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 1911 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1912 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1913 | |
| 1914 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1915 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1916 | timestamp_ms += kFrameIntervalMs; |
| 1917 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1918 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1919 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1920 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1921 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1922 | |
| 1923 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1924 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1925 | timestamp_ms += kFrameIntervalMs; |
| 1926 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1927 | sink_.WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1928 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 1929 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1930 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1931 | |
| 1932 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1933 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1934 | timestamp_ms += kFrameIntervalMs; |
| 1935 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1936 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1937 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1938 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1939 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1940 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1941 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1944 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1945 | AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) { |
| 1946 | const int kWidth = 1280; |
| 1947 | const int kHeight = 720; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1948 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1949 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1950 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1951 | AdaptingFrameForwarder source; |
| 1952 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1953 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1954 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1955 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1956 | int64_t timestamp_ms = kFrameIntervalMs; |
| 1957 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1958 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1959 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1960 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1961 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1962 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1963 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1964 | |
| 1965 | // Trigger cpu adapt down, expect scaled down resolution (960x540). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1966 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1967 | timestamp_ms += kFrameIntervalMs; |
| 1968 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1969 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1970 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1971 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1972 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1973 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1974 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1975 | |
| 1976 | // Trigger cpu adapt down, expect scaled down resolution (640x360). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1977 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1978 | timestamp_ms += kFrameIntervalMs; |
| 1979 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1980 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1981 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1982 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1983 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1984 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1985 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1986 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1987 | // Trigger cpu adapt down, expect scaled down resolution (480x270). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1988 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1989 | timestamp_ms += kFrameIntervalMs; |
| 1990 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1991 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1992 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1993 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1994 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1995 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 1996 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1997 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1998 | // Trigger quality adapt down, expect scaled down resolution (320x180). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1999 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2000 | timestamp_ms += kFrameIntervalMs; |
| 2001 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2002 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2003 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2004 | rtc::VideoSinkWants last_wants = source.sink_wants(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2005 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2006 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2007 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2008 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2009 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2010 | // Trigger quality adapt down, expect no change (min resolution reached). |
| 2011 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2012 | timestamp_ms += kFrameIntervalMs; |
| 2013 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2014 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2015 | VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants); |
| 2016 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2017 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2018 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2019 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2020 | |
| 2021 | // Trigger cpu adapt up, expect upscaled resolution (480x270). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2022 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2023 | timestamp_ms += kFrameIntervalMs; |
| 2024 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2025 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2026 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2027 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2028 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2029 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2030 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2031 | |
| 2032 | // Trigger cpu adapt up, expect upscaled resolution (640x360). |
| 2033 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2034 | timestamp_ms += kFrameIntervalMs; |
| 2035 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2036 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2037 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 2038 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2039 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2040 | EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2041 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2042 | |
| 2043 | // Trigger cpu adapt up, expect upscaled resolution (960x540). |
| 2044 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2045 | timestamp_ms += kFrameIntervalMs; |
| 2046 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2047 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2048 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2049 | last_wants = source.sink_wants(); |
| 2050 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2051 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2052 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2053 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2054 | |
| 2055 | // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2056 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2057 | timestamp_ms += kFrameIntervalMs; |
| 2058 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2059 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2060 | VerifyFpsEqResolutionEq(source.sink_wants(), last_wants); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2061 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2062 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2063 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2064 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2065 | |
| 2066 | // Trigger quality adapt up, expect no restriction (1280x720). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2067 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2068 | timestamp_ms += kFrameIntervalMs; |
| 2069 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2070 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2071 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2072 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2073 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2074 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2075 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2076 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2077 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2078 | video_stream_encoder_->Stop(); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2081 | TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2082 | const int kWidth = 640; |
| 2083 | const int kHeight = 360; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2084 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2085 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2086 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2087 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2088 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2089 | WaitForEncodedFrame(i); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2092 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2093 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2094 | video_source_.IncomingCapturedFrame(CreateFrame( |
| 2095 | SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2096 | WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2099 | video_stream_encoder_->Stop(); |
| 2100 | video_stream_encoder_.reset(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2101 | stats_proxy_.reset(); |
sprang | f8ee65e | 2017-02-28 08:49:33 -0800 | [diff] [blame] | 2102 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2103 | EXPECT_EQ(1, |
| 2104 | metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); |
| 2105 | EXPECT_EQ( |
| 2106 | 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50)); |
| 2107 | } |
| 2108 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2109 | TEST_F(VideoStreamEncoderTest, |
| 2110 | CpuLimitedHistogramIsNotReportedForDisabledDegradation) { |
| 2111 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2112 | const int kWidth = 640; |
| 2113 | const int kHeight = 360; |
| 2114 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2115 | video_stream_encoder_->SetSource(&video_source_, |
| 2116 | webrtc::DegradationPreference::DISABLED); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2117 | |
| 2118 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
| 2119 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2120 | WaitForEncodedFrame(i); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2123 | video_stream_encoder_->Stop(); |
| 2124 | video_stream_encoder_.reset(); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2125 | stats_proxy_.reset(); |
| 2126 | |
| 2127 | EXPECT_EQ(0, |
| 2128 | metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); |
| 2129 | } |
| 2130 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2131 | TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2132 | MockBitrateObserver bitrate_observer; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 2133 | video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2134 | |
| 2135 | const int kDefaultFps = 30; |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 2136 | const VideoBitrateAllocation expected_bitrate = |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2137 | DefaultVideoBitrateAllocator(fake_encoder_.codec_config()) |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2138 | .GetAllocation(kLowTargetBitrateBps, kDefaultFps); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2139 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2140 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 2141 | .Times(1); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2142 | video_stream_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2143 | |
| 2144 | const int64_t kStartTimeMs = 1; |
| 2145 | video_source_.IncomingCapturedFrame( |
| 2146 | CreateFrame(kStartTimeMs, codec_width_, codec_height_)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2147 | WaitForEncodedFrame(kStartTimeMs); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2148 | |
| 2149 | // Not called on second frame. |
| 2150 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
| 2151 | .Times(0); |
| 2152 | video_source_.IncomingCapturedFrame( |
| 2153 | CreateFrame(kStartTimeMs + 1, codec_width_, codec_height_)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2154 | WaitForEncodedFrame(kStartTimeMs + 1); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2155 | |
| 2156 | // Called after a process interval. |
| 2157 | const int64_t kProcessIntervalMs = |
| 2158 | vcm::VCMProcessTimer::kDefaultProcessIntervalMs; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2159 | fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerMillisec * |
| 2160 | (kProcessIntervalMs + (1000 / kDefaultFps))); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2161 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
| 2162 | .Times(1); |
| 2163 | video_source_.IncomingCapturedFrame(CreateFrame( |
| 2164 | kStartTimeMs + kProcessIntervalMs, codec_width_, codec_height_)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2165 | WaitForEncodedFrame(kStartTimeMs + kProcessIntervalMs); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2166 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2167 | video_stream_encoder_->Stop(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2170 | TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) { |
| 2171 | const int kFrameWidth = 1280; |
| 2172 | const int kFrameHeight = 720; |
| 2173 | const int kFramerate = 24; |
| 2174 | |
| 2175 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 2176 | test::FrameForwarder source; |
| 2177 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2178 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2179 | |
| 2180 | // Insert a single frame, triggering initial configuration. |
| 2181 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2182 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2183 | |
| 2184 | EXPECT_EQ( |
| 2185 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2186 | kDefaultFramerate); |
| 2187 | |
| 2188 | // Trigger reconfigure encoder (without resetting the entire instance). |
| 2189 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2190 | video_encoder_config.codec_type = kVideoCodecVP8; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2191 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2192 | video_encoder_config.number_of_streams = 1; |
| 2193 | video_encoder_config.video_stream_factory = |
| 2194 | new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate); |
| 2195 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2196 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2197 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2198 | |
| 2199 | // Detector should be updated with fps limit from codec config. |
| 2200 | EXPECT_EQ( |
| 2201 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2202 | kFramerate); |
| 2203 | |
| 2204 | // Trigger overuse, max framerate should be reduced. |
| 2205 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2206 | stats.input_frame_rate = kFramerate; |
| 2207 | stats_proxy_->SetMockStats(stats); |
| 2208 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2209 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2210 | int adapted_framerate = |
| 2211 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
| 2212 | EXPECT_LT(adapted_framerate, kFramerate); |
| 2213 | |
| 2214 | // Trigger underuse, max framerate should go back to codec configured fps. |
| 2215 | // Set extra low fps, to make sure it's actually reset, not just incremented. |
| 2216 | stats = stats_proxy_->GetStats(); |
| 2217 | stats.input_frame_rate = adapted_framerate / 2; |
| 2218 | stats_proxy_->SetMockStats(stats); |
| 2219 | video_stream_encoder_->TriggerCpuNormalUsage(); |
| 2220 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2221 | EXPECT_EQ( |
| 2222 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2223 | kFramerate); |
| 2224 | |
| 2225 | video_stream_encoder_->Stop(); |
| 2226 | } |
| 2227 | |
| 2228 | TEST_F(VideoStreamEncoderTest, |
| 2229 | OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) { |
| 2230 | const int kFrameWidth = 1280; |
| 2231 | const int kFrameHeight = 720; |
| 2232 | const int kLowFramerate = 15; |
| 2233 | const int kHighFramerate = 25; |
| 2234 | |
| 2235 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 2236 | test::FrameForwarder source; |
| 2237 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2238 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2239 | |
| 2240 | // Trigger initial configuration. |
| 2241 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2242 | video_encoder_config.codec_type = kVideoCodecVP8; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2243 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2244 | video_encoder_config.number_of_streams = 1; |
| 2245 | video_encoder_config.video_stream_factory = |
| 2246 | new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate); |
| 2247 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2248 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2249 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2250 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2251 | |
| 2252 | EXPECT_EQ( |
| 2253 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2254 | kLowFramerate); |
| 2255 | |
| 2256 | // Trigger overuse, max framerate should be reduced. |
| 2257 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2258 | stats.input_frame_rate = kLowFramerate; |
| 2259 | stats_proxy_->SetMockStats(stats); |
| 2260 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2261 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2262 | int adapted_framerate = |
| 2263 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
| 2264 | EXPECT_LT(adapted_framerate, kLowFramerate); |
| 2265 | |
| 2266 | // Reconfigure the encoder with a new (higher max framerate), max fps should |
| 2267 | // still respect the adaptation. |
| 2268 | video_encoder_config.video_stream_factory = |
| 2269 | new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate); |
| 2270 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2271 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2272 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2273 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2274 | |
| 2275 | EXPECT_EQ( |
| 2276 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2277 | adapted_framerate); |
| 2278 | |
| 2279 | // Trigger underuse, max framerate should go back to codec configured fps. |
| 2280 | stats = stats_proxy_->GetStats(); |
| 2281 | stats.input_frame_rate = adapted_framerate; |
| 2282 | stats_proxy_->SetMockStats(stats); |
| 2283 | video_stream_encoder_->TriggerCpuNormalUsage(); |
| 2284 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2285 | EXPECT_EQ( |
| 2286 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2287 | kHighFramerate); |
| 2288 | |
| 2289 | video_stream_encoder_->Stop(); |
| 2290 | } |
| 2291 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2292 | TEST_F(VideoStreamEncoderTest, |
| 2293 | OveruseDetectorUpdatedOnDegradationPreferenceChange) { |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2294 | const int kFrameWidth = 1280; |
| 2295 | const int kFrameHeight = 720; |
| 2296 | const int kFramerate = 24; |
| 2297 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2298 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2299 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2300 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2301 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2302 | |
| 2303 | // Trigger initial configuration. |
| 2304 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2305 | video_encoder_config.codec_type = kVideoCodecVP8; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2306 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2307 | video_encoder_config.number_of_streams = 1; |
| 2308 | video_encoder_config.video_stream_factory = |
| 2309 | new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate); |
| 2310 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2311 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2312 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2313 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2314 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2315 | EXPECT_EQ( |
| 2316 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2317 | kFramerate); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2318 | |
| 2319 | // Trigger overuse, max framerate should be reduced. |
| 2320 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2321 | stats.input_frame_rate = kFramerate; |
| 2322 | stats_proxy_->SetMockStats(stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2323 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2324 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2325 | int adapted_framerate = |
| 2326 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2327 | EXPECT_LT(adapted_framerate, kFramerate); |
| 2328 | |
| 2329 | // Change degradation preference to not enable framerate scaling. Target |
| 2330 | // framerate should be changed to codec defined limit. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2331 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2332 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2333 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2334 | EXPECT_EQ( |
| 2335 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2336 | kFramerate); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2337 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2338 | video_stream_encoder_->Stop(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2341 | TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2342 | const int kTooLowBitrateForFrameSizeBps = 10000; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2343 | video_stream_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2344 | const int kWidth = 640; |
| 2345 | const int kHeight = 360; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2346 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2347 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2348 | |
| 2349 | // Expect to drop this frame, the wait should time out. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2350 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2351 | |
| 2352 | // Expect the sink_wants to specify a scaled frame. |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 2353 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2354 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2355 | int last_pixel_count = video_source_.sink_wants().max_pixel_count; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2356 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2357 | // Next frame is scaled. |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2358 | video_source_.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2359 | CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2360 | |
| 2361 | // Expect to drop this frame, the wait should time out. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2362 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2363 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2364 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2365 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2366 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2367 | } |
| 2368 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2369 | TEST_F(VideoStreamEncoderTest, |
| 2370 | NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2371 | const int kTooLowBitrateForFrameSizeBps = 10000; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2372 | video_stream_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2373 | const int kWidth = 640; |
| 2374 | const int kHeight = 360; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2375 | |
| 2376 | // We expect the n initial frames to get dropped. |
| 2377 | int i; |
| 2378 | for (i = 1; i <= kMaxInitialFramedrop; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2379 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2380 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2381 | } |
| 2382 | // The n+1th frame should not be dropped, even though it's size is too large. |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2383 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2384 | WaitForEncodedFrame(i); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2385 | |
| 2386 | // Expect the sink_wants to specify a scaled frame. |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2387 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2388 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2389 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2390 | } |
| 2391 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2392 | TEST_F(VideoStreamEncoderTest, |
| 2393 | InitialFrameDropOffWithMaintainResolutionPreference) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2394 | const int kWidth = 640; |
| 2395 | const int kHeight = 360; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2396 | video_stream_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2397 | |
| 2398 | // Set degradation preference. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2399 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2400 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2401 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2402 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2403 | // Frame should not be dropped, even if it's too large. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2404 | WaitForEncodedFrame(1); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2405 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2406 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2409 | TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2410 | const int kWidth = 640; |
| 2411 | const int kHeight = 360; |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2412 | fake_encoder_.SetQualityScaling(false); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 2413 | |
| 2414 | VideoEncoderConfig video_encoder_config; |
| 2415 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
| 2416 | // Make format different, to force recreation of encoder. |
| 2417 | video_encoder_config.video_format.parameters["foo"] = "foo"; |
| 2418 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2419 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2420 | video_stream_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2421 | |
kthelgason | b83797b | 2017-02-14 11:57:25 -0800 | [diff] [blame] | 2422 | // Force quality scaler reconfiguration by resetting the source. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2423 | video_stream_encoder_->SetSource(&video_source_, |
| 2424 | webrtc::DegradationPreference::BALANCED); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2425 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2426 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2427 | // Frame should not be dropped, even if it's too large. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2428 | WaitForEncodedFrame(1); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2429 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2430 | video_stream_encoder_->Stop(); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2431 | fake_encoder_.SetQualityScaling(true); |
| 2432 | } |
| 2433 | |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 2434 | TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) { |
| 2435 | webrtc::test::ScopedFieldTrials field_trials( |
| 2436 | "WebRTC-InitialFramedrop/Enabled/"); |
| 2437 | // Reset encoder for field trials to take effect. |
| 2438 | ConfigureEncoder(video_encoder_config_.Copy()); |
| 2439 | const int kTooLowBitrateForFrameSizeBps = 10000; |
| 2440 | const int kWidth = 640; |
| 2441 | const int kHeight = 360; |
| 2442 | |
| 2443 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 2444 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 2445 | // Frame should not be dropped. |
| 2446 | WaitForEncodedFrame(1); |
| 2447 | |
| 2448 | video_stream_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0); |
| 2449 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 2450 | // Expect to drop this frame, the wait should time out. |
| 2451 | ExpectDroppedFrame(); |
| 2452 | |
| 2453 | // Expect the sink_wants to specify a scaled frame. |
| 2454 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
| 2455 | video_stream_encoder_->Stop(); |
| 2456 | } |
| 2457 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2458 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2459 | ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) { |
| 2460 | const int kTooSmallWidth = 10; |
| 2461 | const int kTooSmallHeight = 10; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2462 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2463 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2464 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2465 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2466 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2467 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2468 | VerifyNoLimitation(source.sink_wants()); |
| 2469 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2470 | |
| 2471 | // Trigger adapt down, too small frame, expect no change. |
| 2472 | source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2473 | WaitForEncodedFrame(1); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2474 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2475 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2476 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2477 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2478 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2479 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2482 | TEST_F(VideoStreamEncoderTest, |
| 2483 | ResolutionNotAdaptedForTooSmallFrame_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2484 | const int kTooSmallWidth = 10; |
| 2485 | const int kTooSmallHeight = 10; |
| 2486 | const int kFpsLimit = 7; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2487 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2488 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2489 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2490 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2491 | video_stream_encoder_->SetSource(&source, |
| 2492 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2493 | VerifyNoLimitation(source.sink_wants()); |
| 2494 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2495 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2496 | |
| 2497 | // Trigger adapt down, expect limited framerate. |
| 2498 | source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2499 | WaitForEncodedFrame(1); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2500 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2501 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 2502 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2503 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2504 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2505 | |
| 2506 | // Trigger adapt down, too small frame, expect no change. |
| 2507 | source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2508 | WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2509 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2510 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 2511 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2512 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2513 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2514 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2515 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2518 | TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2519 | fake_encoder_.ForceInitEncodeFailure(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2520 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2521 | ResetEncoder("VP8", 2, 1, 1, false); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2522 | const int kFrameWidth = 1280; |
| 2523 | const int kFrameHeight = 720; |
| 2524 | video_source_.IncomingCapturedFrame( |
| 2525 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2526 | ExpectDroppedFrame(); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2527 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2530 | // TODO(sprang): Extend this with fps throttling and any "balanced" extensions. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2531 | TEST_F(VideoStreamEncoderTest, |
| 2532 | AdaptsResolutionOnOveruse_MaintainFramerateMode) { |
| 2533 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2534 | |
| 2535 | const int kFrameWidth = 1280; |
| 2536 | const int kFrameHeight = 720; |
| 2537 | // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2538 | // requested by |
| 2539 | // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan(). |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2540 | video_source_.set_adaptation_enabled(true); |
| 2541 | |
| 2542 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2543 | CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2544 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2545 | |
| 2546 | // Trigger CPU overuse, downscale by 3/4. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2547 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2548 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2549 | CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2550 | WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2551 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2552 | // Trigger CPU normal use, return to original resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2553 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2554 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2555 | CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2556 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2557 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2558 | video_stream_encoder_->Stop(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2559 | } |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 2560 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2561 | TEST_F(VideoStreamEncoderTest, |
| 2562 | AdaptsFramerateOnOveruse_MaintainResolutionMode) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2563 | const int kFrameWidth = 1280; |
| 2564 | const int kFrameHeight = 720; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2565 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2566 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 2567 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2568 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2569 | video_source_.set_adaptation_enabled(true); |
| 2570 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2571 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2572 | |
| 2573 | video_source_.IncomingCapturedFrame( |
| 2574 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2575 | WaitForEncodedFrame(timestamp_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2576 | |
| 2577 | // Try to trigger overuse. No fps estimate available => no effect. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2578 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2579 | |
| 2580 | // Insert frames for one second to get a stable estimate. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2581 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2582 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2583 | video_source_.IncomingCapturedFrame( |
| 2584 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2585 | WaitForEncodedFrame(timestamp_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | // Trigger CPU overuse, reduce framerate by 2/3. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2589 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2590 | int num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2591 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2592 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2593 | video_source_.IncomingCapturedFrame( |
| 2594 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2595 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2596 | ++num_frames_dropped; |
| 2597 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2598 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2602 | // Add some slack to account for frames dropped by the frame dropper. |
| 2603 | const int kErrorMargin = 1; |
| 2604 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2605 | kErrorMargin); |
| 2606 | |
| 2607 | // Trigger CPU overuse, reduce framerate by 2/3 again. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2608 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2609 | num_frames_dropped = 0; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2610 | for (int i = 0; i <= max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2611 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2612 | video_source_.IncomingCapturedFrame( |
| 2613 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2614 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2615 | ++num_frames_dropped; |
| 2616 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2617 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2618 | } |
| 2619 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2620 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2621 | kErrorMargin); |
| 2622 | |
| 2623 | // Go back up one step. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2624 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2625 | num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2626 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2627 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2628 | video_source_.IncomingCapturedFrame( |
| 2629 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2630 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2631 | ++num_frames_dropped; |
| 2632 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2633 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2636 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2637 | kErrorMargin); |
| 2638 | |
| 2639 | // Go back up to original mode. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2640 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2641 | num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2642 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2643 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2644 | video_source_.IncomingCapturedFrame( |
| 2645 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2646 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2647 | ++num_frames_dropped; |
| 2648 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2649 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin); |
| 2653 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2654 | video_stream_encoder_->Stop(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2657 | TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2658 | const int kFramerateFps = 5; |
| 2659 | const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2660 | const int kFrameWidth = 1280; |
| 2661 | const int kFrameHeight = 720; |
| 2662 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2663 | // Reconfigure encoder with two temporal layers and screensharing, which will |
| 2664 | // disable frame dropping and make testing easier. |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2665 | ResetEncoder("VP8", 1, 2, 1, true); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2666 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2667 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 2668 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2669 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2670 | video_source_.set_adaptation_enabled(true); |
| 2671 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2672 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2673 | |
| 2674 | // Trigger overuse as much as we can. |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2675 | rtc::VideoSinkWants last_wants; |
| 2676 | do { |
| 2677 | last_wants = video_source_.sink_wants(); |
| 2678 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2679 | // Insert frames to get a new fps estimate... |
| 2680 | for (int j = 0; j < kFramerateFps; ++j) { |
| 2681 | video_source_.IncomingCapturedFrame( |
| 2682 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2683 | if (video_source_.last_sent_width()) { |
| 2684 | sink_.WaitForEncodedFrame(timestamp_ms); |
| 2685 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2686 | timestamp_ms += kFrameIntervalMs; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2687 | fake_clock_.AdvanceTimeMicros(kFrameIntervalMs * |
| 2688 | rtc::kNumMicrosecsPerMillisec); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2689 | } |
| 2690 | // ...and then try to adapt again. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2691 | video_stream_encoder_->TriggerCpuOveruse(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2692 | } while (video_source_.sink_wants().max_framerate_fps < |
| 2693 | last_wants.max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2694 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2695 | VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2696 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2697 | video_stream_encoder_->Stop(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2698 | } |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2699 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2700 | TEST_F(VideoStreamEncoderTest, |
| 2701 | AdaptsResolutionAndFramerateForLowQuality_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2702 | const int kWidth = 1280; |
| 2703 | const int kHeight = 720; |
| 2704 | const int64_t kFrameIntervalMs = 150; |
| 2705 | int64_t timestamp_ms = kFrameIntervalMs; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2706 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2707 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2708 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2709 | AdaptingFrameForwarder source; |
| 2710 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2711 | video_stream_encoder_->SetSource(&source, |
| 2712 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2713 | timestamp_ms += kFrameIntervalMs; |
| 2714 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2715 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2716 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2717 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2718 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2719 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2720 | |
| 2721 | // Trigger adapt down, expect scaled down resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2722 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2723 | timestamp_ms += kFrameIntervalMs; |
| 2724 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2725 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2726 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 2727 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2728 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2729 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2730 | |
| 2731 | // Trigger adapt down, expect scaled down resolution (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2732 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2733 | timestamp_ms += kFrameIntervalMs; |
| 2734 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2735 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2736 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
| 2737 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2738 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2739 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2740 | |
| 2741 | // Trigger adapt down, expect reduced fps (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2742 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2743 | timestamp_ms += kFrameIntervalMs; |
| 2744 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2745 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2746 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2747 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2748 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2749 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2750 | |
| 2751 | // Trigger adapt down, expect scaled down resolution (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2752 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2753 | timestamp_ms += kFrameIntervalMs; |
| 2754 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2755 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2756 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 2757 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2758 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2759 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2760 | |
| 2761 | // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2762 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2763 | timestamp_ms += kFrameIntervalMs; |
| 2764 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2765 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2766 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2767 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2768 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2769 | EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2770 | |
| 2771 | // Trigger adapt down, expect scaled down resolution (320x180@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2772 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2773 | timestamp_ms += kFrameIntervalMs; |
| 2774 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2775 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2776 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 2777 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2778 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2779 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2780 | |
| 2781 | // Trigger adapt down, expect reduced fps (320x180@7fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2782 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2783 | timestamp_ms += kFrameIntervalMs; |
| 2784 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2785 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2786 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2787 | rtc::VideoSinkWants last_wants = source.sink_wants(); |
| 2788 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2789 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2790 | EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2791 | |
| 2792 | // Trigger adapt down, min resolution reached, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2793 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2794 | timestamp_ms += kFrameIntervalMs; |
| 2795 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2796 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2797 | VerifyFpsEqResolutionEq(source.sink_wants(), last_wants); |
| 2798 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2799 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2800 | EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2801 | |
| 2802 | // Trigger adapt down, expect expect increased fps (320x180@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2803 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2804 | timestamp_ms += kFrameIntervalMs; |
| 2805 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2806 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2807 | VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2808 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2809 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2810 | EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2811 | |
| 2812 | // Trigger adapt up, expect upscaled resolution (480x270@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2813 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2814 | timestamp_ms += kFrameIntervalMs; |
| 2815 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2816 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2817 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 2818 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2819 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2820 | EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2821 | |
| 2822 | // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2823 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2824 | timestamp_ms += kFrameIntervalMs; |
| 2825 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2826 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2827 | VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2828 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2829 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2830 | EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2831 | |
| 2832 | // Trigger adapt up, expect upscaled resolution (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2833 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2834 | timestamp_ms += kFrameIntervalMs; |
| 2835 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2836 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2837 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 2838 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2839 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2840 | EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2841 | |
| 2842 | // Trigger adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2843 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2844 | timestamp_ms += kFrameIntervalMs; |
| 2845 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2846 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2847 | VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants()); |
| 2848 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2849 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2850 | EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2851 | |
| 2852 | // Trigger adapt up, expect upscaled resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2853 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2854 | timestamp_ms += kFrameIntervalMs; |
| 2855 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2856 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2857 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 2858 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2859 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2860 | EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2861 | |
| 2862 | // Trigger adapt up, expect no restriction (1280x720fps@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2863 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2864 | timestamp_ms += kFrameIntervalMs; |
| 2865 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2866 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2867 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2868 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2869 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2870 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2871 | EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2872 | |
| 2873 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2874 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2875 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2876 | EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2877 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2878 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2881 | TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2882 | const int kWidth = 1280; |
| 2883 | const int kHeight = 720; |
| 2884 | const int64_t kFrameIntervalMs = 150; |
| 2885 | int64_t timestamp_ms = kFrameIntervalMs; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2886 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2887 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2888 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2889 | AdaptingFrameForwarder source; |
| 2890 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2891 | video_stream_encoder_->SetSource(&source, |
| 2892 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2893 | timestamp_ms += kFrameIntervalMs; |
| 2894 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2895 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2896 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2897 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2898 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2899 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2900 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2901 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2902 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2903 | |
| 2904 | // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2905 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2906 | timestamp_ms += kFrameIntervalMs; |
| 2907 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2908 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2909 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 2910 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2911 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2912 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2913 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2914 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2915 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2916 | |
| 2917 | // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2918 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2919 | timestamp_ms += kFrameIntervalMs; |
| 2920 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2921 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2922 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
| 2923 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2924 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2925 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2926 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2927 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2928 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2929 | |
| 2930 | // Trigger quality adapt down, expect reduced fps (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2931 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2932 | timestamp_ms += kFrameIntervalMs; |
| 2933 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2934 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2935 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 2936 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2937 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2938 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2939 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2940 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2941 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2942 | |
| 2943 | // Trigger cpu adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2944 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2945 | timestamp_ms += kFrameIntervalMs; |
| 2946 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2947 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2948 | VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants()); |
| 2949 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2950 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2951 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2952 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2953 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2954 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2955 | |
| 2956 | // Trigger quality adapt up, expect upscaled resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2957 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2958 | timestamp_ms += kFrameIntervalMs; |
| 2959 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2960 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2961 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 2962 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2963 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2964 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2965 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2966 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2967 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2968 | |
| 2969 | // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2970 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2971 | timestamp_ms += kFrameIntervalMs; |
| 2972 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2973 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2974 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2975 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2976 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2977 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2978 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2979 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 2980 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2981 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2982 | |
| 2983 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2984 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2985 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2986 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2987 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2988 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2989 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2992 | TEST_F(VideoStreamEncoderTest, |
| 2993 | AdaptWithTwoReasonsAndDifferentOrder_Resolution) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2994 | const int kWidth = 640; |
| 2995 | const int kHeight = 360; |
| 2996 | const int kFpsLimit = 15; |
| 2997 | const int64_t kFrameIntervalMs = 150; |
| 2998 | int64_t timestamp_ms = kFrameIntervalMs; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2999 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3000 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3001 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3002 | AdaptingFrameForwarder source; |
| 3003 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3004 | video_stream_encoder_->SetSource(&source, |
| 3005 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3006 | timestamp_ms += kFrameIntervalMs; |
| 3007 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3008 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3009 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3010 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3011 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3012 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3013 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3014 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3015 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3016 | |
| 3017 | // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3018 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3019 | timestamp_ms += kFrameIntervalMs; |
| 3020 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3021 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3022 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 3023 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3024 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3025 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3026 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3027 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3028 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3029 | |
| 3030 | // Trigger quality adapt down, expect scaled down resolution (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3031 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3032 | timestamp_ms += kFrameIntervalMs; |
| 3033 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3034 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3035 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 3036 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3037 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3038 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3039 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3040 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3041 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3042 | |
| 3043 | // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3044 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3045 | timestamp_ms += kFrameIntervalMs; |
| 3046 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3047 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3048 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 3049 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3050 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3051 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3052 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3053 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3054 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3055 | |
| 3056 | // Trigger quality adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3057 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3058 | timestamp_ms += kFrameIntervalMs; |
| 3059 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3060 | WaitForEncodedFrame(timestamp_ms); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3061 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3062 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3063 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3064 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3065 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3066 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3067 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3068 | |
| 3069 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3070 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3071 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3072 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3073 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3074 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3075 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3078 | TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) { |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3079 | // Simulates simulcast behavior and makes highest stream resolutions divisible |
| 3080 | // by 4. |
| 3081 | class CroppingVideoStreamFactory |
| 3082 | : public VideoEncoderConfig::VideoStreamFactoryInterface { |
| 3083 | public: |
| 3084 | explicit CroppingVideoStreamFactory(size_t num_temporal_layers, |
| 3085 | int framerate) |
| 3086 | : num_temporal_layers_(num_temporal_layers), framerate_(framerate) { |
| 3087 | EXPECT_GT(num_temporal_layers, 0u); |
| 3088 | EXPECT_GT(framerate, 0); |
| 3089 | } |
| 3090 | |
| 3091 | private: |
| 3092 | std::vector<VideoStream> CreateEncoderStreams( |
| 3093 | int width, |
| 3094 | int height, |
| 3095 | const VideoEncoderConfig& encoder_config) override { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3096 | std::vector<VideoStream> streams = test::CreateVideoStreams( |
| 3097 | width - width % 4, height - height % 4, encoder_config); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3098 | for (VideoStream& stream : streams) { |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 3099 | stream.num_temporal_layers = num_temporal_layers_; |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3100 | stream.max_framerate = framerate_; |
| 3101 | } |
| 3102 | return streams; |
| 3103 | } |
| 3104 | |
| 3105 | const size_t num_temporal_layers_; |
| 3106 | const int framerate_; |
| 3107 | }; |
| 3108 | |
| 3109 | const int kFrameWidth = 1920; |
| 3110 | const int kFrameHeight = 1080; |
| 3111 | // 3/4 of 1920. |
| 3112 | const int kAdaptedFrameWidth = 1440; |
| 3113 | // 3/4 of 1080 rounded down to multiple of 4. |
| 3114 | const int kAdaptedFrameHeight = 808; |
| 3115 | const int kFramerate = 24; |
| 3116 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3117 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3118 | // Trigger reconfigure encoder (without resetting the entire instance). |
| 3119 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 3120 | video_encoder_config.codec_type = kVideoCodecVP8; |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3121 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 3122 | video_encoder_config.number_of_streams = 1; |
| 3123 | video_encoder_config.video_stream_factory = |
| 3124 | new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3125 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 3126 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3127 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3128 | |
| 3129 | video_source_.set_adaptation_enabled(true); |
| 3130 | |
| 3131 | video_source_.IncomingCapturedFrame( |
| 3132 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3133 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3134 | |
| 3135 | // Trigger CPU overuse, downscale by 3/4. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3136 | video_stream_encoder_->TriggerCpuOveruse(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3137 | video_source_.IncomingCapturedFrame( |
| 3138 | CreateFrame(2, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3139 | WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3140 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3141 | video_stream_encoder_->Stop(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3144 | TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3145 | const int kFrameWidth = 1280; |
| 3146 | const int kFrameHeight = 720; |
| 3147 | const int kLowFps = 2; |
| 3148 | const int kHighFps = 30; |
| 3149 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3150 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3151 | |
| 3152 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3153 | max_framerate_ = kLowFps; |
| 3154 | |
| 3155 | // Insert 2 seconds of 2fps video. |
| 3156 | for (int i = 0; i < kLowFps * 2; ++i) { |
| 3157 | video_source_.IncomingCapturedFrame( |
| 3158 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3159 | WaitForEncodedFrame(timestamp_ms); |
| 3160 | timestamp_ms += 1000 / kLowFps; |
| 3161 | } |
| 3162 | |
| 3163 | // Make sure encoder is updated with new target. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3164 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3165 | video_source_.IncomingCapturedFrame( |
| 3166 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3167 | WaitForEncodedFrame(timestamp_ms); |
| 3168 | timestamp_ms += 1000 / kLowFps; |
| 3169 | |
| 3170 | EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate()); |
| 3171 | |
| 3172 | // Insert 30fps frames for just a little more than the forced update period. |
| 3173 | const int kVcmTimerIntervalFrames = |
| 3174 | (vcm::VCMProcessTimer::kDefaultProcessIntervalMs * kHighFps) / 1000; |
| 3175 | const int kFrameIntervalMs = 1000 / kHighFps; |
| 3176 | max_framerate_ = kHighFps; |
| 3177 | for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) { |
| 3178 | video_source_.IncomingCapturedFrame( |
| 3179 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3180 | // Wait for encoded frame, but skip ahead if it doesn't arrive as it might |
| 3181 | // be dropped if the encoder hans't been updated with the new higher target |
| 3182 | // framerate yet, causing it to overshoot the target bitrate and then |
| 3183 | // suffering the wrath of the media optimizer. |
| 3184 | TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs); |
| 3185 | timestamp_ms += kFrameIntervalMs; |
| 3186 | } |
| 3187 | |
| 3188 | // Don expect correct measurement just yet, but it should be higher than |
| 3189 | // before. |
| 3190 | EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps); |
| 3191 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3192 | video_stream_encoder_->Stop(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3195 | TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3196 | const int kFrameWidth = 1280; |
| 3197 | const int kFrameHeight = 720; |
| 3198 | const int kTargetBitrateBps = 1000000; |
| 3199 | |
| 3200 | MockBitrateObserver bitrate_observer; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 3201 | video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3202 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 3203 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3204 | |
| 3205 | // Insert a first video frame, causes another bitrate update. |
| 3206 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3207 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1); |
| 3208 | video_source_.IncomingCapturedFrame( |
| 3209 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3210 | WaitForEncodedFrame(timestamp_ms); |
| 3211 | |
| 3212 | // Next, simulate video suspension due to pacer queue overrun. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3213 | video_stream_encoder_->OnBitrateUpdated(0, 0, 1); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3214 | |
| 3215 | // Skip ahead until a new periodic parameter update should have occured. |
| 3216 | timestamp_ms += vcm::VCMProcessTimer::kDefaultProcessIntervalMs; |
| 3217 | fake_clock_.AdvanceTimeMicros( |
| 3218 | vcm::VCMProcessTimer::kDefaultProcessIntervalMs * |
| 3219 | rtc::kNumMicrosecsPerMillisec); |
| 3220 | |
| 3221 | // Bitrate observer should not be called. |
| 3222 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0); |
| 3223 | video_source_.IncomingCapturedFrame( |
| 3224 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3225 | ExpectDroppedFrame(); |
| 3226 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3227 | video_stream_encoder_->Stop(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3228 | } |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3229 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3230 | TEST_F(VideoStreamEncoderTest, |
| 3231 | DefaultCpuAdaptationThresholdsForSoftwareEncoder) { |
| 3232 | const int kFrameWidth = 1280; |
| 3233 | const int kFrameHeight = 720; |
| 3234 | const CpuOveruseOptions default_options; |
| 3235 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 3236 | video_source_.IncomingCapturedFrame( |
| 3237 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 3238 | WaitForEncodedFrame(1); |
| 3239 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3240 | .low_encode_usage_threshold_percent, |
| 3241 | default_options.low_encode_usage_threshold_percent); |
| 3242 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3243 | .high_encode_usage_threshold_percent, |
| 3244 | default_options.high_encode_usage_threshold_percent); |
| 3245 | video_stream_encoder_->Stop(); |
| 3246 | } |
| 3247 | |
| 3248 | TEST_F(VideoStreamEncoderTest, |
| 3249 | HigherCpuAdaptationThresholdsForHardwareEncoder) { |
| 3250 | const int kFrameWidth = 1280; |
| 3251 | const int kFrameHeight = 720; |
| 3252 | CpuOveruseOptions hardware_options; |
| 3253 | hardware_options.low_encode_usage_threshold_percent = 150; |
| 3254 | hardware_options.high_encode_usage_threshold_percent = 200; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 3255 | fake_encoder_.SetIsHardwareAccelerated(true); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3256 | |
| 3257 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 3258 | video_source_.IncomingCapturedFrame( |
| 3259 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 3260 | WaitForEncodedFrame(1); |
| 3261 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3262 | .low_encode_usage_threshold_percent, |
| 3263 | hardware_options.low_encode_usage_threshold_percent); |
| 3264 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3265 | .high_encode_usage_threshold_percent, |
| 3266 | hardware_options.high_encode_usage_threshold_percent); |
| 3267 | video_stream_encoder_->Stop(); |
| 3268 | } |
| 3269 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3270 | TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) { |
| 3271 | const int kFrameWidth = 320; |
| 3272 | const int kFrameHeight = 240; |
| 3273 | const int kFps = 30; |
| 3274 | const int kTargetBitrateBps = 120000; |
| 3275 | const int kNumFramesInRun = kFps * 5; // Runs of five seconds. |
| 3276 | |
| 3277 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 3278 | |
| 3279 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3280 | max_framerate_ = kFps; |
| 3281 | |
| 3282 | // Insert 3 seconds of video, verify number of drops with normal bitrate. |
| 3283 | fake_encoder_.SimulateOvershoot(1.0); |
| 3284 | int num_dropped = 0; |
| 3285 | for (int i = 0; i < kNumFramesInRun; ++i) { |
| 3286 | video_source_.IncomingCapturedFrame( |
| 3287 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3288 | // Wait up to two frame durations for a frame to arrive. |
| 3289 | if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) { |
| 3290 | ++num_dropped; |
| 3291 | } |
| 3292 | timestamp_ms += 1000 / kFps; |
| 3293 | } |
| 3294 | |
| 3295 | // Frame drops should be less than 5% |
| 3296 | EXPECT_LT(num_dropped, 5 * kNumFramesInRun / 100); |
| 3297 | |
| 3298 | // Make encoder produce frames at double the expected bitrate during 3 seconds |
| 3299 | // of video, verify number of drops. Rate needs to be slightly changed in |
| 3300 | // order to force the rate to be reconfigured. |
| 3301 | fake_encoder_.SimulateOvershoot(2.0); |
| 3302 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps + 1000, 0, 0); |
| 3303 | num_dropped = 0; |
| 3304 | for (int i = 0; i < kNumFramesInRun; ++i) { |
| 3305 | video_source_.IncomingCapturedFrame( |
| 3306 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3307 | // Wait up to two frame durations for a frame to arrive. |
| 3308 | if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) { |
| 3309 | ++num_dropped; |
| 3310 | } |
| 3311 | timestamp_ms += 1000 / kFps; |
| 3312 | } |
| 3313 | |
| 3314 | // Frame drops should be more than 40%. |
| 3315 | EXPECT_GT(num_dropped, 40 * kNumFramesInRun / 100); |
| 3316 | |
| 3317 | video_stream_encoder_->Stop(); |
| 3318 | } |
| 3319 | |
| 3320 | TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) { |
| 3321 | const int kFrameWidth = 320; |
| 3322 | const int kFrameHeight = 240; |
| 3323 | const int kActualInputFps = 24; |
| 3324 | const int kTargetBitrateBps = 120000; |
| 3325 | |
| 3326 | ASSERT_GT(max_framerate_, kActualInputFps); |
| 3327 | |
| 3328 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3329 | max_framerate_ = kActualInputFps; |
| 3330 | video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 3331 | |
| 3332 | // Insert 3 seconds of video, with an input fps lower than configured max. |
| 3333 | for (int i = 0; i < kActualInputFps * 3; ++i) { |
| 3334 | video_source_.IncomingCapturedFrame( |
| 3335 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3336 | // Wait up to two frame durations for a frame to arrive. |
| 3337 | WaitForEncodedFrame(timestamp_ms); |
| 3338 | timestamp_ms += 1000 / kActualInputFps; |
| 3339 | } |
| 3340 | |
| 3341 | EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1); |
| 3342 | |
| 3343 | video_stream_encoder_->Stop(); |
| 3344 | } |
| 3345 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 3346 | } // namespace webrtc |