blob: 472027b478843a21f43b232511881452b4a54cca [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),
550 DataRate::bps(kTargetBitrateBps), 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),
1160 DataRate::bps(kTargetBitrateBps), 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),
1180 DataRate::bps(kTargetBitrateBps), 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),
1193 DataRate::bps(kTargetBitrateBps), 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),
1198 DataRate::bps(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),
1207 DataRate::bps(kTargetBitrateBps), 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),
1217 DataRate::bps(kTargetBitrateBps), 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),
1232 DataRate::bps(kTargetBitrateBps), 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),
1247 DataRate::bps(kTargetBitrateBps), 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),
1265 DataRate::bps(kTargetBitrateBps), 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),
1286 DataRate::bps(kTargetBitrateBps), 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
mflodmancc3d4422017-08-03 08:27:51 -07001304TEST_F(VideoStreamEncoderTest,
1305 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001306 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001307 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1308 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001309 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001310
1311 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001312 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001313 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001314 // The encoder will have been configured once when the first frame is
1315 // received.
1316 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001317
1318 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001319 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001320 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001321 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001322 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001323
1324 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001325 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001326 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001327 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001328 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001329
mflodmancc3d4422017-08-03 08:27:51 -07001330 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001331}
1332
mflodmancc3d4422017-08-03 08:27:51 -07001333TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001334 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001335 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1336 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001337
1338 // Capture a frame and wait for it to synchronize with the encoder thread.
1339 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001340 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001341 // The encoder will have been configured once.
1342 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001343 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1344 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1345
1346 codec_width_ *= 2;
1347 codec_height_ *= 2;
1348 // Capture a frame with a higher resolution and wait for it to synchronize
1349 // with the encoder thread.
1350 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001351 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001352 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1353 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001354 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001355
mflodmancc3d4422017-08-03 08:27:51 -07001356 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001357}
1358
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001359TEST_F(VideoStreamEncoderTest,
1360 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1361 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001362 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1363 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001364
1365 // Capture a frame and wait for it to synchronize with the encoder thread.
1366 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1367 WaitForEncodedFrame(1);
1368
1369 VideoEncoderConfig video_encoder_config;
1370 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1371 // Changing the max payload data length recreates encoder.
1372 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1373 kMaxPayloadLength / 2);
1374
1375 // Capture a frame and wait for it to synchronize with the encoder thread.
1376 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1377 WaitForEncodedFrame(2);
1378 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1379
1380 video_stream_encoder_->Stop();
1381}
1382
Sergey Silkin5ee69672019-07-02 14:18:34 +02001383TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1384 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001385 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1386 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001387
1388 VideoEncoderConfig video_encoder_config;
1389 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1390 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1391 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1392 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1393 kMaxPayloadLength);
1394
1395 // Capture a frame and wait for it to synchronize with the encoder thread.
1396 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1397 WaitForEncodedFrame(1);
1398 // The encoder will have been configured once when the first frame is
1399 // received.
1400 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1401 EXPECT_EQ(kTargetBitrateBps,
1402 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1403 EXPECT_EQ(kStartBitrateBps,
1404 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1405
Sergey Silkin6456e352019-07-08 17:56:40 +02001406 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1407 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001408 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1409 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1410 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1411 kMaxPayloadLength);
1412
1413 // Capture a frame and wait for it to synchronize with the encoder thread.
1414 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1415 WaitForEncodedFrame(2);
1416 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1417 // Bitrate limits have changed - rate allocator should be reconfigured,
1418 // encoder should not be reconfigured.
1419 EXPECT_EQ(kTargetBitrateBps * 2,
1420 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1421 EXPECT_EQ(kStartBitrateBps * 2,
1422 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1423 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1424
1425 video_stream_encoder_->Stop();
1426}
1427
Sergey Silkin6456e352019-07-08 17:56:40 +02001428TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001429 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001430 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001431 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1432 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001433
Sergey Silkincd02eba2020-01-20 14:48:40 +01001434 const uint32_t kMinEncBitrateKbps = 100;
1435 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001436 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001437 /*frame_size_pixels=*/codec_width_ * codec_height_,
1438 /*min_start_bitrate_bps=*/0,
1439 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1440 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001441 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1442
Sergey Silkincd02eba2020-01-20 14:48:40 +01001443 VideoEncoderConfig video_encoder_config;
1444 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1445 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1446 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1447 (kMinEncBitrateKbps + 1) * 1000;
1448 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1449 kMaxPayloadLength);
1450
1451 // When both encoder and app provide bitrate limits, the intersection of
1452 // provided sets should be used.
1453 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1454 WaitForEncodedFrame(1);
1455 EXPECT_EQ(kMaxEncBitrateKbps,
1456 bitrate_allocator_factory_.codec_config().maxBitrate);
1457 EXPECT_EQ(kMinEncBitrateKbps + 1,
1458 bitrate_allocator_factory_.codec_config().minBitrate);
1459
1460 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1461 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1462 (kMinEncBitrateKbps - 1) * 1000;
1463 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1464 kMaxPayloadLength);
1465 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001466 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001467 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001468 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001469 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001470 bitrate_allocator_factory_.codec_config().minBitrate);
1471
Sergey Silkincd02eba2020-01-20 14:48:40 +01001472 video_stream_encoder_->Stop();
1473}
1474
1475TEST_F(VideoStreamEncoderTest,
1476 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1477 video_stream_encoder_->OnBitrateUpdated(
1478 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1479 DataRate::bps(kTargetBitrateBps), 0, 0);
1480
1481 const uint32_t kMinAppBitrateKbps = 100;
1482 const uint32_t kMaxAppBitrateKbps = 200;
1483 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1484 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1485 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1486 /*frame_size_pixels=*/codec_width_ * codec_height_,
1487 /*min_start_bitrate_bps=*/0,
1488 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1489 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1490 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1491
1492 VideoEncoderConfig video_encoder_config;
1493 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1494 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1495 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1496 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001497 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1498 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001499
Sergey Silkincd02eba2020-01-20 14:48:40 +01001500 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1501 WaitForEncodedFrame(1);
1502 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001503 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001504 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001505 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001506
1507 video_stream_encoder_->Stop();
1508}
1509
1510TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001511 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001512 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001513 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1514 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001515
1516 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001517 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001518 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001519 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001520 fake_encoder_.SetResolutionBitrateLimits(
1521 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1522
1523 VideoEncoderConfig video_encoder_config;
1524 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1525 video_encoder_config.max_bitrate_bps = 0;
1526 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1527 kMaxPayloadLength);
1528
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001529 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001530 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1531 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001532 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1533 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001534 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1535 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1536
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001537 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001538 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1539 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001540 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1541 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001542 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1543 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1544
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001545 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001546 // encoder for 360p should be used.
1547 video_source_.IncomingCapturedFrame(
1548 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1549 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001550 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1551 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001552 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1553 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1554
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001555 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001556 // ignored.
1557 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1558 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001559 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1560 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001561 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1562 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001563 EXPECT_NE(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_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1566 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1567
1568 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1569 // for 270p should be used.
1570 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1571 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001572 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1573 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001574 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1575 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1576
1577 video_stream_encoder_->Stop();
1578}
1579
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001580TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1581 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001582 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1583 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001584
1585 VideoEncoderConfig video_encoder_config;
1586 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1587 video_encoder_config.max_bitrate_bps = 0;
1588 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1589 kMaxPayloadLength);
1590
1591 // Encode 720p frame to get the default encoder target bitrate.
1592 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1593 WaitForEncodedFrame(1);
1594 const uint32_t kDefaultTargetBitrateFor720pKbps =
1595 bitrate_allocator_factory_.codec_config()
1596 .simulcastStream[0]
1597 .targetBitrate;
1598
1599 // Set the max recommended encoder bitrate to something lower than the default
1600 // target bitrate.
1601 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1602 1280 * 720, 10 * 1000, 10 * 1000,
1603 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1604 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1605
1606 // Change resolution to trigger encoder reinitialization.
1607 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1608 WaitForEncodedFrame(2);
1609 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1610 WaitForEncodedFrame(3);
1611
1612 // Ensure the target bitrate is capped by the max bitrate.
1613 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1614 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1615 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1616 .simulcastStream[0]
1617 .targetBitrate *
1618 1000,
1619 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1620
1621 video_stream_encoder_->Stop();
1622}
1623
mflodmancc3d4422017-08-03 08:27:51 -07001624TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001625 EXPECT_TRUE(video_source_.has_sinks());
1626 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001627 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001628 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001629 EXPECT_FALSE(video_source_.has_sinks());
1630 EXPECT_TRUE(new_video_source.has_sinks());
1631
mflodmancc3d4422017-08-03 08:27:51 -07001632 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001633}
1634
mflodmancc3d4422017-08-03 08:27:51 -07001635TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001636 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001637 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001638 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001639 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001640}
1641
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001642TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1643 constexpr int kRequestedResolutionAlignment = 7;
1644 video_source_.set_adaptation_enabled(true);
1645 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1646 video_stream_encoder_->OnBitrateUpdated(
1647 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1648 DataRate::bps(kTargetBitrateBps), 0, 0);
1649
1650 // On the 1st frame, we should have initialized the encoder and
1651 // asked for its resolution requirements.
1652 video_source_.IncomingCapturedFrame(
1653 CreateFrame(1, codec_width_, codec_height_));
1654 WaitForEncodedFrame(1);
1655 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1656 kRequestedResolutionAlignment);
1657
1658 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1659 // (It's up the to the encoder to potentially drop the previous frame,
1660 // to avoid coding back-to-back keyframes.)
1661 video_source_.IncomingCapturedFrame(
1662 CreateFrame(2, codec_width_, codec_height_));
1663 WaitForEncodedFrame(2);
1664 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1665
1666 video_stream_encoder_->Stop();
1667}
1668
Jonathan Yubc771b72017-12-08 17:04:29 -08001669TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1670 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001671 const int kWidth = 1280;
1672 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001673
1674 // We rely on the automatic resolution adaptation, but we handle framerate
1675 // adaptation manually by mocking the stats proxy.
1676 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001677
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001678 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001679 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001680 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1681 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001682 video_stream_encoder_->SetSource(&video_source_,
1683 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001684 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001685 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001686 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001687 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1688
Jonathan Yubc771b72017-12-08 17:04:29 -08001689 // Adapt down as far as possible.
1690 rtc::VideoSinkWants last_wants;
1691 int64_t t = 1;
1692 int loop_count = 0;
1693 do {
1694 ++loop_count;
1695 last_wants = video_source_.sink_wants();
1696
1697 // Simulate the framerate we've been asked to adapt to.
1698 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1699 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1700 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1701 mock_stats.input_frame_rate = fps;
1702 stats_proxy_->SetMockStats(mock_stats);
1703
1704 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1705 sink_.WaitForEncodedFrame(t);
1706 t += frame_interval_ms;
1707
mflodmancc3d4422017-08-03 08:27:51 -07001708 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001709 VerifyBalancedModeFpsRange(
1710 video_source_.sink_wants(),
1711 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1712 } while (video_source_.sink_wants().max_pixel_count <
1713 last_wants.max_pixel_count ||
1714 video_source_.sink_wants().max_framerate_fps <
1715 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001716
Jonathan Yubc771b72017-12-08 17:04:29 -08001717 // Verify that we've adapted all the way down.
1718 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001719 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001720 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1721 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001722 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001723 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1724 *video_source_.last_sent_height());
1725 EXPECT_EQ(kMinBalancedFramerateFps,
1726 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001727
Jonathan Yubc771b72017-12-08 17:04:29 -08001728 // Adapt back up the same number of times we adapted down.
1729 for (int i = 0; i < loop_count - 1; ++i) {
1730 last_wants = video_source_.sink_wants();
1731
1732 // Simulate the framerate we've been asked to adapt to.
1733 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1734 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1735 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1736 mock_stats.input_frame_rate = fps;
1737 stats_proxy_->SetMockStats(mock_stats);
1738
1739 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1740 sink_.WaitForEncodedFrame(t);
1741 t += frame_interval_ms;
1742
mflodmancc3d4422017-08-03 08:27:51 -07001743 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001744 VerifyBalancedModeFpsRange(
1745 video_source_.sink_wants(),
1746 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1747 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1748 last_wants.max_pixel_count ||
1749 video_source_.sink_wants().max_framerate_fps >
1750 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001751 }
1752
Åsa Persson8c1bf952018-09-13 10:42:19 +02001753 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001754 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001755 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001756 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1757 EXPECT_EQ((loop_count - 1) * 2,
1758 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001759
mflodmancc3d4422017-08-03 08:27:51 -07001760 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001761}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001762
mflodmancc3d4422017-08-03 08:27:51 -07001763TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001764 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001765 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1766 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001767 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001768
sprangc5d62e22017-04-02 23:53:04 -07001769 const int kFrameWidth = 1280;
1770 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001771
Åsa Persson8c1bf952018-09-13 10:42:19 +02001772 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001773
kthelgason5e13d412016-12-01 03:59:51 -08001774 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001775 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001776 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001777 frame_timestamp += kFrameIntervalMs;
1778
perkj803d97f2016-11-01 11:45:46 -07001779 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001780 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001781 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001782 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001783 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001784 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001785
asapersson0944a802017-04-07 00:57:58 -07001786 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001787 // wanted resolution.
1788 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1789 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1790 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001791 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001792
1793 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001794 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001795 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001796 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001797 // Give the encoder queue time to process the change in degradation preference
1798 // by waiting for an encoded frame.
1799 new_video_source.IncomingCapturedFrame(
1800 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1801 sink_.WaitForEncodedFrame(frame_timestamp);
1802 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001803 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001804 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001805
sprangc5d62e22017-04-02 23:53:04 -07001806 // Force an input frame rate to be available, or the adaptation call won't
1807 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001808 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001809 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001810 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001811 stats_proxy_->SetMockStats(stats);
1812
mflodmancc3d4422017-08-03 08:27:51 -07001813 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001814 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001815 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001816 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001817 frame_timestamp += kFrameIntervalMs;
1818
1819 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001820 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001821 EXPECT_EQ(std::numeric_limits<int>::max(),
1822 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001823 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001824
asapersson02465b82017-04-10 01:12:52 -07001825 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001826 video_stream_encoder_->SetSource(&new_video_source,
1827 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001828 // Give the encoder queue time to process the change in degradation preference
1829 // by waiting for an encoded frame.
1830 new_video_source.IncomingCapturedFrame(
1831 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1832 sink_.WaitForEncodedFrame(frame_timestamp);
1833 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001834 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001835
mflodmancc3d4422017-08-03 08:27:51 -07001836 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001837 new_video_source.IncomingCapturedFrame(
1838 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 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001843 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001844
1845 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001846 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001847 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001848 // Give the encoder queue time to process the change in degradation preference
1849 // by waiting for an encoded frame.
1850 new_video_source.IncomingCapturedFrame(
1851 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1852 sink_.WaitForEncodedFrame(frame_timestamp);
1853 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001854 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1855 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001856 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001857 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001858
1859 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001860 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001861 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001862 // Give the encoder queue time to process the change in degradation preference
1863 // by waiting for an encoded frame.
1864 new_video_source.IncomingCapturedFrame(
1865 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1866 sink_.WaitForEncodedFrame(frame_timestamp);
1867 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001868 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1869 EXPECT_EQ(std::numeric_limits<int>::max(),
1870 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001871 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001872
mflodmancc3d4422017-08-03 08:27:51 -07001873 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001874}
1875
mflodmancc3d4422017-08-03 08:27:51 -07001876TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001877 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001878 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1879 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001880
asaperssonfab67072017-04-04 05:51:49 -07001881 const int kWidth = 1280;
1882 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001883 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001884 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001885 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1886 EXPECT_FALSE(stats.bw_limited_resolution);
1887 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1888
1889 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001890 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001891 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001892 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001893
1894 stats = stats_proxy_->GetStats();
1895 EXPECT_TRUE(stats.bw_limited_resolution);
1896 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1897
1898 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001899 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001900 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001901 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001902
1903 stats = stats_proxy_->GetStats();
1904 EXPECT_FALSE(stats.bw_limited_resolution);
1905 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1906 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1907
mflodmancc3d4422017-08-03 08:27:51 -07001908 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001909}
1910
mflodmancc3d4422017-08-03 08:27:51 -07001911TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001912 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001913 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1914 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001915
1916 const int kWidth = 1280;
1917 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001918 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001919 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001920 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1921 EXPECT_FALSE(stats.cpu_limited_resolution);
1922 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1923
1924 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001925 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001926 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001927 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001928
1929 stats = stats_proxy_->GetStats();
1930 EXPECT_TRUE(stats.cpu_limited_resolution);
1931 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1932
1933 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001934 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001935 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001936 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001937
1938 stats = stats_proxy_->GetStats();
1939 EXPECT_FALSE(stats.cpu_limited_resolution);
1940 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001941 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001942
mflodmancc3d4422017-08-03 08:27:51 -07001943 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001944}
1945
mflodmancc3d4422017-08-03 08:27:51 -07001946TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001947 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001948 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1949 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001950
asaperssonfab67072017-04-04 05:51:49 -07001951 const int kWidth = 1280;
1952 const int kHeight = 720;
1953 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001954 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001955 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001956 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001957 EXPECT_FALSE(stats.cpu_limited_resolution);
1958 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1959
asaperssonfab67072017-04-04 05:51:49 -07001960 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001961 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001962 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001963 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001964 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001965 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001966 EXPECT_TRUE(stats.cpu_limited_resolution);
1967 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1968
1969 // Set new source with adaptation still enabled.
1970 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001971 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001972 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001973
asaperssonfab67072017-04-04 05:51:49 -07001974 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001975 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001976 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001977 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001978 EXPECT_TRUE(stats.cpu_limited_resolution);
1979 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1980
1981 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001982 video_stream_encoder_->SetSource(&new_video_source,
1983 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001984
asaperssonfab67072017-04-04 05:51:49 -07001985 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001986 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001987 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001988 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001989 EXPECT_FALSE(stats.cpu_limited_resolution);
1990 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1991
1992 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001993 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001994 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001995
asaperssonfab67072017-04-04 05:51:49 -07001996 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001997 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001998 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001999 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002000 EXPECT_TRUE(stats.cpu_limited_resolution);
2001 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2002
asaperssonfab67072017-04-04 05:51:49 -07002003 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002004 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002005 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002006 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002007 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002008 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002009 EXPECT_FALSE(stats.cpu_limited_resolution);
2010 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002011 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002012
mflodmancc3d4422017-08-03 08:27:51 -07002013 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002014}
2015
mflodmancc3d4422017-08-03 08:27:51 -07002016TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002017 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002018 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2019 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002020
asaperssonfab67072017-04-04 05:51:49 -07002021 const int kWidth = 1280;
2022 const int kHeight = 720;
2023 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002024 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002025 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002026 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002027 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002028 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002029
2030 // Set new source with adaptation still enabled.
2031 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002032 video_stream_encoder_->SetSource(&new_video_source,
2033 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002034
asaperssonfab67072017-04-04 05:51:49 -07002035 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002036 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002037 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002038 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002039 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002040 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002041
asaperssonfab67072017-04-04 05:51:49 -07002042 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002043 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002044 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002045 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002046 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002047 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002048 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002049 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002050
asaperssonfab67072017-04-04 05:51:49 -07002051 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002052 video_stream_encoder_->SetSource(&new_video_source,
2053 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002054
asaperssonfab67072017-04-04 05:51:49 -07002055 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002056 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002057 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002058 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002059 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002060 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002061
asapersson02465b82017-04-10 01:12:52 -07002062 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002063 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002064 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002065
asaperssonfab67072017-04-04 05:51:49 -07002066 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002067 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002068 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002069 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002070 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002071 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2072 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002073
mflodmancc3d4422017-08-03 08:27:51 -07002074 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002075}
2076
mflodmancc3d4422017-08-03 08:27:51 -07002077TEST_F(VideoStreamEncoderTest,
2078 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002079 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002080 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2081 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002082
2083 const int kWidth = 1280;
2084 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002085 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002086 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002087 video_source_.IncomingCapturedFrame(
2088 CreateFrame(timestamp_ms, kWidth, kHeight));
2089 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002090 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2091 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2092 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2093
2094 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002095 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002096 timestamp_ms += kFrameIntervalMs;
2097 video_source_.IncomingCapturedFrame(
2098 CreateFrame(timestamp_ms, kWidth, kHeight));
2099 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002100 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2101 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2102 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2103
2104 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002105 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002106 timestamp_ms += kFrameIntervalMs;
2107 video_source_.IncomingCapturedFrame(
2108 CreateFrame(timestamp_ms, kWidth, kHeight));
2109 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002110 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2111 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2112 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2113
Niels Möller4db138e2018-04-19 09:04:13 +02002114 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002115 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002116
2117 VideoEncoderConfig video_encoder_config;
2118 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2119 // Make format different, to force recreation of encoder.
2120 video_encoder_config.video_format.parameters["foo"] = "foo";
2121 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002122 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002123 timestamp_ms += kFrameIntervalMs;
2124 video_source_.IncomingCapturedFrame(
2125 CreateFrame(timestamp_ms, kWidth, kHeight));
2126 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002127 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2128 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2129 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2130
mflodmancc3d4422017-08-03 08:27:51 -07002131 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002132}
2133
mflodmancc3d4422017-08-03 08:27:51 -07002134TEST_F(VideoStreamEncoderTest,
2135 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002136 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002137 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2138 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002139
asapersson0944a802017-04-07 00:57:58 -07002140 const int kWidth = 1280;
2141 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002142 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002143
asaperssonfab67072017-04-04 05:51:49 -07002144 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002145 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002146 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002147 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002148 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002149 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2150
asapersson02465b82017-04-10 01:12:52 -07002151 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002152 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002153 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002154 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002155 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002156 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002157 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002158 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2159
2160 // Set new source with adaptation still enabled.
2161 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002162 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002163 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002164
2165 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002166 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002167 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002168 stats = stats_proxy_->GetStats();
2169 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002170 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002171 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2172
sprangc5d62e22017-04-02 23:53:04 -07002173 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002174 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002175 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002176 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002177 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002178 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002179 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002180 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002181 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002182 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002183 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2184
sprangc5d62e22017-04-02 23:53:04 -07002185 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002186 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002187 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2188 mock_stats.input_frame_rate = 30;
2189 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002190 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002191 stats_proxy_->ResetMockStats();
2192
2193 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002194 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002195 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002196
2197 // Framerate now adapted.
2198 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002199 EXPECT_FALSE(stats.cpu_limited_resolution);
2200 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002201 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2202
2203 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002204 video_stream_encoder_->SetSource(&new_video_source,
2205 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002206 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002207 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002208 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002209
2210 stats = stats_proxy_->GetStats();
2211 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002212 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002213 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2214
2215 // Try to trigger overuse. Should not succeed.
2216 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002217 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002218 stats_proxy_->ResetMockStats();
2219
2220 stats = stats_proxy_->GetStats();
2221 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002222 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002223 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2224
2225 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002226 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002227 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002228 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002229 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002230 stats = stats_proxy_->GetStats();
2231 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002232 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002233 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002234
2235 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002236 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002237 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002238 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002239 stats = stats_proxy_->GetStats();
2240 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002241 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002242 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2243
2244 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002245 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002246 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002247 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002248 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002249 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002250 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002251 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002252 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002253 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002254 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2255
2256 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002257 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002258 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002259 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002260 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002261 stats = stats_proxy_->GetStats();
2262 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002263 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002264 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002265 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002266
mflodmancc3d4422017-08-03 08:27:51 -07002267 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002268}
2269
mflodmancc3d4422017-08-03 08:27:51 -07002270TEST_F(VideoStreamEncoderTest,
2271 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002272 const int kWidth = 1280;
2273 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002274 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002275 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2276 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002277
asaperssonfab67072017-04-04 05:51:49 -07002278 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002279 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002280
asaperssonfab67072017-04-04 05:51:49 -07002281 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002282 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002283
asaperssonfab67072017-04-04 05:51:49 -07002284 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002285 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002286
asaperssonfab67072017-04-04 05:51:49 -07002287 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002288 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002289
kthelgason876222f2016-11-29 01:44:11 -08002290 // Expect a scale down.
2291 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002292 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002293
asapersson02465b82017-04-10 01:12:52 -07002294 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002295 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002296 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002297 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002298
asaperssonfab67072017-04-04 05:51:49 -07002299 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002300 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002301 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002302 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002303
asaperssonfab67072017-04-04 05:51:49 -07002304 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002305 EXPECT_EQ(std::numeric_limits<int>::max(),
2306 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002307
asaperssonfab67072017-04-04 05:51:49 -07002308 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002309 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002310 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002311 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002312
asapersson02465b82017-04-10 01:12:52 -07002313 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002314 EXPECT_EQ(std::numeric_limits<int>::max(),
2315 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002316
mflodmancc3d4422017-08-03 08:27:51 -07002317 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002318}
2319
mflodmancc3d4422017-08-03 08:27:51 -07002320TEST_F(VideoStreamEncoderTest,
2321 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002322 const int kWidth = 1280;
2323 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002324 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002325 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2326 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002327
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002328 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002329 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002330 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002331 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002332
2333 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002334 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002335 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002336 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2337 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2338
2339 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002340 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002341 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002342 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2343 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2344 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2345
2346 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002347 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002348 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2349 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2350 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2351
mflodmancc3d4422017-08-03 08:27:51 -07002352 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002353}
2354
mflodmancc3d4422017-08-03 08:27:51 -07002355TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002356 const int kWidth = 1280;
2357 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002358 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002359 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2360 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002361
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002362 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002363 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002364 video_stream_encoder_->SetSource(&source,
2365 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002366 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2367 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002368 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002369
2370 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002371 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002372 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2373 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2374 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2375 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2376
2377 // Trigger adapt down for same input resolution, expect no change.
2378 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2379 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002380 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002381 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2382 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2383 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2384
2385 // Trigger adapt down for larger input resolution, expect no change.
2386 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2387 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002388 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002389 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2390 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2391 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2392
mflodmancc3d4422017-08-03 08:27:51 -07002393 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002394}
2395
mflodmancc3d4422017-08-03 08:27:51 -07002396TEST_F(VideoStreamEncoderTest,
2397 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002398 const int kWidth = 1280;
2399 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002400 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002401 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2402 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002403
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002404 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002405 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002406 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002407 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002408
2409 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002410 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002411 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002412 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2413 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2414
2415 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002416 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002417 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002418 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2419 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2420
mflodmancc3d4422017-08-03 08:27:51 -07002421 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002422}
2423
mflodmancc3d4422017-08-03 08:27:51 -07002424TEST_F(VideoStreamEncoderTest,
2425 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002426 const int kWidth = 1280;
2427 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002428 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002429 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2430 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002431
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002432 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002433 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002434 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002435 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002436
2437 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002438 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002439 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002440 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002441 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2442
2443 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002444 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002445 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002446 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002447 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2448
mflodmancc3d4422017-08-03 08:27:51 -07002449 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002450}
2451
mflodmancc3d4422017-08-03 08:27:51 -07002452TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002453 const int kWidth = 1280;
2454 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002455 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002456 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2457 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002458
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002459 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002460 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002461 video_stream_encoder_->SetSource(&source,
2462 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002463
2464 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2465 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002466 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002467 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2468 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2469 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2470
2471 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002472 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002473 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002474 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2475 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2476 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2477
mflodmancc3d4422017-08-03 08:27:51 -07002478 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002479}
2480
mflodmancc3d4422017-08-03 08:27:51 -07002481TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002482 const int kWidth = 1280;
2483 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002484 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002485 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2486 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002487
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002488 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002489 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002490 video_stream_encoder_->SetSource(&source,
2491 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002492
2493 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2494 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002495 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002496 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2497 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2498 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2499
2500 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002501 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002502 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002503 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2504 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2505 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2506
mflodmancc3d4422017-08-03 08:27:51 -07002507 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002508}
2509
mflodmancc3d4422017-08-03 08:27:51 -07002510TEST_F(VideoStreamEncoderTest,
2511 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002512 const int kWidth = 1280;
2513 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002514 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002515 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2516 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002517
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002518 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002519 AdaptingFrameForwarder source;
2520 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002521 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002522 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002523
2524 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002525 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002526 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002527 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2528 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2529
2530 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002531 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002532 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002533 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002534 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002535 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2536 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2537
2538 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002539 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002540 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002541 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2542 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2543 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2544
mflodmancc3d4422017-08-03 08:27:51 -07002545 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002546}
2547
mflodmancc3d4422017-08-03 08:27:51 -07002548TEST_F(VideoStreamEncoderTest,
2549 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002550 const int kWidth = 1280;
2551 const int kHeight = 720;
2552 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002553 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002554 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2555 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002556
2557 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2558 stats.input_frame_rate = kInputFps;
2559 stats_proxy_->SetMockStats(stats);
2560
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002561 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002562 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2563 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002564 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002565
2566 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002567 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002568 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2569 sink_.WaitForEncodedFrame(2);
2570 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2571
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002572 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002573 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002574 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002575 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002576 // Give the encoder queue time to process the change in degradation preference
2577 // by waiting for an encoded frame.
2578 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2579 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002580 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002581
2582 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002583 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002584 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2585 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002586 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2587
2588 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002589 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002590 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002591
mflodmancc3d4422017-08-03 08:27:51 -07002592 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002593}
2594
mflodmancc3d4422017-08-03 08:27:51 -07002595TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002596 const int kWidth = 1280;
2597 const int kHeight = 720;
2598 const size_t kNumFrames = 10;
2599
Erik Språng4c6ca302019-04-08 15:14:01 +02002600 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002601 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2602 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002603
asaperssond0de2952017-04-21 01:47:31 -07002604 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002605 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002606 video_source_.set_adaptation_enabled(true);
2607
2608 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2609 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2610
2611 int downscales = 0;
2612 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002613 video_source_.IncomingCapturedFrame(
2614 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2615 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002616
asaperssonfab67072017-04-04 05:51:49 -07002617 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002618 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002619 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002620 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002621
2622 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2623 ++downscales;
2624
2625 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2626 EXPECT_EQ(downscales,
2627 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2628 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002629 }
mflodmancc3d4422017-08-03 08:27:51 -07002630 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002631}
2632
mflodmancc3d4422017-08-03 08:27:51 -07002633TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002634 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2635 const int kWidth = 1280;
2636 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002637 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002638 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2639 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002640
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002641 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002642 AdaptingFrameForwarder source;
2643 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002644 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002645 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002646
Åsa Persson8c1bf952018-09-13 10:42:19 +02002647 int64_t timestamp_ms = kFrameIntervalMs;
2648 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002649 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002650 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002651 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2652 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2653
2654 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002655 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002656 timestamp_ms += kFrameIntervalMs;
2657 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2658 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002659 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002660 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2661 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2662
2663 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002664 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002665 timestamp_ms += kFrameIntervalMs;
2666 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002667 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002668 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002669 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2670 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2671
2672 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002673 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002674 timestamp_ms += kFrameIntervalMs;
2675 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2676 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002677 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002678 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2679 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2680
2681 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002682 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002683 timestamp_ms += kFrameIntervalMs;
2684 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002685 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002686 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002687 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2688 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2689
mflodmancc3d4422017-08-03 08:27:51 -07002690 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002691}
2692
mflodmancc3d4422017-08-03 08:27:51 -07002693TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002694 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2695 const int kWidth = 1280;
2696 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002697 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002698 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2699 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002700
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002701 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002702 AdaptingFrameForwarder source;
2703 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002704 video_stream_encoder_->SetSource(&source,
2705 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002706
Åsa Persson8c1bf952018-09-13 10:42:19 +02002707 int64_t timestamp_ms = kFrameIntervalMs;
2708 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002709 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002710 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002711 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2712 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2713
2714 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002715 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002716 timestamp_ms += kFrameIntervalMs;
2717 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2718 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002719 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2720 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2721 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2722
2723 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002724 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002725 timestamp_ms += kFrameIntervalMs;
2726 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002727 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002728 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002729 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2730 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2731
2732 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002733 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002734 timestamp_ms += kFrameIntervalMs;
2735 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2736 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002737 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2738 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2739 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2740
2741 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002742 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002743 timestamp_ms += kFrameIntervalMs;
2744 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002745 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002746 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002747 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2748 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2749
mflodmancc3d4422017-08-03 08:27:51 -07002750 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002751}
2752
Sergey Silkin41c650b2019-10-14 13:12:19 +02002753TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2754 fake_encoder_.SetResolutionBitrateLimits(
2755 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2756
2757 video_stream_encoder_->OnBitrateUpdated(
2758 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2759 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2760 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2761
2762 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2763 AdaptingFrameForwarder source;
2764 source.set_adaptation_enabled(true);
2765 video_stream_encoder_->SetSource(
2766 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2767
2768 // Insert 720p frame.
2769 int64_t timestamp_ms = kFrameIntervalMs;
2770 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2771 WaitForEncodedFrame(1280, 720);
2772
2773 // Reduce bitrate and trigger adapt down.
2774 video_stream_encoder_->OnBitrateUpdated(
2775 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2776 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2777 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2778 video_stream_encoder_->TriggerQualityLow();
2779
2780 // Insert 720p frame. It should be downscaled and encoded.
2781 timestamp_ms += kFrameIntervalMs;
2782 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2783 WaitForEncodedFrame(960, 540);
2784
2785 // Trigger adapt up. Higher resolution should not be requested duo to lack
2786 // of bitrate.
2787 video_stream_encoder_->TriggerQualityHigh();
2788 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2789
2790 // Increase bitrate.
2791 video_stream_encoder_->OnBitrateUpdated(
2792 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2793 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2794 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2795
2796 // Trigger adapt up. Higher resolution should be requested.
2797 video_stream_encoder_->TriggerQualityHigh();
2798 VerifyFpsMaxResolutionMax(source.sink_wants());
2799
2800 video_stream_encoder_->Stop();
2801}
2802
2803TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2804 fake_encoder_.SetResolutionBitrateLimits(
2805 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2806
2807 // Set bitrate equal to min bitrate of 540p.
2808 video_stream_encoder_->OnBitrateUpdated(
2809 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2810 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2811 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2812
2813 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2814 AdaptingFrameForwarder source;
2815 source.set_adaptation_enabled(true);
2816 video_stream_encoder_->SetSource(
2817 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2818
2819 // Insert 720p frame. It should be dropped and lower resolution should be
2820 // requested.
2821 int64_t timestamp_ms = kFrameIntervalMs;
2822 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2823 ExpectDroppedFrame();
2824 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2825
2826 // Insert 720p frame. It should be downscaled and encoded.
2827 timestamp_ms += kFrameIntervalMs;
2828 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2829 WaitForEncodedFrame(960, 540);
2830
2831 video_stream_encoder_->Stop();
2832}
2833
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002834class BalancedDegradationTest : public VideoStreamEncoderTest {
2835 protected:
2836 void SetupTest() {
2837 // Reset encoder for field trials to take effect.
2838 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002839 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002840
2841 // Enable BALANCED preference.
2842 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002843 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2844 }
2845
2846 void OnBitrateUpdated(int bitrate_bps) {
2847 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2848 DataRate::bps(bitrate_bps),
2849 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002850 }
2851
Åsa Persson45b176f2019-09-30 11:19:05 +02002852 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002853 timestamp_ms_ += kFrameIntervalMs;
2854 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002855 }
2856
2857 void InsertFrameAndWaitForEncoded() {
2858 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002859 sink_.WaitForEncodedFrame(timestamp_ms_);
2860 }
2861
2862 const int kWidth = 640; // pixels:640x360=230400
2863 const int kHeight = 360;
2864 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2865 int64_t timestamp_ms_ = 0;
2866 AdaptingFrameForwarder source_;
2867};
2868
2869TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2870 test::ScopedFieldTrials field_trials(
2871 "WebRTC-Video-BalancedDegradationSettings/"
2872 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2873 SetupTest();
2874
2875 // Force input frame rate.
2876 const int kInputFps = 24;
2877 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2878 stats.input_frame_rate = kInputFps;
2879 stats_proxy_->SetMockStats(stats);
2880
Åsa Persson45b176f2019-09-30 11:19:05 +02002881 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002882 VerifyFpsMaxResolutionMax(source_.sink_wants());
2883
2884 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2885 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2886 video_stream_encoder_->TriggerQualityLowExpectFalse();
2887 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2888
2889 video_stream_encoder_->Stop();
2890}
2891
2892TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
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 = 25;
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:1) == threshold, expect AdaptDown to return true.
2909 video_stream_encoder_->TriggerQualityLow();
2910 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2911
2912 video_stream_encoder_->Stop();
2913}
2914
2915TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2916 test::ScopedFieldTrials field_trials(
2917 "WebRTC-Video-BalancedDegradationSettings/"
2918 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2919 SetupTest();
2920
2921 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2922
Åsa Persson45b176f2019-09-30 11:19:05 +02002923 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002924 VerifyFpsMaxResolutionMax(source_.sink_wants());
2925
2926 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2927 video_stream_encoder_->TriggerQualityLow();
2928 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2929
2930 video_stream_encoder_->Stop();
2931}
2932
Åsa Perssonccfb3402019-09-25 15:13:04 +02002933TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002934 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002935 "WebRTC-Video-BalancedDegradationSettings/"
2936 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002937 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002938
Åsa Persson1b247f12019-08-14 17:26:39 +02002939 const int kMinBitrateBps = 425000;
2940 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002941 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002942
Åsa Persson45b176f2019-09-30 11:19:05 +02002943 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002944 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002945 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2946
2947 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2948 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002949 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002950 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002951 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2952
2953 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2954 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002955 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002956 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002957 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2958
Åsa Persson30ab0152019-08-27 12:22:33 +02002959 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2960 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002961 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002962 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2963 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002964 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2965
2966 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002967 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002968 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002969 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002970
Åsa Persson30ab0152019-08-27 12:22:33 +02002971 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002972 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002973 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002974 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002975 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002976 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2977
2978 video_stream_encoder_->Stop();
2979}
2980
Åsa Perssonccfb3402019-09-25 15:13:04 +02002981TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002982 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2983 test::ScopedFieldTrials field_trials(
2984 "WebRTC-Video-BalancedDegradationSettings/"
2985 "pixels:57600|129600|230400,fps:7|24|24/");
2986 SetupTest();
2987 OnBitrateUpdated(kLowTargetBitrateBps);
2988
2989 VerifyNoLimitation(source_.sink_wants());
2990
2991 // Insert frame, expect scaled down:
2992 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2993 InsertFrame();
2994 EXPECT_FALSE(WaitForFrame(1000));
2995 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2996 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2997
2998 // Insert frame, expect scaled down:
2999 // resolution (320x180@24fps).
3000 InsertFrame();
3001 EXPECT_FALSE(WaitForFrame(1000));
3002 EXPECT_LT(source_.sink_wants().max_pixel_count,
3003 source_.last_wants().max_pixel_count);
3004 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3005
3006 // Frame should not be dropped (min pixels per frame reached).
3007 InsertFrameAndWaitForEncoded();
3008
3009 video_stream_encoder_->Stop();
3010}
3011
3012TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003013 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003014 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003015 "WebRTC-Video-BalancedDegradationSettings/"
3016 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003017 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003018
Åsa Persson30ab0152019-08-27 12:22:33 +02003019 const int kResolutionMinBitrateBps = 435000;
3020 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003021 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003022
Åsa Persson45b176f2019-09-30 11:19:05 +02003023 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003024 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003025 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3026
3027 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3028 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003029 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003030 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003031 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3032
3033 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3034 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003035 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003036 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003037 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3038
3039 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3040 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003041 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003042 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003043 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3044
Åsa Persson30ab0152019-08-27 12:22:33 +02003045 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3046 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003047 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003048 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003049 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3050
3051 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3052 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003053 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003054 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3055
3056 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003057 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003058 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003059 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003060 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003061 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3062
3063 video_stream_encoder_->Stop();
3064}
3065
Åsa Perssonccfb3402019-09-25 15:13:04 +02003066TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003067 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003068 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003069 "WebRTC-Video-BalancedDegradationSettings/"
3070 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003071 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003072
Åsa Persson30ab0152019-08-27 12:22:33 +02003073 const int kMinBitrateBps = 425000;
3074 const int kTooLowMinBitrateBps = 424000;
3075 const int kResolutionMinBitrateBps = 435000;
3076 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003077 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003078
Åsa Persson45b176f2019-09-30 11:19:05 +02003079 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003080 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003081 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3082
3083 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3084 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003085 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003086 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003087 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3088
3089 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3090 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003091 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003092 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003093 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3094
3095 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3096 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003097 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003098 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003099 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3100
3101 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3102 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003103 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003104 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3105
3106 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003107 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003108 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003109 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003110 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003111 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3112
3113 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003114 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003115 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003116 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003117 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3118
3119 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003120 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003121 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003122 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003123 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003124 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3125
Åsa Persson1b247f12019-08-14 17:26:39 +02003126 video_stream_encoder_->Stop();
3127}
3128
mflodmancc3d4422017-08-03 08:27:51 -07003129TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003130 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3131 const int kWidth = 1280;
3132 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003133 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003134 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3135 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003136
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003137 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003138 AdaptingFrameForwarder source;
3139 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003140 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003141 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003142
Åsa Persson8c1bf952018-09-13 10:42:19 +02003143 int64_t timestamp_ms = kFrameIntervalMs;
3144 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003145 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003146 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003147 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3148 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3149 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3150 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3151
3152 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003153 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003154 timestamp_ms += kFrameIntervalMs;
3155 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3156 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003157 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003158 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3159 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3160 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3161 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3162
3163 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003164 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003165 timestamp_ms += kFrameIntervalMs;
3166 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3167 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003168 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003169 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3170 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3171 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3172 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3173
Jonathan Yubc771b72017-12-08 17:04:29 -08003174 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003175 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003176 timestamp_ms += kFrameIntervalMs;
3177 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3178 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003179 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003180 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3181 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003182 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003183 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3184
Jonathan Yubc771b72017-12-08 17:04:29 -08003185 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003186 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003187 timestamp_ms += kFrameIntervalMs;
3188 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3189 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003190 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003191 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003192 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3193 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3194 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3195 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3196
Jonathan Yubc771b72017-12-08 17:04:29 -08003197 // Trigger quality adapt down, expect no change (min resolution reached).
3198 video_stream_encoder_->TriggerQualityLow();
Å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 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3203 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3204 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3205 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3206 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3207
3208 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003209 video_stream_encoder_->TriggerCpuNormalUsage();
Å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 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003214 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3215 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3216 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3217 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3218
3219 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3220 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003221 timestamp_ms += kFrameIntervalMs;
3222 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3223 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003224 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3225 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3226 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3227 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3228 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3229
3230 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3231 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003232 timestamp_ms += kFrameIntervalMs;
3233 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3234 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003235 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003236 last_wants = source.sink_wants();
3237 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3238 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003239 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003240 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3241
3242 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003243 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);
asapersson09f05612017-05-15 23:40:18 -07003247 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003248 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3249 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003250 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003251 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3252
3253 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003254 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003255 timestamp_ms += kFrameIntervalMs;
3256 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003257 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003258 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003259 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003260 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3261 EXPECT_FALSE(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(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003264
mflodmancc3d4422017-08-03 08:27:51 -07003265 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003266}
3267
mflodmancc3d4422017-08-03 08:27:51 -07003268TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003269 const int kWidth = 640;
3270 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003271
Erik Språng4c6ca302019-04-08 15:14:01 +02003272 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003273 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3274 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003275
perkj803d97f2016-11-01 11:45:46 -07003276 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003277 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003278 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003279 }
3280
mflodmancc3d4422017-08-03 08:27:51 -07003281 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003282 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003283 video_source_.IncomingCapturedFrame(CreateFrame(
3284 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003285 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003286 }
3287
mflodmancc3d4422017-08-03 08:27:51 -07003288 video_stream_encoder_->Stop();
3289 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003290 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003291
Ying Wangef3998f2019-12-09 13:06:53 +01003292 EXPECT_METRIC_EQ(
3293 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3294 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003295 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3296}
3297
mflodmancc3d4422017-08-03 08:27:51 -07003298TEST_F(VideoStreamEncoderTest,
3299 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003300 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003301 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3302 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003303 const int kWidth = 640;
3304 const int kHeight = 360;
3305
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003306 video_stream_encoder_->SetSource(&video_source_,
3307 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003308
3309 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3310 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003311 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003312 }
3313
mflodmancc3d4422017-08-03 08:27:51 -07003314 video_stream_encoder_->Stop();
3315 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003316 stats_proxy_.reset();
3317
3318 EXPECT_EQ(0,
3319 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3320}
3321
mflodmancc3d4422017-08-03 08:27:51 -07003322TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003323 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003324 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003325
3326 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003327 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003328 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003329 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3330 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003331
sprang57c2fff2017-01-16 06:24:02 -08003332 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003333 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003334 video_stream_encoder_->OnBitrateUpdated(
3335 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3336 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003337
sprang57c2fff2017-01-16 06:24:02 -08003338 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003339 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3340 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003341 VideoBitrateAllocation bitrate_allocation =
3342 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003343 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003344 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003345 // TODO(srte): The use of millisecs here looks like an error, but the tests
3346 // fails using seconds, this should be investigated.
3347 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003348
3349 // Not called on second frame.
3350 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3351 .Times(0);
3352 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003353 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3354 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003355 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003356
3357 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003358 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3359 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003360 const int64_t start_time_ms = rtc::TimeMillis();
3361 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3362 video_source_.IncomingCapturedFrame(
3363 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3364 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003365 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003366 }
3367
3368 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003369 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003370
mflodmancc3d4422017-08-03 08:27:51 -07003371 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003372}
3373
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003374TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3375 // 2 TLs configured, temporal layers supported by encoder.
3376 const int kNumTemporalLayers = 2;
3377 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3378 fake_encoder_.SetTemporalLayersSupported(0, true);
3379
3380 // Bitrate allocated across temporal layers.
3381 const int kTl0Bps = kTargetBitrateBps *
3382 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003383 kNumTemporalLayers, /*temporal_id*/ 0,
3384 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003385 const int kTl1Bps = kTargetBitrateBps *
3386 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003387 kNumTemporalLayers, /*temporal_id*/ 1,
3388 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003389 VideoBitrateAllocation expected_bitrate;
3390 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3391 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3392
3393 VerifyAllocatedBitrate(expected_bitrate);
3394 video_stream_encoder_->Stop();
3395}
3396
3397TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3398 // 2 TLs configured, temporal layers not supported by encoder.
3399 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3400 fake_encoder_.SetTemporalLayersSupported(0, false);
3401
3402 // Temporal layers not supported by the encoder.
3403 // Total bitrate should be at ti:0.
3404 VideoBitrateAllocation expected_bitrate;
3405 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3406
3407 VerifyAllocatedBitrate(expected_bitrate);
3408 video_stream_encoder_->Stop();
3409}
3410
3411TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3412 // 2 TLs configured, temporal layers only supported for first stream.
3413 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3414 fake_encoder_.SetTemporalLayersSupported(0, true);
3415 fake_encoder_.SetTemporalLayersSupported(1, false);
3416
3417 const int kS0Bps = 150000;
3418 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003419 kS0Bps *
3420 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3421 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003422 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003423 kS0Bps *
3424 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3425 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003426 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3427 // Temporal layers not supported by si:1.
3428 VideoBitrateAllocation expected_bitrate;
3429 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3430 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3431 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3432
3433 VerifyAllocatedBitrate(expected_bitrate);
3434 video_stream_encoder_->Stop();
3435}
3436
Niels Möller7dc26b72017-12-06 10:27:48 +01003437TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3438 const int kFrameWidth = 1280;
3439 const int kFrameHeight = 720;
3440 const int kFramerate = 24;
3441
Erik Språng4c6ca302019-04-08 15:14:01 +02003442 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003443 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3444 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003445 test::FrameForwarder source;
3446 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003447 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003448
3449 // Insert a single frame, triggering initial configuration.
3450 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3451 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3452
3453 EXPECT_EQ(
3454 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3455 kDefaultFramerate);
3456
3457 // Trigger reconfigure encoder (without resetting the entire instance).
3458 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003459 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003460 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3461 video_encoder_config.number_of_streams = 1;
3462 video_encoder_config.video_stream_factory =
3463 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3464 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003465 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003466 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3467
3468 // Detector should be updated with fps limit from codec config.
3469 EXPECT_EQ(
3470 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3471 kFramerate);
3472
3473 // Trigger overuse, max framerate should be reduced.
3474 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3475 stats.input_frame_rate = kFramerate;
3476 stats_proxy_->SetMockStats(stats);
3477 video_stream_encoder_->TriggerCpuOveruse();
3478 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3479 int adapted_framerate =
3480 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3481 EXPECT_LT(adapted_framerate, kFramerate);
3482
3483 // Trigger underuse, max framerate should go back to codec configured fps.
3484 // Set extra low fps, to make sure it's actually reset, not just incremented.
3485 stats = stats_proxy_->GetStats();
3486 stats.input_frame_rate = adapted_framerate / 2;
3487 stats_proxy_->SetMockStats(stats);
3488 video_stream_encoder_->TriggerCpuNormalUsage();
3489 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3490 EXPECT_EQ(
3491 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3492 kFramerate);
3493
3494 video_stream_encoder_->Stop();
3495}
3496
3497TEST_F(VideoStreamEncoderTest,
3498 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3499 const int kFrameWidth = 1280;
3500 const int kFrameHeight = 720;
3501 const int kLowFramerate = 15;
3502 const int kHighFramerate = 25;
3503
Erik Språng4c6ca302019-04-08 15:14:01 +02003504 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003505 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3506 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003507 test::FrameForwarder source;
3508 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003509 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003510
3511 // Trigger initial configuration.
3512 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003513 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003514 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3515 video_encoder_config.number_of_streams = 1;
3516 video_encoder_config.video_stream_factory =
3517 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3518 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3519 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003520 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003521 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3522
3523 EXPECT_EQ(
3524 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3525 kLowFramerate);
3526
3527 // Trigger overuse, max framerate should be reduced.
3528 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3529 stats.input_frame_rate = kLowFramerate;
3530 stats_proxy_->SetMockStats(stats);
3531 video_stream_encoder_->TriggerCpuOveruse();
3532 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3533 int adapted_framerate =
3534 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3535 EXPECT_LT(adapted_framerate, kLowFramerate);
3536
3537 // Reconfigure the encoder with a new (higher max framerate), max fps should
3538 // still respect the adaptation.
3539 video_encoder_config.video_stream_factory =
3540 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
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 adapted_framerate);
3549
3550 // Trigger underuse, max framerate should go back to codec configured fps.
3551 stats = stats_proxy_->GetStats();
3552 stats.input_frame_rate = adapted_framerate;
3553 stats_proxy_->SetMockStats(stats);
3554 video_stream_encoder_->TriggerCpuNormalUsage();
3555 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3556 EXPECT_EQ(
3557 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3558 kHighFramerate);
3559
3560 video_stream_encoder_->Stop();
3561}
3562
mflodmancc3d4422017-08-03 08:27:51 -07003563TEST_F(VideoStreamEncoderTest,
3564 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003565 const int kFrameWidth = 1280;
3566 const int kFrameHeight = 720;
3567 const int kFramerate = 24;
3568
Erik Språng4c6ca302019-04-08 15:14:01 +02003569 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003570 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3571 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003572 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003573 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003574 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003575
3576 // Trigger initial configuration.
3577 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003578 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003579 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3580 video_encoder_config.number_of_streams = 1;
3581 video_encoder_config.video_stream_factory =
3582 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3583 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003584 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003585 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003586 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003587
Niels Möller7dc26b72017-12-06 10:27:48 +01003588 EXPECT_EQ(
3589 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3590 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003591
3592 // Trigger overuse, max framerate should be reduced.
3593 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3594 stats.input_frame_rate = kFramerate;
3595 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003596 video_stream_encoder_->TriggerCpuOveruse();
3597 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003598 int adapted_framerate =
3599 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003600 EXPECT_LT(adapted_framerate, kFramerate);
3601
3602 // Change degradation preference to not enable framerate scaling. Target
3603 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003604 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003605 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003606 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003607 EXPECT_EQ(
3608 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3609 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003610
mflodmancc3d4422017-08-03 08:27:51 -07003611 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003612}
3613
mflodmancc3d4422017-08-03 08:27:51 -07003614TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003615 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003616 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003617 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003618 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003619 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003620 const int kWidth = 640;
3621 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003622
asaperssonfab67072017-04-04 05:51:49 -07003623 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003624
3625 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003626 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003627
3628 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003629 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003630
sprangc5d62e22017-04-02 23:53:04 -07003631 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003632
asaperssonfab67072017-04-04 05:51:49 -07003633 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003634 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003635 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003636
3637 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003638 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003639
sprangc5d62e22017-04-02 23:53:04 -07003640 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003641
mflodmancc3d4422017-08-03 08:27:51 -07003642 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003643}
3644
mflodmancc3d4422017-08-03 08:27:51 -07003645TEST_F(VideoStreamEncoderTest,
3646 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003647 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003648 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003649 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003650 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003651 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003652 const int kWidth = 640;
3653 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003654
3655 // We expect the n initial frames to get dropped.
3656 int i;
3657 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003658 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003659 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003660 }
3661 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003662 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003663 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003664
3665 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003666 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003667
mflodmancc3d4422017-08-03 08:27:51 -07003668 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003669}
3670
mflodmancc3d4422017-08-03 08:27:51 -07003671TEST_F(VideoStreamEncoderTest,
3672 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003673 const int kWidth = 640;
3674 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003675 video_stream_encoder_->OnBitrateUpdated(
3676 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3677 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003678
3679 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003680 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003681 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003682
asaperssonfab67072017-04-04 05:51:49 -07003683 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003684 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003685 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003686
mflodmancc3d4422017-08-03 08:27:51 -07003687 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003688}
3689
mflodmancc3d4422017-08-03 08:27:51 -07003690TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003691 const int kWidth = 640;
3692 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003693 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003694
3695 VideoEncoderConfig video_encoder_config;
3696 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3697 // Make format different, to force recreation of encoder.
3698 video_encoder_config.video_format.parameters["foo"] = "foo";
3699 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003700 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003701 video_stream_encoder_->OnBitrateUpdated(
3702 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3703 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003704
kthelgasonb83797b2017-02-14 11:57:25 -08003705 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003706 video_stream_encoder_->SetSource(&video_source_,
3707 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003708
asaperssonfab67072017-04-04 05:51:49 -07003709 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003710 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003711 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003712
mflodmancc3d4422017-08-03 08:27:51 -07003713 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003714 fake_encoder_.SetQualityScaling(true);
3715}
3716
Åsa Persson139f4dc2019-08-02 09:29:58 +02003717TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3718 webrtc::test::ScopedFieldTrials field_trials(
3719 "WebRTC-Video-QualityScalerSettings/"
3720 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3721 // Reset encoder for field trials to take effect.
3722 ConfigureEncoder(video_encoder_config_.Copy());
3723 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3724 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3725 const int kWidth = 640;
3726 const int kHeight = 360;
3727
3728 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003729 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3730 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003731 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3732 // Frame should not be dropped.
3733 WaitForEncodedFrame(1);
3734
3735 video_stream_encoder_->OnBitrateUpdated(
3736 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003737 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003738 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3739 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3740 // Frame should not be dropped.
3741 WaitForEncodedFrame(2);
3742
3743 video_stream_encoder_->OnBitrateUpdated(
3744 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003745 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003746 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3747 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3748 // Expect to drop this frame, the wait should time out.
3749 ExpectDroppedFrame();
3750
3751 // Expect the sink_wants to specify a scaled frame.
3752 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3753 video_stream_encoder_->Stop();
3754}
3755
Åsa Perssone644a032019-11-08 15:56:00 +01003756TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3757 webrtc::test::ScopedFieldTrials field_trials(
3758 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3759
3760 // Reset encoder for field trials to take effect.
3761 VideoEncoderConfig config = video_encoder_config_.Copy();
3762 config.max_bitrate_bps = kTargetBitrateBps;
3763 ConfigureEncoder(std::move(config));
3764 fake_encoder_.SetQp(kQpLow);
3765
3766 // Enable MAINTAIN_FRAMERATE preference.
3767 AdaptingFrameForwarder source;
3768 source.set_adaptation_enabled(true);
3769 video_stream_encoder_->SetSource(&source,
3770 DegradationPreference::MAINTAIN_FRAMERATE);
3771
3772 // Start at low bitrate.
3773 const int kLowBitrateBps = 200000;
3774 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3775 DataRate::bps(kLowBitrateBps),
3776 DataRate::bps(kLowBitrateBps), 0, 0);
3777
3778 // Expect first frame to be dropped and resolution to be limited.
3779 const int kWidth = 1280;
3780 const int kHeight = 720;
3781 const int64_t kFrameIntervalMs = 100;
3782 int64_t timestamp_ms = kFrameIntervalMs;
3783 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3784 ExpectDroppedFrame();
3785 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3786
3787 // Increase bitrate to encoder max.
3788 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3789 DataRate::bps(config.max_bitrate_bps),
3790 DataRate::bps(config.max_bitrate_bps),
3791 0, 0);
3792
3793 // Insert frames and advance |min_duration_ms|.
3794 for (size_t i = 1; i <= 10; i++) {
3795 timestamp_ms += kFrameIntervalMs;
3796 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3797 WaitForEncodedFrame(timestamp_ms);
3798 }
3799 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3800 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3801
3802 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3803
3804 // Insert frame should trigger high BW and release quality limitation.
3805 timestamp_ms += kFrameIntervalMs;
3806 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3807 WaitForEncodedFrame(timestamp_ms);
3808 VerifyFpsMaxResolutionMax(source.sink_wants());
3809
3810 // Frame should not be adapted.
3811 timestamp_ms += kFrameIntervalMs;
3812 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3813 WaitForEncodedFrame(kWidth, kHeight);
3814 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3815
3816 video_stream_encoder_->Stop();
3817}
3818
mflodmancc3d4422017-08-03 08:27:51 -07003819TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003820 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3821 const int kTooSmallWidth = 10;
3822 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003823 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003824 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3825 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003826
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003827 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003828 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003829 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003830 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003831 VerifyNoLimitation(source.sink_wants());
3832 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3833
3834 // Trigger adapt down, too small frame, expect no change.
3835 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003836 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003837 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003838 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003839 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3840 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3841
mflodmancc3d4422017-08-03 08:27:51 -07003842 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003843}
3844
mflodmancc3d4422017-08-03 08:27:51 -07003845TEST_F(VideoStreamEncoderTest,
3846 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003847 const int kTooSmallWidth = 10;
3848 const int kTooSmallHeight = 10;
3849 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003850 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003851 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3852 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003853
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003854 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003855 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003856 video_stream_encoder_->SetSource(&source,
3857 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003858 VerifyNoLimitation(source.sink_wants());
3859 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3860 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3861
3862 // Trigger adapt down, expect limited framerate.
3863 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003864 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003865 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003866 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3867 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3868 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3869 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3870
3871 // Trigger adapt down, too small frame, expect no change.
3872 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003873 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003874 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003875 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3876 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3877 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3878 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3879
mflodmancc3d4422017-08-03 08:27:51 -07003880 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003881}
3882
mflodmancc3d4422017-08-03 08:27:51 -07003883TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003884 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003885 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003886 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3887 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003888 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003889 const int kFrameWidth = 1280;
3890 const int kFrameHeight = 720;
3891 video_source_.IncomingCapturedFrame(
3892 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003893 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003894 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003895}
3896
sprangb1ca0732017-02-01 08:38:12 -08003897// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003898TEST_F(VideoStreamEncoderTest,
3899 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003900 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003901 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3902 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003903
3904 const int kFrameWidth = 1280;
3905 const int kFrameHeight = 720;
3906 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003907 // requested by
3908 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003909 video_source_.set_adaptation_enabled(true);
3910
3911 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003912 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003913 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003914
3915 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003916 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003917 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003918 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003919 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003920
asaperssonfab67072017-04-04 05:51:49 -07003921 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003922 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003923 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003924 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003925 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003926
mflodmancc3d4422017-08-03 08:27:51 -07003927 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003928}
sprangfe627f32017-03-29 08:24:59 -07003929
mflodmancc3d4422017-08-03 08:27:51 -07003930TEST_F(VideoStreamEncoderTest,
3931 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003932 const int kFrameWidth = 1280;
3933 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003934
Erik Språng4c6ca302019-04-08 15:14:01 +02003935 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003936 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3937 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003938 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003939 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003940 video_source_.set_adaptation_enabled(true);
3941
sprang4847ae62017-06-27 07:06:52 -07003942 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003943
3944 video_source_.IncomingCapturedFrame(
3945 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003946 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003947
3948 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003949 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003950
3951 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003952 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003953 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003954 video_source_.IncomingCapturedFrame(
3955 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003956 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003957 }
3958
3959 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003960 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003961 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003962 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003963 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003964 video_source_.IncomingCapturedFrame(
3965 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003966 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003967 ++num_frames_dropped;
3968 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003969 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003970 }
3971 }
3972
sprang4847ae62017-06-27 07:06:52 -07003973 // Add some slack to account for frames dropped by the frame dropper.
3974 const int kErrorMargin = 1;
3975 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003976 kErrorMargin);
3977
3978 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003979 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003980 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003981 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003982 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003983 video_source_.IncomingCapturedFrame(
3984 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003985 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003986 ++num_frames_dropped;
3987 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003988 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003989 }
3990 }
sprang4847ae62017-06-27 07:06:52 -07003991 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003992 kErrorMargin);
3993
3994 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003995 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003996 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003997 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003998 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003999 video_source_.IncomingCapturedFrame(
4000 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004001 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004002 ++num_frames_dropped;
4003 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004004 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004005 }
4006 }
sprang4847ae62017-06-27 07:06:52 -07004007 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004008 kErrorMargin);
4009
4010 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004011 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004012 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004013 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004014 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004015 video_source_.IncomingCapturedFrame(
4016 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004017 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004018 ++num_frames_dropped;
4019 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004020 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004021 }
4022 }
4023 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4024
mflodmancc3d4422017-08-03 08:27:51 -07004025 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004026}
4027
mflodmancc3d4422017-08-03 08:27:51 -07004028TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004029 const int kFramerateFps = 5;
4030 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004031 const int kFrameWidth = 1280;
4032 const int kFrameHeight = 720;
4033
sprang4847ae62017-06-27 07:06:52 -07004034 // Reconfigure encoder with two temporal layers and screensharing, which will
4035 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004036 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004037
Erik Språng4c6ca302019-04-08 15:14:01 +02004038 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004039 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4040 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004041 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004042 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004043 video_source_.set_adaptation_enabled(true);
4044
sprang4847ae62017-06-27 07:06:52 -07004045 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004046
4047 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004048 rtc::VideoSinkWants last_wants;
4049 do {
4050 last_wants = video_source_.sink_wants();
4051
sprangc5d62e22017-04-02 23:53:04 -07004052 // Insert frames to get a new fps estimate...
4053 for (int j = 0; j < kFramerateFps; ++j) {
4054 video_source_.IncomingCapturedFrame(
4055 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004056 if (video_source_.last_sent_width()) {
4057 sink_.WaitForEncodedFrame(timestamp_ms);
4058 }
sprangc5d62e22017-04-02 23:53:04 -07004059 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004060 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004061 }
4062 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004063 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004064 } while (video_source_.sink_wants().max_framerate_fps <
4065 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004066
Jonathan Yubc771b72017-12-08 17:04:29 -08004067 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004068
mflodmancc3d4422017-08-03 08:27:51 -07004069 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004070}
asaperssonf7e294d2017-06-13 23:25:22 -07004071
mflodmancc3d4422017-08-03 08:27:51 -07004072TEST_F(VideoStreamEncoderTest,
4073 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004074 const int kWidth = 1280;
4075 const int kHeight = 720;
4076 const int64_t kFrameIntervalMs = 150;
4077 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004078 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004079 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4080 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004081
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004082 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004083 AdaptingFrameForwarder source;
4084 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004085 video_stream_encoder_->SetSource(&source,
4086 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004087 timestamp_ms += kFrameIntervalMs;
4088 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004089 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004090 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004091 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4092 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4093 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4094
4095 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004096 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004097 timestamp_ms += kFrameIntervalMs;
4098 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004099 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004100 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4101 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4102 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4103 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4104
4105 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004106 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004107 timestamp_ms += kFrameIntervalMs;
4108 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004109 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004110 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4111 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4112 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4113 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4114
4115 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004116 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004117 timestamp_ms += kFrameIntervalMs;
4118 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004119 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004120 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4121 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4122 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4123 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4124
4125 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004126 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004127 timestamp_ms += kFrameIntervalMs;
4128 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004129 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004130 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4131 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4132 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4133 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4134
4135 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004136 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004137 timestamp_ms += kFrameIntervalMs;
4138 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004139 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004140 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4141 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4142 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4143 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4144
4145 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004146 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004147 timestamp_ms += kFrameIntervalMs;
4148 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004149 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004150 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4151 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4152 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4153 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4154
4155 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004156 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004157 timestamp_ms += kFrameIntervalMs;
4158 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004159 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004160 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4161 rtc::VideoSinkWants last_wants = source.sink_wants();
4162 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4163 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4164 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4165
4166 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004167 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004168 timestamp_ms += kFrameIntervalMs;
4169 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004170 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004171 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4172 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4173 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4174 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4175
4176 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004177 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004178 timestamp_ms += kFrameIntervalMs;
4179 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004180 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004181 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4182 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4183 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4184 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4185
4186 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004187 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004188 timestamp_ms += kFrameIntervalMs;
4189 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004190 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004191 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4192 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4193 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4194 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4195
4196 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004197 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004198 timestamp_ms += kFrameIntervalMs;
4199 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004200 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004201 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4202 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4203 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4204 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4205
4206 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004207 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004208 timestamp_ms += kFrameIntervalMs;
4209 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004210 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004211 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4212 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4213 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4214 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4215
4216 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004217 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004218 timestamp_ms += kFrameIntervalMs;
4219 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004220 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004221 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4222 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4223 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4224 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4225
4226 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004227 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004228 timestamp_ms += kFrameIntervalMs;
4229 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004230 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004231 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4232 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4233 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4234 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4235
Åsa Persson30ab0152019-08-27 12:22:33 +02004236 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004237 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004238 timestamp_ms += kFrameIntervalMs;
4239 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004240 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004241 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004242 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004243 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4244 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4245 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4246
4247 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004248 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004249 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004250 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4251
mflodmancc3d4422017-08-03 08:27:51 -07004252 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004253}
4254
mflodmancc3d4422017-08-03 08:27:51 -07004255TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004256 const int kWidth = 1280;
4257 const int kHeight = 720;
4258 const int64_t kFrameIntervalMs = 150;
4259 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004260 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004261 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4262 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004263
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004264 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004265 AdaptingFrameForwarder source;
4266 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004267 video_stream_encoder_->SetSource(&source,
4268 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004269 timestamp_ms += kFrameIntervalMs;
4270 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004271 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004272 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004273 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4274 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4275 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4276 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4277 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4278 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4279
4280 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004281 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004282 timestamp_ms += kFrameIntervalMs;
4283 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004284 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004285 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4286 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4287 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4288 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4289 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4290 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4291 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4292
4293 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004294 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004295 timestamp_ms += kFrameIntervalMs;
4296 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004297 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004298 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4299 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4300 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4301 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4302 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4303 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4304 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4305
4306 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004307 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004308 timestamp_ms += kFrameIntervalMs;
4309 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004310 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004311 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4312 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4313 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4314 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4315 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4316 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4317 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4318
4319 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004320 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004321 timestamp_ms += kFrameIntervalMs;
4322 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004323 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004324 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4325 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4326 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4327 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4328 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4329 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4330 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4331
4332 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004333 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004334 timestamp_ms += kFrameIntervalMs;
4335 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004336 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004337 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4338 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4339 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4340 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4341 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4342 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4343 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4344
4345 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004346 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004347 timestamp_ms += kFrameIntervalMs;
4348 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004349 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004350 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004351 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004352 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4353 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4354 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4355 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4356 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4357 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4358
4359 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004360 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004361 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004362 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4363 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4364
mflodmancc3d4422017-08-03 08:27:51 -07004365 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004366}
4367
mflodmancc3d4422017-08-03 08:27:51 -07004368TEST_F(VideoStreamEncoderTest,
4369 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004370 const int kWidth = 640;
4371 const int kHeight = 360;
4372 const int kFpsLimit = 15;
4373 const int64_t kFrameIntervalMs = 150;
4374 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004375 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004376 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4377 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004378
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004379 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004380 AdaptingFrameForwarder source;
4381 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004382 video_stream_encoder_->SetSource(&source,
4383 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004384 timestamp_ms += kFrameIntervalMs;
4385 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004386 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004387 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004388 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4389 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4390 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4391 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4392 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4393 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4394
4395 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004396 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004397 timestamp_ms += kFrameIntervalMs;
4398 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004399 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004400 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4401 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4402 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4403 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4404 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4405 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4406 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4407
4408 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004409 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004410 timestamp_ms += kFrameIntervalMs;
4411 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004412 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004413 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4414 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4415 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4416 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4417 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4418 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4419 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4420
4421 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004422 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004423 timestamp_ms += kFrameIntervalMs;
4424 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004425 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004426 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4427 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4428 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4429 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4430 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4431 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4432 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4433
4434 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004435 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004436 timestamp_ms += kFrameIntervalMs;
4437 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004438 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004439 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004440 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4441 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4442 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4443 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4444 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4445 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4446
4447 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004448 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004449 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004450 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4451 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4452
mflodmancc3d4422017-08-03 08:27:51 -07004453 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004454}
4455
mflodmancc3d4422017-08-03 08:27:51 -07004456TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004457 const int kFrameWidth = 1920;
4458 const int kFrameHeight = 1080;
4459 // 3/4 of 1920.
4460 const int kAdaptedFrameWidth = 1440;
4461 // 3/4 of 1080 rounded down to multiple of 4.
4462 const int kAdaptedFrameHeight = 808;
4463 const int kFramerate = 24;
4464
Erik Språng4c6ca302019-04-08 15:14:01 +02004465 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004466 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4467 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004468 // Trigger reconfigure encoder (without resetting the entire instance).
4469 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004470 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004471 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4472 video_encoder_config.number_of_streams = 1;
4473 video_encoder_config.video_stream_factory =
4474 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004475 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004476 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004477 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004478
4479 video_source_.set_adaptation_enabled(true);
4480
4481 video_source_.IncomingCapturedFrame(
4482 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004483 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004484
4485 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004486 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004487 video_source_.IncomingCapturedFrame(
4488 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004489 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004490
mflodmancc3d4422017-08-03 08:27:51 -07004491 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004492}
4493
mflodmancc3d4422017-08-03 08:27:51 -07004494TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004495 const int kFrameWidth = 1280;
4496 const int kFrameHeight = 720;
4497 const int kLowFps = 2;
4498 const int kHighFps = 30;
4499
Erik Språng4c6ca302019-04-08 15:14:01 +02004500 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004501 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4502 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004503
4504 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4505 max_framerate_ = kLowFps;
4506
4507 // Insert 2 seconds of 2fps video.
4508 for (int i = 0; i < kLowFps * 2; ++i) {
4509 video_source_.IncomingCapturedFrame(
4510 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4511 WaitForEncodedFrame(timestamp_ms);
4512 timestamp_ms += 1000 / kLowFps;
4513 }
4514
4515 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004516 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004517 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4518 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004519 video_source_.IncomingCapturedFrame(
4520 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4521 WaitForEncodedFrame(timestamp_ms);
4522 timestamp_ms += 1000 / kLowFps;
4523
4524 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4525
4526 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004527 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004528 const int kFrameIntervalMs = 1000 / kHighFps;
4529 max_framerate_ = kHighFps;
4530 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4531 video_source_.IncomingCapturedFrame(
4532 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4533 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4534 // be dropped if the encoder hans't been updated with the new higher target
4535 // framerate yet, causing it to overshoot the target bitrate and then
4536 // suffering the wrath of the media optimizer.
4537 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4538 timestamp_ms += kFrameIntervalMs;
4539 }
4540
4541 // Don expect correct measurement just yet, but it should be higher than
4542 // before.
4543 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4544
mflodmancc3d4422017-08-03 08:27:51 -07004545 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004546}
4547
mflodmancc3d4422017-08-03 08:27:51 -07004548TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004549 const int kFrameWidth = 1280;
4550 const int kFrameHeight = 720;
4551 const int kTargetBitrateBps = 1000000;
4552
4553 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004554 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004555 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004556 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4557 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004558 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004559
4560 // Insert a first video frame, causes another bitrate update.
4561 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4562 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4563 video_source_.IncomingCapturedFrame(
4564 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4565 WaitForEncodedFrame(timestamp_ms);
4566
4567 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004568 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4569 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004570
4571 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004572 timestamp_ms += kProcessIntervalMs;
4573 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004574
4575 // Bitrate observer should not be called.
4576 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4577 video_source_.IncomingCapturedFrame(
4578 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4579 ExpectDroppedFrame();
4580
mflodmancc3d4422017-08-03 08:27:51 -07004581 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004582}
ilnik6b826ef2017-06-16 06:53:48 -07004583
Niels Möller4db138e2018-04-19 09:04:13 +02004584TEST_F(VideoStreamEncoderTest,
4585 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4586 const int kFrameWidth = 1280;
4587 const int kFrameHeight = 720;
4588 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004589 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004590 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4591 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004592 video_source_.IncomingCapturedFrame(
4593 CreateFrame(1, kFrameWidth, kFrameHeight));
4594 WaitForEncodedFrame(1);
4595 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4596 .low_encode_usage_threshold_percent,
4597 default_options.low_encode_usage_threshold_percent);
4598 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4599 .high_encode_usage_threshold_percent,
4600 default_options.high_encode_usage_threshold_percent);
4601 video_stream_encoder_->Stop();
4602}
4603
4604TEST_F(VideoStreamEncoderTest,
4605 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4606 const int kFrameWidth = 1280;
4607 const int kFrameHeight = 720;
4608 CpuOveruseOptions hardware_options;
4609 hardware_options.low_encode_usage_threshold_percent = 150;
4610 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004611 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004612
Erik Språng4c6ca302019-04-08 15:14:01 +02004613 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004614 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4615 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004616 video_source_.IncomingCapturedFrame(
4617 CreateFrame(1, kFrameWidth, kFrameHeight));
4618 WaitForEncodedFrame(1);
4619 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4620 .low_encode_usage_threshold_percent,
4621 hardware_options.low_encode_usage_threshold_percent);
4622 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4623 .high_encode_usage_threshold_percent,
4624 hardware_options.high_encode_usage_threshold_percent);
4625 video_stream_encoder_->Stop();
4626}
4627
Niels Möller6bb5ab92019-01-11 11:11:10 +01004628TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4629 const int kFrameWidth = 320;
4630 const int kFrameHeight = 240;
4631 const int kFps = 30;
4632 const int kTargetBitrateBps = 120000;
4633 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4634
Erik Språng4c6ca302019-04-08 15:14:01 +02004635 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004636 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4637 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004638
4639 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4640 max_framerate_ = kFps;
4641
4642 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4643 fake_encoder_.SimulateOvershoot(1.0);
4644 int num_dropped = 0;
4645 for (int i = 0; i < kNumFramesInRun; ++i) {
4646 video_source_.IncomingCapturedFrame(
4647 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4648 // Wait up to two frame durations for a frame to arrive.
4649 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4650 ++num_dropped;
4651 }
4652 timestamp_ms += 1000 / kFps;
4653 }
4654
Erik Språnga8d48ab2019-02-08 14:17:40 +01004655 // Framerate should be measured to be near the expected target rate.
4656 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4657
4658 // Frame drops should be within 5% of expected 0%.
4659 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004660
4661 // Make encoder produce frames at double the expected bitrate during 3 seconds
4662 // of video, verify number of drops. Rate needs to be slightly changed in
4663 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004664 double overshoot_factor = 2.0;
4665 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4666 // With bitrate adjuster, when need to overshoot even more to trigger
4667 // frame dropping.
4668 overshoot_factor *= 2;
4669 }
4670 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004671 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004672 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004673 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004674 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004675 num_dropped = 0;
4676 for (int i = 0; i < kNumFramesInRun; ++i) {
4677 video_source_.IncomingCapturedFrame(
4678 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4679 // Wait up to two frame durations for a frame to arrive.
4680 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4681 ++num_dropped;
4682 }
4683 timestamp_ms += 1000 / kFps;
4684 }
4685
Erik Språng4c6ca302019-04-08 15:14:01 +02004686 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004687 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4688 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004689
4690 // Target framerate should be still be near the expected target, despite
4691 // the frame drops.
4692 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4693
4694 // Frame drops should be within 5% of expected 50%.
4695 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004696
4697 video_stream_encoder_->Stop();
4698}
4699
4700TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4701 const int kFrameWidth = 320;
4702 const int kFrameHeight = 240;
4703 const int kActualInputFps = 24;
4704 const int kTargetBitrateBps = 120000;
4705
4706 ASSERT_GT(max_framerate_, kActualInputFps);
4707
4708 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4709 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004710 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004711 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4712 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004713
4714 // Insert 3 seconds of video, with an input fps lower than configured max.
4715 for (int i = 0; i < kActualInputFps * 3; ++i) {
4716 video_source_.IncomingCapturedFrame(
4717 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4718 // Wait up to two frame durations for a frame to arrive.
4719 WaitForEncodedFrame(timestamp_ms);
4720 timestamp_ms += 1000 / kActualInputFps;
4721 }
4722
4723 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4724
4725 video_stream_encoder_->Stop();
4726}
4727
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004728TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4729 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004730 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004731 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4732 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004733
4734 fake_encoder_.BlockNextEncode();
4735 video_source_.IncomingCapturedFrame(
4736 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4737 WaitForEncodedFrame(1);
4738 // On the very first frame full update should be forced.
4739 rect = fake_encoder_.GetLastUpdateRect();
4740 EXPECT_EQ(rect.offset_x, 0);
4741 EXPECT_EQ(rect.offset_y, 0);
4742 EXPECT_EQ(rect.height, codec_height_);
4743 EXPECT_EQ(rect.width, codec_width_);
4744 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4745 // call to ContinueEncode.
4746 video_source_.IncomingCapturedFrame(
4747 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4748 ExpectDroppedFrame();
4749 video_source_.IncomingCapturedFrame(
4750 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4751 ExpectDroppedFrame();
4752 fake_encoder_.ContinueEncode();
4753 WaitForEncodedFrame(3);
4754 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4755 rect = fake_encoder_.GetLastUpdateRect();
4756 EXPECT_EQ(rect.offset_x, 1);
4757 EXPECT_EQ(rect.offset_y, 0);
4758 EXPECT_EQ(rect.width, 10);
4759 EXPECT_EQ(rect.height, 1);
4760
4761 video_source_.IncomingCapturedFrame(
4762 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4763 WaitForEncodedFrame(4);
4764 // Previous frame was encoded, so no accumulation should happen.
4765 rect = fake_encoder_.GetLastUpdateRect();
4766 EXPECT_EQ(rect.offset_x, 0);
4767 EXPECT_EQ(rect.offset_y, 0);
4768 EXPECT_EQ(rect.width, 1);
4769 EXPECT_EQ(rect.height, 1);
4770
4771 video_stream_encoder_->Stop();
4772}
4773
Erik Språngd7329ca2019-02-21 21:19:53 +01004774TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004775 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004776 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4777 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004778
4779 // First frame is always keyframe.
4780 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4781 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004782 EXPECT_THAT(
4783 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004784 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004785
4786 // Insert delta frame.
4787 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4788 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004789 EXPECT_THAT(
4790 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004791 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004792
4793 // Request next frame be a key-frame.
4794 video_stream_encoder_->SendKeyFrame();
4795 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4796 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004797 EXPECT_THAT(
4798 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004799 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004800
4801 video_stream_encoder_->Stop();
4802}
4803
4804TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4805 // Setup simulcast with three streams.
4806 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004807 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004808 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004809 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004810 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004811 // Wait for all three layers before triggering event.
4812 sink_.SetNumExpectedLayers(3);
4813
4814 // First frame is always keyframe.
4815 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4816 WaitForEncodedFrame(1);
4817 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004818 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4819 VideoFrameType::kVideoFrameKey,
4820 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004821
4822 // Insert delta frame.
4823 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4824 WaitForEncodedFrame(2);
4825 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004826 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4827 VideoFrameType::kVideoFrameDelta,
4828 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004829
4830 // Request next frame be a key-frame.
4831 // Only first stream is configured to produce key-frame.
4832 video_stream_encoder_->SendKeyFrame();
4833 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4834 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004835
4836 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4837 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004838 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004839 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004840 VideoFrameType::kVideoFrameKey,
4841 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004842
4843 video_stream_encoder_->Stop();
4844}
4845
4846TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4847 // Configure internal source factory and setup test again.
4848 encoder_factory_.SetHasInternalSource(true);
4849 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004850 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004851 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4852 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004853
4854 // Call encoder directly, simulating internal source where encoded frame
4855 // callback in VideoStreamEncoder is called despite no OnFrame().
4856 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4857 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004858 EXPECT_THAT(
4859 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004860 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004861
Niels Möller8f7ce222019-03-21 15:43:58 +01004862 const std::vector<VideoFrameType> kDeltaFrame = {
4863 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004864 // Need to set timestamp manually since manually for injected frame.
4865 VideoFrame frame = CreateFrame(101, nullptr);
4866 frame.set_timestamp(101);
4867 fake_encoder_.InjectFrame(frame, false);
4868 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004869 EXPECT_THAT(
4870 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004871 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004872
4873 // Request key-frame. The forces a dummy frame down into the encoder.
4874 fake_encoder_.ExpectNullFrame();
4875 video_stream_encoder_->SendKeyFrame();
4876 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004877 EXPECT_THAT(
4878 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004879 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004880
4881 video_stream_encoder_->Stop();
4882}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004883
4884TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4885 // Configure internal source factory and setup test again.
4886 encoder_factory_.SetHasInternalSource(true);
4887 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004888 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004889 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4890 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004891
4892 int64_t timestamp = 1;
4893 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004894 image.SetEncodedData(
4895 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004896 image.capture_time_ms_ = ++timestamp;
4897 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4898 const int64_t kEncodeFinishDelayMs = 10;
4899 image.timing_.encode_start_ms = timestamp;
4900 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4901 fake_encoder_.InjectEncodedImage(image);
4902 // Wait for frame without incrementing clock.
4903 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4904 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4905 // capture timestamp should be kEncodeFinishDelayMs in the past.
4906 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4907 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4908 kEncodeFinishDelayMs);
4909
4910 video_stream_encoder_->Stop();
4911}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004912
4913TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4914 // Configure internal source factory and setup test again.
4915 encoder_factory_.SetHasInternalSource(true);
4916 ResetEncoder("H264", 1, 1, 1, false);
4917
4918 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4919 image._frameType = VideoFrameType::kVideoFrameKey;
4920
4921 CodecSpecificInfo codec_specific_info;
4922 codec_specific_info.codecType = kVideoCodecH264;
4923
4924 RTPFragmentationHeader fragmentation;
4925 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4926 fragmentation.fragmentationOffset[0] = 4;
4927 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4928
4929 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4930 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4931
4932 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4933 testing::ElementsAreArray(optimal_sps));
4934 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4935 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4936 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4937 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4938
4939 video_stream_encoder_->Stop();
4940}
4941
4942TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4943 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4944 0x00, 0x00, 0x03, 0x03, 0xF4,
4945 0x05, 0x03, 0xC7, 0xC0};
4946
4947 // Configure internal source factory and setup test again.
4948 encoder_factory_.SetHasInternalSource(true);
4949 ResetEncoder("H264", 1, 1, 1, false);
4950
4951 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4952 image._frameType = VideoFrameType::kVideoFrameKey;
4953
4954 CodecSpecificInfo codec_specific_info;
4955 codec_specific_info.codecType = kVideoCodecH264;
4956
4957 RTPFragmentationHeader fragmentation;
4958 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4959 fragmentation.fragmentationOffset[0] = 4;
4960 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4961
4962 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4963 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4964
4965 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4966 testing::ElementsAreArray(optimal_sps));
4967 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4968 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4969 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4970 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4971
4972 video_stream_encoder_->Stop();
4973}
4974
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004975TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4976 const int kFrameWidth = 1280;
4977 const int kFrameHeight = 720;
4978 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4979
4980 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004981 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4982 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004983 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4984
4985 // Insert a first video frame. It should be dropped because of downscale in
4986 // resolution.
4987 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4988 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4989 frame.set_rotation(kVideoRotation_270);
4990 video_source_.IncomingCapturedFrame(frame);
4991
4992 ExpectDroppedFrame();
4993
4994 // Second frame is downscaled.
4995 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4996 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4997 frame.set_rotation(kVideoRotation_90);
4998 video_source_.IncomingCapturedFrame(frame);
4999
5000 WaitForEncodedFrame(timestamp_ms);
5001 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5002
5003 // Insert another frame, also downscaled.
5004 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5005 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5006 frame.set_rotation(kVideoRotation_180);
5007 video_source_.IncomingCapturedFrame(frame);
5008
5009 WaitForEncodedFrame(timestamp_ms);
5010 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5011
5012 video_stream_encoder_->Stop();
5013}
5014
Erik Språng5056af02019-09-02 15:53:11 +02005015TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5016 const int kFrameWidth = 320;
5017 const int kFrameHeight = 180;
5018
5019 // Initial rate.
5020 video_stream_encoder_->OnBitrateUpdated(
5021 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005022 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005023 /*link_allocation=*/DataRate::kbps(300),
5024 /*fraction_lost=*/0,
5025 /*rtt_ms=*/0);
5026
5027 // Insert a first video frame so that encoder gets configured.
5028 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5029 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5030 frame.set_rotation(kVideoRotation_270);
5031 video_source_.IncomingCapturedFrame(frame);
5032 WaitForEncodedFrame(timestamp_ms);
5033
5034 // Set a target rate below the minimum allowed by the codec settings.
5035 VideoCodec codec_config = fake_encoder_.codec_config();
5036 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5037 DataRate target_rate = min_rate - DataRate::kbps(1);
5038 video_stream_encoder_->OnBitrateUpdated(
5039 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005040 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005041 /*link_allocation=*/target_rate,
5042 /*fraction_lost=*/0,
5043 /*rtt_ms=*/0);
5044 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5045
5046 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5047 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5048 ASSERT_TRUE(rate_settings.has_value());
5049 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5050 EXPECT_EQ(min_rate, allocation_sum);
5051 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5052
5053 video_stream_encoder_->Stop();
5054}
5055
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005056TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005057 video_stream_encoder_->OnBitrateUpdated(
5058 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5059 DataRate::bps(kTargetBitrateBps), 0, 0);
5060 // Capture a frame and wait for it to synchronize with the encoder thread.
5061 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5062 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5063 WaitForEncodedFrame(1);
5064
5065 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5066 ASSERT_TRUE(prev_rate_settings.has_value());
5067 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5068 kDefaultFramerate);
5069
5070 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5071 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5072 timestamp_ms += 1000 / kDefaultFramerate;
5073 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5074 WaitForEncodedFrame(timestamp_ms);
5075 }
5076 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5077 kDefaultFramerate);
5078 // Capture larger frame to trigger a reconfigure.
5079 codec_height_ *= 2;
5080 codec_width_ *= 2;
5081 timestamp_ms += 1000 / kDefaultFramerate;
5082 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5083 WaitForEncodedFrame(timestamp_ms);
5084
5085 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5086 auto current_rate_settings =
5087 fake_encoder_.GetAndResetLastRateControlSettings();
5088 // Ensure we have actually reconfigured twice
5089 // The rate settings should have been set again even though
5090 // they haven't changed.
5091 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005092 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005093
5094 video_stream_encoder_->Stop();
5095}
5096
philipeld9cc8c02019-09-16 14:53:40 +02005097struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5098 MOCK_METHOD0(RequestEncoderFallback, void());
5099 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5100};
5101
5102TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5103 constexpr int kDontCare = 100;
5104
5105 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5106 video_send_config_.encoder_settings.encoder_switch_request_callback =
5107 &switch_callback;
5108 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5109 encoder_config.codec_type = kVideoCodecVP8;
5110 webrtc::test::ScopedFieldTrials field_trial(
5111 "WebRTC-NetworkCondition-EncoderSwitch/"
5112 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5113 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5114
5115 // Reset encoder for new configuration to take effect.
5116 ConfigureEncoder(std::move(encoder_config));
5117
5118 // Send one frame to trigger ReconfigureEncoder.
5119 video_source_.IncomingCapturedFrame(
5120 CreateFrame(kDontCare, kDontCare, kDontCare));
5121
5122 using Config = EncoderSwitchRequestCallback::Config;
5123 EXPECT_CALL(switch_callback,
5124 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5125 Field(&Config::param, "ping"),
5126 Field(&Config::value, "pong"))));
5127
5128 video_stream_encoder_->OnBitrateUpdated(
5129 /*target_bitrate=*/DataRate::kbps(50),
5130 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5131 /*link_allocation=*/DataRate::kbps(kDontCare),
5132 /*fraction_lost=*/0,
5133 /*rtt_ms=*/0);
5134
5135 video_stream_encoder_->Stop();
5136}
5137
5138TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5139 constexpr int kSufficientBitrateToNotDrop = 1000;
5140 constexpr int kHighRes = 500;
5141 constexpr int kLowRes = 100;
5142
5143 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5144 video_send_config_.encoder_settings.encoder_switch_request_callback =
5145 &switch_callback;
5146 webrtc::test::ScopedFieldTrials field_trial(
5147 "WebRTC-NetworkCondition-EncoderSwitch/"
5148 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5149 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5150 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5151 encoder_config.codec_type = kVideoCodecH264;
5152
5153 // Reset encoder for new configuration to take effect.
5154 ConfigureEncoder(std::move(encoder_config));
5155
5156 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5157 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5158 // not fail.
5159 video_stream_encoder_->OnBitrateUpdated(
5160 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5161 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5162 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5163 /*fraction_lost=*/0,
5164 /*rtt_ms=*/0);
5165
5166 // Send one frame to trigger ReconfigureEncoder.
5167 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5168 WaitForEncodedFrame(1);
5169
5170 using Config = EncoderSwitchRequestCallback::Config;
5171 EXPECT_CALL(switch_callback,
5172 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5173 Field(&Config::param, "ping"),
5174 Field(&Config::value, "pong"))));
5175
5176 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5177 WaitForEncodedFrame(2);
5178
5179 video_stream_encoder_->Stop();
5180}
5181
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005182TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005183 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005184 const int kFrameWidth = 320;
5185 const int kFrameHeight = 180;
5186
5187 // Set initial rate.
5188 auto rate = DataRate::kbps(100);
5189 video_stream_encoder_->OnBitrateUpdated(
5190 /*target_bitrate=*/rate,
5191 /*stable_target_bitrate=*/rate,
5192 /*link_allocation=*/rate,
5193 /*fraction_lost=*/0,
5194 /*rtt_ms=*/0);
5195
5196 // Insert a first video frame so that encoder gets configured.
5197 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5198 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5199 frame.set_rotation(kVideoRotation_270);
5200 video_source_.IncomingCapturedFrame(frame);
5201 WaitForEncodedFrame(timestamp_ms);
5202 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5203
5204 // Change of target bitrate propagates to the encoder.
5205 auto new_stable_rate = rate - DataRate::kbps(5);
5206 video_stream_encoder_->OnBitrateUpdated(
5207 /*target_bitrate=*/new_stable_rate,
5208 /*stable_target_bitrate=*/new_stable_rate,
5209 /*link_allocation=*/rate,
5210 /*fraction_lost=*/0,
5211 /*rtt_ms=*/0);
5212 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5213 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5214 video_stream_encoder_->Stop();
5215}
5216
5217TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005218 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005219 const int kFrameWidth = 320;
5220 const int kFrameHeight = 180;
5221
5222 // Set initial rate.
5223 auto rate = DataRate::kbps(100);
5224 video_stream_encoder_->OnBitrateUpdated(
5225 /*target_bitrate=*/rate,
5226 /*stable_target_bitrate=*/rate,
5227 /*link_allocation=*/rate,
5228 /*fraction_lost=*/0,
5229 /*rtt_ms=*/0);
5230
5231 // Insert a first video frame so that encoder gets configured.
5232 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5233 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5234 frame.set_rotation(kVideoRotation_270);
5235 video_source_.IncomingCapturedFrame(frame);
5236 WaitForEncodedFrame(timestamp_ms);
5237 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5238
5239 // Set a higher target rate without changing the link_allocation. Should not
5240 // reset encoder's rate.
5241 auto new_stable_rate = rate - DataRate::kbps(5);
5242 video_stream_encoder_->OnBitrateUpdated(
5243 /*target_bitrate=*/rate,
5244 /*stable_target_bitrate=*/new_stable_rate,
5245 /*link_allocation=*/rate,
5246 /*fraction_lost=*/0,
5247 /*rtt_ms=*/0);
5248 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5249 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5250 video_stream_encoder_->Stop();
5251}
5252
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005253TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5254 test::ScopedFieldTrials field_trials(
5255 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5256 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5257 const int kFramerateFps = 30;
5258 const int kWidth = 1920;
5259 const int kHeight = 1080;
5260 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5261 // Works on screenshare mode.
5262 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5263 // We rely on the automatic resolution adaptation, but we handle framerate
5264 // adaptation manually by mocking the stats proxy.
5265 video_source_.set_adaptation_enabled(true);
5266
5267 // BALANCED degradation preference is required for this feature.
5268 video_stream_encoder_->OnBitrateUpdated(
5269 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5270 DataRate::bps(kTargetBitrateBps), 0, 0);
5271 video_stream_encoder_->SetSource(&video_source_,
5272 webrtc::DegradationPreference::BALANCED);
5273 VerifyNoLimitation(video_source_.sink_wants());
5274
5275 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5276 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5277
5278 // Pass enough frames with the full update to trigger animation detection.
5279 for (int i = 0; i < kNumFrames; ++i) {
5280 int64_t timestamp_ms =
5281 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5282 frame.set_ntp_time_ms(timestamp_ms);
5283 frame.set_timestamp_us(timestamp_ms * 1000);
5284 video_source_.IncomingCapturedFrame(frame);
5285 WaitForEncodedFrame(timestamp_ms);
5286 }
5287
5288 // Resolution should be limited.
5289 rtc::VideoSinkWants expected;
5290 expected.max_framerate_fps = kFramerateFps;
5291 expected.max_pixel_count = 1280 * 720 + 1;
5292 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5293
5294 // Pass one frame with no known update.
5295 // Resolution cap should be removed immediately.
5296 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5297 frame.set_ntp_time_ms(timestamp_ms);
5298 frame.set_timestamp_us(timestamp_ms * 1000);
5299 frame.clear_update_rect();
5300
5301 video_source_.IncomingCapturedFrame(frame);
5302 WaitForEncodedFrame(timestamp_ms);
5303
5304 // Resolution should be unlimited now.
5305 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5306
5307 video_stream_encoder_->Stop();
5308}
5309
perkj26091b12016-09-01 01:17:40 -07005310} // namespace webrtc