blob: 4b4cd2d67fcbb080a62031242e88559181890a84 [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);
perkj803d97f2016-11-01 11:45:46 -07001808
sprangc5d62e22017-04-02 23:53:04 -07001809 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001810 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001811
sprangc5d62e22017-04-02 23:53:04 -07001812 // Force an input frame rate to be available, or the adaptation call won't
1813 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001814 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001815 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001816 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001817 stats_proxy_->SetMockStats(stats);
1818
mflodmancc3d4422017-08-03 08:27:51 -07001819 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001820 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001821 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001822 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001823 frame_timestamp += kFrameIntervalMs;
1824
1825 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001826 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001827 EXPECT_EQ(std::numeric_limits<int>::max(),
1828 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001829 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001830
asapersson02465b82017-04-10 01:12:52 -07001831 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001832 video_stream_encoder_->SetSource(&new_video_source,
1833 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001834 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001835
mflodmancc3d4422017-08-03 08:27:51 -07001836 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001837 new_video_source.IncomingCapturedFrame(
1838 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001839 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001840 frame_timestamp += kFrameIntervalMs;
1841
1842 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001843 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001844
1845 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001846 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001847 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001848 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1849 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001850 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001851 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001852
1853 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001854 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001855 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001856 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1857 EXPECT_EQ(std::numeric_limits<int>::max(),
1858 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001859 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001860
mflodmancc3d4422017-08-03 08:27:51 -07001861 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001862}
1863
mflodmancc3d4422017-08-03 08:27:51 -07001864TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001865 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001866 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1867 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001868
asaperssonfab67072017-04-04 05:51:49 -07001869 const int kWidth = 1280;
1870 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001871 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001872 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001873 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1874 EXPECT_FALSE(stats.bw_limited_resolution);
1875 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1876
1877 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001878 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001879 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001880 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001881
1882 stats = stats_proxy_->GetStats();
1883 EXPECT_TRUE(stats.bw_limited_resolution);
1884 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1885
1886 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001887 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001888 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001889 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001890
1891 stats = stats_proxy_->GetStats();
1892 EXPECT_FALSE(stats.bw_limited_resolution);
1893 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1894 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1895
mflodmancc3d4422017-08-03 08:27:51 -07001896 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001897}
1898
mflodmancc3d4422017-08-03 08:27:51 -07001899TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001900 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001901 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1902 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001903
1904 const int kWidth = 1280;
1905 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001906 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001907 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001908 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1909 EXPECT_FALSE(stats.cpu_limited_resolution);
1910 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1911
1912 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001913 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001914 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001915 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001916
1917 stats = stats_proxy_->GetStats();
1918 EXPECT_TRUE(stats.cpu_limited_resolution);
1919 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1920
1921 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001922 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001923 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001924 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001925
1926 stats = stats_proxy_->GetStats();
1927 EXPECT_FALSE(stats.cpu_limited_resolution);
1928 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001929 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001930
mflodmancc3d4422017-08-03 08:27:51 -07001931 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001932}
1933
mflodmancc3d4422017-08-03 08:27:51 -07001934TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001935 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001936 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1937 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001938
asaperssonfab67072017-04-04 05:51:49 -07001939 const int kWidth = 1280;
1940 const int kHeight = 720;
1941 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001942 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001943 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001944 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001945 EXPECT_FALSE(stats.cpu_limited_resolution);
1946 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1947
asaperssonfab67072017-04-04 05:51:49 -07001948 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001949 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001950 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001951 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001952 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001953 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001954 EXPECT_TRUE(stats.cpu_limited_resolution);
1955 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1956
1957 // Set new source with adaptation still enabled.
1958 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001959 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001960 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001961
asaperssonfab67072017-04-04 05:51:49 -07001962 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001963 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001964 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001965 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001966 EXPECT_TRUE(stats.cpu_limited_resolution);
1967 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1968
1969 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001970 video_stream_encoder_->SetSource(&new_video_source,
1971 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001972
asaperssonfab67072017-04-04 05:51:49 -07001973 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001974 WaitForEncodedFrame(4);
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_FALSE(stats.cpu_limited_resolution);
1978 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1979
1980 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001981 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001982 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001983
asaperssonfab67072017-04-04 05:51:49 -07001984 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001985 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001986 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001987 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001988 EXPECT_TRUE(stats.cpu_limited_resolution);
1989 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1990
asaperssonfab67072017-04-04 05:51:49 -07001991 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001992 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001993 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001994 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001995 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001996 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001997 EXPECT_FALSE(stats.cpu_limited_resolution);
1998 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001999 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002000
mflodmancc3d4422017-08-03 08:27:51 -07002001 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002002}
2003
mflodmancc3d4422017-08-03 08:27:51 -07002004TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002005 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002006 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2007 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002008
asaperssonfab67072017-04-04 05:51:49 -07002009 const int kWidth = 1280;
2010 const int kHeight = 720;
2011 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002012 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002013 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002014 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002015 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002016 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002017
2018 // Set new source with adaptation still enabled.
2019 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002020 video_stream_encoder_->SetSource(&new_video_source,
2021 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002022
asaperssonfab67072017-04-04 05:51:49 -07002023 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002024 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002025 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002026 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002027 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002028 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002029
asaperssonfab67072017-04-04 05:51:49 -07002030 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002031 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002032 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002033 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002034 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002035 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002036 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002037 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002038
asaperssonfab67072017-04-04 05:51:49 -07002039 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002040 video_stream_encoder_->SetSource(&new_video_source,
2041 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002042
asaperssonfab67072017-04-04 05:51:49 -07002043 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002044 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002045 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002046 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002047 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002048 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002049
asapersson02465b82017-04-10 01:12:52 -07002050 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002051 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002052 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002053
asaperssonfab67072017-04-04 05:51:49 -07002054 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002055 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002056 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002057 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002058 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002059 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2060 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002061
mflodmancc3d4422017-08-03 08:27:51 -07002062 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002063}
2064
mflodmancc3d4422017-08-03 08:27:51 -07002065TEST_F(VideoStreamEncoderTest,
2066 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002067 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002068 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2069 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002070
2071 const int kWidth = 1280;
2072 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002073 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002074 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002075 video_source_.IncomingCapturedFrame(
2076 CreateFrame(timestamp_ms, kWidth, kHeight));
2077 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002078 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2079 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2080 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2081
2082 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002083 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002084 timestamp_ms += kFrameIntervalMs;
2085 video_source_.IncomingCapturedFrame(
2086 CreateFrame(timestamp_ms, kWidth, kHeight));
2087 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002088 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2089 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2090 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2091
2092 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002093 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002094 timestamp_ms += kFrameIntervalMs;
2095 video_source_.IncomingCapturedFrame(
2096 CreateFrame(timestamp_ms, kWidth, kHeight));
2097 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002098 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2099 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2100 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2101
Niels Möller4db138e2018-04-19 09:04:13 +02002102 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002103 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002104
2105 VideoEncoderConfig video_encoder_config;
2106 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2107 // Make format different, to force recreation of encoder.
2108 video_encoder_config.video_format.parameters["foo"] = "foo";
2109 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002110 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002111 timestamp_ms += kFrameIntervalMs;
2112 video_source_.IncomingCapturedFrame(
2113 CreateFrame(timestamp_ms, kWidth, kHeight));
2114 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002115 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2116 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2117 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2118
mflodmancc3d4422017-08-03 08:27:51 -07002119 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002120}
2121
mflodmancc3d4422017-08-03 08:27:51 -07002122TEST_F(VideoStreamEncoderTest,
2123 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002124 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002125 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2126 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002127
asapersson0944a802017-04-07 00:57:58 -07002128 const int kWidth = 1280;
2129 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002130 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002131
asaperssonfab67072017-04-04 05:51:49 -07002132 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002133 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002134 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002135 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002136 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002137 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2138
asapersson02465b82017-04-10 01:12:52 -07002139 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002140 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002141 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002142 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002143 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002144 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002145 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002146 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2147
2148 // Set new source with adaptation still enabled.
2149 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002150 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002151 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002152
2153 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002154 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002155 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002156 stats = stats_proxy_->GetStats();
2157 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002158 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002159 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2160
sprangc5d62e22017-04-02 23:53:04 -07002161 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002162 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002163 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002164 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002165 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002166 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002167 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002168 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002169 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002170 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002171 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2172
sprangc5d62e22017-04-02 23:53:04 -07002173 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002174 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002175 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2176 mock_stats.input_frame_rate = 30;
2177 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002178 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002179 stats_proxy_->ResetMockStats();
2180
2181 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002182 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002183 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002184
2185 // Framerate now adapted.
2186 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002187 EXPECT_FALSE(stats.cpu_limited_resolution);
2188 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002189 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2190
2191 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002192 video_stream_encoder_->SetSource(&new_video_source,
2193 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002194 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002195 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002196 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002197
2198 stats = stats_proxy_->GetStats();
2199 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002200 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002201 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2202
2203 // Try to trigger overuse. Should not succeed.
2204 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002205 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002206 stats_proxy_->ResetMockStats();
2207
2208 stats = stats_proxy_->GetStats();
2209 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002210 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002211 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2212
2213 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002214 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002215 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002216 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002217 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002218 stats = stats_proxy_->GetStats();
2219 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002220 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002221 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002222
2223 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002224 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002225 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002226 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002227 stats = stats_proxy_->GetStats();
2228 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002229 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002230 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2231
2232 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002233 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002234 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002235 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002236 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002237 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002238 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002239 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002240 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002241 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002242 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2243
2244 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002245 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002246 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002247 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002248 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002249 stats = stats_proxy_->GetStats();
2250 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002251 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002252 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002253 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002254
mflodmancc3d4422017-08-03 08:27:51 -07002255 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002256}
2257
mflodmancc3d4422017-08-03 08:27:51 -07002258TEST_F(VideoStreamEncoderTest,
2259 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002260 const int kWidth = 1280;
2261 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002262 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002263 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2264 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002265
asaperssonfab67072017-04-04 05:51:49 -07002266 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002267 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002268
asaperssonfab67072017-04-04 05:51:49 -07002269 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002270 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002271
asaperssonfab67072017-04-04 05:51:49 -07002272 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002273 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002274
asaperssonfab67072017-04-04 05:51:49 -07002275 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002276 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002277
kthelgason876222f2016-11-29 01:44:11 -08002278 // Expect a scale down.
2279 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002280 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002281
asapersson02465b82017-04-10 01:12:52 -07002282 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002283 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002284 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002285 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002286
asaperssonfab67072017-04-04 05:51:49 -07002287 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002288 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002289 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002290 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002291
asaperssonfab67072017-04-04 05:51:49 -07002292 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002293 EXPECT_EQ(std::numeric_limits<int>::max(),
2294 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002295
asaperssonfab67072017-04-04 05:51:49 -07002296 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002297 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002298 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002299 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002300
asapersson02465b82017-04-10 01:12:52 -07002301 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002302 EXPECT_EQ(std::numeric_limits<int>::max(),
2303 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002304
mflodmancc3d4422017-08-03 08:27:51 -07002305 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002306}
2307
mflodmancc3d4422017-08-03 08:27:51 -07002308TEST_F(VideoStreamEncoderTest,
2309 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002310 const int kWidth = 1280;
2311 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002312 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002313 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2314 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002315
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002316 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002317 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002318 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002319 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002320
2321 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002322 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002323 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002324 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2325 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2326
2327 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002328 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002329 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002330 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2331 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2332 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2333
2334 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002335 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002336 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2337 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2338 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2339
mflodmancc3d4422017-08-03 08:27:51 -07002340 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002341}
2342
mflodmancc3d4422017-08-03 08:27:51 -07002343TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002344 const int kWidth = 1280;
2345 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002346 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002347 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2348 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002349
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002350 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002351 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002352 video_stream_encoder_->SetSource(&source,
2353 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002354 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2355 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002356 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002357
2358 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002359 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002360 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2361 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2362 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2363 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2364
2365 // Trigger adapt down for same input resolution, expect no change.
2366 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2367 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002368 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002369 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2370 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2371 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2372
2373 // Trigger adapt down for larger input resolution, expect no change.
2374 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2375 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002376 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002377 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2378 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2379 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2380
mflodmancc3d4422017-08-03 08:27:51 -07002381 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002382}
2383
mflodmancc3d4422017-08-03 08:27:51 -07002384TEST_F(VideoStreamEncoderTest,
2385 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002386 const int kWidth = 1280;
2387 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002388 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002389 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2390 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002391
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002392 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002393 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002394 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002395 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002396
2397 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002398 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002399 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002400 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2401 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2402
2403 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002404 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002405 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002406 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2407 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2408
mflodmancc3d4422017-08-03 08:27:51 -07002409 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002410}
2411
mflodmancc3d4422017-08-03 08:27:51 -07002412TEST_F(VideoStreamEncoderTest,
2413 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002414 const int kWidth = 1280;
2415 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002416 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002417 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2418 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002419
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002420 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002421 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002422 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002423 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002424
2425 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002426 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002427 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002428 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002429 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2430
2431 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002432 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002433 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002434 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002435 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2436
mflodmancc3d4422017-08-03 08:27:51 -07002437 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002438}
2439
mflodmancc3d4422017-08-03 08:27:51 -07002440TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002441 const int kWidth = 1280;
2442 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002443 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002444 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2445 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002446
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002447 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002448 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002449 video_stream_encoder_->SetSource(&source,
2450 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002451
2452 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2453 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002454 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002455 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2456 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2457 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2458
2459 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002460 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002461 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002462 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2463 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2464 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2465
mflodmancc3d4422017-08-03 08:27:51 -07002466 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002467}
2468
mflodmancc3d4422017-08-03 08:27:51 -07002469TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002470 const int kWidth = 1280;
2471 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002472 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002473 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2474 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002475
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002476 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002477 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002478 video_stream_encoder_->SetSource(&source,
2479 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002480
2481 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2482 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002483 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002484 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2485 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2486 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2487
2488 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002489 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002490 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002491 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2492 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2493 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2494
mflodmancc3d4422017-08-03 08:27:51 -07002495 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002496}
2497
mflodmancc3d4422017-08-03 08:27:51 -07002498TEST_F(VideoStreamEncoderTest,
2499 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002500 const int kWidth = 1280;
2501 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002502 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002503 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2504 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002505
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002506 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002507 AdaptingFrameForwarder source;
2508 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002509 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002510 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002511
2512 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002513 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002514 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002515 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2516 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2517
2518 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002519 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002520 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002521 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002522 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002523 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2524 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2525
2526 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002527 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002528 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002529 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2530 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2531 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2532
mflodmancc3d4422017-08-03 08:27:51 -07002533 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002534}
2535
mflodmancc3d4422017-08-03 08:27:51 -07002536TEST_F(VideoStreamEncoderTest,
2537 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002538 const int kWidth = 1280;
2539 const int kHeight = 720;
2540 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002541 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002542 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2543 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002544
2545 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2546 stats.input_frame_rate = kInputFps;
2547 stats_proxy_->SetMockStats(stats);
2548
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002549 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002550 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2551 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002552 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002553
2554 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002555 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002556 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2557 sink_.WaitForEncodedFrame(2);
2558 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2559
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002560 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002561 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002562 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002563 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002564 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002565
2566 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002567 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002568 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2569 sink_.WaitForEncodedFrame(3);
2570 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2571
2572 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002573 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002574 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002575
mflodmancc3d4422017-08-03 08:27:51 -07002576 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002577}
2578
mflodmancc3d4422017-08-03 08:27:51 -07002579TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002580 const int kWidth = 1280;
2581 const int kHeight = 720;
2582 const size_t kNumFrames = 10;
2583
Erik Språng4c6ca302019-04-08 15:14:01 +02002584 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002585 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2586 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002587
asaperssond0de2952017-04-21 01:47:31 -07002588 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002589 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002590 video_source_.set_adaptation_enabled(true);
2591
2592 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2593 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2594
2595 int downscales = 0;
2596 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002597 video_source_.IncomingCapturedFrame(
2598 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2599 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002600
asaperssonfab67072017-04-04 05:51:49 -07002601 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002602 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002603 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002604 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002605
2606 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2607 ++downscales;
2608
2609 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2610 EXPECT_EQ(downscales,
2611 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2612 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002613 }
mflodmancc3d4422017-08-03 08:27:51 -07002614 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002615}
2616
mflodmancc3d4422017-08-03 08:27:51 -07002617TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002618 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2619 const int kWidth = 1280;
2620 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002621 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002622 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2623 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002624
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002625 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002626 AdaptingFrameForwarder source;
2627 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002628 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002629 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002630
Åsa Persson8c1bf952018-09-13 10:42:19 +02002631 int64_t timestamp_ms = kFrameIntervalMs;
2632 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002633 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002634 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002635 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2636 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2637
2638 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002639 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002640 timestamp_ms += kFrameIntervalMs;
2641 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2642 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002643 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002644 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2645 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2646
2647 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002648 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002649 timestamp_ms += kFrameIntervalMs;
2650 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002651 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002652 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002653 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2654 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2655
2656 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002657 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002658 timestamp_ms += kFrameIntervalMs;
2659 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2660 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002661 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002662 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2663 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2664
2665 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002667 timestamp_ms += kFrameIntervalMs;
2668 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002669 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002670 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002671 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2672 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2673
mflodmancc3d4422017-08-03 08:27:51 -07002674 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002675}
2676
mflodmancc3d4422017-08-03 08:27:51 -07002677TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002678 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2679 const int kWidth = 1280;
2680 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002681 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002682 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2683 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002684
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002685 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002686 AdaptingFrameForwarder source;
2687 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002688 video_stream_encoder_->SetSource(&source,
2689 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002690
Åsa Persson8c1bf952018-09-13 10:42:19 +02002691 int64_t timestamp_ms = kFrameIntervalMs;
2692 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002693 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002694 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002695 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2696 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2697
2698 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002699 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002700 timestamp_ms += kFrameIntervalMs;
2701 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2702 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002703 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2704 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2705 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2706
2707 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002708 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002709 timestamp_ms += kFrameIntervalMs;
2710 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002711 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002712 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002713 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2714 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2715
2716 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002717 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002718 timestamp_ms += kFrameIntervalMs;
2719 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2720 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002721 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2722 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2723 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2724
2725 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002726 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002727 timestamp_ms += kFrameIntervalMs;
2728 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002729 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002730 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002731 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2732 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2733
mflodmancc3d4422017-08-03 08:27:51 -07002734 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002735}
2736
Sergey Silkin41c650b2019-10-14 13:12:19 +02002737TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2738 fake_encoder_.SetResolutionBitrateLimits(
2739 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2740
2741 video_stream_encoder_->OnBitrateUpdated(
2742 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2743 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2744 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2745
2746 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2747 AdaptingFrameForwarder source;
2748 source.set_adaptation_enabled(true);
2749 video_stream_encoder_->SetSource(
2750 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2751
2752 // Insert 720p frame.
2753 int64_t timestamp_ms = kFrameIntervalMs;
2754 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2755 WaitForEncodedFrame(1280, 720);
2756
2757 // Reduce bitrate and trigger adapt down.
2758 video_stream_encoder_->OnBitrateUpdated(
2759 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2760 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2761 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2762 video_stream_encoder_->TriggerQualityLow();
2763
2764 // Insert 720p frame. It should be downscaled and encoded.
2765 timestamp_ms += kFrameIntervalMs;
2766 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2767 WaitForEncodedFrame(960, 540);
2768
2769 // Trigger adapt up. Higher resolution should not be requested duo to lack
2770 // of bitrate.
2771 video_stream_encoder_->TriggerQualityHigh();
2772 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2773
2774 // Increase bitrate.
2775 video_stream_encoder_->OnBitrateUpdated(
2776 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2777 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2778 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2779
2780 // Trigger adapt up. Higher resolution should be requested.
2781 video_stream_encoder_->TriggerQualityHigh();
2782 VerifyFpsMaxResolutionMax(source.sink_wants());
2783
2784 video_stream_encoder_->Stop();
2785}
2786
2787TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2788 fake_encoder_.SetResolutionBitrateLimits(
2789 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2790
2791 // Set bitrate equal to min bitrate of 540p.
2792 video_stream_encoder_->OnBitrateUpdated(
2793 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2794 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2795 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2796
2797 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2798 AdaptingFrameForwarder source;
2799 source.set_adaptation_enabled(true);
2800 video_stream_encoder_->SetSource(
2801 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2802
2803 // Insert 720p frame. It should be dropped and lower resolution should be
2804 // requested.
2805 int64_t timestamp_ms = kFrameIntervalMs;
2806 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2807 ExpectDroppedFrame();
2808 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2809
2810 // Insert 720p frame. It should be downscaled and encoded.
2811 timestamp_ms += kFrameIntervalMs;
2812 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2813 WaitForEncodedFrame(960, 540);
2814
2815 video_stream_encoder_->Stop();
2816}
2817
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002818class BalancedDegradationTest : public VideoStreamEncoderTest {
2819 protected:
2820 void SetupTest() {
2821 // Reset encoder for field trials to take effect.
2822 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002823 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002824
2825 // Enable BALANCED preference.
2826 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002827 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2828 }
2829
2830 void OnBitrateUpdated(int bitrate_bps) {
2831 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2832 DataRate::bps(bitrate_bps),
2833 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002834 }
2835
Åsa Persson45b176f2019-09-30 11:19:05 +02002836 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002837 timestamp_ms_ += kFrameIntervalMs;
2838 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002839 }
2840
2841 void InsertFrameAndWaitForEncoded() {
2842 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002843 sink_.WaitForEncodedFrame(timestamp_ms_);
2844 }
2845
2846 const int kWidth = 640; // pixels:640x360=230400
2847 const int kHeight = 360;
2848 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2849 int64_t timestamp_ms_ = 0;
2850 AdaptingFrameForwarder source_;
2851};
2852
2853TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2854 test::ScopedFieldTrials field_trials(
2855 "WebRTC-Video-BalancedDegradationSettings/"
2856 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2857 SetupTest();
2858
2859 // Force input frame rate.
2860 const int kInputFps = 24;
2861 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2862 stats.input_frame_rate = kInputFps;
2863 stats_proxy_->SetMockStats(stats);
2864
Åsa Persson45b176f2019-09-30 11:19:05 +02002865 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002866 VerifyFpsMaxResolutionMax(source_.sink_wants());
2867
2868 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2869 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2870 video_stream_encoder_->TriggerQualityLowExpectFalse();
2871 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2872
2873 video_stream_encoder_->Stop();
2874}
2875
2876TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2877 test::ScopedFieldTrials field_trials(
2878 "WebRTC-Video-BalancedDegradationSettings/"
2879 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2880 SetupTest();
2881
2882 // Force input frame rate.
2883 const int kInputFps = 25;
2884 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2885 stats.input_frame_rate = kInputFps;
2886 stats_proxy_->SetMockStats(stats);
2887
Åsa Persson45b176f2019-09-30 11:19:05 +02002888 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002889 VerifyFpsMaxResolutionMax(source_.sink_wants());
2890
2891 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2892 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2893 video_stream_encoder_->TriggerQualityLow();
2894 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2895
2896 video_stream_encoder_->Stop();
2897}
2898
2899TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2900 test::ScopedFieldTrials field_trials(
2901 "WebRTC-Video-BalancedDegradationSettings/"
2902 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2903 SetupTest();
2904
2905 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2906
Åsa Persson45b176f2019-09-30 11:19:05 +02002907 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002908 VerifyFpsMaxResolutionMax(source_.sink_wants());
2909
2910 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2911 video_stream_encoder_->TriggerQualityLow();
2912 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2913
2914 video_stream_encoder_->Stop();
2915}
2916
Åsa Perssonccfb3402019-09-25 15:13:04 +02002917TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002918 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002919 "WebRTC-Video-BalancedDegradationSettings/"
2920 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002921 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002922
Åsa Persson1b247f12019-08-14 17:26:39 +02002923 const int kMinBitrateBps = 425000;
2924 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002925 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002926
Åsa Persson45b176f2019-09-30 11:19:05 +02002927 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002928 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002929 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2930
2931 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2932 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002933 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002934 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002935 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2936
2937 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2938 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002939 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002940 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002941 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2942
Åsa Persson30ab0152019-08-27 12:22:33 +02002943 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2944 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002945 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002946 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2947 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002948 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2949
2950 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002951 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002952 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002953 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002954
Åsa Persson30ab0152019-08-27 12:22:33 +02002955 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002956 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002957 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002958 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002959 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002960 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2961
2962 video_stream_encoder_->Stop();
2963}
2964
Åsa Perssonccfb3402019-09-25 15:13:04 +02002965TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002966 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2967 test::ScopedFieldTrials field_trials(
2968 "WebRTC-Video-BalancedDegradationSettings/"
2969 "pixels:57600|129600|230400,fps:7|24|24/");
2970 SetupTest();
2971 OnBitrateUpdated(kLowTargetBitrateBps);
2972
2973 VerifyNoLimitation(source_.sink_wants());
2974
2975 // Insert frame, expect scaled down:
2976 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2977 InsertFrame();
2978 EXPECT_FALSE(WaitForFrame(1000));
2979 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2980 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2981
2982 // Insert frame, expect scaled down:
2983 // resolution (320x180@24fps).
2984 InsertFrame();
2985 EXPECT_FALSE(WaitForFrame(1000));
2986 EXPECT_LT(source_.sink_wants().max_pixel_count,
2987 source_.last_wants().max_pixel_count);
2988 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2989
2990 // Frame should not be dropped (min pixels per frame reached).
2991 InsertFrameAndWaitForEncoded();
2992
2993 video_stream_encoder_->Stop();
2994}
2995
2996TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002997 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002998 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002999 "WebRTC-Video-BalancedDegradationSettings/"
3000 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003001 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003002
Åsa Persson30ab0152019-08-27 12:22:33 +02003003 const int kResolutionMinBitrateBps = 435000;
3004 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003005 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003006
Åsa Persson45b176f2019-09-30 11:19:05 +02003007 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003008 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003009 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3010
3011 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3012 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003013 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003014 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003015 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3016
3017 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3018 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003019 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003020 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003021 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3022
3023 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3024 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003025 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003026 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003027 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3028
Åsa Persson30ab0152019-08-27 12:22:33 +02003029 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3030 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003031 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003032 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003033 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3034
3035 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3036 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003037 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003038 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3039
3040 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003041 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003042 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003043 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003044 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003045 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3046
3047 video_stream_encoder_->Stop();
3048}
3049
Åsa Perssonccfb3402019-09-25 15:13:04 +02003050TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003051 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003052 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003053 "WebRTC-Video-BalancedDegradationSettings/"
3054 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003055 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003056
Åsa Persson30ab0152019-08-27 12:22:33 +02003057 const int kMinBitrateBps = 425000;
3058 const int kTooLowMinBitrateBps = 424000;
3059 const int kResolutionMinBitrateBps = 435000;
3060 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003061 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003062
Åsa Persson45b176f2019-09-30 11:19:05 +02003063 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003064 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003065 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3066
3067 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3068 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003069 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003070 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003071 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3072
3073 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3074 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003075 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003076 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003077 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3078
3079 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3080 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003081 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003082 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003083 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3084
3085 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3086 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003087 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003088 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3089
3090 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003091 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003092 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003093 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003094 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003095 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3096
3097 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003098 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003099 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003100 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003101 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3102
3103 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003104 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003105 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003106 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003107 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003108 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3109
Åsa Persson1b247f12019-08-14 17:26:39 +02003110 video_stream_encoder_->Stop();
3111}
3112
mflodmancc3d4422017-08-03 08:27:51 -07003113TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003114 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3115 const int kWidth = 1280;
3116 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003117 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003118 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3119 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003120
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003121 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003122 AdaptingFrameForwarder source;
3123 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003124 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003125 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003126
Åsa Persson8c1bf952018-09-13 10:42:19 +02003127 int64_t timestamp_ms = kFrameIntervalMs;
3128 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003129 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003130 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003131 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3132 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3133 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3134 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3135
3136 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003137 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003138 timestamp_ms += kFrameIntervalMs;
3139 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3140 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003141 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003142 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3143 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3144 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3145 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3146
3147 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003148 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003149 timestamp_ms += kFrameIntervalMs;
3150 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3151 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003152 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003153 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3154 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3155 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3156 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3157
Jonathan Yubc771b72017-12-08 17:04:29 -08003158 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003159 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003160 timestamp_ms += kFrameIntervalMs;
3161 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3162 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003163 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003164 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3165 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003166 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003167 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3168
Jonathan Yubc771b72017-12-08 17:04:29 -08003169 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003170 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003171 timestamp_ms += kFrameIntervalMs;
3172 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3173 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003174 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003175 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003176 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3177 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3178 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3179 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3180
Jonathan Yubc771b72017-12-08 17:04:29 -08003181 // Trigger quality adapt down, expect no change (min resolution reached).
3182 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003183 timestamp_ms += kFrameIntervalMs;
3184 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3185 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003186 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3187 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3188 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3189 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3190 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3191
3192 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003193 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003194 timestamp_ms += kFrameIntervalMs;
3195 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3196 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003197 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003198 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3199 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3200 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3201 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3202
3203 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3204 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003205 timestamp_ms += kFrameIntervalMs;
3206 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3207 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003208 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3209 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3210 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3211 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3212 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3213
3214 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3215 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003216 timestamp_ms += kFrameIntervalMs;
3217 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3218 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003219 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003220 last_wants = source.sink_wants();
3221 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3222 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003223 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003224 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3225
3226 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003227 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003228 timestamp_ms += kFrameIntervalMs;
3229 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3230 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003231 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003232 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3233 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003234 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003235 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3236
3237 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003238 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003239 timestamp_ms += kFrameIntervalMs;
3240 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003241 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003242 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003243 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003244 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3245 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003246 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003247 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003248
mflodmancc3d4422017-08-03 08:27:51 -07003249 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003250}
3251
mflodmancc3d4422017-08-03 08:27:51 -07003252TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003253 const int kWidth = 640;
3254 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003255
Erik Språng4c6ca302019-04-08 15:14:01 +02003256 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003257 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3258 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003259
perkj803d97f2016-11-01 11:45:46 -07003260 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003261 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003262 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003263 }
3264
mflodmancc3d4422017-08-03 08:27:51 -07003265 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003266 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003267 video_source_.IncomingCapturedFrame(CreateFrame(
3268 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003269 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003270 }
3271
mflodmancc3d4422017-08-03 08:27:51 -07003272 video_stream_encoder_->Stop();
3273 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003274 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003275
Ying Wangef3998f2019-12-09 13:06:53 +01003276 EXPECT_METRIC_EQ(
3277 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3278 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003279 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3280}
3281
mflodmancc3d4422017-08-03 08:27:51 -07003282TEST_F(VideoStreamEncoderTest,
3283 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003284 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003285 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3286 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003287 const int kWidth = 640;
3288 const int kHeight = 360;
3289
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003290 video_stream_encoder_->SetSource(&video_source_,
3291 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003292
3293 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3294 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003295 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003296 }
3297
mflodmancc3d4422017-08-03 08:27:51 -07003298 video_stream_encoder_->Stop();
3299 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003300 stats_proxy_.reset();
3301
3302 EXPECT_EQ(0,
3303 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3304}
3305
mflodmancc3d4422017-08-03 08:27:51 -07003306TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003307 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003308 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003309
3310 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003311 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003312 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003313 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3314 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003315
sprang57c2fff2017-01-16 06:24:02 -08003316 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003317 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003318 video_stream_encoder_->OnBitrateUpdated(
3319 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3320 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003321
sprang57c2fff2017-01-16 06:24:02 -08003322 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003323 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3324 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003325 VideoBitrateAllocation bitrate_allocation =
3326 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003327 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003328 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003329 // TODO(srte): The use of millisecs here looks like an error, but the tests
3330 // fails using seconds, this should be investigated.
3331 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003332
3333 // Not called on second frame.
3334 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3335 .Times(0);
3336 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003337 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3338 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003339 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003340
3341 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003342 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3343 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003344 const int64_t start_time_ms = rtc::TimeMillis();
3345 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3346 video_source_.IncomingCapturedFrame(
3347 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3348 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003349 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003350 }
3351
3352 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003353 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003354
mflodmancc3d4422017-08-03 08:27:51 -07003355 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003356}
3357
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003358TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3359 // 2 TLs configured, temporal layers supported by encoder.
3360 const int kNumTemporalLayers = 2;
3361 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3362 fake_encoder_.SetTemporalLayersSupported(0, true);
3363
3364 // Bitrate allocated across temporal layers.
3365 const int kTl0Bps = kTargetBitrateBps *
3366 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003367 kNumTemporalLayers, /*temporal_id*/ 0,
3368 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003369 const int kTl1Bps = kTargetBitrateBps *
3370 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003371 kNumTemporalLayers, /*temporal_id*/ 1,
3372 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003373 VideoBitrateAllocation expected_bitrate;
3374 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3375 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3376
3377 VerifyAllocatedBitrate(expected_bitrate);
3378 video_stream_encoder_->Stop();
3379}
3380
3381TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3382 // 2 TLs configured, temporal layers not supported by encoder.
3383 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3384 fake_encoder_.SetTemporalLayersSupported(0, false);
3385
3386 // Temporal layers not supported by the encoder.
3387 // Total bitrate should be at ti:0.
3388 VideoBitrateAllocation expected_bitrate;
3389 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3390
3391 VerifyAllocatedBitrate(expected_bitrate);
3392 video_stream_encoder_->Stop();
3393}
3394
3395TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3396 // 2 TLs configured, temporal layers only supported for first stream.
3397 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3398 fake_encoder_.SetTemporalLayersSupported(0, true);
3399 fake_encoder_.SetTemporalLayersSupported(1, false);
3400
3401 const int kS0Bps = 150000;
3402 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003403 kS0Bps *
3404 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3405 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003406 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003407 kS0Bps *
3408 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3409 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003410 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3411 // Temporal layers not supported by si:1.
3412 VideoBitrateAllocation expected_bitrate;
3413 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3414 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3415 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3416
3417 VerifyAllocatedBitrate(expected_bitrate);
3418 video_stream_encoder_->Stop();
3419}
3420
Niels Möller7dc26b72017-12-06 10:27:48 +01003421TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3422 const int kFrameWidth = 1280;
3423 const int kFrameHeight = 720;
3424 const int kFramerate = 24;
3425
Erik Språng4c6ca302019-04-08 15:14:01 +02003426 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003427 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3428 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003429 test::FrameForwarder source;
3430 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003431 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003432
3433 // Insert a single frame, triggering initial configuration.
3434 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3435 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3436
3437 EXPECT_EQ(
3438 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3439 kDefaultFramerate);
3440
3441 // Trigger reconfigure encoder (without resetting the entire instance).
3442 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003443 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003444 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3445 video_encoder_config.number_of_streams = 1;
3446 video_encoder_config.video_stream_factory =
3447 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3448 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003449 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003450 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3451
3452 // Detector should be updated with fps limit from codec config.
3453 EXPECT_EQ(
3454 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3455 kFramerate);
3456
3457 // Trigger overuse, max framerate should be reduced.
3458 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3459 stats.input_frame_rate = kFramerate;
3460 stats_proxy_->SetMockStats(stats);
3461 video_stream_encoder_->TriggerCpuOveruse();
3462 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3463 int adapted_framerate =
3464 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3465 EXPECT_LT(adapted_framerate, kFramerate);
3466
3467 // Trigger underuse, max framerate should go back to codec configured fps.
3468 // Set extra low fps, to make sure it's actually reset, not just incremented.
3469 stats = stats_proxy_->GetStats();
3470 stats.input_frame_rate = adapted_framerate / 2;
3471 stats_proxy_->SetMockStats(stats);
3472 video_stream_encoder_->TriggerCpuNormalUsage();
3473 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3474 EXPECT_EQ(
3475 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3476 kFramerate);
3477
3478 video_stream_encoder_->Stop();
3479}
3480
3481TEST_F(VideoStreamEncoderTest,
3482 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3483 const int kFrameWidth = 1280;
3484 const int kFrameHeight = 720;
3485 const int kLowFramerate = 15;
3486 const int kHighFramerate = 25;
3487
Erik Språng4c6ca302019-04-08 15:14:01 +02003488 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003489 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3490 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003491 test::FrameForwarder source;
3492 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003493 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003494
3495 // Trigger initial configuration.
3496 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003497 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003498 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3499 video_encoder_config.number_of_streams = 1;
3500 video_encoder_config.video_stream_factory =
3501 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3502 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3503 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003504 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003505 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3506
3507 EXPECT_EQ(
3508 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3509 kLowFramerate);
3510
3511 // Trigger overuse, max framerate should be reduced.
3512 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3513 stats.input_frame_rate = kLowFramerate;
3514 stats_proxy_->SetMockStats(stats);
3515 video_stream_encoder_->TriggerCpuOveruse();
3516 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3517 int adapted_framerate =
3518 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3519 EXPECT_LT(adapted_framerate, kLowFramerate);
3520
3521 // Reconfigure the encoder with a new (higher max framerate), max fps should
3522 // still respect the adaptation.
3523 video_encoder_config.video_stream_factory =
3524 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3525 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3526 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003527 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003528 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3529
3530 EXPECT_EQ(
3531 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3532 adapted_framerate);
3533
3534 // Trigger underuse, max framerate should go back to codec configured fps.
3535 stats = stats_proxy_->GetStats();
3536 stats.input_frame_rate = adapted_framerate;
3537 stats_proxy_->SetMockStats(stats);
3538 video_stream_encoder_->TriggerCpuNormalUsage();
3539 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3540 EXPECT_EQ(
3541 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3542 kHighFramerate);
3543
3544 video_stream_encoder_->Stop();
3545}
3546
mflodmancc3d4422017-08-03 08:27:51 -07003547TEST_F(VideoStreamEncoderTest,
3548 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003549 const int kFrameWidth = 1280;
3550 const int kFrameHeight = 720;
3551 const int kFramerate = 24;
3552
Erik Språng4c6ca302019-04-08 15:14:01 +02003553 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003554 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3555 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003556 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003557 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003558 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003559
3560 // Trigger initial configuration.
3561 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003562 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003563 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3564 video_encoder_config.number_of_streams = 1;
3565 video_encoder_config.video_stream_factory =
3566 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3567 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003568 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003569 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003570 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003571
Niels Möller7dc26b72017-12-06 10:27:48 +01003572 EXPECT_EQ(
3573 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3574 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003575
3576 // Trigger overuse, max framerate should be reduced.
3577 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3578 stats.input_frame_rate = kFramerate;
3579 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003580 video_stream_encoder_->TriggerCpuOveruse();
3581 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003582 int adapted_framerate =
3583 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003584 EXPECT_LT(adapted_framerate, kFramerate);
3585
3586 // Change degradation preference to not enable framerate scaling. Target
3587 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003588 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003589 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003590 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003591 EXPECT_EQ(
3592 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3593 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003594
mflodmancc3d4422017-08-03 08:27:51 -07003595 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003596}
3597
mflodmancc3d4422017-08-03 08:27:51 -07003598TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003599 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003600 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003601 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003602 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003603 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003604 const int kWidth = 640;
3605 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003606
asaperssonfab67072017-04-04 05:51:49 -07003607 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003608
3609 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003610 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003611
3612 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003613 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003614
sprangc5d62e22017-04-02 23:53:04 -07003615 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003616
asaperssonfab67072017-04-04 05:51:49 -07003617 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003618 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003619 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003620
3621 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003622 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003623
sprangc5d62e22017-04-02 23:53:04 -07003624 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003625
mflodmancc3d4422017-08-03 08:27:51 -07003626 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003627}
3628
mflodmancc3d4422017-08-03 08:27:51 -07003629TEST_F(VideoStreamEncoderTest,
3630 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003631 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003632 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003633 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003634 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003635 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003636 const int kWidth = 640;
3637 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003638
3639 // We expect the n initial frames to get dropped.
3640 int i;
3641 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003642 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003643 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003644 }
3645 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003646 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003647 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003648
3649 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003650 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003651
mflodmancc3d4422017-08-03 08:27:51 -07003652 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003653}
3654
mflodmancc3d4422017-08-03 08:27:51 -07003655TEST_F(VideoStreamEncoderTest,
3656 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003657 const int kWidth = 640;
3658 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003659 video_stream_encoder_->OnBitrateUpdated(
3660 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3661 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003662
3663 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003664 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003665 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003666
asaperssonfab67072017-04-04 05:51:49 -07003667 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003668 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003669 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003670
mflodmancc3d4422017-08-03 08:27:51 -07003671 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003672}
3673
mflodmancc3d4422017-08-03 08:27:51 -07003674TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003675 const int kWidth = 640;
3676 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003677 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003678
3679 VideoEncoderConfig video_encoder_config;
3680 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3681 // Make format different, to force recreation of encoder.
3682 video_encoder_config.video_format.parameters["foo"] = "foo";
3683 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003684 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003685 video_stream_encoder_->OnBitrateUpdated(
3686 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3687 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003688
kthelgasonb83797b2017-02-14 11:57:25 -08003689 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003690 video_stream_encoder_->SetSource(&video_source_,
3691 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003692
asaperssonfab67072017-04-04 05:51:49 -07003693 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003694 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003695 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003696
mflodmancc3d4422017-08-03 08:27:51 -07003697 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003698 fake_encoder_.SetQualityScaling(true);
3699}
3700
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003701TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3702 webrtc::test::ScopedFieldTrials field_trials(
3703 "WebRTC-InitialFramedrop/Enabled/");
3704 // Reset encoder for field trials to take effect.
3705 ConfigureEncoder(video_encoder_config_.Copy());
3706 const int kTooLowBitrateForFrameSizeBps = 10000;
3707 const int kWidth = 640;
3708 const int kHeight = 360;
3709
Erik Språng4c6ca302019-04-08 15:14:01 +02003710 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003711 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3712 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003713 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3714 // Frame should not be dropped.
3715 WaitForEncodedFrame(1);
3716
Erik Språng610c7632019-03-06 15:37:33 +01003717 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003718 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003719 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003720 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003721 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3722 // Expect to drop this frame, the wait should time out.
3723 ExpectDroppedFrame();
3724
3725 // Expect the sink_wants to specify a scaled frame.
3726 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3727 video_stream_encoder_->Stop();
3728}
3729
Åsa Persson139f4dc2019-08-02 09:29:58 +02003730TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3731 webrtc::test::ScopedFieldTrials field_trials(
3732 "WebRTC-Video-QualityScalerSettings/"
3733 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3734 // Reset encoder for field trials to take effect.
3735 ConfigureEncoder(video_encoder_config_.Copy());
3736 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3737 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3738 const int kWidth = 640;
3739 const int kHeight = 360;
3740
3741 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003742 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3743 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003744 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3745 // Frame should not be dropped.
3746 WaitForEncodedFrame(1);
3747
3748 video_stream_encoder_->OnBitrateUpdated(
3749 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003750 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003751 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3752 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3753 // Frame should not be dropped.
3754 WaitForEncodedFrame(2);
3755
3756 video_stream_encoder_->OnBitrateUpdated(
3757 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003758 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003759 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3760 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3761 // Expect to drop this frame, the wait should time out.
3762 ExpectDroppedFrame();
3763
3764 // Expect the sink_wants to specify a scaled frame.
3765 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3766 video_stream_encoder_->Stop();
3767}
3768
Åsa Perssone644a032019-11-08 15:56:00 +01003769TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3770 webrtc::test::ScopedFieldTrials field_trials(
3771 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3772
3773 // Reset encoder for field trials to take effect.
3774 VideoEncoderConfig config = video_encoder_config_.Copy();
3775 config.max_bitrate_bps = kTargetBitrateBps;
3776 ConfigureEncoder(std::move(config));
3777 fake_encoder_.SetQp(kQpLow);
3778
3779 // Enable MAINTAIN_FRAMERATE preference.
3780 AdaptingFrameForwarder source;
3781 source.set_adaptation_enabled(true);
3782 video_stream_encoder_->SetSource(&source,
3783 DegradationPreference::MAINTAIN_FRAMERATE);
3784
3785 // Start at low bitrate.
3786 const int kLowBitrateBps = 200000;
3787 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3788 DataRate::bps(kLowBitrateBps),
3789 DataRate::bps(kLowBitrateBps), 0, 0);
3790
3791 // Expect first frame to be dropped and resolution to be limited.
3792 const int kWidth = 1280;
3793 const int kHeight = 720;
3794 const int64_t kFrameIntervalMs = 100;
3795 int64_t timestamp_ms = kFrameIntervalMs;
3796 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3797 ExpectDroppedFrame();
3798 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3799
3800 // Increase bitrate to encoder max.
3801 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3802 DataRate::bps(config.max_bitrate_bps),
3803 DataRate::bps(config.max_bitrate_bps),
3804 0, 0);
3805
3806 // Insert frames and advance |min_duration_ms|.
3807 for (size_t i = 1; i <= 10; i++) {
3808 timestamp_ms += kFrameIntervalMs;
3809 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3810 WaitForEncodedFrame(timestamp_ms);
3811 }
3812 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3813 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3814
3815 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3816
3817 // Insert frame should trigger high BW and release quality limitation.
3818 timestamp_ms += kFrameIntervalMs;
3819 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3820 WaitForEncodedFrame(timestamp_ms);
3821 VerifyFpsMaxResolutionMax(source.sink_wants());
3822
3823 // Frame should not be adapted.
3824 timestamp_ms += kFrameIntervalMs;
3825 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3826 WaitForEncodedFrame(kWidth, kHeight);
3827 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3828
3829 video_stream_encoder_->Stop();
3830}
3831
mflodmancc3d4422017-08-03 08:27:51 -07003832TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003833 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3834 const int kTooSmallWidth = 10;
3835 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003836 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003837 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3838 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003839
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003840 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003841 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003842 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003843 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003844 VerifyNoLimitation(source.sink_wants());
3845 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3846
3847 // Trigger adapt down, too small frame, expect no change.
3848 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003849 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003850 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003851 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003852 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3853 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3854
mflodmancc3d4422017-08-03 08:27:51 -07003855 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003856}
3857
mflodmancc3d4422017-08-03 08:27:51 -07003858TEST_F(VideoStreamEncoderTest,
3859 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003860 const int kTooSmallWidth = 10;
3861 const int kTooSmallHeight = 10;
3862 const int kFpsLimit = 7;
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);
asaperssonf7e294d2017-06-13 23:25:22 -07003866
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003867 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003868 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003869 video_stream_encoder_->SetSource(&source,
3870 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003871 VerifyNoLimitation(source.sink_wants());
3872 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3873 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3874
3875 // Trigger adapt down, expect limited framerate.
3876 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003877 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003878 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003879 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3880 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3881 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3882 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3883
3884 // Trigger adapt down, too small frame, expect no change.
3885 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003886 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003887 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003888 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3889 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3890 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3891 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3892
mflodmancc3d4422017-08-03 08:27:51 -07003893 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003894}
3895
mflodmancc3d4422017-08-03 08:27:51 -07003896TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003897 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003898 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003899 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3900 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003901 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003902 const int kFrameWidth = 1280;
3903 const int kFrameHeight = 720;
3904 video_source_.IncomingCapturedFrame(
3905 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003906 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003907 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003908}
3909
sprangb1ca0732017-02-01 08:38:12 -08003910// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003911TEST_F(VideoStreamEncoderTest,
3912 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003913 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003914 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3915 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003916
3917 const int kFrameWidth = 1280;
3918 const int kFrameHeight = 720;
3919 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003920 // requested by
3921 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003922 video_source_.set_adaptation_enabled(true);
3923
3924 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003925 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003926 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003927
3928 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003929 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003930 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003931 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003932 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003933
asaperssonfab67072017-04-04 05:51:49 -07003934 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003935 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003936 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003937 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003938 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003939
mflodmancc3d4422017-08-03 08:27:51 -07003940 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003941}
sprangfe627f32017-03-29 08:24:59 -07003942
mflodmancc3d4422017-08-03 08:27:51 -07003943TEST_F(VideoStreamEncoderTest,
3944 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003945 const int kFrameWidth = 1280;
3946 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003947
Erik Språng4c6ca302019-04-08 15:14:01 +02003948 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003949 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3950 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003951 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003952 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003953 video_source_.set_adaptation_enabled(true);
3954
sprang4847ae62017-06-27 07:06:52 -07003955 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003956
3957 video_source_.IncomingCapturedFrame(
3958 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003959 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003960
3961 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003962 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003963
3964 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003965 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003966 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003967 video_source_.IncomingCapturedFrame(
3968 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003969 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003970 }
3971
3972 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003973 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003974 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003975 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003976 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003977 video_source_.IncomingCapturedFrame(
3978 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003979 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003980 ++num_frames_dropped;
3981 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003982 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003983 }
3984 }
3985
sprang4847ae62017-06-27 07:06:52 -07003986 // Add some slack to account for frames dropped by the frame dropper.
3987 const int kErrorMargin = 1;
3988 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003989 kErrorMargin);
3990
3991 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003992 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003993 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003994 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003995 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003996 video_source_.IncomingCapturedFrame(
3997 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003998 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003999 ++num_frames_dropped;
4000 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004001 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004002 }
4003 }
sprang4847ae62017-06-27 07:06:52 -07004004 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004005 kErrorMargin);
4006
4007 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004008 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004009 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004010 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004011 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004012 video_source_.IncomingCapturedFrame(
4013 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004014 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004015 ++num_frames_dropped;
4016 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004017 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004018 }
4019 }
sprang4847ae62017-06-27 07:06:52 -07004020 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004021 kErrorMargin);
4022
4023 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004024 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004025 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004026 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004027 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004028 video_source_.IncomingCapturedFrame(
4029 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004030 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004031 ++num_frames_dropped;
4032 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004033 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004034 }
4035 }
4036 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4037
mflodmancc3d4422017-08-03 08:27:51 -07004038 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004039}
4040
mflodmancc3d4422017-08-03 08:27:51 -07004041TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004042 const int kFramerateFps = 5;
4043 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004044 const int kFrameWidth = 1280;
4045 const int kFrameHeight = 720;
4046
sprang4847ae62017-06-27 07:06:52 -07004047 // Reconfigure encoder with two temporal layers and screensharing, which will
4048 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004049 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004050
Erik Språng4c6ca302019-04-08 15:14:01 +02004051 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004052 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4053 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004054 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004055 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004056 video_source_.set_adaptation_enabled(true);
4057
sprang4847ae62017-06-27 07:06:52 -07004058 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004059
4060 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004061 rtc::VideoSinkWants last_wants;
4062 do {
4063 last_wants = video_source_.sink_wants();
4064
sprangc5d62e22017-04-02 23:53:04 -07004065 // Insert frames to get a new fps estimate...
4066 for (int j = 0; j < kFramerateFps; ++j) {
4067 video_source_.IncomingCapturedFrame(
4068 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004069 if (video_source_.last_sent_width()) {
4070 sink_.WaitForEncodedFrame(timestamp_ms);
4071 }
sprangc5d62e22017-04-02 23:53:04 -07004072 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004073 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004074 }
4075 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004076 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004077 } while (video_source_.sink_wants().max_framerate_fps <
4078 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004079
Jonathan Yubc771b72017-12-08 17:04:29 -08004080 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004081
mflodmancc3d4422017-08-03 08:27:51 -07004082 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004083}
asaperssonf7e294d2017-06-13 23:25:22 -07004084
mflodmancc3d4422017-08-03 08:27:51 -07004085TEST_F(VideoStreamEncoderTest,
4086 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004087 const int kWidth = 1280;
4088 const int kHeight = 720;
4089 const int64_t kFrameIntervalMs = 150;
4090 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004091 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004092 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4093 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004094
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004095 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004096 AdaptingFrameForwarder source;
4097 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004098 video_stream_encoder_->SetSource(&source,
4099 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004100 timestamp_ms += kFrameIntervalMs;
4101 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004102 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004103 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004104 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4105 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4106 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4107
4108 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004109 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004110 timestamp_ms += kFrameIntervalMs;
4111 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004112 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004113 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4114 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4115 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4116 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4117
4118 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004119 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004120 timestamp_ms += kFrameIntervalMs;
4121 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004122 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004123 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4124 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4125 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4126 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4127
4128 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004129 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004130 timestamp_ms += kFrameIntervalMs;
4131 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004132 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004133 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4134 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4135 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4136 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4137
4138 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004139 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004140 timestamp_ms += kFrameIntervalMs;
4141 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004142 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004143 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4144 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4145 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4146 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4147
4148 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004149 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004150 timestamp_ms += kFrameIntervalMs;
4151 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004152 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004153 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4154 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4155 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4156 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4157
4158 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004159 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004160 timestamp_ms += kFrameIntervalMs;
4161 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004162 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004163 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4164 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4165 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4166 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4167
4168 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004169 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004170 timestamp_ms += kFrameIntervalMs;
4171 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004172 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004173 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4174 rtc::VideoSinkWants last_wants = source.sink_wants();
4175 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4176 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4177 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4178
4179 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004180 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004181 timestamp_ms += kFrameIntervalMs;
4182 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004183 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004184 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4185 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4186 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4187 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4188
4189 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004190 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004191 timestamp_ms += kFrameIntervalMs;
4192 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004193 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004194 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4195 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4196 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4197 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4198
4199 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004200 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004201 timestamp_ms += kFrameIntervalMs;
4202 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004203 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004204 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4205 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4206 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4207 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4208
4209 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004210 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004211 timestamp_ms += kFrameIntervalMs;
4212 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004213 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004214 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4215 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4216 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4217 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4218
4219 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004220 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004221 timestamp_ms += kFrameIntervalMs;
4222 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004223 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004224 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4225 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4226 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4227 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4228
4229 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004230 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004231 timestamp_ms += kFrameIntervalMs;
4232 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004233 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004234 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4235 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4236 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4237 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4238
4239 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004240 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004241 timestamp_ms += kFrameIntervalMs;
4242 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004243 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004244 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4245 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4246 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4247 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4248
Åsa Persson30ab0152019-08-27 12:22:33 +02004249 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004250 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004251 timestamp_ms += kFrameIntervalMs;
4252 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004253 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004254 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004255 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004256 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4257 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4258 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4259
4260 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004261 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004262 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004263 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4264
mflodmancc3d4422017-08-03 08:27:51 -07004265 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004266}
4267
mflodmancc3d4422017-08-03 08:27:51 -07004268TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004269 const int kWidth = 1280;
4270 const int kHeight = 720;
4271 const int64_t kFrameIntervalMs = 150;
4272 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004273 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004274 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4275 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004276
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004277 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004278 AdaptingFrameForwarder source;
4279 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004280 video_stream_encoder_->SetSource(&source,
4281 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004282 timestamp_ms += kFrameIntervalMs;
4283 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004284 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004285 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004286 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4287 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4288 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4289 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4290 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4291 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4292
4293 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004294 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004295 timestamp_ms += kFrameIntervalMs;
4296 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004297 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004298 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4299 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4300 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4301 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4302 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4303 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4304 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4305
4306 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004307 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004308 timestamp_ms += kFrameIntervalMs;
4309 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004310 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004311 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4312 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4313 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4314 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4315 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4316 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4317 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4318
4319 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004320 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004321 timestamp_ms += kFrameIntervalMs;
4322 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004323 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004324 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4325 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4326 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4327 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4328 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4329 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4330 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4331
4332 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004333 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004334 timestamp_ms += kFrameIntervalMs;
4335 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004336 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004337 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4338 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4339 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4340 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4341 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4342 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4343 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4344
4345 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004346 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004347 timestamp_ms += kFrameIntervalMs;
4348 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004349 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004350 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4351 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4352 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4353 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4354 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4355 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4356 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4357
4358 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004359 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004360 timestamp_ms += kFrameIntervalMs;
4361 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004362 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004363 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004364 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004365 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4366 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4367 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4368 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4369 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4370 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4371
4372 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004373 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004374 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004375 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4376 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4377
mflodmancc3d4422017-08-03 08:27:51 -07004378 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004379}
4380
mflodmancc3d4422017-08-03 08:27:51 -07004381TEST_F(VideoStreamEncoderTest,
4382 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004383 const int kWidth = 640;
4384 const int kHeight = 360;
4385 const int kFpsLimit = 15;
4386 const int64_t kFrameIntervalMs = 150;
4387 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004388 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004389 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4390 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004391
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004392 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004393 AdaptingFrameForwarder source;
4394 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004395 video_stream_encoder_->SetSource(&source,
4396 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004397 timestamp_ms += kFrameIntervalMs;
4398 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004399 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004400 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004401 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4402 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4403 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4404 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4405 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4406 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4407
4408 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004409 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004410 timestamp_ms += kFrameIntervalMs;
4411 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004412 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004413 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4414 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4415 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4416 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4417 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4418 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4419 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4420
4421 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004422 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004423 timestamp_ms += kFrameIntervalMs;
4424 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004425 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004426 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4427 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4428 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4429 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4430 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4431 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4432 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4433
4434 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004435 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004436 timestamp_ms += kFrameIntervalMs;
4437 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004438 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004439 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4440 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4441 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4442 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4443 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4444 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4445 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4446
4447 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004448 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004449 timestamp_ms += kFrameIntervalMs;
4450 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004451 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004452 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004453 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4454 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4455 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4456 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4457 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4458 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4459
4460 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004461 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004462 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004463 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4464 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4465
mflodmancc3d4422017-08-03 08:27:51 -07004466 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004467}
4468
mflodmancc3d4422017-08-03 08:27:51 -07004469TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004470 const int kFrameWidth = 1920;
4471 const int kFrameHeight = 1080;
4472 // 3/4 of 1920.
4473 const int kAdaptedFrameWidth = 1440;
4474 // 3/4 of 1080 rounded down to multiple of 4.
4475 const int kAdaptedFrameHeight = 808;
4476 const int kFramerate = 24;
4477
Erik Språng4c6ca302019-04-08 15:14:01 +02004478 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004479 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4480 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004481 // Trigger reconfigure encoder (without resetting the entire instance).
4482 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004483 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004484 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4485 video_encoder_config.number_of_streams = 1;
4486 video_encoder_config.video_stream_factory =
4487 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004488 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004489 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004490 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004491
4492 video_source_.set_adaptation_enabled(true);
4493
4494 video_source_.IncomingCapturedFrame(
4495 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004496 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004497
4498 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004499 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004500 video_source_.IncomingCapturedFrame(
4501 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004502 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004503
mflodmancc3d4422017-08-03 08:27:51 -07004504 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004505}
4506
mflodmancc3d4422017-08-03 08:27:51 -07004507TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004508 const int kFrameWidth = 1280;
4509 const int kFrameHeight = 720;
4510 const int kLowFps = 2;
4511 const int kHighFps = 30;
4512
Erik Språng4c6ca302019-04-08 15:14:01 +02004513 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004514 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4515 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004516
4517 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4518 max_framerate_ = kLowFps;
4519
4520 // Insert 2 seconds of 2fps video.
4521 for (int i = 0; i < kLowFps * 2; ++i) {
4522 video_source_.IncomingCapturedFrame(
4523 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4524 WaitForEncodedFrame(timestamp_ms);
4525 timestamp_ms += 1000 / kLowFps;
4526 }
4527
4528 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004529 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004530 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4531 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004532 video_source_.IncomingCapturedFrame(
4533 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4534 WaitForEncodedFrame(timestamp_ms);
4535 timestamp_ms += 1000 / kLowFps;
4536
4537 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4538
4539 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004540 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004541 const int kFrameIntervalMs = 1000 / kHighFps;
4542 max_framerate_ = kHighFps;
4543 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4544 video_source_.IncomingCapturedFrame(
4545 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4546 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4547 // be dropped if the encoder hans't been updated with the new higher target
4548 // framerate yet, causing it to overshoot the target bitrate and then
4549 // suffering the wrath of the media optimizer.
4550 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4551 timestamp_ms += kFrameIntervalMs;
4552 }
4553
4554 // Don expect correct measurement just yet, but it should be higher than
4555 // before.
4556 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4557
mflodmancc3d4422017-08-03 08:27:51 -07004558 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004559}
4560
mflodmancc3d4422017-08-03 08:27:51 -07004561TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004562 const int kFrameWidth = 1280;
4563 const int kFrameHeight = 720;
4564 const int kTargetBitrateBps = 1000000;
4565
4566 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004567 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004568 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004569 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4570 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004571 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004572
4573 // Insert a first video frame, causes another bitrate update.
4574 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4575 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4576 video_source_.IncomingCapturedFrame(
4577 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4578 WaitForEncodedFrame(timestamp_ms);
4579
4580 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004581 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4582 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004583
4584 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004585 timestamp_ms += kProcessIntervalMs;
4586 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004587
4588 // Bitrate observer should not be called.
4589 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4590 video_source_.IncomingCapturedFrame(
4591 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4592 ExpectDroppedFrame();
4593
mflodmancc3d4422017-08-03 08:27:51 -07004594 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004595}
ilnik6b826ef2017-06-16 06:53:48 -07004596
Niels Möller4db138e2018-04-19 09:04:13 +02004597TEST_F(VideoStreamEncoderTest,
4598 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4599 const int kFrameWidth = 1280;
4600 const int kFrameHeight = 720;
4601 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004602 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004603 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4604 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004605 video_source_.IncomingCapturedFrame(
4606 CreateFrame(1, kFrameWidth, kFrameHeight));
4607 WaitForEncodedFrame(1);
4608 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4609 .low_encode_usage_threshold_percent,
4610 default_options.low_encode_usage_threshold_percent);
4611 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4612 .high_encode_usage_threshold_percent,
4613 default_options.high_encode_usage_threshold_percent);
4614 video_stream_encoder_->Stop();
4615}
4616
4617TEST_F(VideoStreamEncoderTest,
4618 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4619 const int kFrameWidth = 1280;
4620 const int kFrameHeight = 720;
4621 CpuOveruseOptions hardware_options;
4622 hardware_options.low_encode_usage_threshold_percent = 150;
4623 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004624 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004625
Erik Språng4c6ca302019-04-08 15:14:01 +02004626 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004627 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4628 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004629 video_source_.IncomingCapturedFrame(
4630 CreateFrame(1, kFrameWidth, kFrameHeight));
4631 WaitForEncodedFrame(1);
4632 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4633 .low_encode_usage_threshold_percent,
4634 hardware_options.low_encode_usage_threshold_percent);
4635 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4636 .high_encode_usage_threshold_percent,
4637 hardware_options.high_encode_usage_threshold_percent);
4638 video_stream_encoder_->Stop();
4639}
4640
Niels Möller6bb5ab92019-01-11 11:11:10 +01004641TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4642 const int kFrameWidth = 320;
4643 const int kFrameHeight = 240;
4644 const int kFps = 30;
4645 const int kTargetBitrateBps = 120000;
4646 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4647
Erik Språng4c6ca302019-04-08 15:14:01 +02004648 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004649 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4650 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004651
4652 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4653 max_framerate_ = kFps;
4654
4655 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4656 fake_encoder_.SimulateOvershoot(1.0);
4657 int num_dropped = 0;
4658 for (int i = 0; i < kNumFramesInRun; ++i) {
4659 video_source_.IncomingCapturedFrame(
4660 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4661 // Wait up to two frame durations for a frame to arrive.
4662 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4663 ++num_dropped;
4664 }
4665 timestamp_ms += 1000 / kFps;
4666 }
4667
Erik Språnga8d48ab2019-02-08 14:17:40 +01004668 // Framerate should be measured to be near the expected target rate.
4669 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4670
4671 // Frame drops should be within 5% of expected 0%.
4672 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004673
4674 // Make encoder produce frames at double the expected bitrate during 3 seconds
4675 // of video, verify number of drops. Rate needs to be slightly changed in
4676 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004677 double overshoot_factor = 2.0;
4678 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4679 // With bitrate adjuster, when need to overshoot even more to trigger
4680 // frame dropping.
4681 overshoot_factor *= 2;
4682 }
4683 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004684 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004685 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004686 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004687 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004688 num_dropped = 0;
4689 for (int i = 0; i < kNumFramesInRun; ++i) {
4690 video_source_.IncomingCapturedFrame(
4691 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4692 // Wait up to two frame durations for a frame to arrive.
4693 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4694 ++num_dropped;
4695 }
4696 timestamp_ms += 1000 / kFps;
4697 }
4698
Erik Språng4c6ca302019-04-08 15:14:01 +02004699 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004700 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4701 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004702
4703 // Target framerate should be still be near the expected target, despite
4704 // the frame drops.
4705 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4706
4707 // Frame drops should be within 5% of expected 50%.
4708 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004709
4710 video_stream_encoder_->Stop();
4711}
4712
4713TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4714 const int kFrameWidth = 320;
4715 const int kFrameHeight = 240;
4716 const int kActualInputFps = 24;
4717 const int kTargetBitrateBps = 120000;
4718
4719 ASSERT_GT(max_framerate_, kActualInputFps);
4720
4721 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4722 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004723 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004724 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4725 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004726
4727 // Insert 3 seconds of video, with an input fps lower than configured max.
4728 for (int i = 0; i < kActualInputFps * 3; ++i) {
4729 video_source_.IncomingCapturedFrame(
4730 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4731 // Wait up to two frame durations for a frame to arrive.
4732 WaitForEncodedFrame(timestamp_ms);
4733 timestamp_ms += 1000 / kActualInputFps;
4734 }
4735
4736 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4737
4738 video_stream_encoder_->Stop();
4739}
4740
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004741TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4742 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004743 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004744 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4745 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004746
4747 fake_encoder_.BlockNextEncode();
4748 video_source_.IncomingCapturedFrame(
4749 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4750 WaitForEncodedFrame(1);
4751 // On the very first frame full update should be forced.
4752 rect = fake_encoder_.GetLastUpdateRect();
4753 EXPECT_EQ(rect.offset_x, 0);
4754 EXPECT_EQ(rect.offset_y, 0);
4755 EXPECT_EQ(rect.height, codec_height_);
4756 EXPECT_EQ(rect.width, codec_width_);
4757 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4758 // call to ContinueEncode.
4759 video_source_.IncomingCapturedFrame(
4760 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4761 ExpectDroppedFrame();
4762 video_source_.IncomingCapturedFrame(
4763 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4764 ExpectDroppedFrame();
4765 fake_encoder_.ContinueEncode();
4766 WaitForEncodedFrame(3);
4767 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4768 rect = fake_encoder_.GetLastUpdateRect();
4769 EXPECT_EQ(rect.offset_x, 1);
4770 EXPECT_EQ(rect.offset_y, 0);
4771 EXPECT_EQ(rect.width, 10);
4772 EXPECT_EQ(rect.height, 1);
4773
4774 video_source_.IncomingCapturedFrame(
4775 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4776 WaitForEncodedFrame(4);
4777 // Previous frame was encoded, so no accumulation should happen.
4778 rect = fake_encoder_.GetLastUpdateRect();
4779 EXPECT_EQ(rect.offset_x, 0);
4780 EXPECT_EQ(rect.offset_y, 0);
4781 EXPECT_EQ(rect.width, 1);
4782 EXPECT_EQ(rect.height, 1);
4783
4784 video_stream_encoder_->Stop();
4785}
4786
Erik Språngd7329ca2019-02-21 21:19:53 +01004787TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004788 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004789 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4790 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004791
4792 // First frame is always keyframe.
4793 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4794 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004795 EXPECT_THAT(
4796 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004797 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004798
4799 // Insert delta frame.
4800 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4801 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004802 EXPECT_THAT(
4803 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004804 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004805
4806 // Request next frame be a key-frame.
4807 video_stream_encoder_->SendKeyFrame();
4808 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4809 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004810 EXPECT_THAT(
4811 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004812 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004813
4814 video_stream_encoder_->Stop();
4815}
4816
4817TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4818 // Setup simulcast with three streams.
4819 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004820 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004821 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004822 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004823 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004824 // Wait for all three layers before triggering event.
4825 sink_.SetNumExpectedLayers(3);
4826
4827 // First frame is always keyframe.
4828 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4829 WaitForEncodedFrame(1);
4830 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004831 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4832 VideoFrameType::kVideoFrameKey,
4833 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004834
4835 // Insert delta frame.
4836 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4837 WaitForEncodedFrame(2);
4838 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004839 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4840 VideoFrameType::kVideoFrameDelta,
4841 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004842
4843 // Request next frame be a key-frame.
4844 // Only first stream is configured to produce key-frame.
4845 video_stream_encoder_->SendKeyFrame();
4846 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4847 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004848
4849 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4850 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004851 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004852 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004853 VideoFrameType::kVideoFrameKey,
4854 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004855
4856 video_stream_encoder_->Stop();
4857}
4858
4859TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4860 // Configure internal source factory and setup test again.
4861 encoder_factory_.SetHasInternalSource(true);
4862 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004863 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004864 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4865 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004866
4867 // Call encoder directly, simulating internal source where encoded frame
4868 // callback in VideoStreamEncoder is called despite no OnFrame().
4869 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4870 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004871 EXPECT_THAT(
4872 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004873 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004874
Niels Möller8f7ce222019-03-21 15:43:58 +01004875 const std::vector<VideoFrameType> kDeltaFrame = {
4876 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004877 // Need to set timestamp manually since manually for injected frame.
4878 VideoFrame frame = CreateFrame(101, nullptr);
4879 frame.set_timestamp(101);
4880 fake_encoder_.InjectFrame(frame, false);
4881 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004882 EXPECT_THAT(
4883 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004884 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004885
4886 // Request key-frame. The forces a dummy frame down into the encoder.
4887 fake_encoder_.ExpectNullFrame();
4888 video_stream_encoder_->SendKeyFrame();
4889 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004890 EXPECT_THAT(
4891 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004892 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004893
4894 video_stream_encoder_->Stop();
4895}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004896
4897TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4898 // Configure internal source factory and setup test again.
4899 encoder_factory_.SetHasInternalSource(true);
4900 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004901 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004902 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4903 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004904
4905 int64_t timestamp = 1;
4906 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004907 image.SetEncodedData(
4908 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004909 image.capture_time_ms_ = ++timestamp;
4910 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4911 const int64_t kEncodeFinishDelayMs = 10;
4912 image.timing_.encode_start_ms = timestamp;
4913 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4914 fake_encoder_.InjectEncodedImage(image);
4915 // Wait for frame without incrementing clock.
4916 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4917 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4918 // capture timestamp should be kEncodeFinishDelayMs in the past.
4919 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4920 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4921 kEncodeFinishDelayMs);
4922
4923 video_stream_encoder_->Stop();
4924}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004925
4926TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4927 // Configure internal source factory and setup test again.
4928 encoder_factory_.SetHasInternalSource(true);
4929 ResetEncoder("H264", 1, 1, 1, false);
4930
4931 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4932 image._frameType = VideoFrameType::kVideoFrameKey;
4933
4934 CodecSpecificInfo codec_specific_info;
4935 codec_specific_info.codecType = kVideoCodecH264;
4936
4937 RTPFragmentationHeader fragmentation;
4938 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4939 fragmentation.fragmentationOffset[0] = 4;
4940 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4941
4942 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4943 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4944
4945 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4946 testing::ElementsAreArray(optimal_sps));
4947 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4948 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4949 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4950 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4951
4952 video_stream_encoder_->Stop();
4953}
4954
4955TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4956 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4957 0x00, 0x00, 0x03, 0x03, 0xF4,
4958 0x05, 0x03, 0xC7, 0xC0};
4959
4960 // Configure internal source factory and setup test again.
4961 encoder_factory_.SetHasInternalSource(true);
4962 ResetEncoder("H264", 1, 1, 1, false);
4963
4964 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4965 image._frameType = VideoFrameType::kVideoFrameKey;
4966
4967 CodecSpecificInfo codec_specific_info;
4968 codec_specific_info.codecType = kVideoCodecH264;
4969
4970 RTPFragmentationHeader fragmentation;
4971 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4972 fragmentation.fragmentationOffset[0] = 4;
4973 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4974
4975 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4976 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4977
4978 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4979 testing::ElementsAreArray(optimal_sps));
4980 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4981 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4982 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4983 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4984
4985 video_stream_encoder_->Stop();
4986}
4987
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004988TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4989 const int kFrameWidth = 1280;
4990 const int kFrameHeight = 720;
4991 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4992
4993 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004994 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4995 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004996 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4997
4998 // Insert a first video frame. It should be dropped because of downscale in
4999 // resolution.
5000 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5001 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5002 frame.set_rotation(kVideoRotation_270);
5003 video_source_.IncomingCapturedFrame(frame);
5004
5005 ExpectDroppedFrame();
5006
5007 // Second frame is downscaled.
5008 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5009 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5010 frame.set_rotation(kVideoRotation_90);
5011 video_source_.IncomingCapturedFrame(frame);
5012
5013 WaitForEncodedFrame(timestamp_ms);
5014 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5015
5016 // Insert another frame, also downscaled.
5017 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5018 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5019 frame.set_rotation(kVideoRotation_180);
5020 video_source_.IncomingCapturedFrame(frame);
5021
5022 WaitForEncodedFrame(timestamp_ms);
5023 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5024
5025 video_stream_encoder_->Stop();
5026}
5027
Erik Språng5056af02019-09-02 15:53:11 +02005028TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5029 const int kFrameWidth = 320;
5030 const int kFrameHeight = 180;
5031
5032 // Initial rate.
5033 video_stream_encoder_->OnBitrateUpdated(
5034 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005035 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005036 /*link_allocation=*/DataRate::kbps(300),
5037 /*fraction_lost=*/0,
5038 /*rtt_ms=*/0);
5039
5040 // Insert a first video frame so that encoder gets configured.
5041 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5042 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5043 frame.set_rotation(kVideoRotation_270);
5044 video_source_.IncomingCapturedFrame(frame);
5045 WaitForEncodedFrame(timestamp_ms);
5046
5047 // Set a target rate below the minimum allowed by the codec settings.
5048 VideoCodec codec_config = fake_encoder_.codec_config();
5049 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5050 DataRate target_rate = min_rate - DataRate::kbps(1);
5051 video_stream_encoder_->OnBitrateUpdated(
5052 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005053 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005054 /*link_allocation=*/target_rate,
5055 /*fraction_lost=*/0,
5056 /*rtt_ms=*/0);
5057 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5058
5059 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5060 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5061 ASSERT_TRUE(rate_settings.has_value());
5062 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5063 EXPECT_EQ(min_rate, allocation_sum);
5064 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5065
5066 video_stream_encoder_->Stop();
5067}
5068
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005069TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005070 video_stream_encoder_->OnBitrateUpdated(
5071 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5072 DataRate::bps(kTargetBitrateBps), 0, 0);
5073 // Capture a frame and wait for it to synchronize with the encoder thread.
5074 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5075 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5076 WaitForEncodedFrame(1);
5077
5078 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5079 ASSERT_TRUE(prev_rate_settings.has_value());
5080 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5081 kDefaultFramerate);
5082
5083 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5084 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5085 timestamp_ms += 1000 / kDefaultFramerate;
5086 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5087 WaitForEncodedFrame(timestamp_ms);
5088 }
5089 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5090 kDefaultFramerate);
5091 // Capture larger frame to trigger a reconfigure.
5092 codec_height_ *= 2;
5093 codec_width_ *= 2;
5094 timestamp_ms += 1000 / kDefaultFramerate;
5095 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5096 WaitForEncodedFrame(timestamp_ms);
5097
5098 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5099 auto current_rate_settings =
5100 fake_encoder_.GetAndResetLastRateControlSettings();
5101 // Ensure we have actually reconfigured twice
5102 // The rate settings should have been set again even though
5103 // they haven't changed.
5104 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005105 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005106
5107 video_stream_encoder_->Stop();
5108}
5109
philipeld9cc8c02019-09-16 14:53:40 +02005110struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5111 MOCK_METHOD0(RequestEncoderFallback, void());
5112 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5113};
5114
5115TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5116 constexpr int kDontCare = 100;
5117
5118 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5119 video_send_config_.encoder_settings.encoder_switch_request_callback =
5120 &switch_callback;
5121 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5122 encoder_config.codec_type = kVideoCodecVP8;
5123 webrtc::test::ScopedFieldTrials field_trial(
5124 "WebRTC-NetworkCondition-EncoderSwitch/"
5125 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5126 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5127
5128 // Reset encoder for new configuration to take effect.
5129 ConfigureEncoder(std::move(encoder_config));
5130
5131 // Send one frame to trigger ReconfigureEncoder.
5132 video_source_.IncomingCapturedFrame(
5133 CreateFrame(kDontCare, kDontCare, kDontCare));
5134
5135 using Config = EncoderSwitchRequestCallback::Config;
5136 EXPECT_CALL(switch_callback,
5137 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5138 Field(&Config::param, "ping"),
5139 Field(&Config::value, "pong"))));
5140
5141 video_stream_encoder_->OnBitrateUpdated(
5142 /*target_bitrate=*/DataRate::kbps(50),
5143 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5144 /*link_allocation=*/DataRate::kbps(kDontCare),
5145 /*fraction_lost=*/0,
5146 /*rtt_ms=*/0);
5147
5148 video_stream_encoder_->Stop();
5149}
5150
5151TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5152 constexpr int kSufficientBitrateToNotDrop = 1000;
5153 constexpr int kHighRes = 500;
5154 constexpr int kLowRes = 100;
5155
5156 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5157 video_send_config_.encoder_settings.encoder_switch_request_callback =
5158 &switch_callback;
5159 webrtc::test::ScopedFieldTrials field_trial(
5160 "WebRTC-NetworkCondition-EncoderSwitch/"
5161 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5162 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5163 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5164 encoder_config.codec_type = kVideoCodecH264;
5165
5166 // Reset encoder for new configuration to take effect.
5167 ConfigureEncoder(std::move(encoder_config));
5168
5169 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5170 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5171 // not fail.
5172 video_stream_encoder_->OnBitrateUpdated(
5173 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5174 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5175 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5176 /*fraction_lost=*/0,
5177 /*rtt_ms=*/0);
5178
5179 // Send one frame to trigger ReconfigureEncoder.
5180 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5181 WaitForEncodedFrame(1);
5182
5183 using Config = EncoderSwitchRequestCallback::Config;
5184 EXPECT_CALL(switch_callback,
5185 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5186 Field(&Config::param, "ping"),
5187 Field(&Config::value, "pong"))));
5188
5189 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5190 WaitForEncodedFrame(2);
5191
5192 video_stream_encoder_->Stop();
5193}
5194
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005195TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005196 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005197 const int kFrameWidth = 320;
5198 const int kFrameHeight = 180;
5199
5200 // Set initial rate.
5201 auto rate = DataRate::kbps(100);
5202 video_stream_encoder_->OnBitrateUpdated(
5203 /*target_bitrate=*/rate,
5204 /*stable_target_bitrate=*/rate,
5205 /*link_allocation=*/rate,
5206 /*fraction_lost=*/0,
5207 /*rtt_ms=*/0);
5208
5209 // Insert a first video frame so that encoder gets configured.
5210 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5211 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5212 frame.set_rotation(kVideoRotation_270);
5213 video_source_.IncomingCapturedFrame(frame);
5214 WaitForEncodedFrame(timestamp_ms);
5215 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5216
5217 // Change of target bitrate propagates to the encoder.
5218 auto new_stable_rate = rate - DataRate::kbps(5);
5219 video_stream_encoder_->OnBitrateUpdated(
5220 /*target_bitrate=*/new_stable_rate,
5221 /*stable_target_bitrate=*/new_stable_rate,
5222 /*link_allocation=*/rate,
5223 /*fraction_lost=*/0,
5224 /*rtt_ms=*/0);
5225 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5226 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5227 video_stream_encoder_->Stop();
5228}
5229
5230TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005231 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005232 const int kFrameWidth = 320;
5233 const int kFrameHeight = 180;
5234
5235 // Set initial rate.
5236 auto rate = DataRate::kbps(100);
5237 video_stream_encoder_->OnBitrateUpdated(
5238 /*target_bitrate=*/rate,
5239 /*stable_target_bitrate=*/rate,
5240 /*link_allocation=*/rate,
5241 /*fraction_lost=*/0,
5242 /*rtt_ms=*/0);
5243
5244 // Insert a first video frame so that encoder gets configured.
5245 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5246 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5247 frame.set_rotation(kVideoRotation_270);
5248 video_source_.IncomingCapturedFrame(frame);
5249 WaitForEncodedFrame(timestamp_ms);
5250 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5251
5252 // Set a higher target rate without changing the link_allocation. Should not
5253 // reset encoder's rate.
5254 auto new_stable_rate = rate - DataRate::kbps(5);
5255 video_stream_encoder_->OnBitrateUpdated(
5256 /*target_bitrate=*/rate,
5257 /*stable_target_bitrate=*/new_stable_rate,
5258 /*link_allocation=*/rate,
5259 /*fraction_lost=*/0,
5260 /*rtt_ms=*/0);
5261 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5262 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5263 video_stream_encoder_->Stop();
5264}
5265
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005266TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5267 test::ScopedFieldTrials field_trials(
5268 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5269 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5270 const int kFramerateFps = 30;
5271 const int kWidth = 1920;
5272 const int kHeight = 1080;
5273 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5274 // Works on screenshare mode.
5275 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5276 // We rely on the automatic resolution adaptation, but we handle framerate
5277 // adaptation manually by mocking the stats proxy.
5278 video_source_.set_adaptation_enabled(true);
5279
5280 // BALANCED degradation preference is required for this feature.
5281 video_stream_encoder_->OnBitrateUpdated(
5282 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5283 DataRate::bps(kTargetBitrateBps), 0, 0);
5284 video_stream_encoder_->SetSource(&video_source_,
5285 webrtc::DegradationPreference::BALANCED);
5286 VerifyNoLimitation(video_source_.sink_wants());
5287
5288 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5289 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5290
5291 // Pass enough frames with the full update to trigger animation detection.
5292 for (int i = 0; i < kNumFrames; ++i) {
5293 int64_t timestamp_ms =
5294 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5295 frame.set_ntp_time_ms(timestamp_ms);
5296 frame.set_timestamp_us(timestamp_ms * 1000);
5297 video_source_.IncomingCapturedFrame(frame);
5298 WaitForEncodedFrame(timestamp_ms);
5299 }
5300
5301 // Resolution should be limited.
5302 rtc::VideoSinkWants expected;
5303 expected.max_framerate_fps = kFramerateFps;
5304 expected.max_pixel_count = 1280 * 720 + 1;
5305 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5306
5307 // Pass one frame with no known update.
5308 // Resolution cap should be removed immediately.
5309 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5310 frame.set_ntp_time_ms(timestamp_ms);
5311 frame.set_timestamp_us(timestamp_ms * 1000);
5312 frame.clear_update_rect();
5313
5314 video_source_.IncomingCapturedFrame(frame);
5315 WaitForEncodedFrame(timestamp_ms);
5316
5317 // Resolution should be unlimited now.
5318 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5319
5320 video_stream_encoder_->Stop();
5321}
5322
perkj26091b12016-09-01 01:17:40 -07005323} // namespace webrtc