blob: d7cf579dba28b53a456500917ba564573497e0d8 [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
Henrik Boströmb08882b2020-01-07 10:11:17 +0100158 void PostTaskAndWait(bool down,
159 AdaptationObserverInterface::AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200160 PostTaskAndWait(down, reason, /*expected_results=*/true);
161 }
162
Henrik Boströmb08882b2020-01-07 10:11:17 +0100163 void PostTaskAndWait(bool down,
164 AdaptationObserverInterface::AdaptReason reason,
165 bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100166 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200167 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200168 if (down)
Henrik Boström7875c992020-02-06 10:35:00 +0100169 EXPECT_EQ(expected_results, OnResourceOveruseForTesting(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200170 else
Henrik Boström7875c992020-02-06 10:35:00 +0100171 OnResourceUnderuseForTesting(reason);
perkj803d97f2016-11-01 11:45:46 -0700172 event.Set();
173 });
perkj070ba852017-02-16 15:46:27 -0800174 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700175 }
176
kthelgason2fc52542017-03-03 00:24:41 -0800177 // This is used as a synchronisation mechanism, to make sure that the
178 // encoder queue is not blocked before we start sending it frames.
179 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100180 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200181 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800182 ASSERT_TRUE(event.Wait(5000));
183 }
184
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200185 void TriggerCpuOveruse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100186 PostTaskAndWait(/*down=*/true,
187 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200188 }
kthelgason876222f2016-11-29 01:44:11 -0800189
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200190 void TriggerCpuNormalUsage() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100191 PostTaskAndWait(/*down=*/false,
192 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200193 }
kthelgason876222f2016-11-29 01:44:11 -0800194
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200195 void TriggerQualityLow() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100196 PostTaskAndWait(/*down=*/true,
197 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200198 }
kthelgason876222f2016-11-29 01:44:11 -0800199
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200200 void TriggerQualityLowExpectFalse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100201 PostTaskAndWait(/*down=*/true,
202 AdaptationObserverInterface::AdaptReason::kQuality,
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200203 /*expected_results=*/false);
204 }
205
206 void TriggerQualityHigh() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100207 PostTaskAndWait(/*down=*/false,
208 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200209 }
sprangfda496a2017-06-15 04:21:07 -0700210
Niels Möller7dc26b72017-12-06 10:27:48 +0100211 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700212};
213
asapersson5f7226f2016-11-25 04:37:00 -0800214class VideoStreamFactory
215 : public VideoEncoderConfig::VideoStreamFactoryInterface {
216 public:
sprangfda496a2017-06-15 04:21:07 -0700217 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
218 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800219 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700220 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800221 }
222
223 private:
224 std::vector<VideoStream> CreateEncoderStreams(
225 int width,
226 int height,
227 const VideoEncoderConfig& encoder_config) override {
228 std::vector<VideoStream> streams =
229 test::CreateVideoStreams(width, height, encoder_config);
230 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100231 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700232 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800233 }
234 return streams;
235 }
sprangfda496a2017-06-15 04:21:07 -0700236
asapersson5f7226f2016-11-25 04:37:00 -0800237 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700238 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800239};
240
Noah Richards51db4212019-06-12 06:59:12 -0700241// Simulates simulcast behavior and makes highest stream resolutions divisible
242// by 4.
243class CroppingVideoStreamFactory
244 : public VideoEncoderConfig::VideoStreamFactoryInterface {
245 public:
246 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
247 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
248 EXPECT_GT(num_temporal_layers, 0u);
249 EXPECT_GT(framerate, 0);
250 }
251
252 private:
253 std::vector<VideoStream> CreateEncoderStreams(
254 int width,
255 int height,
256 const VideoEncoderConfig& encoder_config) override {
257 std::vector<VideoStream> streams = test::CreateVideoStreams(
258 width - width % 4, height - height % 4, encoder_config);
259 for (VideoStream& stream : streams) {
260 stream.num_temporal_layers = num_temporal_layers_;
261 stream.max_framerate = framerate_;
262 }
263 return streams;
264 }
265
266 const size_t num_temporal_layers_;
267 const int framerate_;
268};
269
sprangb1ca0732017-02-01 08:38:12 -0800270class AdaptingFrameForwarder : public test::FrameForwarder {
271 public:
272 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700273 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800274
275 void set_adaptation_enabled(bool enabled) {
276 rtc::CritScope cs(&crit_);
277 adaptation_enabled_ = enabled;
278 }
279
asaperssonfab67072017-04-04 05:51:49 -0700280 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800281 rtc::CritScope cs(&crit_);
282 return adaptation_enabled_;
283 }
284
asapersson09f05612017-05-15 23:40:18 -0700285 rtc::VideoSinkWants last_wants() const {
286 rtc::CritScope cs(&crit_);
287 return last_wants_;
288 }
289
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200290 absl::optional<int> last_sent_width() const { return last_width_; }
291 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800292
sprangb1ca0732017-02-01 08:38:12 -0800293 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
294 int cropped_width = 0;
295 int cropped_height = 0;
296 int out_width = 0;
297 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700298 if (adaption_enabled()) {
299 if (adapter_.AdaptFrameResolution(
300 video_frame.width(), video_frame.height(),
301 video_frame.timestamp_us() * 1000, &cropped_width,
302 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100303 VideoFrame adapted_frame =
304 VideoFrame::Builder()
305 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
306 nullptr, out_width, out_height))
307 .set_timestamp_rtp(99)
308 .set_timestamp_ms(99)
309 .set_rotation(kVideoRotation_0)
310 .build();
sprangc5d62e22017-04-02 23:53:04 -0700311 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100312 if (video_frame.has_update_rect()) {
313 adapted_frame.set_update_rect(
314 video_frame.update_rect().ScaleWithFrame(
315 video_frame.width(), video_frame.height(), 0, 0,
316 video_frame.width(), video_frame.height(), out_width,
317 out_height));
318 }
sprangc5d62e22017-04-02 23:53:04 -0700319 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800320 last_width_.emplace(adapted_frame.width());
321 last_height_.emplace(adapted_frame.height());
322 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200323 last_width_ = absl::nullopt;
324 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700325 }
sprangb1ca0732017-02-01 08:38:12 -0800326 } else {
327 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800328 last_width_.emplace(video_frame.width());
329 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800330 }
331 }
332
333 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
334 const rtc::VideoSinkWants& wants) override {
335 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700336 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100337 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800338 test::FrameForwarder::AddOrUpdateSink(sink, wants);
339 }
sprangb1ca0732017-02-01 08:38:12 -0800340 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700341 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
342 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200343 absl::optional<int> last_width_;
344 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800345};
sprangc5d62e22017-04-02 23:53:04 -0700346
Niels Möller213618e2018-07-24 09:29:58 +0200347// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700348class MockableSendStatisticsProxy : public SendStatisticsProxy {
349 public:
350 MockableSendStatisticsProxy(Clock* clock,
351 const VideoSendStream::Config& config,
352 VideoEncoderConfig::ContentType content_type)
353 : SendStatisticsProxy(clock, config, content_type) {}
354
355 VideoSendStream::Stats GetStats() override {
356 rtc::CritScope cs(&lock_);
357 if (mock_stats_)
358 return *mock_stats_;
359 return SendStatisticsProxy::GetStats();
360 }
361
Niels Möller213618e2018-07-24 09:29:58 +0200362 int GetInputFrameRate() const override {
363 rtc::CritScope cs(&lock_);
364 if (mock_stats_)
365 return mock_stats_->input_frame_rate;
366 return SendStatisticsProxy::GetInputFrameRate();
367 }
sprangc5d62e22017-04-02 23:53:04 -0700368 void SetMockStats(const VideoSendStream::Stats& stats) {
369 rtc::CritScope cs(&lock_);
370 mock_stats_.emplace(stats);
371 }
372
373 void ResetMockStats() {
374 rtc::CritScope cs(&lock_);
375 mock_stats_.reset();
376 }
377
378 private:
379 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200380 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700381};
382
sprang4847ae62017-06-27 07:06:52 -0700383class MockBitrateObserver : public VideoBitrateAllocationObserver {
384 public:
Erik Språng566124a2018-04-23 12:32:22 +0200385 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700386};
387
perkj803d97f2016-11-01 11:45:46 -0700388} // namespace
389
mflodmancc3d4422017-08-03 08:27:51 -0700390class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700391 public:
392 static const int kDefaultTimeoutMs = 30 * 1000;
393
mflodmancc3d4422017-08-03 08:27:51 -0700394 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700395 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700396 codec_width_(320),
397 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200398 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200399 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700400 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200401 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700402 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700403 Clock::GetRealTimeClock(),
404 video_send_config_,
405 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700406 sink_(&fake_encoder_) {}
407
408 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700409 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700410 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200411 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800412 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200413 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200414 video_send_config_.rtp.payload_name = "FAKE";
415 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700416
Per512ecb32016-09-23 15:52:06 +0200417 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200418 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700419 video_encoder_config.video_stream_factory =
420 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100421 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700422
423 // Framerate limit is specified by the VideoStreamFactory.
424 std::vector<VideoStream> streams =
425 video_encoder_config.video_stream_factory->CreateEncoderStreams(
426 codec_width_, codec_height_, video_encoder_config);
427 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200428 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700429
Niels Möllerf1338562018-04-26 09:51:47 +0200430 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800431 }
432
Niels Möllerf1338562018-04-26 09:51:47 +0200433 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700434 if (video_stream_encoder_)
435 video_stream_encoder_->Stop();
436 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200437 stats_proxy_.get(), video_send_config_.encoder_settings,
438 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700439 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
440 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700441 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700442 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
443 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200444 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700445 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800446 }
447
448 void ResetEncoder(const std::string& payload_name,
449 size_t num_streams,
450 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700451 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700452 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200453 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800454
455 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200456 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800457 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100458 video_encoder_config.max_bitrate_bps =
459 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800460 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700461 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
462 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700463 video_encoder_config.content_type =
464 screenshare ? VideoEncoderConfig::ContentType::kScreen
465 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700466 if (payload_name == "VP9") {
467 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
468 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
469 video_encoder_config.encoder_specific_settings =
470 new rtc::RefCountedObject<
471 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
472 }
Niels Möllerf1338562018-04-26 09:51:47 +0200473 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700474 }
475
sprang57c2fff2017-01-16 06:24:02 -0800476 VideoFrame CreateFrame(int64_t ntp_time_ms,
477 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100478 VideoFrame frame =
479 VideoFrame::Builder()
480 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
481 destruction_event, codec_width_, codec_height_))
482 .set_timestamp_rtp(99)
483 .set_timestamp_ms(99)
484 .set_rotation(kVideoRotation_0)
485 .build();
sprang57c2fff2017-01-16 06:24:02 -0800486 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700487 return frame;
488 }
489
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100490 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
491 rtc::Event* destruction_event,
492 int offset_x) const {
493 VideoFrame frame =
494 VideoFrame::Builder()
495 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
496 destruction_event, codec_width_, codec_height_))
497 .set_timestamp_rtp(99)
498 .set_timestamp_ms(99)
499 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100500 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100501 .build();
502 frame.set_ntp_time_ms(ntp_time_ms);
503 return frame;
504 }
505
sprang57c2fff2017-01-16 06:24:02 -0800506 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100507 VideoFrame frame =
508 VideoFrame::Builder()
509 .set_video_frame_buffer(
510 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
511 .set_timestamp_rtp(99)
512 .set_timestamp_ms(99)
513 .set_rotation(kVideoRotation_0)
514 .build();
sprang57c2fff2017-01-16 06:24:02 -0800515 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700516 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700517 return frame;
518 }
519
Noah Richards51db4212019-06-12 06:59:12 -0700520 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
521 rtc::Event* destruction_event,
522 int width,
523 int height) const {
524 VideoFrame frame =
525 VideoFrame::Builder()
526 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
527 destruction_event, width, height))
528 .set_timestamp_rtp(99)
529 .set_timestamp_ms(99)
530 .set_rotation(kVideoRotation_0)
531 .build();
532 frame.set_ntp_time_ms(ntp_time_ms);
533 return frame;
534 }
535
536 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
537 rtc::Event* destruction_event) const {
538 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
539 codec_height_);
540 }
541
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100542 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
543 MockBitrateObserver bitrate_observer;
544 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
545
546 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
547 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200548 video_stream_encoder_->OnBitrateUpdated(
549 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +0100550 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100551
552 video_source_.IncomingCapturedFrame(
553 CreateFrame(1, codec_width_, codec_height_));
554 WaitForEncodedFrame(1);
555 }
556
asapersson02465b82017-04-10 01:12:52 -0700557 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700558 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700559 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
560 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700561 }
562
asapersson09f05612017-05-15 23:40:18 -0700563 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
564 const rtc::VideoSinkWants& wants2) {
565 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
566 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
567 }
568
Åsa Persson8c1bf952018-09-13 10:42:19 +0200569 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
570 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
571 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
572 EXPECT_FALSE(wants.target_pixel_count);
573 }
574
asapersson09f05612017-05-15 23:40:18 -0700575 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
576 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200577 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700578 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
579 EXPECT_GT(wants1.max_pixel_count, 0);
580 }
581
582 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
583 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200584 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700585 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
586 }
587
asaperssonf7e294d2017-06-13 23:25:22 -0700588 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
589 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200590 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700591 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
592 }
593
594 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
595 const rtc::VideoSinkWants& wants2) {
596 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
597 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
598 }
599
600 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
601 const rtc::VideoSinkWants& wants2) {
602 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
603 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
604 }
605
606 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
607 const rtc::VideoSinkWants& wants2) {
608 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
609 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
610 EXPECT_GT(wants1.max_pixel_count, 0);
611 }
612
613 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
614 const rtc::VideoSinkWants& wants2) {
615 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
616 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
617 }
618
asapersson09f05612017-05-15 23:40:18 -0700619 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
620 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200621 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700622 EXPECT_LT(wants.max_pixel_count, pixel_count);
623 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700624 }
625
626 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
627 EXPECT_LT(wants.max_framerate_fps, fps);
628 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
629 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700630 }
631
asaperssonf7e294d2017-06-13 23:25:22 -0700632 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
633 int expected_fps) {
634 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
635 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
636 EXPECT_FALSE(wants.target_pixel_count);
637 }
638
Jonathan Yubc771b72017-12-08 17:04:29 -0800639 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
640 int last_frame_pixels) {
641 // Balanced mode should always scale FPS to the desired range before
642 // attempting to scale resolution.
643 int fps_limit = wants.max_framerate_fps;
644 if (last_frame_pixels <= 320 * 240) {
645 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
646 } else if (last_frame_pixels <= 480 * 270) {
647 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
648 } else if (last_frame_pixels <= 640 * 480) {
649 EXPECT_LE(15, fps_limit);
650 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200651 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800652 }
653 }
654
sprang4847ae62017-06-27 07:06:52 -0700655 void WaitForEncodedFrame(int64_t expected_ntp_time) {
656 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200657 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700658 }
659
660 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
661 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200662 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700663 return ok;
664 }
665
666 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
667 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200668 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700669 }
670
671 void ExpectDroppedFrame() {
672 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200673 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700674 }
675
676 bool WaitForFrame(int64_t timeout_ms) {
677 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200678 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700679 return ok;
680 }
681
perkj26091b12016-09-01 01:17:40 -0700682 class TestEncoder : public test::FakeEncoder {
683 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100684 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700685
asaperssonfab67072017-04-04 05:51:49 -0700686 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800687 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700688 return config_;
689 }
690
691 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800692 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700693 block_next_encode_ = true;
694 }
695
Erik Språngaed30702018-11-05 12:57:17 +0100696 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800697 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100698 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100699 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100700 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100701 info.scaling_settings = VideoEncoder::ScalingSettings(
702 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100703 }
704 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100705 for (int i = 0; i < kMaxSpatialLayers; ++i) {
706 if (temporal_layers_supported_[i]) {
707 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
708 info.fps_allocation[i].resize(num_layers);
709 }
710 }
Erik Språngaed30702018-11-05 12:57:17 +0100711 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200712
713 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100714 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100715 return info;
kthelgason876222f2016-11-29 01:44:11 -0800716 }
717
Erik Språngb7cb7b52019-02-26 15:52:33 +0100718 int32_t RegisterEncodeCompleteCallback(
719 EncodedImageCallback* callback) override {
720 rtc::CritScope lock(&local_crit_sect_);
721 encoded_image_callback_ = callback;
722 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
723 }
724
perkjfa10b552016-10-02 23:45:26 -0700725 void ContinueEncode() { continue_encode_event_.Set(); }
726
727 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
728 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800729 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700730 EXPECT_EQ(timestamp_, timestamp);
731 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
732 }
733
kthelgason2fc52542017-03-03 00:24:41 -0800734 void SetQualityScaling(bool b) {
735 rtc::CritScope lock(&local_crit_sect_);
736 quality_scaling_ = b;
737 }
kthelgasonad9010c2017-02-14 00:46:51 -0800738
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100739 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
740 rtc::CritScope lock(&local_crit_sect_);
741 requested_resolution_alignment_ = requested_resolution_alignment;
742 }
743
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100744 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
745 rtc::CritScope lock(&local_crit_sect_);
746 is_hardware_accelerated_ = is_hardware_accelerated;
747 }
748
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100749 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
750 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
751 rtc::CritScope lock(&local_crit_sect_);
752 temporal_layers_supported_[spatial_idx] = supported;
753 }
754
Sergey Silkin6456e352019-07-08 17:56:40 +0200755 void SetResolutionBitrateLimits(
756 std::vector<ResolutionBitrateLimits> thresholds) {
757 rtc::CritScope cs(&local_crit_sect_);
758 resolution_bitrate_limits_ = thresholds;
759 }
760
sprangfe627f32017-03-29 08:24:59 -0700761 void ForceInitEncodeFailure(bool force_failure) {
762 rtc::CritScope lock(&local_crit_sect_);
763 force_init_encode_failed_ = force_failure;
764 }
765
Niels Möller6bb5ab92019-01-11 11:11:10 +0100766 void SimulateOvershoot(double rate_factor) {
767 rtc::CritScope lock(&local_crit_sect_);
768 rate_factor_ = rate_factor;
769 }
770
Erik Språngd7329ca2019-02-21 21:19:53 +0100771 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100772 rtc::CritScope lock(&local_crit_sect_);
773 return last_framerate_;
774 }
775
Erik Språngd7329ca2019-02-21 21:19:53 +0100776 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100777 rtc::CritScope lock(&local_crit_sect_);
778 return last_update_rect_;
779 }
780
Niels Möller87e2d782019-03-07 10:18:23 +0100781 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100782 rtc::CritScope lock(&local_crit_sect_);
783 return last_frame_types_;
784 }
785
786 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100787 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100788 keyframe ? VideoFrameType::kVideoFrameKey
789 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100790 {
791 rtc::CritScope lock(&local_crit_sect_);
792 last_frame_types_ = frame_type;
793 }
Niels Möllerb859b322019-03-07 12:40:01 +0100794 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100795 }
796
Erik Språngb7cb7b52019-02-26 15:52:33 +0100797 void InjectEncodedImage(const EncodedImage& image) {
798 rtc::CritScope lock(&local_crit_sect_);
799 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
800 }
801
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200802 void InjectEncodedImage(const EncodedImage& image,
803 const CodecSpecificInfo* codec_specific_info,
804 const RTPFragmentationHeader* fragmentation) {
805 rtc::CritScope lock(&local_crit_sect_);
806 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
807 fragmentation);
808 }
809
Erik Språngd7329ca2019-02-21 21:19:53 +0100810 void ExpectNullFrame() {
811 rtc::CritScope lock(&local_crit_sect_);
812 expect_null_frame_ = true;
813 }
814
Erik Språng5056af02019-09-02 15:53:11 +0200815 absl::optional<VideoEncoder::RateControlParameters>
816 GetAndResetLastRateControlSettings() {
817 auto settings = last_rate_control_settings_;
818 last_rate_control_settings_.reset();
819 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100820 }
821
Sergey Silkin5ee69672019-07-02 14:18:34 +0200822 int GetNumEncoderInitializations() const {
823 rtc::CritScope lock(&local_crit_sect_);
824 return num_encoder_initializations_;
825 }
826
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200827 int GetNumSetRates() const {
828 rtc::CritScope lock(&local_crit_sect_);
829 return num_set_rates_;
830 }
831
perkjfa10b552016-10-02 23:45:26 -0700832 private:
perkj26091b12016-09-01 01:17:40 -0700833 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100834 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700835 bool block_encode;
836 {
brandtre78d2662017-01-16 05:57:16 -0800837 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100838 if (expect_null_frame_) {
839 EXPECT_EQ(input_image.timestamp(), 0u);
840 EXPECT_EQ(input_image.width(), 1);
841 last_frame_types_ = *frame_types;
842 expect_null_frame_ = false;
843 } else {
844 EXPECT_GT(input_image.timestamp(), timestamp_);
845 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
846 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
847 }
perkj26091b12016-09-01 01:17:40 -0700848
849 timestamp_ = input_image.timestamp();
850 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700851 last_input_width_ = input_image.width();
852 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700853 block_encode = block_next_encode_;
854 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100855 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100856 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700857 }
Niels Möllerb859b322019-03-07 12:40:01 +0100858 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700859 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700860 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700861 return result;
862 }
863
sprangfe627f32017-03-29 08:24:59 -0700864 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200865 const Settings& settings) override {
866 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200867
sprangfe627f32017-03-29 08:24:59 -0700868 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100869 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200870
871 ++num_encoder_initializations_;
872
Erik Språng82fad3d2018-03-21 09:57:23 +0100873 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700874 // Simulate setting up temporal layers, in order to validate the life
875 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100876 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200877 frame_buffer_controller_ =
878 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700879 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100880 if (force_init_encode_failed_) {
881 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700882 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100883 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100884
Erik Språngb7cb7b52019-02-26 15:52:33 +0100885 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700886 return res;
887 }
888
Erik Språngb7cb7b52019-02-26 15:52:33 +0100889 int32_t Release() override {
890 rtc::CritScope lock(&local_crit_sect_);
891 EXPECT_NE(initialized_, EncoderState::kUninitialized);
892 initialized_ = EncoderState::kUninitialized;
893 return FakeEncoder::Release();
894 }
895
Erik Språng16cb8f52019-04-12 13:59:09 +0200896 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100897 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200898 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100899 VideoBitrateAllocation adjusted_rate_allocation;
900 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
901 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200902 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100903 adjusted_rate_allocation.SetBitrate(
904 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200905 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100906 rate_factor_));
907 }
908 }
909 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200910 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200911 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200912 RateControlParameters adjusted_paramters = parameters;
913 adjusted_paramters.bitrate = adjusted_rate_allocation;
914 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100915 }
916
brandtre78d2662017-01-16 05:57:16 -0800917 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100918 enum class EncoderState {
919 kUninitialized,
920 kInitializationFailed,
921 kInitialized
922 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
923 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700924 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700925 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700926 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
927 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
928 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
929 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
930 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100931 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100932 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100933 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700934 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100935 absl::optional<bool>
936 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
937 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700938 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100939 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
940 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200941 absl::optional<VideoEncoder::RateControlParameters>
942 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100943 VideoFrame::UpdateRect last_update_rect_
944 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100945 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100946 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100947 EncodedImageCallback* encoded_image_callback_
948 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200949 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200950 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200951 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
952 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200953 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700954 };
955
mflodmancc3d4422017-08-03 08:27:51 -0700956 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700957 public:
958 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100959 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700960
perkj26091b12016-09-01 01:17:40 -0700961 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700962 EXPECT_TRUE(
963 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
964 }
965
966 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
967 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700968 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700969 if (!encoded_frame_event_.Wait(timeout_ms))
970 return false;
perkj26091b12016-09-01 01:17:40 -0700971 {
972 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800973 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700974 }
975 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700976 return true;
perkj26091b12016-09-01 01:17:40 -0700977 }
978
sprangb1ca0732017-02-01 08:38:12 -0800979 void WaitForEncodedFrame(uint32_t expected_width,
980 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700981 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100982 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700983 }
984
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100985 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700986 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800987 uint32_t width = 0;
988 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800989 {
990 rtc::CritScope lock(&crit_);
991 width = last_width_;
992 height = last_height_;
993 }
994 EXPECT_EQ(expected_height, height);
995 EXPECT_EQ(expected_width, width);
996 }
997
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100998 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
999 int width = 0;
1000 int height = 0;
1001 {
1002 rtc::CritScope lock(&crit_);
1003 width = last_width_;
1004 height = last_height_;
1005 }
1006 EXPECT_EQ(width % resolution_alignment, 0);
1007 EXPECT_EQ(height % resolution_alignment, 0);
1008 }
1009
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001010 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1011 VideoRotation rotation;
1012 {
1013 rtc::CritScope lock(&crit_);
1014 rotation = last_rotation_;
1015 }
1016 EXPECT_EQ(expected_rotation, rotation);
1017 }
1018
kthelgason2fc52542017-03-03 00:24:41 -08001019 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001020
sprangc5d62e22017-04-02 23:53:04 -07001021 bool WaitForFrame(int64_t timeout_ms) {
1022 return encoded_frame_event_.Wait(timeout_ms);
1023 }
1024
perkj26091b12016-09-01 01:17:40 -07001025 void SetExpectNoFrames() {
1026 rtc::CritScope lock(&crit_);
1027 expect_frames_ = false;
1028 }
1029
asaperssonfab67072017-04-04 05:51:49 -07001030 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001031 rtc::CritScope lock(&crit_);
1032 return number_of_reconfigurations_;
1033 }
1034
asaperssonfab67072017-04-04 05:51:49 -07001035 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001036 rtc::CritScope lock(&crit_);
1037 return min_transmit_bitrate_bps_;
1038 }
1039
Erik Språngd7329ca2019-02-21 21:19:53 +01001040 void SetNumExpectedLayers(size_t num_layers) {
1041 rtc::CritScope lock(&crit_);
1042 num_expected_layers_ = num_layers;
1043 }
1044
Erik Språngb7cb7b52019-02-26 15:52:33 +01001045 int64_t GetLastCaptureTimeMs() const {
1046 rtc::CritScope lock(&crit_);
1047 return last_capture_time_ms_;
1048 }
1049
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001050 std::vector<uint8_t> GetLastEncodedImageData() {
1051 rtc::CritScope lock(&crit_);
1052 return std::move(last_encoded_image_data_);
1053 }
1054
1055 RTPFragmentationHeader GetLastFragmentation() {
1056 rtc::CritScope lock(&crit_);
1057 return std::move(last_fragmentation_);
1058 }
1059
perkj26091b12016-09-01 01:17:40 -07001060 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001061 Result OnEncodedImage(
1062 const EncodedImage& encoded_image,
1063 const CodecSpecificInfo* codec_specific_info,
1064 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001065 rtc::CritScope lock(&crit_);
1066 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001067 last_encoded_image_data_ = std::vector<uint8_t>(
1068 encoded_image.data(), encoded_image.data() + encoded_image.size());
1069 if (fragmentation) {
1070 last_fragmentation_.CopyFrom(*fragmentation);
1071 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001072 uint32_t timestamp = encoded_image.Timestamp();
1073 if (last_timestamp_ != timestamp) {
1074 num_received_layers_ = 1;
1075 } else {
1076 ++num_received_layers_;
1077 }
1078 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001079 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001080 last_width_ = encoded_image._encodedWidth;
1081 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001082 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001083 if (num_received_layers_ == num_expected_layers_) {
1084 encoded_frame_event_.Set();
1085 }
sprangb1ca0732017-02-01 08:38:12 -08001086 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001087 }
1088
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001089 void OnEncoderConfigurationChanged(
1090 std::vector<VideoStream> streams,
1091 VideoEncoderConfig::ContentType content_type,
1092 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001093 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001094 ++number_of_reconfigurations_;
1095 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1096 }
1097
perkj26091b12016-09-01 01:17:40 -07001098 rtc::CriticalSection crit_;
1099 TestEncoder* test_encoder_;
1100 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001101 std::vector<uint8_t> last_encoded_image_data_;
1102 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001103 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001104 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001105 uint32_t last_height_ = 0;
1106 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001107 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001108 size_t num_expected_layers_ = 1;
1109 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001110 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001111 int number_of_reconfigurations_ = 0;
1112 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001113 };
1114
Sergey Silkin5ee69672019-07-02 14:18:34 +02001115 class VideoBitrateAllocatorProxyFactory
1116 : public VideoBitrateAllocatorFactory {
1117 public:
1118 VideoBitrateAllocatorProxyFactory()
1119 : bitrate_allocator_factory_(
1120 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1121
1122 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1123 const VideoCodec& codec) override {
1124 rtc::CritScope lock(&crit_);
1125 codec_config_ = codec;
1126 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1127 }
1128
1129 VideoCodec codec_config() const {
1130 rtc::CritScope lock(&crit_);
1131 return codec_config_;
1132 }
1133
1134 private:
1135 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1136
1137 rtc::CriticalSection crit_;
1138 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1139 };
1140
perkj26091b12016-09-01 01:17:40 -07001141 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001142 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001143 int codec_width_;
1144 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001145 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001146 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001147 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001148 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001149 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001150 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001151 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001152 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001153 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001154 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001155};
1156
mflodmancc3d4422017-08-03 08:27:51 -07001157TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001158 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001159 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001160 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001161 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001162 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001163 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001164 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001165 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001166}
1167
mflodmancc3d4422017-08-03 08:27:51 -07001168TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001169 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001170 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001171 // The encoder will cache up to one frame for a short duration. Adding two
1172 // frames means that the first frame will be dropped and the second frame will
1173 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001174 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001175 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001176 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001177
Erik Språng4c6ca302019-04-08 15:14:01 +02001178 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001179 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001180 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001181
Sebastian Janssona3177052018-04-10 13:05:49 +02001182 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001183 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001184 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1185
1186 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001187 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001188}
1189
mflodmancc3d4422017-08-03 08:27:51 -07001190TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001191 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001192 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001193 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001194 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001195 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001196
Florent Castellia8336d32019-09-09 13:36:55 +02001197 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
Ying Wang9b881ab2020-02-07 14:29:32 +01001198 DataRate::bps(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001199 // The encoder will cache up to one frame for a short duration. Adding two
1200 // frames means that the first frame will be dropped and the second frame will
1201 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001202 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001203 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001204
Erik Språng4c6ca302019-04-08 15:14:01 +02001205 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001206 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001207 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001208 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001209 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1210 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001211 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001212}
1213
mflodmancc3d4422017-08-03 08:27:51 -07001214TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001215 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001216 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001217 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001218 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001219 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001220
1221 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001222 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001223
perkja49cbd32016-09-16 07:53:41 -07001224 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001225 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001226 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001227}
1228
mflodmancc3d4422017-08-03 08:27:51 -07001229TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001230 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001231 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001232 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001233
perkja49cbd32016-09-16 07:53:41 -07001234 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001235 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001236
mflodmancc3d4422017-08-03 08:27:51 -07001237 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001238 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001239 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001240 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1241 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001242}
1243
mflodmancc3d4422017-08-03 08:27:51 -07001244TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001245 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001246 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001247 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001248
1249 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001250 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001251 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001252 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1253 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001254 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1255 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001256 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001257 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001258
mflodmancc3d4422017-08-03 08:27:51 -07001259 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001260}
1261
Noah Richards51db4212019-06-12 06:59:12 -07001262TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1263 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001264 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001265 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001266
1267 rtc::Event frame_destroyed_event;
1268 video_source_.IncomingCapturedFrame(
1269 CreateFakeNativeFrame(1, &frame_destroyed_event));
1270 ExpectDroppedFrame();
1271 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1272 video_stream_encoder_->Stop();
1273}
1274
1275TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1276 // Use the cropping factory.
1277 video_encoder_config_.video_stream_factory =
1278 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1279 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1280 kMaxPayloadLength);
1281 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1282
1283 // Capture a frame at codec_width_/codec_height_.
1284 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001285 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001286 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001287 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1288 WaitForEncodedFrame(1);
1289 // The encoder will have been configured once.
1290 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1291 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1292 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1293
1294 // Now send in a fake frame that needs to be cropped as the width/height
1295 // aren't divisible by 4 (see CreateEncoderStreams above).
1296 rtc::Event frame_destroyed_event;
1297 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1298 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1299 ExpectDroppedFrame();
1300 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1301 video_stream_encoder_->Stop();
1302}
1303
Ying Wang9b881ab2020-02-07 14:29:32 +01001304TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1305 video_stream_encoder_->OnBitrateUpdated(
1306 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1307 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
1308 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1309 WaitForEncodedFrame(1);
1310
1311 video_stream_encoder_->OnBitrateUpdated(
1312 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1313 DataRate::bps(kTargetBitrateBps), 0, 0, 0.5);
1314 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1315 // frames. Adding two frames means that the first frame will be dropped and
1316 // the second frame will be sent to the encoder.
1317 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1318 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1319 WaitForEncodedFrame(3);
1320 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1321 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1322 WaitForEncodedFrame(5);
1323 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1324 video_stream_encoder_->Stop();
1325}
1326
mflodmancc3d4422017-08-03 08:27:51 -07001327TEST_F(VideoStreamEncoderTest,
1328 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001329 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001330 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001331 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001332 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001333
1334 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001335 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001336 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001337 // The encoder will have been configured once when the first frame is
1338 // received.
1339 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001340
1341 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001342 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001343 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001344 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001345 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001346
1347 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001348 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001349 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001350 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001351 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001352
mflodmancc3d4422017-08-03 08:27:51 -07001353 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001354}
1355
mflodmancc3d4422017-08-03 08:27:51 -07001356TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001357 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001358 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001359 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001360
1361 // Capture a frame and wait for it to synchronize with the encoder thread.
1362 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001363 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001364 // The encoder will have been configured once.
1365 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001366 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1367 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1368
1369 codec_width_ *= 2;
1370 codec_height_ *= 2;
1371 // Capture a frame with a higher resolution and wait for it to synchronize
1372 // with the encoder thread.
1373 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001374 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001375 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1376 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001377 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001378
mflodmancc3d4422017-08-03 08:27:51 -07001379 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001380}
1381
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001382TEST_F(VideoStreamEncoderTest,
1383 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1384 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001385 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001386 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001387
1388 // Capture a frame and wait for it to synchronize with the encoder thread.
1389 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1390 WaitForEncodedFrame(1);
1391
1392 VideoEncoderConfig video_encoder_config;
1393 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1394 // Changing the max payload data length recreates encoder.
1395 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1396 kMaxPayloadLength / 2);
1397
1398 // Capture a frame and wait for it to synchronize with the encoder thread.
1399 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1400 WaitForEncodedFrame(2);
1401 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1402
1403 video_stream_encoder_->Stop();
1404}
1405
Sergey Silkin5ee69672019-07-02 14:18:34 +02001406TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1407 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001408 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001409 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001410
1411 VideoEncoderConfig video_encoder_config;
1412 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1413 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1414 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1415 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1416 kMaxPayloadLength);
1417
1418 // Capture a frame and wait for it to synchronize with the encoder thread.
1419 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1420 WaitForEncodedFrame(1);
1421 // The encoder will have been configured once when the first frame is
1422 // received.
1423 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1424 EXPECT_EQ(kTargetBitrateBps,
1425 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1426 EXPECT_EQ(kStartBitrateBps,
1427 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1428
Sergey Silkin6456e352019-07-08 17:56:40 +02001429 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1430 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001431 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1432 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1433 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1434 kMaxPayloadLength);
1435
1436 // Capture a frame and wait for it to synchronize with the encoder thread.
1437 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1438 WaitForEncodedFrame(2);
1439 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1440 // Bitrate limits have changed - rate allocator should be reconfigured,
1441 // encoder should not be reconfigured.
1442 EXPECT_EQ(kTargetBitrateBps * 2,
1443 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1444 EXPECT_EQ(kStartBitrateBps * 2,
1445 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1446 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1447
1448 video_stream_encoder_->Stop();
1449}
1450
Sergey Silkin6456e352019-07-08 17:56:40 +02001451TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001452 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001453 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001454 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001455 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001456
Sergey Silkincd02eba2020-01-20 14:48:40 +01001457 const uint32_t kMinEncBitrateKbps = 100;
1458 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001459 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001460 /*frame_size_pixels=*/codec_width_ * codec_height_,
1461 /*min_start_bitrate_bps=*/0,
1462 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1463 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001464 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1465
Sergey Silkincd02eba2020-01-20 14:48:40 +01001466 VideoEncoderConfig video_encoder_config;
1467 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1468 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1469 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1470 (kMinEncBitrateKbps + 1) * 1000;
1471 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1472 kMaxPayloadLength);
1473
1474 // When both encoder and app provide bitrate limits, the intersection of
1475 // provided sets should be used.
1476 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1477 WaitForEncodedFrame(1);
1478 EXPECT_EQ(kMaxEncBitrateKbps,
1479 bitrate_allocator_factory_.codec_config().maxBitrate);
1480 EXPECT_EQ(kMinEncBitrateKbps + 1,
1481 bitrate_allocator_factory_.codec_config().minBitrate);
1482
1483 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1484 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1485 (kMinEncBitrateKbps - 1) * 1000;
1486 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1487 kMaxPayloadLength);
1488 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001489 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001490 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001491 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001492 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001493 bitrate_allocator_factory_.codec_config().minBitrate);
1494
Sergey Silkincd02eba2020-01-20 14:48:40 +01001495 video_stream_encoder_->Stop();
1496}
1497
1498TEST_F(VideoStreamEncoderTest,
1499 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1500 video_stream_encoder_->OnBitrateUpdated(
1501 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001502 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001503
1504 const uint32_t kMinAppBitrateKbps = 100;
1505 const uint32_t kMaxAppBitrateKbps = 200;
1506 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1507 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1508 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1509 /*frame_size_pixels=*/codec_width_ * codec_height_,
1510 /*min_start_bitrate_bps=*/0,
1511 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1512 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1513 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1514
1515 VideoEncoderConfig video_encoder_config;
1516 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1517 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1518 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1519 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001520 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1521 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001522
Sergey Silkincd02eba2020-01-20 14:48:40 +01001523 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1524 WaitForEncodedFrame(1);
1525 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001526 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001527 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001528 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001529
1530 video_stream_encoder_->Stop();
1531}
1532
1533TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001534 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001535 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001536 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001537 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001538
1539 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001540 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001541 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001542 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001543 fake_encoder_.SetResolutionBitrateLimits(
1544 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1545
1546 VideoEncoderConfig video_encoder_config;
1547 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1548 video_encoder_config.max_bitrate_bps = 0;
1549 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1550 kMaxPayloadLength);
1551
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001552 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001553 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1554 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001555 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1556 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001557 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1558 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1559
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001560 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001561 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1562 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001563 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1564 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001565 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1566 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1567
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001568 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001569 // encoder for 360p should be used.
1570 video_source_.IncomingCapturedFrame(
1571 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1572 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001573 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1574 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001575 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1576 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1577
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001578 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001579 // ignored.
1580 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1581 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001582 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1583 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001584 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1585 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001586 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1587 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001588 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1589 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1590
1591 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1592 // for 270p should be used.
1593 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1594 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001595 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1596 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001597 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1598 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1599
1600 video_stream_encoder_->Stop();
1601}
1602
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001603TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1604 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001605 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001606 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001607
1608 VideoEncoderConfig video_encoder_config;
1609 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1610 video_encoder_config.max_bitrate_bps = 0;
1611 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1612 kMaxPayloadLength);
1613
1614 // Encode 720p frame to get the default encoder target bitrate.
1615 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1616 WaitForEncodedFrame(1);
1617 const uint32_t kDefaultTargetBitrateFor720pKbps =
1618 bitrate_allocator_factory_.codec_config()
1619 .simulcastStream[0]
1620 .targetBitrate;
1621
1622 // Set the max recommended encoder bitrate to something lower than the default
1623 // target bitrate.
1624 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1625 1280 * 720, 10 * 1000, 10 * 1000,
1626 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1627 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1628
1629 // Change resolution to trigger encoder reinitialization.
1630 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1631 WaitForEncodedFrame(2);
1632 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1633 WaitForEncodedFrame(3);
1634
1635 // Ensure the target bitrate is capped by the max bitrate.
1636 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1637 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1638 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1639 .simulcastStream[0]
1640 .targetBitrate *
1641 1000,
1642 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1643
1644 video_stream_encoder_->Stop();
1645}
1646
mflodmancc3d4422017-08-03 08:27:51 -07001647TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001648 EXPECT_TRUE(video_source_.has_sinks());
1649 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001650 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001651 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001652 EXPECT_FALSE(video_source_.has_sinks());
1653 EXPECT_TRUE(new_video_source.has_sinks());
1654
mflodmancc3d4422017-08-03 08:27:51 -07001655 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001656}
1657
mflodmancc3d4422017-08-03 08:27:51 -07001658TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001659 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001660 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001661 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001662 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001663}
1664
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001665TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1666 constexpr int kRequestedResolutionAlignment = 7;
1667 video_source_.set_adaptation_enabled(true);
1668 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1669 video_stream_encoder_->OnBitrateUpdated(
1670 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001671 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001672
1673 // On the 1st frame, we should have initialized the encoder and
1674 // asked for its resolution requirements.
1675 video_source_.IncomingCapturedFrame(
1676 CreateFrame(1, codec_width_, codec_height_));
1677 WaitForEncodedFrame(1);
1678 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1679 kRequestedResolutionAlignment);
1680
1681 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1682 // (It's up the to the encoder to potentially drop the previous frame,
1683 // to avoid coding back-to-back keyframes.)
1684 video_source_.IncomingCapturedFrame(
1685 CreateFrame(2, codec_width_, codec_height_));
1686 WaitForEncodedFrame(2);
1687 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1688
1689 video_stream_encoder_->Stop();
1690}
1691
Jonathan Yubc771b72017-12-08 17:04:29 -08001692TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1693 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001694 const int kWidth = 1280;
1695 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001696
1697 // We rely on the automatic resolution adaptation, but we handle framerate
1698 // adaptation manually by mocking the stats proxy.
1699 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001700
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001701 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001702 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001703 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001704 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001705 video_stream_encoder_->SetSource(&video_source_,
1706 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001707 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001708 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001709 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001710 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1711
Jonathan Yubc771b72017-12-08 17:04:29 -08001712 // Adapt down as far as possible.
1713 rtc::VideoSinkWants last_wants;
1714 int64_t t = 1;
1715 int loop_count = 0;
1716 do {
1717 ++loop_count;
1718 last_wants = video_source_.sink_wants();
1719
1720 // Simulate the framerate we've been asked to adapt to.
1721 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1722 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1723 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1724 mock_stats.input_frame_rate = fps;
1725 stats_proxy_->SetMockStats(mock_stats);
1726
1727 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1728 sink_.WaitForEncodedFrame(t);
1729 t += frame_interval_ms;
1730
mflodmancc3d4422017-08-03 08:27:51 -07001731 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001732 VerifyBalancedModeFpsRange(
1733 video_source_.sink_wants(),
1734 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1735 } while (video_source_.sink_wants().max_pixel_count <
1736 last_wants.max_pixel_count ||
1737 video_source_.sink_wants().max_framerate_fps <
1738 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001739
Jonathan Yubc771b72017-12-08 17:04:29 -08001740 // Verify that we've adapted all the way down.
1741 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001742 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001743 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1744 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001745 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001746 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1747 *video_source_.last_sent_height());
1748 EXPECT_EQ(kMinBalancedFramerateFps,
1749 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001750
Jonathan Yubc771b72017-12-08 17:04:29 -08001751 // Adapt back up the same number of times we adapted down.
1752 for (int i = 0; i < loop_count - 1; ++i) {
1753 last_wants = video_source_.sink_wants();
1754
1755 // Simulate the framerate we've been asked to adapt to.
1756 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1757 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1758 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1759 mock_stats.input_frame_rate = fps;
1760 stats_proxy_->SetMockStats(mock_stats);
1761
1762 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1763 sink_.WaitForEncodedFrame(t);
1764 t += frame_interval_ms;
1765
mflodmancc3d4422017-08-03 08:27:51 -07001766 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001767 VerifyBalancedModeFpsRange(
1768 video_source_.sink_wants(),
1769 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1770 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1771 last_wants.max_pixel_count ||
1772 video_source_.sink_wants().max_framerate_fps >
1773 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001774 }
1775
Åsa Persson8c1bf952018-09-13 10:42:19 +02001776 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001777 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001778 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001779 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1780 EXPECT_EQ((loop_count - 1) * 2,
1781 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001782
mflodmancc3d4422017-08-03 08:27:51 -07001783 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001784}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001785
mflodmancc3d4422017-08-03 08:27:51 -07001786TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001787 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001788 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001789 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001790 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001791
sprangc5d62e22017-04-02 23:53:04 -07001792 const int kFrameWidth = 1280;
1793 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001794
Åsa Persson8c1bf952018-09-13 10:42:19 +02001795 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001796
kthelgason5e13d412016-12-01 03:59:51 -08001797 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001798 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001799 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001800 frame_timestamp += kFrameIntervalMs;
1801
perkj803d97f2016-11-01 11:45:46 -07001802 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001803 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001804 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001805 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001806 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001807 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001808
asapersson0944a802017-04-07 00:57:58 -07001809 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001810 // wanted resolution.
1811 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1812 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1813 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001814 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001815
1816 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001817 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001818 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001819 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001820 // Give the encoder queue time to process the change in degradation preference
1821 // by waiting for an encoded frame.
1822 new_video_source.IncomingCapturedFrame(
1823 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1824 sink_.WaitForEncodedFrame(frame_timestamp);
1825 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001826 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001827 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001828
sprangc5d62e22017-04-02 23:53:04 -07001829 // Force an input frame rate to be available, or the adaptation call won't
1830 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001831 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001832 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001833 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001834 stats_proxy_->SetMockStats(stats);
1835
mflodmancc3d4422017-08-03 08:27:51 -07001836 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001837 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001838 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001839 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001840 frame_timestamp += kFrameIntervalMs;
1841
1842 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001843 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001844 EXPECT_EQ(std::numeric_limits<int>::max(),
1845 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001846 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001847
asapersson02465b82017-04-10 01:12:52 -07001848 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001849 video_stream_encoder_->SetSource(&new_video_source,
1850 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001851 // Give the encoder queue time to process the change in degradation preference
1852 // by waiting for an encoded frame.
1853 new_video_source.IncomingCapturedFrame(
1854 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1855 sink_.WaitForEncodedFrame(frame_timestamp);
1856 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001857 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001858
mflodmancc3d4422017-08-03 08:27:51 -07001859 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001860 new_video_source.IncomingCapturedFrame(
1861 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001862 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001863 frame_timestamp += kFrameIntervalMs;
1864
1865 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001866 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001867
1868 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001869 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001870 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001871 // Give the encoder queue time to process the change in degradation preference
1872 // by waiting for an encoded frame.
1873 new_video_source.IncomingCapturedFrame(
1874 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1875 sink_.WaitForEncodedFrame(frame_timestamp);
1876 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001877 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1878 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001879 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001880 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001881
1882 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001883 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001884 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001885 // Give the encoder queue time to process the change in degradation preference
1886 // by waiting for an encoded frame.
1887 new_video_source.IncomingCapturedFrame(
1888 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1889 sink_.WaitForEncodedFrame(frame_timestamp);
1890 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001891 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1892 EXPECT_EQ(std::numeric_limits<int>::max(),
1893 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001894 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001895
mflodmancc3d4422017-08-03 08:27:51 -07001896 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001897}
1898
mflodmancc3d4422017-08-03 08:27:51 -07001899TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001900 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001901 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001902 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001903
asaperssonfab67072017-04-04 05:51:49 -07001904 const int kWidth = 1280;
1905 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001906 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001907 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001908 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1909 EXPECT_FALSE(stats.bw_limited_resolution);
1910 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1911
1912 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001913 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001914 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001915 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001916
1917 stats = stats_proxy_->GetStats();
1918 EXPECT_TRUE(stats.bw_limited_resolution);
1919 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1920
1921 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001922 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001923 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001924 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001925
1926 stats = stats_proxy_->GetStats();
1927 EXPECT_FALSE(stats.bw_limited_resolution);
1928 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1929 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1930
mflodmancc3d4422017-08-03 08:27:51 -07001931 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001932}
1933
mflodmancc3d4422017-08-03 08:27:51 -07001934TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001935 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001936 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001937 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001938
1939 const int kWidth = 1280;
1940 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001941 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001942 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001943 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1944 EXPECT_FALSE(stats.cpu_limited_resolution);
1945 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1946
1947 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001948 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001949 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001950 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001951
1952 stats = stats_proxy_->GetStats();
1953 EXPECT_TRUE(stats.cpu_limited_resolution);
1954 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1955
1956 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001957 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001958 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001959 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001960
1961 stats = stats_proxy_->GetStats();
1962 EXPECT_FALSE(stats.cpu_limited_resolution);
1963 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001964 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001965
mflodmancc3d4422017-08-03 08:27:51 -07001966 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001967}
1968
mflodmancc3d4422017-08-03 08:27:51 -07001969TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001970 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001971 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001972 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001973
asaperssonfab67072017-04-04 05:51:49 -07001974 const int kWidth = 1280;
1975 const int kHeight = 720;
1976 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001977 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001978 VideoSendStream::Stats 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_FALSE(stats.cpu_limited_resolution);
1981 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1982
asaperssonfab67072017-04-04 05:51:49 -07001983 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001984 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001985 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001986 WaitForEncodedFrame(2);
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_TRUE(stats.cpu_limited_resolution);
1990 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1991
1992 // Set new source with adaptation still enabled.
1993 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001994 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001995 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001996
asaperssonfab67072017-04-04 05:51:49 -07001997 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001998 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001999 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002000 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002001 EXPECT_TRUE(stats.cpu_limited_resolution);
2002 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2003
2004 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002005 video_stream_encoder_->SetSource(&new_video_source,
2006 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002007
asaperssonfab67072017-04-04 05:51:49 -07002008 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002009 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002010 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002011 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002012 EXPECT_FALSE(stats.cpu_limited_resolution);
2013 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2014
2015 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002016 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002017 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002018
asaperssonfab67072017-04-04 05:51:49 -07002019 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002020 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002021 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002022 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002023 EXPECT_TRUE(stats.cpu_limited_resolution);
2024 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2025
asaperssonfab67072017-04-04 05:51:49 -07002026 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002027 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002028 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002029 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002030 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002031 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002032 EXPECT_FALSE(stats.cpu_limited_resolution);
2033 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002034 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002035
mflodmancc3d4422017-08-03 08:27:51 -07002036 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002037}
2038
mflodmancc3d4422017-08-03 08:27:51 -07002039TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002040 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002041 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002042 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002043
asaperssonfab67072017-04-04 05:51:49 -07002044 const int kWidth = 1280;
2045 const int kHeight = 720;
2046 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002047 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002048 VideoSendStream::Stats 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(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002052
2053 // Set new source with adaptation still enabled.
2054 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002055 video_stream_encoder_->SetSource(&new_video_source,
2056 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002057
asaperssonfab67072017-04-04 05:51:49 -07002058 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002059 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002060 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002061 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002062 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002063 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002064
asaperssonfab67072017-04-04 05:51:49 -07002065 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002066 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002067 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002068 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002069 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002070 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002071 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002072 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002073
asaperssonfab67072017-04-04 05:51:49 -07002074 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002075 video_stream_encoder_->SetSource(&new_video_source,
2076 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002077
asaperssonfab67072017-04-04 05:51:49 -07002078 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002079 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002080 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002081 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002082 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002083 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002084
asapersson02465b82017-04-10 01:12:52 -07002085 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002086 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002087 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002088
asaperssonfab67072017-04-04 05:51:49 -07002089 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002090 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002091 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002092 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002093 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002094 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2095 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002096
mflodmancc3d4422017-08-03 08:27:51 -07002097 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002098}
2099
mflodmancc3d4422017-08-03 08:27:51 -07002100TEST_F(VideoStreamEncoderTest,
2101 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002102 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002103 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002104 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002105
2106 const int kWidth = 1280;
2107 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002108 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002109 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002110 video_source_.IncomingCapturedFrame(
2111 CreateFrame(timestamp_ms, kWidth, kHeight));
2112 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002113 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2114 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2115 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2116
2117 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002118 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002119 timestamp_ms += kFrameIntervalMs;
2120 video_source_.IncomingCapturedFrame(
2121 CreateFrame(timestamp_ms, kWidth, kHeight));
2122 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002123 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2124 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2125 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2126
2127 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002128 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002129 timestamp_ms += kFrameIntervalMs;
2130 video_source_.IncomingCapturedFrame(
2131 CreateFrame(timestamp_ms, kWidth, kHeight));
2132 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002133 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2134 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2135 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2136
Niels Möller4db138e2018-04-19 09:04:13 +02002137 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002138 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002139
2140 VideoEncoderConfig video_encoder_config;
2141 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2142 // Make format different, to force recreation of encoder.
2143 video_encoder_config.video_format.parameters["foo"] = "foo";
2144 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002145 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002146 timestamp_ms += kFrameIntervalMs;
2147 video_source_.IncomingCapturedFrame(
2148 CreateFrame(timestamp_ms, kWidth, kHeight));
2149 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002150 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2151 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2152 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2153
mflodmancc3d4422017-08-03 08:27:51 -07002154 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002155}
2156
mflodmancc3d4422017-08-03 08:27:51 -07002157TEST_F(VideoStreamEncoderTest,
2158 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002159 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002160 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002161 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002162
asapersson0944a802017-04-07 00:57:58 -07002163 const int kWidth = 1280;
2164 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002165 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002166
asaperssonfab67072017-04-04 05:51:49 -07002167 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002168 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002169 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002170 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002171 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002172 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2173
asapersson02465b82017-04-10 01:12:52 -07002174 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002175 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002176 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002177 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002178 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002179 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002180 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002181 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2182
2183 // Set new source with adaptation still enabled.
2184 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002185 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002186 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002187
2188 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002189 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002190 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002191 stats = stats_proxy_->GetStats();
2192 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002193 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002194 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2195
sprangc5d62e22017-04-02 23:53:04 -07002196 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002197 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002198 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002199 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002200 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002201 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002202 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002203 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002204 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002205 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002206 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2207
sprangc5d62e22017-04-02 23:53:04 -07002208 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002209 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002210 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2211 mock_stats.input_frame_rate = 30;
2212 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002213 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002214 stats_proxy_->ResetMockStats();
2215
2216 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002217 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002218 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002219
2220 // Framerate now adapted.
2221 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002222 EXPECT_FALSE(stats.cpu_limited_resolution);
2223 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002224 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2225
2226 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002227 video_stream_encoder_->SetSource(&new_video_source,
2228 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002229 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002230 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002231 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002232
2233 stats = stats_proxy_->GetStats();
2234 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002235 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002236 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2237
2238 // Try to trigger overuse. Should not succeed.
2239 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002240 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002241 stats_proxy_->ResetMockStats();
2242
2243 stats = stats_proxy_->GetStats();
2244 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002245 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002246 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2247
2248 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002249 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002250 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002251 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002252 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002253 stats = stats_proxy_->GetStats();
2254 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002255 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002256 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002257
2258 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002259 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002260 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002261 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002262 stats = stats_proxy_->GetStats();
2263 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002264 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002265 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2266
2267 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002268 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002269 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002270 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002271 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002272 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002273 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002274 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002275 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002276 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002277 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2278
2279 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002280 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002281 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002282 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002283 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002284 stats = stats_proxy_->GetStats();
2285 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002286 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002287 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002288 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002289
mflodmancc3d4422017-08-03 08:27:51 -07002290 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002291}
2292
mflodmancc3d4422017-08-03 08:27:51 -07002293TEST_F(VideoStreamEncoderTest,
2294 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002295 const int kWidth = 1280;
2296 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002297 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002298 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002299 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002300
asaperssonfab67072017-04-04 05:51:49 -07002301 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002302 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002303
asaperssonfab67072017-04-04 05:51:49 -07002304 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002305 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002306
asaperssonfab67072017-04-04 05:51:49 -07002307 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002308 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002309
asaperssonfab67072017-04-04 05:51:49 -07002310 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002311 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002312
kthelgason876222f2016-11-29 01:44:11 -08002313 // Expect a scale down.
2314 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002315 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002316
asapersson02465b82017-04-10 01:12:52 -07002317 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002318 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002319 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002320 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002321
asaperssonfab67072017-04-04 05:51:49 -07002322 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002323 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002324 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002325 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002326
asaperssonfab67072017-04-04 05:51:49 -07002327 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002328 EXPECT_EQ(std::numeric_limits<int>::max(),
2329 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002330
asaperssonfab67072017-04-04 05:51:49 -07002331 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002332 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002333 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002334 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002335
asapersson02465b82017-04-10 01:12:52 -07002336 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002337 EXPECT_EQ(std::numeric_limits<int>::max(),
2338 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002339
mflodmancc3d4422017-08-03 08:27:51 -07002340 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002341}
2342
mflodmancc3d4422017-08-03 08:27:51 -07002343TEST_F(VideoStreamEncoderTest,
2344 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002345 const int kWidth = 1280;
2346 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002347 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002348 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002349 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002350
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002351 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002352 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002353 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002354 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002355
2356 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002357 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002358 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002359 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2360 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2361
2362 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002363 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002364 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002365 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2366 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2367 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2368
2369 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002370 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002371 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2372 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2373 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2374
mflodmancc3d4422017-08-03 08:27:51 -07002375 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002376}
2377
mflodmancc3d4422017-08-03 08:27:51 -07002378TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002379 const int kWidth = 1280;
2380 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002381 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002382 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002383 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002384
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002385 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002386 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002387 video_stream_encoder_->SetSource(&source,
2388 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002389 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2390 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002391 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002392
2393 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002394 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002395 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2396 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2397 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2398 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2399
2400 // Trigger adapt down for same input resolution, expect no change.
2401 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2402 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002403 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002404 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2405 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2406 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2407
2408 // Trigger adapt down for larger input resolution, expect no change.
2409 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2410 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002411 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002412 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2413 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2414 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2415
mflodmancc3d4422017-08-03 08:27:51 -07002416 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002417}
2418
mflodmancc3d4422017-08-03 08:27:51 -07002419TEST_F(VideoStreamEncoderTest,
2420 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002421 const int kWidth = 1280;
2422 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002423 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002424 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002425 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002426
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002427 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002428 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002430 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002431
2432 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002433 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002434 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002435 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2436 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2437
2438 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002439 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002440 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002441 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2442 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2443
mflodmancc3d4422017-08-03 08:27:51 -07002444 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002445}
2446
mflodmancc3d4422017-08-03 08:27:51 -07002447TEST_F(VideoStreamEncoderTest,
2448 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002449 const int kWidth = 1280;
2450 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002451 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002452 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002453 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002454
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002455 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002456 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002457 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002458 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002459
2460 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002461 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002462 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002463 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002464 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2465
2466 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002467 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002468 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002469 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002470 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2471
mflodmancc3d4422017-08-03 08:27:51 -07002472 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002473}
2474
mflodmancc3d4422017-08-03 08:27:51 -07002475TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002476 const int kWidth = 1280;
2477 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002478 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002479 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002480 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002481
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002482 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002483 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002484 video_stream_encoder_->SetSource(&source,
2485 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002486
2487 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2488 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002489 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002490 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2491 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2492 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2493
2494 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002495 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002496 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002497 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2498 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2499 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2500
mflodmancc3d4422017-08-03 08:27:51 -07002501 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002502}
2503
mflodmancc3d4422017-08-03 08:27:51 -07002504TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002505 const int kWidth = 1280;
2506 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002507 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002508 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002509 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002510
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002511 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002512 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002513 video_stream_encoder_->SetSource(&source,
2514 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002515
2516 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2517 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002518 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002519 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2520 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2521 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2522
2523 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002524 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002525 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002526 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2527 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2528 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2529
mflodmancc3d4422017-08-03 08:27:51 -07002530 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002531}
2532
mflodmancc3d4422017-08-03 08:27:51 -07002533TEST_F(VideoStreamEncoderTest,
2534 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002535 const int kWidth = 1280;
2536 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002537 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002538 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002539 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002540
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002541 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002542 AdaptingFrameForwarder source;
2543 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002544 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002545 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002546
2547 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002548 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002549 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002550 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2551 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2552
2553 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002554 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002555 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002556 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002557 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002558 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2559 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2560
2561 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002562 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002563 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002564 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2565 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2566 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2567
mflodmancc3d4422017-08-03 08:27:51 -07002568 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002569}
2570
mflodmancc3d4422017-08-03 08:27:51 -07002571TEST_F(VideoStreamEncoderTest,
2572 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002573 const int kWidth = 1280;
2574 const int kHeight = 720;
2575 const int kInputFps = 30;
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),
Ying Wang9b881ab2020-02-07 14:29:32 +01002578 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002579
2580 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2581 stats.input_frame_rate = kInputFps;
2582 stats_proxy_->SetMockStats(stats);
2583
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002584 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002585 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2586 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002587 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002588
2589 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002590 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002591 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2592 sink_.WaitForEncodedFrame(2);
2593 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2594
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002595 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002596 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002597 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002598 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002599 // Give the encoder queue time to process the change in degradation preference
2600 // by waiting for an encoded frame.
2601 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2602 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002603 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002604
2605 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002606 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002607 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2608 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002609 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2610
2611 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002612 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002613 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002614
mflodmancc3d4422017-08-03 08:27:51 -07002615 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002616}
2617
mflodmancc3d4422017-08-03 08:27:51 -07002618TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002619 const int kWidth = 1280;
2620 const int kHeight = 720;
2621 const size_t kNumFrames = 10;
2622
Erik Språng4c6ca302019-04-08 15:14:01 +02002623 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002624 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002625 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002626
asaperssond0de2952017-04-21 01:47:31 -07002627 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002628 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002629 video_source_.set_adaptation_enabled(true);
2630
2631 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2632 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2633
2634 int downscales = 0;
2635 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002636 video_source_.IncomingCapturedFrame(
2637 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2638 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002639
asaperssonfab67072017-04-04 05:51:49 -07002640 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002641 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002642 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002643 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002644
2645 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2646 ++downscales;
2647
2648 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2649 EXPECT_EQ(downscales,
2650 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2651 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002652 }
mflodmancc3d4422017-08-03 08:27:51 -07002653 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002654}
2655
mflodmancc3d4422017-08-03 08:27:51 -07002656TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002657 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2658 const int kWidth = 1280;
2659 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002660 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002661 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002662 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002663
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002664 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002665 AdaptingFrameForwarder source;
2666 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002667 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002668 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002669
Åsa Persson8c1bf952018-09-13 10:42:19 +02002670 int64_t timestamp_ms = kFrameIntervalMs;
2671 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002672 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002673 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002674 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2675 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2676
2677 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002678 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002679 timestamp_ms += kFrameIntervalMs;
2680 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2681 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002682 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002683 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2684 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2685
2686 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002687 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002688 timestamp_ms += kFrameIntervalMs;
2689 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002690 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002691 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002692 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2693 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2694
2695 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002696 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002697 timestamp_ms += kFrameIntervalMs;
2698 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2699 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002700 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002701 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2702 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2703
2704 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002705 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002706 timestamp_ms += kFrameIntervalMs;
2707 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002708 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002709 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002710 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2711 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2712
mflodmancc3d4422017-08-03 08:27:51 -07002713 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002714}
2715
mflodmancc3d4422017-08-03 08:27:51 -07002716TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002717 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2718 const int kWidth = 1280;
2719 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002720 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002721 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002722 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002723
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002724 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002725 AdaptingFrameForwarder source;
2726 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002727 video_stream_encoder_->SetSource(&source,
2728 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002729
Åsa Persson8c1bf952018-09-13 10:42:19 +02002730 int64_t timestamp_ms = kFrameIntervalMs;
2731 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002732 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002733 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002734 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2735 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2736
2737 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002738 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002739 timestamp_ms += kFrameIntervalMs;
2740 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2741 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002742 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2743 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2744 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2745
2746 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002747 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002748 timestamp_ms += kFrameIntervalMs;
2749 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002750 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002751 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002752 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2753 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2754
2755 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002756 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002757 timestamp_ms += kFrameIntervalMs;
2758 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2759 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002760 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2761 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2762 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2763
2764 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002765 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002766 timestamp_ms += kFrameIntervalMs;
2767 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002768 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002769 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002770 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2771 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2772
mflodmancc3d4422017-08-03 08:27:51 -07002773 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002774}
2775
Sergey Silkin41c650b2019-10-14 13:12:19 +02002776TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2777 fake_encoder_.SetResolutionBitrateLimits(
2778 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2779
2780 video_stream_encoder_->OnBitrateUpdated(
2781 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2782 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002783 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002784
2785 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2786 AdaptingFrameForwarder source;
2787 source.set_adaptation_enabled(true);
2788 video_stream_encoder_->SetSource(
2789 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2790
2791 // Insert 720p frame.
2792 int64_t timestamp_ms = kFrameIntervalMs;
2793 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2794 WaitForEncodedFrame(1280, 720);
2795
2796 // Reduce bitrate and trigger adapt down.
2797 video_stream_encoder_->OnBitrateUpdated(
2798 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2799 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002800 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002801 video_stream_encoder_->TriggerQualityLow();
2802
2803 // Insert 720p frame. It should be downscaled and encoded.
2804 timestamp_ms += kFrameIntervalMs;
2805 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2806 WaitForEncodedFrame(960, 540);
2807
2808 // Trigger adapt up. Higher resolution should not be requested duo to lack
2809 // of bitrate.
2810 video_stream_encoder_->TriggerQualityHigh();
2811 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2812
2813 // Increase bitrate.
2814 video_stream_encoder_->OnBitrateUpdated(
2815 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2816 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002817 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002818
2819 // Trigger adapt up. Higher resolution should be requested.
2820 video_stream_encoder_->TriggerQualityHigh();
2821 VerifyFpsMaxResolutionMax(source.sink_wants());
2822
2823 video_stream_encoder_->Stop();
2824}
2825
2826TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2827 fake_encoder_.SetResolutionBitrateLimits(
2828 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2829
2830 // Set bitrate equal to min bitrate of 540p.
2831 video_stream_encoder_->OnBitrateUpdated(
2832 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2833 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002834 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002835
2836 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2837 AdaptingFrameForwarder source;
2838 source.set_adaptation_enabled(true);
2839 video_stream_encoder_->SetSource(
2840 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2841
2842 // Insert 720p frame. It should be dropped and lower resolution should be
2843 // requested.
2844 int64_t timestamp_ms = kFrameIntervalMs;
2845 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2846 ExpectDroppedFrame();
2847 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2848
2849 // Insert 720p frame. It should be downscaled and encoded.
2850 timestamp_ms += kFrameIntervalMs;
2851 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2852 WaitForEncodedFrame(960, 540);
2853
2854 video_stream_encoder_->Stop();
2855}
2856
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002857class BalancedDegradationTest : public VideoStreamEncoderTest {
2858 protected:
2859 void SetupTest() {
2860 // Reset encoder for field trials to take effect.
2861 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002862 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002863
2864 // Enable BALANCED preference.
2865 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002866 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2867 }
2868
2869 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01002870 video_stream_encoder_->OnBitrateUpdated(
2871 DataRate::bps(bitrate_bps), DataRate::bps(bitrate_bps),
2872 DataRate::bps(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002873 }
2874
Åsa Persson45b176f2019-09-30 11:19:05 +02002875 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002876 timestamp_ms_ += kFrameIntervalMs;
2877 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002878 }
2879
2880 void InsertFrameAndWaitForEncoded() {
2881 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002882 sink_.WaitForEncodedFrame(timestamp_ms_);
2883 }
2884
2885 const int kWidth = 640; // pixels:640x360=230400
2886 const int kHeight = 360;
2887 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2888 int64_t timestamp_ms_ = 0;
2889 AdaptingFrameForwarder source_;
2890};
2891
2892TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2893 test::ScopedFieldTrials field_trials(
2894 "WebRTC-Video-BalancedDegradationSettings/"
2895 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2896 SetupTest();
2897
2898 // Force input frame rate.
2899 const int kInputFps = 24;
2900 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2901 stats.input_frame_rate = kInputFps;
2902 stats_proxy_->SetMockStats(stats);
2903
Åsa Persson45b176f2019-09-30 11:19:05 +02002904 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002905 VerifyFpsMaxResolutionMax(source_.sink_wants());
2906
2907 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2908 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2909 video_stream_encoder_->TriggerQualityLowExpectFalse();
2910 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2911
2912 video_stream_encoder_->Stop();
2913}
2914
2915TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2916 test::ScopedFieldTrials field_trials(
2917 "WebRTC-Video-BalancedDegradationSettings/"
2918 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2919 SetupTest();
2920
2921 // Force input frame rate.
2922 const int kInputFps = 25;
2923 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2924 stats.input_frame_rate = kInputFps;
2925 stats_proxy_->SetMockStats(stats);
2926
Åsa Persson45b176f2019-09-30 11:19:05 +02002927 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002928 VerifyFpsMaxResolutionMax(source_.sink_wants());
2929
2930 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2931 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2932 video_stream_encoder_->TriggerQualityLow();
2933 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2934
2935 video_stream_encoder_->Stop();
2936}
2937
2938TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2939 test::ScopedFieldTrials field_trials(
2940 "WebRTC-Video-BalancedDegradationSettings/"
2941 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2942 SetupTest();
2943
2944 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2945
Åsa Persson45b176f2019-09-30 11:19:05 +02002946 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002947 VerifyFpsMaxResolutionMax(source_.sink_wants());
2948
2949 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2950 video_stream_encoder_->TriggerQualityLow();
2951 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2952
2953 video_stream_encoder_->Stop();
2954}
2955
Åsa Perssonccfb3402019-09-25 15:13:04 +02002956TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002957 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002958 "WebRTC-Video-BalancedDegradationSettings/"
2959 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002960 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002961
Åsa Persson1b247f12019-08-14 17:26:39 +02002962 const int kMinBitrateBps = 425000;
2963 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002964 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002965
Åsa Persson45b176f2019-09-30 11:19:05 +02002966 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002967 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002968 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2969
2970 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2971 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002972 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002973 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002974 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2975
2976 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2977 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002978 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002979 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002980 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2981
Åsa Persson30ab0152019-08-27 12:22:33 +02002982 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2983 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002984 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002985 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2986 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002987 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2988
2989 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002990 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002991 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002992 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002993
Åsa Persson30ab0152019-08-27 12:22:33 +02002994 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002995 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002996 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002997 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002998 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002999 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3000
3001 video_stream_encoder_->Stop();
3002}
3003
Åsa Perssonccfb3402019-09-25 15:13:04 +02003004TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003005 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3006 test::ScopedFieldTrials field_trials(
3007 "WebRTC-Video-BalancedDegradationSettings/"
3008 "pixels:57600|129600|230400,fps:7|24|24/");
3009 SetupTest();
3010 OnBitrateUpdated(kLowTargetBitrateBps);
3011
3012 VerifyNoLimitation(source_.sink_wants());
3013
3014 // Insert frame, expect scaled down:
3015 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3016 InsertFrame();
3017 EXPECT_FALSE(WaitForFrame(1000));
3018 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3019 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3020
3021 // Insert frame, expect scaled down:
3022 // resolution (320x180@24fps).
3023 InsertFrame();
3024 EXPECT_FALSE(WaitForFrame(1000));
3025 EXPECT_LT(source_.sink_wants().max_pixel_count,
3026 source_.last_wants().max_pixel_count);
3027 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3028
3029 // Frame should not be dropped (min pixels per frame reached).
3030 InsertFrameAndWaitForEncoded();
3031
3032 video_stream_encoder_->Stop();
3033}
3034
3035TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003036 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003037 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003038 "WebRTC-Video-BalancedDegradationSettings/"
3039 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003040 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003041
Åsa Persson30ab0152019-08-27 12:22:33 +02003042 const int kResolutionMinBitrateBps = 435000;
3043 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003044 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003045
Åsa Persson45b176f2019-09-30 11:19:05 +02003046 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003047 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003048 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3049
3050 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3051 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003052 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003053 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003054 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3055
3056 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3057 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003058 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003059 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003060 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3061
3062 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3063 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003064 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003065 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003066 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3067
Åsa Persson30ab0152019-08-27 12:22:33 +02003068 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3069 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003070 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003071 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003072 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3073
3074 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3075 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003076 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003077 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3078
3079 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003080 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003081 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003082 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003083 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003084 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3085
3086 video_stream_encoder_->Stop();
3087}
3088
Åsa Perssonccfb3402019-09-25 15:13:04 +02003089TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003090 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003091 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003092 "WebRTC-Video-BalancedDegradationSettings/"
3093 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003094 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003095
Åsa Persson30ab0152019-08-27 12:22:33 +02003096 const int kMinBitrateBps = 425000;
3097 const int kTooLowMinBitrateBps = 424000;
3098 const int kResolutionMinBitrateBps = 435000;
3099 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003100 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003101
Åsa Persson45b176f2019-09-30 11:19:05 +02003102 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003103 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003104 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3105
3106 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3107 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003108 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003109 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003110 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3111
3112 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3113 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003114 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003115 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003116 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3117
3118 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3119 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003120 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003121 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003122 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3123
3124 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3125 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003126 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003127 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3128
3129 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003130 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003131 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003132 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003133 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003134 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3135
3136 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003137 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003138 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003139 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003140 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3141
3142 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003143 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003144 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003145 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003146 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003147 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3148
Åsa Persson1b247f12019-08-14 17:26:39 +02003149 video_stream_encoder_->Stop();
3150}
3151
mflodmancc3d4422017-08-03 08:27:51 -07003152TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003153 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3154 const int kWidth = 1280;
3155 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003156 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003157 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003158 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003159
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003160 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003161 AdaptingFrameForwarder source;
3162 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003163 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003164 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003165
Åsa Persson8c1bf952018-09-13 10:42:19 +02003166 int64_t timestamp_ms = kFrameIntervalMs;
3167 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003168 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003169 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003170 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3171 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3172 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3173 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3174
3175 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003176 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003177 timestamp_ms += kFrameIntervalMs;
3178 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3179 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003180 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003181 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3182 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3183 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3184 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3185
3186 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003187 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003188 timestamp_ms += kFrameIntervalMs;
3189 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3190 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003191 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003192 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3193 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3194 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3195 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3196
Jonathan Yubc771b72017-12-08 17:04:29 -08003197 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003198 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003199 timestamp_ms += kFrameIntervalMs;
3200 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3201 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003202 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003203 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3204 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003205 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003206 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3207
Jonathan Yubc771b72017-12-08 17:04:29 -08003208 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003209 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003210 timestamp_ms += kFrameIntervalMs;
3211 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3212 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003213 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003214 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003215 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3216 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3217 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3218 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3219
Jonathan Yubc771b72017-12-08 17:04:29 -08003220 // Trigger quality adapt down, expect no change (min resolution reached).
3221 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003222 timestamp_ms += kFrameIntervalMs;
3223 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3224 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003225 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3226 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3227 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3228 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3229 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3230
3231 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003232 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003233 timestamp_ms += kFrameIntervalMs;
3234 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3235 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003236 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003237 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3238 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3239 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3240 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3241
3242 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3243 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003244 timestamp_ms += kFrameIntervalMs;
3245 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3246 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003247 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3248 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3249 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3250 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3251 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3252
3253 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3254 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003255 timestamp_ms += kFrameIntervalMs;
3256 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3257 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003258 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003259 last_wants = source.sink_wants();
3260 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3261 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003262 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003263 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3264
3265 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003266 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003267 timestamp_ms += kFrameIntervalMs;
3268 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3269 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003270 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003271 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3272 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003273 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003274 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3275
3276 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003277 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003278 timestamp_ms += kFrameIntervalMs;
3279 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003280 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003281 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003282 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003283 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3284 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003285 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003286 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003287
mflodmancc3d4422017-08-03 08:27:51 -07003288 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003289}
3290
mflodmancc3d4422017-08-03 08:27:51 -07003291TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003292 const int kWidth = 640;
3293 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003294
Erik Språng4c6ca302019-04-08 15:14:01 +02003295 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003296 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003297 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003298
perkj803d97f2016-11-01 11:45:46 -07003299 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003300 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003301 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003302 }
3303
mflodmancc3d4422017-08-03 08:27:51 -07003304 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003305 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003306 video_source_.IncomingCapturedFrame(CreateFrame(
3307 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003308 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003309 }
3310
mflodmancc3d4422017-08-03 08:27:51 -07003311 video_stream_encoder_->Stop();
3312 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003313 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003314
Ying Wangef3998f2019-12-09 13:06:53 +01003315 EXPECT_METRIC_EQ(
3316 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3317 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003318 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3319}
3320
mflodmancc3d4422017-08-03 08:27:51 -07003321TEST_F(VideoStreamEncoderTest,
3322 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003323 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003324 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003325 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003326 const int kWidth = 640;
3327 const int kHeight = 360;
3328
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003329 video_stream_encoder_->SetSource(&video_source_,
3330 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003331
3332 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3333 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003334 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003335 }
3336
mflodmancc3d4422017-08-03 08:27:51 -07003337 video_stream_encoder_->Stop();
3338 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003339 stats_proxy_.reset();
3340
3341 EXPECT_EQ(0,
3342 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3343}
3344
mflodmancc3d4422017-08-03 08:27:51 -07003345TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003346 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003347 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003348
3349 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003350 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003351 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003352 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3353 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003354
sprang57c2fff2017-01-16 06:24:02 -08003355 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003356 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003357 video_stream_encoder_->OnBitrateUpdated(
3358 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003359 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003360
sprang57c2fff2017-01-16 06:24:02 -08003361 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003362 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3363 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003364 VideoBitrateAllocation bitrate_allocation =
3365 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003366 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003367 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003368 // TODO(srte): The use of millisecs here looks like an error, but the tests
3369 // fails using seconds, this should be investigated.
3370 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003371
3372 // Not called on second frame.
3373 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3374 .Times(0);
3375 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003376 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3377 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003378 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003379
3380 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003381 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3382 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003383 const int64_t start_time_ms = rtc::TimeMillis();
3384 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3385 video_source_.IncomingCapturedFrame(
3386 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3387 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003388 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003389 }
3390
3391 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003392 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003393
mflodmancc3d4422017-08-03 08:27:51 -07003394 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003395}
3396
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003397TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3398 // 2 TLs configured, temporal layers supported by encoder.
3399 const int kNumTemporalLayers = 2;
3400 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3401 fake_encoder_.SetTemporalLayersSupported(0, true);
3402
3403 // Bitrate allocated across temporal layers.
3404 const int kTl0Bps = kTargetBitrateBps *
3405 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003406 kNumTemporalLayers, /*temporal_id*/ 0,
3407 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003408 const int kTl1Bps = kTargetBitrateBps *
3409 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003410 kNumTemporalLayers, /*temporal_id*/ 1,
3411 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003412 VideoBitrateAllocation expected_bitrate;
3413 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3414 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3415
3416 VerifyAllocatedBitrate(expected_bitrate);
3417 video_stream_encoder_->Stop();
3418}
3419
3420TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3421 // 2 TLs configured, temporal layers not supported by encoder.
3422 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3423 fake_encoder_.SetTemporalLayersSupported(0, false);
3424
3425 // Temporal layers not supported by the encoder.
3426 // Total bitrate should be at ti:0.
3427 VideoBitrateAllocation expected_bitrate;
3428 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3429
3430 VerifyAllocatedBitrate(expected_bitrate);
3431 video_stream_encoder_->Stop();
3432}
3433
3434TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3435 // 2 TLs configured, temporal layers only supported for first stream.
3436 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3437 fake_encoder_.SetTemporalLayersSupported(0, true);
3438 fake_encoder_.SetTemporalLayersSupported(1, false);
3439
3440 const int kS0Bps = 150000;
3441 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003442 kS0Bps *
3443 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3444 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003445 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003446 kS0Bps *
3447 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3448 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003449 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3450 // Temporal layers not supported by si:1.
3451 VideoBitrateAllocation expected_bitrate;
3452 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3453 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3454 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3455
3456 VerifyAllocatedBitrate(expected_bitrate);
3457 video_stream_encoder_->Stop();
3458}
3459
Niels Möller7dc26b72017-12-06 10:27:48 +01003460TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3461 const int kFrameWidth = 1280;
3462 const int kFrameHeight = 720;
3463 const int kFramerate = 24;
3464
Erik Språng4c6ca302019-04-08 15:14:01 +02003465 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003466 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003467 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003468 test::FrameForwarder source;
3469 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003470 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003471
3472 // Insert a single frame, triggering initial configuration.
3473 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3474 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3475
3476 EXPECT_EQ(
3477 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3478 kDefaultFramerate);
3479
3480 // Trigger reconfigure encoder (without resetting the entire instance).
3481 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003482 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003483 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3484 video_encoder_config.number_of_streams = 1;
3485 video_encoder_config.video_stream_factory =
3486 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3487 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003488 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003489 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3490
3491 // Detector should be updated with fps limit from codec config.
3492 EXPECT_EQ(
3493 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3494 kFramerate);
3495
3496 // Trigger overuse, max framerate should be reduced.
3497 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3498 stats.input_frame_rate = kFramerate;
3499 stats_proxy_->SetMockStats(stats);
3500 video_stream_encoder_->TriggerCpuOveruse();
3501 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3502 int adapted_framerate =
3503 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3504 EXPECT_LT(adapted_framerate, kFramerate);
3505
3506 // Trigger underuse, max framerate should go back to codec configured fps.
3507 // Set extra low fps, to make sure it's actually reset, not just incremented.
3508 stats = stats_proxy_->GetStats();
3509 stats.input_frame_rate = adapted_framerate / 2;
3510 stats_proxy_->SetMockStats(stats);
3511 video_stream_encoder_->TriggerCpuNormalUsage();
3512 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3513 EXPECT_EQ(
3514 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3515 kFramerate);
3516
3517 video_stream_encoder_->Stop();
3518}
3519
3520TEST_F(VideoStreamEncoderTest,
3521 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3522 const int kFrameWidth = 1280;
3523 const int kFrameHeight = 720;
3524 const int kLowFramerate = 15;
3525 const int kHighFramerate = 25;
3526
Erik Språng4c6ca302019-04-08 15:14:01 +02003527 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003528 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003529 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003530 test::FrameForwarder source;
3531 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003532 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003533
3534 // Trigger initial configuration.
3535 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003536 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003537 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3538 video_encoder_config.number_of_streams = 1;
3539 video_encoder_config.video_stream_factory =
3540 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3541 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3542 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003543 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003544 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3545
3546 EXPECT_EQ(
3547 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3548 kLowFramerate);
3549
3550 // Trigger overuse, max framerate should be reduced.
3551 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3552 stats.input_frame_rate = kLowFramerate;
3553 stats_proxy_->SetMockStats(stats);
3554 video_stream_encoder_->TriggerCpuOveruse();
3555 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3556 int adapted_framerate =
3557 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3558 EXPECT_LT(adapted_framerate, kLowFramerate);
3559
3560 // Reconfigure the encoder with a new (higher max framerate), max fps should
3561 // still respect the adaptation.
3562 video_encoder_config.video_stream_factory =
3563 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3564 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3565 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003566 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003567 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3568
3569 EXPECT_EQ(
3570 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3571 adapted_framerate);
3572
3573 // Trigger underuse, max framerate should go back to codec configured fps.
3574 stats = stats_proxy_->GetStats();
3575 stats.input_frame_rate = adapted_framerate;
3576 stats_proxy_->SetMockStats(stats);
3577 video_stream_encoder_->TriggerCpuNormalUsage();
3578 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3579 EXPECT_EQ(
3580 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3581 kHighFramerate);
3582
3583 video_stream_encoder_->Stop();
3584}
3585
mflodmancc3d4422017-08-03 08:27:51 -07003586TEST_F(VideoStreamEncoderTest,
3587 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003588 const int kFrameWidth = 1280;
3589 const int kFrameHeight = 720;
3590 const int kFramerate = 24;
3591
Erik Språng4c6ca302019-04-08 15:14:01 +02003592 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003593 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003594 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003595 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003596 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003597 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003598
3599 // Trigger initial configuration.
3600 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003601 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003602 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3603 video_encoder_config.number_of_streams = 1;
3604 video_encoder_config.video_stream_factory =
3605 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3606 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003607 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003608 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003609 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003610
Niels Möller7dc26b72017-12-06 10:27:48 +01003611 EXPECT_EQ(
3612 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3613 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003614
3615 // Trigger overuse, max framerate should be reduced.
3616 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3617 stats.input_frame_rate = kFramerate;
3618 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003619 video_stream_encoder_->TriggerCpuOveruse();
3620 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003621 int adapted_framerate =
3622 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003623 EXPECT_LT(adapted_framerate, kFramerate);
3624
3625 // Change degradation preference to not enable framerate scaling. Target
3626 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003627 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003628 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003629 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003630 EXPECT_EQ(
3631 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3632 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003633
mflodmancc3d4422017-08-03 08:27:51 -07003634 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003635}
3636
mflodmancc3d4422017-08-03 08:27:51 -07003637TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003638 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003639 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003640 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003641 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003642 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003643 const int kWidth = 640;
3644 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003645
asaperssonfab67072017-04-04 05:51:49 -07003646 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003647
3648 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003649 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003650
3651 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003652 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003653
sprangc5d62e22017-04-02 23:53:04 -07003654 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003655
asaperssonfab67072017-04-04 05:51:49 -07003656 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003657 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003658 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003659
3660 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003661 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003662
sprangc5d62e22017-04-02 23:53:04 -07003663 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003664
mflodmancc3d4422017-08-03 08:27:51 -07003665 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003666}
3667
mflodmancc3d4422017-08-03 08:27:51 -07003668TEST_F(VideoStreamEncoderTest,
3669 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003670 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003671 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003672 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003673 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003674 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003675 const int kWidth = 640;
3676 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003677
3678 // We expect the n initial frames to get dropped.
3679 int i;
3680 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003681 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003682 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003683 }
3684 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003685 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003686 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003687
3688 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003689 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003690
mflodmancc3d4422017-08-03 08:27:51 -07003691 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003692}
3693
mflodmancc3d4422017-08-03 08:27:51 -07003694TEST_F(VideoStreamEncoderTest,
3695 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003696 const int kWidth = 640;
3697 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003698 video_stream_encoder_->OnBitrateUpdated(
3699 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003700 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003701
3702 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003703 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003704 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003705
asaperssonfab67072017-04-04 05:51:49 -07003706 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003707 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003708 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003709
mflodmancc3d4422017-08-03 08:27:51 -07003710 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003711}
3712
mflodmancc3d4422017-08-03 08:27:51 -07003713TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003714 const int kWidth = 640;
3715 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003716 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003717
3718 VideoEncoderConfig video_encoder_config;
3719 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3720 // Make format different, to force recreation of encoder.
3721 video_encoder_config.video_format.parameters["foo"] = "foo";
3722 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003723 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003724 video_stream_encoder_->OnBitrateUpdated(
3725 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003726 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003727
kthelgasonb83797b2017-02-14 11:57:25 -08003728 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003729 video_stream_encoder_->SetSource(&video_source_,
3730 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003731
asaperssonfab67072017-04-04 05:51:49 -07003732 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003733 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003734 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003735
mflodmancc3d4422017-08-03 08:27:51 -07003736 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003737 fake_encoder_.SetQualityScaling(true);
3738}
3739
Åsa Persson139f4dc2019-08-02 09:29:58 +02003740TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3741 webrtc::test::ScopedFieldTrials field_trials(
3742 "WebRTC-Video-QualityScalerSettings/"
3743 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3744 // Reset encoder for field trials to take effect.
3745 ConfigureEncoder(video_encoder_config_.Copy());
3746 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3747 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3748 const int kWidth = 640;
3749 const int kHeight = 360;
3750
3751 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003752 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003753 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003754 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3755 // Frame should not be dropped.
3756 WaitForEncodedFrame(1);
3757
3758 video_stream_encoder_->OnBitrateUpdated(
3759 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003760 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003761 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003762 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3763 // Frame should not be dropped.
3764 WaitForEncodedFrame(2);
3765
3766 video_stream_encoder_->OnBitrateUpdated(
3767 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003768 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003769 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003770 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3771 // Expect to drop this frame, the wait should time out.
3772 ExpectDroppedFrame();
3773
3774 // Expect the sink_wants to specify a scaled frame.
3775 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3776 video_stream_encoder_->Stop();
3777}
3778
Åsa Perssone644a032019-11-08 15:56:00 +01003779TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3780 webrtc::test::ScopedFieldTrials field_trials(
3781 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3782
3783 // Reset encoder for field trials to take effect.
3784 VideoEncoderConfig config = video_encoder_config_.Copy();
3785 config.max_bitrate_bps = kTargetBitrateBps;
3786 ConfigureEncoder(std::move(config));
3787 fake_encoder_.SetQp(kQpLow);
3788
3789 // Enable MAINTAIN_FRAMERATE preference.
3790 AdaptingFrameForwarder source;
3791 source.set_adaptation_enabled(true);
3792 video_stream_encoder_->SetSource(&source,
3793 DegradationPreference::MAINTAIN_FRAMERATE);
3794
3795 // Start at low bitrate.
3796 const int kLowBitrateBps = 200000;
Ying Wang9b881ab2020-02-07 14:29:32 +01003797 video_stream_encoder_->OnBitrateUpdated(
3798 DataRate::bps(kLowBitrateBps), DataRate::bps(kLowBitrateBps),
3799 DataRate::bps(kLowBitrateBps), 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003800
3801 // Expect first frame to be dropped and resolution to be limited.
3802 const int kWidth = 1280;
3803 const int kHeight = 720;
3804 const int64_t kFrameIntervalMs = 100;
3805 int64_t timestamp_ms = kFrameIntervalMs;
3806 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3807 ExpectDroppedFrame();
3808 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3809
3810 // Increase bitrate to encoder max.
3811 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3812 DataRate::bps(config.max_bitrate_bps),
3813 DataRate::bps(config.max_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003814 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003815
3816 // Insert frames and advance |min_duration_ms|.
3817 for (size_t i = 1; i <= 10; i++) {
3818 timestamp_ms += kFrameIntervalMs;
3819 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3820 WaitForEncodedFrame(timestamp_ms);
3821 }
3822 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3823 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3824
3825 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3826
3827 // Insert frame should trigger high BW and release quality limitation.
3828 timestamp_ms += kFrameIntervalMs;
3829 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3830 WaitForEncodedFrame(timestamp_ms);
3831 VerifyFpsMaxResolutionMax(source.sink_wants());
3832
3833 // Frame should not be adapted.
3834 timestamp_ms += kFrameIntervalMs;
3835 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3836 WaitForEncodedFrame(kWidth, kHeight);
3837 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3838
3839 video_stream_encoder_->Stop();
3840}
3841
mflodmancc3d4422017-08-03 08:27:51 -07003842TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003843 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3844 const int kTooSmallWidth = 10;
3845 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003846 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003847 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003848 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003849
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003850 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003851 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003852 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003853 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003854 VerifyNoLimitation(source.sink_wants());
3855 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3856
3857 // Trigger adapt down, too small frame, expect no change.
3858 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003859 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003860 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003861 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003862 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3863 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3864
mflodmancc3d4422017-08-03 08:27:51 -07003865 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003866}
3867
mflodmancc3d4422017-08-03 08:27:51 -07003868TEST_F(VideoStreamEncoderTest,
3869 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003870 const int kTooSmallWidth = 10;
3871 const int kTooSmallHeight = 10;
3872 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003873 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003874 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003875 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003876
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003877 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003878 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003879 video_stream_encoder_->SetSource(&source,
3880 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003881 VerifyNoLimitation(source.sink_wants());
3882 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3883 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3884
3885 // Trigger adapt down, expect limited framerate.
3886 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003887 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003888 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003889 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3890 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3891 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3892 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3893
3894 // Trigger adapt down, too small frame, expect no change.
3895 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003896 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003897 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003898 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3899 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3900 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3901 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3902
mflodmancc3d4422017-08-03 08:27:51 -07003903 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003904}
3905
mflodmancc3d4422017-08-03 08:27:51 -07003906TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003907 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003908 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003909 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003910 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003911 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003912 const int kFrameWidth = 1280;
3913 const int kFrameHeight = 720;
3914 video_source_.IncomingCapturedFrame(
3915 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003916 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003917 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003918}
3919
sprangb1ca0732017-02-01 08:38:12 -08003920// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003921TEST_F(VideoStreamEncoderTest,
3922 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003923 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003924 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003925 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003926
3927 const int kFrameWidth = 1280;
3928 const int kFrameHeight = 720;
3929 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003930 // requested by
3931 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003932 video_source_.set_adaptation_enabled(true);
3933
3934 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003935 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003936 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003937
3938 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003939 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003940 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003941 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003942 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003943
asaperssonfab67072017-04-04 05:51:49 -07003944 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003945 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003946 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003947 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003948 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003949
mflodmancc3d4422017-08-03 08:27:51 -07003950 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003951}
sprangfe627f32017-03-29 08:24:59 -07003952
mflodmancc3d4422017-08-03 08:27:51 -07003953TEST_F(VideoStreamEncoderTest,
3954 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003955 const int kFrameWidth = 1280;
3956 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003957
Erik Språng4c6ca302019-04-08 15:14:01 +02003958 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003959 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003960 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003961 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003962 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003963 video_source_.set_adaptation_enabled(true);
3964
sprang4847ae62017-06-27 07:06:52 -07003965 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003966
3967 video_source_.IncomingCapturedFrame(
3968 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003969 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003970
3971 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003972 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003973
3974 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003975 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003976 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003977 video_source_.IncomingCapturedFrame(
3978 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003979 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003980 }
3981
3982 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003983 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003984 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003985 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003986 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003987 video_source_.IncomingCapturedFrame(
3988 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003989 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003990 ++num_frames_dropped;
3991 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003992 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003993 }
3994 }
3995
sprang4847ae62017-06-27 07:06:52 -07003996 // Add some slack to account for frames dropped by the frame dropper.
3997 const int kErrorMargin = 1;
3998 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003999 kErrorMargin);
4000
4001 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004002 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004003 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004004 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004005 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004006 video_source_.IncomingCapturedFrame(
4007 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004008 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004009 ++num_frames_dropped;
4010 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004011 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004012 }
4013 }
sprang4847ae62017-06-27 07:06:52 -07004014 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004015 kErrorMargin);
4016
4017 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004018 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004019 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004020 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004021 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004022 video_source_.IncomingCapturedFrame(
4023 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004024 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004025 ++num_frames_dropped;
4026 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004027 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004028 }
4029 }
sprang4847ae62017-06-27 07:06:52 -07004030 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004031 kErrorMargin);
4032
4033 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004034 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004035 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004036 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004037 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004038 video_source_.IncomingCapturedFrame(
4039 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004040 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004041 ++num_frames_dropped;
4042 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004043 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004044 }
4045 }
4046 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4047
mflodmancc3d4422017-08-03 08:27:51 -07004048 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004049}
4050
mflodmancc3d4422017-08-03 08:27:51 -07004051TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004052 const int kFramerateFps = 5;
4053 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004054 const int kFrameWidth = 1280;
4055 const int kFrameHeight = 720;
4056
sprang4847ae62017-06-27 07:06:52 -07004057 // Reconfigure encoder with two temporal layers and screensharing, which will
4058 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004059 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004060
Erik Språng4c6ca302019-04-08 15:14:01 +02004061 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004062 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004063 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004064 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004065 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004066 video_source_.set_adaptation_enabled(true);
4067
sprang4847ae62017-06-27 07:06:52 -07004068 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004069
4070 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004071 rtc::VideoSinkWants last_wants;
4072 do {
4073 last_wants = video_source_.sink_wants();
4074
sprangc5d62e22017-04-02 23:53:04 -07004075 // Insert frames to get a new fps estimate...
4076 for (int j = 0; j < kFramerateFps; ++j) {
4077 video_source_.IncomingCapturedFrame(
4078 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004079 if (video_source_.last_sent_width()) {
4080 sink_.WaitForEncodedFrame(timestamp_ms);
4081 }
sprangc5d62e22017-04-02 23:53:04 -07004082 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004083 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004084 }
4085 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004086 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004087 } while (video_source_.sink_wants().max_framerate_fps <
4088 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004089
Jonathan Yubc771b72017-12-08 17:04:29 -08004090 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004091
mflodmancc3d4422017-08-03 08:27:51 -07004092 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004093}
asaperssonf7e294d2017-06-13 23:25:22 -07004094
mflodmancc3d4422017-08-03 08:27:51 -07004095TEST_F(VideoStreamEncoderTest,
4096 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004097 const int kWidth = 1280;
4098 const int kHeight = 720;
4099 const int64_t kFrameIntervalMs = 150;
4100 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004101 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004102 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004103 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004104
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004105 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004106 AdaptingFrameForwarder source;
4107 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004108 video_stream_encoder_->SetSource(&source,
4109 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004110 timestamp_ms += kFrameIntervalMs;
4111 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004112 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004113 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004114 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4115 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4116 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4117
4118 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004119 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004120 timestamp_ms += kFrameIntervalMs;
4121 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004122 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004123 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4124 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4125 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4126 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4127
4128 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004129 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004130 timestamp_ms += kFrameIntervalMs;
4131 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004132 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004133 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4134 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4135 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4136 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4137
4138 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004139 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004140 timestamp_ms += kFrameIntervalMs;
4141 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004142 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004143 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4144 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4145 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4146 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4147
4148 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004149 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004150 timestamp_ms += kFrameIntervalMs;
4151 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004152 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004153 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4154 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4155 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4156 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4157
4158 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004159 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004160 timestamp_ms += kFrameIntervalMs;
4161 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004162 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004163 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4164 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4165 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4166 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4167
4168 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004169 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004170 timestamp_ms += kFrameIntervalMs;
4171 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004172 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004173 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4174 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4175 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4176 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4177
4178 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004179 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004180 timestamp_ms += kFrameIntervalMs;
4181 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004182 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004183 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4184 rtc::VideoSinkWants last_wants = source.sink_wants();
4185 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4186 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4187 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4188
4189 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004190 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004191 timestamp_ms += kFrameIntervalMs;
4192 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004193 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004194 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4195 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4196 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4197 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4198
4199 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004200 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004201 timestamp_ms += kFrameIntervalMs;
4202 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004203 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004204 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4205 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4206 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4207 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4208
4209 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004210 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004211 timestamp_ms += kFrameIntervalMs;
4212 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004213 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004214 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4215 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4216 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4217 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4218
4219 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004220 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004221 timestamp_ms += kFrameIntervalMs;
4222 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004223 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004224 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4225 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4226 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4227 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4228
4229 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004230 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004231 timestamp_ms += kFrameIntervalMs;
4232 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004233 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004234 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4235 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4236 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4237 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4238
4239 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004240 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004241 timestamp_ms += kFrameIntervalMs;
4242 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004243 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004244 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4245 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4246 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4247 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4248
4249 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004250 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004251 timestamp_ms += kFrameIntervalMs;
4252 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004253 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004254 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4255 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4256 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4257 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4258
Åsa Persson30ab0152019-08-27 12:22:33 +02004259 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004260 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004261 timestamp_ms += kFrameIntervalMs;
4262 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004263 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004264 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004265 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004266 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4267 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4268 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4269
4270 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004271 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004272 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004273 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4274
mflodmancc3d4422017-08-03 08:27:51 -07004275 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004276}
4277
mflodmancc3d4422017-08-03 08:27:51 -07004278TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004279 const int kWidth = 1280;
4280 const int kHeight = 720;
4281 const int64_t kFrameIntervalMs = 150;
4282 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004283 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004284 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004285 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004286
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004287 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004288 AdaptingFrameForwarder source;
4289 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004290 video_stream_encoder_->SetSource(&source,
4291 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004292 timestamp_ms += kFrameIntervalMs;
4293 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004294 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004295 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004296 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4297 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4298 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4299 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4300 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4301 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4302
4303 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004304 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004305 timestamp_ms += kFrameIntervalMs;
4306 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004307 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004308 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4309 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4310 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4311 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4312 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4313 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4314 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4315
4316 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004317 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004318 timestamp_ms += kFrameIntervalMs;
4319 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004320 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004321 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4322 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4323 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4324 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4325 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4326 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4327 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4328
4329 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004330 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004331 timestamp_ms += kFrameIntervalMs;
4332 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004333 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004334 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4335 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4336 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4337 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4338 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4339 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4340 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4341
4342 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004343 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004344 timestamp_ms += kFrameIntervalMs;
4345 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004346 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004347 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4348 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4349 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4350 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4351 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4352 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4353 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4354
4355 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004356 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004357 timestamp_ms += kFrameIntervalMs;
4358 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004359 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004360 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4361 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4362 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4363 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4364 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4365 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4366 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4367
4368 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004369 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004370 timestamp_ms += kFrameIntervalMs;
4371 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004372 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004373 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004374 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004375 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4376 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4377 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4378 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4379 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4380 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4381
4382 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004383 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004384 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004385 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4386 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4387
mflodmancc3d4422017-08-03 08:27:51 -07004388 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004389}
4390
mflodmancc3d4422017-08-03 08:27:51 -07004391TEST_F(VideoStreamEncoderTest,
4392 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004393 const int kWidth = 640;
4394 const int kHeight = 360;
4395 const int kFpsLimit = 15;
4396 const int64_t kFrameIntervalMs = 150;
4397 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004398 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004399 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004400 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004401
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004402 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004403 AdaptingFrameForwarder source;
4404 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004405 video_stream_encoder_->SetSource(&source,
4406 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004407 timestamp_ms += kFrameIntervalMs;
4408 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004409 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004410 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004411 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4412 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4413 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4414 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4415 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4416 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4417
4418 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004419 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004420 timestamp_ms += kFrameIntervalMs;
4421 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004422 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004423 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4424 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4425 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4426 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4427 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4428 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4429 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4430
4431 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004432 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004433 timestamp_ms += kFrameIntervalMs;
4434 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004435 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004436 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4437 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4438 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4439 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4440 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4441 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4442 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4443
4444 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004445 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004446 timestamp_ms += kFrameIntervalMs;
4447 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004448 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004449 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4450 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4451 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4452 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4453 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4454 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4455 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4456
4457 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004458 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004459 timestamp_ms += kFrameIntervalMs;
4460 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004461 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004462 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004463 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4464 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4465 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4466 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4467 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4468 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4469
4470 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004471 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004472 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004473 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4474 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4475
mflodmancc3d4422017-08-03 08:27:51 -07004476 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004477}
4478
mflodmancc3d4422017-08-03 08:27:51 -07004479TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004480 const int kFrameWidth = 1920;
4481 const int kFrameHeight = 1080;
4482 // 3/4 of 1920.
4483 const int kAdaptedFrameWidth = 1440;
4484 // 3/4 of 1080 rounded down to multiple of 4.
4485 const int kAdaptedFrameHeight = 808;
4486 const int kFramerate = 24;
4487
Erik Språng4c6ca302019-04-08 15:14:01 +02004488 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004489 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004490 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004491 // Trigger reconfigure encoder (without resetting the entire instance).
4492 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004493 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004494 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4495 video_encoder_config.number_of_streams = 1;
4496 video_encoder_config.video_stream_factory =
4497 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004498 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004499 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004500 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004501
4502 video_source_.set_adaptation_enabled(true);
4503
4504 video_source_.IncomingCapturedFrame(
4505 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004506 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004507
4508 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004509 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004510 video_source_.IncomingCapturedFrame(
4511 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004512 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004513
mflodmancc3d4422017-08-03 08:27:51 -07004514 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004515}
4516
mflodmancc3d4422017-08-03 08:27:51 -07004517TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004518 const int kFrameWidth = 1280;
4519 const int kFrameHeight = 720;
4520 const int kLowFps = 2;
4521 const int kHighFps = 30;
4522
Erik Språng4c6ca302019-04-08 15:14:01 +02004523 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004524 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004525 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004526
4527 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4528 max_framerate_ = kLowFps;
4529
4530 // Insert 2 seconds of 2fps video.
4531 for (int i = 0; i < kLowFps * 2; ++i) {
4532 video_source_.IncomingCapturedFrame(
4533 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4534 WaitForEncodedFrame(timestamp_ms);
4535 timestamp_ms += 1000 / kLowFps;
4536 }
4537
4538 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004539 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004540 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004541 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004542 video_source_.IncomingCapturedFrame(
4543 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4544 WaitForEncodedFrame(timestamp_ms);
4545 timestamp_ms += 1000 / kLowFps;
4546
4547 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4548
4549 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004550 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004551 const int kFrameIntervalMs = 1000 / kHighFps;
4552 max_framerate_ = kHighFps;
4553 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4554 video_source_.IncomingCapturedFrame(
4555 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4556 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4557 // be dropped if the encoder hans't been updated with the new higher target
4558 // framerate yet, causing it to overshoot the target bitrate and then
4559 // suffering the wrath of the media optimizer.
4560 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4561 timestamp_ms += kFrameIntervalMs;
4562 }
4563
4564 // Don expect correct measurement just yet, but it should be higher than
4565 // before.
4566 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4567
mflodmancc3d4422017-08-03 08:27:51 -07004568 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004569}
4570
mflodmancc3d4422017-08-03 08:27:51 -07004571TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004572 const int kFrameWidth = 1280;
4573 const int kFrameHeight = 720;
4574 const int kTargetBitrateBps = 1000000;
4575
4576 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004577 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004578 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004579 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004580 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004581 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004582
4583 // Insert a first video frame, causes another bitrate update.
4584 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4585 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4586 video_source_.IncomingCapturedFrame(
4587 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4588 WaitForEncodedFrame(timestamp_ms);
4589
4590 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004591 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
Ying Wang9b881ab2020-02-07 14:29:32 +01004592 DataRate::bps(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004593
4594 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004595 timestamp_ms += kProcessIntervalMs;
4596 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004597
4598 // Bitrate observer should not be called.
4599 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4600 video_source_.IncomingCapturedFrame(
4601 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4602 ExpectDroppedFrame();
4603
mflodmancc3d4422017-08-03 08:27:51 -07004604 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004605}
ilnik6b826ef2017-06-16 06:53:48 -07004606
Niels Möller4db138e2018-04-19 09:04:13 +02004607TEST_F(VideoStreamEncoderTest,
4608 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4609 const int kFrameWidth = 1280;
4610 const int kFrameHeight = 720;
4611 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004612 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004613 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004614 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004615 video_source_.IncomingCapturedFrame(
4616 CreateFrame(1, kFrameWidth, kFrameHeight));
4617 WaitForEncodedFrame(1);
4618 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4619 .low_encode_usage_threshold_percent,
4620 default_options.low_encode_usage_threshold_percent);
4621 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4622 .high_encode_usage_threshold_percent,
4623 default_options.high_encode_usage_threshold_percent);
4624 video_stream_encoder_->Stop();
4625}
4626
4627TEST_F(VideoStreamEncoderTest,
4628 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4629 const int kFrameWidth = 1280;
4630 const int kFrameHeight = 720;
4631 CpuOveruseOptions hardware_options;
4632 hardware_options.low_encode_usage_threshold_percent = 150;
4633 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004634 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004635
Erik Språng4c6ca302019-04-08 15:14:01 +02004636 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004637 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004638 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004639 video_source_.IncomingCapturedFrame(
4640 CreateFrame(1, kFrameWidth, kFrameHeight));
4641 WaitForEncodedFrame(1);
4642 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4643 .low_encode_usage_threshold_percent,
4644 hardware_options.low_encode_usage_threshold_percent);
4645 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4646 .high_encode_usage_threshold_percent,
4647 hardware_options.high_encode_usage_threshold_percent);
4648 video_stream_encoder_->Stop();
4649}
4650
Niels Möller6bb5ab92019-01-11 11:11:10 +01004651TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4652 const int kFrameWidth = 320;
4653 const int kFrameHeight = 240;
4654 const int kFps = 30;
4655 const int kTargetBitrateBps = 120000;
4656 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4657
Erik Språng4c6ca302019-04-08 15:14:01 +02004658 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004659 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004660 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004661
4662 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4663 max_framerate_ = kFps;
4664
4665 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4666 fake_encoder_.SimulateOvershoot(1.0);
4667 int num_dropped = 0;
4668 for (int i = 0; i < kNumFramesInRun; ++i) {
4669 video_source_.IncomingCapturedFrame(
4670 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4671 // Wait up to two frame durations for a frame to arrive.
4672 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4673 ++num_dropped;
4674 }
4675 timestamp_ms += 1000 / kFps;
4676 }
4677
Erik Språnga8d48ab2019-02-08 14:17:40 +01004678 // Framerate should be measured to be near the expected target rate.
4679 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4680
4681 // Frame drops should be within 5% of expected 0%.
4682 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004683
4684 // Make encoder produce frames at double the expected bitrate during 3 seconds
4685 // of video, verify number of drops. Rate needs to be slightly changed in
4686 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004687 double overshoot_factor = 2.0;
4688 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4689 // With bitrate adjuster, when need to overshoot even more to trigger
4690 // frame dropping.
4691 overshoot_factor *= 2;
4692 }
4693 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004694 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004695 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004696 DataRate::bps(kTargetBitrateBps + 1000),
Ying Wang9b881ab2020-02-07 14:29:32 +01004697 DataRate::bps(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004698 num_dropped = 0;
4699 for (int i = 0; i < kNumFramesInRun; ++i) {
4700 video_source_.IncomingCapturedFrame(
4701 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4702 // Wait up to two frame durations for a frame to arrive.
4703 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4704 ++num_dropped;
4705 }
4706 timestamp_ms += 1000 / kFps;
4707 }
4708
Erik Språng4c6ca302019-04-08 15:14:01 +02004709 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004710 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004711 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004712
4713 // Target framerate should be still be near the expected target, despite
4714 // the frame drops.
4715 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4716
4717 // Frame drops should be within 5% of expected 50%.
4718 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004719
4720 video_stream_encoder_->Stop();
4721}
4722
4723TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4724 const int kFrameWidth = 320;
4725 const int kFrameHeight = 240;
4726 const int kActualInputFps = 24;
4727 const int kTargetBitrateBps = 120000;
4728
4729 ASSERT_GT(max_framerate_, kActualInputFps);
4730
4731 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4732 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004733 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004734 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004735 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004736
4737 // Insert 3 seconds of video, with an input fps lower than configured max.
4738 for (int i = 0; i < kActualInputFps * 3; ++i) {
4739 video_source_.IncomingCapturedFrame(
4740 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4741 // Wait up to two frame durations for a frame to arrive.
4742 WaitForEncodedFrame(timestamp_ms);
4743 timestamp_ms += 1000 / kActualInputFps;
4744 }
4745
4746 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4747
4748 video_stream_encoder_->Stop();
4749}
4750
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004751TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4752 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004753 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004754 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004755 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004756
4757 fake_encoder_.BlockNextEncode();
4758 video_source_.IncomingCapturedFrame(
4759 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4760 WaitForEncodedFrame(1);
4761 // On the very first frame full update should be forced.
4762 rect = fake_encoder_.GetLastUpdateRect();
4763 EXPECT_EQ(rect.offset_x, 0);
4764 EXPECT_EQ(rect.offset_y, 0);
4765 EXPECT_EQ(rect.height, codec_height_);
4766 EXPECT_EQ(rect.width, codec_width_);
4767 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4768 // call to ContinueEncode.
4769 video_source_.IncomingCapturedFrame(
4770 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4771 ExpectDroppedFrame();
4772 video_source_.IncomingCapturedFrame(
4773 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4774 ExpectDroppedFrame();
4775 fake_encoder_.ContinueEncode();
4776 WaitForEncodedFrame(3);
4777 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4778 rect = fake_encoder_.GetLastUpdateRect();
4779 EXPECT_EQ(rect.offset_x, 1);
4780 EXPECT_EQ(rect.offset_y, 0);
4781 EXPECT_EQ(rect.width, 10);
4782 EXPECT_EQ(rect.height, 1);
4783
4784 video_source_.IncomingCapturedFrame(
4785 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4786 WaitForEncodedFrame(4);
4787 // Previous frame was encoded, so no accumulation should happen.
4788 rect = fake_encoder_.GetLastUpdateRect();
4789 EXPECT_EQ(rect.offset_x, 0);
4790 EXPECT_EQ(rect.offset_y, 0);
4791 EXPECT_EQ(rect.width, 1);
4792 EXPECT_EQ(rect.height, 1);
4793
4794 video_stream_encoder_->Stop();
4795}
4796
Erik Språngd7329ca2019-02-21 21:19:53 +01004797TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004798 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004799 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004800 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004801
4802 // First frame is always keyframe.
4803 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4804 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004805 EXPECT_THAT(
4806 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004807 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004808
4809 // Insert delta frame.
4810 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4811 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004812 EXPECT_THAT(
4813 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004814 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004815
4816 // Request next frame be a key-frame.
4817 video_stream_encoder_->SendKeyFrame();
4818 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4819 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004820 EXPECT_THAT(
4821 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004822 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004823
4824 video_stream_encoder_->Stop();
4825}
4826
4827TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4828 // Setup simulcast with three streams.
4829 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004830 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004831 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004832 DataRate::bps(kSimulcastTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004833 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004834 // Wait for all three layers before triggering event.
4835 sink_.SetNumExpectedLayers(3);
4836
4837 // First frame is always keyframe.
4838 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4839 WaitForEncodedFrame(1);
4840 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004841 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4842 VideoFrameType::kVideoFrameKey,
4843 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004844
4845 // Insert delta frame.
4846 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4847 WaitForEncodedFrame(2);
4848 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004849 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4850 VideoFrameType::kVideoFrameDelta,
4851 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004852
4853 // Request next frame be a key-frame.
4854 // Only first stream is configured to produce key-frame.
4855 video_stream_encoder_->SendKeyFrame();
4856 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4857 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004858
4859 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4860 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004861 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004862 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004863 VideoFrameType::kVideoFrameKey,
4864 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004865
4866 video_stream_encoder_->Stop();
4867}
4868
4869TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4870 // Configure internal source factory and setup test again.
4871 encoder_factory_.SetHasInternalSource(true);
4872 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004873 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004874 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004875 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004876
4877 // Call encoder directly, simulating internal source where encoded frame
4878 // callback in VideoStreamEncoder is called despite no OnFrame().
4879 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4880 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004881 EXPECT_THAT(
4882 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004883 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004884
Niels Möller8f7ce222019-03-21 15:43:58 +01004885 const std::vector<VideoFrameType> kDeltaFrame = {
4886 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004887 // Need to set timestamp manually since manually for injected frame.
4888 VideoFrame frame = CreateFrame(101, nullptr);
4889 frame.set_timestamp(101);
4890 fake_encoder_.InjectFrame(frame, false);
4891 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004892 EXPECT_THAT(
4893 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004894 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004895
4896 // Request key-frame. The forces a dummy frame down into the encoder.
4897 fake_encoder_.ExpectNullFrame();
4898 video_stream_encoder_->SendKeyFrame();
4899 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004900 EXPECT_THAT(
4901 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004902 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004903
4904 video_stream_encoder_->Stop();
4905}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004906
4907TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4908 // Configure internal source factory and setup test again.
4909 encoder_factory_.SetHasInternalSource(true);
4910 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004911 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004912 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004913 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004914
4915 int64_t timestamp = 1;
4916 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004917 image.SetEncodedData(
4918 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004919 image.capture_time_ms_ = ++timestamp;
4920 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4921 const int64_t kEncodeFinishDelayMs = 10;
4922 image.timing_.encode_start_ms = timestamp;
4923 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4924 fake_encoder_.InjectEncodedImage(image);
4925 // Wait for frame without incrementing clock.
4926 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4927 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4928 // capture timestamp should be kEncodeFinishDelayMs in the past.
4929 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4930 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4931 kEncodeFinishDelayMs);
4932
4933 video_stream_encoder_->Stop();
4934}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004935
4936TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4937 // Configure internal source factory and setup test again.
4938 encoder_factory_.SetHasInternalSource(true);
4939 ResetEncoder("H264", 1, 1, 1, false);
4940
4941 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4942 image._frameType = VideoFrameType::kVideoFrameKey;
4943
4944 CodecSpecificInfo codec_specific_info;
4945 codec_specific_info.codecType = kVideoCodecH264;
4946
4947 RTPFragmentationHeader fragmentation;
4948 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4949 fragmentation.fragmentationOffset[0] = 4;
4950 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4951
4952 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4953 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4954
4955 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4956 testing::ElementsAreArray(optimal_sps));
4957 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4958 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4959 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4960 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4961
4962 video_stream_encoder_->Stop();
4963}
4964
4965TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4966 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4967 0x00, 0x00, 0x03, 0x03, 0xF4,
4968 0x05, 0x03, 0xC7, 0xC0};
4969
4970 // Configure internal source factory and setup test again.
4971 encoder_factory_.SetHasInternalSource(true);
4972 ResetEncoder("H264", 1, 1, 1, false);
4973
4974 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4975 image._frameType = VideoFrameType::kVideoFrameKey;
4976
4977 CodecSpecificInfo codec_specific_info;
4978 codec_specific_info.codecType = kVideoCodecH264;
4979
4980 RTPFragmentationHeader fragmentation;
4981 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4982 fragmentation.fragmentationOffset[0] = 4;
4983 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4984
4985 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4986 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4987
4988 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4989 testing::ElementsAreArray(optimal_sps));
4990 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4991 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4992 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4993 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4994
4995 video_stream_encoder_->Stop();
4996}
4997
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004998TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4999 const int kFrameWidth = 1280;
5000 const int kFrameHeight = 720;
5001 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5002
5003 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02005004 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005005 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005006 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5007
5008 // Insert a first video frame. It should be dropped because of downscale in
5009 // resolution.
5010 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5011 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5012 frame.set_rotation(kVideoRotation_270);
5013 video_source_.IncomingCapturedFrame(frame);
5014
5015 ExpectDroppedFrame();
5016
5017 // Second frame is downscaled.
5018 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5019 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5020 frame.set_rotation(kVideoRotation_90);
5021 video_source_.IncomingCapturedFrame(frame);
5022
5023 WaitForEncodedFrame(timestamp_ms);
5024 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5025
5026 // Insert another frame, also downscaled.
5027 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5028 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5029 frame.set_rotation(kVideoRotation_180);
5030 video_source_.IncomingCapturedFrame(frame);
5031
5032 WaitForEncodedFrame(timestamp_ms);
5033 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5034
5035 video_stream_encoder_->Stop();
5036}
5037
Erik Språng5056af02019-09-02 15:53:11 +02005038TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5039 const int kFrameWidth = 320;
5040 const int kFrameHeight = 180;
5041
5042 // Initial rate.
5043 video_stream_encoder_->OnBitrateUpdated(
5044 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005045 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005046 /*link_allocation=*/DataRate::kbps(300),
5047 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005048 /*rtt_ms=*/0,
5049 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005050
5051 // Insert a first video frame so that encoder gets configured.
5052 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5053 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5054 frame.set_rotation(kVideoRotation_270);
5055 video_source_.IncomingCapturedFrame(frame);
5056 WaitForEncodedFrame(timestamp_ms);
5057
5058 // Set a target rate below the minimum allowed by the codec settings.
5059 VideoCodec codec_config = fake_encoder_.codec_config();
5060 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5061 DataRate target_rate = min_rate - DataRate::kbps(1);
5062 video_stream_encoder_->OnBitrateUpdated(
5063 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005064 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005065 /*link_allocation=*/target_rate,
5066 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005067 /*rtt_ms=*/0,
5068 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005069 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5070
5071 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5072 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5073 ASSERT_TRUE(rate_settings.has_value());
5074 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5075 EXPECT_EQ(min_rate, allocation_sum);
5076 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5077
5078 video_stream_encoder_->Stop();
5079}
5080
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005081TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005082 video_stream_encoder_->OnBitrateUpdated(
5083 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005084 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005085 // Capture a frame and wait for it to synchronize with the encoder thread.
5086 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5087 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5088 WaitForEncodedFrame(1);
5089
5090 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5091 ASSERT_TRUE(prev_rate_settings.has_value());
5092 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5093 kDefaultFramerate);
5094
5095 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5096 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5097 timestamp_ms += 1000 / kDefaultFramerate;
5098 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5099 WaitForEncodedFrame(timestamp_ms);
5100 }
5101 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5102 kDefaultFramerate);
5103 // Capture larger frame to trigger a reconfigure.
5104 codec_height_ *= 2;
5105 codec_width_ *= 2;
5106 timestamp_ms += 1000 / kDefaultFramerate;
5107 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5108 WaitForEncodedFrame(timestamp_ms);
5109
5110 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5111 auto current_rate_settings =
5112 fake_encoder_.GetAndResetLastRateControlSettings();
5113 // Ensure we have actually reconfigured twice
5114 // The rate settings should have been set again even though
5115 // they haven't changed.
5116 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005117 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005118
5119 video_stream_encoder_->Stop();
5120}
5121
philipeld9cc8c02019-09-16 14:53:40 +02005122struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5123 MOCK_METHOD0(RequestEncoderFallback, void());
5124 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5125};
5126
5127TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5128 constexpr int kDontCare = 100;
5129
5130 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5131 video_send_config_.encoder_settings.encoder_switch_request_callback =
5132 &switch_callback;
5133 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5134 encoder_config.codec_type = kVideoCodecVP8;
5135 webrtc::test::ScopedFieldTrials field_trial(
5136 "WebRTC-NetworkCondition-EncoderSwitch/"
5137 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5138 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5139
5140 // Reset encoder for new configuration to take effect.
5141 ConfigureEncoder(std::move(encoder_config));
5142
5143 // Send one frame to trigger ReconfigureEncoder.
5144 video_source_.IncomingCapturedFrame(
5145 CreateFrame(kDontCare, kDontCare, kDontCare));
5146
5147 using Config = EncoderSwitchRequestCallback::Config;
5148 EXPECT_CALL(switch_callback,
5149 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5150 Field(&Config::param, "ping"),
5151 Field(&Config::value, "pong"))));
5152
5153 video_stream_encoder_->OnBitrateUpdated(
5154 /*target_bitrate=*/DataRate::kbps(50),
5155 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5156 /*link_allocation=*/DataRate::kbps(kDontCare),
5157 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005158 /*rtt_ms=*/0,
5159 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005160
5161 video_stream_encoder_->Stop();
5162}
5163
5164TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5165 constexpr int kSufficientBitrateToNotDrop = 1000;
5166 constexpr int kHighRes = 500;
5167 constexpr int kLowRes = 100;
5168
5169 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5170 video_send_config_.encoder_settings.encoder_switch_request_callback =
5171 &switch_callback;
5172 webrtc::test::ScopedFieldTrials field_trial(
5173 "WebRTC-NetworkCondition-EncoderSwitch/"
5174 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5175 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5176 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5177 encoder_config.codec_type = kVideoCodecH264;
5178
5179 // Reset encoder for new configuration to take effect.
5180 ConfigureEncoder(std::move(encoder_config));
5181
5182 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5183 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5184 // not fail.
5185 video_stream_encoder_->OnBitrateUpdated(
5186 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5187 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5188 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5189 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005190 /*rtt_ms=*/0,
5191 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005192
5193 // Send one frame to trigger ReconfigureEncoder.
5194 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5195 WaitForEncodedFrame(1);
5196
5197 using Config = EncoderSwitchRequestCallback::Config;
5198 EXPECT_CALL(switch_callback,
5199 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5200 Field(&Config::param, "ping"),
5201 Field(&Config::value, "pong"))));
5202
5203 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5204 WaitForEncodedFrame(2);
5205
5206 video_stream_encoder_->Stop();
5207}
5208
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005209TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005210 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005211 const int kFrameWidth = 320;
5212 const int kFrameHeight = 180;
5213
5214 // Set initial rate.
5215 auto rate = DataRate::kbps(100);
5216 video_stream_encoder_->OnBitrateUpdated(
5217 /*target_bitrate=*/rate,
5218 /*stable_target_bitrate=*/rate,
5219 /*link_allocation=*/rate,
5220 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005221 /*rtt_ms=*/0,
5222 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005223
5224 // Insert a first video frame so that encoder gets configured.
5225 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5226 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5227 frame.set_rotation(kVideoRotation_270);
5228 video_source_.IncomingCapturedFrame(frame);
5229 WaitForEncodedFrame(timestamp_ms);
5230 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5231
5232 // Change of target bitrate propagates to the encoder.
5233 auto new_stable_rate = rate - DataRate::kbps(5);
5234 video_stream_encoder_->OnBitrateUpdated(
5235 /*target_bitrate=*/new_stable_rate,
5236 /*stable_target_bitrate=*/new_stable_rate,
5237 /*link_allocation=*/rate,
5238 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005239 /*rtt_ms=*/0,
5240 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005241 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5242 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5243 video_stream_encoder_->Stop();
5244}
5245
5246TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005247 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005248 const int kFrameWidth = 320;
5249 const int kFrameHeight = 180;
5250
5251 // Set initial rate.
5252 auto rate = DataRate::kbps(100);
5253 video_stream_encoder_->OnBitrateUpdated(
5254 /*target_bitrate=*/rate,
5255 /*stable_target_bitrate=*/rate,
5256 /*link_allocation=*/rate,
5257 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005258 /*rtt_ms=*/0,
5259 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005260
5261 // Insert a first video frame so that encoder gets configured.
5262 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5263 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5264 frame.set_rotation(kVideoRotation_270);
5265 video_source_.IncomingCapturedFrame(frame);
5266 WaitForEncodedFrame(timestamp_ms);
5267 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5268
5269 // Set a higher target rate without changing the link_allocation. Should not
5270 // reset encoder's rate.
5271 auto new_stable_rate = rate - DataRate::kbps(5);
5272 video_stream_encoder_->OnBitrateUpdated(
5273 /*target_bitrate=*/rate,
5274 /*stable_target_bitrate=*/new_stable_rate,
5275 /*link_allocation=*/rate,
5276 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005277 /*rtt_ms=*/0,
5278 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005279 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5280 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5281 video_stream_encoder_->Stop();
5282}
5283
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005284TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5285 test::ScopedFieldTrials field_trials(
5286 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5287 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5288 const int kFramerateFps = 30;
5289 const int kWidth = 1920;
5290 const int kHeight = 1080;
5291 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5292 // Works on screenshare mode.
5293 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5294 // We rely on the automatic resolution adaptation, but we handle framerate
5295 // adaptation manually by mocking the stats proxy.
5296 video_source_.set_adaptation_enabled(true);
5297
5298 // BALANCED degradation preference is required for this feature.
5299 video_stream_encoder_->OnBitrateUpdated(
5300 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005301 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005302 video_stream_encoder_->SetSource(&video_source_,
5303 webrtc::DegradationPreference::BALANCED);
5304 VerifyNoLimitation(video_source_.sink_wants());
5305
5306 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5307 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5308
5309 // Pass enough frames with the full update to trigger animation detection.
5310 for (int i = 0; i < kNumFrames; ++i) {
5311 int64_t timestamp_ms =
5312 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5313 frame.set_ntp_time_ms(timestamp_ms);
5314 frame.set_timestamp_us(timestamp_ms * 1000);
5315 video_source_.IncomingCapturedFrame(frame);
5316 WaitForEncodedFrame(timestamp_ms);
5317 }
5318
5319 // Resolution should be limited.
5320 rtc::VideoSinkWants expected;
5321 expected.max_framerate_fps = kFramerateFps;
5322 expected.max_pixel_count = 1280 * 720 + 1;
5323 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5324
5325 // Pass one frame with no known update.
5326 // Resolution cap should be removed immediately.
5327 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5328 frame.set_ntp_time_ms(timestamp_ms);
5329 frame.set_timestamp_us(timestamp_ms * 1000);
5330 frame.clear_update_rect();
5331
5332 video_source_.IncomingCapturedFrame(frame);
5333 WaitForEncodedFrame(timestamp_ms);
5334
5335 // Resolution should be unlimited now.
5336 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5337
5338 video_stream_encoder_->Stop();
5339}
5340
perkj26091b12016-09-01 01:17:40 -07005341} // namespace webrtc