blob: 068e4dd1df3083a3e9d0003a4bde4fefc0beefd9 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/frame_generator.h"
43#include "test/gmock.h"
44#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020045#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070047
48namespace webrtc {
49
sprangb1ca0732017-02-01 08:38:12 -080050using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080051using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020052using ::testing::AllOf;
53using ::testing::Field;
54using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080055
perkj803d97f2016-11-01 11:45:46 -070056namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020057const int kMinPixelsPerFrame = 320 * 180;
Åsa Perssone644a032019-11-08 15:56:00 +010058const int kQpLow = 1;
59const int kQpHigh = 2;
Åsa Persson8c1bf952018-09-13 10:42:19 +020060const int kMinFramerateFps = 2;
61const int kMinBalancedFramerateFps = 7;
62const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080063const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010064const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020065const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010066const uint32_t kSimulcastTargetBitrateBps = 3150000;
67const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080068const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070069const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020070const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020071const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020072const VideoEncoder::ResolutionBitrateLimits
73 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
74const VideoEncoder::ResolutionBitrateLimits
75 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080076
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020077uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
78 0x00, 0x00, 0x03, 0x03, 0xF4,
79 0x05, 0x03, 0xC7, 0xE0, 0x1B,
80 0x41, 0x10, 0x8D, 0x00};
81
perkj803d97f2016-11-01 11:45:46 -070082class TestBuffer : public webrtc::I420Buffer {
83 public:
84 TestBuffer(rtc::Event* event, int width, int height)
85 : I420Buffer(width, height), event_(event) {}
86
87 private:
88 friend class rtc::RefCountedObject<TestBuffer>;
89 ~TestBuffer() override {
90 if (event_)
91 event_->Set();
92 }
93 rtc::Event* const event_;
94};
95
Noah Richards51db4212019-06-12 06:59:12 -070096// A fake native buffer that can't be converted to I420.
97class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
98 public:
99 FakeNativeBuffer(rtc::Event* event, int width, int height)
100 : event_(event), width_(width), height_(height) {}
101 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
102 int width() const override { return width_; }
103 int height() const override { return height_; }
104 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
105 return nullptr;
106 }
107
108 private:
109 friend class rtc::RefCountedObject<FakeNativeBuffer>;
110 ~FakeNativeBuffer() override {
111 if (event_)
112 event_->Set();
113 }
114 rtc::Event* const event_;
115 const int width_;
116 const int height_;
117};
118
Niels Möller7dc26b72017-12-06 10:27:48 +0100119class CpuOveruseDetectorProxy : public OveruseFrameDetector {
120 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200121 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
122 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100123 last_target_framerate_fps_(-1) {}
124 virtual ~CpuOveruseDetectorProxy() {}
125
126 void OnTargetFramerateUpdated(int framerate_fps) override {
127 rtc::CritScope cs(&lock_);
128 last_target_framerate_fps_ = framerate_fps;
129 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
130 }
131
132 int GetLastTargetFramerate() {
133 rtc::CritScope cs(&lock_);
134 return last_target_framerate_fps_;
135 }
136
Niels Möller4db138e2018-04-19 09:04:13 +0200137 CpuOveruseOptions GetOptions() { return options_; }
138
Niels Möller7dc26b72017-12-06 10:27:48 +0100139 private:
140 rtc::CriticalSection lock_;
141 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
142};
143
mflodmancc3d4422017-08-03 08:27:51 -0700144class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700145 public:
Niels Möller213618e2018-07-24 09:29:58 +0200146 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200147 const VideoStreamEncoderSettings& settings,
148 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100149 : VideoStreamEncoder(Clock::GetRealTimeClock(),
150 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200151 stats_proxy,
152 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200153 std::unique_ptr<OveruseFrameDetector>(
154 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100155 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200156 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700157
sprangb1ca0732017-02-01 08:38:12 -0800158 void PostTaskAndWait(bool down, AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200159 PostTaskAndWait(down, reason, /*expected_results=*/true);
160 }
161
162 void PostTaskAndWait(bool down, AdaptReason reason, bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100163 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200164 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200165 if (down)
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200166 EXPECT_EQ(expected_results, AdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200167 else
168 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700169 event.Set();
170 });
perkj070ba852017-02-16 15:46:27 -0800171 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700172 }
173
kthelgason2fc52542017-03-03 00:24:41 -0800174 // This is used as a synchronisation mechanism, to make sure that the
175 // encoder queue is not blocked before we start sending it frames.
176 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100177 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200178 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800179 ASSERT_TRUE(event.Wait(5000));
180 }
181
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200182 void TriggerCpuOveruse() {
183 PostTaskAndWait(/*down=*/true, AdaptReason::kCpu);
184 }
kthelgason876222f2016-11-29 01:44:11 -0800185
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200186 void TriggerCpuNormalUsage() {
187 PostTaskAndWait(/*down=*/false, AdaptReason::kCpu);
188 }
kthelgason876222f2016-11-29 01:44:11 -0800189
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200190 void TriggerQualityLow() {
191 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality);
192 }
kthelgason876222f2016-11-29 01:44:11 -0800193
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200194 void TriggerQualityLowExpectFalse() {
195 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality,
196 /*expected_results=*/false);
197 }
198
199 void TriggerQualityHigh() {
200 PostTaskAndWait(/*down=*/false, AdaptReason::kQuality);
201 }
sprangfda496a2017-06-15 04:21:07 -0700202
Niels Möller7dc26b72017-12-06 10:27:48 +0100203 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700204};
205
asapersson5f7226f2016-11-25 04:37:00 -0800206class VideoStreamFactory
207 : public VideoEncoderConfig::VideoStreamFactoryInterface {
208 public:
sprangfda496a2017-06-15 04:21:07 -0700209 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
210 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800211 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700212 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800213 }
214
215 private:
216 std::vector<VideoStream> CreateEncoderStreams(
217 int width,
218 int height,
219 const VideoEncoderConfig& encoder_config) override {
220 std::vector<VideoStream> streams =
221 test::CreateVideoStreams(width, height, encoder_config);
222 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100223 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700224 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800225 }
226 return streams;
227 }
sprangfda496a2017-06-15 04:21:07 -0700228
asapersson5f7226f2016-11-25 04:37:00 -0800229 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700230 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800231};
232
Noah Richards51db4212019-06-12 06:59:12 -0700233// Simulates simulcast behavior and makes highest stream resolutions divisible
234// by 4.
235class CroppingVideoStreamFactory
236 : public VideoEncoderConfig::VideoStreamFactoryInterface {
237 public:
238 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
239 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
240 EXPECT_GT(num_temporal_layers, 0u);
241 EXPECT_GT(framerate, 0);
242 }
243
244 private:
245 std::vector<VideoStream> CreateEncoderStreams(
246 int width,
247 int height,
248 const VideoEncoderConfig& encoder_config) override {
249 std::vector<VideoStream> streams = test::CreateVideoStreams(
250 width - width % 4, height - height % 4, encoder_config);
251 for (VideoStream& stream : streams) {
252 stream.num_temporal_layers = num_temporal_layers_;
253 stream.max_framerate = framerate_;
254 }
255 return streams;
256 }
257
258 const size_t num_temporal_layers_;
259 const int framerate_;
260};
261
sprangb1ca0732017-02-01 08:38:12 -0800262class AdaptingFrameForwarder : public test::FrameForwarder {
263 public:
264 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700265 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800266
267 void set_adaptation_enabled(bool enabled) {
268 rtc::CritScope cs(&crit_);
269 adaptation_enabled_ = enabled;
270 }
271
asaperssonfab67072017-04-04 05:51:49 -0700272 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800273 rtc::CritScope cs(&crit_);
274 return adaptation_enabled_;
275 }
276
asapersson09f05612017-05-15 23:40:18 -0700277 rtc::VideoSinkWants last_wants() const {
278 rtc::CritScope cs(&crit_);
279 return last_wants_;
280 }
281
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200282 absl::optional<int> last_sent_width() const { return last_width_; }
283 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800284
sprangb1ca0732017-02-01 08:38:12 -0800285 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
286 int cropped_width = 0;
287 int cropped_height = 0;
288 int out_width = 0;
289 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700290 if (adaption_enabled()) {
291 if (adapter_.AdaptFrameResolution(
292 video_frame.width(), video_frame.height(),
293 video_frame.timestamp_us() * 1000, &cropped_width,
294 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100295 VideoFrame adapted_frame =
296 VideoFrame::Builder()
297 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
298 nullptr, out_width, out_height))
299 .set_timestamp_rtp(99)
300 .set_timestamp_ms(99)
301 .set_rotation(kVideoRotation_0)
302 .build();
sprangc5d62e22017-04-02 23:53:04 -0700303 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
304 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800305 last_width_.emplace(adapted_frame.width());
306 last_height_.emplace(adapted_frame.height());
307 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200308 last_width_ = absl::nullopt;
309 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700310 }
sprangb1ca0732017-02-01 08:38:12 -0800311 } else {
312 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800313 last_width_.emplace(video_frame.width());
314 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800315 }
316 }
317
318 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
319 const rtc::VideoSinkWants& wants) override {
320 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700321 last_wants_ = sink_wants();
sprangc5d62e22017-04-02 23:53:04 -0700322 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
323 wants.max_pixel_count,
324 wants.max_framerate_fps);
sprangb1ca0732017-02-01 08:38:12 -0800325 test::FrameForwarder::AddOrUpdateSink(sink, wants);
326 }
sprangb1ca0732017-02-01 08:38:12 -0800327 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700328 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
329 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200330 absl::optional<int> last_width_;
331 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800332};
sprangc5d62e22017-04-02 23:53:04 -0700333
Niels Möller213618e2018-07-24 09:29:58 +0200334// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700335class MockableSendStatisticsProxy : public SendStatisticsProxy {
336 public:
337 MockableSendStatisticsProxy(Clock* clock,
338 const VideoSendStream::Config& config,
339 VideoEncoderConfig::ContentType content_type)
340 : SendStatisticsProxy(clock, config, content_type) {}
341
342 VideoSendStream::Stats GetStats() override {
343 rtc::CritScope cs(&lock_);
344 if (mock_stats_)
345 return *mock_stats_;
346 return SendStatisticsProxy::GetStats();
347 }
348
Niels Möller213618e2018-07-24 09:29:58 +0200349 int GetInputFrameRate() const override {
350 rtc::CritScope cs(&lock_);
351 if (mock_stats_)
352 return mock_stats_->input_frame_rate;
353 return SendStatisticsProxy::GetInputFrameRate();
354 }
sprangc5d62e22017-04-02 23:53:04 -0700355 void SetMockStats(const VideoSendStream::Stats& stats) {
356 rtc::CritScope cs(&lock_);
357 mock_stats_.emplace(stats);
358 }
359
360 void ResetMockStats() {
361 rtc::CritScope cs(&lock_);
362 mock_stats_.reset();
363 }
364
365 private:
366 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200367 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700368};
369
sprang4847ae62017-06-27 07:06:52 -0700370class MockBitrateObserver : public VideoBitrateAllocationObserver {
371 public:
Erik Språng566124a2018-04-23 12:32:22 +0200372 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700373};
374
perkj803d97f2016-11-01 11:45:46 -0700375} // namespace
376
mflodmancc3d4422017-08-03 08:27:51 -0700377class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700378 public:
379 static const int kDefaultTimeoutMs = 30 * 1000;
380
mflodmancc3d4422017-08-03 08:27:51 -0700381 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700382 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700383 codec_width_(320),
384 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200385 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200386 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700387 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200388 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700389 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700390 Clock::GetRealTimeClock(),
391 video_send_config_,
392 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700393 sink_(&fake_encoder_) {}
394
395 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700396 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700397 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200398 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800399 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200400 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200401 video_send_config_.rtp.payload_name = "FAKE";
402 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700403
Per512ecb32016-09-23 15:52:06 +0200404 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200405 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700406 video_encoder_config.video_stream_factory =
407 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100408 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700409
410 // Framerate limit is specified by the VideoStreamFactory.
411 std::vector<VideoStream> streams =
412 video_encoder_config.video_stream_factory->CreateEncoderStreams(
413 codec_width_, codec_height_, video_encoder_config);
414 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200415 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700416
Niels Möllerf1338562018-04-26 09:51:47 +0200417 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800418 }
419
Niels Möllerf1338562018-04-26 09:51:47 +0200420 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700421 if (video_stream_encoder_)
422 video_stream_encoder_->Stop();
423 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200424 stats_proxy_.get(), video_send_config_.encoder_settings,
425 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700426 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
427 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700428 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700429 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
430 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200431 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700432 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800433 }
434
435 void ResetEncoder(const std::string& payload_name,
436 size_t num_streams,
437 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700438 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700439 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200440 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800441
442 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200443 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800444 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100445 video_encoder_config.max_bitrate_bps =
446 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800447 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700448 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
449 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700450 video_encoder_config.content_type =
451 screenshare ? VideoEncoderConfig::ContentType::kScreen
452 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700453 if (payload_name == "VP9") {
454 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
455 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
456 video_encoder_config.encoder_specific_settings =
457 new rtc::RefCountedObject<
458 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
459 }
Niels Möllerf1338562018-04-26 09:51:47 +0200460 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700461 }
462
sprang57c2fff2017-01-16 06:24:02 -0800463 VideoFrame CreateFrame(int64_t ntp_time_ms,
464 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100465 VideoFrame frame =
466 VideoFrame::Builder()
467 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
468 destruction_event, codec_width_, codec_height_))
469 .set_timestamp_rtp(99)
470 .set_timestamp_ms(99)
471 .set_rotation(kVideoRotation_0)
472 .build();
sprang57c2fff2017-01-16 06:24:02 -0800473 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700474 return frame;
475 }
476
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100477 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
478 rtc::Event* destruction_event,
479 int offset_x) const {
480 VideoFrame frame =
481 VideoFrame::Builder()
482 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
483 destruction_event, codec_width_, codec_height_))
484 .set_timestamp_rtp(99)
485 .set_timestamp_ms(99)
486 .set_rotation(kVideoRotation_0)
487 .set_update_rect({offset_x, 0, 1, 1})
488 .build();
489 frame.set_ntp_time_ms(ntp_time_ms);
490 return frame;
491 }
492
sprang57c2fff2017-01-16 06:24:02 -0800493 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100494 VideoFrame frame =
495 VideoFrame::Builder()
496 .set_video_frame_buffer(
497 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
498 .set_timestamp_rtp(99)
499 .set_timestamp_ms(99)
500 .set_rotation(kVideoRotation_0)
501 .build();
sprang57c2fff2017-01-16 06:24:02 -0800502 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700503 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700504 return frame;
505 }
506
Noah Richards51db4212019-06-12 06:59:12 -0700507 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
508 rtc::Event* destruction_event,
509 int width,
510 int height) const {
511 VideoFrame frame =
512 VideoFrame::Builder()
513 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
514 destruction_event, width, height))
515 .set_timestamp_rtp(99)
516 .set_timestamp_ms(99)
517 .set_rotation(kVideoRotation_0)
518 .build();
519 frame.set_ntp_time_ms(ntp_time_ms);
520 return frame;
521 }
522
523 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
524 rtc::Event* destruction_event) const {
525 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
526 codec_height_);
527 }
528
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100529 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
530 MockBitrateObserver bitrate_observer;
531 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
532
533 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
534 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200535 video_stream_encoder_->OnBitrateUpdated(
536 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
537 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100538
539 video_source_.IncomingCapturedFrame(
540 CreateFrame(1, codec_width_, codec_height_));
541 WaitForEncodedFrame(1);
542 }
543
asapersson02465b82017-04-10 01:12:52 -0700544 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700545 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700546 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
547 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700548 }
549
asapersson09f05612017-05-15 23:40:18 -0700550 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
551 const rtc::VideoSinkWants& wants2) {
552 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
553 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
554 }
555
Åsa Persson8c1bf952018-09-13 10:42:19 +0200556 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
557 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
558 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
559 EXPECT_FALSE(wants.target_pixel_count);
560 }
561
asapersson09f05612017-05-15 23:40:18 -0700562 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
563 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200564 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700565 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
566 EXPECT_GT(wants1.max_pixel_count, 0);
567 }
568
569 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
570 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200571 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700572 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
573 }
574
asaperssonf7e294d2017-06-13 23:25:22 -0700575 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
576 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200577 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700578 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
579 }
580
581 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
582 const rtc::VideoSinkWants& wants2) {
583 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
584 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
585 }
586
587 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
588 const rtc::VideoSinkWants& wants2) {
589 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
590 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
591 }
592
593 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
594 const rtc::VideoSinkWants& wants2) {
595 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
596 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
597 EXPECT_GT(wants1.max_pixel_count, 0);
598 }
599
600 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
601 const rtc::VideoSinkWants& wants2) {
602 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
603 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
604 }
605
asapersson09f05612017-05-15 23:40:18 -0700606 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
607 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200608 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700609 EXPECT_LT(wants.max_pixel_count, pixel_count);
610 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700611 }
612
613 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
614 EXPECT_LT(wants.max_framerate_fps, fps);
615 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
616 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700617 }
618
asaperssonf7e294d2017-06-13 23:25:22 -0700619 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
620 int expected_fps) {
621 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
622 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
623 EXPECT_FALSE(wants.target_pixel_count);
624 }
625
Jonathan Yubc771b72017-12-08 17:04:29 -0800626 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
627 int last_frame_pixels) {
628 // Balanced mode should always scale FPS to the desired range before
629 // attempting to scale resolution.
630 int fps_limit = wants.max_framerate_fps;
631 if (last_frame_pixels <= 320 * 240) {
632 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
633 } else if (last_frame_pixels <= 480 * 270) {
634 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
635 } else if (last_frame_pixels <= 640 * 480) {
636 EXPECT_LE(15, fps_limit);
637 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200638 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800639 }
640 }
641
sprang4847ae62017-06-27 07:06:52 -0700642 void WaitForEncodedFrame(int64_t expected_ntp_time) {
643 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200644 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700645 }
646
647 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
648 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200649 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700650 return ok;
651 }
652
653 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
654 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200655 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700656 }
657
658 void ExpectDroppedFrame() {
659 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200660 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700661 }
662
663 bool WaitForFrame(int64_t timeout_ms) {
664 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200665 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700666 return ok;
667 }
668
perkj26091b12016-09-01 01:17:40 -0700669 class TestEncoder : public test::FakeEncoder {
670 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100671 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700672
asaperssonfab67072017-04-04 05:51:49 -0700673 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800674 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700675 return config_;
676 }
677
678 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800679 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700680 block_next_encode_ = true;
681 }
682
Erik Språngaed30702018-11-05 12:57:17 +0100683 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800684 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100685 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100686 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100687 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100688 info.scaling_settings = VideoEncoder::ScalingSettings(
689 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100690 }
691 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100692 for (int i = 0; i < kMaxSpatialLayers; ++i) {
693 if (temporal_layers_supported_[i]) {
694 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
695 info.fps_allocation[i].resize(num_layers);
696 }
697 }
Erik Språngaed30702018-11-05 12:57:17 +0100698 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200699
700 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Erik Språngaed30702018-11-05 12:57:17 +0100701 return info;
kthelgason876222f2016-11-29 01:44:11 -0800702 }
703
Erik Språngb7cb7b52019-02-26 15:52:33 +0100704 int32_t RegisterEncodeCompleteCallback(
705 EncodedImageCallback* callback) override {
706 rtc::CritScope lock(&local_crit_sect_);
707 encoded_image_callback_ = callback;
708 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
709 }
710
perkjfa10b552016-10-02 23:45:26 -0700711 void ContinueEncode() { continue_encode_event_.Set(); }
712
713 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
714 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800715 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700716 EXPECT_EQ(timestamp_, timestamp);
717 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
718 }
719
kthelgason2fc52542017-03-03 00:24:41 -0800720 void SetQualityScaling(bool b) {
721 rtc::CritScope lock(&local_crit_sect_);
722 quality_scaling_ = b;
723 }
kthelgasonad9010c2017-02-14 00:46:51 -0800724
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100725 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
726 rtc::CritScope lock(&local_crit_sect_);
727 is_hardware_accelerated_ = is_hardware_accelerated;
728 }
729
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100730 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
731 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
732 rtc::CritScope lock(&local_crit_sect_);
733 temporal_layers_supported_[spatial_idx] = supported;
734 }
735
Sergey Silkin6456e352019-07-08 17:56:40 +0200736 void SetResolutionBitrateLimits(
737 std::vector<ResolutionBitrateLimits> thresholds) {
738 rtc::CritScope cs(&local_crit_sect_);
739 resolution_bitrate_limits_ = thresholds;
740 }
741
sprangfe627f32017-03-29 08:24:59 -0700742 void ForceInitEncodeFailure(bool force_failure) {
743 rtc::CritScope lock(&local_crit_sect_);
744 force_init_encode_failed_ = force_failure;
745 }
746
Niels Möller6bb5ab92019-01-11 11:11:10 +0100747 void SimulateOvershoot(double rate_factor) {
748 rtc::CritScope lock(&local_crit_sect_);
749 rate_factor_ = rate_factor;
750 }
751
Erik Språngd7329ca2019-02-21 21:19:53 +0100752 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100753 rtc::CritScope lock(&local_crit_sect_);
754 return last_framerate_;
755 }
756
Erik Språngd7329ca2019-02-21 21:19:53 +0100757 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100758 rtc::CritScope lock(&local_crit_sect_);
759 return last_update_rect_;
760 }
761
Niels Möller87e2d782019-03-07 10:18:23 +0100762 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100763 rtc::CritScope lock(&local_crit_sect_);
764 return last_frame_types_;
765 }
766
767 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100768 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100769 keyframe ? VideoFrameType::kVideoFrameKey
770 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100771 {
772 rtc::CritScope lock(&local_crit_sect_);
773 last_frame_types_ = frame_type;
774 }
Niels Möllerb859b322019-03-07 12:40:01 +0100775 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100776 }
777
Erik Språngb7cb7b52019-02-26 15:52:33 +0100778 void InjectEncodedImage(const EncodedImage& image) {
779 rtc::CritScope lock(&local_crit_sect_);
780 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
781 }
782
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200783 void InjectEncodedImage(const EncodedImage& image,
784 const CodecSpecificInfo* codec_specific_info,
785 const RTPFragmentationHeader* fragmentation) {
786 rtc::CritScope lock(&local_crit_sect_);
787 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
788 fragmentation);
789 }
790
Erik Språngd7329ca2019-02-21 21:19:53 +0100791 void ExpectNullFrame() {
792 rtc::CritScope lock(&local_crit_sect_);
793 expect_null_frame_ = true;
794 }
795
Erik Språng5056af02019-09-02 15:53:11 +0200796 absl::optional<VideoEncoder::RateControlParameters>
797 GetAndResetLastRateControlSettings() {
798 auto settings = last_rate_control_settings_;
799 last_rate_control_settings_.reset();
800 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100801 }
802
Sergey Silkin5ee69672019-07-02 14:18:34 +0200803 int GetNumEncoderInitializations() const {
804 rtc::CritScope lock(&local_crit_sect_);
805 return num_encoder_initializations_;
806 }
807
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200808 int GetNumSetRates() const {
809 rtc::CritScope lock(&local_crit_sect_);
810 return num_set_rates_;
811 }
812
perkjfa10b552016-10-02 23:45:26 -0700813 private:
perkj26091b12016-09-01 01:17:40 -0700814 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100815 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700816 bool block_encode;
817 {
brandtre78d2662017-01-16 05:57:16 -0800818 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100819 if (expect_null_frame_) {
820 EXPECT_EQ(input_image.timestamp(), 0u);
821 EXPECT_EQ(input_image.width(), 1);
822 last_frame_types_ = *frame_types;
823 expect_null_frame_ = false;
824 } else {
825 EXPECT_GT(input_image.timestamp(), timestamp_);
826 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
827 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
828 }
perkj26091b12016-09-01 01:17:40 -0700829
830 timestamp_ = input_image.timestamp();
831 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700832 last_input_width_ = input_image.width();
833 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700834 block_encode = block_next_encode_;
835 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100836 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100837 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700838 }
Niels Möllerb859b322019-03-07 12:40:01 +0100839 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700840 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700841 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700842 return result;
843 }
844
sprangfe627f32017-03-29 08:24:59 -0700845 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200846 const Settings& settings) override {
847 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200848
sprangfe627f32017-03-29 08:24:59 -0700849 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100850 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200851
852 ++num_encoder_initializations_;
853
Erik Språng82fad3d2018-03-21 09:57:23 +0100854 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700855 // Simulate setting up temporal layers, in order to validate the life
856 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100857 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200858 frame_buffer_controller_ =
859 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700860 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100861 if (force_init_encode_failed_) {
862 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700863 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100864 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100865
Erik Språngb7cb7b52019-02-26 15:52:33 +0100866 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700867 return res;
868 }
869
Erik Språngb7cb7b52019-02-26 15:52:33 +0100870 int32_t Release() override {
871 rtc::CritScope lock(&local_crit_sect_);
872 EXPECT_NE(initialized_, EncoderState::kUninitialized);
873 initialized_ = EncoderState::kUninitialized;
874 return FakeEncoder::Release();
875 }
876
Erik Språng16cb8f52019-04-12 13:59:09 +0200877 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100878 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200879 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100880 VideoBitrateAllocation adjusted_rate_allocation;
881 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
882 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200883 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100884 adjusted_rate_allocation.SetBitrate(
885 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200886 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100887 rate_factor_));
888 }
889 }
890 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200891 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200892 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200893 RateControlParameters adjusted_paramters = parameters;
894 adjusted_paramters.bitrate = adjusted_rate_allocation;
895 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100896 }
897
brandtre78d2662017-01-16 05:57:16 -0800898 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100899 enum class EncoderState {
900 kUninitialized,
901 kInitializationFailed,
902 kInitialized
903 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
904 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700905 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700906 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700907 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
908 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
909 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
910 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
911 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100912 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100913 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700914 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100915 absl::optional<bool>
916 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
917 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700918 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100919 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
920 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200921 absl::optional<VideoEncoder::RateControlParameters>
922 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100923 VideoFrame::UpdateRect last_update_rect_
924 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100925 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100926 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100927 EncodedImageCallback* encoded_image_callback_
928 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200929 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200930 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200931 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
932 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200933 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700934 };
935
mflodmancc3d4422017-08-03 08:27:51 -0700936 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700937 public:
938 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100939 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700940
perkj26091b12016-09-01 01:17:40 -0700941 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700942 EXPECT_TRUE(
943 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
944 }
945
946 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
947 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700948 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700949 if (!encoded_frame_event_.Wait(timeout_ms))
950 return false;
perkj26091b12016-09-01 01:17:40 -0700951 {
952 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800953 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700954 }
955 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700956 return true;
perkj26091b12016-09-01 01:17:40 -0700957 }
958
sprangb1ca0732017-02-01 08:38:12 -0800959 void WaitForEncodedFrame(uint32_t expected_width,
960 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700961 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100962 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700963 }
964
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100965 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700966 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800967 uint32_t width = 0;
968 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800969 {
970 rtc::CritScope lock(&crit_);
971 width = last_width_;
972 height = last_height_;
973 }
974 EXPECT_EQ(expected_height, height);
975 EXPECT_EQ(expected_width, width);
976 }
977
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +0200978 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
979 VideoRotation rotation;
980 {
981 rtc::CritScope lock(&crit_);
982 rotation = last_rotation_;
983 }
984 EXPECT_EQ(expected_rotation, rotation);
985 }
986
kthelgason2fc52542017-03-03 00:24:41 -0800987 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -0800988
sprangc5d62e22017-04-02 23:53:04 -0700989 bool WaitForFrame(int64_t timeout_ms) {
990 return encoded_frame_event_.Wait(timeout_ms);
991 }
992
perkj26091b12016-09-01 01:17:40 -0700993 void SetExpectNoFrames() {
994 rtc::CritScope lock(&crit_);
995 expect_frames_ = false;
996 }
997
asaperssonfab67072017-04-04 05:51:49 -0700998 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +0200999 rtc::CritScope lock(&crit_);
1000 return number_of_reconfigurations_;
1001 }
1002
asaperssonfab67072017-04-04 05:51:49 -07001003 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001004 rtc::CritScope lock(&crit_);
1005 return min_transmit_bitrate_bps_;
1006 }
1007
Erik Språngd7329ca2019-02-21 21:19:53 +01001008 void SetNumExpectedLayers(size_t num_layers) {
1009 rtc::CritScope lock(&crit_);
1010 num_expected_layers_ = num_layers;
1011 }
1012
Erik Språngb7cb7b52019-02-26 15:52:33 +01001013 int64_t GetLastCaptureTimeMs() const {
1014 rtc::CritScope lock(&crit_);
1015 return last_capture_time_ms_;
1016 }
1017
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001018 std::vector<uint8_t> GetLastEncodedImageData() {
1019 rtc::CritScope lock(&crit_);
1020 return std::move(last_encoded_image_data_);
1021 }
1022
1023 RTPFragmentationHeader GetLastFragmentation() {
1024 rtc::CritScope lock(&crit_);
1025 return std::move(last_fragmentation_);
1026 }
1027
perkj26091b12016-09-01 01:17:40 -07001028 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001029 Result OnEncodedImage(
1030 const EncodedImage& encoded_image,
1031 const CodecSpecificInfo* codec_specific_info,
1032 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001033 rtc::CritScope lock(&crit_);
1034 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001035 last_encoded_image_data_ = std::vector<uint8_t>(
1036 encoded_image.data(), encoded_image.data() + encoded_image.size());
1037 if (fragmentation) {
1038 last_fragmentation_.CopyFrom(*fragmentation);
1039 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001040 uint32_t timestamp = encoded_image.Timestamp();
1041 if (last_timestamp_ != timestamp) {
1042 num_received_layers_ = 1;
1043 } else {
1044 ++num_received_layers_;
1045 }
1046 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001047 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001048 last_width_ = encoded_image._encodedWidth;
1049 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001050 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001051 if (num_received_layers_ == num_expected_layers_) {
1052 encoded_frame_event_.Set();
1053 }
sprangb1ca0732017-02-01 08:38:12 -08001054 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001055 }
1056
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001057 void OnEncoderConfigurationChanged(
1058 std::vector<VideoStream> streams,
1059 VideoEncoderConfig::ContentType content_type,
1060 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001061 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001062 ++number_of_reconfigurations_;
1063 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1064 }
1065
perkj26091b12016-09-01 01:17:40 -07001066 rtc::CriticalSection crit_;
1067 TestEncoder* test_encoder_;
1068 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001069 std::vector<uint8_t> last_encoded_image_data_;
1070 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001071 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001072 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001073 uint32_t last_height_ = 0;
1074 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001075 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001076 size_t num_expected_layers_ = 1;
1077 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001078 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001079 int number_of_reconfigurations_ = 0;
1080 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001081 };
1082
Sergey Silkin5ee69672019-07-02 14:18:34 +02001083 class VideoBitrateAllocatorProxyFactory
1084 : public VideoBitrateAllocatorFactory {
1085 public:
1086 VideoBitrateAllocatorProxyFactory()
1087 : bitrate_allocator_factory_(
1088 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1089
1090 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1091 const VideoCodec& codec) override {
1092 rtc::CritScope lock(&crit_);
1093 codec_config_ = codec;
1094 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1095 }
1096
1097 VideoCodec codec_config() const {
1098 rtc::CritScope lock(&crit_);
1099 return codec_config_;
1100 }
1101
1102 private:
1103 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1104
1105 rtc::CriticalSection crit_;
1106 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1107 };
1108
perkj26091b12016-09-01 01:17:40 -07001109 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001110 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001111 int codec_width_;
1112 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001113 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001114 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001115 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001116 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001117 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001118 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001119 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001120 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001121 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001122 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001123};
1124
mflodmancc3d4422017-08-03 08:27:51 -07001125TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001126 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001127 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1128 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001129 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001130 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001131 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001132 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001133 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001134}
1135
mflodmancc3d4422017-08-03 08:27:51 -07001136TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001137 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001138 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001139 // The encoder will cache up to one frame for a short duration. Adding two
1140 // frames means that the first frame will be dropped and the second frame will
1141 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001142 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001143 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001144 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001145
Erik Språng4c6ca302019-04-08 15:14:01 +02001146 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001147 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1148 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001149
Sebastian Janssona3177052018-04-10 13:05:49 +02001150 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001151 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001152 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1153
1154 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001155 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001156}
1157
mflodmancc3d4422017-08-03 08:27:51 -07001158TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001159 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001160 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1161 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001162 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001163 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001164
Florent Castellia8336d32019-09-09 13:36:55 +02001165 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1166 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001167 // The encoder will cache up to one frame for a short duration. Adding two
1168 // frames means that the first frame will be dropped and the second frame will
1169 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001170 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001171 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001172
Erik Språng4c6ca302019-04-08 15:14:01 +02001173 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001174 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1175 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001176 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001177 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1178 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001179 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001180}
1181
mflodmancc3d4422017-08-03 08:27:51 -07001182TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001183 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001184 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1185 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001186 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001187 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001188
1189 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001190 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001191
perkja49cbd32016-09-16 07:53:41 -07001192 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001193 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001194 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001195}
1196
mflodmancc3d4422017-08-03 08:27:51 -07001197TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001198 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001199 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1200 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001201
perkja49cbd32016-09-16 07:53:41 -07001202 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001203 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001204
mflodmancc3d4422017-08-03 08:27:51 -07001205 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001206 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001207 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001208 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1209 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001210}
1211
mflodmancc3d4422017-08-03 08:27:51 -07001212TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001213 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001214 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1215 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001216
1217 fake_encoder_.BlockNextEncode();
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 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1221 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001222 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1223 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001224 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001225 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001226
mflodmancc3d4422017-08-03 08:27:51 -07001227 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001228}
1229
Noah Richards51db4212019-06-12 06:59:12 -07001230TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1231 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001232 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1233 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001234
1235 rtc::Event frame_destroyed_event;
1236 video_source_.IncomingCapturedFrame(
1237 CreateFakeNativeFrame(1, &frame_destroyed_event));
1238 ExpectDroppedFrame();
1239 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1240 video_stream_encoder_->Stop();
1241}
1242
1243TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1244 // Use the cropping factory.
1245 video_encoder_config_.video_stream_factory =
1246 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1247 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1248 kMaxPayloadLength);
1249 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1250
1251 // Capture a frame at codec_width_/codec_height_.
1252 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001253 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1254 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001255 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1256 WaitForEncodedFrame(1);
1257 // The encoder will have been configured once.
1258 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1259 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1260 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1261
1262 // Now send in a fake frame that needs to be cropped as the width/height
1263 // aren't divisible by 4 (see CreateEncoderStreams above).
1264 rtc::Event frame_destroyed_event;
1265 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1266 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1267 ExpectDroppedFrame();
1268 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1269 video_stream_encoder_->Stop();
1270}
1271
mflodmancc3d4422017-08-03 08:27:51 -07001272TEST_F(VideoStreamEncoderTest,
1273 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001274 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001275 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1276 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001277 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001278
1279 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001280 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001281 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001282 // The encoder will have been configured once when the first frame is
1283 // received.
1284 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001285
1286 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001287 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001288 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001289 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001290 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001291
1292 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001293 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001294 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001295 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001296 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001297
mflodmancc3d4422017-08-03 08:27:51 -07001298 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001299}
1300
mflodmancc3d4422017-08-03 08:27:51 -07001301TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001302 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001303 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1304 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001305
1306 // Capture a frame and wait for it to synchronize with the encoder thread.
1307 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001308 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001309 // The encoder will have been configured once.
1310 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001311 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1312 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1313
1314 codec_width_ *= 2;
1315 codec_height_ *= 2;
1316 // Capture a frame with a higher resolution and wait for it to synchronize
1317 // with the encoder thread.
1318 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001319 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001320 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1321 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001322 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001323
mflodmancc3d4422017-08-03 08:27:51 -07001324 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001325}
1326
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001327TEST_F(VideoStreamEncoderTest,
1328 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1329 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001330 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1331 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001332
1333 // Capture a frame and wait for it to synchronize with the encoder thread.
1334 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1335 WaitForEncodedFrame(1);
1336
1337 VideoEncoderConfig video_encoder_config;
1338 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1339 // Changing the max payload data length recreates encoder.
1340 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1341 kMaxPayloadLength / 2);
1342
1343 // Capture a frame and wait for it to synchronize with the encoder thread.
1344 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1345 WaitForEncodedFrame(2);
1346 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1347
1348 video_stream_encoder_->Stop();
1349}
1350
Sergey Silkin5ee69672019-07-02 14:18:34 +02001351TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1352 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001353 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1354 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001355
1356 VideoEncoderConfig video_encoder_config;
1357 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1358 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1359 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1360 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1361 kMaxPayloadLength);
1362
1363 // Capture a frame and wait for it to synchronize with the encoder thread.
1364 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1365 WaitForEncodedFrame(1);
1366 // The encoder will have been configured once when the first frame is
1367 // received.
1368 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1369 EXPECT_EQ(kTargetBitrateBps,
1370 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1371 EXPECT_EQ(kStartBitrateBps,
1372 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1373
Sergey Silkin6456e352019-07-08 17:56:40 +02001374 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1375 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001376 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1377 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1378 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1379 kMaxPayloadLength);
1380
1381 // Capture a frame and wait for it to synchronize with the encoder thread.
1382 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1383 WaitForEncodedFrame(2);
1384 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1385 // Bitrate limits have changed - rate allocator should be reconfigured,
1386 // encoder should not be reconfigured.
1387 EXPECT_EQ(kTargetBitrateBps * 2,
1388 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1389 EXPECT_EQ(kStartBitrateBps * 2,
1390 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1391 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1392
1393 video_stream_encoder_->Stop();
1394}
1395
Sergey Silkin6456e352019-07-08 17:56:40 +02001396TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001397 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001398 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001399 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1400 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001401
Sergey Silkin6456e352019-07-08 17:56:40 +02001402 VideoEncoderConfig video_encoder_config;
1403 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1404 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001405 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001406 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1407 kMaxPayloadLength);
1408
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001409 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001410 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001411
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001412 // Get the default bitrate limits and use them as baseline for custom
1413 // application and encoder recommended limits.
1414 const uint32_t kDefaultMinBitrateKbps =
1415 bitrate_allocator_factory_.codec_config().minBitrate;
1416 const uint32_t kDefaultMaxBitrateKbps =
1417 bitrate_allocator_factory_.codec_config().maxBitrate;
1418 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1419 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1420 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1421 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1422
1423 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1424 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1425 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1426 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1427
1428 // Change resolution. This will trigger encoder re-configuration and video
1429 // stream encoder will pick up the bitrate limits recommended by encoder.
1430 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1431 WaitForEncodedFrame(2);
1432 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1433 WaitForEncodedFrame(3);
1434
1435 // App bitrate limits are not set - bitrate limits recommended by encoder
1436 // should be used.
1437 EXPECT_EQ(kEncMaxBitrateKbps,
1438 bitrate_allocator_factory_.codec_config().maxBitrate);
1439 EXPECT_EQ(kEncMinBitrateKbps,
1440 bitrate_allocator_factory_.codec_config().minBitrate);
1441
1442 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1443 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1444 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1445 kMaxPayloadLength);
1446 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1447 WaitForEncodedFrame(4);
1448
1449 // App limited the max bitrate - bitrate limits recommended by encoder should
1450 // not be applied.
1451 EXPECT_EQ(kAppMaxBitrateKbps,
1452 bitrate_allocator_factory_.codec_config().maxBitrate);
1453 EXPECT_EQ(kDefaultMinBitrateKbps,
1454 bitrate_allocator_factory_.codec_config().minBitrate);
1455
1456 video_encoder_config.max_bitrate_bps = 0;
1457 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1458 kAppMinBitrateKbps * 1000;
1459 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1460 kMaxPayloadLength);
1461 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1462 WaitForEncodedFrame(5);
1463
1464 // App limited the min bitrate - bitrate limits recommended by encoder should
1465 // not be applied.
1466 EXPECT_EQ(kDefaultMaxBitrateKbps,
1467 bitrate_allocator_factory_.codec_config().maxBitrate);
1468 EXPECT_EQ(kAppMinBitrateKbps,
1469 bitrate_allocator_factory_.codec_config().minBitrate);
1470
1471 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1472 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1473 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001474 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1475 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001476 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1477 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001478
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001479 // App limited both min and max bitrates - bitrate limits recommended by
1480 // encoder should not be applied.
1481 EXPECT_EQ(kAppMaxBitrateKbps,
1482 bitrate_allocator_factory_.codec_config().maxBitrate);
1483 EXPECT_EQ(kAppMinBitrateKbps,
1484 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001485
1486 video_stream_encoder_->Stop();
1487}
1488
1489TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001490 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001491 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001492 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1493 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001494
1495 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001496 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001497 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001498 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001499 fake_encoder_.SetResolutionBitrateLimits(
1500 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1501
1502 VideoEncoderConfig video_encoder_config;
1503 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1504 video_encoder_config.max_bitrate_bps = 0;
1505 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1506 kMaxPayloadLength);
1507
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001508 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001509 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1510 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001511 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1512 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001513 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1514 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1515
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001516 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001517 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1518 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001519 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1520 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001521 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1522 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1523
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001524 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001525 // encoder for 360p should be used.
1526 video_source_.IncomingCapturedFrame(
1527 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1528 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001529 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1530 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001531 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1532 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1533
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001534 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001535 // ignored.
1536 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1537 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001538 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1539 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001540 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1541 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001542 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1543 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001544 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1545 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1546
1547 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1548 // for 270p should be used.
1549 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1550 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001551 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.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_270p.max_bitrate_bps),
1554 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1555
1556 video_stream_encoder_->Stop();
1557}
1558
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001559TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1560 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001561 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1562 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001563
1564 VideoEncoderConfig video_encoder_config;
1565 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1566 video_encoder_config.max_bitrate_bps = 0;
1567 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1568 kMaxPayloadLength);
1569
1570 // Encode 720p frame to get the default encoder target bitrate.
1571 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1572 WaitForEncodedFrame(1);
1573 const uint32_t kDefaultTargetBitrateFor720pKbps =
1574 bitrate_allocator_factory_.codec_config()
1575 .simulcastStream[0]
1576 .targetBitrate;
1577
1578 // Set the max recommended encoder bitrate to something lower than the default
1579 // target bitrate.
1580 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1581 1280 * 720, 10 * 1000, 10 * 1000,
1582 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1583 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1584
1585 // Change resolution to trigger encoder reinitialization.
1586 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1587 WaitForEncodedFrame(2);
1588 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1589 WaitForEncodedFrame(3);
1590
1591 // Ensure the target bitrate is capped by the max bitrate.
1592 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1593 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1594 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1595 .simulcastStream[0]
1596 .targetBitrate *
1597 1000,
1598 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1599
1600 video_stream_encoder_->Stop();
1601}
1602
mflodmancc3d4422017-08-03 08:27:51 -07001603TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001604 EXPECT_TRUE(video_source_.has_sinks());
1605 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001606 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001607 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001608 EXPECT_FALSE(video_source_.has_sinks());
1609 EXPECT_TRUE(new_video_source.has_sinks());
1610
mflodmancc3d4422017-08-03 08:27:51 -07001611 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001612}
1613
mflodmancc3d4422017-08-03 08:27:51 -07001614TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001615 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001616 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001617 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001618 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001619}
1620
Jonathan Yubc771b72017-12-08 17:04:29 -08001621TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1622 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001623 const int kWidth = 1280;
1624 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001625
1626 // We rely on the automatic resolution adaptation, but we handle framerate
1627 // adaptation manually by mocking the stats proxy.
1628 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001629
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001630 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001631 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001632 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1633 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001634 video_stream_encoder_->SetSource(&video_source_,
1635 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001636 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001637 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001638 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001639 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1640
Jonathan Yubc771b72017-12-08 17:04:29 -08001641 // Adapt down as far as possible.
1642 rtc::VideoSinkWants last_wants;
1643 int64_t t = 1;
1644 int loop_count = 0;
1645 do {
1646 ++loop_count;
1647 last_wants = video_source_.sink_wants();
1648
1649 // Simulate the framerate we've been asked to adapt to.
1650 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1651 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1652 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1653 mock_stats.input_frame_rate = fps;
1654 stats_proxy_->SetMockStats(mock_stats);
1655
1656 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1657 sink_.WaitForEncodedFrame(t);
1658 t += frame_interval_ms;
1659
mflodmancc3d4422017-08-03 08:27:51 -07001660 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001661 VerifyBalancedModeFpsRange(
1662 video_source_.sink_wants(),
1663 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1664 } while (video_source_.sink_wants().max_pixel_count <
1665 last_wants.max_pixel_count ||
1666 video_source_.sink_wants().max_framerate_fps <
1667 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001668
Jonathan Yubc771b72017-12-08 17:04:29 -08001669 // Verify that we've adapted all the way down.
1670 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001671 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001672 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1673 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001674 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001675 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1676 *video_source_.last_sent_height());
1677 EXPECT_EQ(kMinBalancedFramerateFps,
1678 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001679
Jonathan Yubc771b72017-12-08 17:04:29 -08001680 // Adapt back up the same number of times we adapted down.
1681 for (int i = 0; i < loop_count - 1; ++i) {
1682 last_wants = video_source_.sink_wants();
1683
1684 // Simulate the framerate we've been asked to adapt to.
1685 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1686 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1687 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1688 mock_stats.input_frame_rate = fps;
1689 stats_proxy_->SetMockStats(mock_stats);
1690
1691 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1692 sink_.WaitForEncodedFrame(t);
1693 t += frame_interval_ms;
1694
mflodmancc3d4422017-08-03 08:27:51 -07001695 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001696 VerifyBalancedModeFpsRange(
1697 video_source_.sink_wants(),
1698 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1699 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1700 last_wants.max_pixel_count ||
1701 video_source_.sink_wants().max_framerate_fps >
1702 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001703 }
1704
Åsa Persson8c1bf952018-09-13 10:42:19 +02001705 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001706 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001707 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001708 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1709 EXPECT_EQ((loop_count - 1) * 2,
1710 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001711
mflodmancc3d4422017-08-03 08:27:51 -07001712 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001713}
mflodmancc3d4422017-08-03 08:27:51 -07001714TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001715 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001716 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1717 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001718 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001719
sprangc5d62e22017-04-02 23:53:04 -07001720 const int kFrameWidth = 1280;
1721 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001722
Åsa Persson8c1bf952018-09-13 10:42:19 +02001723 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001724
kthelgason5e13d412016-12-01 03:59:51 -08001725 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001726 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001727 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001728 frame_timestamp += kFrameIntervalMs;
1729
perkj803d97f2016-11-01 11:45:46 -07001730 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001731 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001732 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001733 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001734 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001735 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001736
asapersson0944a802017-04-07 00:57:58 -07001737 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001738 // wanted resolution.
1739 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1740 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1741 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001742 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001743
1744 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001745 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001746 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001747 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001748
sprangc5d62e22017-04-02 23:53:04 -07001749 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001750 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001751
sprangc5d62e22017-04-02 23:53:04 -07001752 // Force an input frame rate to be available, or the adaptation call won't
1753 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001754 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001755 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001756 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001757 stats_proxy_->SetMockStats(stats);
1758
mflodmancc3d4422017-08-03 08:27:51 -07001759 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001760 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001761 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001762 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001763 frame_timestamp += kFrameIntervalMs;
1764
1765 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001766 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001767 EXPECT_EQ(std::numeric_limits<int>::max(),
1768 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001769 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001770
asapersson02465b82017-04-10 01:12:52 -07001771 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001772 video_stream_encoder_->SetSource(&new_video_source,
1773 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001774 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001775
mflodmancc3d4422017-08-03 08:27:51 -07001776 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001777 new_video_source.IncomingCapturedFrame(
1778 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001779 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001780 frame_timestamp += kFrameIntervalMs;
1781
1782 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001783 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001784
1785 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001786 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001787 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001788 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1789 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001790 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001791 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001792
1793 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001794 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001795 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001796 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1797 EXPECT_EQ(std::numeric_limits<int>::max(),
1798 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001799 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001800
mflodmancc3d4422017-08-03 08:27:51 -07001801 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001802}
1803
mflodmancc3d4422017-08-03 08:27:51 -07001804TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001805 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001806 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1807 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001808
asaperssonfab67072017-04-04 05:51:49 -07001809 const int kWidth = 1280;
1810 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001811 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001812 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001813 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1814 EXPECT_FALSE(stats.bw_limited_resolution);
1815 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1816
1817 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001818 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001819 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001820 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001821
1822 stats = stats_proxy_->GetStats();
1823 EXPECT_TRUE(stats.bw_limited_resolution);
1824 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1825
1826 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001827 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001828 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001829 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001830
1831 stats = stats_proxy_->GetStats();
1832 EXPECT_FALSE(stats.bw_limited_resolution);
1833 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1834 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1835
mflodmancc3d4422017-08-03 08:27:51 -07001836 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001837}
1838
mflodmancc3d4422017-08-03 08:27:51 -07001839TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001840 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001841 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1842 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001843
1844 const int kWidth = 1280;
1845 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001846 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001847 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001848 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1849 EXPECT_FALSE(stats.cpu_limited_resolution);
1850 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1851
1852 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001853 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001854 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001855 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001856
1857 stats = stats_proxy_->GetStats();
1858 EXPECT_TRUE(stats.cpu_limited_resolution);
1859 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1860
1861 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001862 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001863 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001864 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001865
1866 stats = stats_proxy_->GetStats();
1867 EXPECT_FALSE(stats.cpu_limited_resolution);
1868 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001869 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001870
mflodmancc3d4422017-08-03 08:27:51 -07001871 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001872}
1873
mflodmancc3d4422017-08-03 08:27:51 -07001874TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001875 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001876 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1877 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001878
asaperssonfab67072017-04-04 05:51:49 -07001879 const int kWidth = 1280;
1880 const int kHeight = 720;
1881 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001882 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001883 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001884 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001885 EXPECT_FALSE(stats.cpu_limited_resolution);
1886 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1887
asaperssonfab67072017-04-04 05:51:49 -07001888 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001889 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001890 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001891 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001892 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001893 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001894 EXPECT_TRUE(stats.cpu_limited_resolution);
1895 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1896
1897 // Set new source with adaptation still enabled.
1898 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001899 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001900 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001901
asaperssonfab67072017-04-04 05:51:49 -07001902 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001903 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001904 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001905 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001906 EXPECT_TRUE(stats.cpu_limited_resolution);
1907 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1908
1909 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001910 video_stream_encoder_->SetSource(&new_video_source,
1911 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001912
asaperssonfab67072017-04-04 05:51:49 -07001913 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001914 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001915 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001916 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001917 EXPECT_FALSE(stats.cpu_limited_resolution);
1918 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1919
1920 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001921 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001922 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001923
asaperssonfab67072017-04-04 05:51:49 -07001924 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001925 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001926 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001927 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001928 EXPECT_TRUE(stats.cpu_limited_resolution);
1929 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1930
asaperssonfab67072017-04-04 05:51:49 -07001931 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001932 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001933 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001934 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001935 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001936 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001937 EXPECT_FALSE(stats.cpu_limited_resolution);
1938 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001939 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001940
mflodmancc3d4422017-08-03 08:27:51 -07001941 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001942}
1943
mflodmancc3d4422017-08-03 08:27:51 -07001944TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001945 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001946 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1947 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001948
asaperssonfab67072017-04-04 05:51:49 -07001949 const int kWidth = 1280;
1950 const int kHeight = 720;
1951 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001952 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001953 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001954 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001955 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001956 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001957
1958 // Set new source with adaptation still enabled.
1959 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001960 video_stream_encoder_->SetSource(&new_video_source,
1961 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001962
asaperssonfab67072017-04-04 05:51:49 -07001963 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001964 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001965 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001966 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001967 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001968 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001969
asaperssonfab67072017-04-04 05:51:49 -07001970 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001971 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001972 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001973 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001974 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001975 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001976 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001977 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001978
asaperssonfab67072017-04-04 05:51:49 -07001979 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001980 video_stream_encoder_->SetSource(&new_video_source,
1981 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001982
asaperssonfab67072017-04-04 05:51:49 -07001983 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001984 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001985 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001986 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001987 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001988 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001989
asapersson02465b82017-04-10 01:12:52 -07001990 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07001991 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001992 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08001993
asaperssonfab67072017-04-04 05:51:49 -07001994 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001995 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001996 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001997 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001998 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001999 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2000 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002001
mflodmancc3d4422017-08-03 08:27:51 -07002002 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002003}
2004
mflodmancc3d4422017-08-03 08:27:51 -07002005TEST_F(VideoStreamEncoderTest,
2006 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002007 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002008 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2009 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002010
2011 const int kWidth = 1280;
2012 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002013 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002014 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002015 video_source_.IncomingCapturedFrame(
2016 CreateFrame(timestamp_ms, kWidth, kHeight));
2017 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002018 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2019 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2020 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2021
2022 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002023 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002024 timestamp_ms += kFrameIntervalMs;
2025 video_source_.IncomingCapturedFrame(
2026 CreateFrame(timestamp_ms, kWidth, kHeight));
2027 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002028 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2029 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2030 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2031
2032 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002033 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002034 timestamp_ms += kFrameIntervalMs;
2035 video_source_.IncomingCapturedFrame(
2036 CreateFrame(timestamp_ms, kWidth, kHeight));
2037 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002038 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2039 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2040 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2041
Niels Möller4db138e2018-04-19 09:04:13 +02002042 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002043 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002044
2045 VideoEncoderConfig video_encoder_config;
2046 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2047 // Make format different, to force recreation of encoder.
2048 video_encoder_config.video_format.parameters["foo"] = "foo";
2049 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002050 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002051 timestamp_ms += kFrameIntervalMs;
2052 video_source_.IncomingCapturedFrame(
2053 CreateFrame(timestamp_ms, kWidth, kHeight));
2054 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002055 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2056 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2057 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2058
mflodmancc3d4422017-08-03 08:27:51 -07002059 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002060}
2061
mflodmancc3d4422017-08-03 08:27:51 -07002062TEST_F(VideoStreamEncoderTest,
2063 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002064 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002065 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2066 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002067
asapersson0944a802017-04-07 00:57:58 -07002068 const int kWidth = 1280;
2069 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002070 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002071
asaperssonfab67072017-04-04 05:51:49 -07002072 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002073 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002074 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002075 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002076 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002077 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2078
asapersson02465b82017-04-10 01:12:52 -07002079 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002080 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002081 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002082 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002083 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002084 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002085 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002086 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2087
2088 // Set new source with adaptation still enabled.
2089 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002090 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002091 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002092
2093 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002094 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002095 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002096 stats = stats_proxy_->GetStats();
2097 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002098 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002099 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2100
sprangc5d62e22017-04-02 23:53:04 -07002101 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002102 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002103 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002104 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002105 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002106 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002107 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002108 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002109 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002110 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002111 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2112
sprangc5d62e22017-04-02 23:53:04 -07002113 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002114 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002115 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2116 mock_stats.input_frame_rate = 30;
2117 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002118 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002119 stats_proxy_->ResetMockStats();
2120
2121 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002122 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002123 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002124
2125 // Framerate now adapted.
2126 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002127 EXPECT_FALSE(stats.cpu_limited_resolution);
2128 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002129 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2130
2131 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002132 video_stream_encoder_->SetSource(&new_video_source,
2133 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002134 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002135 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002136 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002137
2138 stats = stats_proxy_->GetStats();
2139 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002140 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002141 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2142
2143 // Try to trigger overuse. Should not succeed.
2144 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002145 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002146 stats_proxy_->ResetMockStats();
2147
2148 stats = stats_proxy_->GetStats();
2149 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002150 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002151 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2152
2153 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002154 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002155 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002156 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002157 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002158 stats = stats_proxy_->GetStats();
2159 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002160 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002161 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002162
2163 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002164 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002165 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002166 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002167 stats = stats_proxy_->GetStats();
2168 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002169 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002170 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2171
2172 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002173 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002174 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002175 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002176 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002177 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002178 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002179 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002180 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002181 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002182 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2183
2184 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002185 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002186 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002187 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002188 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002189 stats = stats_proxy_->GetStats();
2190 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002191 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002192 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002193 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002194
mflodmancc3d4422017-08-03 08:27:51 -07002195 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002196}
2197
mflodmancc3d4422017-08-03 08:27:51 -07002198TEST_F(VideoStreamEncoderTest,
2199 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002200 const int kWidth = 1280;
2201 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002202 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002203 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2204 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002205
asaperssonfab67072017-04-04 05:51:49 -07002206 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002207 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002208
asaperssonfab67072017-04-04 05:51:49 -07002209 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002210 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002211
asaperssonfab67072017-04-04 05:51:49 -07002212 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002213 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002214
asaperssonfab67072017-04-04 05:51:49 -07002215 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002216 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002217
kthelgason876222f2016-11-29 01:44:11 -08002218 // Expect a scale down.
2219 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002220 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002221
asapersson02465b82017-04-10 01:12:52 -07002222 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002223 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002224 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002225 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002226
asaperssonfab67072017-04-04 05:51:49 -07002227 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002228 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002229 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002230 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002231
asaperssonfab67072017-04-04 05:51:49 -07002232 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002233 EXPECT_EQ(std::numeric_limits<int>::max(),
2234 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002235
asaperssonfab67072017-04-04 05:51:49 -07002236 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002237 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002238 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002239 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002240
asapersson02465b82017-04-10 01:12:52 -07002241 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002242 EXPECT_EQ(std::numeric_limits<int>::max(),
2243 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002244
mflodmancc3d4422017-08-03 08:27:51 -07002245 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002246}
2247
mflodmancc3d4422017-08-03 08:27:51 -07002248TEST_F(VideoStreamEncoderTest,
2249 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002250 const int kWidth = 1280;
2251 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002252 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002253 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2254 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002255
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002256 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002257 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002258 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002259 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002260
2261 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002262 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002263 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002264 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2265 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2266
2267 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002268 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002269 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002270 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2271 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2272 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2273
2274 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002275 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002276 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2277 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2278 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2279
mflodmancc3d4422017-08-03 08:27:51 -07002280 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002281}
2282
mflodmancc3d4422017-08-03 08:27:51 -07002283TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002284 const int kWidth = 1280;
2285 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002286 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002287 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2288 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002289
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002290 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002291 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002292 video_stream_encoder_->SetSource(&source,
2293 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002294 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2295 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002296 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002297
2298 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002299 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002300 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2301 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2302 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2303 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2304
2305 // Trigger adapt down for same input resolution, expect no change.
2306 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2307 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002308 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002309 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2310 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2311 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2312
2313 // Trigger adapt down for larger input resolution, expect no change.
2314 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2315 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002316 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002317 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2318 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2319 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2320
mflodmancc3d4422017-08-03 08:27:51 -07002321 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002322}
2323
mflodmancc3d4422017-08-03 08:27:51 -07002324TEST_F(VideoStreamEncoderTest,
2325 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002326 const int kWidth = 1280;
2327 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002328 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002329 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2330 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002331
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002332 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002333 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002334 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002335 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002336
2337 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002338 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002339 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002340 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2341 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2342
2343 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002344 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002345 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002346 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2347 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2348
mflodmancc3d4422017-08-03 08:27:51 -07002349 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002350}
2351
mflodmancc3d4422017-08-03 08:27:51 -07002352TEST_F(VideoStreamEncoderTest,
2353 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002354 const int kWidth = 1280;
2355 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002356 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002357 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2358 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002359
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002360 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002361 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002362 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002363 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002364
2365 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002366 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002367 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002368 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002369 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2370
2371 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002372 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002373 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002374 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002375 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2376
mflodmancc3d4422017-08-03 08:27:51 -07002377 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002378}
2379
mflodmancc3d4422017-08-03 08:27:51 -07002380TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002381 const int kWidth = 1280;
2382 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002383 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002384 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2385 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002386
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002387 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002388 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002389 video_stream_encoder_->SetSource(&source,
2390 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002391
2392 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2393 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002394 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002395 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2396 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2397 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2398
2399 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002400 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002401 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002402 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2403 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2404 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2405
mflodmancc3d4422017-08-03 08:27:51 -07002406 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002407}
2408
mflodmancc3d4422017-08-03 08:27:51 -07002409TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002410 const int kWidth = 1280;
2411 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002412 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002413 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2414 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002415
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002416 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002417 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002418 video_stream_encoder_->SetSource(&source,
2419 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002420
2421 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2422 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002423 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002424 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2425 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2426 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2427
2428 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002430 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002431 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2432 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2433 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2434
mflodmancc3d4422017-08-03 08:27:51 -07002435 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002436}
2437
mflodmancc3d4422017-08-03 08:27:51 -07002438TEST_F(VideoStreamEncoderTest,
2439 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002440 const int kWidth = 1280;
2441 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002442 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002443 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2444 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002445
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002446 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002447 AdaptingFrameForwarder source;
2448 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002449 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002450 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002451
2452 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002453 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002454 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002455 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2456 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2457
2458 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002459 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002460 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002461 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002462 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002463 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2464 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2465
2466 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002467 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002468 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002469 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2470 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2471 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2472
mflodmancc3d4422017-08-03 08:27:51 -07002473 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002474}
2475
mflodmancc3d4422017-08-03 08:27:51 -07002476TEST_F(VideoStreamEncoderTest,
2477 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002478 const int kWidth = 1280;
2479 const int kHeight = 720;
2480 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002481 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002482 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2483 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002484
2485 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2486 stats.input_frame_rate = kInputFps;
2487 stats_proxy_->SetMockStats(stats);
2488
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002489 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002490 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2491 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002492 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002493
2494 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002495 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002496 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2497 sink_.WaitForEncodedFrame(2);
2498 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2499
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002500 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002501 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002502 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002503 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002504 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002505
2506 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002507 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002508 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2509 sink_.WaitForEncodedFrame(3);
2510 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2511
2512 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002513 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002514 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002515
mflodmancc3d4422017-08-03 08:27:51 -07002516 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002517}
2518
mflodmancc3d4422017-08-03 08:27:51 -07002519TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002520 const int kWidth = 1280;
2521 const int kHeight = 720;
2522 const size_t kNumFrames = 10;
2523
Erik Språng4c6ca302019-04-08 15:14:01 +02002524 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002525 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2526 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002527
asaperssond0de2952017-04-21 01:47:31 -07002528 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002529 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002530 video_source_.set_adaptation_enabled(true);
2531
2532 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2533 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2534
2535 int downscales = 0;
2536 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002537 video_source_.IncomingCapturedFrame(
2538 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2539 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002540
asaperssonfab67072017-04-04 05:51:49 -07002541 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002542 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002543 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002544 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002545
2546 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2547 ++downscales;
2548
2549 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2550 EXPECT_EQ(downscales,
2551 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2552 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002553 }
mflodmancc3d4422017-08-03 08:27:51 -07002554 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002555}
2556
mflodmancc3d4422017-08-03 08:27:51 -07002557TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002558 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2559 const int kWidth = 1280;
2560 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002561 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002562 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2563 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002564
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002565 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002566 AdaptingFrameForwarder source;
2567 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002568 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002569 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002570
Åsa Persson8c1bf952018-09-13 10:42:19 +02002571 int64_t timestamp_ms = kFrameIntervalMs;
2572 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002573 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002574 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002575 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2576 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2577
2578 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002579 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002580 timestamp_ms += kFrameIntervalMs;
2581 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2582 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002583 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002584 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2585 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2586
2587 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002588 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002589 timestamp_ms += kFrameIntervalMs;
2590 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002591 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002592 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002593 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2594 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2595
2596 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002597 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002598 timestamp_ms += kFrameIntervalMs;
2599 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2600 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002601 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002602 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2603 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2604
2605 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002606 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002607 timestamp_ms += kFrameIntervalMs;
2608 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002609 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002610 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002611 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2612 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2613
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,
asaperssonf7e294d2017-06-13 23:25:22 -07002618 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
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);
asaperssonf7e294d2017-06-13 23:25:22 -07002624
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002625 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002626 AdaptingFrameForwarder source;
2627 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002628 video_stream_encoder_->SetSource(&source,
2629 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002630
Åsa Persson8c1bf952018-09-13 10:42:19 +02002631 int64_t timestamp_ms = kFrameIntervalMs;
2632 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002633 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002634 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002635 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2636 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2637
2638 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002639 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002640 timestamp_ms += kFrameIntervalMs;
2641 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2642 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002643 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2644 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2645 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2646
2647 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002648 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002649 timestamp_ms += kFrameIntervalMs;
2650 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002651 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002652 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002653 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2654 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2655
2656 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002657 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002658 timestamp_ms += kFrameIntervalMs;
2659 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2660 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002661 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2662 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2663 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2664
2665 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002667 timestamp_ms += kFrameIntervalMs;
2668 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002669 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002670 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002671 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2672 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2673
mflodmancc3d4422017-08-03 08:27:51 -07002674 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002675}
2676
Sergey Silkin41c650b2019-10-14 13:12:19 +02002677TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2678 fake_encoder_.SetResolutionBitrateLimits(
2679 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2680
2681 video_stream_encoder_->OnBitrateUpdated(
2682 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2683 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2684 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2685
2686 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2687 AdaptingFrameForwarder source;
2688 source.set_adaptation_enabled(true);
2689 video_stream_encoder_->SetSource(
2690 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2691
2692 // Insert 720p frame.
2693 int64_t timestamp_ms = kFrameIntervalMs;
2694 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2695 WaitForEncodedFrame(1280, 720);
2696
2697 // Reduce bitrate and trigger adapt down.
2698 video_stream_encoder_->OnBitrateUpdated(
2699 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2700 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2701 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2702 video_stream_encoder_->TriggerQualityLow();
2703
2704 // Insert 720p frame. It should be downscaled and encoded.
2705 timestamp_ms += kFrameIntervalMs;
2706 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2707 WaitForEncodedFrame(960, 540);
2708
2709 // Trigger adapt up. Higher resolution should not be requested duo to lack
2710 // of bitrate.
2711 video_stream_encoder_->TriggerQualityHigh();
2712 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2713
2714 // Increase bitrate.
2715 video_stream_encoder_->OnBitrateUpdated(
2716 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2717 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2718 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2719
2720 // Trigger adapt up. Higher resolution should be requested.
2721 video_stream_encoder_->TriggerQualityHigh();
2722 VerifyFpsMaxResolutionMax(source.sink_wants());
2723
2724 video_stream_encoder_->Stop();
2725}
2726
2727TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2728 fake_encoder_.SetResolutionBitrateLimits(
2729 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2730
2731 // Set bitrate equal to min bitrate of 540p.
2732 video_stream_encoder_->OnBitrateUpdated(
2733 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2734 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2735 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2736
2737 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2738 AdaptingFrameForwarder source;
2739 source.set_adaptation_enabled(true);
2740 video_stream_encoder_->SetSource(
2741 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2742
2743 // Insert 720p frame. It should be dropped and lower resolution should be
2744 // requested.
2745 int64_t timestamp_ms = kFrameIntervalMs;
2746 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2747 ExpectDroppedFrame();
2748 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2749
2750 // Insert 720p frame. It should be downscaled and encoded.
2751 timestamp_ms += kFrameIntervalMs;
2752 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2753 WaitForEncodedFrame(960, 540);
2754
2755 video_stream_encoder_->Stop();
2756}
2757
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002758class BalancedDegradationTest : public VideoStreamEncoderTest {
2759 protected:
2760 void SetupTest() {
2761 // Reset encoder for field trials to take effect.
2762 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002763 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002764
2765 // Enable BALANCED preference.
2766 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002767 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2768 }
2769
2770 void OnBitrateUpdated(int bitrate_bps) {
2771 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2772 DataRate::bps(bitrate_bps),
2773 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002774 }
2775
Åsa Persson45b176f2019-09-30 11:19:05 +02002776 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002777 timestamp_ms_ += kFrameIntervalMs;
2778 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002779 }
2780
2781 void InsertFrameAndWaitForEncoded() {
2782 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002783 sink_.WaitForEncodedFrame(timestamp_ms_);
2784 }
2785
2786 const int kWidth = 640; // pixels:640x360=230400
2787 const int kHeight = 360;
2788 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2789 int64_t timestamp_ms_ = 0;
2790 AdaptingFrameForwarder source_;
2791};
2792
2793TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2794 test::ScopedFieldTrials field_trials(
2795 "WebRTC-Video-BalancedDegradationSettings/"
2796 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2797 SetupTest();
2798
2799 // Force input frame rate.
2800 const int kInputFps = 24;
2801 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2802 stats.input_frame_rate = kInputFps;
2803 stats_proxy_->SetMockStats(stats);
2804
Åsa Persson45b176f2019-09-30 11:19:05 +02002805 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002806 VerifyFpsMaxResolutionMax(source_.sink_wants());
2807
2808 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2809 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2810 video_stream_encoder_->TriggerQualityLowExpectFalse();
2811 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2812
2813 video_stream_encoder_->Stop();
2814}
2815
2816TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2817 test::ScopedFieldTrials field_trials(
2818 "WebRTC-Video-BalancedDegradationSettings/"
2819 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2820 SetupTest();
2821
2822 // Force input frame rate.
2823 const int kInputFps = 25;
2824 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2825 stats.input_frame_rate = kInputFps;
2826 stats_proxy_->SetMockStats(stats);
2827
Åsa Persson45b176f2019-09-30 11:19:05 +02002828 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002829 VerifyFpsMaxResolutionMax(source_.sink_wants());
2830
2831 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2832 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2833 video_stream_encoder_->TriggerQualityLow();
2834 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2835
2836 video_stream_encoder_->Stop();
2837}
2838
2839TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2840 test::ScopedFieldTrials field_trials(
2841 "WebRTC-Video-BalancedDegradationSettings/"
2842 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2843 SetupTest();
2844
2845 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2846
Åsa Persson45b176f2019-09-30 11:19:05 +02002847 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002848 VerifyFpsMaxResolutionMax(source_.sink_wants());
2849
2850 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2851 video_stream_encoder_->TriggerQualityLow();
2852 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2853
2854 video_stream_encoder_->Stop();
2855}
2856
Åsa Perssonccfb3402019-09-25 15:13:04 +02002857TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002858 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002859 "WebRTC-Video-BalancedDegradationSettings/"
2860 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002861 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002862
Åsa Persson1b247f12019-08-14 17:26:39 +02002863 const int kMinBitrateBps = 425000;
2864 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002865 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002866
Åsa Persson45b176f2019-09-30 11:19:05 +02002867 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002868 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002869 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2870
2871 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2872 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002873 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002874 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002875 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2876
2877 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2878 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002879 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002880 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002881 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2882
Åsa Persson30ab0152019-08-27 12:22:33 +02002883 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2884 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002885 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002886 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2887 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002888 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2889
2890 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002891 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002892 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002893 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002894
Åsa Persson30ab0152019-08-27 12:22:33 +02002895 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002896 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002897 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002898 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002899 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002900 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2901
2902 video_stream_encoder_->Stop();
2903}
2904
Åsa Perssonccfb3402019-09-25 15:13:04 +02002905TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002906 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2907 test::ScopedFieldTrials field_trials(
2908 "WebRTC-Video-BalancedDegradationSettings/"
2909 "pixels:57600|129600|230400,fps:7|24|24/");
2910 SetupTest();
2911 OnBitrateUpdated(kLowTargetBitrateBps);
2912
2913 VerifyNoLimitation(source_.sink_wants());
2914
2915 // Insert frame, expect scaled down:
2916 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2917 InsertFrame();
2918 EXPECT_FALSE(WaitForFrame(1000));
2919 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2920 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2921
2922 // Insert frame, expect scaled down:
2923 // resolution (320x180@24fps).
2924 InsertFrame();
2925 EXPECT_FALSE(WaitForFrame(1000));
2926 EXPECT_LT(source_.sink_wants().max_pixel_count,
2927 source_.last_wants().max_pixel_count);
2928 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2929
2930 // Frame should not be dropped (min pixels per frame reached).
2931 InsertFrameAndWaitForEncoded();
2932
2933 video_stream_encoder_->Stop();
2934}
2935
2936TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002937 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002938 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002939 "WebRTC-Video-BalancedDegradationSettings/"
2940 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002941 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002942
Åsa Persson30ab0152019-08-27 12:22:33 +02002943 const int kResolutionMinBitrateBps = 435000;
2944 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002945 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002946
Åsa Persson45b176f2019-09-30 11:19:05 +02002947 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002948 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002949 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2950
2951 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2952 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002953 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002954 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002955 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2956
2957 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2958 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002959 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002960 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002961 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2962
2963 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2964 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002965 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002966 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002967 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2968
Åsa Persson30ab0152019-08-27 12:22:33 +02002969 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2970 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002971 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002972 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002973 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2974
2975 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2976 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002977 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002978 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2979
2980 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002981 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002982 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002983 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002984 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002985 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2986
2987 video_stream_encoder_->Stop();
2988}
2989
Åsa Perssonccfb3402019-09-25 15:13:04 +02002990TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002991 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002992 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002993 "WebRTC-Video-BalancedDegradationSettings/"
2994 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002995 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002996
Åsa Persson30ab0152019-08-27 12:22:33 +02002997 const int kMinBitrateBps = 425000;
2998 const int kTooLowMinBitrateBps = 424000;
2999 const int kResolutionMinBitrateBps = 435000;
3000 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003001 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003002
Åsa Persson45b176f2019-09-30 11:19:05 +02003003 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003004 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003005 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3006
3007 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3008 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003009 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003010 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003011 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3012
3013 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3014 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003015 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003016 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003017 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3018
3019 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3020 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003021 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003022 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003023 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3024
3025 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3026 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003027 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003028 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3029
3030 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003031 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003032 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003033 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003034 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003035 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3036
3037 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003038 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003039 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003040 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003041 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3042
3043 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003044 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003045 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003046 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003047 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003048 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3049
Åsa Persson1b247f12019-08-14 17:26:39 +02003050 video_stream_encoder_->Stop();
3051}
3052
mflodmancc3d4422017-08-03 08:27:51 -07003053TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003054 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3055 const int kWidth = 1280;
3056 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003057 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003058 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3059 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003060
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003061 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003062 AdaptingFrameForwarder source;
3063 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003064 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003065 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003066
Åsa Persson8c1bf952018-09-13 10:42:19 +02003067 int64_t timestamp_ms = kFrameIntervalMs;
3068 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003069 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003070 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003071 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3072 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3073 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3074 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3075
3076 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003077 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003078 timestamp_ms += kFrameIntervalMs;
3079 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3080 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003081 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003082 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3083 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3084 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3085 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3086
3087 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003088 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003089 timestamp_ms += kFrameIntervalMs;
3090 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3091 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003092 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003093 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3094 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3095 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3096 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3097
Jonathan Yubc771b72017-12-08 17:04:29 -08003098 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003099 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003100 timestamp_ms += kFrameIntervalMs;
3101 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3102 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003103 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003104 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3105 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003106 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003107 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3108
Jonathan Yubc771b72017-12-08 17:04:29 -08003109 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003110 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003111 timestamp_ms += kFrameIntervalMs;
3112 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3113 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003114 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003115 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003116 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3117 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3118 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3119 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3120
Jonathan Yubc771b72017-12-08 17:04:29 -08003121 // Trigger quality adapt down, expect no change (min resolution reached).
3122 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003123 timestamp_ms += kFrameIntervalMs;
3124 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3125 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003126 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3127 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3128 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3129 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3130 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3131
3132 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003133 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003134 timestamp_ms += kFrameIntervalMs;
3135 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3136 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003137 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003138 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3139 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3140 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3141 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3142
3143 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3144 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003145 timestamp_ms += kFrameIntervalMs;
3146 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3147 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003148 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3149 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3150 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3151 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3152 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3153
3154 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3155 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003156 timestamp_ms += kFrameIntervalMs;
3157 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3158 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003159 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003160 last_wants = source.sink_wants();
3161 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3162 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003163 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003164 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3165
3166 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003167 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003168 timestamp_ms += kFrameIntervalMs;
3169 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3170 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003171 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003172 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3173 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003174 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003175 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3176
3177 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003178 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003179 timestamp_ms += kFrameIntervalMs;
3180 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003181 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003182 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003183 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003184 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3185 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003186 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003187 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003188
mflodmancc3d4422017-08-03 08:27:51 -07003189 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003190}
3191
mflodmancc3d4422017-08-03 08:27:51 -07003192TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003193 const int kWidth = 640;
3194 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003195
Erik Språng4c6ca302019-04-08 15:14:01 +02003196 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003197 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3198 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003199
perkj803d97f2016-11-01 11:45:46 -07003200 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003201 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003202 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003203 }
3204
mflodmancc3d4422017-08-03 08:27:51 -07003205 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003206 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003207 video_source_.IncomingCapturedFrame(CreateFrame(
3208 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003209 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003210 }
3211
mflodmancc3d4422017-08-03 08:27:51 -07003212 video_stream_encoder_->Stop();
3213 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003214 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003215
perkj803d97f2016-11-01 11:45:46 -07003216 EXPECT_EQ(1,
3217 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3218 EXPECT_EQ(
3219 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3220}
3221
mflodmancc3d4422017-08-03 08:27:51 -07003222TEST_F(VideoStreamEncoderTest,
3223 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003224 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003225 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3226 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003227 const int kWidth = 640;
3228 const int kHeight = 360;
3229
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003230 video_stream_encoder_->SetSource(&video_source_,
3231 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003232
3233 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3234 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003235 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003236 }
3237
mflodmancc3d4422017-08-03 08:27:51 -07003238 video_stream_encoder_->Stop();
3239 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003240 stats_proxy_.reset();
3241
3242 EXPECT_EQ(0,
3243 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3244}
3245
mflodmancc3d4422017-08-03 08:27:51 -07003246TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003247 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003248 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003249
3250 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003251 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003252 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003253 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3254 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003255
sprang57c2fff2017-01-16 06:24:02 -08003256 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003257 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003258 video_stream_encoder_->OnBitrateUpdated(
3259 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3260 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003261
sprang57c2fff2017-01-16 06:24:02 -08003262 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003263 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3264 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003265 VideoBitrateAllocation bitrate_allocation =
3266 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003267 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003268 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003269 // TODO(srte): The use of millisecs here looks like an error, but the tests
3270 // fails using seconds, this should be investigated.
3271 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003272
3273 // Not called on second frame.
3274 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3275 .Times(0);
3276 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003277 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3278 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003279 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003280
3281 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003282 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3283 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003284 const int64_t start_time_ms = rtc::TimeMillis();
3285 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3286 video_source_.IncomingCapturedFrame(
3287 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3288 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003289 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003290 }
3291
3292 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003293 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003294
mflodmancc3d4422017-08-03 08:27:51 -07003295 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003296}
3297
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003298TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3299 // 2 TLs configured, temporal layers supported by encoder.
3300 const int kNumTemporalLayers = 2;
3301 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3302 fake_encoder_.SetTemporalLayersSupported(0, true);
3303
3304 // Bitrate allocated across temporal layers.
3305 const int kTl0Bps = kTargetBitrateBps *
3306 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003307 kNumTemporalLayers, /*temporal_id*/ 0,
3308 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003309 const int kTl1Bps = kTargetBitrateBps *
3310 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003311 kNumTemporalLayers, /*temporal_id*/ 1,
3312 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003313 VideoBitrateAllocation expected_bitrate;
3314 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3315 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3316
3317 VerifyAllocatedBitrate(expected_bitrate);
3318 video_stream_encoder_->Stop();
3319}
3320
3321TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3322 // 2 TLs configured, temporal layers not supported by encoder.
3323 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3324 fake_encoder_.SetTemporalLayersSupported(0, false);
3325
3326 // Temporal layers not supported by the encoder.
3327 // Total bitrate should be at ti:0.
3328 VideoBitrateAllocation expected_bitrate;
3329 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3330
3331 VerifyAllocatedBitrate(expected_bitrate);
3332 video_stream_encoder_->Stop();
3333}
3334
3335TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3336 // 2 TLs configured, temporal layers only supported for first stream.
3337 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3338 fake_encoder_.SetTemporalLayersSupported(0, true);
3339 fake_encoder_.SetTemporalLayersSupported(1, false);
3340
3341 const int kS0Bps = 150000;
3342 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003343 kS0Bps *
3344 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3345 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003346 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003347 kS0Bps *
3348 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3349 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003350 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3351 // Temporal layers not supported by si:1.
3352 VideoBitrateAllocation expected_bitrate;
3353 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3354 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3355 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3356
3357 VerifyAllocatedBitrate(expected_bitrate);
3358 video_stream_encoder_->Stop();
3359}
3360
Niels Möller7dc26b72017-12-06 10:27:48 +01003361TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3362 const int kFrameWidth = 1280;
3363 const int kFrameHeight = 720;
3364 const int kFramerate = 24;
3365
Erik Språng4c6ca302019-04-08 15:14:01 +02003366 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003367 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3368 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003369 test::FrameForwarder source;
3370 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003371 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003372
3373 // Insert a single frame, triggering initial configuration.
3374 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3375 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3376
3377 EXPECT_EQ(
3378 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3379 kDefaultFramerate);
3380
3381 // Trigger reconfigure encoder (without resetting the entire instance).
3382 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003383 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003384 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3385 video_encoder_config.number_of_streams = 1;
3386 video_encoder_config.video_stream_factory =
3387 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3388 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003389 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003390 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3391
3392 // Detector should be updated with fps limit from codec config.
3393 EXPECT_EQ(
3394 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3395 kFramerate);
3396
3397 // Trigger overuse, max framerate should be reduced.
3398 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3399 stats.input_frame_rate = kFramerate;
3400 stats_proxy_->SetMockStats(stats);
3401 video_stream_encoder_->TriggerCpuOveruse();
3402 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3403 int adapted_framerate =
3404 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3405 EXPECT_LT(adapted_framerate, kFramerate);
3406
3407 // Trigger underuse, max framerate should go back to codec configured fps.
3408 // Set extra low fps, to make sure it's actually reset, not just incremented.
3409 stats = stats_proxy_->GetStats();
3410 stats.input_frame_rate = adapted_framerate / 2;
3411 stats_proxy_->SetMockStats(stats);
3412 video_stream_encoder_->TriggerCpuNormalUsage();
3413 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3414 EXPECT_EQ(
3415 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3416 kFramerate);
3417
3418 video_stream_encoder_->Stop();
3419}
3420
3421TEST_F(VideoStreamEncoderTest,
3422 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3423 const int kFrameWidth = 1280;
3424 const int kFrameHeight = 720;
3425 const int kLowFramerate = 15;
3426 const int kHighFramerate = 25;
3427
Erik Språng4c6ca302019-04-08 15:14:01 +02003428 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003429 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3430 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003431 test::FrameForwarder source;
3432 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003433 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003434
3435 // Trigger initial configuration.
3436 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003437 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003438 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3439 video_encoder_config.number_of_streams = 1;
3440 video_encoder_config.video_stream_factory =
3441 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3442 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3443 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003444 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003445 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3446
3447 EXPECT_EQ(
3448 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3449 kLowFramerate);
3450
3451 // Trigger overuse, max framerate should be reduced.
3452 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3453 stats.input_frame_rate = kLowFramerate;
3454 stats_proxy_->SetMockStats(stats);
3455 video_stream_encoder_->TriggerCpuOveruse();
3456 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3457 int adapted_framerate =
3458 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3459 EXPECT_LT(adapted_framerate, kLowFramerate);
3460
3461 // Reconfigure the encoder with a new (higher max framerate), max fps should
3462 // still respect the adaptation.
3463 video_encoder_config.video_stream_factory =
3464 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3465 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3466 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003467 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003468 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3469
3470 EXPECT_EQ(
3471 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3472 adapted_framerate);
3473
3474 // Trigger underuse, max framerate should go back to codec configured fps.
3475 stats = stats_proxy_->GetStats();
3476 stats.input_frame_rate = adapted_framerate;
3477 stats_proxy_->SetMockStats(stats);
3478 video_stream_encoder_->TriggerCpuNormalUsage();
3479 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3480 EXPECT_EQ(
3481 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3482 kHighFramerate);
3483
3484 video_stream_encoder_->Stop();
3485}
3486
mflodmancc3d4422017-08-03 08:27:51 -07003487TEST_F(VideoStreamEncoderTest,
3488 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003489 const int kFrameWidth = 1280;
3490 const int kFrameHeight = 720;
3491 const int kFramerate = 24;
3492
Erik Språng4c6ca302019-04-08 15:14:01 +02003493 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003494 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3495 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003496 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003497 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003498 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003499
3500 // Trigger initial configuration.
3501 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003502 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003503 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3504 video_encoder_config.number_of_streams = 1;
3505 video_encoder_config.video_stream_factory =
3506 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3507 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003508 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003509 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003510 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003511
Niels Möller7dc26b72017-12-06 10:27:48 +01003512 EXPECT_EQ(
3513 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3514 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003515
3516 // Trigger overuse, max framerate should be reduced.
3517 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3518 stats.input_frame_rate = kFramerate;
3519 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003520 video_stream_encoder_->TriggerCpuOveruse();
3521 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003522 int adapted_framerate =
3523 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003524 EXPECT_LT(adapted_framerate, kFramerate);
3525
3526 // Change degradation preference to not enable framerate scaling. Target
3527 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003528 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003529 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003530 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003531 EXPECT_EQ(
3532 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3533 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003534
mflodmancc3d4422017-08-03 08:27:51 -07003535 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003536}
3537
mflodmancc3d4422017-08-03 08:27:51 -07003538TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003539 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003540 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003541 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003542 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003543 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003544 const int kWidth = 640;
3545 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003546
asaperssonfab67072017-04-04 05:51:49 -07003547 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003548
3549 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003550 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003551
3552 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003553 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003554
sprangc5d62e22017-04-02 23:53:04 -07003555 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003556
asaperssonfab67072017-04-04 05:51:49 -07003557 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003558 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003559 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003560
3561 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003562 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003563
sprangc5d62e22017-04-02 23:53:04 -07003564 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003565
mflodmancc3d4422017-08-03 08:27:51 -07003566 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003567}
3568
mflodmancc3d4422017-08-03 08:27:51 -07003569TEST_F(VideoStreamEncoderTest,
3570 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003571 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003572 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003573 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003574 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003575 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003576 const int kWidth = 640;
3577 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003578
3579 // We expect the n initial frames to get dropped.
3580 int i;
3581 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003582 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003583 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003584 }
3585 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003586 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003587 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003588
3589 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003590 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003591
mflodmancc3d4422017-08-03 08:27:51 -07003592 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003593}
3594
mflodmancc3d4422017-08-03 08:27:51 -07003595TEST_F(VideoStreamEncoderTest,
3596 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003597 const int kWidth = 640;
3598 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003599 video_stream_encoder_->OnBitrateUpdated(
3600 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3601 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003602
3603 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003604 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003605 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
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 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003609 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003610
mflodmancc3d4422017-08-03 08:27:51 -07003611 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003612}
3613
mflodmancc3d4422017-08-03 08:27:51 -07003614TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003615 const int kWidth = 640;
3616 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003617 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003618
3619 VideoEncoderConfig video_encoder_config;
3620 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3621 // Make format different, to force recreation of encoder.
3622 video_encoder_config.video_format.parameters["foo"] = "foo";
3623 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003624 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003625 video_stream_encoder_->OnBitrateUpdated(
3626 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3627 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003628
kthelgasonb83797b2017-02-14 11:57:25 -08003629 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003630 video_stream_encoder_->SetSource(&video_source_,
3631 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003632
asaperssonfab67072017-04-04 05:51:49 -07003633 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003634 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003635 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003636
mflodmancc3d4422017-08-03 08:27:51 -07003637 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003638 fake_encoder_.SetQualityScaling(true);
3639}
3640
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003641TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3642 webrtc::test::ScopedFieldTrials field_trials(
3643 "WebRTC-InitialFramedrop/Enabled/");
3644 // Reset encoder for field trials to take effect.
3645 ConfigureEncoder(video_encoder_config_.Copy());
3646 const int kTooLowBitrateForFrameSizeBps = 10000;
3647 const int kWidth = 640;
3648 const int kHeight = 360;
3649
Erik Språng4c6ca302019-04-08 15:14:01 +02003650 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003651 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3652 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003653 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3654 // Frame should not be dropped.
3655 WaitForEncodedFrame(1);
3656
Erik Språng610c7632019-03-06 15:37:33 +01003657 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003658 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003659 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003660 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003661 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3662 // Expect to drop this frame, the wait should time out.
3663 ExpectDroppedFrame();
3664
3665 // Expect the sink_wants to specify a scaled frame.
3666 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3667 video_stream_encoder_->Stop();
3668}
3669
Åsa Persson139f4dc2019-08-02 09:29:58 +02003670TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3671 webrtc::test::ScopedFieldTrials field_trials(
3672 "WebRTC-Video-QualityScalerSettings/"
3673 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3674 // Reset encoder for field trials to take effect.
3675 ConfigureEncoder(video_encoder_config_.Copy());
3676 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3677 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3678 const int kWidth = 640;
3679 const int kHeight = 360;
3680
3681 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003682 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3683 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003684 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3685 // Frame should not be dropped.
3686 WaitForEncodedFrame(1);
3687
3688 video_stream_encoder_->OnBitrateUpdated(
3689 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003690 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003691 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3692 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3693 // Frame should not be dropped.
3694 WaitForEncodedFrame(2);
3695
3696 video_stream_encoder_->OnBitrateUpdated(
3697 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003698 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003699 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3700 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3701 // Expect to drop this frame, the wait should time out.
3702 ExpectDroppedFrame();
3703
3704 // Expect the sink_wants to specify a scaled frame.
3705 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3706 video_stream_encoder_->Stop();
3707}
3708
Åsa Perssone644a032019-11-08 15:56:00 +01003709TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3710 webrtc::test::ScopedFieldTrials field_trials(
3711 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3712
3713 // Reset encoder for field trials to take effect.
3714 VideoEncoderConfig config = video_encoder_config_.Copy();
3715 config.max_bitrate_bps = kTargetBitrateBps;
3716 ConfigureEncoder(std::move(config));
3717 fake_encoder_.SetQp(kQpLow);
3718
3719 // Enable MAINTAIN_FRAMERATE preference.
3720 AdaptingFrameForwarder source;
3721 source.set_adaptation_enabled(true);
3722 video_stream_encoder_->SetSource(&source,
3723 DegradationPreference::MAINTAIN_FRAMERATE);
3724
3725 // Start at low bitrate.
3726 const int kLowBitrateBps = 200000;
3727 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3728 DataRate::bps(kLowBitrateBps),
3729 DataRate::bps(kLowBitrateBps), 0, 0);
3730
3731 // Expect first frame to be dropped and resolution to be limited.
3732 const int kWidth = 1280;
3733 const int kHeight = 720;
3734 const int64_t kFrameIntervalMs = 100;
3735 int64_t timestamp_ms = kFrameIntervalMs;
3736 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3737 ExpectDroppedFrame();
3738 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3739
3740 // Increase bitrate to encoder max.
3741 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3742 DataRate::bps(config.max_bitrate_bps),
3743 DataRate::bps(config.max_bitrate_bps),
3744 0, 0);
3745
3746 // Insert frames and advance |min_duration_ms|.
3747 for (size_t i = 1; i <= 10; i++) {
3748 timestamp_ms += kFrameIntervalMs;
3749 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3750 WaitForEncodedFrame(timestamp_ms);
3751 }
3752 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3753 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3754
3755 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3756
3757 // Insert frame should trigger high BW and release quality limitation.
3758 timestamp_ms += kFrameIntervalMs;
3759 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3760 WaitForEncodedFrame(timestamp_ms);
3761 VerifyFpsMaxResolutionMax(source.sink_wants());
3762
3763 // Frame should not be adapted.
3764 timestamp_ms += kFrameIntervalMs;
3765 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3766 WaitForEncodedFrame(kWidth, kHeight);
3767 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3768
3769 video_stream_encoder_->Stop();
3770}
3771
mflodmancc3d4422017-08-03 08:27:51 -07003772TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003773 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3774 const int kTooSmallWidth = 10;
3775 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003776 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003777 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3778 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003779
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003780 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003781 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003782 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003783 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003784 VerifyNoLimitation(source.sink_wants());
3785 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3786
3787 // Trigger adapt down, too small frame, expect no change.
3788 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003789 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003790 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003791 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003792 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3793 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3794
mflodmancc3d4422017-08-03 08:27:51 -07003795 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003796}
3797
mflodmancc3d4422017-08-03 08:27:51 -07003798TEST_F(VideoStreamEncoderTest,
3799 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003800 const int kTooSmallWidth = 10;
3801 const int kTooSmallHeight = 10;
3802 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003803 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003804 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3805 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003806
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003807 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003808 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003809 video_stream_encoder_->SetSource(&source,
3810 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003811 VerifyNoLimitation(source.sink_wants());
3812 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3813 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3814
3815 // Trigger adapt down, expect limited framerate.
3816 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003817 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003818 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003819 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3820 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3821 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3822 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3823
3824 // Trigger adapt down, too small frame, expect no change.
3825 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003826 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003827 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003828 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3829 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3830 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3831 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3832
mflodmancc3d4422017-08-03 08:27:51 -07003833 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003834}
3835
mflodmancc3d4422017-08-03 08:27:51 -07003836TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003837 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003838 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003839 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3840 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003841 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003842 const int kFrameWidth = 1280;
3843 const int kFrameHeight = 720;
3844 video_source_.IncomingCapturedFrame(
3845 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003846 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003847 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003848}
3849
sprangb1ca0732017-02-01 08:38:12 -08003850// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003851TEST_F(VideoStreamEncoderTest,
3852 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003853 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003854 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3855 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003856
3857 const int kFrameWidth = 1280;
3858 const int kFrameHeight = 720;
3859 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003860 // requested by
3861 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003862 video_source_.set_adaptation_enabled(true);
3863
3864 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003865 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003866 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003867
3868 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003869 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003870 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003871 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003872 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003873
asaperssonfab67072017-04-04 05:51:49 -07003874 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003875 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003876 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003877 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003878 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003879
mflodmancc3d4422017-08-03 08:27:51 -07003880 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003881}
sprangfe627f32017-03-29 08:24:59 -07003882
mflodmancc3d4422017-08-03 08:27:51 -07003883TEST_F(VideoStreamEncoderTest,
3884 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003885 const int kFrameWidth = 1280;
3886 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003887
Erik Språng4c6ca302019-04-08 15:14:01 +02003888 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003889 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3890 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003891 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003892 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003893 video_source_.set_adaptation_enabled(true);
3894
sprang4847ae62017-06-27 07:06:52 -07003895 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003896
3897 video_source_.IncomingCapturedFrame(
3898 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003899 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003900
3901 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003902 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003903
3904 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003905 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003906 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003907 video_source_.IncomingCapturedFrame(
3908 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003909 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003910 }
3911
3912 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003913 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003914 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003915 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003916 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003917 video_source_.IncomingCapturedFrame(
3918 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003919 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003920 ++num_frames_dropped;
3921 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003922 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003923 }
3924 }
3925
sprang4847ae62017-06-27 07:06:52 -07003926 // Add some slack to account for frames dropped by the frame dropper.
3927 const int kErrorMargin = 1;
3928 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003929 kErrorMargin);
3930
3931 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003932 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003933 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003934 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003935 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003936 video_source_.IncomingCapturedFrame(
3937 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003938 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003939 ++num_frames_dropped;
3940 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003941 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003942 }
3943 }
sprang4847ae62017-06-27 07:06:52 -07003944 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003945 kErrorMargin);
3946
3947 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003948 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003949 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003950 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003951 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003952 video_source_.IncomingCapturedFrame(
3953 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003954 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003955 ++num_frames_dropped;
3956 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003957 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003958 }
3959 }
sprang4847ae62017-06-27 07:06:52 -07003960 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003961 kErrorMargin);
3962
3963 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003964 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003965 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003966 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003967 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003968 video_source_.IncomingCapturedFrame(
3969 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003970 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003971 ++num_frames_dropped;
3972 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003973 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003974 }
3975 }
3976 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3977
mflodmancc3d4422017-08-03 08:27:51 -07003978 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003979}
3980
mflodmancc3d4422017-08-03 08:27:51 -07003981TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003982 const int kFramerateFps = 5;
3983 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003984 const int kFrameWidth = 1280;
3985 const int kFrameHeight = 720;
3986
sprang4847ae62017-06-27 07:06:52 -07003987 // Reconfigure encoder with two temporal layers and screensharing, which will
3988 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003989 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003990
Erik Språng4c6ca302019-04-08 15:14:01 +02003991 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003992 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3993 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003994 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003995 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003996 video_source_.set_adaptation_enabled(true);
3997
sprang4847ae62017-06-27 07:06:52 -07003998 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003999
4000 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004001 rtc::VideoSinkWants last_wants;
4002 do {
4003 last_wants = video_source_.sink_wants();
4004
sprangc5d62e22017-04-02 23:53:04 -07004005 // Insert frames to get a new fps estimate...
4006 for (int j = 0; j < kFramerateFps; ++j) {
4007 video_source_.IncomingCapturedFrame(
4008 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004009 if (video_source_.last_sent_width()) {
4010 sink_.WaitForEncodedFrame(timestamp_ms);
4011 }
sprangc5d62e22017-04-02 23:53:04 -07004012 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004013 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004014 }
4015 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004016 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004017 } while (video_source_.sink_wants().max_framerate_fps <
4018 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004019
Jonathan Yubc771b72017-12-08 17:04:29 -08004020 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004021
mflodmancc3d4422017-08-03 08:27:51 -07004022 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004023}
asaperssonf7e294d2017-06-13 23:25:22 -07004024
mflodmancc3d4422017-08-03 08:27:51 -07004025TEST_F(VideoStreamEncoderTest,
4026 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004027 const int kWidth = 1280;
4028 const int kHeight = 720;
4029 const int64_t kFrameIntervalMs = 150;
4030 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004031 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004032 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4033 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004034
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004035 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004036 AdaptingFrameForwarder source;
4037 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004038 video_stream_encoder_->SetSource(&source,
4039 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004040 timestamp_ms += kFrameIntervalMs;
4041 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004042 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004043 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004044 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4045 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4046 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4047
4048 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004049 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004050 timestamp_ms += kFrameIntervalMs;
4051 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004052 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004053 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4054 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4055 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4056 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4057
4058 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004059 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004060 timestamp_ms += kFrameIntervalMs;
4061 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004062 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004063 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4064 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4065 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4066 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4067
4068 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004069 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004070 timestamp_ms += kFrameIntervalMs;
4071 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004072 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004073 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4074 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4075 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4076 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4077
4078 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004079 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004080 timestamp_ms += kFrameIntervalMs;
4081 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004082 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004083 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4084 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4085 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4086 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4087
4088 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004089 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004090 timestamp_ms += kFrameIntervalMs;
4091 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004092 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004093 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4094 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4095 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4096 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4097
4098 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004099 video_stream_encoder_->TriggerQualityLow();
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004103 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4104 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4105 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4106 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4107
4108 // Trigger adapt down, expect reduced fps (320x180@7fps).
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 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4114 rtc::VideoSinkWants last_wants = source.sink_wants();
4115 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4116 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4117 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4118
4119 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004120 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004121 timestamp_ms += kFrameIntervalMs;
4122 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004123 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004124 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4125 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4126 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4127 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4128
4129 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004130 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004131 timestamp_ms += kFrameIntervalMs;
4132 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004133 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004134 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4135 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4136 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4137 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4138
4139 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004140 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004141 timestamp_ms += kFrameIntervalMs;
4142 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004143 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004144 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4145 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4146 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4147 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4148
4149 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004150 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004151 timestamp_ms += kFrameIntervalMs;
4152 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004153 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004154 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4155 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4156 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4157 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4158
4159 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004160 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004161 timestamp_ms += kFrameIntervalMs;
4162 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004163 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004164 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4165 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4166 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4167 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4168
4169 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004170 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004171 timestamp_ms += kFrameIntervalMs;
4172 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004173 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004174 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4175 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4176 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4177 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4178
4179 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004180 video_stream_encoder_->TriggerQualityHigh();
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 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4185 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4186 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4187 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4188
Åsa Persson30ab0152019-08-27 12:22:33 +02004189 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
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(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004194 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004195 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004196 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4197 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4198 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4199
4200 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004201 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004202 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004203 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4204
mflodmancc3d4422017-08-03 08:27:51 -07004205 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004206}
4207
mflodmancc3d4422017-08-03 08:27:51 -07004208TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004209 const int kWidth = 1280;
4210 const int kHeight = 720;
4211 const int64_t kFrameIntervalMs = 150;
4212 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004213 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004214 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4215 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004216
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004217 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004218 AdaptingFrameForwarder source;
4219 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004220 video_stream_encoder_->SetSource(&source,
4221 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004222 timestamp_ms += kFrameIntervalMs;
4223 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004224 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004225 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004226 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4227 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4228 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4229 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4230 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4231 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4232
4233 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004234 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004235 timestamp_ms += kFrameIntervalMs;
4236 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004237 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004238 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4239 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4240 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4241 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4242 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4243 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4244 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4245
4246 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004247 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004248 timestamp_ms += kFrameIntervalMs;
4249 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004250 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004251 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4252 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4253 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4254 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4255 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4256 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4257 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4258
4259 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004260 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004261 timestamp_ms += kFrameIntervalMs;
4262 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004263 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004264 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4265 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4266 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4267 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4268 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4269 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4270 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4271
4272 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004273 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004274 timestamp_ms += kFrameIntervalMs;
4275 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004276 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004277 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4278 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4279 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4280 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4281 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4282 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4283 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4284
4285 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004286 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004287 timestamp_ms += kFrameIntervalMs;
4288 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004289 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004290 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4291 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4292 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4293 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4294 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4295 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4296 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4297
4298 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004299 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004300 timestamp_ms += kFrameIntervalMs;
4301 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004302 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004303 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004304 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004305 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4306 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4307 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4308 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4309 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4310 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4311
4312 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004313 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004314 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004315 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4316 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4317
mflodmancc3d4422017-08-03 08:27:51 -07004318 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004319}
4320
mflodmancc3d4422017-08-03 08:27:51 -07004321TEST_F(VideoStreamEncoderTest,
4322 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004323 const int kWidth = 640;
4324 const int kHeight = 360;
4325 const int kFpsLimit = 15;
4326 const int64_t kFrameIntervalMs = 150;
4327 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004328 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004329 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4330 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004331
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004332 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004333 AdaptingFrameForwarder source;
4334 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004335 video_stream_encoder_->SetSource(&source,
4336 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004337 timestamp_ms += kFrameIntervalMs;
4338 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004339 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004340 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004341 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4342 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4343 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4344 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4345 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4346 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4347
4348 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004349 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004350 timestamp_ms += kFrameIntervalMs;
4351 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004352 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004353 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4354 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4355 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4356 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4357 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4358 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4359 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4360
4361 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004362 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004363 timestamp_ms += kFrameIntervalMs;
4364 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004365 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004366 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4367 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4368 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4369 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4370 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4371 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4372 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4373
4374 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004375 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004376 timestamp_ms += kFrameIntervalMs;
4377 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004378 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004379 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4380 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4381 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4382 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4383 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4384 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4385 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4386
4387 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004388 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004389 timestamp_ms += kFrameIntervalMs;
4390 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004391 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004392 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004393 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4394 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4395 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4396 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4397 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4398 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4399
4400 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004401 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004402 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004403 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4404 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4405
mflodmancc3d4422017-08-03 08:27:51 -07004406 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004407}
4408
mflodmancc3d4422017-08-03 08:27:51 -07004409TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004410 const int kFrameWidth = 1920;
4411 const int kFrameHeight = 1080;
4412 // 3/4 of 1920.
4413 const int kAdaptedFrameWidth = 1440;
4414 // 3/4 of 1080 rounded down to multiple of 4.
4415 const int kAdaptedFrameHeight = 808;
4416 const int kFramerate = 24;
4417
Erik Språng4c6ca302019-04-08 15:14:01 +02004418 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004419 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4420 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004421 // Trigger reconfigure encoder (without resetting the entire instance).
4422 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004423 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004424 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4425 video_encoder_config.number_of_streams = 1;
4426 video_encoder_config.video_stream_factory =
4427 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004428 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004429 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004430 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004431
4432 video_source_.set_adaptation_enabled(true);
4433
4434 video_source_.IncomingCapturedFrame(
4435 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004436 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004437
4438 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004439 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004440 video_source_.IncomingCapturedFrame(
4441 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004442 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004443
mflodmancc3d4422017-08-03 08:27:51 -07004444 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004445}
4446
mflodmancc3d4422017-08-03 08:27:51 -07004447TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004448 const int kFrameWidth = 1280;
4449 const int kFrameHeight = 720;
4450 const int kLowFps = 2;
4451 const int kHighFps = 30;
4452
Erik Språng4c6ca302019-04-08 15:14:01 +02004453 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004454 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4455 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004456
4457 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4458 max_framerate_ = kLowFps;
4459
4460 // Insert 2 seconds of 2fps video.
4461 for (int i = 0; i < kLowFps * 2; ++i) {
4462 video_source_.IncomingCapturedFrame(
4463 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4464 WaitForEncodedFrame(timestamp_ms);
4465 timestamp_ms += 1000 / kLowFps;
4466 }
4467
4468 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004469 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004470 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4471 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004472 video_source_.IncomingCapturedFrame(
4473 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4474 WaitForEncodedFrame(timestamp_ms);
4475 timestamp_ms += 1000 / kLowFps;
4476
4477 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4478
4479 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004480 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004481 const int kFrameIntervalMs = 1000 / kHighFps;
4482 max_framerate_ = kHighFps;
4483 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4484 video_source_.IncomingCapturedFrame(
4485 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4486 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4487 // be dropped if the encoder hans't been updated with the new higher target
4488 // framerate yet, causing it to overshoot the target bitrate and then
4489 // suffering the wrath of the media optimizer.
4490 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4491 timestamp_ms += kFrameIntervalMs;
4492 }
4493
4494 // Don expect correct measurement just yet, but it should be higher than
4495 // before.
4496 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4497
mflodmancc3d4422017-08-03 08:27:51 -07004498 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004499}
4500
mflodmancc3d4422017-08-03 08:27:51 -07004501TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004502 const int kFrameWidth = 1280;
4503 const int kFrameHeight = 720;
4504 const int kTargetBitrateBps = 1000000;
4505
4506 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004507 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004508 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004509 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4510 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004511 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004512
4513 // Insert a first video frame, causes another bitrate update.
4514 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4515 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4516 video_source_.IncomingCapturedFrame(
4517 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4518 WaitForEncodedFrame(timestamp_ms);
4519
4520 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004521 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4522 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004523
4524 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004525 timestamp_ms += kProcessIntervalMs;
4526 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004527
4528 // Bitrate observer should not be called.
4529 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4530 video_source_.IncomingCapturedFrame(
4531 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4532 ExpectDroppedFrame();
4533
mflodmancc3d4422017-08-03 08:27:51 -07004534 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004535}
ilnik6b826ef2017-06-16 06:53:48 -07004536
Niels Möller4db138e2018-04-19 09:04:13 +02004537TEST_F(VideoStreamEncoderTest,
4538 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4539 const int kFrameWidth = 1280;
4540 const int kFrameHeight = 720;
4541 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004542 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004543 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4544 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004545 video_source_.IncomingCapturedFrame(
4546 CreateFrame(1, kFrameWidth, kFrameHeight));
4547 WaitForEncodedFrame(1);
4548 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4549 .low_encode_usage_threshold_percent,
4550 default_options.low_encode_usage_threshold_percent);
4551 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4552 .high_encode_usage_threshold_percent,
4553 default_options.high_encode_usage_threshold_percent);
4554 video_stream_encoder_->Stop();
4555}
4556
4557TEST_F(VideoStreamEncoderTest,
4558 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4559 const int kFrameWidth = 1280;
4560 const int kFrameHeight = 720;
4561 CpuOveruseOptions hardware_options;
4562 hardware_options.low_encode_usage_threshold_percent = 150;
4563 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004564 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004565
Erik Språng4c6ca302019-04-08 15:14:01 +02004566 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004567 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4568 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004569 video_source_.IncomingCapturedFrame(
4570 CreateFrame(1, kFrameWidth, kFrameHeight));
4571 WaitForEncodedFrame(1);
4572 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4573 .low_encode_usage_threshold_percent,
4574 hardware_options.low_encode_usage_threshold_percent);
4575 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4576 .high_encode_usage_threshold_percent,
4577 hardware_options.high_encode_usage_threshold_percent);
4578 video_stream_encoder_->Stop();
4579}
4580
Niels Möller6bb5ab92019-01-11 11:11:10 +01004581TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4582 const int kFrameWidth = 320;
4583 const int kFrameHeight = 240;
4584 const int kFps = 30;
4585 const int kTargetBitrateBps = 120000;
4586 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4587
Erik Språng4c6ca302019-04-08 15:14:01 +02004588 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004589 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4590 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004591
4592 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4593 max_framerate_ = kFps;
4594
4595 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4596 fake_encoder_.SimulateOvershoot(1.0);
4597 int num_dropped = 0;
4598 for (int i = 0; i < kNumFramesInRun; ++i) {
4599 video_source_.IncomingCapturedFrame(
4600 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4601 // Wait up to two frame durations for a frame to arrive.
4602 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4603 ++num_dropped;
4604 }
4605 timestamp_ms += 1000 / kFps;
4606 }
4607
Erik Språnga8d48ab2019-02-08 14:17:40 +01004608 // Framerate should be measured to be near the expected target rate.
4609 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4610
4611 // Frame drops should be within 5% of expected 0%.
4612 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004613
4614 // Make encoder produce frames at double the expected bitrate during 3 seconds
4615 // of video, verify number of drops. Rate needs to be slightly changed in
4616 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004617 double overshoot_factor = 2.0;
4618 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4619 // With bitrate adjuster, when need to overshoot even more to trigger
4620 // frame dropping.
4621 overshoot_factor *= 2;
4622 }
4623 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004624 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004625 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004626 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004627 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004628 num_dropped = 0;
4629 for (int i = 0; i < kNumFramesInRun; ++i) {
4630 video_source_.IncomingCapturedFrame(
4631 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4632 // Wait up to two frame durations for a frame to arrive.
4633 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4634 ++num_dropped;
4635 }
4636 timestamp_ms += 1000 / kFps;
4637 }
4638
Erik Språng4c6ca302019-04-08 15:14:01 +02004639 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004640 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4641 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004642
4643 // Target framerate should be still be near the expected target, despite
4644 // the frame drops.
4645 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4646
4647 // Frame drops should be within 5% of expected 50%.
4648 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004649
4650 video_stream_encoder_->Stop();
4651}
4652
4653TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4654 const int kFrameWidth = 320;
4655 const int kFrameHeight = 240;
4656 const int kActualInputFps = 24;
4657 const int kTargetBitrateBps = 120000;
4658
4659 ASSERT_GT(max_framerate_, kActualInputFps);
4660
4661 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4662 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004663 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004664 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4665 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004666
4667 // Insert 3 seconds of video, with an input fps lower than configured max.
4668 for (int i = 0; i < kActualInputFps * 3; ++i) {
4669 video_source_.IncomingCapturedFrame(
4670 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4671 // Wait up to two frame durations for a frame to arrive.
4672 WaitForEncodedFrame(timestamp_ms);
4673 timestamp_ms += 1000 / kActualInputFps;
4674 }
4675
4676 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4677
4678 video_stream_encoder_->Stop();
4679}
4680
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004681TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4682 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004683 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004684 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4685 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004686
4687 fake_encoder_.BlockNextEncode();
4688 video_source_.IncomingCapturedFrame(
4689 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4690 WaitForEncodedFrame(1);
4691 // On the very first frame full update should be forced.
4692 rect = fake_encoder_.GetLastUpdateRect();
4693 EXPECT_EQ(rect.offset_x, 0);
4694 EXPECT_EQ(rect.offset_y, 0);
4695 EXPECT_EQ(rect.height, codec_height_);
4696 EXPECT_EQ(rect.width, codec_width_);
4697 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4698 // call to ContinueEncode.
4699 video_source_.IncomingCapturedFrame(
4700 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4701 ExpectDroppedFrame();
4702 video_source_.IncomingCapturedFrame(
4703 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4704 ExpectDroppedFrame();
4705 fake_encoder_.ContinueEncode();
4706 WaitForEncodedFrame(3);
4707 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4708 rect = fake_encoder_.GetLastUpdateRect();
4709 EXPECT_EQ(rect.offset_x, 1);
4710 EXPECT_EQ(rect.offset_y, 0);
4711 EXPECT_EQ(rect.width, 10);
4712 EXPECT_EQ(rect.height, 1);
4713
4714 video_source_.IncomingCapturedFrame(
4715 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4716 WaitForEncodedFrame(4);
4717 // Previous frame was encoded, so no accumulation should happen.
4718 rect = fake_encoder_.GetLastUpdateRect();
4719 EXPECT_EQ(rect.offset_x, 0);
4720 EXPECT_EQ(rect.offset_y, 0);
4721 EXPECT_EQ(rect.width, 1);
4722 EXPECT_EQ(rect.height, 1);
4723
4724 video_stream_encoder_->Stop();
4725}
4726
Erik Språngd7329ca2019-02-21 21:19:53 +01004727TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004728 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004729 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4730 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004731
4732 // First frame is always keyframe.
4733 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4734 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004735 EXPECT_THAT(
4736 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004737 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004738
4739 // Insert delta frame.
4740 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4741 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004742 EXPECT_THAT(
4743 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004744 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004745
4746 // Request next frame be a key-frame.
4747 video_stream_encoder_->SendKeyFrame();
4748 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4749 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004750 EXPECT_THAT(
4751 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004752 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004753
4754 video_stream_encoder_->Stop();
4755}
4756
4757TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4758 // Setup simulcast with three streams.
4759 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004760 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004761 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004762 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004763 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004764 // Wait for all three layers before triggering event.
4765 sink_.SetNumExpectedLayers(3);
4766
4767 // First frame is always keyframe.
4768 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4769 WaitForEncodedFrame(1);
4770 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004771 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4772 VideoFrameType::kVideoFrameKey,
4773 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004774
4775 // Insert delta frame.
4776 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4777 WaitForEncodedFrame(2);
4778 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004779 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4780 VideoFrameType::kVideoFrameDelta,
4781 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004782
4783 // Request next frame be a key-frame.
4784 // Only first stream is configured to produce key-frame.
4785 video_stream_encoder_->SendKeyFrame();
4786 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4787 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004788
4789 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4790 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004791 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004792 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004793 VideoFrameType::kVideoFrameKey,
4794 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004795
4796 video_stream_encoder_->Stop();
4797}
4798
4799TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4800 // Configure internal source factory and setup test again.
4801 encoder_factory_.SetHasInternalSource(true);
4802 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004803 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004804 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4805 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004806
4807 // Call encoder directly, simulating internal source where encoded frame
4808 // callback in VideoStreamEncoder is called despite no OnFrame().
4809 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4810 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004811 EXPECT_THAT(
4812 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004813 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004814
Niels Möller8f7ce222019-03-21 15:43:58 +01004815 const std::vector<VideoFrameType> kDeltaFrame = {
4816 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004817 // Need to set timestamp manually since manually for injected frame.
4818 VideoFrame frame = CreateFrame(101, nullptr);
4819 frame.set_timestamp(101);
4820 fake_encoder_.InjectFrame(frame, false);
4821 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004822 EXPECT_THAT(
4823 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004824 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004825
4826 // Request key-frame. The forces a dummy frame down into the encoder.
4827 fake_encoder_.ExpectNullFrame();
4828 video_stream_encoder_->SendKeyFrame();
4829 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004830 EXPECT_THAT(
4831 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004832 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004833
4834 video_stream_encoder_->Stop();
4835}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004836
4837TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4838 // Configure internal source factory and setup test again.
4839 encoder_factory_.SetHasInternalSource(true);
4840 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004841 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004842 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4843 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004844
4845 int64_t timestamp = 1;
4846 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004847 image.SetEncodedData(
4848 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004849 image.capture_time_ms_ = ++timestamp;
4850 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4851 const int64_t kEncodeFinishDelayMs = 10;
4852 image.timing_.encode_start_ms = timestamp;
4853 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4854 fake_encoder_.InjectEncodedImage(image);
4855 // Wait for frame without incrementing clock.
4856 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4857 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4858 // capture timestamp should be kEncodeFinishDelayMs in the past.
4859 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4860 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4861 kEncodeFinishDelayMs);
4862
4863 video_stream_encoder_->Stop();
4864}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004865
4866TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4867 // Configure internal source factory and setup test again.
4868 encoder_factory_.SetHasInternalSource(true);
4869 ResetEncoder("H264", 1, 1, 1, false);
4870
4871 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4872 image._frameType = VideoFrameType::kVideoFrameKey;
4873
4874 CodecSpecificInfo codec_specific_info;
4875 codec_specific_info.codecType = kVideoCodecH264;
4876
4877 RTPFragmentationHeader fragmentation;
4878 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4879 fragmentation.fragmentationOffset[0] = 4;
4880 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4881
4882 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4883 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4884
4885 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4886 testing::ElementsAreArray(optimal_sps));
4887 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4888 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4889 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4890 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4891
4892 video_stream_encoder_->Stop();
4893}
4894
4895TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4896 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4897 0x00, 0x00, 0x03, 0x03, 0xF4,
4898 0x05, 0x03, 0xC7, 0xC0};
4899
4900 // Configure internal source factory and setup test again.
4901 encoder_factory_.SetHasInternalSource(true);
4902 ResetEncoder("H264", 1, 1, 1, false);
4903
4904 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4905 image._frameType = VideoFrameType::kVideoFrameKey;
4906
4907 CodecSpecificInfo codec_specific_info;
4908 codec_specific_info.codecType = kVideoCodecH264;
4909
4910 RTPFragmentationHeader fragmentation;
4911 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4912 fragmentation.fragmentationOffset[0] = 4;
4913 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4914
4915 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4916 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4917
4918 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4919 testing::ElementsAreArray(optimal_sps));
4920 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4921 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4922 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4923 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4924
4925 video_stream_encoder_->Stop();
4926}
4927
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004928TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4929 const int kFrameWidth = 1280;
4930 const int kFrameHeight = 720;
4931 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4932
4933 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004934 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4935 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004936 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4937
4938 // Insert a first video frame. It should be dropped because of downscale in
4939 // resolution.
4940 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4941 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4942 frame.set_rotation(kVideoRotation_270);
4943 video_source_.IncomingCapturedFrame(frame);
4944
4945 ExpectDroppedFrame();
4946
4947 // Second frame is downscaled.
4948 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4949 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4950 frame.set_rotation(kVideoRotation_90);
4951 video_source_.IncomingCapturedFrame(frame);
4952
4953 WaitForEncodedFrame(timestamp_ms);
4954 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4955
4956 // Insert another frame, also downscaled.
4957 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4958 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4959 frame.set_rotation(kVideoRotation_180);
4960 video_source_.IncomingCapturedFrame(frame);
4961
4962 WaitForEncodedFrame(timestamp_ms);
4963 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4964
4965 video_stream_encoder_->Stop();
4966}
4967
Erik Språng5056af02019-09-02 15:53:11 +02004968TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4969 const int kFrameWidth = 320;
4970 const int kFrameHeight = 180;
4971
4972 // Initial rate.
4973 video_stream_encoder_->OnBitrateUpdated(
4974 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004975 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004976 /*link_allocation=*/DataRate::kbps(300),
4977 /*fraction_lost=*/0,
4978 /*rtt_ms=*/0);
4979
4980 // Insert a first video frame so that encoder gets configured.
4981 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4982 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4983 frame.set_rotation(kVideoRotation_270);
4984 video_source_.IncomingCapturedFrame(frame);
4985 WaitForEncodedFrame(timestamp_ms);
4986
4987 // Set a target rate below the minimum allowed by the codec settings.
4988 VideoCodec codec_config = fake_encoder_.codec_config();
4989 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4990 DataRate target_rate = min_rate - DataRate::kbps(1);
4991 video_stream_encoder_->OnBitrateUpdated(
4992 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004993 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004994 /*link_allocation=*/target_rate,
4995 /*fraction_lost=*/0,
4996 /*rtt_ms=*/0);
4997 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4998
4999 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5000 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5001 ASSERT_TRUE(rate_settings.has_value());
5002 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5003 EXPECT_EQ(min_rate, allocation_sum);
5004 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5005
5006 video_stream_encoder_->Stop();
5007}
5008
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005009TEST_F(VideoStreamEncoderTest, EncoderRatesPropegatedOnReconfigure) {
5010 video_stream_encoder_->OnBitrateUpdated(
5011 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5012 DataRate::bps(kTargetBitrateBps), 0, 0);
5013 // Capture a frame and wait for it to synchronize with the encoder thread.
5014 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5015 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5016 WaitForEncodedFrame(1);
5017
5018 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5019 ASSERT_TRUE(prev_rate_settings.has_value());
5020 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5021 kDefaultFramerate);
5022
5023 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5024 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5025 timestamp_ms += 1000 / kDefaultFramerate;
5026 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5027 WaitForEncodedFrame(timestamp_ms);
5028 }
5029 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5030 kDefaultFramerate);
5031 // Capture larger frame to trigger a reconfigure.
5032 codec_height_ *= 2;
5033 codec_width_ *= 2;
5034 timestamp_ms += 1000 / kDefaultFramerate;
5035 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5036 WaitForEncodedFrame(timestamp_ms);
5037
5038 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5039 auto current_rate_settings =
5040 fake_encoder_.GetAndResetLastRateControlSettings();
5041 // Ensure we have actually reconfigured twice
5042 // The rate settings should have been set again even though
5043 // they haven't changed.
5044 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005045 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005046
5047 video_stream_encoder_->Stop();
5048}
5049
philipeld9cc8c02019-09-16 14:53:40 +02005050struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5051 MOCK_METHOD0(RequestEncoderFallback, void());
5052 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5053};
5054
5055TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5056 constexpr int kDontCare = 100;
5057
5058 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5059 video_send_config_.encoder_settings.encoder_switch_request_callback =
5060 &switch_callback;
5061 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5062 encoder_config.codec_type = kVideoCodecVP8;
5063 webrtc::test::ScopedFieldTrials field_trial(
5064 "WebRTC-NetworkCondition-EncoderSwitch/"
5065 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5066 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5067
5068 // Reset encoder for new configuration to take effect.
5069 ConfigureEncoder(std::move(encoder_config));
5070
5071 // Send one frame to trigger ReconfigureEncoder.
5072 video_source_.IncomingCapturedFrame(
5073 CreateFrame(kDontCare, kDontCare, kDontCare));
5074
5075 using Config = EncoderSwitchRequestCallback::Config;
5076 EXPECT_CALL(switch_callback,
5077 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5078 Field(&Config::param, "ping"),
5079 Field(&Config::value, "pong"))));
5080
5081 video_stream_encoder_->OnBitrateUpdated(
5082 /*target_bitrate=*/DataRate::kbps(50),
5083 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5084 /*link_allocation=*/DataRate::kbps(kDontCare),
5085 /*fraction_lost=*/0,
5086 /*rtt_ms=*/0);
5087
5088 video_stream_encoder_->Stop();
5089}
5090
5091TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5092 constexpr int kSufficientBitrateToNotDrop = 1000;
5093 constexpr int kHighRes = 500;
5094 constexpr int kLowRes = 100;
5095
5096 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5097 video_send_config_.encoder_settings.encoder_switch_request_callback =
5098 &switch_callback;
5099 webrtc::test::ScopedFieldTrials field_trial(
5100 "WebRTC-NetworkCondition-EncoderSwitch/"
5101 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5102 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5103 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5104 encoder_config.codec_type = kVideoCodecH264;
5105
5106 // Reset encoder for new configuration to take effect.
5107 ConfigureEncoder(std::move(encoder_config));
5108
5109 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5110 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5111 // not fail.
5112 video_stream_encoder_->OnBitrateUpdated(
5113 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5114 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5115 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5116 /*fraction_lost=*/0,
5117 /*rtt_ms=*/0);
5118
5119 // Send one frame to trigger ReconfigureEncoder.
5120 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5121 WaitForEncodedFrame(1);
5122
5123 using Config = EncoderSwitchRequestCallback::Config;
5124 EXPECT_CALL(switch_callback,
5125 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5126 Field(&Config::param, "ping"),
5127 Field(&Config::value, "pong"))));
5128
5129 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5130 WaitForEncodedFrame(2);
5131
5132 video_stream_encoder_->Stop();
5133}
5134
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005135TEST_F(VideoStreamEncoderTest,
5136 AllocationPropegratedToEncoderWhenTargetRateChanged) {
5137 const int kFrameWidth = 320;
5138 const int kFrameHeight = 180;
5139
5140 // Set initial rate.
5141 auto rate = DataRate::kbps(100);
5142 video_stream_encoder_->OnBitrateUpdated(
5143 /*target_bitrate=*/rate,
5144 /*stable_target_bitrate=*/rate,
5145 /*link_allocation=*/rate,
5146 /*fraction_lost=*/0,
5147 /*rtt_ms=*/0);
5148
5149 // Insert a first video frame so that encoder gets configured.
5150 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5151 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5152 frame.set_rotation(kVideoRotation_270);
5153 video_source_.IncomingCapturedFrame(frame);
5154 WaitForEncodedFrame(timestamp_ms);
5155 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5156
5157 // Change of target bitrate propagates to the encoder.
5158 auto new_stable_rate = rate - DataRate::kbps(5);
5159 video_stream_encoder_->OnBitrateUpdated(
5160 /*target_bitrate=*/new_stable_rate,
5161 /*stable_target_bitrate=*/new_stable_rate,
5162 /*link_allocation=*/rate,
5163 /*fraction_lost=*/0,
5164 /*rtt_ms=*/0);
5165 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5166 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5167 video_stream_encoder_->Stop();
5168}
5169
5170TEST_F(VideoStreamEncoderTest,
5171 AllocationNotPropegratedToEncoderWhenTargetRateUnchanged) {
5172 const int kFrameWidth = 320;
5173 const int kFrameHeight = 180;
5174
5175 // Set initial rate.
5176 auto rate = DataRate::kbps(100);
5177 video_stream_encoder_->OnBitrateUpdated(
5178 /*target_bitrate=*/rate,
5179 /*stable_target_bitrate=*/rate,
5180 /*link_allocation=*/rate,
5181 /*fraction_lost=*/0,
5182 /*rtt_ms=*/0);
5183
5184 // Insert a first video frame so that encoder gets configured.
5185 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5186 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5187 frame.set_rotation(kVideoRotation_270);
5188 video_source_.IncomingCapturedFrame(frame);
5189 WaitForEncodedFrame(timestamp_ms);
5190 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5191
5192 // Set a higher target rate without changing the link_allocation. Should not
5193 // reset encoder's rate.
5194 auto new_stable_rate = rate - DataRate::kbps(5);
5195 video_stream_encoder_->OnBitrateUpdated(
5196 /*target_bitrate=*/rate,
5197 /*stable_target_bitrate=*/new_stable_rate,
5198 /*link_allocation=*/rate,
5199 /*fraction_lost=*/0,
5200 /*rtt_ms=*/0);
5201 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5202 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5203 video_stream_encoder_->Stop();
5204}
5205
perkj26091b12016-09-01 01:17:40 -07005206} // namespace webrtc