blob: e5439f33d35cf1cd33524c2edca04ce85ec3b5dc [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ömb08882b2020-01-07 10:11:17 +0100169 EXPECT_EQ(expected_results, TriggerAdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200170 else
Henrik Boströmb08882b2020-01-07 10:11:17 +0100171 TriggerAdaptUp(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 Silkin6b2cec12019-08-09 16:04:05 +02001429 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
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 Silkin6456e352019-07-08 17:56:40 +02001434 VideoEncoderConfig video_encoder_config;
1435 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1436 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001437 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001438 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1439 kMaxPayloadLength);
1440
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001441 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001442 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001443
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001444 // Get the default bitrate limits and use them as baseline for custom
1445 // application and encoder recommended limits.
1446 const uint32_t kDefaultMinBitrateKbps =
1447 bitrate_allocator_factory_.codec_config().minBitrate;
1448 const uint32_t kDefaultMaxBitrateKbps =
1449 bitrate_allocator_factory_.codec_config().maxBitrate;
1450 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1451 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1452 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1453 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1454
1455 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1456 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1457 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1458 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1459
1460 // Change resolution. This will trigger encoder re-configuration and video
1461 // stream encoder will pick up the bitrate limits recommended by encoder.
1462 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1463 WaitForEncodedFrame(2);
1464 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1465 WaitForEncodedFrame(3);
1466
1467 // App bitrate limits are not set - bitrate limits recommended by encoder
1468 // should be used.
1469 EXPECT_EQ(kEncMaxBitrateKbps,
1470 bitrate_allocator_factory_.codec_config().maxBitrate);
1471 EXPECT_EQ(kEncMinBitrateKbps,
1472 bitrate_allocator_factory_.codec_config().minBitrate);
1473
1474 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1475 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1476 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1477 kMaxPayloadLength);
1478 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1479 WaitForEncodedFrame(4);
1480
1481 // App limited the max bitrate - bitrate limits recommended by encoder should
1482 // not be applied.
1483 EXPECT_EQ(kAppMaxBitrateKbps,
1484 bitrate_allocator_factory_.codec_config().maxBitrate);
1485 EXPECT_EQ(kDefaultMinBitrateKbps,
1486 bitrate_allocator_factory_.codec_config().minBitrate);
1487
1488 video_encoder_config.max_bitrate_bps = 0;
1489 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1490 kAppMinBitrateKbps * 1000;
1491 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1492 kMaxPayloadLength);
1493 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1494 WaitForEncodedFrame(5);
1495
1496 // App limited the min bitrate - bitrate limits recommended by encoder should
1497 // not be applied.
1498 EXPECT_EQ(kDefaultMaxBitrateKbps,
1499 bitrate_allocator_factory_.codec_config().maxBitrate);
1500 EXPECT_EQ(kAppMinBitrateKbps,
1501 bitrate_allocator_factory_.codec_config().minBitrate);
1502
1503 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1504 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1505 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001506 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1507 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001508 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1509 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001510
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001511 // App limited both min and max bitrates - bitrate limits recommended by
1512 // encoder should not be applied.
1513 EXPECT_EQ(kAppMaxBitrateKbps,
1514 bitrate_allocator_factory_.codec_config().maxBitrate);
1515 EXPECT_EQ(kAppMinBitrateKbps,
1516 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001517
1518 video_stream_encoder_->Stop();
1519}
1520
1521TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001522 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001523 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001524 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1525 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001526
1527 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001528 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001529 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001530 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001531 fake_encoder_.SetResolutionBitrateLimits(
1532 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1533
1534 VideoEncoderConfig video_encoder_config;
1535 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1536 video_encoder_config.max_bitrate_bps = 0;
1537 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1538 kMaxPayloadLength);
1539
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001540 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001541 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1542 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001543 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1544 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001545 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1546 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1547
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001548 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001549 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1550 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001551 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1552 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001553 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1554 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1555
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001556 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001557 // encoder for 360p should be used.
1558 video_source_.IncomingCapturedFrame(
1559 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1560 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001561 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1562 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001563 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1564 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1565
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001566 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001567 // ignored.
1568 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1569 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001570 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1571 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001572 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1573 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001574 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1575 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001576 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1577 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1578
1579 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1580 // for 270p should be used.
1581 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1582 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001583 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1584 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001585 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1586 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1587
1588 video_stream_encoder_->Stop();
1589}
1590
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001591TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1592 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001593 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1594 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001595
1596 VideoEncoderConfig video_encoder_config;
1597 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1598 video_encoder_config.max_bitrate_bps = 0;
1599 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1600 kMaxPayloadLength);
1601
1602 // Encode 720p frame to get the default encoder target bitrate.
1603 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1604 WaitForEncodedFrame(1);
1605 const uint32_t kDefaultTargetBitrateFor720pKbps =
1606 bitrate_allocator_factory_.codec_config()
1607 .simulcastStream[0]
1608 .targetBitrate;
1609
1610 // Set the max recommended encoder bitrate to something lower than the default
1611 // target bitrate.
1612 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1613 1280 * 720, 10 * 1000, 10 * 1000,
1614 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1615 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1616
1617 // Change resolution to trigger encoder reinitialization.
1618 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1619 WaitForEncodedFrame(2);
1620 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1621 WaitForEncodedFrame(3);
1622
1623 // Ensure the target bitrate is capped by the max bitrate.
1624 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1625 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1626 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1627 .simulcastStream[0]
1628 .targetBitrate *
1629 1000,
1630 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1631
1632 video_stream_encoder_->Stop();
1633}
1634
mflodmancc3d4422017-08-03 08:27:51 -07001635TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001636 EXPECT_TRUE(video_source_.has_sinks());
1637 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001638 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001639 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001640 EXPECT_FALSE(video_source_.has_sinks());
1641 EXPECT_TRUE(new_video_source.has_sinks());
1642
mflodmancc3d4422017-08-03 08:27:51 -07001643 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001644}
1645
mflodmancc3d4422017-08-03 08:27:51 -07001646TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001647 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001648 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001649 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001650 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001651}
1652
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001653TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1654 constexpr int kRequestedResolutionAlignment = 7;
1655 video_source_.set_adaptation_enabled(true);
1656 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1657 video_stream_encoder_->OnBitrateUpdated(
1658 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1659 DataRate::bps(kTargetBitrateBps), 0, 0);
1660
1661 // On the 1st frame, we should have initialized the encoder and
1662 // asked for its resolution requirements.
1663 video_source_.IncomingCapturedFrame(
1664 CreateFrame(1, codec_width_, codec_height_));
1665 WaitForEncodedFrame(1);
1666 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1667 kRequestedResolutionAlignment);
1668
1669 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1670 // (It's up the to the encoder to potentially drop the previous frame,
1671 // to avoid coding back-to-back keyframes.)
1672 video_source_.IncomingCapturedFrame(
1673 CreateFrame(2, codec_width_, codec_height_));
1674 WaitForEncodedFrame(2);
1675 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1676
1677 video_stream_encoder_->Stop();
1678}
1679
Jonathan Yubc771b72017-12-08 17:04:29 -08001680TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1681 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001682 const int kWidth = 1280;
1683 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001684
1685 // We rely on the automatic resolution adaptation, but we handle framerate
1686 // adaptation manually by mocking the stats proxy.
1687 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001688
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001689 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001690 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001691 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1692 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001693 video_stream_encoder_->SetSource(&video_source_,
1694 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001695 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001696 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001697 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001698 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1699
Jonathan Yubc771b72017-12-08 17:04:29 -08001700 // Adapt down as far as possible.
1701 rtc::VideoSinkWants last_wants;
1702 int64_t t = 1;
1703 int loop_count = 0;
1704 do {
1705 ++loop_count;
1706 last_wants = video_source_.sink_wants();
1707
1708 // Simulate the framerate we've been asked to adapt to.
1709 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1710 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1711 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1712 mock_stats.input_frame_rate = fps;
1713 stats_proxy_->SetMockStats(mock_stats);
1714
1715 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1716 sink_.WaitForEncodedFrame(t);
1717 t += frame_interval_ms;
1718
mflodmancc3d4422017-08-03 08:27:51 -07001719 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001720 VerifyBalancedModeFpsRange(
1721 video_source_.sink_wants(),
1722 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1723 } while (video_source_.sink_wants().max_pixel_count <
1724 last_wants.max_pixel_count ||
1725 video_source_.sink_wants().max_framerate_fps <
1726 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001727
Jonathan Yubc771b72017-12-08 17:04:29 -08001728 // Verify that we've adapted all the way down.
1729 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001730 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001731 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1732 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001733 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001734 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1735 *video_source_.last_sent_height());
1736 EXPECT_EQ(kMinBalancedFramerateFps,
1737 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001738
Jonathan Yubc771b72017-12-08 17:04:29 -08001739 // Adapt back up the same number of times we adapted down.
1740 for (int i = 0; i < loop_count - 1; ++i) {
1741 last_wants = video_source_.sink_wants();
1742
1743 // Simulate the framerate we've been asked to adapt to.
1744 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1745 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1746 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1747 mock_stats.input_frame_rate = fps;
1748 stats_proxy_->SetMockStats(mock_stats);
1749
1750 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1751 sink_.WaitForEncodedFrame(t);
1752 t += frame_interval_ms;
1753
mflodmancc3d4422017-08-03 08:27:51 -07001754 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001755 VerifyBalancedModeFpsRange(
1756 video_source_.sink_wants(),
1757 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1758 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1759 last_wants.max_pixel_count ||
1760 video_source_.sink_wants().max_framerate_fps >
1761 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001762 }
1763
Åsa Persson8c1bf952018-09-13 10:42:19 +02001764 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001765 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001766 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001767 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1768 EXPECT_EQ((loop_count - 1) * 2,
1769 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001770
mflodmancc3d4422017-08-03 08:27:51 -07001771 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001772}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001773
mflodmancc3d4422017-08-03 08:27:51 -07001774TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001775 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001776 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1777 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001778 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001779
sprangc5d62e22017-04-02 23:53:04 -07001780 const int kFrameWidth = 1280;
1781 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001782
Åsa Persson8c1bf952018-09-13 10:42:19 +02001783 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001784
kthelgason5e13d412016-12-01 03:59:51 -08001785 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001786 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001787 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001788 frame_timestamp += kFrameIntervalMs;
1789
perkj803d97f2016-11-01 11:45:46 -07001790 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001791 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001792 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001793 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001794 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001795 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001796
asapersson0944a802017-04-07 00:57:58 -07001797 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001798 // wanted resolution.
1799 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1800 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1801 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001802 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001803
1804 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001805 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001806 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001807 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001808 // Give the encoder queue time to process the change in degradation preference
1809 // by waiting for an encoded frame.
1810 new_video_source.IncomingCapturedFrame(
1811 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1812 sink_.WaitForEncodedFrame(frame_timestamp);
1813 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001814 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001815 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001816
sprangc5d62e22017-04-02 23:53:04 -07001817 // Force an input frame rate to be available, or the adaptation call won't
1818 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001819 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001820 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001821 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001822 stats_proxy_->SetMockStats(stats);
1823
mflodmancc3d4422017-08-03 08:27:51 -07001824 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001825 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001826 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001827 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001828 frame_timestamp += kFrameIntervalMs;
1829
1830 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001831 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001832 EXPECT_EQ(std::numeric_limits<int>::max(),
1833 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001834 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001835
asapersson02465b82017-04-10 01:12:52 -07001836 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001837 video_stream_encoder_->SetSource(&new_video_source,
1838 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001839 // Give the encoder queue time to process the change in degradation preference
1840 // by waiting for an encoded frame.
1841 new_video_source.IncomingCapturedFrame(
1842 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1843 sink_.WaitForEncodedFrame(frame_timestamp);
1844 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001845 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001846
mflodmancc3d4422017-08-03 08:27:51 -07001847 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001848 new_video_source.IncomingCapturedFrame(
1849 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001850 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001851 frame_timestamp += kFrameIntervalMs;
1852
1853 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001854 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001855
1856 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001857 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001858 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001859 // Give the encoder queue time to process the change in degradation preference
1860 // by waiting for an encoded frame.
1861 new_video_source.IncomingCapturedFrame(
1862 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1863 sink_.WaitForEncodedFrame(frame_timestamp);
1864 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001865 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1866 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001867 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001868 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001869
1870 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001871 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001872 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001873 // Give the encoder queue time to process the change in degradation preference
1874 // by waiting for an encoded frame.
1875 new_video_source.IncomingCapturedFrame(
1876 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1877 sink_.WaitForEncodedFrame(frame_timestamp);
1878 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001879 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1880 EXPECT_EQ(std::numeric_limits<int>::max(),
1881 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001882 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001883
mflodmancc3d4422017-08-03 08:27:51 -07001884 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001885}
1886
mflodmancc3d4422017-08-03 08:27:51 -07001887TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001888 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001889 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1890 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001891
asaperssonfab67072017-04-04 05:51:49 -07001892 const int kWidth = 1280;
1893 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001894 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001895 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001896 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1897 EXPECT_FALSE(stats.bw_limited_resolution);
1898 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1899
1900 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001901 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001902 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001903 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001904
1905 stats = stats_proxy_->GetStats();
1906 EXPECT_TRUE(stats.bw_limited_resolution);
1907 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1908
1909 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001910 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001911 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001912 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001913
1914 stats = stats_proxy_->GetStats();
1915 EXPECT_FALSE(stats.bw_limited_resolution);
1916 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1917 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1918
mflodmancc3d4422017-08-03 08:27:51 -07001919 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001920}
1921
mflodmancc3d4422017-08-03 08:27:51 -07001922TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001923 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001924 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1925 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001926
1927 const int kWidth = 1280;
1928 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001929 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001930 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001931 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1932 EXPECT_FALSE(stats.cpu_limited_resolution);
1933 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1934
1935 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001936 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001937 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001938 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001939
1940 stats = stats_proxy_->GetStats();
1941 EXPECT_TRUE(stats.cpu_limited_resolution);
1942 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1943
1944 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001945 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001946 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001947 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001948
1949 stats = stats_proxy_->GetStats();
1950 EXPECT_FALSE(stats.cpu_limited_resolution);
1951 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001952 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001953
mflodmancc3d4422017-08-03 08:27:51 -07001954 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001955}
1956
mflodmancc3d4422017-08-03 08:27:51 -07001957TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001958 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001959 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1960 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001961
asaperssonfab67072017-04-04 05:51:49 -07001962 const int kWidth = 1280;
1963 const int kHeight = 720;
1964 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001965 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001966 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001967 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001968 EXPECT_FALSE(stats.cpu_limited_resolution);
1969 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1970
asaperssonfab67072017-04-04 05:51:49 -07001971 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001972 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001973 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001974 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001975 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001976 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001977 EXPECT_TRUE(stats.cpu_limited_resolution);
1978 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1979
1980 // Set new source with adaptation still enabled.
1981 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001982 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001983 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001984
asaperssonfab67072017-04-04 05:51:49 -07001985 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001986 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001987 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001988 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001989 EXPECT_TRUE(stats.cpu_limited_resolution);
1990 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1991
1992 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001993 video_stream_encoder_->SetSource(&new_video_source,
1994 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001995
asaperssonfab67072017-04-04 05:51:49 -07001996 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001997 WaitForEncodedFrame(4);
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_FALSE(stats.cpu_limited_resolution);
2001 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2002
2003 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002004 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002005 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002006
asaperssonfab67072017-04-04 05:51:49 -07002007 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002008 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002009 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002010 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002011 EXPECT_TRUE(stats.cpu_limited_resolution);
2012 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2013
asaperssonfab67072017-04-04 05:51:49 -07002014 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002015 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002016 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002017 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002018 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002019 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002020 EXPECT_FALSE(stats.cpu_limited_resolution);
2021 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002022 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002023
mflodmancc3d4422017-08-03 08:27:51 -07002024 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002025}
2026
mflodmancc3d4422017-08-03 08:27:51 -07002027TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002028 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002029 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2030 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002031
asaperssonfab67072017-04-04 05:51:49 -07002032 const int kWidth = 1280;
2033 const int kHeight = 720;
2034 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002035 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002036 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002037 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002038 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002039 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002040
2041 // Set new source with adaptation still enabled.
2042 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002043 video_stream_encoder_->SetSource(&new_video_source,
2044 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002045
asaperssonfab67072017-04-04 05:51:49 -07002046 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002047 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002048 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002049 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002050 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002051 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002052
asaperssonfab67072017-04-04 05:51:49 -07002053 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002054 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002055 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002056 WaitForEncodedFrame(3);
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
asaperssonfab67072017-04-04 05:51:49 -07002062 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002063 video_stream_encoder_->SetSource(&new_video_source,
2064 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002065
asaperssonfab67072017-04-04 05:51:49 -07002066 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002067 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002068 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002069 EXPECT_TRUE(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);
kthelgason876222f2016-11-29 01:44:11 -08002072
asapersson02465b82017-04-10 01:12:52 -07002073 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002074 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002075 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002076
asaperssonfab67072017-04-04 05:51:49 -07002077 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002078 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002079 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002080 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002081 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002082 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2083 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002084
mflodmancc3d4422017-08-03 08:27:51 -07002085 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002086}
2087
mflodmancc3d4422017-08-03 08:27:51 -07002088TEST_F(VideoStreamEncoderTest,
2089 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002090 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002091 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2092 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002093
2094 const int kWidth = 1280;
2095 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002096 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002097 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002098 video_source_.IncomingCapturedFrame(
2099 CreateFrame(timestamp_ms, kWidth, kHeight));
2100 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002101 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2102 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2103 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2104
2105 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002106 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002107 timestamp_ms += kFrameIntervalMs;
2108 video_source_.IncomingCapturedFrame(
2109 CreateFrame(timestamp_ms, kWidth, kHeight));
2110 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002111 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2112 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2113 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2114
2115 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002116 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002117 timestamp_ms += kFrameIntervalMs;
2118 video_source_.IncomingCapturedFrame(
2119 CreateFrame(timestamp_ms, kWidth, kHeight));
2120 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002121 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2122 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2123 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2124
Niels Möller4db138e2018-04-19 09:04:13 +02002125 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002126 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002127
2128 VideoEncoderConfig video_encoder_config;
2129 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2130 // Make format different, to force recreation of encoder.
2131 video_encoder_config.video_format.parameters["foo"] = "foo";
2132 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002133 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002134 timestamp_ms += kFrameIntervalMs;
2135 video_source_.IncomingCapturedFrame(
2136 CreateFrame(timestamp_ms, kWidth, kHeight));
2137 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002138 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2139 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2140 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2141
mflodmancc3d4422017-08-03 08:27:51 -07002142 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002143}
2144
mflodmancc3d4422017-08-03 08:27:51 -07002145TEST_F(VideoStreamEncoderTest,
2146 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002147 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002148 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2149 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002150
asapersson0944a802017-04-07 00:57:58 -07002151 const int kWidth = 1280;
2152 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002153 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002154
asaperssonfab67072017-04-04 05:51:49 -07002155 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002156 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002157 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002158 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002159 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002160 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2161
asapersson02465b82017-04-10 01:12:52 -07002162 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002163 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002164 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002165 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002166 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002167 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002168 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002169 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2170
2171 // Set new source with adaptation still enabled.
2172 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002173 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002174 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002175
2176 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();
2180 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002181 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002182 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2183
sprangc5d62e22017-04-02 23:53:04 -07002184 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002185 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002186 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002187 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002188 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002189 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002190 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002191 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002192 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002193 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002194 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2195
sprangc5d62e22017-04-02 23:53:04 -07002196 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002197 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002198 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2199 mock_stats.input_frame_rate = 30;
2200 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002201 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002202 stats_proxy_->ResetMockStats();
2203
2204 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002205 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002206 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002207
2208 // Framerate now adapted.
2209 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002210 EXPECT_FALSE(stats.cpu_limited_resolution);
2211 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002212 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2213
2214 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002215 video_stream_encoder_->SetSource(&new_video_source,
2216 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002217 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002218 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002219 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002220
2221 stats = stats_proxy_->GetStats();
2222 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002223 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002224 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2225
2226 // Try to trigger overuse. Should not succeed.
2227 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002228 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002229 stats_proxy_->ResetMockStats();
2230
2231 stats = stats_proxy_->GetStats();
2232 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002233 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002234 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2235
2236 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002237 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002238 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002239 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002240 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002241 stats = stats_proxy_->GetStats();
2242 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002243 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002244 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002245
2246 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002247 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002248 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002249 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002250 stats = stats_proxy_->GetStats();
2251 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002252 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002253 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2254
2255 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002256 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002257 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
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();
asapersson13874762017-06-07 00:01:02 -07002262 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002263 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002264 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002265 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2266
2267 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002268 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002269 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002270 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002271 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002272 stats = stats_proxy_->GetStats();
2273 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002274 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002275 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002276 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002277
mflodmancc3d4422017-08-03 08:27:51 -07002278 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002279}
2280
mflodmancc3d4422017-08-03 08:27:51 -07002281TEST_F(VideoStreamEncoderTest,
2282 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002283 const int kWidth = 1280;
2284 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002285 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002286 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2287 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002288
asaperssonfab67072017-04-04 05:51:49 -07002289 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002290 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002291
asaperssonfab67072017-04-04 05:51:49 -07002292 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002293 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002294
asaperssonfab67072017-04-04 05:51:49 -07002295 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002296 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002297
asaperssonfab67072017-04-04 05:51:49 -07002298 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002299 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002300
kthelgason876222f2016-11-29 01:44:11 -08002301 // Expect a scale down.
2302 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002303 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002304
asapersson02465b82017-04-10 01:12:52 -07002305 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002306 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002307 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002308 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002309
asaperssonfab67072017-04-04 05:51:49 -07002310 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002311 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002312 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002313 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002314
asaperssonfab67072017-04-04 05:51:49 -07002315 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002316 EXPECT_EQ(std::numeric_limits<int>::max(),
2317 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002318
asaperssonfab67072017-04-04 05:51:49 -07002319 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002320 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002321 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002322 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002323
asapersson02465b82017-04-10 01:12:52 -07002324 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002325 EXPECT_EQ(std::numeric_limits<int>::max(),
2326 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002327
mflodmancc3d4422017-08-03 08:27:51 -07002328 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002329}
2330
mflodmancc3d4422017-08-03 08:27:51 -07002331TEST_F(VideoStreamEncoderTest,
2332 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002333 const int kWidth = 1280;
2334 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002335 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002336 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2337 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002338
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002339 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002340 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002341 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002342 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002343
2344 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002345 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002346 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002347 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2348 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2349
2350 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002351 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002352 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002353 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2354 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2355 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2356
2357 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002358 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002359 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2360 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2361 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2362
mflodmancc3d4422017-08-03 08:27:51 -07002363 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002364}
2365
mflodmancc3d4422017-08-03 08:27:51 -07002366TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002367 const int kWidth = 1280;
2368 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002369 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002370 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2371 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002372
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002373 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002374 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002375 video_stream_encoder_->SetSource(&source,
2376 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002377 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2378 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002379 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002380
2381 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002382 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002383 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2384 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2385 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2386 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2387
2388 // Trigger adapt down for same input resolution, expect no change.
2389 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2390 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002391 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002392 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2393 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2394 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2395
2396 // Trigger adapt down for larger input resolution, expect no change.
2397 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2398 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002399 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002400 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2401 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2402 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2403
mflodmancc3d4422017-08-03 08:27:51 -07002404 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002405}
2406
mflodmancc3d4422017-08-03 08:27:51 -07002407TEST_F(VideoStreamEncoderTest,
2408 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002409 const int kWidth = 1280;
2410 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002411 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002412 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2413 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002414
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002415 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002416 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002417 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002418 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002419
2420 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002421 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002422 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002423 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2424 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2425
2426 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002427 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002428 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002429 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2430 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2431
mflodmancc3d4422017-08-03 08:27:51 -07002432 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002433}
2434
mflodmancc3d4422017-08-03 08:27:51 -07002435TEST_F(VideoStreamEncoderTest,
2436 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002437 const int kWidth = 1280;
2438 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002439 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002440 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2441 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002442
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002443 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002444 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002445 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002446 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002447
2448 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002449 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002450 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002451 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002452 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2453
2454 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002455 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002456 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002457 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002458 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2459
mflodmancc3d4422017-08-03 08:27:51 -07002460 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002461}
2462
mflodmancc3d4422017-08-03 08:27:51 -07002463TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002464 const int kWidth = 1280;
2465 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002466 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002467 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2468 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002469
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002470 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002471 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002472 video_stream_encoder_->SetSource(&source,
2473 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002474
2475 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2476 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002477 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002478 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2479 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2480 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2481
2482 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002483 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002484 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002485 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2486 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2487 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2488
mflodmancc3d4422017-08-03 08:27:51 -07002489 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002490}
2491
mflodmancc3d4422017-08-03 08:27:51 -07002492TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002493 const int kWidth = 1280;
2494 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002495 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002496 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2497 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002498
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002499 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002500 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002501 video_stream_encoder_->SetSource(&source,
2502 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002503
2504 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2505 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002506 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002507 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2508 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2509 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2510
2511 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002512 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002513 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002514 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2515 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2516 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2517
mflodmancc3d4422017-08-03 08:27:51 -07002518 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002519}
2520
mflodmancc3d4422017-08-03 08:27:51 -07002521TEST_F(VideoStreamEncoderTest,
2522 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002523 const int kWidth = 1280;
2524 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002525 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002526 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2527 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002528
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002529 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002530 AdaptingFrameForwarder source;
2531 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002532 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002533 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002534
2535 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002536 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002537 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002538 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2539 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2540
2541 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002542 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002543 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002544 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002545 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002546 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2547 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2548
2549 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002550 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002551 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002552 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2553 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2554 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2555
mflodmancc3d4422017-08-03 08:27:51 -07002556 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002557}
2558
mflodmancc3d4422017-08-03 08:27:51 -07002559TEST_F(VideoStreamEncoderTest,
2560 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002561 const int kWidth = 1280;
2562 const int kHeight = 720;
2563 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002564 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002565 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2566 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002567
2568 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2569 stats.input_frame_rate = kInputFps;
2570 stats_proxy_->SetMockStats(stats);
2571
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002572 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002573 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2574 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002575 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002576
2577 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002578 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002579 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2580 sink_.WaitForEncodedFrame(2);
2581 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2582
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002583 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002584 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002585 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002586 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002587 // Give the encoder queue time to process the change in degradation preference
2588 // by waiting for an encoded frame.
2589 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2590 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002591 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002592
2593 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002594 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002595 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2596 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002597 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2598
2599 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002600 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002601 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002602
mflodmancc3d4422017-08-03 08:27:51 -07002603 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002604}
2605
mflodmancc3d4422017-08-03 08:27:51 -07002606TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002607 const int kWidth = 1280;
2608 const int kHeight = 720;
2609 const size_t kNumFrames = 10;
2610
Erik Språng4c6ca302019-04-08 15:14:01 +02002611 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002612 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2613 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002614
asaperssond0de2952017-04-21 01:47:31 -07002615 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002616 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002617 video_source_.set_adaptation_enabled(true);
2618
2619 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2620 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2621
2622 int downscales = 0;
2623 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002624 video_source_.IncomingCapturedFrame(
2625 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2626 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002627
asaperssonfab67072017-04-04 05:51:49 -07002628 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002629 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002630 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002631 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002632
2633 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2634 ++downscales;
2635
2636 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2637 EXPECT_EQ(downscales,
2638 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2639 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002640 }
mflodmancc3d4422017-08-03 08:27:51 -07002641 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002642}
2643
mflodmancc3d4422017-08-03 08:27:51 -07002644TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002645 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2646 const int kWidth = 1280;
2647 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002648 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002649 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2650 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002651
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002652 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002653 AdaptingFrameForwarder source;
2654 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002655 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002656 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002657
Åsa Persson8c1bf952018-09-13 10:42:19 +02002658 int64_t timestamp_ms = kFrameIntervalMs;
2659 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002660 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002661 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002662 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2663 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2664
2665 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002667 timestamp_ms += kFrameIntervalMs;
2668 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2669 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002670 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002671 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2672 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2673
2674 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002675 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002676 timestamp_ms += kFrameIntervalMs;
2677 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002678 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002679 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002680 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2681 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2682
2683 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002684 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002685 timestamp_ms += kFrameIntervalMs;
2686 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2687 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002688 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002689 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2690 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2691
2692 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002693 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002694 timestamp_ms += kFrameIntervalMs;
2695 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002696 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002697 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002698 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2699 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2700
mflodmancc3d4422017-08-03 08:27:51 -07002701 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002702}
2703
mflodmancc3d4422017-08-03 08:27:51 -07002704TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002705 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2706 const int kWidth = 1280;
2707 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002708 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002709 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2710 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002711
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002712 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002713 AdaptingFrameForwarder source;
2714 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002715 video_stream_encoder_->SetSource(&source,
2716 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002717
Åsa Persson8c1bf952018-09-13 10:42:19 +02002718 int64_t timestamp_ms = kFrameIntervalMs;
2719 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002720 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002721 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002722 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2723 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2724
2725 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002726 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002727 timestamp_ms += kFrameIntervalMs;
2728 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2729 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002730 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2731 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2732 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2733
2734 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002735 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002736 timestamp_ms += kFrameIntervalMs;
2737 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002738 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002739 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002740 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2741 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2742
2743 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002744 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002745 timestamp_ms += kFrameIntervalMs;
2746 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2747 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002748 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2749 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2750 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2751
2752 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002753 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002754 timestamp_ms += kFrameIntervalMs;
2755 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002756 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002757 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002758 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2759 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2760
mflodmancc3d4422017-08-03 08:27:51 -07002761 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002762}
2763
Sergey Silkin41c650b2019-10-14 13:12:19 +02002764TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2765 fake_encoder_.SetResolutionBitrateLimits(
2766 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2767
2768 video_stream_encoder_->OnBitrateUpdated(
2769 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2770 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2771 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2772
2773 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2774 AdaptingFrameForwarder source;
2775 source.set_adaptation_enabled(true);
2776 video_stream_encoder_->SetSource(
2777 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2778
2779 // Insert 720p frame.
2780 int64_t timestamp_ms = kFrameIntervalMs;
2781 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2782 WaitForEncodedFrame(1280, 720);
2783
2784 // Reduce bitrate and trigger adapt down.
2785 video_stream_encoder_->OnBitrateUpdated(
2786 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2787 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2788 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2789 video_stream_encoder_->TriggerQualityLow();
2790
2791 // Insert 720p frame. It should be downscaled and encoded.
2792 timestamp_ms += kFrameIntervalMs;
2793 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2794 WaitForEncodedFrame(960, 540);
2795
2796 // Trigger adapt up. Higher resolution should not be requested duo to lack
2797 // of bitrate.
2798 video_stream_encoder_->TriggerQualityHigh();
2799 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2800
2801 // Increase bitrate.
2802 video_stream_encoder_->OnBitrateUpdated(
2803 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2804 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2805 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2806
2807 // Trigger adapt up. Higher resolution should be requested.
2808 video_stream_encoder_->TriggerQualityHigh();
2809 VerifyFpsMaxResolutionMax(source.sink_wants());
2810
2811 video_stream_encoder_->Stop();
2812}
2813
2814TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2815 fake_encoder_.SetResolutionBitrateLimits(
2816 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2817
2818 // Set bitrate equal to min bitrate of 540p.
2819 video_stream_encoder_->OnBitrateUpdated(
2820 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2821 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2822 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2823
2824 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2825 AdaptingFrameForwarder source;
2826 source.set_adaptation_enabled(true);
2827 video_stream_encoder_->SetSource(
2828 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2829
2830 // Insert 720p frame. It should be dropped and lower resolution should be
2831 // requested.
2832 int64_t timestamp_ms = kFrameIntervalMs;
2833 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2834 ExpectDroppedFrame();
2835 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2836
2837 // Insert 720p frame. It should be downscaled and encoded.
2838 timestamp_ms += kFrameIntervalMs;
2839 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2840 WaitForEncodedFrame(960, 540);
2841
2842 video_stream_encoder_->Stop();
2843}
2844
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002845class BalancedDegradationTest : public VideoStreamEncoderTest {
2846 protected:
2847 void SetupTest() {
2848 // Reset encoder for field trials to take effect.
2849 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002850 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002851
2852 // Enable BALANCED preference.
2853 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002854 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2855 }
2856
2857 void OnBitrateUpdated(int bitrate_bps) {
2858 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2859 DataRate::bps(bitrate_bps),
2860 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002861 }
2862
Åsa Persson45b176f2019-09-30 11:19:05 +02002863 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002864 timestamp_ms_ += kFrameIntervalMs;
2865 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002866 }
2867
2868 void InsertFrameAndWaitForEncoded() {
2869 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002870 sink_.WaitForEncodedFrame(timestamp_ms_);
2871 }
2872
2873 const int kWidth = 640; // pixels:640x360=230400
2874 const int kHeight = 360;
2875 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2876 int64_t timestamp_ms_ = 0;
2877 AdaptingFrameForwarder source_;
2878};
2879
2880TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2881 test::ScopedFieldTrials field_trials(
2882 "WebRTC-Video-BalancedDegradationSettings/"
2883 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2884 SetupTest();
2885
2886 // Force input frame rate.
2887 const int kInputFps = 24;
2888 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2889 stats.input_frame_rate = kInputFps;
2890 stats_proxy_->SetMockStats(stats);
2891
Åsa Persson45b176f2019-09-30 11:19:05 +02002892 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002893 VerifyFpsMaxResolutionMax(source_.sink_wants());
2894
2895 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2896 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2897 video_stream_encoder_->TriggerQualityLowExpectFalse();
2898 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2899
2900 video_stream_encoder_->Stop();
2901}
2902
2903TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2904 test::ScopedFieldTrials field_trials(
2905 "WebRTC-Video-BalancedDegradationSettings/"
2906 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2907 SetupTest();
2908
2909 // Force input frame rate.
2910 const int kInputFps = 25;
2911 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2912 stats.input_frame_rate = kInputFps;
2913 stats_proxy_->SetMockStats(stats);
2914
Åsa Persson45b176f2019-09-30 11:19:05 +02002915 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002916 VerifyFpsMaxResolutionMax(source_.sink_wants());
2917
2918 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2919 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2920 video_stream_encoder_->TriggerQualityLow();
2921 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2922
2923 video_stream_encoder_->Stop();
2924}
2925
2926TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2927 test::ScopedFieldTrials field_trials(
2928 "WebRTC-Video-BalancedDegradationSettings/"
2929 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2930 SetupTest();
2931
2932 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2933
Åsa Persson45b176f2019-09-30 11:19:05 +02002934 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002935 VerifyFpsMaxResolutionMax(source_.sink_wants());
2936
2937 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2938 video_stream_encoder_->TriggerQualityLow();
2939 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2940
2941 video_stream_encoder_->Stop();
2942}
2943
Åsa Perssonccfb3402019-09-25 15:13:04 +02002944TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002945 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002946 "WebRTC-Video-BalancedDegradationSettings/"
2947 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002948 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002949
Åsa Persson1b247f12019-08-14 17:26:39 +02002950 const int kMinBitrateBps = 425000;
2951 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002952 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002953
Åsa Persson45b176f2019-09-30 11:19:05 +02002954 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002955 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002956 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2957
2958 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2959 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002960 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002961 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002962 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2963
2964 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2965 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002966 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002967 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002968 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2969
Åsa Persson30ab0152019-08-27 12:22:33 +02002970 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2971 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002972 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002973 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2974 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002975 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2976
2977 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002978 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002979 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002980 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002981
Åsa Persson30ab0152019-08-27 12:22:33 +02002982 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002983 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002984 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002985 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002986 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002987 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2988
2989 video_stream_encoder_->Stop();
2990}
2991
Åsa Perssonccfb3402019-09-25 15:13:04 +02002992TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002993 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2994 test::ScopedFieldTrials field_trials(
2995 "WebRTC-Video-BalancedDegradationSettings/"
2996 "pixels:57600|129600|230400,fps:7|24|24/");
2997 SetupTest();
2998 OnBitrateUpdated(kLowTargetBitrateBps);
2999
3000 VerifyNoLimitation(source_.sink_wants());
3001
3002 // Insert frame, expect scaled down:
3003 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3004 InsertFrame();
3005 EXPECT_FALSE(WaitForFrame(1000));
3006 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3007 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3008
3009 // Insert frame, expect scaled down:
3010 // resolution (320x180@24fps).
3011 InsertFrame();
3012 EXPECT_FALSE(WaitForFrame(1000));
3013 EXPECT_LT(source_.sink_wants().max_pixel_count,
3014 source_.last_wants().max_pixel_count);
3015 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3016
3017 // Frame should not be dropped (min pixels per frame reached).
3018 InsertFrameAndWaitForEncoded();
3019
3020 video_stream_encoder_->Stop();
3021}
3022
3023TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003024 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003025 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003026 "WebRTC-Video-BalancedDegradationSettings/"
3027 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003028 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003029
Åsa Persson30ab0152019-08-27 12:22:33 +02003030 const int kResolutionMinBitrateBps = 435000;
3031 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003032 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003033
Åsa Persson45b176f2019-09-30 11:19:05 +02003034 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003035 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003036 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3037
3038 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3039 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003040 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003041 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003042 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3043
3044 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3045 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003046 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003047 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003048 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3049
3050 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3051 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003052 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003053 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003054 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3055
Åsa Persson30ab0152019-08-27 12:22:33 +02003056 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3057 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003058 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003059 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003060 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3061
3062 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3063 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003064 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003065 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3066
3067 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003068 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003069 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003070 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003071 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003072 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3073
3074 video_stream_encoder_->Stop();
3075}
3076
Åsa Perssonccfb3402019-09-25 15:13:04 +02003077TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003078 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003079 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003080 "WebRTC-Video-BalancedDegradationSettings/"
3081 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003082 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003083
Åsa Persson30ab0152019-08-27 12:22:33 +02003084 const int kMinBitrateBps = 425000;
3085 const int kTooLowMinBitrateBps = 424000;
3086 const int kResolutionMinBitrateBps = 435000;
3087 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003088 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003089
Åsa Persson45b176f2019-09-30 11:19:05 +02003090 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003091 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003092 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3093
3094 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3095 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003096 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003097 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003098 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3099
3100 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3101 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003102 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003103 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003104 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3105
3106 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3107 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003108 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003109 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003110 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3111
3112 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3113 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003114 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003115 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3116
3117 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003118 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003119 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003120 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003121 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003122 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3123
3124 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003125 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003126 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003127 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003128 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3129
3130 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003131 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003132 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003133 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003134 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003135 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3136
Åsa Persson1b247f12019-08-14 17:26:39 +02003137 video_stream_encoder_->Stop();
3138}
3139
mflodmancc3d4422017-08-03 08:27:51 -07003140TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003141 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3142 const int kWidth = 1280;
3143 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003144 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003145 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3146 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003147
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003148 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003149 AdaptingFrameForwarder source;
3150 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003151 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003152 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003153
Åsa Persson8c1bf952018-09-13 10:42:19 +02003154 int64_t timestamp_ms = kFrameIntervalMs;
3155 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003156 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003157 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003158 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3159 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3160 EXPECT_EQ(0, 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 (960x540).
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(), kWidth * kHeight);
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(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3172 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3173
3174 // Trigger cpu adapt down, expect scaled down resolution (640x360).
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);
asapersson09f05612017-05-15 23:40:18 -07003179 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);
3182 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3183 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3184
Jonathan Yubc771b72017-12-08 17:04:29 -08003185 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003186 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003187 timestamp_ms += kFrameIntervalMs;
3188 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3189 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003190 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003191 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3192 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003193 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003194 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3195
Jonathan Yubc771b72017-12-08 17:04:29 -08003196 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003197 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003198 timestamp_ms += kFrameIntervalMs;
3199 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3200 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003201 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003202 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003203 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
Jonathan Yubc771b72017-12-08 17:04:29 -08003208 // Trigger quality adapt down, expect no change (min resolution reached).
3209 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003210 timestamp_ms += kFrameIntervalMs;
3211 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3212 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003213 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3214 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3215 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3216 EXPECT_EQ(3, 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 (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003220 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);
asapersson09f05612017-05-15 23:40:18 -07003224 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003225 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3226 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3227 EXPECT_EQ(4, 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 (640x360).
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());
3236 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3237 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3238 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3239 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3240
3241 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3242 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003243 timestamp_ms += kFrameIntervalMs;
3244 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3245 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003246 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003247 last_wants = source.sink_wants();
3248 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 cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003254 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003255 timestamp_ms += kFrameIntervalMs;
3256 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3257 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003258 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003259 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3260 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003261 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003262 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3263
3264 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003265 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003266 timestamp_ms += kFrameIntervalMs;
3267 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003268 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003269 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003270 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003271 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3272 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003273 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003274 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003275
mflodmancc3d4422017-08-03 08:27:51 -07003276 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003277}
3278
mflodmancc3d4422017-08-03 08:27:51 -07003279TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003280 const int kWidth = 640;
3281 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003282
Erik Språng4c6ca302019-04-08 15:14:01 +02003283 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003284 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3285 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003286
perkj803d97f2016-11-01 11:45:46 -07003287 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003288 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003289 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003290 }
3291
mflodmancc3d4422017-08-03 08:27:51 -07003292 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003293 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003294 video_source_.IncomingCapturedFrame(CreateFrame(
3295 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003296 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003297 }
3298
mflodmancc3d4422017-08-03 08:27:51 -07003299 video_stream_encoder_->Stop();
3300 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003301 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003302
Ying Wangef3998f2019-12-09 13:06:53 +01003303 EXPECT_METRIC_EQ(
3304 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3305 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003306 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3307}
3308
mflodmancc3d4422017-08-03 08:27:51 -07003309TEST_F(VideoStreamEncoderTest,
3310 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003311 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003312 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3313 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003314 const int kWidth = 640;
3315 const int kHeight = 360;
3316
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003317 video_stream_encoder_->SetSource(&video_source_,
3318 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003319
3320 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3321 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003322 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003323 }
3324
mflodmancc3d4422017-08-03 08:27:51 -07003325 video_stream_encoder_->Stop();
3326 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003327 stats_proxy_.reset();
3328
3329 EXPECT_EQ(0,
3330 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3331}
3332
mflodmancc3d4422017-08-03 08:27:51 -07003333TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003334 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003335 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003336
3337 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003338 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003339 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003340 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3341 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003342
sprang57c2fff2017-01-16 06:24:02 -08003343 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003344 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003345 video_stream_encoder_->OnBitrateUpdated(
3346 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3347 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003348
sprang57c2fff2017-01-16 06:24:02 -08003349 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003350 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3351 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003352 VideoBitrateAllocation bitrate_allocation =
3353 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003354 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003355 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003356 // TODO(srte): The use of millisecs here looks like an error, but the tests
3357 // fails using seconds, this should be investigated.
3358 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003359
3360 // Not called on second frame.
3361 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3362 .Times(0);
3363 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003364 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3365 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003366 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003367
3368 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003369 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3370 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003371 const int64_t start_time_ms = rtc::TimeMillis();
3372 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3373 video_source_.IncomingCapturedFrame(
3374 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3375 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003376 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003377 }
3378
3379 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003380 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003381
mflodmancc3d4422017-08-03 08:27:51 -07003382 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003383}
3384
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003385TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3386 // 2 TLs configured, temporal layers supported by encoder.
3387 const int kNumTemporalLayers = 2;
3388 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3389 fake_encoder_.SetTemporalLayersSupported(0, true);
3390
3391 // Bitrate allocated across temporal layers.
3392 const int kTl0Bps = kTargetBitrateBps *
3393 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003394 kNumTemporalLayers, /*temporal_id*/ 0,
3395 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003396 const int kTl1Bps = kTargetBitrateBps *
3397 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003398 kNumTemporalLayers, /*temporal_id*/ 1,
3399 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003400 VideoBitrateAllocation expected_bitrate;
3401 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3402 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3403
3404 VerifyAllocatedBitrate(expected_bitrate);
3405 video_stream_encoder_->Stop();
3406}
3407
3408TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3409 // 2 TLs configured, temporal layers not supported by encoder.
3410 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3411 fake_encoder_.SetTemporalLayersSupported(0, false);
3412
3413 // Temporal layers not supported by the encoder.
3414 // Total bitrate should be at ti:0.
3415 VideoBitrateAllocation expected_bitrate;
3416 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3417
3418 VerifyAllocatedBitrate(expected_bitrate);
3419 video_stream_encoder_->Stop();
3420}
3421
3422TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3423 // 2 TLs configured, temporal layers only supported for first stream.
3424 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3425 fake_encoder_.SetTemporalLayersSupported(0, true);
3426 fake_encoder_.SetTemporalLayersSupported(1, false);
3427
3428 const int kS0Bps = 150000;
3429 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003430 kS0Bps *
3431 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3432 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003433 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003434 kS0Bps *
3435 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3436 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003437 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3438 // Temporal layers not supported by si:1.
3439 VideoBitrateAllocation expected_bitrate;
3440 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3441 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3442 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3443
3444 VerifyAllocatedBitrate(expected_bitrate);
3445 video_stream_encoder_->Stop();
3446}
3447
Niels Möller7dc26b72017-12-06 10:27:48 +01003448TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3449 const int kFrameWidth = 1280;
3450 const int kFrameHeight = 720;
3451 const int kFramerate = 24;
3452
Erik Språng4c6ca302019-04-08 15:14:01 +02003453 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003454 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3455 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003456 test::FrameForwarder source;
3457 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003458 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003459
3460 // Insert a single frame, triggering initial configuration.
3461 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3462 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3463
3464 EXPECT_EQ(
3465 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3466 kDefaultFramerate);
3467
3468 // Trigger reconfigure encoder (without resetting the entire instance).
3469 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003470 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003471 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3472 video_encoder_config.number_of_streams = 1;
3473 video_encoder_config.video_stream_factory =
3474 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3475 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003476 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003477 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3478
3479 // Detector should be updated with fps limit from codec config.
3480 EXPECT_EQ(
3481 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3482 kFramerate);
3483
3484 // Trigger overuse, max framerate should be reduced.
3485 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3486 stats.input_frame_rate = kFramerate;
3487 stats_proxy_->SetMockStats(stats);
3488 video_stream_encoder_->TriggerCpuOveruse();
3489 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3490 int adapted_framerate =
3491 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3492 EXPECT_LT(adapted_framerate, kFramerate);
3493
3494 // Trigger underuse, max framerate should go back to codec configured fps.
3495 // Set extra low fps, to make sure it's actually reset, not just incremented.
3496 stats = stats_proxy_->GetStats();
3497 stats.input_frame_rate = adapted_framerate / 2;
3498 stats_proxy_->SetMockStats(stats);
3499 video_stream_encoder_->TriggerCpuNormalUsage();
3500 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3501 EXPECT_EQ(
3502 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3503 kFramerate);
3504
3505 video_stream_encoder_->Stop();
3506}
3507
3508TEST_F(VideoStreamEncoderTest,
3509 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3510 const int kFrameWidth = 1280;
3511 const int kFrameHeight = 720;
3512 const int kLowFramerate = 15;
3513 const int kHighFramerate = 25;
3514
Erik Språng4c6ca302019-04-08 15:14:01 +02003515 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003516 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3517 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003518 test::FrameForwarder source;
3519 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003520 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003521
3522 // Trigger initial configuration.
3523 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003524 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003525 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3526 video_encoder_config.number_of_streams = 1;
3527 video_encoder_config.video_stream_factory =
3528 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3529 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3530 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003531 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003532 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3533
3534 EXPECT_EQ(
3535 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3536 kLowFramerate);
3537
3538 // Trigger overuse, max framerate should be reduced.
3539 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3540 stats.input_frame_rate = kLowFramerate;
3541 stats_proxy_->SetMockStats(stats);
3542 video_stream_encoder_->TriggerCpuOveruse();
3543 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3544 int adapted_framerate =
3545 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3546 EXPECT_LT(adapted_framerate, kLowFramerate);
3547
3548 // Reconfigure the encoder with a new (higher max framerate), max fps should
3549 // still respect the adaptation.
3550 video_encoder_config.video_stream_factory =
3551 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3552 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3553 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003554 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003555 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3556
3557 EXPECT_EQ(
3558 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3559 adapted_framerate);
3560
3561 // Trigger underuse, max framerate should go back to codec configured fps.
3562 stats = stats_proxy_->GetStats();
3563 stats.input_frame_rate = adapted_framerate;
3564 stats_proxy_->SetMockStats(stats);
3565 video_stream_encoder_->TriggerCpuNormalUsage();
3566 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3567 EXPECT_EQ(
3568 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3569 kHighFramerate);
3570
3571 video_stream_encoder_->Stop();
3572}
3573
mflodmancc3d4422017-08-03 08:27:51 -07003574TEST_F(VideoStreamEncoderTest,
3575 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003576 const int kFrameWidth = 1280;
3577 const int kFrameHeight = 720;
3578 const int kFramerate = 24;
3579
Erik Språng4c6ca302019-04-08 15:14:01 +02003580 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003581 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3582 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003583 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003584 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003585 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003586
3587 // Trigger initial configuration.
3588 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003589 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003590 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3591 video_encoder_config.number_of_streams = 1;
3592 video_encoder_config.video_stream_factory =
3593 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3594 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003595 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003596 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003597 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003598
Niels Möller7dc26b72017-12-06 10:27:48 +01003599 EXPECT_EQ(
3600 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3601 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003602
3603 // Trigger overuse, max framerate should be reduced.
3604 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3605 stats.input_frame_rate = kFramerate;
3606 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003607 video_stream_encoder_->TriggerCpuOveruse();
3608 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003609 int adapted_framerate =
3610 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003611 EXPECT_LT(adapted_framerate, kFramerate);
3612
3613 // Change degradation preference to not enable framerate scaling. Target
3614 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003615 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003616 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003617 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003618 EXPECT_EQ(
3619 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3620 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003621
mflodmancc3d4422017-08-03 08:27:51 -07003622 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003623}
3624
mflodmancc3d4422017-08-03 08:27:51 -07003625TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003626 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003627 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003628 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003629 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003630 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003631 const int kWidth = 640;
3632 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003633
asaperssonfab67072017-04-04 05:51:49 -07003634 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003635
3636 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003637 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003638
3639 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003640 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003641
sprangc5d62e22017-04-02 23:53:04 -07003642 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003643
asaperssonfab67072017-04-04 05:51:49 -07003644 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003645 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003646 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003647
3648 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003649 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003650
sprangc5d62e22017-04-02 23:53:04 -07003651 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003652
mflodmancc3d4422017-08-03 08:27:51 -07003653 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003654}
3655
mflodmancc3d4422017-08-03 08:27:51 -07003656TEST_F(VideoStreamEncoderTest,
3657 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003658 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003659 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003660 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003661 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003662 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003663 const int kWidth = 640;
3664 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003665
3666 // We expect the n initial frames to get dropped.
3667 int i;
3668 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003669 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003670 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003671 }
3672 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003673 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003674 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003675
3676 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003677 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003678
mflodmancc3d4422017-08-03 08:27:51 -07003679 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003680}
3681
mflodmancc3d4422017-08-03 08:27:51 -07003682TEST_F(VideoStreamEncoderTest,
3683 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003684 const int kWidth = 640;
3685 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003686 video_stream_encoder_->OnBitrateUpdated(
3687 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3688 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003689
3690 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003691 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003692 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003693
asaperssonfab67072017-04-04 05:51:49 -07003694 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003695 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003696 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003697
mflodmancc3d4422017-08-03 08:27:51 -07003698 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003699}
3700
mflodmancc3d4422017-08-03 08:27:51 -07003701TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003702 const int kWidth = 640;
3703 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003704 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003705
3706 VideoEncoderConfig video_encoder_config;
3707 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3708 // Make format different, to force recreation of encoder.
3709 video_encoder_config.video_format.parameters["foo"] = "foo";
3710 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003711 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003712 video_stream_encoder_->OnBitrateUpdated(
3713 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3714 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003715
kthelgasonb83797b2017-02-14 11:57:25 -08003716 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003717 video_stream_encoder_->SetSource(&video_source_,
3718 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003719
asaperssonfab67072017-04-04 05:51:49 -07003720 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003721 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003722 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003723
mflodmancc3d4422017-08-03 08:27:51 -07003724 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003725 fake_encoder_.SetQualityScaling(true);
3726}
3727
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003728TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3729 webrtc::test::ScopedFieldTrials field_trials(
3730 "WebRTC-InitialFramedrop/Enabled/");
3731 // Reset encoder for field trials to take effect.
3732 ConfigureEncoder(video_encoder_config_.Copy());
3733 const int kTooLowBitrateForFrameSizeBps = 10000;
3734 const int kWidth = 640;
3735 const int kHeight = 360;
3736
Erik Språng4c6ca302019-04-08 15:14:01 +02003737 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003738 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3739 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003740 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3741 // Frame should not be dropped.
3742 WaitForEncodedFrame(1);
3743
Erik Språng610c7632019-03-06 15:37:33 +01003744 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003745 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003746 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003747 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003748 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3749 // Expect to drop this frame, the wait should time out.
3750 ExpectDroppedFrame();
3751
3752 // Expect the sink_wants to specify a scaled frame.
3753 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3754 video_stream_encoder_->Stop();
3755}
3756
Åsa Persson139f4dc2019-08-02 09:29:58 +02003757TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3758 webrtc::test::ScopedFieldTrials field_trials(
3759 "WebRTC-Video-QualityScalerSettings/"
3760 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3761 // Reset encoder for field trials to take effect.
3762 ConfigureEncoder(video_encoder_config_.Copy());
3763 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3764 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3765 const int kWidth = 640;
3766 const int kHeight = 360;
3767
3768 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003769 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3770 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003771 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3772 // Frame should not be dropped.
3773 WaitForEncodedFrame(1);
3774
3775 video_stream_encoder_->OnBitrateUpdated(
3776 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003777 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003778 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3779 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3780 // Frame should not be dropped.
3781 WaitForEncodedFrame(2);
3782
3783 video_stream_encoder_->OnBitrateUpdated(
3784 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003785 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003786 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3787 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3788 // Expect to drop this frame, the wait should time out.
3789 ExpectDroppedFrame();
3790
3791 // Expect the sink_wants to specify a scaled frame.
3792 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3793 video_stream_encoder_->Stop();
3794}
3795
Åsa Perssone644a032019-11-08 15:56:00 +01003796TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3797 webrtc::test::ScopedFieldTrials field_trials(
3798 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3799
3800 // Reset encoder for field trials to take effect.
3801 VideoEncoderConfig config = video_encoder_config_.Copy();
3802 config.max_bitrate_bps = kTargetBitrateBps;
3803 ConfigureEncoder(std::move(config));
3804 fake_encoder_.SetQp(kQpLow);
3805
3806 // Enable MAINTAIN_FRAMERATE preference.
3807 AdaptingFrameForwarder source;
3808 source.set_adaptation_enabled(true);
3809 video_stream_encoder_->SetSource(&source,
3810 DegradationPreference::MAINTAIN_FRAMERATE);
3811
3812 // Start at low bitrate.
3813 const int kLowBitrateBps = 200000;
3814 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3815 DataRate::bps(kLowBitrateBps),
3816 DataRate::bps(kLowBitrateBps), 0, 0);
3817
3818 // Expect first frame to be dropped and resolution to be limited.
3819 const int kWidth = 1280;
3820 const int kHeight = 720;
3821 const int64_t kFrameIntervalMs = 100;
3822 int64_t timestamp_ms = kFrameIntervalMs;
3823 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3824 ExpectDroppedFrame();
3825 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3826
3827 // Increase bitrate to encoder max.
3828 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3829 DataRate::bps(config.max_bitrate_bps),
3830 DataRate::bps(config.max_bitrate_bps),
3831 0, 0);
3832
3833 // Insert frames and advance |min_duration_ms|.
3834 for (size_t i = 1; i <= 10; i++) {
3835 timestamp_ms += kFrameIntervalMs;
3836 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3837 WaitForEncodedFrame(timestamp_ms);
3838 }
3839 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3840 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3841
3842 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3843
3844 // Insert frame should trigger high BW and release quality limitation.
3845 timestamp_ms += kFrameIntervalMs;
3846 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3847 WaitForEncodedFrame(timestamp_ms);
3848 VerifyFpsMaxResolutionMax(source.sink_wants());
3849
3850 // Frame should not be adapted.
3851 timestamp_ms += kFrameIntervalMs;
3852 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3853 WaitForEncodedFrame(kWidth, kHeight);
3854 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3855
3856 video_stream_encoder_->Stop();
3857}
3858
mflodmancc3d4422017-08-03 08:27:51 -07003859TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003860 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3861 const int kTooSmallWidth = 10;
3862 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003863 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003864 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3865 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003866
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003867 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003868 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003869 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003870 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003871 VerifyNoLimitation(source.sink_wants());
3872 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3873
3874 // Trigger adapt down, too small frame, expect no change.
3875 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003876 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003877 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003878 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003879 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3880 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3881
mflodmancc3d4422017-08-03 08:27:51 -07003882 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003883}
3884
mflodmancc3d4422017-08-03 08:27:51 -07003885TEST_F(VideoStreamEncoderTest,
3886 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003887 const int kTooSmallWidth = 10;
3888 const int kTooSmallHeight = 10;
3889 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003890 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003891 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3892 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003893
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003894 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003895 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003896 video_stream_encoder_->SetSource(&source,
3897 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003898 VerifyNoLimitation(source.sink_wants());
3899 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3900 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3901
3902 // Trigger adapt down, expect limited framerate.
3903 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003904 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003905 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003906 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3907 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3908 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3909 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3910
3911 // Trigger adapt down, too small frame, expect no change.
3912 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003913 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003914 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003915 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3916 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3917 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3918 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3919
mflodmancc3d4422017-08-03 08:27:51 -07003920 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003921}
3922
mflodmancc3d4422017-08-03 08:27:51 -07003923TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003924 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003925 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003926 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3927 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003928 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003929 const int kFrameWidth = 1280;
3930 const int kFrameHeight = 720;
3931 video_source_.IncomingCapturedFrame(
3932 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003933 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003934 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003935}
3936
sprangb1ca0732017-02-01 08:38:12 -08003937// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003938TEST_F(VideoStreamEncoderTest,
3939 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003940 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003941 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3942 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003943
3944 const int kFrameWidth = 1280;
3945 const int kFrameHeight = 720;
3946 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003947 // requested by
3948 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003949 video_source_.set_adaptation_enabled(true);
3950
3951 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003952 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003953 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003954
3955 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003956 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003957 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003958 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003959 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003960
asaperssonfab67072017-04-04 05:51:49 -07003961 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003962 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003963 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003964 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003965 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003966
mflodmancc3d4422017-08-03 08:27:51 -07003967 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003968}
sprangfe627f32017-03-29 08:24:59 -07003969
mflodmancc3d4422017-08-03 08:27:51 -07003970TEST_F(VideoStreamEncoderTest,
3971 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003972 const int kFrameWidth = 1280;
3973 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003974
Erik Språng4c6ca302019-04-08 15:14:01 +02003975 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003976 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3977 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003978 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003979 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003980 video_source_.set_adaptation_enabled(true);
3981
sprang4847ae62017-06-27 07:06:52 -07003982 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003983
3984 video_source_.IncomingCapturedFrame(
3985 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003986 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003987
3988 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003989 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003990
3991 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003992 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003993 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003994 video_source_.IncomingCapturedFrame(
3995 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003996 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003997 }
3998
3999 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07004000 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004001 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004002 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004003 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004004 video_source_.IncomingCapturedFrame(
4005 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004006 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004007 ++num_frames_dropped;
4008 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004009 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004010 }
4011 }
4012
sprang4847ae62017-06-27 07:06:52 -07004013 // Add some slack to account for frames dropped by the frame dropper.
4014 const int kErrorMargin = 1;
4015 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004016 kErrorMargin);
4017
4018 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004019 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004020 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004021 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004022 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004023 video_source_.IncomingCapturedFrame(
4024 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004025 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004026 ++num_frames_dropped;
4027 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004028 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004029 }
4030 }
sprang4847ae62017-06-27 07:06:52 -07004031 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004032 kErrorMargin);
4033
4034 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004035 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004036 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004037 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004038 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004039 video_source_.IncomingCapturedFrame(
4040 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004041 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004042 ++num_frames_dropped;
4043 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004044 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004045 }
4046 }
sprang4847ae62017-06-27 07:06:52 -07004047 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004048 kErrorMargin);
4049
4050 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004051 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004052 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004053 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004054 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004055 video_source_.IncomingCapturedFrame(
4056 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004057 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004058 ++num_frames_dropped;
4059 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004060 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004061 }
4062 }
4063 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4064
mflodmancc3d4422017-08-03 08:27:51 -07004065 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004066}
4067
mflodmancc3d4422017-08-03 08:27:51 -07004068TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004069 const int kFramerateFps = 5;
4070 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004071 const int kFrameWidth = 1280;
4072 const int kFrameHeight = 720;
4073
sprang4847ae62017-06-27 07:06:52 -07004074 // Reconfigure encoder with two temporal layers and screensharing, which will
4075 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004076 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004077
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);
mflodmancc3d4422017-08-03 08:27:51 -07004081 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004082 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004083 video_source_.set_adaptation_enabled(true);
4084
sprang4847ae62017-06-27 07:06:52 -07004085 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004086
4087 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004088 rtc::VideoSinkWants last_wants;
4089 do {
4090 last_wants = video_source_.sink_wants();
4091
sprangc5d62e22017-04-02 23:53:04 -07004092 // Insert frames to get a new fps estimate...
4093 for (int j = 0; j < kFramerateFps; ++j) {
4094 video_source_.IncomingCapturedFrame(
4095 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004096 if (video_source_.last_sent_width()) {
4097 sink_.WaitForEncodedFrame(timestamp_ms);
4098 }
sprangc5d62e22017-04-02 23:53:04 -07004099 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004100 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004101 }
4102 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004103 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004104 } while (video_source_.sink_wants().max_framerate_fps <
4105 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004106
Jonathan Yubc771b72017-12-08 17:04:29 -08004107 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004108
mflodmancc3d4422017-08-03 08:27:51 -07004109 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004110}
asaperssonf7e294d2017-06-13 23:25:22 -07004111
mflodmancc3d4422017-08-03 08:27:51 -07004112TEST_F(VideoStreamEncoderTest,
4113 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004114 const int kWidth = 1280;
4115 const int kHeight = 720;
4116 const int64_t kFrameIntervalMs = 150;
4117 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004118 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004119 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4120 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004121
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004122 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004123 AdaptingFrameForwarder source;
4124 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004125 video_stream_encoder_->SetSource(&source,
4126 webrtc::DegradationPreference::BALANCED);
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(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004130 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004131 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4132 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4133 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4134
4135 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
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 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4141 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4142 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4143 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4144
4145 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
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 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4151 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4152 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4153 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4154
4155 // Trigger adapt down, expect reduced fps (640x360@15fps).
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 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4162 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4163 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4164
4165 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004166 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004167 timestamp_ms += kFrameIntervalMs;
4168 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004169 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004170 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4171 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4172 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4173 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4174
4175 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004176 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004177 timestamp_ms += kFrameIntervalMs;
4178 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004179 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004180 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4181 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4182 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4183 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4184
4185 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004186 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004187 timestamp_ms += kFrameIntervalMs;
4188 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004189 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004190 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4191 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4192 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4193 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4194
4195 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004196 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004197 timestamp_ms += kFrameIntervalMs;
4198 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004199 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004200 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4201 rtc::VideoSinkWants last_wants = source.sink_wants();
4202 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4203 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4204 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4205
4206 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004207 video_stream_encoder_->TriggerQualityLow();
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 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4212 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4213 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4214 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4215
4216 // Trigger adapt down, expect expect increased fps (320x180@10fps).
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 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4222 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4223 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4224 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4225
4226 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
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 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4232 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4233 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4234 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4235
4236 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004241 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4242 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4243 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4244 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4245
4246 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004247 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004248 timestamp_ms += kFrameIntervalMs;
4249 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004250 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004251 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4252 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4253 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4254 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4255
4256 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004257 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004258 timestamp_ms += kFrameIntervalMs;
4259 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004260 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004261 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4262 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4263 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4264 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4265
4266 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004267 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004268 timestamp_ms += kFrameIntervalMs;
4269 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004270 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004271 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4272 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4273 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4274 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4275
Åsa Persson30ab0152019-08-27 12:22:33 +02004276 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004277 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004278 timestamp_ms += kFrameIntervalMs;
4279 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004280 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004281 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004282 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004283 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4284 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4285 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4286
4287 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004288 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004289 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004290 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4291
mflodmancc3d4422017-08-03 08:27:51 -07004292 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004293}
4294
mflodmancc3d4422017-08-03 08:27:51 -07004295TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004296 const int kWidth = 1280;
4297 const int kHeight = 720;
4298 const int64_t kFrameIntervalMs = 150;
4299 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004300 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004301 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4302 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004303
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004304 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004305 AdaptingFrameForwarder source;
4306 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004307 video_stream_encoder_->SetSource(&source,
4308 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004309 timestamp_ms += kFrameIntervalMs;
4310 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004311 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004312 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004313 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4314 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4315 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4316 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4317 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4318 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4319
4320 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004321 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004322 timestamp_ms += kFrameIntervalMs;
4323 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004324 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004325 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4326 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4327 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4328 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4329 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4330 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4331 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4332
4333 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004334 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004335 timestamp_ms += kFrameIntervalMs;
4336 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004337 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004338 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4339 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4340 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4341 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4342 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4343 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4344 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4345
4346 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004347 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004348 timestamp_ms += kFrameIntervalMs;
4349 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004350 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004351 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4352 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4353 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4354 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4355 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4356 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4357 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4358
4359 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004360 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004361 timestamp_ms += kFrameIntervalMs;
4362 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004363 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004364 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4365 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4366 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4367 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4368 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4369 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4370 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4371
4372 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004373 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004374 timestamp_ms += kFrameIntervalMs;
4375 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004376 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004377 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4378 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4379 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4380 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4381 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4382 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4383 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4384
4385 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004386 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004387 timestamp_ms += kFrameIntervalMs;
4388 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004389 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004390 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004391 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004392 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4393 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4394 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4395 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4396 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4397 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4398
4399 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004400 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004401 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004402 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4403 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4404
mflodmancc3d4422017-08-03 08:27:51 -07004405 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004406}
4407
mflodmancc3d4422017-08-03 08:27:51 -07004408TEST_F(VideoStreamEncoderTest,
4409 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004410 const int kWidth = 640;
4411 const int kHeight = 360;
4412 const int kFpsLimit = 15;
4413 const int64_t kFrameIntervalMs = 150;
4414 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004415 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004416 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4417 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004418
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004419 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004420 AdaptingFrameForwarder source;
4421 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004422 video_stream_encoder_->SetSource(&source,
4423 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004424 timestamp_ms += kFrameIntervalMs;
4425 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004426 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004427 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004428 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4429 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4430 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4431 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4432 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4433 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4434
4435 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004436 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004437 timestamp_ms += kFrameIntervalMs;
4438 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004439 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004440 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4441 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4442 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4443 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4444 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4445 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4446 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4447
4448 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004449 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004450 timestamp_ms += kFrameIntervalMs;
4451 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004452 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004453 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4454 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4455 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4456 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4457 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4458 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4459 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4460
4461 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004462 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004463 timestamp_ms += kFrameIntervalMs;
4464 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004465 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004466 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4467 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4468 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4469 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4470 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4471 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4472 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4473
4474 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004475 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004476 timestamp_ms += kFrameIntervalMs;
4477 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004478 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004479 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004480 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4481 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4482 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4483 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4484 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4485 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4486
4487 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004488 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004489 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004490 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4491 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4492
mflodmancc3d4422017-08-03 08:27:51 -07004493 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004494}
4495
mflodmancc3d4422017-08-03 08:27:51 -07004496TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004497 const int kFrameWidth = 1920;
4498 const int kFrameHeight = 1080;
4499 // 3/4 of 1920.
4500 const int kAdaptedFrameWidth = 1440;
4501 // 3/4 of 1080 rounded down to multiple of 4.
4502 const int kAdaptedFrameHeight = 808;
4503 const int kFramerate = 24;
4504
Erik Språng4c6ca302019-04-08 15:14:01 +02004505 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004506 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4507 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004508 // Trigger reconfigure encoder (without resetting the entire instance).
4509 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004510 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004511 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4512 video_encoder_config.number_of_streams = 1;
4513 video_encoder_config.video_stream_factory =
4514 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004515 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004516 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004517 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004518
4519 video_source_.set_adaptation_enabled(true);
4520
4521 video_source_.IncomingCapturedFrame(
4522 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004523 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004524
4525 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004526 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004527 video_source_.IncomingCapturedFrame(
4528 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004529 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004530
mflodmancc3d4422017-08-03 08:27:51 -07004531 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004532}
4533
mflodmancc3d4422017-08-03 08:27:51 -07004534TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004535 const int kFrameWidth = 1280;
4536 const int kFrameHeight = 720;
4537 const int kLowFps = 2;
4538 const int kHighFps = 30;
4539
Erik Språng4c6ca302019-04-08 15:14:01 +02004540 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004541 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4542 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004543
4544 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4545 max_framerate_ = kLowFps;
4546
4547 // Insert 2 seconds of 2fps video.
4548 for (int i = 0; i < kLowFps * 2; ++i) {
4549 video_source_.IncomingCapturedFrame(
4550 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4551 WaitForEncodedFrame(timestamp_ms);
4552 timestamp_ms += 1000 / kLowFps;
4553 }
4554
4555 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004556 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004557 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4558 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004559 video_source_.IncomingCapturedFrame(
4560 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4561 WaitForEncodedFrame(timestamp_ms);
4562 timestamp_ms += 1000 / kLowFps;
4563
4564 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4565
4566 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004567 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004568 const int kFrameIntervalMs = 1000 / kHighFps;
4569 max_framerate_ = kHighFps;
4570 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4571 video_source_.IncomingCapturedFrame(
4572 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4573 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4574 // be dropped if the encoder hans't been updated with the new higher target
4575 // framerate yet, causing it to overshoot the target bitrate and then
4576 // suffering the wrath of the media optimizer.
4577 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4578 timestamp_ms += kFrameIntervalMs;
4579 }
4580
4581 // Don expect correct measurement just yet, but it should be higher than
4582 // before.
4583 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4584
mflodmancc3d4422017-08-03 08:27:51 -07004585 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004586}
4587
mflodmancc3d4422017-08-03 08:27:51 -07004588TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004589 const int kFrameWidth = 1280;
4590 const int kFrameHeight = 720;
4591 const int kTargetBitrateBps = 1000000;
4592
4593 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004594 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004595 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004596 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4597 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004598 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004599
4600 // Insert a first video frame, causes another bitrate update.
4601 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4602 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4603 video_source_.IncomingCapturedFrame(
4604 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4605 WaitForEncodedFrame(timestamp_ms);
4606
4607 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004608 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4609 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004610
4611 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004612 timestamp_ms += kProcessIntervalMs;
4613 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004614
4615 // Bitrate observer should not be called.
4616 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4617 video_source_.IncomingCapturedFrame(
4618 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4619 ExpectDroppedFrame();
4620
mflodmancc3d4422017-08-03 08:27:51 -07004621 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004622}
ilnik6b826ef2017-06-16 06:53:48 -07004623
Niels Möller4db138e2018-04-19 09:04:13 +02004624TEST_F(VideoStreamEncoderTest,
4625 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4626 const int kFrameWidth = 1280;
4627 const int kFrameHeight = 720;
4628 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004629 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004630 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4631 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004632 video_source_.IncomingCapturedFrame(
4633 CreateFrame(1, kFrameWidth, kFrameHeight));
4634 WaitForEncodedFrame(1);
4635 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4636 .low_encode_usage_threshold_percent,
4637 default_options.low_encode_usage_threshold_percent);
4638 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4639 .high_encode_usage_threshold_percent,
4640 default_options.high_encode_usage_threshold_percent);
4641 video_stream_encoder_->Stop();
4642}
4643
4644TEST_F(VideoStreamEncoderTest,
4645 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4646 const int kFrameWidth = 1280;
4647 const int kFrameHeight = 720;
4648 CpuOveruseOptions hardware_options;
4649 hardware_options.low_encode_usage_threshold_percent = 150;
4650 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004651 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004652
Erik Språng4c6ca302019-04-08 15:14:01 +02004653 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004654 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4655 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004656 video_source_.IncomingCapturedFrame(
4657 CreateFrame(1, kFrameWidth, kFrameHeight));
4658 WaitForEncodedFrame(1);
4659 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4660 .low_encode_usage_threshold_percent,
4661 hardware_options.low_encode_usage_threshold_percent);
4662 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4663 .high_encode_usage_threshold_percent,
4664 hardware_options.high_encode_usage_threshold_percent);
4665 video_stream_encoder_->Stop();
4666}
4667
Niels Möller6bb5ab92019-01-11 11:11:10 +01004668TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4669 const int kFrameWidth = 320;
4670 const int kFrameHeight = 240;
4671 const int kFps = 30;
4672 const int kTargetBitrateBps = 120000;
4673 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4674
Erik Språng4c6ca302019-04-08 15:14:01 +02004675 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004676 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4677 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004678
4679 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4680 max_framerate_ = kFps;
4681
4682 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4683 fake_encoder_.SimulateOvershoot(1.0);
4684 int num_dropped = 0;
4685 for (int i = 0; i < kNumFramesInRun; ++i) {
4686 video_source_.IncomingCapturedFrame(
4687 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4688 // Wait up to two frame durations for a frame to arrive.
4689 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4690 ++num_dropped;
4691 }
4692 timestamp_ms += 1000 / kFps;
4693 }
4694
Erik Språnga8d48ab2019-02-08 14:17:40 +01004695 // Framerate should be measured to be near the expected target rate.
4696 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4697
4698 // Frame drops should be within 5% of expected 0%.
4699 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004700
4701 // Make encoder produce frames at double the expected bitrate during 3 seconds
4702 // of video, verify number of drops. Rate needs to be slightly changed in
4703 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004704 double overshoot_factor = 2.0;
4705 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4706 // With bitrate adjuster, when need to overshoot even more to trigger
4707 // frame dropping.
4708 overshoot_factor *= 2;
4709 }
4710 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004711 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004712 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004713 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004714 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004715 num_dropped = 0;
4716 for (int i = 0; i < kNumFramesInRun; ++i) {
4717 video_source_.IncomingCapturedFrame(
4718 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4719 // Wait up to two frame durations for a frame to arrive.
4720 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4721 ++num_dropped;
4722 }
4723 timestamp_ms += 1000 / kFps;
4724 }
4725
Erik Språng4c6ca302019-04-08 15:14:01 +02004726 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004727 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4728 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004729
4730 // Target framerate should be still be near the expected target, despite
4731 // the frame drops.
4732 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4733
4734 // Frame drops should be within 5% of expected 50%.
4735 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004736
4737 video_stream_encoder_->Stop();
4738}
4739
4740TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4741 const int kFrameWidth = 320;
4742 const int kFrameHeight = 240;
4743 const int kActualInputFps = 24;
4744 const int kTargetBitrateBps = 120000;
4745
4746 ASSERT_GT(max_framerate_, kActualInputFps);
4747
4748 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4749 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004750 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004751 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4752 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004753
4754 // Insert 3 seconds of video, with an input fps lower than configured max.
4755 for (int i = 0; i < kActualInputFps * 3; ++i) {
4756 video_source_.IncomingCapturedFrame(
4757 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4758 // Wait up to two frame durations for a frame to arrive.
4759 WaitForEncodedFrame(timestamp_ms);
4760 timestamp_ms += 1000 / kActualInputFps;
4761 }
4762
4763 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4764
4765 video_stream_encoder_->Stop();
4766}
4767
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004768TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4769 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004770 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004771 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4772 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004773
4774 fake_encoder_.BlockNextEncode();
4775 video_source_.IncomingCapturedFrame(
4776 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4777 WaitForEncodedFrame(1);
4778 // On the very first frame full update should be forced.
4779 rect = fake_encoder_.GetLastUpdateRect();
4780 EXPECT_EQ(rect.offset_x, 0);
4781 EXPECT_EQ(rect.offset_y, 0);
4782 EXPECT_EQ(rect.height, codec_height_);
4783 EXPECT_EQ(rect.width, codec_width_);
4784 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4785 // call to ContinueEncode.
4786 video_source_.IncomingCapturedFrame(
4787 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4788 ExpectDroppedFrame();
4789 video_source_.IncomingCapturedFrame(
4790 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4791 ExpectDroppedFrame();
4792 fake_encoder_.ContinueEncode();
4793 WaitForEncodedFrame(3);
4794 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4795 rect = fake_encoder_.GetLastUpdateRect();
4796 EXPECT_EQ(rect.offset_x, 1);
4797 EXPECT_EQ(rect.offset_y, 0);
4798 EXPECT_EQ(rect.width, 10);
4799 EXPECT_EQ(rect.height, 1);
4800
4801 video_source_.IncomingCapturedFrame(
4802 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4803 WaitForEncodedFrame(4);
4804 // Previous frame was encoded, so no accumulation should happen.
4805 rect = fake_encoder_.GetLastUpdateRect();
4806 EXPECT_EQ(rect.offset_x, 0);
4807 EXPECT_EQ(rect.offset_y, 0);
4808 EXPECT_EQ(rect.width, 1);
4809 EXPECT_EQ(rect.height, 1);
4810
4811 video_stream_encoder_->Stop();
4812}
4813
Erik Språngd7329ca2019-02-21 21:19:53 +01004814TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004815 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004816 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4817 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004818
4819 // First frame is always keyframe.
4820 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4821 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004822 EXPECT_THAT(
4823 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004824 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004825
4826 // Insert delta frame.
4827 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4828 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004829 EXPECT_THAT(
4830 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004831 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004832
4833 // Request next frame be a key-frame.
4834 video_stream_encoder_->SendKeyFrame();
4835 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4836 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004837 EXPECT_THAT(
4838 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004839 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004840
4841 video_stream_encoder_->Stop();
4842}
4843
4844TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4845 // Setup simulcast with three streams.
4846 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004847 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004848 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004849 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004850 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004851 // Wait for all three layers before triggering event.
4852 sink_.SetNumExpectedLayers(3);
4853
4854 // First frame is always keyframe.
4855 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4856 WaitForEncodedFrame(1);
4857 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004858 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4859 VideoFrameType::kVideoFrameKey,
4860 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004861
4862 // Insert delta frame.
4863 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4864 WaitForEncodedFrame(2);
4865 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004866 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4867 VideoFrameType::kVideoFrameDelta,
4868 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004869
4870 // Request next frame be a key-frame.
4871 // Only first stream is configured to produce key-frame.
4872 video_stream_encoder_->SendKeyFrame();
4873 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4874 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004875
4876 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4877 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004878 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004879 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004880 VideoFrameType::kVideoFrameKey,
4881 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004882
4883 video_stream_encoder_->Stop();
4884}
4885
4886TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4887 // Configure internal source factory and setup test again.
4888 encoder_factory_.SetHasInternalSource(true);
4889 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004890 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004891 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4892 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004893
4894 // Call encoder directly, simulating internal source where encoded frame
4895 // callback in VideoStreamEncoder is called despite no OnFrame().
4896 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4897 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004898 EXPECT_THAT(
4899 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004900 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004901
Niels Möller8f7ce222019-03-21 15:43:58 +01004902 const std::vector<VideoFrameType> kDeltaFrame = {
4903 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004904 // Need to set timestamp manually since manually for injected frame.
4905 VideoFrame frame = CreateFrame(101, nullptr);
4906 frame.set_timestamp(101);
4907 fake_encoder_.InjectFrame(frame, false);
4908 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004909 EXPECT_THAT(
4910 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004911 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004912
4913 // Request key-frame. The forces a dummy frame down into the encoder.
4914 fake_encoder_.ExpectNullFrame();
4915 video_stream_encoder_->SendKeyFrame();
4916 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004917 EXPECT_THAT(
4918 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004919 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004920
4921 video_stream_encoder_->Stop();
4922}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004923
4924TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4925 // Configure internal source factory and setup test again.
4926 encoder_factory_.SetHasInternalSource(true);
4927 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004928 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004929 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4930 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004931
4932 int64_t timestamp = 1;
4933 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004934 image.SetEncodedData(
4935 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004936 image.capture_time_ms_ = ++timestamp;
4937 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4938 const int64_t kEncodeFinishDelayMs = 10;
4939 image.timing_.encode_start_ms = timestamp;
4940 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4941 fake_encoder_.InjectEncodedImage(image);
4942 // Wait for frame without incrementing clock.
4943 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4944 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4945 // capture timestamp should be kEncodeFinishDelayMs in the past.
4946 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4947 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4948 kEncodeFinishDelayMs);
4949
4950 video_stream_encoder_->Stop();
4951}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004952
4953TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4954 // Configure internal source factory and setup test again.
4955 encoder_factory_.SetHasInternalSource(true);
4956 ResetEncoder("H264", 1, 1, 1, false);
4957
4958 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4959 image._frameType = VideoFrameType::kVideoFrameKey;
4960
4961 CodecSpecificInfo codec_specific_info;
4962 codec_specific_info.codecType = kVideoCodecH264;
4963
4964 RTPFragmentationHeader fragmentation;
4965 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4966 fragmentation.fragmentationOffset[0] = 4;
4967 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4968
4969 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4970 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4971
4972 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4973 testing::ElementsAreArray(optimal_sps));
4974 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4975 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4976 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4977 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4978
4979 video_stream_encoder_->Stop();
4980}
4981
4982TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4983 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4984 0x00, 0x00, 0x03, 0x03, 0xF4,
4985 0x05, 0x03, 0xC7, 0xC0};
4986
4987 // Configure internal source factory and setup test again.
4988 encoder_factory_.SetHasInternalSource(true);
4989 ResetEncoder("H264", 1, 1, 1, false);
4990
4991 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4992 image._frameType = VideoFrameType::kVideoFrameKey;
4993
4994 CodecSpecificInfo codec_specific_info;
4995 codec_specific_info.codecType = kVideoCodecH264;
4996
4997 RTPFragmentationHeader fragmentation;
4998 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4999 fragmentation.fragmentationOffset[0] = 4;
5000 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
5001
5002 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5003 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5004
5005 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5006 testing::ElementsAreArray(optimal_sps));
5007 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5008 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5009 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5010 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5011
5012 video_stream_encoder_->Stop();
5013}
5014
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005015TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5016 const int kFrameWidth = 1280;
5017 const int kFrameHeight = 720;
5018 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5019
5020 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02005021 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5022 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005023 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5024
5025 // Insert a first video frame. It should be dropped because of downscale in
5026 // resolution.
5027 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5028 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5029 frame.set_rotation(kVideoRotation_270);
5030 video_source_.IncomingCapturedFrame(frame);
5031
5032 ExpectDroppedFrame();
5033
5034 // Second frame is downscaled.
5035 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5036 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5037 frame.set_rotation(kVideoRotation_90);
5038 video_source_.IncomingCapturedFrame(frame);
5039
5040 WaitForEncodedFrame(timestamp_ms);
5041 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5042
5043 // Insert another frame, also downscaled.
5044 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5045 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5046 frame.set_rotation(kVideoRotation_180);
5047 video_source_.IncomingCapturedFrame(frame);
5048
5049 WaitForEncodedFrame(timestamp_ms);
5050 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5051
5052 video_stream_encoder_->Stop();
5053}
5054
Erik Språng5056af02019-09-02 15:53:11 +02005055TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5056 const int kFrameWidth = 320;
5057 const int kFrameHeight = 180;
5058
5059 // Initial rate.
5060 video_stream_encoder_->OnBitrateUpdated(
5061 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005062 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005063 /*link_allocation=*/DataRate::kbps(300),
5064 /*fraction_lost=*/0,
5065 /*rtt_ms=*/0);
5066
5067 // Insert a first video frame so that encoder gets configured.
5068 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5069 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5070 frame.set_rotation(kVideoRotation_270);
5071 video_source_.IncomingCapturedFrame(frame);
5072 WaitForEncodedFrame(timestamp_ms);
5073
5074 // Set a target rate below the minimum allowed by the codec settings.
5075 VideoCodec codec_config = fake_encoder_.codec_config();
5076 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5077 DataRate target_rate = min_rate - DataRate::kbps(1);
5078 video_stream_encoder_->OnBitrateUpdated(
5079 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005080 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005081 /*link_allocation=*/target_rate,
5082 /*fraction_lost=*/0,
5083 /*rtt_ms=*/0);
5084 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5085
5086 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5087 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5088 ASSERT_TRUE(rate_settings.has_value());
5089 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5090 EXPECT_EQ(min_rate, allocation_sum);
5091 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5092
5093 video_stream_encoder_->Stop();
5094}
5095
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005096TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005097 video_stream_encoder_->OnBitrateUpdated(
5098 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5099 DataRate::bps(kTargetBitrateBps), 0, 0);
5100 // Capture a frame and wait for it to synchronize with the encoder thread.
5101 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5102 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5103 WaitForEncodedFrame(1);
5104
5105 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5106 ASSERT_TRUE(prev_rate_settings.has_value());
5107 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5108 kDefaultFramerate);
5109
5110 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5111 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5112 timestamp_ms += 1000 / kDefaultFramerate;
5113 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5114 WaitForEncodedFrame(timestamp_ms);
5115 }
5116 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5117 kDefaultFramerate);
5118 // Capture larger frame to trigger a reconfigure.
5119 codec_height_ *= 2;
5120 codec_width_ *= 2;
5121 timestamp_ms += 1000 / kDefaultFramerate;
5122 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5123 WaitForEncodedFrame(timestamp_ms);
5124
5125 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5126 auto current_rate_settings =
5127 fake_encoder_.GetAndResetLastRateControlSettings();
5128 // Ensure we have actually reconfigured twice
5129 // The rate settings should have been set again even though
5130 // they haven't changed.
5131 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005132 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005133
5134 video_stream_encoder_->Stop();
5135}
5136
philipeld9cc8c02019-09-16 14:53:40 +02005137struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5138 MOCK_METHOD0(RequestEncoderFallback, void());
5139 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5140};
5141
5142TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5143 constexpr int kDontCare = 100;
5144
5145 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5146 video_send_config_.encoder_settings.encoder_switch_request_callback =
5147 &switch_callback;
5148 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5149 encoder_config.codec_type = kVideoCodecVP8;
5150 webrtc::test::ScopedFieldTrials field_trial(
5151 "WebRTC-NetworkCondition-EncoderSwitch/"
5152 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5153 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5154
5155 // Reset encoder for new configuration to take effect.
5156 ConfigureEncoder(std::move(encoder_config));
5157
5158 // Send one frame to trigger ReconfigureEncoder.
5159 video_source_.IncomingCapturedFrame(
5160 CreateFrame(kDontCare, kDontCare, kDontCare));
5161
5162 using Config = EncoderSwitchRequestCallback::Config;
5163 EXPECT_CALL(switch_callback,
5164 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5165 Field(&Config::param, "ping"),
5166 Field(&Config::value, "pong"))));
5167
5168 video_stream_encoder_->OnBitrateUpdated(
5169 /*target_bitrate=*/DataRate::kbps(50),
5170 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5171 /*link_allocation=*/DataRate::kbps(kDontCare),
5172 /*fraction_lost=*/0,
5173 /*rtt_ms=*/0);
5174
5175 video_stream_encoder_->Stop();
5176}
5177
5178TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5179 constexpr int kSufficientBitrateToNotDrop = 1000;
5180 constexpr int kHighRes = 500;
5181 constexpr int kLowRes = 100;
5182
5183 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5184 video_send_config_.encoder_settings.encoder_switch_request_callback =
5185 &switch_callback;
5186 webrtc::test::ScopedFieldTrials field_trial(
5187 "WebRTC-NetworkCondition-EncoderSwitch/"
5188 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5189 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5190 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5191 encoder_config.codec_type = kVideoCodecH264;
5192
5193 // Reset encoder for new configuration to take effect.
5194 ConfigureEncoder(std::move(encoder_config));
5195
5196 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5197 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5198 // not fail.
5199 video_stream_encoder_->OnBitrateUpdated(
5200 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5201 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5202 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5203 /*fraction_lost=*/0,
5204 /*rtt_ms=*/0);
5205
5206 // Send one frame to trigger ReconfigureEncoder.
5207 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5208 WaitForEncodedFrame(1);
5209
5210 using Config = EncoderSwitchRequestCallback::Config;
5211 EXPECT_CALL(switch_callback,
5212 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5213 Field(&Config::param, "ping"),
5214 Field(&Config::value, "pong"))));
5215
5216 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5217 WaitForEncodedFrame(2);
5218
5219 video_stream_encoder_->Stop();
5220}
5221
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005222TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005223 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005224 const int kFrameWidth = 320;
5225 const int kFrameHeight = 180;
5226
5227 // Set initial rate.
5228 auto rate = DataRate::kbps(100);
5229 video_stream_encoder_->OnBitrateUpdated(
5230 /*target_bitrate=*/rate,
5231 /*stable_target_bitrate=*/rate,
5232 /*link_allocation=*/rate,
5233 /*fraction_lost=*/0,
5234 /*rtt_ms=*/0);
5235
5236 // Insert a first video frame so that encoder gets configured.
5237 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5238 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5239 frame.set_rotation(kVideoRotation_270);
5240 video_source_.IncomingCapturedFrame(frame);
5241 WaitForEncodedFrame(timestamp_ms);
5242 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5243
5244 // Change of target bitrate propagates to the encoder.
5245 auto new_stable_rate = rate - DataRate::kbps(5);
5246 video_stream_encoder_->OnBitrateUpdated(
5247 /*target_bitrate=*/new_stable_rate,
5248 /*stable_target_bitrate=*/new_stable_rate,
5249 /*link_allocation=*/rate,
5250 /*fraction_lost=*/0,
5251 /*rtt_ms=*/0);
5252 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5253 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5254 video_stream_encoder_->Stop();
5255}
5256
5257TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005258 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005259 const int kFrameWidth = 320;
5260 const int kFrameHeight = 180;
5261
5262 // Set initial rate.
5263 auto rate = DataRate::kbps(100);
5264 video_stream_encoder_->OnBitrateUpdated(
5265 /*target_bitrate=*/rate,
5266 /*stable_target_bitrate=*/rate,
5267 /*link_allocation=*/rate,
5268 /*fraction_lost=*/0,
5269 /*rtt_ms=*/0);
5270
5271 // Insert a first video frame so that encoder gets configured.
5272 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5273 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5274 frame.set_rotation(kVideoRotation_270);
5275 video_source_.IncomingCapturedFrame(frame);
5276 WaitForEncodedFrame(timestamp_ms);
5277 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5278
5279 // Set a higher target rate without changing the link_allocation. Should not
5280 // reset encoder's rate.
5281 auto new_stable_rate = rate - DataRate::kbps(5);
5282 video_stream_encoder_->OnBitrateUpdated(
5283 /*target_bitrate=*/rate,
5284 /*stable_target_bitrate=*/new_stable_rate,
5285 /*link_allocation=*/rate,
5286 /*fraction_lost=*/0,
5287 /*rtt_ms=*/0);
5288 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5289 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5290 video_stream_encoder_->Stop();
5291}
5292
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005293TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5294 test::ScopedFieldTrials field_trials(
5295 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5296 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5297 const int kFramerateFps = 30;
5298 const int kWidth = 1920;
5299 const int kHeight = 1080;
5300 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5301 // Works on screenshare mode.
5302 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5303 // We rely on the automatic resolution adaptation, but we handle framerate
5304 // adaptation manually by mocking the stats proxy.
5305 video_source_.set_adaptation_enabled(true);
5306
5307 // BALANCED degradation preference is required for this feature.
5308 video_stream_encoder_->OnBitrateUpdated(
5309 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5310 DataRate::bps(kTargetBitrateBps), 0, 0);
5311 video_stream_encoder_->SetSource(&video_source_,
5312 webrtc::DegradationPreference::BALANCED);
5313 VerifyNoLimitation(video_source_.sink_wants());
5314
5315 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5316 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5317
5318 // Pass enough frames with the full update to trigger animation detection.
5319 for (int i = 0; i < kNumFrames; ++i) {
5320 int64_t timestamp_ms =
5321 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5322 frame.set_ntp_time_ms(timestamp_ms);
5323 frame.set_timestamp_us(timestamp_ms * 1000);
5324 video_source_.IncomingCapturedFrame(frame);
5325 WaitForEncodedFrame(timestamp_ms);
5326 }
5327
5328 // Resolution should be limited.
5329 rtc::VideoSinkWants expected;
5330 expected.max_framerate_fps = kFramerateFps;
5331 expected.max_pixel_count = 1280 * 720 + 1;
5332 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5333
5334 // Pass one frame with no known update.
5335 // Resolution cap should be removed immediately.
5336 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5337 frame.set_ntp_time_ms(timestamp_ms);
5338 frame.set_timestamp_us(timestamp_ms * 1000);
5339 frame.clear_update_rect();
5340
5341 video_source_.IncomingCapturedFrame(frame);
5342 WaitForEncodedFrame(timestamp_ms);
5343
5344 // Resolution should be unlimited now.
5345 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5346
5347 video_stream_encoder_->Stop();
5348}
5349
perkj26091b12016-09-01 01:17:40 -07005350} // namespace webrtc