blob: 57b2b35e7c98f327100f6fc0b253b1cf22c1efa8 [file] [log] [blame]
perkj26091b12016-09-01 01:17:40 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Erik Språng4529fbc2018-10-12 10:30:31 +020011#include "video/video_stream_encoder.h"
12
sprangfe627f32017-03-29 08:24:59 -070013#include <algorithm>
perkj803d97f2016-11-01 11:45:46 -070014#include <limits>
Danil Chapovalovd3ba2362019-04-10 17:01:23 +020015#include <memory>
Per512ecb32016-09-23 15:52:06 +020016#include <utility>
17
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020018#include "absl/memory/memory.h"
Danil Chapovalovd3ba2362019-04-10 17:01:23 +020019#include "api/task_queue/default_task_queue_factory.h"
Elad Alon45befc52019-07-02 11:20:09 +020020#include "api/test/mock_fec_controller_override.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080021#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/i420_buffer.h"
Erik Språngf93eda12019-01-16 17:10:57 +010023#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020024#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020025#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010026#include "api/video_codecs/vp8_temporal_layers_factory.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020027#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070028#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020030#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010032#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020037#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "system_wrappers/include/sleep.h"
39#include "test/encoder_settings.h"
40#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020041#include "test/field_trial.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010042#include "test/frame_forwarder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/gmock.h"
44#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020045#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070047
48namespace webrtc {
49
sprangb1ca0732017-02-01 08:38:12 -080050using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080051using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020052using ::testing::AllOf;
53using ::testing::Field;
54using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080055
perkj803d97f2016-11-01 11:45:46 -070056namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020057const int kMinPixelsPerFrame = 320 * 180;
Åsa Perssone644a032019-11-08 15:56:00 +010058const int kQpLow = 1;
59const int kQpHigh = 2;
Åsa Persson8c1bf952018-09-13 10:42:19 +020060const int kMinFramerateFps = 2;
61const int kMinBalancedFramerateFps = 7;
62const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080063const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010064const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020065const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010066const uint32_t kSimulcastTargetBitrateBps = 3150000;
67const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080068const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070069const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020070const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020071const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020072const VideoEncoder::ResolutionBitrateLimits
73 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
74const VideoEncoder::ResolutionBitrateLimits
75 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080076
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020077uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
78 0x00, 0x00, 0x03, 0x03, 0xF4,
79 0x05, 0x03, 0xC7, 0xE0, 0x1B,
80 0x41, 0x10, 0x8D, 0x00};
81
perkj803d97f2016-11-01 11:45:46 -070082class TestBuffer : public webrtc::I420Buffer {
83 public:
84 TestBuffer(rtc::Event* event, int width, int height)
85 : I420Buffer(width, height), event_(event) {}
86
87 private:
88 friend class rtc::RefCountedObject<TestBuffer>;
89 ~TestBuffer() override {
90 if (event_)
91 event_->Set();
92 }
93 rtc::Event* const event_;
94};
95
Noah Richards51db4212019-06-12 06:59:12 -070096// A fake native buffer that can't be converted to I420.
97class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
98 public:
99 FakeNativeBuffer(rtc::Event* event, int width, int height)
100 : event_(event), width_(width), height_(height) {}
101 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
102 int width() const override { return width_; }
103 int height() const override { return height_; }
104 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
105 return nullptr;
106 }
107
108 private:
109 friend class rtc::RefCountedObject<FakeNativeBuffer>;
110 ~FakeNativeBuffer() override {
111 if (event_)
112 event_->Set();
113 }
114 rtc::Event* const event_;
115 const int width_;
116 const int height_;
117};
118
Niels Möller7dc26b72017-12-06 10:27:48 +0100119class CpuOveruseDetectorProxy : public OveruseFrameDetector {
120 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200121 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
122 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100123 last_target_framerate_fps_(-1) {}
124 virtual ~CpuOveruseDetectorProxy() {}
125
126 void OnTargetFramerateUpdated(int framerate_fps) override {
127 rtc::CritScope cs(&lock_);
128 last_target_framerate_fps_ = framerate_fps;
129 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
130 }
131
132 int GetLastTargetFramerate() {
133 rtc::CritScope cs(&lock_);
134 return last_target_framerate_fps_;
135 }
136
Niels Möller4db138e2018-04-19 09:04:13 +0200137 CpuOveruseOptions GetOptions() { return options_; }
138
Niels Möller7dc26b72017-12-06 10:27:48 +0100139 private:
140 rtc::CriticalSection lock_;
141 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
142};
143
mflodmancc3d4422017-08-03 08:27:51 -0700144class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700145 public:
Niels Möller213618e2018-07-24 09:29:58 +0200146 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200147 const VideoStreamEncoderSettings& settings,
148 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100149 : VideoStreamEncoder(Clock::GetRealTimeClock(),
150 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200151 stats_proxy,
152 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200153 std::unique_ptr<OveruseFrameDetector>(
154 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100155 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200156 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700157
sprangb1ca0732017-02-01 08:38:12 -0800158 void PostTaskAndWait(bool down, AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200159 PostTaskAndWait(down, reason, /*expected_results=*/true);
160 }
161
162 void PostTaskAndWait(bool down, AdaptReason reason, bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100163 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200164 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200165 if (down)
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200166 EXPECT_EQ(expected_results, AdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200167 else
168 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700169 event.Set();
170 });
perkj070ba852017-02-16 15:46:27 -0800171 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700172 }
173
kthelgason2fc52542017-03-03 00:24:41 -0800174 // This is used as a synchronisation mechanism, to make sure that the
175 // encoder queue is not blocked before we start sending it frames.
176 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100177 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200178 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800179 ASSERT_TRUE(event.Wait(5000));
180 }
181
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200182 void TriggerCpuOveruse() {
183 PostTaskAndWait(/*down=*/true, AdaptReason::kCpu);
184 }
kthelgason876222f2016-11-29 01:44:11 -0800185
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200186 void TriggerCpuNormalUsage() {
187 PostTaskAndWait(/*down=*/false, AdaptReason::kCpu);
188 }
kthelgason876222f2016-11-29 01:44:11 -0800189
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200190 void TriggerQualityLow() {
191 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality);
192 }
kthelgason876222f2016-11-29 01:44:11 -0800193
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200194 void TriggerQualityLowExpectFalse() {
195 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality,
196 /*expected_results=*/false);
197 }
198
199 void TriggerQualityHigh() {
200 PostTaskAndWait(/*down=*/false, AdaptReason::kQuality);
201 }
sprangfda496a2017-06-15 04:21:07 -0700202
Niels Möller7dc26b72017-12-06 10:27:48 +0100203 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700204};
205
asapersson5f7226f2016-11-25 04:37:00 -0800206class VideoStreamFactory
207 : public VideoEncoderConfig::VideoStreamFactoryInterface {
208 public:
sprangfda496a2017-06-15 04:21:07 -0700209 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
210 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800211 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700212 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800213 }
214
215 private:
216 std::vector<VideoStream> CreateEncoderStreams(
217 int width,
218 int height,
219 const VideoEncoderConfig& encoder_config) override {
220 std::vector<VideoStream> streams =
221 test::CreateVideoStreams(width, height, encoder_config);
222 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100223 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700224 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800225 }
226 return streams;
227 }
sprangfda496a2017-06-15 04:21:07 -0700228
asapersson5f7226f2016-11-25 04:37:00 -0800229 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700230 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800231};
232
Noah Richards51db4212019-06-12 06:59:12 -0700233// Simulates simulcast behavior and makes highest stream resolutions divisible
234// by 4.
235class CroppingVideoStreamFactory
236 : public VideoEncoderConfig::VideoStreamFactoryInterface {
237 public:
238 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
239 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
240 EXPECT_GT(num_temporal_layers, 0u);
241 EXPECT_GT(framerate, 0);
242 }
243
244 private:
245 std::vector<VideoStream> CreateEncoderStreams(
246 int width,
247 int height,
248 const VideoEncoderConfig& encoder_config) override {
249 std::vector<VideoStream> streams = test::CreateVideoStreams(
250 width - width % 4, height - height % 4, encoder_config);
251 for (VideoStream& stream : streams) {
252 stream.num_temporal_layers = num_temporal_layers_;
253 stream.max_framerate = framerate_;
254 }
255 return streams;
256 }
257
258 const size_t num_temporal_layers_;
259 const int framerate_;
260};
261
sprangb1ca0732017-02-01 08:38:12 -0800262class AdaptingFrameForwarder : public test::FrameForwarder {
263 public:
264 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700265 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800266
267 void set_adaptation_enabled(bool enabled) {
268 rtc::CritScope cs(&crit_);
269 adaptation_enabled_ = enabled;
270 }
271
asaperssonfab67072017-04-04 05:51:49 -0700272 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800273 rtc::CritScope cs(&crit_);
274 return adaptation_enabled_;
275 }
276
asapersson09f05612017-05-15 23:40:18 -0700277 rtc::VideoSinkWants last_wants() const {
278 rtc::CritScope cs(&crit_);
279 return last_wants_;
280 }
281
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200282 absl::optional<int> last_sent_width() const { return last_width_; }
283 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800284
sprangb1ca0732017-02-01 08:38:12 -0800285 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
286 int cropped_width = 0;
287 int cropped_height = 0;
288 int out_width = 0;
289 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700290 if (adaption_enabled()) {
291 if (adapter_.AdaptFrameResolution(
292 video_frame.width(), video_frame.height(),
293 video_frame.timestamp_us() * 1000, &cropped_width,
294 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100295 VideoFrame adapted_frame =
296 VideoFrame::Builder()
297 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
298 nullptr, out_width, out_height))
299 .set_timestamp_rtp(99)
300 .set_timestamp_ms(99)
301 .set_rotation(kVideoRotation_0)
302 .build();
sprangc5d62e22017-04-02 23:53:04 -0700303 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100304 if (video_frame.has_update_rect()) {
305 adapted_frame.set_update_rect(
306 video_frame.update_rect().ScaleWithFrame(
307 video_frame.width(), video_frame.height(), 0, 0,
308 video_frame.width(), video_frame.height(), out_width,
309 out_height));
310 }
sprangc5d62e22017-04-02 23:53:04 -0700311 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800312 last_width_.emplace(adapted_frame.width());
313 last_height_.emplace(adapted_frame.height());
314 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200315 last_width_ = absl::nullopt;
316 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700317 }
sprangb1ca0732017-02-01 08:38:12 -0800318 } else {
319 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800320 last_width_.emplace(video_frame.width());
321 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800322 }
323 }
324
325 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
326 const rtc::VideoSinkWants& wants) override {
327 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700328 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100329 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800330 test::FrameForwarder::AddOrUpdateSink(sink, wants);
331 }
sprangb1ca0732017-02-01 08:38:12 -0800332 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700333 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
334 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200335 absl::optional<int> last_width_;
336 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800337};
sprangc5d62e22017-04-02 23:53:04 -0700338
Niels Möller213618e2018-07-24 09:29:58 +0200339// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700340class MockableSendStatisticsProxy : public SendStatisticsProxy {
341 public:
342 MockableSendStatisticsProxy(Clock* clock,
343 const VideoSendStream::Config& config,
344 VideoEncoderConfig::ContentType content_type)
345 : SendStatisticsProxy(clock, config, content_type) {}
346
347 VideoSendStream::Stats GetStats() override {
348 rtc::CritScope cs(&lock_);
349 if (mock_stats_)
350 return *mock_stats_;
351 return SendStatisticsProxy::GetStats();
352 }
353
Niels Möller213618e2018-07-24 09:29:58 +0200354 int GetInputFrameRate() const override {
355 rtc::CritScope cs(&lock_);
356 if (mock_stats_)
357 return mock_stats_->input_frame_rate;
358 return SendStatisticsProxy::GetInputFrameRate();
359 }
sprangc5d62e22017-04-02 23:53:04 -0700360 void SetMockStats(const VideoSendStream::Stats& stats) {
361 rtc::CritScope cs(&lock_);
362 mock_stats_.emplace(stats);
363 }
364
365 void ResetMockStats() {
366 rtc::CritScope cs(&lock_);
367 mock_stats_.reset();
368 }
369
370 private:
371 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200372 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700373};
374
sprang4847ae62017-06-27 07:06:52 -0700375class MockBitrateObserver : public VideoBitrateAllocationObserver {
376 public:
Erik Språng566124a2018-04-23 12:32:22 +0200377 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700378};
379
perkj803d97f2016-11-01 11:45:46 -0700380} // namespace
381
mflodmancc3d4422017-08-03 08:27:51 -0700382class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700383 public:
384 static const int kDefaultTimeoutMs = 30 * 1000;
385
mflodmancc3d4422017-08-03 08:27:51 -0700386 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700387 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700388 codec_width_(320),
389 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200390 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200391 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700392 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200393 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700394 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700395 Clock::GetRealTimeClock(),
396 video_send_config_,
397 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700398 sink_(&fake_encoder_) {}
399
400 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700401 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700402 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200403 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800404 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200405 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200406 video_send_config_.rtp.payload_name = "FAKE";
407 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700408
Per512ecb32016-09-23 15:52:06 +0200409 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200410 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700411 video_encoder_config.video_stream_factory =
412 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100413 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700414
415 // Framerate limit is specified by the VideoStreamFactory.
416 std::vector<VideoStream> streams =
417 video_encoder_config.video_stream_factory->CreateEncoderStreams(
418 codec_width_, codec_height_, video_encoder_config);
419 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200420 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700421
Niels Möllerf1338562018-04-26 09:51:47 +0200422 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800423 }
424
Niels Möllerf1338562018-04-26 09:51:47 +0200425 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700426 if (video_stream_encoder_)
427 video_stream_encoder_->Stop();
428 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200429 stats_proxy_.get(), video_send_config_.encoder_settings,
430 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700431 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
432 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700433 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700434 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
435 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200436 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700437 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800438 }
439
440 void ResetEncoder(const std::string& payload_name,
441 size_t num_streams,
442 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700443 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700444 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200445 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800446
447 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200448 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800449 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100450 video_encoder_config.max_bitrate_bps =
451 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800452 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700453 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
454 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700455 video_encoder_config.content_type =
456 screenshare ? VideoEncoderConfig::ContentType::kScreen
457 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700458 if (payload_name == "VP9") {
459 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
460 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
461 video_encoder_config.encoder_specific_settings =
462 new rtc::RefCountedObject<
463 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
464 }
Niels Möllerf1338562018-04-26 09:51:47 +0200465 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700466 }
467
sprang57c2fff2017-01-16 06:24:02 -0800468 VideoFrame CreateFrame(int64_t ntp_time_ms,
469 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100470 VideoFrame frame =
471 VideoFrame::Builder()
472 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
473 destruction_event, codec_width_, codec_height_))
474 .set_timestamp_rtp(99)
475 .set_timestamp_ms(99)
476 .set_rotation(kVideoRotation_0)
477 .build();
sprang57c2fff2017-01-16 06:24:02 -0800478 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700479 return frame;
480 }
481
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100482 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
483 rtc::Event* destruction_event,
484 int offset_x) const {
485 VideoFrame frame =
486 VideoFrame::Builder()
487 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
488 destruction_event, codec_width_, codec_height_))
489 .set_timestamp_rtp(99)
490 .set_timestamp_ms(99)
491 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100492 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100493 .build();
494 frame.set_ntp_time_ms(ntp_time_ms);
495 return frame;
496 }
497
sprang57c2fff2017-01-16 06:24:02 -0800498 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100499 VideoFrame frame =
500 VideoFrame::Builder()
501 .set_video_frame_buffer(
502 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
503 .set_timestamp_rtp(99)
504 .set_timestamp_ms(99)
505 .set_rotation(kVideoRotation_0)
506 .build();
sprang57c2fff2017-01-16 06:24:02 -0800507 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700508 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700509 return frame;
510 }
511
Noah Richards51db4212019-06-12 06:59:12 -0700512 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
513 rtc::Event* destruction_event,
514 int width,
515 int height) const {
516 VideoFrame frame =
517 VideoFrame::Builder()
518 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
519 destruction_event, width, height))
520 .set_timestamp_rtp(99)
521 .set_timestamp_ms(99)
522 .set_rotation(kVideoRotation_0)
523 .build();
524 frame.set_ntp_time_ms(ntp_time_ms);
525 return frame;
526 }
527
528 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
529 rtc::Event* destruction_event) const {
530 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
531 codec_height_);
532 }
533
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100534 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
535 MockBitrateObserver bitrate_observer;
536 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
537
538 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
539 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200540 video_stream_encoder_->OnBitrateUpdated(
541 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
542 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100543
544 video_source_.IncomingCapturedFrame(
545 CreateFrame(1, codec_width_, codec_height_));
546 WaitForEncodedFrame(1);
547 }
548
asapersson02465b82017-04-10 01:12:52 -0700549 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700550 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700551 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
552 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700553 }
554
asapersson09f05612017-05-15 23:40:18 -0700555 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
556 const rtc::VideoSinkWants& wants2) {
557 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
558 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
559 }
560
Åsa Persson8c1bf952018-09-13 10:42:19 +0200561 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
562 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
563 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
564 EXPECT_FALSE(wants.target_pixel_count);
565 }
566
asapersson09f05612017-05-15 23:40:18 -0700567 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
568 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200569 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700570 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
571 EXPECT_GT(wants1.max_pixel_count, 0);
572 }
573
574 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
575 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200576 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700577 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
578 }
579
asaperssonf7e294d2017-06-13 23:25:22 -0700580 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
581 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200582 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700583 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
584 }
585
586 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
587 const rtc::VideoSinkWants& wants2) {
588 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
589 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
590 }
591
592 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
593 const rtc::VideoSinkWants& wants2) {
594 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
595 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
596 }
597
598 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
599 const rtc::VideoSinkWants& wants2) {
600 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
601 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
602 EXPECT_GT(wants1.max_pixel_count, 0);
603 }
604
605 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
606 const rtc::VideoSinkWants& wants2) {
607 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
608 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
609 }
610
asapersson09f05612017-05-15 23:40:18 -0700611 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
612 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200613 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700614 EXPECT_LT(wants.max_pixel_count, pixel_count);
615 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700616 }
617
618 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
619 EXPECT_LT(wants.max_framerate_fps, fps);
620 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
621 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700622 }
623
asaperssonf7e294d2017-06-13 23:25:22 -0700624 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
625 int expected_fps) {
626 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
627 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
628 EXPECT_FALSE(wants.target_pixel_count);
629 }
630
Jonathan Yubc771b72017-12-08 17:04:29 -0800631 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
632 int last_frame_pixels) {
633 // Balanced mode should always scale FPS to the desired range before
634 // attempting to scale resolution.
635 int fps_limit = wants.max_framerate_fps;
636 if (last_frame_pixels <= 320 * 240) {
637 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
638 } else if (last_frame_pixels <= 480 * 270) {
639 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
640 } else if (last_frame_pixels <= 640 * 480) {
641 EXPECT_LE(15, fps_limit);
642 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200643 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800644 }
645 }
646
sprang4847ae62017-06-27 07:06:52 -0700647 void WaitForEncodedFrame(int64_t expected_ntp_time) {
648 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200649 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700650 }
651
652 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
653 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200654 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700655 return ok;
656 }
657
658 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
659 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200660 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700661 }
662
663 void ExpectDroppedFrame() {
664 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200665 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700666 }
667
668 bool WaitForFrame(int64_t timeout_ms) {
669 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200670 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700671 return ok;
672 }
673
perkj26091b12016-09-01 01:17:40 -0700674 class TestEncoder : public test::FakeEncoder {
675 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100676 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700677
asaperssonfab67072017-04-04 05:51:49 -0700678 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800679 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700680 return config_;
681 }
682
683 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800684 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700685 block_next_encode_ = true;
686 }
687
Erik Språngaed30702018-11-05 12:57:17 +0100688 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800689 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100690 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100691 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100692 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100693 info.scaling_settings = VideoEncoder::ScalingSettings(
694 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100695 }
696 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100697 for (int i = 0; i < kMaxSpatialLayers; ++i) {
698 if (temporal_layers_supported_[i]) {
699 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
700 info.fps_allocation[i].resize(num_layers);
701 }
702 }
Erik Språngaed30702018-11-05 12:57:17 +0100703 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200704
705 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100706 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100707 return info;
kthelgason876222f2016-11-29 01:44:11 -0800708 }
709
Erik Språngb7cb7b52019-02-26 15:52:33 +0100710 int32_t RegisterEncodeCompleteCallback(
711 EncodedImageCallback* callback) override {
712 rtc::CritScope lock(&local_crit_sect_);
713 encoded_image_callback_ = callback;
714 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
715 }
716
perkjfa10b552016-10-02 23:45:26 -0700717 void ContinueEncode() { continue_encode_event_.Set(); }
718
719 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
720 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800721 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700722 EXPECT_EQ(timestamp_, timestamp);
723 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
724 }
725
kthelgason2fc52542017-03-03 00:24:41 -0800726 void SetQualityScaling(bool b) {
727 rtc::CritScope lock(&local_crit_sect_);
728 quality_scaling_ = b;
729 }
kthelgasonad9010c2017-02-14 00:46:51 -0800730
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100731 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
732 rtc::CritScope lock(&local_crit_sect_);
733 requested_resolution_alignment_ = requested_resolution_alignment;
734 }
735
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100736 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
737 rtc::CritScope lock(&local_crit_sect_);
738 is_hardware_accelerated_ = is_hardware_accelerated;
739 }
740
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100741 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
742 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
743 rtc::CritScope lock(&local_crit_sect_);
744 temporal_layers_supported_[spatial_idx] = supported;
745 }
746
Sergey Silkin6456e352019-07-08 17:56:40 +0200747 void SetResolutionBitrateLimits(
748 std::vector<ResolutionBitrateLimits> thresholds) {
749 rtc::CritScope cs(&local_crit_sect_);
750 resolution_bitrate_limits_ = thresholds;
751 }
752
sprangfe627f32017-03-29 08:24:59 -0700753 void ForceInitEncodeFailure(bool force_failure) {
754 rtc::CritScope lock(&local_crit_sect_);
755 force_init_encode_failed_ = force_failure;
756 }
757
Niels Möller6bb5ab92019-01-11 11:11:10 +0100758 void SimulateOvershoot(double rate_factor) {
759 rtc::CritScope lock(&local_crit_sect_);
760 rate_factor_ = rate_factor;
761 }
762
Erik Språngd7329ca2019-02-21 21:19:53 +0100763 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100764 rtc::CritScope lock(&local_crit_sect_);
765 return last_framerate_;
766 }
767
Erik Språngd7329ca2019-02-21 21:19:53 +0100768 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100769 rtc::CritScope lock(&local_crit_sect_);
770 return last_update_rect_;
771 }
772
Niels Möller87e2d782019-03-07 10:18:23 +0100773 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100774 rtc::CritScope lock(&local_crit_sect_);
775 return last_frame_types_;
776 }
777
778 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100779 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100780 keyframe ? VideoFrameType::kVideoFrameKey
781 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100782 {
783 rtc::CritScope lock(&local_crit_sect_);
784 last_frame_types_ = frame_type;
785 }
Niels Möllerb859b322019-03-07 12:40:01 +0100786 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100787 }
788
Erik Språngb7cb7b52019-02-26 15:52:33 +0100789 void InjectEncodedImage(const EncodedImage& image) {
790 rtc::CritScope lock(&local_crit_sect_);
791 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
792 }
793
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200794 void InjectEncodedImage(const EncodedImage& image,
795 const CodecSpecificInfo* codec_specific_info,
796 const RTPFragmentationHeader* fragmentation) {
797 rtc::CritScope lock(&local_crit_sect_);
798 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
799 fragmentation);
800 }
801
Erik Språngd7329ca2019-02-21 21:19:53 +0100802 void ExpectNullFrame() {
803 rtc::CritScope lock(&local_crit_sect_);
804 expect_null_frame_ = true;
805 }
806
Erik Språng5056af02019-09-02 15:53:11 +0200807 absl::optional<VideoEncoder::RateControlParameters>
808 GetAndResetLastRateControlSettings() {
809 auto settings = last_rate_control_settings_;
810 last_rate_control_settings_.reset();
811 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100812 }
813
Sergey Silkin5ee69672019-07-02 14:18:34 +0200814 int GetNumEncoderInitializations() const {
815 rtc::CritScope lock(&local_crit_sect_);
816 return num_encoder_initializations_;
817 }
818
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200819 int GetNumSetRates() const {
820 rtc::CritScope lock(&local_crit_sect_);
821 return num_set_rates_;
822 }
823
perkjfa10b552016-10-02 23:45:26 -0700824 private:
perkj26091b12016-09-01 01:17:40 -0700825 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100826 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700827 bool block_encode;
828 {
brandtre78d2662017-01-16 05:57:16 -0800829 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100830 if (expect_null_frame_) {
831 EXPECT_EQ(input_image.timestamp(), 0u);
832 EXPECT_EQ(input_image.width(), 1);
833 last_frame_types_ = *frame_types;
834 expect_null_frame_ = false;
835 } else {
836 EXPECT_GT(input_image.timestamp(), timestamp_);
837 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
838 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
839 }
perkj26091b12016-09-01 01:17:40 -0700840
841 timestamp_ = input_image.timestamp();
842 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700843 last_input_width_ = input_image.width();
844 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700845 block_encode = block_next_encode_;
846 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100847 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100848 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700849 }
Niels Möllerb859b322019-03-07 12:40:01 +0100850 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700851 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700852 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700853 return result;
854 }
855
sprangfe627f32017-03-29 08:24:59 -0700856 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200857 const Settings& settings) override {
858 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200859
sprangfe627f32017-03-29 08:24:59 -0700860 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100861 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200862
863 ++num_encoder_initializations_;
864
Erik Språng82fad3d2018-03-21 09:57:23 +0100865 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700866 // Simulate setting up temporal layers, in order to validate the life
867 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100868 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200869 frame_buffer_controller_ =
870 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700871 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100872 if (force_init_encode_failed_) {
873 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700874 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100875 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100876
Erik Språngb7cb7b52019-02-26 15:52:33 +0100877 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700878 return res;
879 }
880
Erik Språngb7cb7b52019-02-26 15:52:33 +0100881 int32_t Release() override {
882 rtc::CritScope lock(&local_crit_sect_);
883 EXPECT_NE(initialized_, EncoderState::kUninitialized);
884 initialized_ = EncoderState::kUninitialized;
885 return FakeEncoder::Release();
886 }
887
Erik Språng16cb8f52019-04-12 13:59:09 +0200888 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100889 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200890 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100891 VideoBitrateAllocation adjusted_rate_allocation;
892 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
893 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200894 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100895 adjusted_rate_allocation.SetBitrate(
896 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200897 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100898 rate_factor_));
899 }
900 }
901 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200902 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200903 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200904 RateControlParameters adjusted_paramters = parameters;
905 adjusted_paramters.bitrate = adjusted_rate_allocation;
906 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100907 }
908
brandtre78d2662017-01-16 05:57:16 -0800909 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100910 enum class EncoderState {
911 kUninitialized,
912 kInitializationFailed,
913 kInitialized
914 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
915 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700916 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700917 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700918 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
919 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
920 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
921 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
922 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100923 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100924 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100925 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700926 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100927 absl::optional<bool>
928 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
929 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700930 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100931 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
932 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200933 absl::optional<VideoEncoder::RateControlParameters>
934 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100935 VideoFrame::UpdateRect last_update_rect_
936 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100937 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100938 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100939 EncodedImageCallback* encoded_image_callback_
940 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200941 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200942 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200943 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
944 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200945 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700946 };
947
mflodmancc3d4422017-08-03 08:27:51 -0700948 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700949 public:
950 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100951 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700952
perkj26091b12016-09-01 01:17:40 -0700953 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700954 EXPECT_TRUE(
955 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
956 }
957
958 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
959 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700960 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700961 if (!encoded_frame_event_.Wait(timeout_ms))
962 return false;
perkj26091b12016-09-01 01:17:40 -0700963 {
964 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800965 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700966 }
967 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700968 return true;
perkj26091b12016-09-01 01:17:40 -0700969 }
970
sprangb1ca0732017-02-01 08:38:12 -0800971 void WaitForEncodedFrame(uint32_t expected_width,
972 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700973 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100974 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700975 }
976
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100977 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700978 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800979 uint32_t width = 0;
980 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800981 {
982 rtc::CritScope lock(&crit_);
983 width = last_width_;
984 height = last_height_;
985 }
986 EXPECT_EQ(expected_height, height);
987 EXPECT_EQ(expected_width, width);
988 }
989
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100990 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
991 int width = 0;
992 int height = 0;
993 {
994 rtc::CritScope lock(&crit_);
995 width = last_width_;
996 height = last_height_;
997 }
998 EXPECT_EQ(width % resolution_alignment, 0);
999 EXPECT_EQ(height % resolution_alignment, 0);
1000 }
1001
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001002 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1003 VideoRotation rotation;
1004 {
1005 rtc::CritScope lock(&crit_);
1006 rotation = last_rotation_;
1007 }
1008 EXPECT_EQ(expected_rotation, rotation);
1009 }
1010
kthelgason2fc52542017-03-03 00:24:41 -08001011 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001012
sprangc5d62e22017-04-02 23:53:04 -07001013 bool WaitForFrame(int64_t timeout_ms) {
1014 return encoded_frame_event_.Wait(timeout_ms);
1015 }
1016
perkj26091b12016-09-01 01:17:40 -07001017 void SetExpectNoFrames() {
1018 rtc::CritScope lock(&crit_);
1019 expect_frames_ = false;
1020 }
1021
asaperssonfab67072017-04-04 05:51:49 -07001022 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001023 rtc::CritScope lock(&crit_);
1024 return number_of_reconfigurations_;
1025 }
1026
asaperssonfab67072017-04-04 05:51:49 -07001027 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001028 rtc::CritScope lock(&crit_);
1029 return min_transmit_bitrate_bps_;
1030 }
1031
Erik Språngd7329ca2019-02-21 21:19:53 +01001032 void SetNumExpectedLayers(size_t num_layers) {
1033 rtc::CritScope lock(&crit_);
1034 num_expected_layers_ = num_layers;
1035 }
1036
Erik Språngb7cb7b52019-02-26 15:52:33 +01001037 int64_t GetLastCaptureTimeMs() const {
1038 rtc::CritScope lock(&crit_);
1039 return last_capture_time_ms_;
1040 }
1041
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001042 std::vector<uint8_t> GetLastEncodedImageData() {
1043 rtc::CritScope lock(&crit_);
1044 return std::move(last_encoded_image_data_);
1045 }
1046
1047 RTPFragmentationHeader GetLastFragmentation() {
1048 rtc::CritScope lock(&crit_);
1049 return std::move(last_fragmentation_);
1050 }
1051
perkj26091b12016-09-01 01:17:40 -07001052 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001053 Result OnEncodedImage(
1054 const EncodedImage& encoded_image,
1055 const CodecSpecificInfo* codec_specific_info,
1056 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001057 rtc::CritScope lock(&crit_);
1058 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001059 last_encoded_image_data_ = std::vector<uint8_t>(
1060 encoded_image.data(), encoded_image.data() + encoded_image.size());
1061 if (fragmentation) {
1062 last_fragmentation_.CopyFrom(*fragmentation);
1063 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001064 uint32_t timestamp = encoded_image.Timestamp();
1065 if (last_timestamp_ != timestamp) {
1066 num_received_layers_ = 1;
1067 } else {
1068 ++num_received_layers_;
1069 }
1070 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001071 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001072 last_width_ = encoded_image._encodedWidth;
1073 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001074 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001075 if (num_received_layers_ == num_expected_layers_) {
1076 encoded_frame_event_.Set();
1077 }
sprangb1ca0732017-02-01 08:38:12 -08001078 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001079 }
1080
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001081 void OnEncoderConfigurationChanged(
1082 std::vector<VideoStream> streams,
1083 VideoEncoderConfig::ContentType content_type,
1084 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001085 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001086 ++number_of_reconfigurations_;
1087 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1088 }
1089
perkj26091b12016-09-01 01:17:40 -07001090 rtc::CriticalSection crit_;
1091 TestEncoder* test_encoder_;
1092 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001093 std::vector<uint8_t> last_encoded_image_data_;
1094 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001095 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001096 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001097 uint32_t last_height_ = 0;
1098 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001099 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001100 size_t num_expected_layers_ = 1;
1101 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001102 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001103 int number_of_reconfigurations_ = 0;
1104 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001105 };
1106
Sergey Silkin5ee69672019-07-02 14:18:34 +02001107 class VideoBitrateAllocatorProxyFactory
1108 : public VideoBitrateAllocatorFactory {
1109 public:
1110 VideoBitrateAllocatorProxyFactory()
1111 : bitrate_allocator_factory_(
1112 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1113
1114 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1115 const VideoCodec& codec) override {
1116 rtc::CritScope lock(&crit_);
1117 codec_config_ = codec;
1118 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1119 }
1120
1121 VideoCodec codec_config() const {
1122 rtc::CritScope lock(&crit_);
1123 return codec_config_;
1124 }
1125
1126 private:
1127 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1128
1129 rtc::CriticalSection crit_;
1130 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1131 };
1132
perkj26091b12016-09-01 01:17:40 -07001133 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001134 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001135 int codec_width_;
1136 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001137 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001138 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001139 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001140 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001141 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001142 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001143 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001144 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001145 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001146 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001147};
1148
mflodmancc3d4422017-08-03 08:27:51 -07001149TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001150 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001151 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1152 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001153 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001154 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001155 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001156 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001157 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001158}
1159
mflodmancc3d4422017-08-03 08:27:51 -07001160TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001161 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001162 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001163 // The encoder will cache up to one frame for a short duration. Adding two
1164 // frames means that the first frame will be dropped and the second frame will
1165 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001166 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001167 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001168 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001169
Erik Språng4c6ca302019-04-08 15:14:01 +02001170 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001171 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1172 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001173
Sebastian Janssona3177052018-04-10 13:05:49 +02001174 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001175 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001176 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1177
1178 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001179 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001180}
1181
mflodmancc3d4422017-08-03 08:27:51 -07001182TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001183 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001184 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1185 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001186 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001187 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001188
Florent Castellia8336d32019-09-09 13:36:55 +02001189 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1190 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001191 // The encoder will cache up to one frame for a short duration. Adding two
1192 // frames means that the first frame will be dropped and the second frame will
1193 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001194 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001195 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001196
Erik Språng4c6ca302019-04-08 15:14:01 +02001197 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001198 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1199 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001200 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001201 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1202 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001203 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001204}
1205
mflodmancc3d4422017-08-03 08:27:51 -07001206TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001207 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001208 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1209 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001210 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001211 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001212
1213 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001214 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001215
perkja49cbd32016-09-16 07:53:41 -07001216 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001217 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001218 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001219}
1220
mflodmancc3d4422017-08-03 08:27:51 -07001221TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001222 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001223 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1224 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001225
perkja49cbd32016-09-16 07:53:41 -07001226 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001227 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001228
mflodmancc3d4422017-08-03 08:27:51 -07001229 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001230 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001231 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001232 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1233 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001234}
1235
mflodmancc3d4422017-08-03 08:27:51 -07001236TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001237 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001238 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1239 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001240
1241 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001242 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001243 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001244 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1245 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001246 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1247 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001248 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001249 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001250
mflodmancc3d4422017-08-03 08:27:51 -07001251 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001252}
1253
Noah Richards51db4212019-06-12 06:59:12 -07001254TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1255 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001256 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1257 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001258
1259 rtc::Event frame_destroyed_event;
1260 video_source_.IncomingCapturedFrame(
1261 CreateFakeNativeFrame(1, &frame_destroyed_event));
1262 ExpectDroppedFrame();
1263 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1264 video_stream_encoder_->Stop();
1265}
1266
1267TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1268 // Use the cropping factory.
1269 video_encoder_config_.video_stream_factory =
1270 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1271 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1272 kMaxPayloadLength);
1273 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1274
1275 // Capture a frame at codec_width_/codec_height_.
1276 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001277 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1278 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001279 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1280 WaitForEncodedFrame(1);
1281 // The encoder will have been configured once.
1282 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1283 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1284 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1285
1286 // Now send in a fake frame that needs to be cropped as the width/height
1287 // aren't divisible by 4 (see CreateEncoderStreams above).
1288 rtc::Event frame_destroyed_event;
1289 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1290 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1291 ExpectDroppedFrame();
1292 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1293 video_stream_encoder_->Stop();
1294}
1295
mflodmancc3d4422017-08-03 08:27:51 -07001296TEST_F(VideoStreamEncoderTest,
1297 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001298 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001299 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1300 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001301 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001302
1303 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001304 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001305 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001306 // The encoder will have been configured once when the first frame is
1307 // received.
1308 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001309
1310 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001311 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001312 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001313 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001314 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001315
1316 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001317 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001318 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001319 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001320 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001321
mflodmancc3d4422017-08-03 08:27:51 -07001322 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001323}
1324
mflodmancc3d4422017-08-03 08:27:51 -07001325TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001326 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001327 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1328 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001329
1330 // Capture a frame and wait for it to synchronize with the encoder thread.
1331 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001332 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001333 // The encoder will have been configured once.
1334 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001335 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1336 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1337
1338 codec_width_ *= 2;
1339 codec_height_ *= 2;
1340 // Capture a frame with a higher resolution and wait for it to synchronize
1341 // with the encoder thread.
1342 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001343 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001344 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1345 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001346 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001347
mflodmancc3d4422017-08-03 08:27:51 -07001348 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001349}
1350
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001351TEST_F(VideoStreamEncoderTest,
1352 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1353 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001354 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1355 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001356
1357 // Capture a frame and wait for it to synchronize with the encoder thread.
1358 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1359 WaitForEncodedFrame(1);
1360
1361 VideoEncoderConfig video_encoder_config;
1362 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1363 // Changing the max payload data length recreates encoder.
1364 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1365 kMaxPayloadLength / 2);
1366
1367 // Capture a frame and wait for it to synchronize with the encoder thread.
1368 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1369 WaitForEncodedFrame(2);
1370 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1371
1372 video_stream_encoder_->Stop();
1373}
1374
Sergey Silkin5ee69672019-07-02 14:18:34 +02001375TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1376 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001377 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1378 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001379
1380 VideoEncoderConfig video_encoder_config;
1381 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1382 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1383 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1384 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1385 kMaxPayloadLength);
1386
1387 // Capture a frame and wait for it to synchronize with the encoder thread.
1388 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1389 WaitForEncodedFrame(1);
1390 // The encoder will have been configured once when the first frame is
1391 // received.
1392 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1393 EXPECT_EQ(kTargetBitrateBps,
1394 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1395 EXPECT_EQ(kStartBitrateBps,
1396 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1397
Sergey Silkin6456e352019-07-08 17:56:40 +02001398 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1399 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001400 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1401 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1402 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1403 kMaxPayloadLength);
1404
1405 // Capture a frame and wait for it to synchronize with the encoder thread.
1406 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1407 WaitForEncodedFrame(2);
1408 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1409 // Bitrate limits have changed - rate allocator should be reconfigured,
1410 // encoder should not be reconfigured.
1411 EXPECT_EQ(kTargetBitrateBps * 2,
1412 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1413 EXPECT_EQ(kStartBitrateBps * 2,
1414 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1415 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1416
1417 video_stream_encoder_->Stop();
1418}
1419
Sergey Silkin6456e352019-07-08 17:56:40 +02001420TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001421 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001422 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001423 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1424 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001425
Sergey Silkin6456e352019-07-08 17:56:40 +02001426 VideoEncoderConfig video_encoder_config;
1427 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1428 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001429 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001430 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1431 kMaxPayloadLength);
1432
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001433 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001434 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001435
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001436 // Get the default bitrate limits and use them as baseline for custom
1437 // application and encoder recommended limits.
1438 const uint32_t kDefaultMinBitrateKbps =
1439 bitrate_allocator_factory_.codec_config().minBitrate;
1440 const uint32_t kDefaultMaxBitrateKbps =
1441 bitrate_allocator_factory_.codec_config().maxBitrate;
1442 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1443 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1444 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1445 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1446
1447 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1448 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1449 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1450 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1451
1452 // Change resolution. This will trigger encoder re-configuration and video
1453 // stream encoder will pick up the bitrate limits recommended by encoder.
1454 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1455 WaitForEncodedFrame(2);
1456 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1457 WaitForEncodedFrame(3);
1458
1459 // App bitrate limits are not set - bitrate limits recommended by encoder
1460 // should be used.
1461 EXPECT_EQ(kEncMaxBitrateKbps,
1462 bitrate_allocator_factory_.codec_config().maxBitrate);
1463 EXPECT_EQ(kEncMinBitrateKbps,
1464 bitrate_allocator_factory_.codec_config().minBitrate);
1465
1466 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1467 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1468 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1469 kMaxPayloadLength);
1470 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1471 WaitForEncodedFrame(4);
1472
1473 // App limited the max bitrate - bitrate limits recommended by encoder should
1474 // not be applied.
1475 EXPECT_EQ(kAppMaxBitrateKbps,
1476 bitrate_allocator_factory_.codec_config().maxBitrate);
1477 EXPECT_EQ(kDefaultMinBitrateKbps,
1478 bitrate_allocator_factory_.codec_config().minBitrate);
1479
1480 video_encoder_config.max_bitrate_bps = 0;
1481 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1482 kAppMinBitrateKbps * 1000;
1483 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1484 kMaxPayloadLength);
1485 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1486 WaitForEncodedFrame(5);
1487
1488 // App limited the min bitrate - bitrate limits recommended by encoder should
1489 // not be applied.
1490 EXPECT_EQ(kDefaultMaxBitrateKbps,
1491 bitrate_allocator_factory_.codec_config().maxBitrate);
1492 EXPECT_EQ(kAppMinBitrateKbps,
1493 bitrate_allocator_factory_.codec_config().minBitrate);
1494
1495 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1496 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1497 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001498 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1499 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001500 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1501 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001502
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001503 // App limited both min and max bitrates - bitrate limits recommended by
1504 // encoder should not be applied.
1505 EXPECT_EQ(kAppMaxBitrateKbps,
1506 bitrate_allocator_factory_.codec_config().maxBitrate);
1507 EXPECT_EQ(kAppMinBitrateKbps,
1508 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001509
1510 video_stream_encoder_->Stop();
1511}
1512
1513TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001514 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001515 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001516 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1517 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001518
1519 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001520 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001521 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001522 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001523 fake_encoder_.SetResolutionBitrateLimits(
1524 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1525
1526 VideoEncoderConfig video_encoder_config;
1527 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1528 video_encoder_config.max_bitrate_bps = 0;
1529 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1530 kMaxPayloadLength);
1531
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001532 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001533 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1534 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001535 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1536 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001537 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1538 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1539
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001540 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001541 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1542 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001543 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1544 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001545 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1546 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1547
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001548 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001549 // encoder for 360p should be used.
1550 video_source_.IncomingCapturedFrame(
1551 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1552 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001553 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1554 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001555 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1556 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1557
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001558 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001559 // ignored.
1560 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1561 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001562 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1563 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001564 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1565 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001566 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1567 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001568 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1569 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1570
1571 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1572 // for 270p should be used.
1573 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1574 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001575 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1576 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001577 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1578 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1579
1580 video_stream_encoder_->Stop();
1581}
1582
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001583TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1584 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001585 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1586 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001587
1588 VideoEncoderConfig video_encoder_config;
1589 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1590 video_encoder_config.max_bitrate_bps = 0;
1591 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1592 kMaxPayloadLength);
1593
1594 // Encode 720p frame to get the default encoder target bitrate.
1595 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1596 WaitForEncodedFrame(1);
1597 const uint32_t kDefaultTargetBitrateFor720pKbps =
1598 bitrate_allocator_factory_.codec_config()
1599 .simulcastStream[0]
1600 .targetBitrate;
1601
1602 // Set the max recommended encoder bitrate to something lower than the default
1603 // target bitrate.
1604 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1605 1280 * 720, 10 * 1000, 10 * 1000,
1606 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1607 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1608
1609 // Change resolution to trigger encoder reinitialization.
1610 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1611 WaitForEncodedFrame(2);
1612 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1613 WaitForEncodedFrame(3);
1614
1615 // Ensure the target bitrate is capped by the max bitrate.
1616 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1617 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1618 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1619 .simulcastStream[0]
1620 .targetBitrate *
1621 1000,
1622 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1623
1624 video_stream_encoder_->Stop();
1625}
1626
mflodmancc3d4422017-08-03 08:27:51 -07001627TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001628 EXPECT_TRUE(video_source_.has_sinks());
1629 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001630 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001631 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001632 EXPECT_FALSE(video_source_.has_sinks());
1633 EXPECT_TRUE(new_video_source.has_sinks());
1634
mflodmancc3d4422017-08-03 08:27:51 -07001635 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001636}
1637
mflodmancc3d4422017-08-03 08:27:51 -07001638TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001639 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001640 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001641 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001642 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001643}
1644
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001645TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1646 constexpr int kRequestedResolutionAlignment = 7;
1647 video_source_.set_adaptation_enabled(true);
1648 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1649 video_stream_encoder_->OnBitrateUpdated(
1650 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1651 DataRate::bps(kTargetBitrateBps), 0, 0);
1652
1653 // On the 1st frame, we should have initialized the encoder and
1654 // asked for its resolution requirements.
1655 video_source_.IncomingCapturedFrame(
1656 CreateFrame(1, codec_width_, codec_height_));
1657 WaitForEncodedFrame(1);
1658 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1659 kRequestedResolutionAlignment);
1660
1661 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1662 // (It's up the to the encoder to potentially drop the previous frame,
1663 // to avoid coding back-to-back keyframes.)
1664 video_source_.IncomingCapturedFrame(
1665 CreateFrame(2, codec_width_, codec_height_));
1666 WaitForEncodedFrame(2);
1667 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1668
1669 video_stream_encoder_->Stop();
1670}
1671
Jonathan Yubc771b72017-12-08 17:04:29 -08001672TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1673 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001674 const int kWidth = 1280;
1675 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001676
1677 // We rely on the automatic resolution adaptation, but we handle framerate
1678 // adaptation manually by mocking the stats proxy.
1679 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001680
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001681 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001682 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001683 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1684 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001685 video_stream_encoder_->SetSource(&video_source_,
1686 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001687 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001688 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001689 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001690 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1691
Jonathan Yubc771b72017-12-08 17:04:29 -08001692 // Adapt down as far as possible.
1693 rtc::VideoSinkWants last_wants;
1694 int64_t t = 1;
1695 int loop_count = 0;
1696 do {
1697 ++loop_count;
1698 last_wants = video_source_.sink_wants();
1699
1700 // Simulate the framerate we've been asked to adapt to.
1701 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1702 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1703 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1704 mock_stats.input_frame_rate = fps;
1705 stats_proxy_->SetMockStats(mock_stats);
1706
1707 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1708 sink_.WaitForEncodedFrame(t);
1709 t += frame_interval_ms;
1710
mflodmancc3d4422017-08-03 08:27:51 -07001711 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001712 VerifyBalancedModeFpsRange(
1713 video_source_.sink_wants(),
1714 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1715 } while (video_source_.sink_wants().max_pixel_count <
1716 last_wants.max_pixel_count ||
1717 video_source_.sink_wants().max_framerate_fps <
1718 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001719
Jonathan Yubc771b72017-12-08 17:04:29 -08001720 // Verify that we've adapted all the way down.
1721 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001722 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001723 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1724 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001725 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001726 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1727 *video_source_.last_sent_height());
1728 EXPECT_EQ(kMinBalancedFramerateFps,
1729 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001730
Jonathan Yubc771b72017-12-08 17:04:29 -08001731 // Adapt back up the same number of times we adapted down.
1732 for (int i = 0; i < loop_count - 1; ++i) {
1733 last_wants = video_source_.sink_wants();
1734
1735 // Simulate the framerate we've been asked to adapt to.
1736 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1737 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1738 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1739 mock_stats.input_frame_rate = fps;
1740 stats_proxy_->SetMockStats(mock_stats);
1741
1742 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1743 sink_.WaitForEncodedFrame(t);
1744 t += frame_interval_ms;
1745
mflodmancc3d4422017-08-03 08:27:51 -07001746 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001747 VerifyBalancedModeFpsRange(
1748 video_source_.sink_wants(),
1749 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1750 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1751 last_wants.max_pixel_count ||
1752 video_source_.sink_wants().max_framerate_fps >
1753 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001754 }
1755
Åsa Persson8c1bf952018-09-13 10:42:19 +02001756 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001757 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001758 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001759 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1760 EXPECT_EQ((loop_count - 1) * 2,
1761 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001762
mflodmancc3d4422017-08-03 08:27:51 -07001763 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001764}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001765
mflodmancc3d4422017-08-03 08:27:51 -07001766TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001767 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001768 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1769 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001770 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001771
sprangc5d62e22017-04-02 23:53:04 -07001772 const int kFrameWidth = 1280;
1773 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001774
Åsa Persson8c1bf952018-09-13 10:42:19 +02001775 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001776
kthelgason5e13d412016-12-01 03:59:51 -08001777 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001778 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001779 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001780 frame_timestamp += kFrameIntervalMs;
1781
perkj803d97f2016-11-01 11:45:46 -07001782 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001783 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001784 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001785 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001786 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001787 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001788
asapersson0944a802017-04-07 00:57:58 -07001789 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001790 // wanted resolution.
1791 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1792 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1793 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001794 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001795
1796 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001797 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001798 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001799 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001800
sprangc5d62e22017-04-02 23:53:04 -07001801 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001802 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001803
sprangc5d62e22017-04-02 23:53:04 -07001804 // Force an input frame rate to be available, or the adaptation call won't
1805 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001806 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001807 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001808 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001809 stats_proxy_->SetMockStats(stats);
1810
mflodmancc3d4422017-08-03 08:27:51 -07001811 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001812 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001813 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001814 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001815 frame_timestamp += kFrameIntervalMs;
1816
1817 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001818 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001819 EXPECT_EQ(std::numeric_limits<int>::max(),
1820 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001821 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001822
asapersson02465b82017-04-10 01:12:52 -07001823 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001824 video_stream_encoder_->SetSource(&new_video_source,
1825 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001826 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001827
mflodmancc3d4422017-08-03 08:27:51 -07001828 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001829 new_video_source.IncomingCapturedFrame(
1830 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001831 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001832 frame_timestamp += kFrameIntervalMs;
1833
1834 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001835 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001836
1837 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001838 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001839 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001840 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1841 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001842 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001843 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001844
1845 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001846 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001847 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001848 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1849 EXPECT_EQ(std::numeric_limits<int>::max(),
1850 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001851 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001852
mflodmancc3d4422017-08-03 08:27:51 -07001853 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001854}
1855
mflodmancc3d4422017-08-03 08:27:51 -07001856TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001857 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001858 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1859 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001860
asaperssonfab67072017-04-04 05:51:49 -07001861 const int kWidth = 1280;
1862 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001863 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001864 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001865 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1866 EXPECT_FALSE(stats.bw_limited_resolution);
1867 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1868
1869 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001870 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001871 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001872 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001873
1874 stats = stats_proxy_->GetStats();
1875 EXPECT_TRUE(stats.bw_limited_resolution);
1876 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1877
1878 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001879 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001880 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001881 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001882
1883 stats = stats_proxy_->GetStats();
1884 EXPECT_FALSE(stats.bw_limited_resolution);
1885 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1886 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1887
mflodmancc3d4422017-08-03 08:27:51 -07001888 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001889}
1890
mflodmancc3d4422017-08-03 08:27:51 -07001891TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001892 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001893 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1894 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001895
1896 const int kWidth = 1280;
1897 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001898 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001899 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001900 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1901 EXPECT_FALSE(stats.cpu_limited_resolution);
1902 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1903
1904 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001905 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001906 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001907 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001908
1909 stats = stats_proxy_->GetStats();
1910 EXPECT_TRUE(stats.cpu_limited_resolution);
1911 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1912
1913 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001914 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001915 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001916 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001917
1918 stats = stats_proxy_->GetStats();
1919 EXPECT_FALSE(stats.cpu_limited_resolution);
1920 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001921 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001922
mflodmancc3d4422017-08-03 08:27:51 -07001923 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001924}
1925
mflodmancc3d4422017-08-03 08:27:51 -07001926TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001927 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001928 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1929 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001930
asaperssonfab67072017-04-04 05:51:49 -07001931 const int kWidth = 1280;
1932 const int kHeight = 720;
1933 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001934 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001935 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001936 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001937 EXPECT_FALSE(stats.cpu_limited_resolution);
1938 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1939
asaperssonfab67072017-04-04 05:51:49 -07001940 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001941 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001942 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001943 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001944 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001945 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001946 EXPECT_TRUE(stats.cpu_limited_resolution);
1947 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1948
1949 // Set new source with adaptation still enabled.
1950 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001951 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001952 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001953
asaperssonfab67072017-04-04 05:51:49 -07001954 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001955 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001956 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001957 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001958 EXPECT_TRUE(stats.cpu_limited_resolution);
1959 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1960
1961 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001962 video_stream_encoder_->SetSource(&new_video_source,
1963 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001964
asaperssonfab67072017-04-04 05:51:49 -07001965 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001966 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001967 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001968 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001969 EXPECT_FALSE(stats.cpu_limited_resolution);
1970 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1971
1972 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001973 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001974 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001975
asaperssonfab67072017-04-04 05:51:49 -07001976 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001977 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001978 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001979 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001980 EXPECT_TRUE(stats.cpu_limited_resolution);
1981 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1982
asaperssonfab67072017-04-04 05:51:49 -07001983 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001984 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001985 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001986 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001987 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001988 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001989 EXPECT_FALSE(stats.cpu_limited_resolution);
1990 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001991 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001992
mflodmancc3d4422017-08-03 08:27:51 -07001993 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001994}
1995
mflodmancc3d4422017-08-03 08:27:51 -07001996TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001997 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001998 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1999 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002000
asaperssonfab67072017-04-04 05:51:49 -07002001 const int kWidth = 1280;
2002 const int kHeight = 720;
2003 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002004 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002005 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002006 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002007 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002008 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002009
2010 // Set new source with adaptation still enabled.
2011 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002012 video_stream_encoder_->SetSource(&new_video_source,
2013 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002014
asaperssonfab67072017-04-04 05:51:49 -07002015 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002016 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002017 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002018 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002019 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002020 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002021
asaperssonfab67072017-04-04 05:51:49 -07002022 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002023 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002024 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002025 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002026 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002027 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002028 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002029 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002030
asaperssonfab67072017-04-04 05:51:49 -07002031 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002032 video_stream_encoder_->SetSource(&new_video_source,
2033 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002034
asaperssonfab67072017-04-04 05:51:49 -07002035 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002036 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002037 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002038 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002039 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002040 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002041
asapersson02465b82017-04-10 01:12:52 -07002042 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002043 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002044 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002045
asaperssonfab67072017-04-04 05:51:49 -07002046 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002047 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002048 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002049 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002050 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002051 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2052 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002053
mflodmancc3d4422017-08-03 08:27:51 -07002054 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002055}
2056
mflodmancc3d4422017-08-03 08:27:51 -07002057TEST_F(VideoStreamEncoderTest,
2058 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002059 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002060 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2061 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002062
2063 const int kWidth = 1280;
2064 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002065 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002066 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002067 video_source_.IncomingCapturedFrame(
2068 CreateFrame(timestamp_ms, kWidth, kHeight));
2069 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002070 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2071 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2072 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2073
2074 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002075 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002076 timestamp_ms += kFrameIntervalMs;
2077 video_source_.IncomingCapturedFrame(
2078 CreateFrame(timestamp_ms, kWidth, kHeight));
2079 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002080 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2081 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2082 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2083
2084 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002085 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002086 timestamp_ms += kFrameIntervalMs;
2087 video_source_.IncomingCapturedFrame(
2088 CreateFrame(timestamp_ms, kWidth, kHeight));
2089 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002090 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2091 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2092 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2093
Niels Möller4db138e2018-04-19 09:04:13 +02002094 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002095 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002096
2097 VideoEncoderConfig video_encoder_config;
2098 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2099 // Make format different, to force recreation of encoder.
2100 video_encoder_config.video_format.parameters["foo"] = "foo";
2101 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002102 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002103 timestamp_ms += kFrameIntervalMs;
2104 video_source_.IncomingCapturedFrame(
2105 CreateFrame(timestamp_ms, kWidth, kHeight));
2106 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002107 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2108 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2109 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2110
mflodmancc3d4422017-08-03 08:27:51 -07002111 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002112}
2113
mflodmancc3d4422017-08-03 08:27:51 -07002114TEST_F(VideoStreamEncoderTest,
2115 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002116 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002117 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2118 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002119
asapersson0944a802017-04-07 00:57:58 -07002120 const int kWidth = 1280;
2121 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002122 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002123
asaperssonfab67072017-04-04 05:51:49 -07002124 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002125 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002126 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002127 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002128 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002129 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2130
asapersson02465b82017-04-10 01:12:52 -07002131 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002132 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002133 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002134 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002135 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002136 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002137 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002138 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2139
2140 // Set new source with adaptation still enabled.
2141 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002142 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002143 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002144
2145 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002146 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002147 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002148 stats = stats_proxy_->GetStats();
2149 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002150 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002151 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2152
sprangc5d62e22017-04-02 23:53:04 -07002153 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002154 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002155 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002156 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002157 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002158 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002159 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002160 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002161 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002162 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002163 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2164
sprangc5d62e22017-04-02 23:53:04 -07002165 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002166 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002167 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2168 mock_stats.input_frame_rate = 30;
2169 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002170 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002171 stats_proxy_->ResetMockStats();
2172
2173 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002174 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002175 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002176
2177 // Framerate now adapted.
2178 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002179 EXPECT_FALSE(stats.cpu_limited_resolution);
2180 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002181 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2182
2183 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002184 video_stream_encoder_->SetSource(&new_video_source,
2185 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002186 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002187 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002188 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002189
2190 stats = stats_proxy_->GetStats();
2191 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002192 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002193 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2194
2195 // Try to trigger overuse. Should not succeed.
2196 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002197 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002198 stats_proxy_->ResetMockStats();
2199
2200 stats = stats_proxy_->GetStats();
2201 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002202 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002203 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2204
2205 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002206 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002207 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002208 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002209 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002210 stats = stats_proxy_->GetStats();
2211 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002212 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002213 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002214
2215 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002216 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002217 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002218 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002219 stats = stats_proxy_->GetStats();
2220 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002221 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002222 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2223
2224 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002225 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002226 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002227 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002228 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002229 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002230 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002231 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002232 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002233 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002234 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2235
2236 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002237 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002238 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002239 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002240 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002241 stats = stats_proxy_->GetStats();
2242 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002243 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002244 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002245 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002246
mflodmancc3d4422017-08-03 08:27:51 -07002247 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002248}
2249
mflodmancc3d4422017-08-03 08:27:51 -07002250TEST_F(VideoStreamEncoderTest,
2251 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002252 const int kWidth = 1280;
2253 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002254 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002255 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2256 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002257
asaperssonfab67072017-04-04 05:51:49 -07002258 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002259 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002260
asaperssonfab67072017-04-04 05:51:49 -07002261 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002262 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002263
asaperssonfab67072017-04-04 05:51:49 -07002264 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002265 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002266
asaperssonfab67072017-04-04 05:51:49 -07002267 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002268 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002269
kthelgason876222f2016-11-29 01:44:11 -08002270 // Expect a scale down.
2271 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002272 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002273
asapersson02465b82017-04-10 01:12:52 -07002274 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002275 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002276 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002277 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002278
asaperssonfab67072017-04-04 05:51:49 -07002279 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002280 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002281 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002282 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002283
asaperssonfab67072017-04-04 05:51:49 -07002284 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002285 EXPECT_EQ(std::numeric_limits<int>::max(),
2286 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002287
asaperssonfab67072017-04-04 05:51:49 -07002288 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002289 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002290 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002291 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002292
asapersson02465b82017-04-10 01:12:52 -07002293 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002294 EXPECT_EQ(std::numeric_limits<int>::max(),
2295 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002296
mflodmancc3d4422017-08-03 08:27:51 -07002297 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002298}
2299
mflodmancc3d4422017-08-03 08:27:51 -07002300TEST_F(VideoStreamEncoderTest,
2301 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002302 const int kWidth = 1280;
2303 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002304 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002305 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2306 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002307
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002308 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002309 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002310 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002311 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002312
2313 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002314 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002315 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002316 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2317 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2318
2319 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002320 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002321 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002322 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2323 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2324 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2325
2326 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002327 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002328 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2329 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2330 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2331
mflodmancc3d4422017-08-03 08:27:51 -07002332 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002333}
2334
mflodmancc3d4422017-08-03 08:27:51 -07002335TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002336 const int kWidth = 1280;
2337 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002338 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002339 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2340 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002341
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002342 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002343 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002344 video_stream_encoder_->SetSource(&source,
2345 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002346 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2347 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002348 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002349
2350 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002351 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002352 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2353 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2354 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2355 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2356
2357 // Trigger adapt down for same input resolution, expect no change.
2358 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2359 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002360 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002361 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2362 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2363 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2364
2365 // Trigger adapt down for larger input resolution, expect no change.
2366 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2367 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002368 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002369 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2370 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2371 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2372
mflodmancc3d4422017-08-03 08:27:51 -07002373 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002374}
2375
mflodmancc3d4422017-08-03 08:27:51 -07002376TEST_F(VideoStreamEncoderTest,
2377 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002378 const int kWidth = 1280;
2379 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002380 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002381 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2382 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002383
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002384 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002385 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002386 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002387 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002388
2389 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002390 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002391 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002392 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2393 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2394
2395 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002396 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002397 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002398 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2399 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2400
mflodmancc3d4422017-08-03 08:27:51 -07002401 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002402}
2403
mflodmancc3d4422017-08-03 08:27:51 -07002404TEST_F(VideoStreamEncoderTest,
2405 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002406 const int kWidth = 1280;
2407 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002408 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002409 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2410 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002411
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002412 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002413 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002414 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002415 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002416
2417 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002418 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002419 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002420 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002421 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2422
2423 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002424 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002425 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002426 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002427 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2428
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002430}
2431
mflodmancc3d4422017-08-03 08:27:51 -07002432TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002433 const int kWidth = 1280;
2434 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002435 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002436 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2437 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002438
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002439 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002440 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002441 video_stream_encoder_->SetSource(&source,
2442 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002443
2444 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2445 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002446 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002447 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2448 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2449 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2450
2451 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002452 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002453 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002454 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2455 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2456 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2457
mflodmancc3d4422017-08-03 08:27:51 -07002458 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002459}
2460
mflodmancc3d4422017-08-03 08:27:51 -07002461TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002462 const int kWidth = 1280;
2463 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002464 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002465 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2466 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002467
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002468 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002469 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002470 video_stream_encoder_->SetSource(&source,
2471 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002472
2473 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2474 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002475 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002476 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2477 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2478 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2479
2480 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002481 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002482 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002483 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2484 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2485 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2486
mflodmancc3d4422017-08-03 08:27:51 -07002487 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002488}
2489
mflodmancc3d4422017-08-03 08:27:51 -07002490TEST_F(VideoStreamEncoderTest,
2491 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002492 const int kWidth = 1280;
2493 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002494 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002495 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2496 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002497
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002498 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002499 AdaptingFrameForwarder source;
2500 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002501 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002502 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002503
2504 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002505 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002506 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002507 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2508 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2509
2510 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002511 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002512 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002513 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002514 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002515 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2516 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2517
2518 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002519 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002520 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002521 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2522 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2523 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2524
mflodmancc3d4422017-08-03 08:27:51 -07002525 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002526}
2527
mflodmancc3d4422017-08-03 08:27:51 -07002528TEST_F(VideoStreamEncoderTest,
2529 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002530 const int kWidth = 1280;
2531 const int kHeight = 720;
2532 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002533 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002534 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2535 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002536
2537 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2538 stats.input_frame_rate = kInputFps;
2539 stats_proxy_->SetMockStats(stats);
2540
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002541 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002542 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2543 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002544 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002545
2546 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002547 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002548 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2549 sink_.WaitForEncodedFrame(2);
2550 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2551
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002552 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002553 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002554 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002555 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002556 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002557
2558 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002559 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002560 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2561 sink_.WaitForEncodedFrame(3);
2562 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2563
2564 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002565 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002566 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002567
mflodmancc3d4422017-08-03 08:27:51 -07002568 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002569}
2570
mflodmancc3d4422017-08-03 08:27:51 -07002571TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002572 const int kWidth = 1280;
2573 const int kHeight = 720;
2574 const size_t kNumFrames = 10;
2575
Erik Språng4c6ca302019-04-08 15:14:01 +02002576 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002577 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2578 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002579
asaperssond0de2952017-04-21 01:47:31 -07002580 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002581 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002582 video_source_.set_adaptation_enabled(true);
2583
2584 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2585 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2586
2587 int downscales = 0;
2588 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002589 video_source_.IncomingCapturedFrame(
2590 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2591 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002592
asaperssonfab67072017-04-04 05:51:49 -07002593 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002594 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002595 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002596 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002597
2598 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2599 ++downscales;
2600
2601 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2602 EXPECT_EQ(downscales,
2603 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2604 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002605 }
mflodmancc3d4422017-08-03 08:27:51 -07002606 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002607}
2608
mflodmancc3d4422017-08-03 08:27:51 -07002609TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002610 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2611 const int kWidth = 1280;
2612 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002613 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002614 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2615 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002616
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002617 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002618 AdaptingFrameForwarder source;
2619 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002620 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002621 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002622
Åsa Persson8c1bf952018-09-13 10:42:19 +02002623 int64_t timestamp_ms = kFrameIntervalMs;
2624 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002625 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002626 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002627 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2628 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2629
2630 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002631 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002632 timestamp_ms += kFrameIntervalMs;
2633 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2634 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002635 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002636 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2637 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2638
2639 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002640 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002641 timestamp_ms += kFrameIntervalMs;
2642 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002643 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002644 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002645 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2646 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2647
2648 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002649 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002650 timestamp_ms += kFrameIntervalMs;
2651 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2652 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002653 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002654 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2655 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2656
2657 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002658 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002659 timestamp_ms += kFrameIntervalMs;
2660 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002661 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002662 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002663 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2664 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2665
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002667}
2668
mflodmancc3d4422017-08-03 08:27:51 -07002669TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002670 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2671 const int kWidth = 1280;
2672 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002673 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002674 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2675 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002676
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002677 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002678 AdaptingFrameForwarder source;
2679 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002680 video_stream_encoder_->SetSource(&source,
2681 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002682
Åsa Persson8c1bf952018-09-13 10:42:19 +02002683 int64_t timestamp_ms = kFrameIntervalMs;
2684 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002685 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002686 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002687 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2688 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2689
2690 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002691 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002692 timestamp_ms += kFrameIntervalMs;
2693 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2694 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002695 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2696 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2697 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2698
2699 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002700 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002701 timestamp_ms += kFrameIntervalMs;
2702 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002703 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002704 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002705 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2706 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2707
2708 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002709 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002710 timestamp_ms += kFrameIntervalMs;
2711 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2712 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002713 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2714 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2715 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2716
2717 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002718 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002719 timestamp_ms += kFrameIntervalMs;
2720 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002721 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002722 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002723 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2724 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2725
mflodmancc3d4422017-08-03 08:27:51 -07002726 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002727}
2728
Sergey Silkin41c650b2019-10-14 13:12:19 +02002729TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2730 fake_encoder_.SetResolutionBitrateLimits(
2731 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2732
2733 video_stream_encoder_->OnBitrateUpdated(
2734 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2735 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2736 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2737
2738 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2739 AdaptingFrameForwarder source;
2740 source.set_adaptation_enabled(true);
2741 video_stream_encoder_->SetSource(
2742 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2743
2744 // Insert 720p frame.
2745 int64_t timestamp_ms = kFrameIntervalMs;
2746 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2747 WaitForEncodedFrame(1280, 720);
2748
2749 // Reduce bitrate and trigger adapt down.
2750 video_stream_encoder_->OnBitrateUpdated(
2751 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2752 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2753 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2754 video_stream_encoder_->TriggerQualityLow();
2755
2756 // Insert 720p frame. It should be downscaled and encoded.
2757 timestamp_ms += kFrameIntervalMs;
2758 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2759 WaitForEncodedFrame(960, 540);
2760
2761 // Trigger adapt up. Higher resolution should not be requested duo to lack
2762 // of bitrate.
2763 video_stream_encoder_->TriggerQualityHigh();
2764 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2765
2766 // Increase bitrate.
2767 video_stream_encoder_->OnBitrateUpdated(
2768 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2769 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2770 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2771
2772 // Trigger adapt up. Higher resolution should be requested.
2773 video_stream_encoder_->TriggerQualityHigh();
2774 VerifyFpsMaxResolutionMax(source.sink_wants());
2775
2776 video_stream_encoder_->Stop();
2777}
2778
2779TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2780 fake_encoder_.SetResolutionBitrateLimits(
2781 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2782
2783 // Set bitrate equal to min bitrate of 540p.
2784 video_stream_encoder_->OnBitrateUpdated(
2785 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2786 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2787 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2788
2789 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2790 AdaptingFrameForwarder source;
2791 source.set_adaptation_enabled(true);
2792 video_stream_encoder_->SetSource(
2793 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2794
2795 // Insert 720p frame. It should be dropped and lower resolution should be
2796 // requested.
2797 int64_t timestamp_ms = kFrameIntervalMs;
2798 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2799 ExpectDroppedFrame();
2800 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2801
2802 // Insert 720p frame. It should be downscaled and encoded.
2803 timestamp_ms += kFrameIntervalMs;
2804 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2805 WaitForEncodedFrame(960, 540);
2806
2807 video_stream_encoder_->Stop();
2808}
2809
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002810class BalancedDegradationTest : public VideoStreamEncoderTest {
2811 protected:
2812 void SetupTest() {
2813 // Reset encoder for field trials to take effect.
2814 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002815 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002816
2817 // Enable BALANCED preference.
2818 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002819 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2820 }
2821
2822 void OnBitrateUpdated(int bitrate_bps) {
2823 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2824 DataRate::bps(bitrate_bps),
2825 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002826 }
2827
Åsa Persson45b176f2019-09-30 11:19:05 +02002828 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002829 timestamp_ms_ += kFrameIntervalMs;
2830 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002831 }
2832
2833 void InsertFrameAndWaitForEncoded() {
2834 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002835 sink_.WaitForEncodedFrame(timestamp_ms_);
2836 }
2837
2838 const int kWidth = 640; // pixels:640x360=230400
2839 const int kHeight = 360;
2840 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2841 int64_t timestamp_ms_ = 0;
2842 AdaptingFrameForwarder source_;
2843};
2844
2845TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2846 test::ScopedFieldTrials field_trials(
2847 "WebRTC-Video-BalancedDegradationSettings/"
2848 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2849 SetupTest();
2850
2851 // Force input frame rate.
2852 const int kInputFps = 24;
2853 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2854 stats.input_frame_rate = kInputFps;
2855 stats_proxy_->SetMockStats(stats);
2856
Åsa Persson45b176f2019-09-30 11:19:05 +02002857 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002858 VerifyFpsMaxResolutionMax(source_.sink_wants());
2859
2860 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2861 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2862 video_stream_encoder_->TriggerQualityLowExpectFalse();
2863 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2864
2865 video_stream_encoder_->Stop();
2866}
2867
2868TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2869 test::ScopedFieldTrials field_trials(
2870 "WebRTC-Video-BalancedDegradationSettings/"
2871 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2872 SetupTest();
2873
2874 // Force input frame rate.
2875 const int kInputFps = 25;
2876 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2877 stats.input_frame_rate = kInputFps;
2878 stats_proxy_->SetMockStats(stats);
2879
Åsa Persson45b176f2019-09-30 11:19:05 +02002880 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002881 VerifyFpsMaxResolutionMax(source_.sink_wants());
2882
2883 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2884 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2885 video_stream_encoder_->TriggerQualityLow();
2886 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2887
2888 video_stream_encoder_->Stop();
2889}
2890
2891TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2892 test::ScopedFieldTrials field_trials(
2893 "WebRTC-Video-BalancedDegradationSettings/"
2894 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2895 SetupTest();
2896
2897 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2898
Åsa Persson45b176f2019-09-30 11:19:05 +02002899 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002900 VerifyFpsMaxResolutionMax(source_.sink_wants());
2901
2902 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2903 video_stream_encoder_->TriggerQualityLow();
2904 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2905
2906 video_stream_encoder_->Stop();
2907}
2908
Åsa Perssonccfb3402019-09-25 15:13:04 +02002909TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002910 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002911 "WebRTC-Video-BalancedDegradationSettings/"
2912 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002913 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002914
Åsa Persson1b247f12019-08-14 17:26:39 +02002915 const int kMinBitrateBps = 425000;
2916 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002917 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002918
Åsa Persson45b176f2019-09-30 11:19:05 +02002919 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002920 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002921 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2922
2923 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2924 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002925 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002926 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002927 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2928
2929 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2930 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002931 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002932 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002933 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2934
Åsa Persson30ab0152019-08-27 12:22:33 +02002935 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2936 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002937 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002938 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2939 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002940 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2941
2942 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002943 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002944 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002945 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002946
Åsa Persson30ab0152019-08-27 12:22:33 +02002947 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002948 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002949 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002950 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002951 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002952 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2953
2954 video_stream_encoder_->Stop();
2955}
2956
Åsa Perssonccfb3402019-09-25 15:13:04 +02002957TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002958 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2959 test::ScopedFieldTrials field_trials(
2960 "WebRTC-Video-BalancedDegradationSettings/"
2961 "pixels:57600|129600|230400,fps:7|24|24/");
2962 SetupTest();
2963 OnBitrateUpdated(kLowTargetBitrateBps);
2964
2965 VerifyNoLimitation(source_.sink_wants());
2966
2967 // Insert frame, expect scaled down:
2968 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2969 InsertFrame();
2970 EXPECT_FALSE(WaitForFrame(1000));
2971 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2972 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2973
2974 // Insert frame, expect scaled down:
2975 // resolution (320x180@24fps).
2976 InsertFrame();
2977 EXPECT_FALSE(WaitForFrame(1000));
2978 EXPECT_LT(source_.sink_wants().max_pixel_count,
2979 source_.last_wants().max_pixel_count);
2980 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2981
2982 // Frame should not be dropped (min pixels per frame reached).
2983 InsertFrameAndWaitForEncoded();
2984
2985 video_stream_encoder_->Stop();
2986}
2987
2988TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002989 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002990 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002991 "WebRTC-Video-BalancedDegradationSettings/"
2992 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002993 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002994
Åsa Persson30ab0152019-08-27 12:22:33 +02002995 const int kResolutionMinBitrateBps = 435000;
2996 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002997 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002998
Åsa Persson45b176f2019-09-30 11:19:05 +02002999 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003000 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003001 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3002
3003 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3004 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003005 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003006 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003007 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3008
3009 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3010 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003011 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003012 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003013 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3014
3015 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3016 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003017 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003018 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003019 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3020
Åsa Persson30ab0152019-08-27 12:22:33 +02003021 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3022 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003023 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003024 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003025 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3026
3027 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3028 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003029 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003030 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3031
3032 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003033 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003034 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003035 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003036 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003037 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3038
3039 video_stream_encoder_->Stop();
3040}
3041
Åsa Perssonccfb3402019-09-25 15:13:04 +02003042TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003043 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003044 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003045 "WebRTC-Video-BalancedDegradationSettings/"
3046 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003047 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003048
Åsa Persson30ab0152019-08-27 12:22:33 +02003049 const int kMinBitrateBps = 425000;
3050 const int kTooLowMinBitrateBps = 424000;
3051 const int kResolutionMinBitrateBps = 435000;
3052 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003053 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003054
Åsa Persson45b176f2019-09-30 11:19:05 +02003055 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003056 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003057 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3058
3059 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3060 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003061 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003062 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003063 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3064
3065 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3066 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003067 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003068 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003069 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3070
3071 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3072 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003073 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003074 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003075 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3076
3077 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3078 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003079 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003080 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3081
3082 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003083 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003084 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003085 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003086 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003087 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3088
3089 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003090 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003091 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003092 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003093 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3094
3095 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003096 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003097 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003098 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003099 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003100 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3101
Åsa Persson1b247f12019-08-14 17:26:39 +02003102 video_stream_encoder_->Stop();
3103}
3104
mflodmancc3d4422017-08-03 08:27:51 -07003105TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003106 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3107 const int kWidth = 1280;
3108 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003109 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003110 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3111 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003112
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003113 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003114 AdaptingFrameForwarder source;
3115 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003116 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003117 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003118
Åsa Persson8c1bf952018-09-13 10:42:19 +02003119 int64_t timestamp_ms = kFrameIntervalMs;
3120 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003121 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003122 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003123 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3124 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3125 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3126 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3127
3128 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003129 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003130 timestamp_ms += kFrameIntervalMs;
3131 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3132 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003133 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003134 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3135 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3136 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3137 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3138
3139 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003140 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003141 timestamp_ms += kFrameIntervalMs;
3142 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3143 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003144 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003145 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3146 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3147 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3148 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3149
Jonathan Yubc771b72017-12-08 17:04:29 -08003150 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003151 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003152 timestamp_ms += kFrameIntervalMs;
3153 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3154 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003155 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003156 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3157 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003158 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003159 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3160
Jonathan Yubc771b72017-12-08 17:04:29 -08003161 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003162 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003163 timestamp_ms += kFrameIntervalMs;
3164 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3165 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003166 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003167 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003168 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3169 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3170 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3171 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3172
Jonathan Yubc771b72017-12-08 17:04:29 -08003173 // Trigger quality adapt down, expect no change (min resolution reached).
3174 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003175 timestamp_ms += kFrameIntervalMs;
3176 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3177 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003178 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3179 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3180 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3181 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3182 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3183
3184 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003185 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003186 timestamp_ms += kFrameIntervalMs;
3187 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3188 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003189 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003190 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3191 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3192 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3193 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3194
3195 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3196 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003197 timestamp_ms += kFrameIntervalMs;
3198 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3199 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003200 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3201 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3202 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3203 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3204 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3205
3206 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3207 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003208 timestamp_ms += kFrameIntervalMs;
3209 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3210 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003211 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003212 last_wants = source.sink_wants();
3213 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3214 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003215 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003216 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3217
3218 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003219 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003220 timestamp_ms += kFrameIntervalMs;
3221 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3222 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003223 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003224 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3225 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003226 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003227 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3228
3229 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003230 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003231 timestamp_ms += kFrameIntervalMs;
3232 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003233 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003234 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003235 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003236 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3237 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003238 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003239 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003240
mflodmancc3d4422017-08-03 08:27:51 -07003241 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003242}
3243
mflodmancc3d4422017-08-03 08:27:51 -07003244TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003245 const int kWidth = 640;
3246 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003247
Erik Språng4c6ca302019-04-08 15:14:01 +02003248 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003249 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3250 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003251
perkj803d97f2016-11-01 11:45:46 -07003252 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003253 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003254 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003255 }
3256
mflodmancc3d4422017-08-03 08:27:51 -07003257 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003258 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003259 video_source_.IncomingCapturedFrame(CreateFrame(
3260 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003261 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003262 }
3263
mflodmancc3d4422017-08-03 08:27:51 -07003264 video_stream_encoder_->Stop();
3265 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003266 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003267
Ying Wangef3998f2019-12-09 13:06:53 +01003268 EXPECT_METRIC_EQ(
3269 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3270 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003271 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3272}
3273
mflodmancc3d4422017-08-03 08:27:51 -07003274TEST_F(VideoStreamEncoderTest,
3275 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003276 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003277 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3278 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003279 const int kWidth = 640;
3280 const int kHeight = 360;
3281
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003282 video_stream_encoder_->SetSource(&video_source_,
3283 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003284
3285 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3286 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003287 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003288 }
3289
mflodmancc3d4422017-08-03 08:27:51 -07003290 video_stream_encoder_->Stop();
3291 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003292 stats_proxy_.reset();
3293
3294 EXPECT_EQ(0,
3295 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3296}
3297
mflodmancc3d4422017-08-03 08:27:51 -07003298TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003299 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003300 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003301
3302 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003303 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003304 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003305 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3306 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003307
sprang57c2fff2017-01-16 06:24:02 -08003308 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003309 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003310 video_stream_encoder_->OnBitrateUpdated(
3311 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3312 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003313
sprang57c2fff2017-01-16 06:24:02 -08003314 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003315 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3316 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003317 VideoBitrateAllocation bitrate_allocation =
3318 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003319 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003320 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003321 // TODO(srte): The use of millisecs here looks like an error, but the tests
3322 // fails using seconds, this should be investigated.
3323 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003324
3325 // Not called on second frame.
3326 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3327 .Times(0);
3328 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003329 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3330 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003331 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003332
3333 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003334 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3335 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003336 const int64_t start_time_ms = rtc::TimeMillis();
3337 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3338 video_source_.IncomingCapturedFrame(
3339 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3340 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003341 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003342 }
3343
3344 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003345 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003346
mflodmancc3d4422017-08-03 08:27:51 -07003347 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003348}
3349
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003350TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3351 // 2 TLs configured, temporal layers supported by encoder.
3352 const int kNumTemporalLayers = 2;
3353 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3354 fake_encoder_.SetTemporalLayersSupported(0, true);
3355
3356 // Bitrate allocated across temporal layers.
3357 const int kTl0Bps = kTargetBitrateBps *
3358 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003359 kNumTemporalLayers, /*temporal_id*/ 0,
3360 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003361 const int kTl1Bps = kTargetBitrateBps *
3362 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003363 kNumTemporalLayers, /*temporal_id*/ 1,
3364 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003365 VideoBitrateAllocation expected_bitrate;
3366 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3367 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3368
3369 VerifyAllocatedBitrate(expected_bitrate);
3370 video_stream_encoder_->Stop();
3371}
3372
3373TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3374 // 2 TLs configured, temporal layers not supported by encoder.
3375 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3376 fake_encoder_.SetTemporalLayersSupported(0, false);
3377
3378 // Temporal layers not supported by the encoder.
3379 // Total bitrate should be at ti:0.
3380 VideoBitrateAllocation expected_bitrate;
3381 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3382
3383 VerifyAllocatedBitrate(expected_bitrate);
3384 video_stream_encoder_->Stop();
3385}
3386
3387TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3388 // 2 TLs configured, temporal layers only supported for first stream.
3389 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3390 fake_encoder_.SetTemporalLayersSupported(0, true);
3391 fake_encoder_.SetTemporalLayersSupported(1, false);
3392
3393 const int kS0Bps = 150000;
3394 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003395 kS0Bps *
3396 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3397 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003398 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003399 kS0Bps *
3400 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3401 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003402 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3403 // Temporal layers not supported by si:1.
3404 VideoBitrateAllocation expected_bitrate;
3405 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3406 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3407 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3408
3409 VerifyAllocatedBitrate(expected_bitrate);
3410 video_stream_encoder_->Stop();
3411}
3412
Niels Möller7dc26b72017-12-06 10:27:48 +01003413TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3414 const int kFrameWidth = 1280;
3415 const int kFrameHeight = 720;
3416 const int kFramerate = 24;
3417
Erik Språng4c6ca302019-04-08 15:14:01 +02003418 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003419 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3420 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003421 test::FrameForwarder source;
3422 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003423 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003424
3425 // Insert a single frame, triggering initial configuration.
3426 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3427 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3428
3429 EXPECT_EQ(
3430 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3431 kDefaultFramerate);
3432
3433 // Trigger reconfigure encoder (without resetting the entire instance).
3434 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003435 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003436 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3437 video_encoder_config.number_of_streams = 1;
3438 video_encoder_config.video_stream_factory =
3439 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3440 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003441 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003442 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3443
3444 // Detector should be updated with fps limit from codec config.
3445 EXPECT_EQ(
3446 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3447 kFramerate);
3448
3449 // Trigger overuse, max framerate should be reduced.
3450 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3451 stats.input_frame_rate = kFramerate;
3452 stats_proxy_->SetMockStats(stats);
3453 video_stream_encoder_->TriggerCpuOveruse();
3454 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3455 int adapted_framerate =
3456 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3457 EXPECT_LT(adapted_framerate, kFramerate);
3458
3459 // Trigger underuse, max framerate should go back to codec configured fps.
3460 // Set extra low fps, to make sure it's actually reset, not just incremented.
3461 stats = stats_proxy_->GetStats();
3462 stats.input_frame_rate = adapted_framerate / 2;
3463 stats_proxy_->SetMockStats(stats);
3464 video_stream_encoder_->TriggerCpuNormalUsage();
3465 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3466 EXPECT_EQ(
3467 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3468 kFramerate);
3469
3470 video_stream_encoder_->Stop();
3471}
3472
3473TEST_F(VideoStreamEncoderTest,
3474 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3475 const int kFrameWidth = 1280;
3476 const int kFrameHeight = 720;
3477 const int kLowFramerate = 15;
3478 const int kHighFramerate = 25;
3479
Erik Språng4c6ca302019-04-08 15:14:01 +02003480 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003481 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3482 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003483 test::FrameForwarder source;
3484 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003485 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003486
3487 // Trigger initial configuration.
3488 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003489 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003490 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3491 video_encoder_config.number_of_streams = 1;
3492 video_encoder_config.video_stream_factory =
3493 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3494 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3495 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003496 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003497 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3498
3499 EXPECT_EQ(
3500 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3501 kLowFramerate);
3502
3503 // Trigger overuse, max framerate should be reduced.
3504 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3505 stats.input_frame_rate = kLowFramerate;
3506 stats_proxy_->SetMockStats(stats);
3507 video_stream_encoder_->TriggerCpuOveruse();
3508 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3509 int adapted_framerate =
3510 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3511 EXPECT_LT(adapted_framerate, kLowFramerate);
3512
3513 // Reconfigure the encoder with a new (higher max framerate), max fps should
3514 // still respect the adaptation.
3515 video_encoder_config.video_stream_factory =
3516 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3517 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3518 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003519 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003520 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3521
3522 EXPECT_EQ(
3523 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3524 adapted_framerate);
3525
3526 // Trigger underuse, max framerate should go back to codec configured fps.
3527 stats = stats_proxy_->GetStats();
3528 stats.input_frame_rate = adapted_framerate;
3529 stats_proxy_->SetMockStats(stats);
3530 video_stream_encoder_->TriggerCpuNormalUsage();
3531 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3532 EXPECT_EQ(
3533 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3534 kHighFramerate);
3535
3536 video_stream_encoder_->Stop();
3537}
3538
mflodmancc3d4422017-08-03 08:27:51 -07003539TEST_F(VideoStreamEncoderTest,
3540 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003541 const int kFrameWidth = 1280;
3542 const int kFrameHeight = 720;
3543 const int kFramerate = 24;
3544
Erik Språng4c6ca302019-04-08 15:14:01 +02003545 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003546 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3547 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003548 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003549 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003550 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003551
3552 // Trigger initial configuration.
3553 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003554 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003555 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3556 video_encoder_config.number_of_streams = 1;
3557 video_encoder_config.video_stream_factory =
3558 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3559 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003560 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003561 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003562 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003563
Niels Möller7dc26b72017-12-06 10:27:48 +01003564 EXPECT_EQ(
3565 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3566 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003567
3568 // Trigger overuse, max framerate should be reduced.
3569 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3570 stats.input_frame_rate = kFramerate;
3571 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003572 video_stream_encoder_->TriggerCpuOveruse();
3573 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003574 int adapted_framerate =
3575 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003576 EXPECT_LT(adapted_framerate, kFramerate);
3577
3578 // Change degradation preference to not enable framerate scaling. Target
3579 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003580 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003581 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003582 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003583 EXPECT_EQ(
3584 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3585 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003586
mflodmancc3d4422017-08-03 08:27:51 -07003587 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003588}
3589
mflodmancc3d4422017-08-03 08:27:51 -07003590TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003591 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003592 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003593 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003594 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003595 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003596 const int kWidth = 640;
3597 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003598
asaperssonfab67072017-04-04 05:51:49 -07003599 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003600
3601 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003602 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003603
3604 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003605 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003606
sprangc5d62e22017-04-02 23:53:04 -07003607 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003608
asaperssonfab67072017-04-04 05:51:49 -07003609 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003610 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003611 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003612
3613 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003614 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003615
sprangc5d62e22017-04-02 23:53:04 -07003616 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003617
mflodmancc3d4422017-08-03 08:27:51 -07003618 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003619}
3620
mflodmancc3d4422017-08-03 08:27:51 -07003621TEST_F(VideoStreamEncoderTest,
3622 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003623 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003624 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003625 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003626 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003627 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003628 const int kWidth = 640;
3629 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003630
3631 // We expect the n initial frames to get dropped.
3632 int i;
3633 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003634 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003635 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003636 }
3637 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003638 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003639 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003640
3641 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003642 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003643
mflodmancc3d4422017-08-03 08:27:51 -07003644 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003645}
3646
mflodmancc3d4422017-08-03 08:27:51 -07003647TEST_F(VideoStreamEncoderTest,
3648 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003649 const int kWidth = 640;
3650 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003651 video_stream_encoder_->OnBitrateUpdated(
3652 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3653 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003654
3655 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003656 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003657 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003658
asaperssonfab67072017-04-04 05:51:49 -07003659 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003660 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003661 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003662
mflodmancc3d4422017-08-03 08:27:51 -07003663 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003664}
3665
mflodmancc3d4422017-08-03 08:27:51 -07003666TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003667 const int kWidth = 640;
3668 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003669 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003670
3671 VideoEncoderConfig video_encoder_config;
3672 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3673 // Make format different, to force recreation of encoder.
3674 video_encoder_config.video_format.parameters["foo"] = "foo";
3675 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003676 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003677 video_stream_encoder_->OnBitrateUpdated(
3678 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3679 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003680
kthelgasonb83797b2017-02-14 11:57:25 -08003681 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003682 video_stream_encoder_->SetSource(&video_source_,
3683 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003684
asaperssonfab67072017-04-04 05:51:49 -07003685 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003686 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003687 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003688
mflodmancc3d4422017-08-03 08:27:51 -07003689 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003690 fake_encoder_.SetQualityScaling(true);
3691}
3692
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003693TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3694 webrtc::test::ScopedFieldTrials field_trials(
3695 "WebRTC-InitialFramedrop/Enabled/");
3696 // Reset encoder for field trials to take effect.
3697 ConfigureEncoder(video_encoder_config_.Copy());
3698 const int kTooLowBitrateForFrameSizeBps = 10000;
3699 const int kWidth = 640;
3700 const int kHeight = 360;
3701
Erik Språng4c6ca302019-04-08 15:14:01 +02003702 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003703 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3704 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003705 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3706 // Frame should not be dropped.
3707 WaitForEncodedFrame(1);
3708
Erik Språng610c7632019-03-06 15:37:33 +01003709 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003710 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003711 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003712 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003713 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3714 // Expect to drop this frame, the wait should time out.
3715 ExpectDroppedFrame();
3716
3717 // Expect the sink_wants to specify a scaled frame.
3718 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3719 video_stream_encoder_->Stop();
3720}
3721
Åsa Persson139f4dc2019-08-02 09:29:58 +02003722TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3723 webrtc::test::ScopedFieldTrials field_trials(
3724 "WebRTC-Video-QualityScalerSettings/"
3725 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3726 // Reset encoder for field trials to take effect.
3727 ConfigureEncoder(video_encoder_config_.Copy());
3728 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3729 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3730 const int kWidth = 640;
3731 const int kHeight = 360;
3732
3733 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003734 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3735 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003736 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3737 // Frame should not be dropped.
3738 WaitForEncodedFrame(1);
3739
3740 video_stream_encoder_->OnBitrateUpdated(
3741 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003742 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003743 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3744 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3745 // Frame should not be dropped.
3746 WaitForEncodedFrame(2);
3747
3748 video_stream_encoder_->OnBitrateUpdated(
3749 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003750 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003751 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3752 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3753 // Expect to drop this frame, the wait should time out.
3754 ExpectDroppedFrame();
3755
3756 // Expect the sink_wants to specify a scaled frame.
3757 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3758 video_stream_encoder_->Stop();
3759}
3760
Åsa Perssone644a032019-11-08 15:56:00 +01003761TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3762 webrtc::test::ScopedFieldTrials field_trials(
3763 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3764
3765 // Reset encoder for field trials to take effect.
3766 VideoEncoderConfig config = video_encoder_config_.Copy();
3767 config.max_bitrate_bps = kTargetBitrateBps;
3768 ConfigureEncoder(std::move(config));
3769 fake_encoder_.SetQp(kQpLow);
3770
3771 // Enable MAINTAIN_FRAMERATE preference.
3772 AdaptingFrameForwarder source;
3773 source.set_adaptation_enabled(true);
3774 video_stream_encoder_->SetSource(&source,
3775 DegradationPreference::MAINTAIN_FRAMERATE);
3776
3777 // Start at low bitrate.
3778 const int kLowBitrateBps = 200000;
3779 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3780 DataRate::bps(kLowBitrateBps),
3781 DataRate::bps(kLowBitrateBps), 0, 0);
3782
3783 // Expect first frame to be dropped and resolution to be limited.
3784 const int kWidth = 1280;
3785 const int kHeight = 720;
3786 const int64_t kFrameIntervalMs = 100;
3787 int64_t timestamp_ms = kFrameIntervalMs;
3788 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3789 ExpectDroppedFrame();
3790 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3791
3792 // Increase bitrate to encoder max.
3793 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3794 DataRate::bps(config.max_bitrate_bps),
3795 DataRate::bps(config.max_bitrate_bps),
3796 0, 0);
3797
3798 // Insert frames and advance |min_duration_ms|.
3799 for (size_t i = 1; i <= 10; i++) {
3800 timestamp_ms += kFrameIntervalMs;
3801 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3802 WaitForEncodedFrame(timestamp_ms);
3803 }
3804 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3805 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3806
3807 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3808
3809 // Insert frame should trigger high BW and release quality limitation.
3810 timestamp_ms += kFrameIntervalMs;
3811 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3812 WaitForEncodedFrame(timestamp_ms);
3813 VerifyFpsMaxResolutionMax(source.sink_wants());
3814
3815 // Frame should not be adapted.
3816 timestamp_ms += kFrameIntervalMs;
3817 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3818 WaitForEncodedFrame(kWidth, kHeight);
3819 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3820
3821 video_stream_encoder_->Stop();
3822}
3823
mflodmancc3d4422017-08-03 08:27:51 -07003824TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003825 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3826 const int kTooSmallWidth = 10;
3827 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003828 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003829 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3830 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003831
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003832 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003833 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003834 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003835 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003836 VerifyNoLimitation(source.sink_wants());
3837 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3838
3839 // Trigger adapt down, too small frame, expect no change.
3840 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003841 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003842 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003843 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003844 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3845 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3846
mflodmancc3d4422017-08-03 08:27:51 -07003847 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003848}
3849
mflodmancc3d4422017-08-03 08:27:51 -07003850TEST_F(VideoStreamEncoderTest,
3851 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003852 const int kTooSmallWidth = 10;
3853 const int kTooSmallHeight = 10;
3854 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003855 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003856 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3857 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003858
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003859 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003860 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003861 video_stream_encoder_->SetSource(&source,
3862 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003863 VerifyNoLimitation(source.sink_wants());
3864 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3865 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3866
3867 // Trigger adapt down, expect limited framerate.
3868 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003869 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003870 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003871 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3872 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3873 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3874 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3875
3876 // Trigger adapt down, too small frame, expect no change.
3877 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003878 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003879 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003880 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3881 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3882 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3883 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3884
mflodmancc3d4422017-08-03 08:27:51 -07003885 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003886}
3887
mflodmancc3d4422017-08-03 08:27:51 -07003888TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003889 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003890 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003891 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3892 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003893 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003894 const int kFrameWidth = 1280;
3895 const int kFrameHeight = 720;
3896 video_source_.IncomingCapturedFrame(
3897 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003898 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003899 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003900}
3901
sprangb1ca0732017-02-01 08:38:12 -08003902// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003903TEST_F(VideoStreamEncoderTest,
3904 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003905 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003906 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3907 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003908
3909 const int kFrameWidth = 1280;
3910 const int kFrameHeight = 720;
3911 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003912 // requested by
3913 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003914 video_source_.set_adaptation_enabled(true);
3915
3916 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003917 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003918 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003919
3920 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003921 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003922 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003923 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003924 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003925
asaperssonfab67072017-04-04 05:51:49 -07003926 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003927 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003928 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003929 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003930 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003931
mflodmancc3d4422017-08-03 08:27:51 -07003932 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003933}
sprangfe627f32017-03-29 08:24:59 -07003934
mflodmancc3d4422017-08-03 08:27:51 -07003935TEST_F(VideoStreamEncoderTest,
3936 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003937 const int kFrameWidth = 1280;
3938 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003939
Erik Språng4c6ca302019-04-08 15:14:01 +02003940 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003941 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3942 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003943 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003944 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003945 video_source_.set_adaptation_enabled(true);
3946
sprang4847ae62017-06-27 07:06:52 -07003947 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003948
3949 video_source_.IncomingCapturedFrame(
3950 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003951 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003952
3953 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003954 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003955
3956 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003957 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003958 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003959 video_source_.IncomingCapturedFrame(
3960 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003961 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003962 }
3963
3964 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003965 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003966 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003967 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003968 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003969 video_source_.IncomingCapturedFrame(
3970 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003971 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003972 ++num_frames_dropped;
3973 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003974 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003975 }
3976 }
3977
sprang4847ae62017-06-27 07:06:52 -07003978 // Add some slack to account for frames dropped by the frame dropper.
3979 const int kErrorMargin = 1;
3980 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003981 kErrorMargin);
3982
3983 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003984 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003985 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003986 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003987 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003988 video_source_.IncomingCapturedFrame(
3989 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003990 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003991 ++num_frames_dropped;
3992 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003993 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003994 }
3995 }
sprang4847ae62017-06-27 07:06:52 -07003996 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003997 kErrorMargin);
3998
3999 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004000 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004001 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004002 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004003 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004004 video_source_.IncomingCapturedFrame(
4005 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004006 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004007 ++num_frames_dropped;
4008 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004009 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004010 }
4011 }
sprang4847ae62017-06-27 07:06:52 -07004012 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004013 kErrorMargin);
4014
4015 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004016 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004017 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004018 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004019 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004020 video_source_.IncomingCapturedFrame(
4021 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004022 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004023 ++num_frames_dropped;
4024 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004025 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004026 }
4027 }
4028 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4029
mflodmancc3d4422017-08-03 08:27:51 -07004030 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004031}
4032
mflodmancc3d4422017-08-03 08:27:51 -07004033TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004034 const int kFramerateFps = 5;
4035 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004036 const int kFrameWidth = 1280;
4037 const int kFrameHeight = 720;
4038
sprang4847ae62017-06-27 07:06:52 -07004039 // Reconfigure encoder with two temporal layers and screensharing, which will
4040 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004041 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004042
Erik Språng4c6ca302019-04-08 15:14:01 +02004043 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004044 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4045 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004046 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004047 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004048 video_source_.set_adaptation_enabled(true);
4049
sprang4847ae62017-06-27 07:06:52 -07004050 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004051
4052 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004053 rtc::VideoSinkWants last_wants;
4054 do {
4055 last_wants = video_source_.sink_wants();
4056
sprangc5d62e22017-04-02 23:53:04 -07004057 // Insert frames to get a new fps estimate...
4058 for (int j = 0; j < kFramerateFps; ++j) {
4059 video_source_.IncomingCapturedFrame(
4060 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004061 if (video_source_.last_sent_width()) {
4062 sink_.WaitForEncodedFrame(timestamp_ms);
4063 }
sprangc5d62e22017-04-02 23:53:04 -07004064 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004065 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004066 }
4067 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004068 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004069 } while (video_source_.sink_wants().max_framerate_fps <
4070 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004071
Jonathan Yubc771b72017-12-08 17:04:29 -08004072 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004073
mflodmancc3d4422017-08-03 08:27:51 -07004074 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004075}
asaperssonf7e294d2017-06-13 23:25:22 -07004076
mflodmancc3d4422017-08-03 08:27:51 -07004077TEST_F(VideoStreamEncoderTest,
4078 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004079 const int kWidth = 1280;
4080 const int kHeight = 720;
4081 const int64_t kFrameIntervalMs = 150;
4082 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004083 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004084 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4085 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004086
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004087 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004088 AdaptingFrameForwarder source;
4089 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004090 video_stream_encoder_->SetSource(&source,
4091 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004092 timestamp_ms += kFrameIntervalMs;
4093 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004094 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004095 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004096 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4097 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4098 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4099
4100 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004101 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004102 timestamp_ms += kFrameIntervalMs;
4103 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004104 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004105 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4106 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4107 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4108 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4109
4110 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004111 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004112 timestamp_ms += kFrameIntervalMs;
4113 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004114 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004115 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4116 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4117 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4118 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4119
4120 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004121 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004122 timestamp_ms += kFrameIntervalMs;
4123 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004124 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004125 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4126 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4127 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4128 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4129
4130 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004131 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004132 timestamp_ms += kFrameIntervalMs;
4133 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004134 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004135 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4136 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4137 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4138 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4139
4140 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004141 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004142 timestamp_ms += kFrameIntervalMs;
4143 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004144 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004145 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4146 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4147 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4148 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4149
4150 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004151 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004152 timestamp_ms += kFrameIntervalMs;
4153 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004154 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004155 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4156 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4157 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4158 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4159
4160 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004161 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004162 timestamp_ms += kFrameIntervalMs;
4163 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004164 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004165 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4166 rtc::VideoSinkWants last_wants = source.sink_wants();
4167 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4168 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4169 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4170
4171 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004172 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004173 timestamp_ms += kFrameIntervalMs;
4174 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004175 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004176 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4177 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4178 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4179 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4180
4181 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004182 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004183 timestamp_ms += kFrameIntervalMs;
4184 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004185 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004186 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4187 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4188 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4189 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4190
4191 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004192 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004193 timestamp_ms += kFrameIntervalMs;
4194 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004195 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004196 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4197 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4198 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4199 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4200
4201 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004202 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004203 timestamp_ms += kFrameIntervalMs;
4204 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004205 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004206 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4207 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4208 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4209 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4210
4211 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004212 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004213 timestamp_ms += kFrameIntervalMs;
4214 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004215 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004216 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4217 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4218 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4219 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4220
4221 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004222 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004223 timestamp_ms += kFrameIntervalMs;
4224 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004225 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004226 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4227 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4228 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4229 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4230
4231 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004232 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004233 timestamp_ms += kFrameIntervalMs;
4234 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004235 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004236 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4237 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4238 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4239 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4240
Åsa Persson30ab0152019-08-27 12:22:33 +02004241 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004242 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004243 timestamp_ms += kFrameIntervalMs;
4244 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004245 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004246 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004247 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004248 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4249 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4250 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4251
4252 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004253 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004254 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004255 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4256
mflodmancc3d4422017-08-03 08:27:51 -07004257 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004258}
4259
mflodmancc3d4422017-08-03 08:27:51 -07004260TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004261 const int kWidth = 1280;
4262 const int kHeight = 720;
4263 const int64_t kFrameIntervalMs = 150;
4264 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004265 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004266 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4267 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004268
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004269 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004270 AdaptingFrameForwarder source;
4271 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004272 video_stream_encoder_->SetSource(&source,
4273 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004274 timestamp_ms += kFrameIntervalMs;
4275 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004276 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004277 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004278 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4279 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4280 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4281 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4282 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4283 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4284
4285 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004286 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004287 timestamp_ms += kFrameIntervalMs;
4288 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004289 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004290 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4291 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4292 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4293 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4294 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4295 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4296 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4297
4298 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004299 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004300 timestamp_ms += kFrameIntervalMs;
4301 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004302 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004303 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4304 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4305 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4306 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4307 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4308 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4309 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4310
4311 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004312 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004313 timestamp_ms += kFrameIntervalMs;
4314 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004315 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004316 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4317 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4318 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4319 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4320 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4321 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4322 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4323
4324 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004325 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004326 timestamp_ms += kFrameIntervalMs;
4327 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004328 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004329 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4330 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4331 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4332 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4333 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4334 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4335 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4336
4337 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004338 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004339 timestamp_ms += kFrameIntervalMs;
4340 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004341 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004342 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4343 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4344 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4345 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4346 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4347 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4348 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4349
4350 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004351 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004352 timestamp_ms += kFrameIntervalMs;
4353 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004354 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004355 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004356 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004357 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4358 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4359 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4360 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4361 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4362 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4363
4364 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004365 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004366 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004367 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4368 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4369
mflodmancc3d4422017-08-03 08:27:51 -07004370 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004371}
4372
mflodmancc3d4422017-08-03 08:27:51 -07004373TEST_F(VideoStreamEncoderTest,
4374 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004375 const int kWidth = 640;
4376 const int kHeight = 360;
4377 const int kFpsLimit = 15;
4378 const int64_t kFrameIntervalMs = 150;
4379 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004380 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004381 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4382 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004383
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004384 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004385 AdaptingFrameForwarder source;
4386 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004387 video_stream_encoder_->SetSource(&source,
4388 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004389 timestamp_ms += kFrameIntervalMs;
4390 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004391 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004392 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004393 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4394 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4395 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4396 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4397 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4398 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4399
4400 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004401 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004402 timestamp_ms += kFrameIntervalMs;
4403 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004404 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004405 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4406 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4407 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4408 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4409 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4410 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4411 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4412
4413 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004414 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004415 timestamp_ms += kFrameIntervalMs;
4416 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004417 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004418 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4419 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4420 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4421 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4422 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4423 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4424 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4425
4426 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004427 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004428 timestamp_ms += kFrameIntervalMs;
4429 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004430 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004431 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4432 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4433 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4434 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4435 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4436 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4437 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4438
4439 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004440 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004441 timestamp_ms += kFrameIntervalMs;
4442 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004443 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004444 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004445 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4446 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4447 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4448 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4449 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4450 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4451
4452 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004453 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004454 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004455 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4456 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4457
mflodmancc3d4422017-08-03 08:27:51 -07004458 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004459}
4460
mflodmancc3d4422017-08-03 08:27:51 -07004461TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004462 const int kFrameWidth = 1920;
4463 const int kFrameHeight = 1080;
4464 // 3/4 of 1920.
4465 const int kAdaptedFrameWidth = 1440;
4466 // 3/4 of 1080 rounded down to multiple of 4.
4467 const int kAdaptedFrameHeight = 808;
4468 const int kFramerate = 24;
4469
Erik Språng4c6ca302019-04-08 15:14:01 +02004470 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004471 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4472 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004473 // Trigger reconfigure encoder (without resetting the entire instance).
4474 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004475 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004476 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4477 video_encoder_config.number_of_streams = 1;
4478 video_encoder_config.video_stream_factory =
4479 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004480 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004481 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004482 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004483
4484 video_source_.set_adaptation_enabled(true);
4485
4486 video_source_.IncomingCapturedFrame(
4487 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004488 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004489
4490 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004491 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004492 video_source_.IncomingCapturedFrame(
4493 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004494 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004495
mflodmancc3d4422017-08-03 08:27:51 -07004496 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004497}
4498
mflodmancc3d4422017-08-03 08:27:51 -07004499TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004500 const int kFrameWidth = 1280;
4501 const int kFrameHeight = 720;
4502 const int kLowFps = 2;
4503 const int kHighFps = 30;
4504
Erik Språng4c6ca302019-04-08 15:14:01 +02004505 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004506 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4507 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004508
4509 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4510 max_framerate_ = kLowFps;
4511
4512 // Insert 2 seconds of 2fps video.
4513 for (int i = 0; i < kLowFps * 2; ++i) {
4514 video_source_.IncomingCapturedFrame(
4515 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4516 WaitForEncodedFrame(timestamp_ms);
4517 timestamp_ms += 1000 / kLowFps;
4518 }
4519
4520 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004521 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004522 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4523 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004524 video_source_.IncomingCapturedFrame(
4525 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4526 WaitForEncodedFrame(timestamp_ms);
4527 timestamp_ms += 1000 / kLowFps;
4528
4529 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4530
4531 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004532 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004533 const int kFrameIntervalMs = 1000 / kHighFps;
4534 max_framerate_ = kHighFps;
4535 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4536 video_source_.IncomingCapturedFrame(
4537 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4538 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4539 // be dropped if the encoder hans't been updated with the new higher target
4540 // framerate yet, causing it to overshoot the target bitrate and then
4541 // suffering the wrath of the media optimizer.
4542 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4543 timestamp_ms += kFrameIntervalMs;
4544 }
4545
4546 // Don expect correct measurement just yet, but it should be higher than
4547 // before.
4548 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4549
mflodmancc3d4422017-08-03 08:27:51 -07004550 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004551}
4552
mflodmancc3d4422017-08-03 08:27:51 -07004553TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004554 const int kFrameWidth = 1280;
4555 const int kFrameHeight = 720;
4556 const int kTargetBitrateBps = 1000000;
4557
4558 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004559 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004560 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004561 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4562 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004563 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004564
4565 // Insert a first video frame, causes another bitrate update.
4566 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4567 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4568 video_source_.IncomingCapturedFrame(
4569 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4570 WaitForEncodedFrame(timestamp_ms);
4571
4572 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004573 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4574 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004575
4576 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004577 timestamp_ms += kProcessIntervalMs;
4578 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004579
4580 // Bitrate observer should not be called.
4581 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4582 video_source_.IncomingCapturedFrame(
4583 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4584 ExpectDroppedFrame();
4585
mflodmancc3d4422017-08-03 08:27:51 -07004586 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004587}
ilnik6b826ef2017-06-16 06:53:48 -07004588
Niels Möller4db138e2018-04-19 09:04:13 +02004589TEST_F(VideoStreamEncoderTest,
4590 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4591 const int kFrameWidth = 1280;
4592 const int kFrameHeight = 720;
4593 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004594 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004595 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4596 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004597 video_source_.IncomingCapturedFrame(
4598 CreateFrame(1, kFrameWidth, kFrameHeight));
4599 WaitForEncodedFrame(1);
4600 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4601 .low_encode_usage_threshold_percent,
4602 default_options.low_encode_usage_threshold_percent);
4603 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4604 .high_encode_usage_threshold_percent,
4605 default_options.high_encode_usage_threshold_percent);
4606 video_stream_encoder_->Stop();
4607}
4608
4609TEST_F(VideoStreamEncoderTest,
4610 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4611 const int kFrameWidth = 1280;
4612 const int kFrameHeight = 720;
4613 CpuOveruseOptions hardware_options;
4614 hardware_options.low_encode_usage_threshold_percent = 150;
4615 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004616 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004617
Erik Språng4c6ca302019-04-08 15:14:01 +02004618 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004619 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4620 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004621 video_source_.IncomingCapturedFrame(
4622 CreateFrame(1, kFrameWidth, kFrameHeight));
4623 WaitForEncodedFrame(1);
4624 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4625 .low_encode_usage_threshold_percent,
4626 hardware_options.low_encode_usage_threshold_percent);
4627 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4628 .high_encode_usage_threshold_percent,
4629 hardware_options.high_encode_usage_threshold_percent);
4630 video_stream_encoder_->Stop();
4631}
4632
Niels Möller6bb5ab92019-01-11 11:11:10 +01004633TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4634 const int kFrameWidth = 320;
4635 const int kFrameHeight = 240;
4636 const int kFps = 30;
4637 const int kTargetBitrateBps = 120000;
4638 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4639
Erik Språng4c6ca302019-04-08 15:14:01 +02004640 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004641 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4642 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004643
4644 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4645 max_framerate_ = kFps;
4646
4647 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4648 fake_encoder_.SimulateOvershoot(1.0);
4649 int num_dropped = 0;
4650 for (int i = 0; i < kNumFramesInRun; ++i) {
4651 video_source_.IncomingCapturedFrame(
4652 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4653 // Wait up to two frame durations for a frame to arrive.
4654 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4655 ++num_dropped;
4656 }
4657 timestamp_ms += 1000 / kFps;
4658 }
4659
Erik Språnga8d48ab2019-02-08 14:17:40 +01004660 // Framerate should be measured to be near the expected target rate.
4661 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4662
4663 // Frame drops should be within 5% of expected 0%.
4664 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004665
4666 // Make encoder produce frames at double the expected bitrate during 3 seconds
4667 // of video, verify number of drops. Rate needs to be slightly changed in
4668 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004669 double overshoot_factor = 2.0;
4670 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4671 // With bitrate adjuster, when need to overshoot even more to trigger
4672 // frame dropping.
4673 overshoot_factor *= 2;
4674 }
4675 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004676 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004677 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004678 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004679 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004680 num_dropped = 0;
4681 for (int i = 0; i < kNumFramesInRun; ++i) {
4682 video_source_.IncomingCapturedFrame(
4683 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4684 // Wait up to two frame durations for a frame to arrive.
4685 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4686 ++num_dropped;
4687 }
4688 timestamp_ms += 1000 / kFps;
4689 }
4690
Erik Språng4c6ca302019-04-08 15:14:01 +02004691 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004692 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4693 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004694
4695 // Target framerate should be still be near the expected target, despite
4696 // the frame drops.
4697 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4698
4699 // Frame drops should be within 5% of expected 50%.
4700 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004701
4702 video_stream_encoder_->Stop();
4703}
4704
4705TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4706 const int kFrameWidth = 320;
4707 const int kFrameHeight = 240;
4708 const int kActualInputFps = 24;
4709 const int kTargetBitrateBps = 120000;
4710
4711 ASSERT_GT(max_framerate_, kActualInputFps);
4712
4713 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4714 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004715 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004716 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4717 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004718
4719 // Insert 3 seconds of video, with an input fps lower than configured max.
4720 for (int i = 0; i < kActualInputFps * 3; ++i) {
4721 video_source_.IncomingCapturedFrame(
4722 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4723 // Wait up to two frame durations for a frame to arrive.
4724 WaitForEncodedFrame(timestamp_ms);
4725 timestamp_ms += 1000 / kActualInputFps;
4726 }
4727
4728 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4729
4730 video_stream_encoder_->Stop();
4731}
4732
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004733TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4734 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004735 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004736 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4737 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004738
4739 fake_encoder_.BlockNextEncode();
4740 video_source_.IncomingCapturedFrame(
4741 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4742 WaitForEncodedFrame(1);
4743 // On the very first frame full update should be forced.
4744 rect = fake_encoder_.GetLastUpdateRect();
4745 EXPECT_EQ(rect.offset_x, 0);
4746 EXPECT_EQ(rect.offset_y, 0);
4747 EXPECT_EQ(rect.height, codec_height_);
4748 EXPECT_EQ(rect.width, codec_width_);
4749 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4750 // call to ContinueEncode.
4751 video_source_.IncomingCapturedFrame(
4752 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4753 ExpectDroppedFrame();
4754 video_source_.IncomingCapturedFrame(
4755 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4756 ExpectDroppedFrame();
4757 fake_encoder_.ContinueEncode();
4758 WaitForEncodedFrame(3);
4759 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4760 rect = fake_encoder_.GetLastUpdateRect();
4761 EXPECT_EQ(rect.offset_x, 1);
4762 EXPECT_EQ(rect.offset_y, 0);
4763 EXPECT_EQ(rect.width, 10);
4764 EXPECT_EQ(rect.height, 1);
4765
4766 video_source_.IncomingCapturedFrame(
4767 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4768 WaitForEncodedFrame(4);
4769 // Previous frame was encoded, so no accumulation should happen.
4770 rect = fake_encoder_.GetLastUpdateRect();
4771 EXPECT_EQ(rect.offset_x, 0);
4772 EXPECT_EQ(rect.offset_y, 0);
4773 EXPECT_EQ(rect.width, 1);
4774 EXPECT_EQ(rect.height, 1);
4775
4776 video_stream_encoder_->Stop();
4777}
4778
Erik Språngd7329ca2019-02-21 21:19:53 +01004779TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004780 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004781 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4782 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004783
4784 // First frame is always keyframe.
4785 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4786 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004787 EXPECT_THAT(
4788 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004789 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004790
4791 // Insert delta frame.
4792 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4793 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004794 EXPECT_THAT(
4795 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004796 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004797
4798 // Request next frame be a key-frame.
4799 video_stream_encoder_->SendKeyFrame();
4800 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4801 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004802 EXPECT_THAT(
4803 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004804 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004805
4806 video_stream_encoder_->Stop();
4807}
4808
4809TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4810 // Setup simulcast with three streams.
4811 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004812 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004813 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004814 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004815 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004816 // Wait for all three layers before triggering event.
4817 sink_.SetNumExpectedLayers(3);
4818
4819 // First frame is always keyframe.
4820 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4821 WaitForEncodedFrame(1);
4822 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004823 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4824 VideoFrameType::kVideoFrameKey,
4825 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004826
4827 // Insert delta frame.
4828 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4829 WaitForEncodedFrame(2);
4830 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004831 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4832 VideoFrameType::kVideoFrameDelta,
4833 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004834
4835 // Request next frame be a key-frame.
4836 // Only first stream is configured to produce key-frame.
4837 video_stream_encoder_->SendKeyFrame();
4838 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4839 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004840
4841 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4842 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004843 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004844 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004845 VideoFrameType::kVideoFrameKey,
4846 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004847
4848 video_stream_encoder_->Stop();
4849}
4850
4851TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4852 // Configure internal source factory and setup test again.
4853 encoder_factory_.SetHasInternalSource(true);
4854 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004855 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004856 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4857 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004858
4859 // Call encoder directly, simulating internal source where encoded frame
4860 // callback in VideoStreamEncoder is called despite no OnFrame().
4861 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4862 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004863 EXPECT_THAT(
4864 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004865 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004866
Niels Möller8f7ce222019-03-21 15:43:58 +01004867 const std::vector<VideoFrameType> kDeltaFrame = {
4868 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004869 // Need to set timestamp manually since manually for injected frame.
4870 VideoFrame frame = CreateFrame(101, nullptr);
4871 frame.set_timestamp(101);
4872 fake_encoder_.InjectFrame(frame, false);
4873 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004874 EXPECT_THAT(
4875 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004876 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004877
4878 // Request key-frame. The forces a dummy frame down into the encoder.
4879 fake_encoder_.ExpectNullFrame();
4880 video_stream_encoder_->SendKeyFrame();
4881 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004882 EXPECT_THAT(
4883 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004884 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004885
4886 video_stream_encoder_->Stop();
4887}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004888
4889TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4890 // Configure internal source factory and setup test again.
4891 encoder_factory_.SetHasInternalSource(true);
4892 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004893 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004894 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4895 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004896
4897 int64_t timestamp = 1;
4898 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004899 image.SetEncodedData(
4900 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004901 image.capture_time_ms_ = ++timestamp;
4902 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4903 const int64_t kEncodeFinishDelayMs = 10;
4904 image.timing_.encode_start_ms = timestamp;
4905 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4906 fake_encoder_.InjectEncodedImage(image);
4907 // Wait for frame without incrementing clock.
4908 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4909 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4910 // capture timestamp should be kEncodeFinishDelayMs in the past.
4911 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4912 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4913 kEncodeFinishDelayMs);
4914
4915 video_stream_encoder_->Stop();
4916}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004917
4918TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4919 // Configure internal source factory and setup test again.
4920 encoder_factory_.SetHasInternalSource(true);
4921 ResetEncoder("H264", 1, 1, 1, false);
4922
4923 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4924 image._frameType = VideoFrameType::kVideoFrameKey;
4925
4926 CodecSpecificInfo codec_specific_info;
4927 codec_specific_info.codecType = kVideoCodecH264;
4928
4929 RTPFragmentationHeader fragmentation;
4930 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4931 fragmentation.fragmentationOffset[0] = 4;
4932 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4933
4934 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4935 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4936
4937 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4938 testing::ElementsAreArray(optimal_sps));
4939 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4940 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4941 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4942 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4943
4944 video_stream_encoder_->Stop();
4945}
4946
4947TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4948 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4949 0x00, 0x00, 0x03, 0x03, 0xF4,
4950 0x05, 0x03, 0xC7, 0xC0};
4951
4952 // Configure internal source factory and setup test again.
4953 encoder_factory_.SetHasInternalSource(true);
4954 ResetEncoder("H264", 1, 1, 1, false);
4955
4956 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4957 image._frameType = VideoFrameType::kVideoFrameKey;
4958
4959 CodecSpecificInfo codec_specific_info;
4960 codec_specific_info.codecType = kVideoCodecH264;
4961
4962 RTPFragmentationHeader fragmentation;
4963 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4964 fragmentation.fragmentationOffset[0] = 4;
4965 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4966
4967 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4968 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4969
4970 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4971 testing::ElementsAreArray(optimal_sps));
4972 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4973 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4974 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4975 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4976
4977 video_stream_encoder_->Stop();
4978}
4979
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004980TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4981 const int kFrameWidth = 1280;
4982 const int kFrameHeight = 720;
4983 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4984
4985 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004986 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4987 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004988 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4989
4990 // Insert a first video frame. It should be dropped because of downscale in
4991 // resolution.
4992 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4993 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4994 frame.set_rotation(kVideoRotation_270);
4995 video_source_.IncomingCapturedFrame(frame);
4996
4997 ExpectDroppedFrame();
4998
4999 // Second frame is downscaled.
5000 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5001 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5002 frame.set_rotation(kVideoRotation_90);
5003 video_source_.IncomingCapturedFrame(frame);
5004
5005 WaitForEncodedFrame(timestamp_ms);
5006 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5007
5008 // Insert another frame, also downscaled.
5009 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5010 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5011 frame.set_rotation(kVideoRotation_180);
5012 video_source_.IncomingCapturedFrame(frame);
5013
5014 WaitForEncodedFrame(timestamp_ms);
5015 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5016
5017 video_stream_encoder_->Stop();
5018}
5019
Erik Språng5056af02019-09-02 15:53:11 +02005020TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5021 const int kFrameWidth = 320;
5022 const int kFrameHeight = 180;
5023
5024 // Initial rate.
5025 video_stream_encoder_->OnBitrateUpdated(
5026 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005027 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005028 /*link_allocation=*/DataRate::kbps(300),
5029 /*fraction_lost=*/0,
5030 /*rtt_ms=*/0);
5031
5032 // Insert a first video frame so that encoder gets configured.
5033 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5034 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5035 frame.set_rotation(kVideoRotation_270);
5036 video_source_.IncomingCapturedFrame(frame);
5037 WaitForEncodedFrame(timestamp_ms);
5038
5039 // Set a target rate below the minimum allowed by the codec settings.
5040 VideoCodec codec_config = fake_encoder_.codec_config();
5041 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5042 DataRate target_rate = min_rate - DataRate::kbps(1);
5043 video_stream_encoder_->OnBitrateUpdated(
5044 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005045 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005046 /*link_allocation=*/target_rate,
5047 /*fraction_lost=*/0,
5048 /*rtt_ms=*/0);
5049 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5050
5051 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5052 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5053 ASSERT_TRUE(rate_settings.has_value());
5054 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5055 EXPECT_EQ(min_rate, allocation_sum);
5056 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5057
5058 video_stream_encoder_->Stop();
5059}
5060
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005061TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005062 video_stream_encoder_->OnBitrateUpdated(
5063 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5064 DataRate::bps(kTargetBitrateBps), 0, 0);
5065 // Capture a frame and wait for it to synchronize with the encoder thread.
5066 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5067 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5068 WaitForEncodedFrame(1);
5069
5070 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5071 ASSERT_TRUE(prev_rate_settings.has_value());
5072 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5073 kDefaultFramerate);
5074
5075 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5076 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5077 timestamp_ms += 1000 / kDefaultFramerate;
5078 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5079 WaitForEncodedFrame(timestamp_ms);
5080 }
5081 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5082 kDefaultFramerate);
5083 // Capture larger frame to trigger a reconfigure.
5084 codec_height_ *= 2;
5085 codec_width_ *= 2;
5086 timestamp_ms += 1000 / kDefaultFramerate;
5087 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5088 WaitForEncodedFrame(timestamp_ms);
5089
5090 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5091 auto current_rate_settings =
5092 fake_encoder_.GetAndResetLastRateControlSettings();
5093 // Ensure we have actually reconfigured twice
5094 // The rate settings should have been set again even though
5095 // they haven't changed.
5096 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005097 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005098
5099 video_stream_encoder_->Stop();
5100}
5101
philipeld9cc8c02019-09-16 14:53:40 +02005102struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5103 MOCK_METHOD0(RequestEncoderFallback, void());
5104 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5105};
5106
5107TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5108 constexpr int kDontCare = 100;
5109
5110 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5111 video_send_config_.encoder_settings.encoder_switch_request_callback =
5112 &switch_callback;
5113 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5114 encoder_config.codec_type = kVideoCodecVP8;
5115 webrtc::test::ScopedFieldTrials field_trial(
5116 "WebRTC-NetworkCondition-EncoderSwitch/"
5117 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5118 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5119
5120 // Reset encoder for new configuration to take effect.
5121 ConfigureEncoder(std::move(encoder_config));
5122
5123 // Send one frame to trigger ReconfigureEncoder.
5124 video_source_.IncomingCapturedFrame(
5125 CreateFrame(kDontCare, kDontCare, kDontCare));
5126
5127 using Config = EncoderSwitchRequestCallback::Config;
5128 EXPECT_CALL(switch_callback,
5129 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5130 Field(&Config::param, "ping"),
5131 Field(&Config::value, "pong"))));
5132
5133 video_stream_encoder_->OnBitrateUpdated(
5134 /*target_bitrate=*/DataRate::kbps(50),
5135 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5136 /*link_allocation=*/DataRate::kbps(kDontCare),
5137 /*fraction_lost=*/0,
5138 /*rtt_ms=*/0);
5139
5140 video_stream_encoder_->Stop();
5141}
5142
5143TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5144 constexpr int kSufficientBitrateToNotDrop = 1000;
5145 constexpr int kHighRes = 500;
5146 constexpr int kLowRes = 100;
5147
5148 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5149 video_send_config_.encoder_settings.encoder_switch_request_callback =
5150 &switch_callback;
5151 webrtc::test::ScopedFieldTrials field_trial(
5152 "WebRTC-NetworkCondition-EncoderSwitch/"
5153 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5154 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5155 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5156 encoder_config.codec_type = kVideoCodecH264;
5157
5158 // Reset encoder for new configuration to take effect.
5159 ConfigureEncoder(std::move(encoder_config));
5160
5161 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5162 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5163 // not fail.
5164 video_stream_encoder_->OnBitrateUpdated(
5165 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5166 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5167 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5168 /*fraction_lost=*/0,
5169 /*rtt_ms=*/0);
5170
5171 // Send one frame to trigger ReconfigureEncoder.
5172 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5173 WaitForEncodedFrame(1);
5174
5175 using Config = EncoderSwitchRequestCallback::Config;
5176 EXPECT_CALL(switch_callback,
5177 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5178 Field(&Config::param, "ping"),
5179 Field(&Config::value, "pong"))));
5180
5181 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5182 WaitForEncodedFrame(2);
5183
5184 video_stream_encoder_->Stop();
5185}
5186
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005187TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005188 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005189 const int kFrameWidth = 320;
5190 const int kFrameHeight = 180;
5191
5192 // Set initial rate.
5193 auto rate = DataRate::kbps(100);
5194 video_stream_encoder_->OnBitrateUpdated(
5195 /*target_bitrate=*/rate,
5196 /*stable_target_bitrate=*/rate,
5197 /*link_allocation=*/rate,
5198 /*fraction_lost=*/0,
5199 /*rtt_ms=*/0);
5200
5201 // Insert a first video frame so that encoder gets configured.
5202 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5203 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5204 frame.set_rotation(kVideoRotation_270);
5205 video_source_.IncomingCapturedFrame(frame);
5206 WaitForEncodedFrame(timestamp_ms);
5207 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5208
5209 // Change of target bitrate propagates to the encoder.
5210 auto new_stable_rate = rate - DataRate::kbps(5);
5211 video_stream_encoder_->OnBitrateUpdated(
5212 /*target_bitrate=*/new_stable_rate,
5213 /*stable_target_bitrate=*/new_stable_rate,
5214 /*link_allocation=*/rate,
5215 /*fraction_lost=*/0,
5216 /*rtt_ms=*/0);
5217 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5218 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5219 video_stream_encoder_->Stop();
5220}
5221
5222TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005223 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005224 const int kFrameWidth = 320;
5225 const int kFrameHeight = 180;
5226
5227 // Set initial rate.
5228 auto rate = DataRate::kbps(100);
5229 video_stream_encoder_->OnBitrateUpdated(
5230 /*target_bitrate=*/rate,
5231 /*stable_target_bitrate=*/rate,
5232 /*link_allocation=*/rate,
5233 /*fraction_lost=*/0,
5234 /*rtt_ms=*/0);
5235
5236 // Insert a first video frame so that encoder gets configured.
5237 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5238 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5239 frame.set_rotation(kVideoRotation_270);
5240 video_source_.IncomingCapturedFrame(frame);
5241 WaitForEncodedFrame(timestamp_ms);
5242 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5243
5244 // Set a higher target rate without changing the link_allocation. Should not
5245 // reset encoder's rate.
5246 auto new_stable_rate = rate - DataRate::kbps(5);
5247 video_stream_encoder_->OnBitrateUpdated(
5248 /*target_bitrate=*/rate,
5249 /*stable_target_bitrate=*/new_stable_rate,
5250 /*link_allocation=*/rate,
5251 /*fraction_lost=*/0,
5252 /*rtt_ms=*/0);
5253 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5254 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5255 video_stream_encoder_->Stop();
5256}
5257
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005258TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5259 test::ScopedFieldTrials field_trials(
5260 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5261 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5262 const int kFramerateFps = 30;
5263 const int kWidth = 1920;
5264 const int kHeight = 1080;
5265 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5266 // Works on screenshare mode.
5267 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5268 // We rely on the automatic resolution adaptation, but we handle framerate
5269 // adaptation manually by mocking the stats proxy.
5270 video_source_.set_adaptation_enabled(true);
5271
5272 // BALANCED degradation preference is required for this feature.
5273 video_stream_encoder_->OnBitrateUpdated(
5274 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5275 DataRate::bps(kTargetBitrateBps), 0, 0);
5276 video_stream_encoder_->SetSource(&video_source_,
5277 webrtc::DegradationPreference::BALANCED);
5278 VerifyNoLimitation(video_source_.sink_wants());
5279
5280 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5281 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5282
5283 // Pass enough frames with the full update to trigger animation detection.
5284 for (int i = 0; i < kNumFrames; ++i) {
5285 int64_t timestamp_ms =
5286 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5287 frame.set_ntp_time_ms(timestamp_ms);
5288 frame.set_timestamp_us(timestamp_ms * 1000);
5289 video_source_.IncomingCapturedFrame(frame);
5290 WaitForEncodedFrame(timestamp_ms);
5291 }
5292
5293 // Resolution should be limited.
5294 rtc::VideoSinkWants expected;
5295 expected.max_framerate_fps = kFramerateFps;
5296 expected.max_pixel_count = 1280 * 720 + 1;
5297 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5298
5299 // Pass one frame with no known update.
5300 // Resolution cap should be removed immediately.
5301 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5302 frame.set_ntp_time_ms(timestamp_ms);
5303 frame.set_timestamp_us(timestamp_ms * 1000);
5304 frame.clear_update_rect();
5305
5306 video_source_.IncomingCapturedFrame(frame);
5307 WaitForEncodedFrame(timestamp_ms);
5308
5309 // Resolution should be unlimited now.
5310 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5311
5312 video_stream_encoder_->Stop();
5313}
5314
perkj26091b12016-09-01 01:17:40 -07005315} // namespace webrtc