blob: f50afbd9a6846c587d9986f0efec3bb54a600e3d [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();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100322 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800323 test::FrameForwarder::AddOrUpdateSink(sink, wants);
324 }
sprangb1ca0732017-02-01 08:38:12 -0800325 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700326 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
327 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200328 absl::optional<int> last_width_;
329 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800330};
sprangc5d62e22017-04-02 23:53:04 -0700331
Niels Möller213618e2018-07-24 09:29:58 +0200332// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700333class MockableSendStatisticsProxy : public SendStatisticsProxy {
334 public:
335 MockableSendStatisticsProxy(Clock* clock,
336 const VideoSendStream::Config& config,
337 VideoEncoderConfig::ContentType content_type)
338 : SendStatisticsProxy(clock, config, content_type) {}
339
340 VideoSendStream::Stats GetStats() override {
341 rtc::CritScope cs(&lock_);
342 if (mock_stats_)
343 return *mock_stats_;
344 return SendStatisticsProxy::GetStats();
345 }
346
Niels Möller213618e2018-07-24 09:29:58 +0200347 int GetInputFrameRate() const override {
348 rtc::CritScope cs(&lock_);
349 if (mock_stats_)
350 return mock_stats_->input_frame_rate;
351 return SendStatisticsProxy::GetInputFrameRate();
352 }
sprangc5d62e22017-04-02 23:53:04 -0700353 void SetMockStats(const VideoSendStream::Stats& stats) {
354 rtc::CritScope cs(&lock_);
355 mock_stats_.emplace(stats);
356 }
357
358 void ResetMockStats() {
359 rtc::CritScope cs(&lock_);
360 mock_stats_.reset();
361 }
362
363 private:
364 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200365 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700366};
367
sprang4847ae62017-06-27 07:06:52 -0700368class MockBitrateObserver : public VideoBitrateAllocationObserver {
369 public:
Erik Språng566124a2018-04-23 12:32:22 +0200370 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700371};
372
perkj803d97f2016-11-01 11:45:46 -0700373} // namespace
374
mflodmancc3d4422017-08-03 08:27:51 -0700375class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700376 public:
377 static const int kDefaultTimeoutMs = 30 * 1000;
378
mflodmancc3d4422017-08-03 08:27:51 -0700379 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700380 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700381 codec_width_(320),
382 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200383 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200384 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700385 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200386 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700387 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700388 Clock::GetRealTimeClock(),
389 video_send_config_,
390 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700391 sink_(&fake_encoder_) {}
392
393 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700394 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700395 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200396 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800397 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200398 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200399 video_send_config_.rtp.payload_name = "FAKE";
400 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700401
Per512ecb32016-09-23 15:52:06 +0200402 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200403 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700404 video_encoder_config.video_stream_factory =
405 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100406 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700407
408 // Framerate limit is specified by the VideoStreamFactory.
409 std::vector<VideoStream> streams =
410 video_encoder_config.video_stream_factory->CreateEncoderStreams(
411 codec_width_, codec_height_, video_encoder_config);
412 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200413 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700414
Niels Möllerf1338562018-04-26 09:51:47 +0200415 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800416 }
417
Niels Möllerf1338562018-04-26 09:51:47 +0200418 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700419 if (video_stream_encoder_)
420 video_stream_encoder_->Stop();
421 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200422 stats_proxy_.get(), video_send_config_.encoder_settings,
423 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700424 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
425 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700426 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700427 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
428 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200429 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700430 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800431 }
432
433 void ResetEncoder(const std::string& payload_name,
434 size_t num_streams,
435 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700436 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700437 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200438 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800439
440 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200441 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800442 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100443 video_encoder_config.max_bitrate_bps =
444 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800445 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700446 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
447 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700448 video_encoder_config.content_type =
449 screenshare ? VideoEncoderConfig::ContentType::kScreen
450 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700451 if (payload_name == "VP9") {
452 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
453 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
454 video_encoder_config.encoder_specific_settings =
455 new rtc::RefCountedObject<
456 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
457 }
Niels Möllerf1338562018-04-26 09:51:47 +0200458 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700459 }
460
sprang57c2fff2017-01-16 06:24:02 -0800461 VideoFrame CreateFrame(int64_t ntp_time_ms,
462 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100463 VideoFrame frame =
464 VideoFrame::Builder()
465 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
466 destruction_event, codec_width_, codec_height_))
467 .set_timestamp_rtp(99)
468 .set_timestamp_ms(99)
469 .set_rotation(kVideoRotation_0)
470 .build();
sprang57c2fff2017-01-16 06:24:02 -0800471 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700472 return frame;
473 }
474
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100475 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
476 rtc::Event* destruction_event,
477 int offset_x) const {
478 VideoFrame frame =
479 VideoFrame::Builder()
480 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
481 destruction_event, codec_width_, codec_height_))
482 .set_timestamp_rtp(99)
483 .set_timestamp_ms(99)
484 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100485 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100486 .build();
487 frame.set_ntp_time_ms(ntp_time_ms);
488 return frame;
489 }
490
sprang57c2fff2017-01-16 06:24:02 -0800491 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100492 VideoFrame frame =
493 VideoFrame::Builder()
494 .set_video_frame_buffer(
495 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
496 .set_timestamp_rtp(99)
497 .set_timestamp_ms(99)
498 .set_rotation(kVideoRotation_0)
499 .build();
sprang57c2fff2017-01-16 06:24:02 -0800500 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700501 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700502 return frame;
503 }
504
Noah Richards51db4212019-06-12 06:59:12 -0700505 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
506 rtc::Event* destruction_event,
507 int width,
508 int height) const {
509 VideoFrame frame =
510 VideoFrame::Builder()
511 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
512 destruction_event, width, height))
513 .set_timestamp_rtp(99)
514 .set_timestamp_ms(99)
515 .set_rotation(kVideoRotation_0)
516 .build();
517 frame.set_ntp_time_ms(ntp_time_ms);
518 return frame;
519 }
520
521 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
522 rtc::Event* destruction_event) const {
523 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
524 codec_height_);
525 }
526
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100527 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
528 MockBitrateObserver bitrate_observer;
529 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
530
531 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
532 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200533 video_stream_encoder_->OnBitrateUpdated(
534 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
535 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100536
537 video_source_.IncomingCapturedFrame(
538 CreateFrame(1, codec_width_, codec_height_));
539 WaitForEncodedFrame(1);
540 }
541
asapersson02465b82017-04-10 01:12:52 -0700542 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700543 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700544 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
545 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700546 }
547
asapersson09f05612017-05-15 23:40:18 -0700548 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
549 const rtc::VideoSinkWants& wants2) {
550 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
551 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
552 }
553
Åsa Persson8c1bf952018-09-13 10:42:19 +0200554 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
555 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
556 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
557 EXPECT_FALSE(wants.target_pixel_count);
558 }
559
asapersson09f05612017-05-15 23:40:18 -0700560 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
561 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200562 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700563 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
564 EXPECT_GT(wants1.max_pixel_count, 0);
565 }
566
567 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
568 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200569 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700570 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
571 }
572
asaperssonf7e294d2017-06-13 23:25:22 -0700573 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
574 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200575 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700576 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
577 }
578
579 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
580 const rtc::VideoSinkWants& wants2) {
581 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
582 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
583 }
584
585 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
586 const rtc::VideoSinkWants& wants2) {
587 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
588 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
589 }
590
591 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
592 const rtc::VideoSinkWants& wants2) {
593 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
594 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
595 EXPECT_GT(wants1.max_pixel_count, 0);
596 }
597
598 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
599 const rtc::VideoSinkWants& wants2) {
600 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
601 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
602 }
603
asapersson09f05612017-05-15 23:40:18 -0700604 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
605 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200606 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700607 EXPECT_LT(wants.max_pixel_count, pixel_count);
608 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700609 }
610
611 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
612 EXPECT_LT(wants.max_framerate_fps, fps);
613 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
614 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700615 }
616
asaperssonf7e294d2017-06-13 23:25:22 -0700617 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
618 int expected_fps) {
619 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
620 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
621 EXPECT_FALSE(wants.target_pixel_count);
622 }
623
Jonathan Yubc771b72017-12-08 17:04:29 -0800624 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
625 int last_frame_pixels) {
626 // Balanced mode should always scale FPS to the desired range before
627 // attempting to scale resolution.
628 int fps_limit = wants.max_framerate_fps;
629 if (last_frame_pixels <= 320 * 240) {
630 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
631 } else if (last_frame_pixels <= 480 * 270) {
632 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
633 } else if (last_frame_pixels <= 640 * 480) {
634 EXPECT_LE(15, fps_limit);
635 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200636 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800637 }
638 }
639
sprang4847ae62017-06-27 07:06:52 -0700640 void WaitForEncodedFrame(int64_t expected_ntp_time) {
641 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200642 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700643 }
644
645 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
646 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200647 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700648 return ok;
649 }
650
651 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
652 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200653 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700654 }
655
656 void ExpectDroppedFrame() {
657 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200658 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700659 }
660
661 bool WaitForFrame(int64_t timeout_ms) {
662 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200663 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700664 return ok;
665 }
666
perkj26091b12016-09-01 01:17:40 -0700667 class TestEncoder : public test::FakeEncoder {
668 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100669 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700670
asaperssonfab67072017-04-04 05:51:49 -0700671 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800672 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700673 return config_;
674 }
675
676 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800677 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700678 block_next_encode_ = true;
679 }
680
Erik Språngaed30702018-11-05 12:57:17 +0100681 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800682 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100683 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100684 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100685 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100686 info.scaling_settings = VideoEncoder::ScalingSettings(
687 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100688 }
689 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100690 for (int i = 0; i < kMaxSpatialLayers; ++i) {
691 if (temporal_layers_supported_[i]) {
692 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
693 info.fps_allocation[i].resize(num_layers);
694 }
695 }
Erik Språngaed30702018-11-05 12:57:17 +0100696 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200697
698 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Erik Språngaed30702018-11-05 12:57:17 +0100699 return info;
kthelgason876222f2016-11-29 01:44:11 -0800700 }
701
Erik Språngb7cb7b52019-02-26 15:52:33 +0100702 int32_t RegisterEncodeCompleteCallback(
703 EncodedImageCallback* callback) override {
704 rtc::CritScope lock(&local_crit_sect_);
705 encoded_image_callback_ = callback;
706 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
707 }
708
perkjfa10b552016-10-02 23:45:26 -0700709 void ContinueEncode() { continue_encode_event_.Set(); }
710
711 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
712 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800713 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700714 EXPECT_EQ(timestamp_, timestamp);
715 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
716 }
717
kthelgason2fc52542017-03-03 00:24:41 -0800718 void SetQualityScaling(bool b) {
719 rtc::CritScope lock(&local_crit_sect_);
720 quality_scaling_ = b;
721 }
kthelgasonad9010c2017-02-14 00:46:51 -0800722
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100723 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
724 rtc::CritScope lock(&local_crit_sect_);
725 is_hardware_accelerated_ = is_hardware_accelerated;
726 }
727
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100728 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
729 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
730 rtc::CritScope lock(&local_crit_sect_);
731 temporal_layers_supported_[spatial_idx] = supported;
732 }
733
Sergey Silkin6456e352019-07-08 17:56:40 +0200734 void SetResolutionBitrateLimits(
735 std::vector<ResolutionBitrateLimits> thresholds) {
736 rtc::CritScope cs(&local_crit_sect_);
737 resolution_bitrate_limits_ = thresholds;
738 }
739
sprangfe627f32017-03-29 08:24:59 -0700740 void ForceInitEncodeFailure(bool force_failure) {
741 rtc::CritScope lock(&local_crit_sect_);
742 force_init_encode_failed_ = force_failure;
743 }
744
Niels Möller6bb5ab92019-01-11 11:11:10 +0100745 void SimulateOvershoot(double rate_factor) {
746 rtc::CritScope lock(&local_crit_sect_);
747 rate_factor_ = rate_factor;
748 }
749
Erik Språngd7329ca2019-02-21 21:19:53 +0100750 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100751 rtc::CritScope lock(&local_crit_sect_);
752 return last_framerate_;
753 }
754
Erik Språngd7329ca2019-02-21 21:19:53 +0100755 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100756 rtc::CritScope lock(&local_crit_sect_);
757 return last_update_rect_;
758 }
759
Niels Möller87e2d782019-03-07 10:18:23 +0100760 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100761 rtc::CritScope lock(&local_crit_sect_);
762 return last_frame_types_;
763 }
764
765 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100766 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100767 keyframe ? VideoFrameType::kVideoFrameKey
768 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100769 {
770 rtc::CritScope lock(&local_crit_sect_);
771 last_frame_types_ = frame_type;
772 }
Niels Möllerb859b322019-03-07 12:40:01 +0100773 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100774 }
775
Erik Språngb7cb7b52019-02-26 15:52:33 +0100776 void InjectEncodedImage(const EncodedImage& image) {
777 rtc::CritScope lock(&local_crit_sect_);
778 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
779 }
780
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200781 void InjectEncodedImage(const EncodedImage& image,
782 const CodecSpecificInfo* codec_specific_info,
783 const RTPFragmentationHeader* fragmentation) {
784 rtc::CritScope lock(&local_crit_sect_);
785 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
786 fragmentation);
787 }
788
Erik Språngd7329ca2019-02-21 21:19:53 +0100789 void ExpectNullFrame() {
790 rtc::CritScope lock(&local_crit_sect_);
791 expect_null_frame_ = true;
792 }
793
Erik Språng5056af02019-09-02 15:53:11 +0200794 absl::optional<VideoEncoder::RateControlParameters>
795 GetAndResetLastRateControlSettings() {
796 auto settings = last_rate_control_settings_;
797 last_rate_control_settings_.reset();
798 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100799 }
800
Sergey Silkin5ee69672019-07-02 14:18:34 +0200801 int GetNumEncoderInitializations() const {
802 rtc::CritScope lock(&local_crit_sect_);
803 return num_encoder_initializations_;
804 }
805
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200806 int GetNumSetRates() const {
807 rtc::CritScope lock(&local_crit_sect_);
808 return num_set_rates_;
809 }
810
perkjfa10b552016-10-02 23:45:26 -0700811 private:
perkj26091b12016-09-01 01:17:40 -0700812 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100813 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700814 bool block_encode;
815 {
brandtre78d2662017-01-16 05:57:16 -0800816 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100817 if (expect_null_frame_) {
818 EXPECT_EQ(input_image.timestamp(), 0u);
819 EXPECT_EQ(input_image.width(), 1);
820 last_frame_types_ = *frame_types;
821 expect_null_frame_ = false;
822 } else {
823 EXPECT_GT(input_image.timestamp(), timestamp_);
824 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
825 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
826 }
perkj26091b12016-09-01 01:17:40 -0700827
828 timestamp_ = input_image.timestamp();
829 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700830 last_input_width_ = input_image.width();
831 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700832 block_encode = block_next_encode_;
833 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100834 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100835 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700836 }
Niels Möllerb859b322019-03-07 12:40:01 +0100837 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700838 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700839 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700840 return result;
841 }
842
sprangfe627f32017-03-29 08:24:59 -0700843 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200844 const Settings& settings) override {
845 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200846
sprangfe627f32017-03-29 08:24:59 -0700847 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100848 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200849
850 ++num_encoder_initializations_;
851
Erik Språng82fad3d2018-03-21 09:57:23 +0100852 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700853 // Simulate setting up temporal layers, in order to validate the life
854 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100855 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200856 frame_buffer_controller_ =
857 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700858 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100859 if (force_init_encode_failed_) {
860 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700861 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100862 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100863
Erik Språngb7cb7b52019-02-26 15:52:33 +0100864 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700865 return res;
866 }
867
Erik Språngb7cb7b52019-02-26 15:52:33 +0100868 int32_t Release() override {
869 rtc::CritScope lock(&local_crit_sect_);
870 EXPECT_NE(initialized_, EncoderState::kUninitialized);
871 initialized_ = EncoderState::kUninitialized;
872 return FakeEncoder::Release();
873 }
874
Erik Språng16cb8f52019-04-12 13:59:09 +0200875 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100876 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200877 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100878 VideoBitrateAllocation adjusted_rate_allocation;
879 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
880 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200881 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100882 adjusted_rate_allocation.SetBitrate(
883 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200884 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100885 rate_factor_));
886 }
887 }
888 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200889 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200890 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200891 RateControlParameters adjusted_paramters = parameters;
892 adjusted_paramters.bitrate = adjusted_rate_allocation;
893 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100894 }
895
brandtre78d2662017-01-16 05:57:16 -0800896 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100897 enum class EncoderState {
898 kUninitialized,
899 kInitializationFailed,
900 kInitialized
901 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
902 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700903 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700904 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700905 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
906 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
907 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
908 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
909 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100910 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100911 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700912 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100913 absl::optional<bool>
914 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
915 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700916 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100917 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
918 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200919 absl::optional<VideoEncoder::RateControlParameters>
920 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100921 VideoFrame::UpdateRect last_update_rect_
922 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100923 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100924 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100925 EncodedImageCallback* encoded_image_callback_
926 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200927 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200928 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200929 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
930 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200931 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700932 };
933
mflodmancc3d4422017-08-03 08:27:51 -0700934 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700935 public:
936 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100937 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700938
perkj26091b12016-09-01 01:17:40 -0700939 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700940 EXPECT_TRUE(
941 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
942 }
943
944 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
945 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700946 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700947 if (!encoded_frame_event_.Wait(timeout_ms))
948 return false;
perkj26091b12016-09-01 01:17:40 -0700949 {
950 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800951 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700952 }
953 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700954 return true;
perkj26091b12016-09-01 01:17:40 -0700955 }
956
sprangb1ca0732017-02-01 08:38:12 -0800957 void WaitForEncodedFrame(uint32_t expected_width,
958 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700959 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100960 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700961 }
962
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100963 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700964 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800965 uint32_t width = 0;
966 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800967 {
968 rtc::CritScope lock(&crit_);
969 width = last_width_;
970 height = last_height_;
971 }
972 EXPECT_EQ(expected_height, height);
973 EXPECT_EQ(expected_width, width);
974 }
975
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +0200976 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
977 VideoRotation rotation;
978 {
979 rtc::CritScope lock(&crit_);
980 rotation = last_rotation_;
981 }
982 EXPECT_EQ(expected_rotation, rotation);
983 }
984
kthelgason2fc52542017-03-03 00:24:41 -0800985 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -0800986
sprangc5d62e22017-04-02 23:53:04 -0700987 bool WaitForFrame(int64_t timeout_ms) {
988 return encoded_frame_event_.Wait(timeout_ms);
989 }
990
perkj26091b12016-09-01 01:17:40 -0700991 void SetExpectNoFrames() {
992 rtc::CritScope lock(&crit_);
993 expect_frames_ = false;
994 }
995
asaperssonfab67072017-04-04 05:51:49 -0700996 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +0200997 rtc::CritScope lock(&crit_);
998 return number_of_reconfigurations_;
999 }
1000
asaperssonfab67072017-04-04 05:51:49 -07001001 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001002 rtc::CritScope lock(&crit_);
1003 return min_transmit_bitrate_bps_;
1004 }
1005
Erik Språngd7329ca2019-02-21 21:19:53 +01001006 void SetNumExpectedLayers(size_t num_layers) {
1007 rtc::CritScope lock(&crit_);
1008 num_expected_layers_ = num_layers;
1009 }
1010
Erik Språngb7cb7b52019-02-26 15:52:33 +01001011 int64_t GetLastCaptureTimeMs() const {
1012 rtc::CritScope lock(&crit_);
1013 return last_capture_time_ms_;
1014 }
1015
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001016 std::vector<uint8_t> GetLastEncodedImageData() {
1017 rtc::CritScope lock(&crit_);
1018 return std::move(last_encoded_image_data_);
1019 }
1020
1021 RTPFragmentationHeader GetLastFragmentation() {
1022 rtc::CritScope lock(&crit_);
1023 return std::move(last_fragmentation_);
1024 }
1025
perkj26091b12016-09-01 01:17:40 -07001026 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001027 Result OnEncodedImage(
1028 const EncodedImage& encoded_image,
1029 const CodecSpecificInfo* codec_specific_info,
1030 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001031 rtc::CritScope lock(&crit_);
1032 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001033 last_encoded_image_data_ = std::vector<uint8_t>(
1034 encoded_image.data(), encoded_image.data() + encoded_image.size());
1035 if (fragmentation) {
1036 last_fragmentation_.CopyFrom(*fragmentation);
1037 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001038 uint32_t timestamp = encoded_image.Timestamp();
1039 if (last_timestamp_ != timestamp) {
1040 num_received_layers_ = 1;
1041 } else {
1042 ++num_received_layers_;
1043 }
1044 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001045 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001046 last_width_ = encoded_image._encodedWidth;
1047 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001048 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001049 if (num_received_layers_ == num_expected_layers_) {
1050 encoded_frame_event_.Set();
1051 }
sprangb1ca0732017-02-01 08:38:12 -08001052 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001053 }
1054
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001055 void OnEncoderConfigurationChanged(
1056 std::vector<VideoStream> streams,
1057 VideoEncoderConfig::ContentType content_type,
1058 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001059 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001060 ++number_of_reconfigurations_;
1061 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1062 }
1063
perkj26091b12016-09-01 01:17:40 -07001064 rtc::CriticalSection crit_;
1065 TestEncoder* test_encoder_;
1066 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001067 std::vector<uint8_t> last_encoded_image_data_;
1068 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001069 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001070 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001071 uint32_t last_height_ = 0;
1072 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001073 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001074 size_t num_expected_layers_ = 1;
1075 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001076 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001077 int number_of_reconfigurations_ = 0;
1078 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001079 };
1080
Sergey Silkin5ee69672019-07-02 14:18:34 +02001081 class VideoBitrateAllocatorProxyFactory
1082 : public VideoBitrateAllocatorFactory {
1083 public:
1084 VideoBitrateAllocatorProxyFactory()
1085 : bitrate_allocator_factory_(
1086 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1087
1088 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1089 const VideoCodec& codec) override {
1090 rtc::CritScope lock(&crit_);
1091 codec_config_ = codec;
1092 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1093 }
1094
1095 VideoCodec codec_config() const {
1096 rtc::CritScope lock(&crit_);
1097 return codec_config_;
1098 }
1099
1100 private:
1101 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1102
1103 rtc::CriticalSection crit_;
1104 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1105 };
1106
perkj26091b12016-09-01 01:17:40 -07001107 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001108 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001109 int codec_width_;
1110 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001111 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001112 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001113 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001114 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001115 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001116 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001117 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001118 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001119 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001120 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001121};
1122
mflodmancc3d4422017-08-03 08:27:51 -07001123TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001124 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001125 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1126 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001127 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001128 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001129 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001130 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001131 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001132}
1133
mflodmancc3d4422017-08-03 08:27:51 -07001134TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001135 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001136 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001137 // The encoder will cache up to one frame for a short duration. Adding two
1138 // frames means that the first frame will be dropped and the second frame will
1139 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001140 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001141 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001142 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001143
Erik Språng4c6ca302019-04-08 15:14:01 +02001144 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001145 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1146 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001147
Sebastian Janssona3177052018-04-10 13:05:49 +02001148 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001149 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001150 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1151
1152 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001153 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001154}
1155
mflodmancc3d4422017-08-03 08:27:51 -07001156TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001157 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001158 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1159 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001160 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001161 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001162
Florent Castellia8336d32019-09-09 13:36:55 +02001163 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1164 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001165 // The encoder will cache up to one frame for a short duration. Adding two
1166 // frames means that the first frame will be dropped and the second frame will
1167 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001168 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001169 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001170
Erik Språng4c6ca302019-04-08 15:14:01 +02001171 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001172 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1173 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001174 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001175 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1176 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001177 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001178}
1179
mflodmancc3d4422017-08-03 08:27:51 -07001180TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001181 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001182 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1183 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001184 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001185 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001186
1187 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001188 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001189
perkja49cbd32016-09-16 07:53:41 -07001190 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001191 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001192 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001193}
1194
mflodmancc3d4422017-08-03 08:27:51 -07001195TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001196 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001197 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1198 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001199
perkja49cbd32016-09-16 07:53:41 -07001200 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001201 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001202
mflodmancc3d4422017-08-03 08:27:51 -07001203 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001204 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001205 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001206 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1207 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001208}
1209
mflodmancc3d4422017-08-03 08:27:51 -07001210TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001211 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001212 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1213 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001214
1215 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001216 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001217 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001218 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1219 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001220 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1221 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001222 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001223 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001224
mflodmancc3d4422017-08-03 08:27:51 -07001225 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001226}
1227
Noah Richards51db4212019-06-12 06:59:12 -07001228TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1229 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001230 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1231 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001232
1233 rtc::Event frame_destroyed_event;
1234 video_source_.IncomingCapturedFrame(
1235 CreateFakeNativeFrame(1, &frame_destroyed_event));
1236 ExpectDroppedFrame();
1237 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1238 video_stream_encoder_->Stop();
1239}
1240
1241TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1242 // Use the cropping factory.
1243 video_encoder_config_.video_stream_factory =
1244 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1245 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1246 kMaxPayloadLength);
1247 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1248
1249 // Capture a frame at codec_width_/codec_height_.
1250 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001251 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1252 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001253 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1254 WaitForEncodedFrame(1);
1255 // The encoder will have been configured once.
1256 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1257 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1258 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1259
1260 // Now send in a fake frame that needs to be cropped as the width/height
1261 // aren't divisible by 4 (see CreateEncoderStreams above).
1262 rtc::Event frame_destroyed_event;
1263 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1264 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1265 ExpectDroppedFrame();
1266 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1267 video_stream_encoder_->Stop();
1268}
1269
mflodmancc3d4422017-08-03 08:27:51 -07001270TEST_F(VideoStreamEncoderTest,
1271 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001272 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001273 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1274 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001275 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001276
1277 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001278 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001279 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001280 // The encoder will have been configured once when the first frame is
1281 // received.
1282 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001283
1284 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001285 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001286 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001287 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001288 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001289
1290 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001291 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001292 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001293 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001294 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001295
mflodmancc3d4422017-08-03 08:27:51 -07001296 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001297}
1298
mflodmancc3d4422017-08-03 08:27:51 -07001299TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001300 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001301 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1302 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001303
1304 // Capture a frame and wait for it to synchronize with the encoder thread.
1305 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001306 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001307 // The encoder will have been configured once.
1308 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001309 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1310 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1311
1312 codec_width_ *= 2;
1313 codec_height_ *= 2;
1314 // Capture a frame with a higher resolution and wait for it to synchronize
1315 // with the encoder thread.
1316 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001317 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001318 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1319 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001320 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001321
mflodmancc3d4422017-08-03 08:27:51 -07001322 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001323}
1324
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001325TEST_F(VideoStreamEncoderTest,
1326 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1327 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001328 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1329 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001330
1331 // Capture a frame and wait for it to synchronize with the encoder thread.
1332 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1333 WaitForEncodedFrame(1);
1334
1335 VideoEncoderConfig video_encoder_config;
1336 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1337 // Changing the max payload data length recreates encoder.
1338 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1339 kMaxPayloadLength / 2);
1340
1341 // Capture a frame and wait for it to synchronize with the encoder thread.
1342 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1343 WaitForEncodedFrame(2);
1344 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1345
1346 video_stream_encoder_->Stop();
1347}
1348
Sergey Silkin5ee69672019-07-02 14:18:34 +02001349TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1350 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001351 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1352 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001353
1354 VideoEncoderConfig video_encoder_config;
1355 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1356 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1357 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1358 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1359 kMaxPayloadLength);
1360
1361 // Capture a frame and wait for it to synchronize with the encoder thread.
1362 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1363 WaitForEncodedFrame(1);
1364 // The encoder will have been configured once when the first frame is
1365 // received.
1366 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1367 EXPECT_EQ(kTargetBitrateBps,
1368 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1369 EXPECT_EQ(kStartBitrateBps,
1370 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1371
Sergey Silkin6456e352019-07-08 17:56:40 +02001372 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1373 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001374 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1375 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1376 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1377 kMaxPayloadLength);
1378
1379 // Capture a frame and wait for it to synchronize with the encoder thread.
1380 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1381 WaitForEncodedFrame(2);
1382 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1383 // Bitrate limits have changed - rate allocator should be reconfigured,
1384 // encoder should not be reconfigured.
1385 EXPECT_EQ(kTargetBitrateBps * 2,
1386 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1387 EXPECT_EQ(kStartBitrateBps * 2,
1388 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1389 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1390
1391 video_stream_encoder_->Stop();
1392}
1393
Sergey Silkin6456e352019-07-08 17:56:40 +02001394TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001395 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001396 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001397 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1398 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001399
Sergey Silkin6456e352019-07-08 17:56:40 +02001400 VideoEncoderConfig video_encoder_config;
1401 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1402 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001403 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001404 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1405 kMaxPayloadLength);
1406
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001407 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001408 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001409
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001410 // Get the default bitrate limits and use them as baseline for custom
1411 // application and encoder recommended limits.
1412 const uint32_t kDefaultMinBitrateKbps =
1413 bitrate_allocator_factory_.codec_config().minBitrate;
1414 const uint32_t kDefaultMaxBitrateKbps =
1415 bitrate_allocator_factory_.codec_config().maxBitrate;
1416 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1417 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1418 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1419 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1420
1421 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1422 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1423 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1424 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1425
1426 // Change resolution. This will trigger encoder re-configuration and video
1427 // stream encoder will pick up the bitrate limits recommended by encoder.
1428 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1429 WaitForEncodedFrame(2);
1430 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1431 WaitForEncodedFrame(3);
1432
1433 // App bitrate limits are not set - bitrate limits recommended by encoder
1434 // should be used.
1435 EXPECT_EQ(kEncMaxBitrateKbps,
1436 bitrate_allocator_factory_.codec_config().maxBitrate);
1437 EXPECT_EQ(kEncMinBitrateKbps,
1438 bitrate_allocator_factory_.codec_config().minBitrate);
1439
1440 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1441 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1442 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1443 kMaxPayloadLength);
1444 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1445 WaitForEncodedFrame(4);
1446
1447 // App limited the max bitrate - bitrate limits recommended by encoder should
1448 // not be applied.
1449 EXPECT_EQ(kAppMaxBitrateKbps,
1450 bitrate_allocator_factory_.codec_config().maxBitrate);
1451 EXPECT_EQ(kDefaultMinBitrateKbps,
1452 bitrate_allocator_factory_.codec_config().minBitrate);
1453
1454 video_encoder_config.max_bitrate_bps = 0;
1455 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1456 kAppMinBitrateKbps * 1000;
1457 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1458 kMaxPayloadLength);
1459 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1460 WaitForEncodedFrame(5);
1461
1462 // App limited the min bitrate - bitrate limits recommended by encoder should
1463 // not be applied.
1464 EXPECT_EQ(kDefaultMaxBitrateKbps,
1465 bitrate_allocator_factory_.codec_config().maxBitrate);
1466 EXPECT_EQ(kAppMinBitrateKbps,
1467 bitrate_allocator_factory_.codec_config().minBitrate);
1468
1469 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1470 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1471 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001472 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1473 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001474 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1475 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001476
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001477 // App limited both min and max bitrates - bitrate limits recommended by
1478 // encoder should not be applied.
1479 EXPECT_EQ(kAppMaxBitrateKbps,
1480 bitrate_allocator_factory_.codec_config().maxBitrate);
1481 EXPECT_EQ(kAppMinBitrateKbps,
1482 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001483
1484 video_stream_encoder_->Stop();
1485}
1486
1487TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001488 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001489 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001490 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1491 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001492
1493 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001494 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001495 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001496 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001497 fake_encoder_.SetResolutionBitrateLimits(
1498 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1499
1500 VideoEncoderConfig video_encoder_config;
1501 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1502 video_encoder_config.max_bitrate_bps = 0;
1503 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1504 kMaxPayloadLength);
1505
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001506 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001507 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1508 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001509 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1510 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001511 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1512 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1513
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001514 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001515 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1516 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001517 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1518 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001519 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1520 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1521
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001522 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001523 // encoder for 360p should be used.
1524 video_source_.IncomingCapturedFrame(
1525 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1526 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001527 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1528 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001529 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1530 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1531
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001532 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001533 // ignored.
1534 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1535 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001536 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1537 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001538 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1539 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001540 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1541 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001542 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1543 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1544
1545 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1546 // for 270p should be used.
1547 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1548 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001549 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1550 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001551 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1552 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1553
1554 video_stream_encoder_->Stop();
1555}
1556
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001557TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1558 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001559 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1560 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001561
1562 VideoEncoderConfig video_encoder_config;
1563 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1564 video_encoder_config.max_bitrate_bps = 0;
1565 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1566 kMaxPayloadLength);
1567
1568 // Encode 720p frame to get the default encoder target bitrate.
1569 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1570 WaitForEncodedFrame(1);
1571 const uint32_t kDefaultTargetBitrateFor720pKbps =
1572 bitrate_allocator_factory_.codec_config()
1573 .simulcastStream[0]
1574 .targetBitrate;
1575
1576 // Set the max recommended encoder bitrate to something lower than the default
1577 // target bitrate.
1578 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1579 1280 * 720, 10 * 1000, 10 * 1000,
1580 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1581 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1582
1583 // Change resolution to trigger encoder reinitialization.
1584 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1585 WaitForEncodedFrame(2);
1586 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1587 WaitForEncodedFrame(3);
1588
1589 // Ensure the target bitrate is capped by the max bitrate.
1590 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1591 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1592 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1593 .simulcastStream[0]
1594 .targetBitrate *
1595 1000,
1596 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1597
1598 video_stream_encoder_->Stop();
1599}
1600
mflodmancc3d4422017-08-03 08:27:51 -07001601TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001602 EXPECT_TRUE(video_source_.has_sinks());
1603 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001604 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001605 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001606 EXPECT_FALSE(video_source_.has_sinks());
1607 EXPECT_TRUE(new_video_source.has_sinks());
1608
mflodmancc3d4422017-08-03 08:27:51 -07001609 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001610}
1611
mflodmancc3d4422017-08-03 08:27:51 -07001612TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001613 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001614 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001615 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001616 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001617}
1618
Jonathan Yubc771b72017-12-08 17:04:29 -08001619TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1620 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001621 const int kWidth = 1280;
1622 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001623
1624 // We rely on the automatic resolution adaptation, but we handle framerate
1625 // adaptation manually by mocking the stats proxy.
1626 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001627
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001628 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001629 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001630 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1631 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001632 video_stream_encoder_->SetSource(&video_source_,
1633 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001634 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001635 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001636 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001637 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1638
Jonathan Yubc771b72017-12-08 17:04:29 -08001639 // Adapt down as far as possible.
1640 rtc::VideoSinkWants last_wants;
1641 int64_t t = 1;
1642 int loop_count = 0;
1643 do {
1644 ++loop_count;
1645 last_wants = video_source_.sink_wants();
1646
1647 // Simulate the framerate we've been asked to adapt to.
1648 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1649 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1650 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1651 mock_stats.input_frame_rate = fps;
1652 stats_proxy_->SetMockStats(mock_stats);
1653
1654 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1655 sink_.WaitForEncodedFrame(t);
1656 t += frame_interval_ms;
1657
mflodmancc3d4422017-08-03 08:27:51 -07001658 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001659 VerifyBalancedModeFpsRange(
1660 video_source_.sink_wants(),
1661 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1662 } while (video_source_.sink_wants().max_pixel_count <
1663 last_wants.max_pixel_count ||
1664 video_source_.sink_wants().max_framerate_fps <
1665 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001666
Jonathan Yubc771b72017-12-08 17:04:29 -08001667 // Verify that we've adapted all the way down.
1668 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001669 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001670 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1671 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001672 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001673 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1674 *video_source_.last_sent_height());
1675 EXPECT_EQ(kMinBalancedFramerateFps,
1676 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001677
Jonathan Yubc771b72017-12-08 17:04:29 -08001678 // Adapt back up the same number of times we adapted down.
1679 for (int i = 0; i < loop_count - 1; ++i) {
1680 last_wants = video_source_.sink_wants();
1681
1682 // Simulate the framerate we've been asked to adapt to.
1683 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1684 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1685 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1686 mock_stats.input_frame_rate = fps;
1687 stats_proxy_->SetMockStats(mock_stats);
1688
1689 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1690 sink_.WaitForEncodedFrame(t);
1691 t += frame_interval_ms;
1692
mflodmancc3d4422017-08-03 08:27:51 -07001693 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001694 VerifyBalancedModeFpsRange(
1695 video_source_.sink_wants(),
1696 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1697 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1698 last_wants.max_pixel_count ||
1699 video_source_.sink_wants().max_framerate_fps >
1700 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001701 }
1702
Åsa Persson8c1bf952018-09-13 10:42:19 +02001703 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001704 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001705 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001706 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1707 EXPECT_EQ((loop_count - 1) * 2,
1708 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001709
mflodmancc3d4422017-08-03 08:27:51 -07001710 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001711}
mflodmancc3d4422017-08-03 08:27:51 -07001712TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001713 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001714 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1715 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001716 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001717
sprangc5d62e22017-04-02 23:53:04 -07001718 const int kFrameWidth = 1280;
1719 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001720
Åsa Persson8c1bf952018-09-13 10:42:19 +02001721 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001722
kthelgason5e13d412016-12-01 03:59:51 -08001723 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001724 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001725 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001726 frame_timestamp += kFrameIntervalMs;
1727
perkj803d97f2016-11-01 11:45:46 -07001728 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001729 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001730 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001731 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001732 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001733 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001734
asapersson0944a802017-04-07 00:57:58 -07001735 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001736 // wanted resolution.
1737 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1738 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1739 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001740 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001741
1742 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001743 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001744 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001745 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001746
sprangc5d62e22017-04-02 23:53:04 -07001747 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001748 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001749
sprangc5d62e22017-04-02 23:53:04 -07001750 // Force an input frame rate to be available, or the adaptation call won't
1751 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001752 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001753 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001754 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001755 stats_proxy_->SetMockStats(stats);
1756
mflodmancc3d4422017-08-03 08:27:51 -07001757 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001758 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001759 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001760 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001761 frame_timestamp += kFrameIntervalMs;
1762
1763 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001764 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001765 EXPECT_EQ(std::numeric_limits<int>::max(),
1766 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001767 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001768
asapersson02465b82017-04-10 01:12:52 -07001769 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001770 video_stream_encoder_->SetSource(&new_video_source,
1771 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001772 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001773
mflodmancc3d4422017-08-03 08:27:51 -07001774 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001775 new_video_source.IncomingCapturedFrame(
1776 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001777 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001778 frame_timestamp += kFrameIntervalMs;
1779
1780 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001781 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001782
1783 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001784 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001785 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001786 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1787 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001788 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001789 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001790
1791 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001792 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001793 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001794 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1795 EXPECT_EQ(std::numeric_limits<int>::max(),
1796 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001797 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001798
mflodmancc3d4422017-08-03 08:27:51 -07001799 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001800}
1801
mflodmancc3d4422017-08-03 08:27:51 -07001802TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001803 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001804 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1805 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001806
asaperssonfab67072017-04-04 05:51:49 -07001807 const int kWidth = 1280;
1808 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001809 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001810 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001811 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1812 EXPECT_FALSE(stats.bw_limited_resolution);
1813 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1814
1815 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001816 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001817 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001818 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001819
1820 stats = stats_proxy_->GetStats();
1821 EXPECT_TRUE(stats.bw_limited_resolution);
1822 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1823
1824 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001825 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001826 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001827 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001828
1829 stats = stats_proxy_->GetStats();
1830 EXPECT_FALSE(stats.bw_limited_resolution);
1831 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1832 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1833
mflodmancc3d4422017-08-03 08:27:51 -07001834 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001835}
1836
mflodmancc3d4422017-08-03 08:27:51 -07001837TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001838 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001839 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1840 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001841
1842 const int kWidth = 1280;
1843 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001844 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001845 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001846 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1847 EXPECT_FALSE(stats.cpu_limited_resolution);
1848 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1849
1850 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001851 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001852 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001853 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001854
1855 stats = stats_proxy_->GetStats();
1856 EXPECT_TRUE(stats.cpu_limited_resolution);
1857 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1858
1859 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001860 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001861 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001862 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001863
1864 stats = stats_proxy_->GetStats();
1865 EXPECT_FALSE(stats.cpu_limited_resolution);
1866 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001867 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001868
mflodmancc3d4422017-08-03 08:27:51 -07001869 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001870}
1871
mflodmancc3d4422017-08-03 08:27:51 -07001872TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001873 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001874 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1875 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001876
asaperssonfab67072017-04-04 05:51:49 -07001877 const int kWidth = 1280;
1878 const int kHeight = 720;
1879 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001880 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001881 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001882 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001883 EXPECT_FALSE(stats.cpu_limited_resolution);
1884 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1885
asaperssonfab67072017-04-04 05:51:49 -07001886 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001887 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001888 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001889 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001890 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001891 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001892 EXPECT_TRUE(stats.cpu_limited_resolution);
1893 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1894
1895 // Set new source with adaptation still enabled.
1896 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001897 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001898 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001899
asaperssonfab67072017-04-04 05:51:49 -07001900 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001901 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001902 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001903 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001904 EXPECT_TRUE(stats.cpu_limited_resolution);
1905 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1906
1907 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001908 video_stream_encoder_->SetSource(&new_video_source,
1909 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001910
asaperssonfab67072017-04-04 05:51:49 -07001911 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001912 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001913 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001914 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001915 EXPECT_FALSE(stats.cpu_limited_resolution);
1916 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1917
1918 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001919 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001920 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001921
asaperssonfab67072017-04-04 05:51:49 -07001922 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001923 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001924 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001925 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001926 EXPECT_TRUE(stats.cpu_limited_resolution);
1927 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1928
asaperssonfab67072017-04-04 05:51:49 -07001929 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001930 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001931 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001932 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001933 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001934 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001935 EXPECT_FALSE(stats.cpu_limited_resolution);
1936 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001937 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001938
mflodmancc3d4422017-08-03 08:27:51 -07001939 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001940}
1941
mflodmancc3d4422017-08-03 08:27:51 -07001942TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001943 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001944 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1945 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001946
asaperssonfab67072017-04-04 05:51:49 -07001947 const int kWidth = 1280;
1948 const int kHeight = 720;
1949 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001950 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001951 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001952 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001953 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001954 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001955
1956 // Set new source with adaptation still enabled.
1957 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001958 video_stream_encoder_->SetSource(&new_video_source,
1959 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001960
asaperssonfab67072017-04-04 05:51:49 -07001961 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001962 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001963 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001964 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001965 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001966 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001967
asaperssonfab67072017-04-04 05:51:49 -07001968 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001969 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001970 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001971 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001972 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001973 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001974 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001975 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001976
asaperssonfab67072017-04-04 05:51:49 -07001977 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001978 video_stream_encoder_->SetSource(&new_video_source,
1979 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001980
asaperssonfab67072017-04-04 05:51:49 -07001981 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001982 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001983 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001984 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001985 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001986 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001987
asapersson02465b82017-04-10 01:12:52 -07001988 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07001989 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001990 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08001991
asaperssonfab67072017-04-04 05:51:49 -07001992 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001993 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001994 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001995 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001996 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001997 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1998 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001999
mflodmancc3d4422017-08-03 08:27:51 -07002000 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002001}
2002
mflodmancc3d4422017-08-03 08:27:51 -07002003TEST_F(VideoStreamEncoderTest,
2004 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002005 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002006 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2007 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002008
2009 const int kWidth = 1280;
2010 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002011 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002012 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002013 video_source_.IncomingCapturedFrame(
2014 CreateFrame(timestamp_ms, kWidth, kHeight));
2015 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002016 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2017 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2018 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2019
2020 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002021 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002022 timestamp_ms += kFrameIntervalMs;
2023 video_source_.IncomingCapturedFrame(
2024 CreateFrame(timestamp_ms, kWidth, kHeight));
2025 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002026 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2027 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2028 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2029
2030 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002031 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002032 timestamp_ms += kFrameIntervalMs;
2033 video_source_.IncomingCapturedFrame(
2034 CreateFrame(timestamp_ms, kWidth, kHeight));
2035 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002036 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2037 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2038 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2039
Niels Möller4db138e2018-04-19 09:04:13 +02002040 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002041 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002042
2043 VideoEncoderConfig video_encoder_config;
2044 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2045 // Make format different, to force recreation of encoder.
2046 video_encoder_config.video_format.parameters["foo"] = "foo";
2047 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002048 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002049 timestamp_ms += kFrameIntervalMs;
2050 video_source_.IncomingCapturedFrame(
2051 CreateFrame(timestamp_ms, kWidth, kHeight));
2052 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002053 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2054 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2055 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2056
mflodmancc3d4422017-08-03 08:27:51 -07002057 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002058}
2059
mflodmancc3d4422017-08-03 08:27:51 -07002060TEST_F(VideoStreamEncoderTest,
2061 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002062 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002063 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2064 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002065
asapersson0944a802017-04-07 00:57:58 -07002066 const int kWidth = 1280;
2067 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002068 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002069
asaperssonfab67072017-04-04 05:51:49 -07002070 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002071 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002072 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002073 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002074 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002075 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2076
asapersson02465b82017-04-10 01:12:52 -07002077 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002078 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002079 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002080 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002081 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002082 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002083 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002084 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2085
2086 // Set new source with adaptation still enabled.
2087 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002088 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002089 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002090
2091 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002092 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002093 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002094 stats = stats_proxy_->GetStats();
2095 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002096 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002097 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2098
sprangc5d62e22017-04-02 23:53:04 -07002099 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002100 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002101 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002102 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002103 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002104 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002105 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002106 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002107 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002108 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002109 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2110
sprangc5d62e22017-04-02 23:53:04 -07002111 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002112 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002113 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2114 mock_stats.input_frame_rate = 30;
2115 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002116 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002117 stats_proxy_->ResetMockStats();
2118
2119 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002120 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002121 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002122
2123 // Framerate now adapted.
2124 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002125 EXPECT_FALSE(stats.cpu_limited_resolution);
2126 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002127 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2128
2129 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002130 video_stream_encoder_->SetSource(&new_video_source,
2131 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002132 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002133 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002134 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002135
2136 stats = stats_proxy_->GetStats();
2137 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002138 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002139 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2140
2141 // Try to trigger overuse. Should not succeed.
2142 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002143 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002144 stats_proxy_->ResetMockStats();
2145
2146 stats = stats_proxy_->GetStats();
2147 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002148 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002149 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2150
2151 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002152 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002153 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002154 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002155 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002156 stats = stats_proxy_->GetStats();
2157 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002158 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002159 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002160
2161 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002162 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002163 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002164 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002165 stats = stats_proxy_->GetStats();
2166 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002167 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002168 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2169
2170 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002171 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002172 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002173 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002174 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002175 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002176 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002177 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002178 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002179 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002180 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2181
2182 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002183 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002184 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002185 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002186 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002187 stats = stats_proxy_->GetStats();
2188 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002189 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002190 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002191 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002192
mflodmancc3d4422017-08-03 08:27:51 -07002193 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002194}
2195
mflodmancc3d4422017-08-03 08:27:51 -07002196TEST_F(VideoStreamEncoderTest,
2197 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002198 const int kWidth = 1280;
2199 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002200 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002201 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2202 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002203
asaperssonfab67072017-04-04 05:51:49 -07002204 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002205 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002206
asaperssonfab67072017-04-04 05:51:49 -07002207 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002208 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002209
asaperssonfab67072017-04-04 05:51:49 -07002210 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002211 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002212
asaperssonfab67072017-04-04 05:51:49 -07002213 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002214 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002215
kthelgason876222f2016-11-29 01:44:11 -08002216 // Expect a scale down.
2217 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002218 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002219
asapersson02465b82017-04-10 01:12:52 -07002220 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002221 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002222 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002223 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002224
asaperssonfab67072017-04-04 05:51:49 -07002225 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002226 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002227 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002228 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002229
asaperssonfab67072017-04-04 05:51:49 -07002230 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002231 EXPECT_EQ(std::numeric_limits<int>::max(),
2232 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002233
asaperssonfab67072017-04-04 05:51:49 -07002234 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002235 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002236 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002237 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002238
asapersson02465b82017-04-10 01:12:52 -07002239 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002240 EXPECT_EQ(std::numeric_limits<int>::max(),
2241 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002242
mflodmancc3d4422017-08-03 08:27:51 -07002243 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002244}
2245
mflodmancc3d4422017-08-03 08:27:51 -07002246TEST_F(VideoStreamEncoderTest,
2247 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002248 const int kWidth = 1280;
2249 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002250 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002251 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2252 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002253
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002254 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002255 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002256 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002257 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002258
2259 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002260 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002261 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002262 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2263 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2264
2265 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002266 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002267 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002268 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2269 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2270 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2271
2272 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002273 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002274 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2275 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2276 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2277
mflodmancc3d4422017-08-03 08:27:51 -07002278 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002279}
2280
mflodmancc3d4422017-08-03 08:27:51 -07002281TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002282 const int kWidth = 1280;
2283 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002284 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002285 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2286 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002287
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002288 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002289 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002290 video_stream_encoder_->SetSource(&source,
2291 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002292 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2293 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002294 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002295
2296 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002297 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002298 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2299 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2300 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2301 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2302
2303 // Trigger adapt down for same input resolution, expect no change.
2304 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2305 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002306 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002307 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2308 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2309 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2310
2311 // Trigger adapt down for larger input resolution, expect no change.
2312 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2313 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002314 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002315 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2316 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2317 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2318
mflodmancc3d4422017-08-03 08:27:51 -07002319 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002320}
2321
mflodmancc3d4422017-08-03 08:27:51 -07002322TEST_F(VideoStreamEncoderTest,
2323 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002324 const int kWidth = 1280;
2325 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002326 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002327 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2328 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002329
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002330 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002331 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002332 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002333 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002334
2335 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002336 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002337 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002338 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2339 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2340
2341 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002342 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002343 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002344 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2345 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2346
mflodmancc3d4422017-08-03 08:27:51 -07002347 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002348}
2349
mflodmancc3d4422017-08-03 08:27:51 -07002350TEST_F(VideoStreamEncoderTest,
2351 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002352 const int kWidth = 1280;
2353 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002354 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002355 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2356 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002357
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002358 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002359 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002360 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002361 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002362
2363 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002364 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002365 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002366 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002367 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2368
2369 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002370 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002371 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002372 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002373 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2374
mflodmancc3d4422017-08-03 08:27:51 -07002375 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002376}
2377
mflodmancc3d4422017-08-03 08:27:51 -07002378TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002379 const int kWidth = 1280;
2380 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002381 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002382 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2383 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002384
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002385 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002386 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002387 video_stream_encoder_->SetSource(&source,
2388 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002389
2390 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2391 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002392 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002393 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2394 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2395 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2396
2397 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002398 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002399 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002400 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2401 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2402 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2403
mflodmancc3d4422017-08-03 08:27:51 -07002404 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002405}
2406
mflodmancc3d4422017-08-03 08:27:51 -07002407TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002408 const int kWidth = 1280;
2409 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002410 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002411 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2412 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002413
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002414 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002415 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002416 video_stream_encoder_->SetSource(&source,
2417 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002418
2419 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2420 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002421 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002422 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2423 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2424 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2425
2426 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002427 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002428 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002429 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2430 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2431 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2432
mflodmancc3d4422017-08-03 08:27:51 -07002433 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002434}
2435
mflodmancc3d4422017-08-03 08:27:51 -07002436TEST_F(VideoStreamEncoderTest,
2437 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002438 const int kWidth = 1280;
2439 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002440 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002441 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2442 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002443
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002444 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002445 AdaptingFrameForwarder source;
2446 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002447 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002448 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002449
2450 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002451 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002452 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002453 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2454 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2455
2456 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002457 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002458 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002459 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002460 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002461 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2462 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2463
2464 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002465 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002466 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002467 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2468 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2469 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2470
mflodmancc3d4422017-08-03 08:27:51 -07002471 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002472}
2473
mflodmancc3d4422017-08-03 08:27:51 -07002474TEST_F(VideoStreamEncoderTest,
2475 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002476 const int kWidth = 1280;
2477 const int kHeight = 720;
2478 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002479 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002480 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2481 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002482
2483 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2484 stats.input_frame_rate = kInputFps;
2485 stats_proxy_->SetMockStats(stats);
2486
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002487 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002488 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2489 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002490 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002491
2492 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002493 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002494 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2495 sink_.WaitForEncodedFrame(2);
2496 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2497
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002498 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002499 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002500 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002501 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002502 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002503
2504 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002505 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002506 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2507 sink_.WaitForEncodedFrame(3);
2508 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2509
2510 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002511 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002512 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002513
mflodmancc3d4422017-08-03 08:27:51 -07002514 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002515}
2516
mflodmancc3d4422017-08-03 08:27:51 -07002517TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002518 const int kWidth = 1280;
2519 const int kHeight = 720;
2520 const size_t kNumFrames = 10;
2521
Erik Språng4c6ca302019-04-08 15:14:01 +02002522 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002523 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2524 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002525
asaperssond0de2952017-04-21 01:47:31 -07002526 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002527 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002528 video_source_.set_adaptation_enabled(true);
2529
2530 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2531 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2532
2533 int downscales = 0;
2534 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002535 video_source_.IncomingCapturedFrame(
2536 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2537 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002538
asaperssonfab67072017-04-04 05:51:49 -07002539 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002540 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002541 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002542 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002543
2544 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2545 ++downscales;
2546
2547 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2548 EXPECT_EQ(downscales,
2549 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2550 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002551 }
mflodmancc3d4422017-08-03 08:27:51 -07002552 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002553}
2554
mflodmancc3d4422017-08-03 08:27:51 -07002555TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002556 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2557 const int kWidth = 1280;
2558 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002559 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002560 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2561 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002562
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002563 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002564 AdaptingFrameForwarder source;
2565 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002566 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002567 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002568
Åsa Persson8c1bf952018-09-13 10:42:19 +02002569 int64_t timestamp_ms = kFrameIntervalMs;
2570 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002571 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002572 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002573 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2574 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2575
2576 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002577 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002578 timestamp_ms += kFrameIntervalMs;
2579 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2580 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002581 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002582 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2583 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2584
2585 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002586 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002587 timestamp_ms += kFrameIntervalMs;
2588 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002589 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002590 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002591 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2592 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2593
2594 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002595 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002596 timestamp_ms += kFrameIntervalMs;
2597 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2598 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002599 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002600 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2601 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2602
2603 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002604 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002605 timestamp_ms += kFrameIntervalMs;
2606 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002607 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002608 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002609 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2610 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2611
mflodmancc3d4422017-08-03 08:27:51 -07002612 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002613}
2614
mflodmancc3d4422017-08-03 08:27:51 -07002615TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002616 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2617 const int kWidth = 1280;
2618 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002619 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002620 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2621 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002622
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002623 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002624 AdaptingFrameForwarder source;
2625 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002626 video_stream_encoder_->SetSource(&source,
2627 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002628
Åsa Persson8c1bf952018-09-13 10:42:19 +02002629 int64_t timestamp_ms = kFrameIntervalMs;
2630 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002631 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002632 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002633 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2634 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2635
2636 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002637 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002638 timestamp_ms += kFrameIntervalMs;
2639 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2640 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002641 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2642 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2643 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2644
2645 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002646 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002647 timestamp_ms += kFrameIntervalMs;
2648 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002649 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002650 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002651 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2652 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2653
2654 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002655 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002656 timestamp_ms += kFrameIntervalMs;
2657 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2658 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002659 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2660 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2661 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2662
2663 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002664 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002665 timestamp_ms += kFrameIntervalMs;
2666 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002667 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002668 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002669 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2670 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2671
mflodmancc3d4422017-08-03 08:27:51 -07002672 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002673}
2674
Sergey Silkin41c650b2019-10-14 13:12:19 +02002675TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2676 fake_encoder_.SetResolutionBitrateLimits(
2677 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2678
2679 video_stream_encoder_->OnBitrateUpdated(
2680 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2681 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2682 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2683
2684 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2685 AdaptingFrameForwarder source;
2686 source.set_adaptation_enabled(true);
2687 video_stream_encoder_->SetSource(
2688 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2689
2690 // Insert 720p frame.
2691 int64_t timestamp_ms = kFrameIntervalMs;
2692 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2693 WaitForEncodedFrame(1280, 720);
2694
2695 // Reduce bitrate and trigger adapt down.
2696 video_stream_encoder_->OnBitrateUpdated(
2697 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2698 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2699 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2700 video_stream_encoder_->TriggerQualityLow();
2701
2702 // Insert 720p frame. It should be downscaled and encoded.
2703 timestamp_ms += kFrameIntervalMs;
2704 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2705 WaitForEncodedFrame(960, 540);
2706
2707 // Trigger adapt up. Higher resolution should not be requested duo to lack
2708 // of bitrate.
2709 video_stream_encoder_->TriggerQualityHigh();
2710 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2711
2712 // Increase bitrate.
2713 video_stream_encoder_->OnBitrateUpdated(
2714 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2715 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2716 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0);
2717
2718 // Trigger adapt up. Higher resolution should be requested.
2719 video_stream_encoder_->TriggerQualityHigh();
2720 VerifyFpsMaxResolutionMax(source.sink_wants());
2721
2722 video_stream_encoder_->Stop();
2723}
2724
2725TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2726 fake_encoder_.SetResolutionBitrateLimits(
2727 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2728
2729 // Set bitrate equal to min bitrate of 540p.
2730 video_stream_encoder_->OnBitrateUpdated(
2731 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2732 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2733 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0);
2734
2735 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2736 AdaptingFrameForwarder source;
2737 source.set_adaptation_enabled(true);
2738 video_stream_encoder_->SetSource(
2739 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2740
2741 // Insert 720p frame. It should be dropped and lower resolution should be
2742 // requested.
2743 int64_t timestamp_ms = kFrameIntervalMs;
2744 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2745 ExpectDroppedFrame();
2746 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2747
2748 // Insert 720p frame. It should be downscaled and encoded.
2749 timestamp_ms += kFrameIntervalMs;
2750 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2751 WaitForEncodedFrame(960, 540);
2752
2753 video_stream_encoder_->Stop();
2754}
2755
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002756class BalancedDegradationTest : public VideoStreamEncoderTest {
2757 protected:
2758 void SetupTest() {
2759 // Reset encoder for field trials to take effect.
2760 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002761 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002762
2763 // Enable BALANCED preference.
2764 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002765 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2766 }
2767
2768 void OnBitrateUpdated(int bitrate_bps) {
2769 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2770 DataRate::bps(bitrate_bps),
2771 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002772 }
2773
Åsa Persson45b176f2019-09-30 11:19:05 +02002774 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002775 timestamp_ms_ += kFrameIntervalMs;
2776 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002777 }
2778
2779 void InsertFrameAndWaitForEncoded() {
2780 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002781 sink_.WaitForEncodedFrame(timestamp_ms_);
2782 }
2783
2784 const int kWidth = 640; // pixels:640x360=230400
2785 const int kHeight = 360;
2786 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2787 int64_t timestamp_ms_ = 0;
2788 AdaptingFrameForwarder source_;
2789};
2790
2791TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2792 test::ScopedFieldTrials field_trials(
2793 "WebRTC-Video-BalancedDegradationSettings/"
2794 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2795 SetupTest();
2796
2797 // Force input frame rate.
2798 const int kInputFps = 24;
2799 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2800 stats.input_frame_rate = kInputFps;
2801 stats_proxy_->SetMockStats(stats);
2802
Åsa Persson45b176f2019-09-30 11:19:05 +02002803 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002804 VerifyFpsMaxResolutionMax(source_.sink_wants());
2805
2806 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2807 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2808 video_stream_encoder_->TriggerQualityLowExpectFalse();
2809 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2810
2811 video_stream_encoder_->Stop();
2812}
2813
2814TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2815 test::ScopedFieldTrials field_trials(
2816 "WebRTC-Video-BalancedDegradationSettings/"
2817 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2818 SetupTest();
2819
2820 // Force input frame rate.
2821 const int kInputFps = 25;
2822 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2823 stats.input_frame_rate = kInputFps;
2824 stats_proxy_->SetMockStats(stats);
2825
Åsa Persson45b176f2019-09-30 11:19:05 +02002826 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002827 VerifyFpsMaxResolutionMax(source_.sink_wants());
2828
2829 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2830 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2831 video_stream_encoder_->TriggerQualityLow();
2832 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2833
2834 video_stream_encoder_->Stop();
2835}
2836
2837TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2838 test::ScopedFieldTrials field_trials(
2839 "WebRTC-Video-BalancedDegradationSettings/"
2840 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2841 SetupTest();
2842
2843 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2844
Åsa Persson45b176f2019-09-30 11:19:05 +02002845 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002846 VerifyFpsMaxResolutionMax(source_.sink_wants());
2847
2848 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2849 video_stream_encoder_->TriggerQualityLow();
2850 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2851
2852 video_stream_encoder_->Stop();
2853}
2854
Åsa Perssonccfb3402019-09-25 15:13:04 +02002855TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002856 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002857 "WebRTC-Video-BalancedDegradationSettings/"
2858 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002859 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002860
Åsa Persson1b247f12019-08-14 17:26:39 +02002861 const int kMinBitrateBps = 425000;
2862 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002863 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002864
Åsa Persson45b176f2019-09-30 11:19:05 +02002865 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002866 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002867 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2868
2869 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2870 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002871 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002872 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002873 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2874
2875 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2876 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002877 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002878 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002879 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2880
Åsa Persson30ab0152019-08-27 12:22:33 +02002881 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2882 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002883 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002884 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2885 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002886 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2887
2888 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002889 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002890 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002891 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002892
Åsa Persson30ab0152019-08-27 12:22:33 +02002893 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002894 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002895 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002896 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002897 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002898 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2899
2900 video_stream_encoder_->Stop();
2901}
2902
Åsa Perssonccfb3402019-09-25 15:13:04 +02002903TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002904 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2905 test::ScopedFieldTrials field_trials(
2906 "WebRTC-Video-BalancedDegradationSettings/"
2907 "pixels:57600|129600|230400,fps:7|24|24/");
2908 SetupTest();
2909 OnBitrateUpdated(kLowTargetBitrateBps);
2910
2911 VerifyNoLimitation(source_.sink_wants());
2912
2913 // Insert frame, expect scaled down:
2914 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2915 InsertFrame();
2916 EXPECT_FALSE(WaitForFrame(1000));
2917 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2918 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2919
2920 // Insert frame, expect scaled down:
2921 // resolution (320x180@24fps).
2922 InsertFrame();
2923 EXPECT_FALSE(WaitForFrame(1000));
2924 EXPECT_LT(source_.sink_wants().max_pixel_count,
2925 source_.last_wants().max_pixel_count);
2926 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2927
2928 // Frame should not be dropped (min pixels per frame reached).
2929 InsertFrameAndWaitForEncoded();
2930
2931 video_stream_encoder_->Stop();
2932}
2933
2934TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002935 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002936 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002937 "WebRTC-Video-BalancedDegradationSettings/"
2938 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002939 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002940
Åsa Persson30ab0152019-08-27 12:22:33 +02002941 const int kResolutionMinBitrateBps = 435000;
2942 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002943 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002944
Åsa Persson45b176f2019-09-30 11:19:05 +02002945 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002946 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002947 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2948
2949 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2950 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002951 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002952 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002953 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2954
2955 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2956 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002957 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002958 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002959 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2960
2961 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2962 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002963 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002964 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002965 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2966
Åsa Persson30ab0152019-08-27 12:22:33 +02002967 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2968 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002969 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002970 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002971 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2972
2973 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2974 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002975 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002976 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2977
2978 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002979 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002980 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002981 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002982 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002983 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2984
2985 video_stream_encoder_->Stop();
2986}
2987
Åsa Perssonccfb3402019-09-25 15:13:04 +02002988TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002989 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002990 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002991 "WebRTC-Video-BalancedDegradationSettings/"
2992 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002993 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002994
Åsa Persson30ab0152019-08-27 12:22:33 +02002995 const int kMinBitrateBps = 425000;
2996 const int kTooLowMinBitrateBps = 424000;
2997 const int kResolutionMinBitrateBps = 435000;
2998 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002999 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003000
Åsa Persson45b176f2019-09-30 11:19:05 +02003001 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003002 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003003 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3004
3005 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3006 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003007 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003008 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003009 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3010
3011 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3012 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003013 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003014 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003015 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3016
3017 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3018 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003019 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003020 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003021 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3022
3023 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3024 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003025 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003026 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3027
3028 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003029 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003030 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003031 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003032 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003033 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3034
3035 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003036 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003037 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003038 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003039 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3040
3041 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003042 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003043 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003044 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003045 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003046 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3047
Åsa Persson1b247f12019-08-14 17:26:39 +02003048 video_stream_encoder_->Stop();
3049}
3050
mflodmancc3d4422017-08-03 08:27:51 -07003051TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003052 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3053 const int kWidth = 1280;
3054 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003055 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003056 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3057 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003058
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003059 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003060 AdaptingFrameForwarder source;
3061 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003062 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003063 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003064
Åsa Persson8c1bf952018-09-13 10:42:19 +02003065 int64_t timestamp_ms = kFrameIntervalMs;
3066 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003067 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003068 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003069 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3070 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3071 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3072 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3073
3074 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003075 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003076 timestamp_ms += kFrameIntervalMs;
3077 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3078 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003079 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003080 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3081 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3082 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3083 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3084
3085 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003086 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003087 timestamp_ms += kFrameIntervalMs;
3088 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3089 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003090 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003091 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3092 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3093 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3094 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3095
Jonathan Yubc771b72017-12-08 17:04:29 -08003096 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003097 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003098 timestamp_ms += kFrameIntervalMs;
3099 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3100 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003101 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003102 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3103 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003104 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003105 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3106
Jonathan Yubc771b72017-12-08 17:04:29 -08003107 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003108 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003109 timestamp_ms += kFrameIntervalMs;
3110 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3111 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003112 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003113 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003114 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3115 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3116 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3117 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3118
Jonathan Yubc771b72017-12-08 17:04:29 -08003119 // Trigger quality adapt down, expect no change (min resolution reached).
3120 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003121 timestamp_ms += kFrameIntervalMs;
3122 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3123 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003124 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3125 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3126 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3127 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3128 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3129
3130 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003131 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003132 timestamp_ms += kFrameIntervalMs;
3133 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3134 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003135 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003136 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3137 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3138 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3139 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3140
3141 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3142 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003143 timestamp_ms += kFrameIntervalMs;
3144 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3145 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003146 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3147 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3148 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3149 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3150 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3151
3152 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3153 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003154 timestamp_ms += kFrameIntervalMs;
3155 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3156 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003157 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003158 last_wants = source.sink_wants();
3159 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3160 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003161 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003162 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3163
3164 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003165 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003166 timestamp_ms += kFrameIntervalMs;
3167 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3168 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003169 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003170 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3171 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003172 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003173 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3174
3175 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003176 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003177 timestamp_ms += kFrameIntervalMs;
3178 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003179 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003180 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003181 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003182 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3183 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003184 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003185 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003186
mflodmancc3d4422017-08-03 08:27:51 -07003187 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003188}
3189
mflodmancc3d4422017-08-03 08:27:51 -07003190TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003191 const int kWidth = 640;
3192 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003193
Erik Språng4c6ca302019-04-08 15:14:01 +02003194 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003195 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3196 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003197
perkj803d97f2016-11-01 11:45:46 -07003198 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003199 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003200 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003201 }
3202
mflodmancc3d4422017-08-03 08:27:51 -07003203 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003204 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003205 video_source_.IncomingCapturedFrame(CreateFrame(
3206 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003207 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003208 }
3209
mflodmancc3d4422017-08-03 08:27:51 -07003210 video_stream_encoder_->Stop();
3211 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003212 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003213
perkj803d97f2016-11-01 11:45:46 -07003214 EXPECT_EQ(1,
3215 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3216 EXPECT_EQ(
3217 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3218}
3219
mflodmancc3d4422017-08-03 08:27:51 -07003220TEST_F(VideoStreamEncoderTest,
3221 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003222 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003223 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3224 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003225 const int kWidth = 640;
3226 const int kHeight = 360;
3227
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003228 video_stream_encoder_->SetSource(&video_source_,
3229 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003230
3231 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3232 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003233 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003234 }
3235
mflodmancc3d4422017-08-03 08:27:51 -07003236 video_stream_encoder_->Stop();
3237 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003238 stats_proxy_.reset();
3239
3240 EXPECT_EQ(0,
3241 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3242}
3243
mflodmancc3d4422017-08-03 08:27:51 -07003244TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003245 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003246 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003247
3248 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003249 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003250 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003251 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3252 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003253
sprang57c2fff2017-01-16 06:24:02 -08003254 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003255 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003256 video_stream_encoder_->OnBitrateUpdated(
3257 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3258 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003259
sprang57c2fff2017-01-16 06:24:02 -08003260 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003261 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3262 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003263 VideoBitrateAllocation bitrate_allocation =
3264 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003265 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003266 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003267 // TODO(srte): The use of millisecs here looks like an error, but the tests
3268 // fails using seconds, this should be investigated.
3269 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003270
3271 // Not called on second frame.
3272 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3273 .Times(0);
3274 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003275 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3276 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003277 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003278
3279 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003280 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3281 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003282 const int64_t start_time_ms = rtc::TimeMillis();
3283 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3284 video_source_.IncomingCapturedFrame(
3285 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3286 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003287 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003288 }
3289
3290 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003291 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003292
mflodmancc3d4422017-08-03 08:27:51 -07003293 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003294}
3295
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003296TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3297 // 2 TLs configured, temporal layers supported by encoder.
3298 const int kNumTemporalLayers = 2;
3299 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3300 fake_encoder_.SetTemporalLayersSupported(0, true);
3301
3302 // Bitrate allocated across temporal layers.
3303 const int kTl0Bps = kTargetBitrateBps *
3304 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003305 kNumTemporalLayers, /*temporal_id*/ 0,
3306 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003307 const int kTl1Bps = kTargetBitrateBps *
3308 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003309 kNumTemporalLayers, /*temporal_id*/ 1,
3310 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003311 VideoBitrateAllocation expected_bitrate;
3312 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3313 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3314
3315 VerifyAllocatedBitrate(expected_bitrate);
3316 video_stream_encoder_->Stop();
3317}
3318
3319TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3320 // 2 TLs configured, temporal layers not supported by encoder.
3321 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3322 fake_encoder_.SetTemporalLayersSupported(0, false);
3323
3324 // Temporal layers not supported by the encoder.
3325 // Total bitrate should be at ti:0.
3326 VideoBitrateAllocation expected_bitrate;
3327 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3328
3329 VerifyAllocatedBitrate(expected_bitrate);
3330 video_stream_encoder_->Stop();
3331}
3332
3333TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3334 // 2 TLs configured, temporal layers only supported for first stream.
3335 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3336 fake_encoder_.SetTemporalLayersSupported(0, true);
3337 fake_encoder_.SetTemporalLayersSupported(1, false);
3338
3339 const int kS0Bps = 150000;
3340 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003341 kS0Bps *
3342 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3343 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003344 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003345 kS0Bps *
3346 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3347 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003348 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3349 // Temporal layers not supported by si:1.
3350 VideoBitrateAllocation expected_bitrate;
3351 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3352 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3353 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3354
3355 VerifyAllocatedBitrate(expected_bitrate);
3356 video_stream_encoder_->Stop();
3357}
3358
Niels Möller7dc26b72017-12-06 10:27:48 +01003359TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3360 const int kFrameWidth = 1280;
3361 const int kFrameHeight = 720;
3362 const int kFramerate = 24;
3363
Erik Språng4c6ca302019-04-08 15:14:01 +02003364 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003365 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3366 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003367 test::FrameForwarder source;
3368 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003369 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003370
3371 // Insert a single frame, triggering initial configuration.
3372 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3373 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3374
3375 EXPECT_EQ(
3376 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3377 kDefaultFramerate);
3378
3379 // Trigger reconfigure encoder (without resetting the entire instance).
3380 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003381 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003382 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3383 video_encoder_config.number_of_streams = 1;
3384 video_encoder_config.video_stream_factory =
3385 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3386 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003387 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003388 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3389
3390 // Detector should be updated with fps limit from codec config.
3391 EXPECT_EQ(
3392 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3393 kFramerate);
3394
3395 // Trigger overuse, max framerate should be reduced.
3396 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3397 stats.input_frame_rate = kFramerate;
3398 stats_proxy_->SetMockStats(stats);
3399 video_stream_encoder_->TriggerCpuOveruse();
3400 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3401 int adapted_framerate =
3402 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3403 EXPECT_LT(adapted_framerate, kFramerate);
3404
3405 // Trigger underuse, max framerate should go back to codec configured fps.
3406 // Set extra low fps, to make sure it's actually reset, not just incremented.
3407 stats = stats_proxy_->GetStats();
3408 stats.input_frame_rate = adapted_framerate / 2;
3409 stats_proxy_->SetMockStats(stats);
3410 video_stream_encoder_->TriggerCpuNormalUsage();
3411 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3412 EXPECT_EQ(
3413 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3414 kFramerate);
3415
3416 video_stream_encoder_->Stop();
3417}
3418
3419TEST_F(VideoStreamEncoderTest,
3420 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3421 const int kFrameWidth = 1280;
3422 const int kFrameHeight = 720;
3423 const int kLowFramerate = 15;
3424 const int kHighFramerate = 25;
3425
Erik Språng4c6ca302019-04-08 15:14:01 +02003426 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003427 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3428 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003429 test::FrameForwarder source;
3430 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003431 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003432
3433 // Trigger initial configuration.
3434 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003435 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003436 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3437 video_encoder_config.number_of_streams = 1;
3438 video_encoder_config.video_stream_factory =
3439 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3440 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3441 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003442 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003443 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3444
3445 EXPECT_EQ(
3446 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3447 kLowFramerate);
3448
3449 // Trigger overuse, max framerate should be reduced.
3450 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3451 stats.input_frame_rate = kLowFramerate;
3452 stats_proxy_->SetMockStats(stats);
3453 video_stream_encoder_->TriggerCpuOveruse();
3454 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3455 int adapted_framerate =
3456 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3457 EXPECT_LT(adapted_framerate, kLowFramerate);
3458
3459 // Reconfigure the encoder with a new (higher max framerate), max fps should
3460 // still respect the adaptation.
3461 video_encoder_config.video_stream_factory =
3462 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3463 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3464 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003465 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003466 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3467
3468 EXPECT_EQ(
3469 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3470 adapted_framerate);
3471
3472 // Trigger underuse, max framerate should go back to codec configured fps.
3473 stats = stats_proxy_->GetStats();
3474 stats.input_frame_rate = adapted_framerate;
3475 stats_proxy_->SetMockStats(stats);
3476 video_stream_encoder_->TriggerCpuNormalUsage();
3477 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3478 EXPECT_EQ(
3479 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3480 kHighFramerate);
3481
3482 video_stream_encoder_->Stop();
3483}
3484
mflodmancc3d4422017-08-03 08:27:51 -07003485TEST_F(VideoStreamEncoderTest,
3486 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003487 const int kFrameWidth = 1280;
3488 const int kFrameHeight = 720;
3489 const int kFramerate = 24;
3490
Erik Språng4c6ca302019-04-08 15:14:01 +02003491 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003492 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3493 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003494 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003495 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003496 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003497
3498 // Trigger initial configuration.
3499 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003500 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003501 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3502 video_encoder_config.number_of_streams = 1;
3503 video_encoder_config.video_stream_factory =
3504 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3505 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003506 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003507 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003508 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003509
Niels Möller7dc26b72017-12-06 10:27:48 +01003510 EXPECT_EQ(
3511 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3512 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003513
3514 // Trigger overuse, max framerate should be reduced.
3515 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3516 stats.input_frame_rate = kFramerate;
3517 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003518 video_stream_encoder_->TriggerCpuOveruse();
3519 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003520 int adapted_framerate =
3521 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003522 EXPECT_LT(adapted_framerate, kFramerate);
3523
3524 // Change degradation preference to not enable framerate scaling. Target
3525 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003526 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003527 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003528 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003529 EXPECT_EQ(
3530 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3531 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003532
mflodmancc3d4422017-08-03 08:27:51 -07003533 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003534}
3535
mflodmancc3d4422017-08-03 08:27:51 -07003536TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003537 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003538 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003539 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003540 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003541 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003542 const int kWidth = 640;
3543 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003544
asaperssonfab67072017-04-04 05:51:49 -07003545 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003546
3547 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003548 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003549
3550 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003551 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003552
sprangc5d62e22017-04-02 23:53:04 -07003553 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003554
asaperssonfab67072017-04-04 05:51:49 -07003555 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003556 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003557 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003558
3559 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003560 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003561
sprangc5d62e22017-04-02 23:53:04 -07003562 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003563
mflodmancc3d4422017-08-03 08:27:51 -07003564 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003565}
3566
mflodmancc3d4422017-08-03 08:27:51 -07003567TEST_F(VideoStreamEncoderTest,
3568 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003569 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003570 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003571 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003572 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003573 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003574 const int kWidth = 640;
3575 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003576
3577 // We expect the n initial frames to get dropped.
3578 int i;
3579 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003580 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003581 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003582 }
3583 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003584 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003585 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003586
3587 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003588 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003589
mflodmancc3d4422017-08-03 08:27:51 -07003590 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003591}
3592
mflodmancc3d4422017-08-03 08:27:51 -07003593TEST_F(VideoStreamEncoderTest,
3594 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003595 const int kWidth = 640;
3596 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003597 video_stream_encoder_->OnBitrateUpdated(
3598 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3599 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003600
3601 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003602 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003603 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003604
asaperssonfab67072017-04-04 05:51:49 -07003605 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003606 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003607 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003608
mflodmancc3d4422017-08-03 08:27:51 -07003609 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003610}
3611
mflodmancc3d4422017-08-03 08:27:51 -07003612TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003613 const int kWidth = 640;
3614 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003615 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003616
3617 VideoEncoderConfig video_encoder_config;
3618 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3619 // Make format different, to force recreation of encoder.
3620 video_encoder_config.video_format.parameters["foo"] = "foo";
3621 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003622 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003623 video_stream_encoder_->OnBitrateUpdated(
3624 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3625 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003626
kthelgasonb83797b2017-02-14 11:57:25 -08003627 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003628 video_stream_encoder_->SetSource(&video_source_,
3629 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003630
asaperssonfab67072017-04-04 05:51:49 -07003631 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003632 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003633 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003634
mflodmancc3d4422017-08-03 08:27:51 -07003635 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003636 fake_encoder_.SetQualityScaling(true);
3637}
3638
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003639TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3640 webrtc::test::ScopedFieldTrials field_trials(
3641 "WebRTC-InitialFramedrop/Enabled/");
3642 // Reset encoder for field trials to take effect.
3643 ConfigureEncoder(video_encoder_config_.Copy());
3644 const int kTooLowBitrateForFrameSizeBps = 10000;
3645 const int kWidth = 640;
3646 const int kHeight = 360;
3647
Erik Språng4c6ca302019-04-08 15:14:01 +02003648 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003649 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3650 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003651 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3652 // Frame should not be dropped.
3653 WaitForEncodedFrame(1);
3654
Erik Språng610c7632019-03-06 15:37:33 +01003655 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003656 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003657 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003658 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003659 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3660 // Expect to drop this frame, the wait should time out.
3661 ExpectDroppedFrame();
3662
3663 // Expect the sink_wants to specify a scaled frame.
3664 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3665 video_stream_encoder_->Stop();
3666}
3667
Åsa Persson139f4dc2019-08-02 09:29:58 +02003668TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3669 webrtc::test::ScopedFieldTrials field_trials(
3670 "WebRTC-Video-QualityScalerSettings/"
3671 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3672 // Reset encoder for field trials to take effect.
3673 ConfigureEncoder(video_encoder_config_.Copy());
3674 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3675 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3676 const int kWidth = 640;
3677 const int kHeight = 360;
3678
3679 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003680 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3681 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003682 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3683 // Frame should not be dropped.
3684 WaitForEncodedFrame(1);
3685
3686 video_stream_encoder_->OnBitrateUpdated(
3687 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003688 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003689 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3690 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3691 // Frame should not be dropped.
3692 WaitForEncodedFrame(2);
3693
3694 video_stream_encoder_->OnBitrateUpdated(
3695 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003696 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003697 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3698 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3699 // Expect to drop this frame, the wait should time out.
3700 ExpectDroppedFrame();
3701
3702 // Expect the sink_wants to specify a scaled frame.
3703 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3704 video_stream_encoder_->Stop();
3705}
3706
Åsa Perssone644a032019-11-08 15:56:00 +01003707TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3708 webrtc::test::ScopedFieldTrials field_trials(
3709 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3710
3711 // Reset encoder for field trials to take effect.
3712 VideoEncoderConfig config = video_encoder_config_.Copy();
3713 config.max_bitrate_bps = kTargetBitrateBps;
3714 ConfigureEncoder(std::move(config));
3715 fake_encoder_.SetQp(kQpLow);
3716
3717 // Enable MAINTAIN_FRAMERATE preference.
3718 AdaptingFrameForwarder source;
3719 source.set_adaptation_enabled(true);
3720 video_stream_encoder_->SetSource(&source,
3721 DegradationPreference::MAINTAIN_FRAMERATE);
3722
3723 // Start at low bitrate.
3724 const int kLowBitrateBps = 200000;
3725 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowBitrateBps),
3726 DataRate::bps(kLowBitrateBps),
3727 DataRate::bps(kLowBitrateBps), 0, 0);
3728
3729 // Expect first frame to be dropped and resolution to be limited.
3730 const int kWidth = 1280;
3731 const int kHeight = 720;
3732 const int64_t kFrameIntervalMs = 100;
3733 int64_t timestamp_ms = kFrameIntervalMs;
3734 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3735 ExpectDroppedFrame();
3736 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3737
3738 // Increase bitrate to encoder max.
3739 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3740 DataRate::bps(config.max_bitrate_bps),
3741 DataRate::bps(config.max_bitrate_bps),
3742 0, 0);
3743
3744 // Insert frames and advance |min_duration_ms|.
3745 for (size_t i = 1; i <= 10; i++) {
3746 timestamp_ms += kFrameIntervalMs;
3747 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3748 WaitForEncodedFrame(timestamp_ms);
3749 }
3750 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3751 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3752
3753 fake_clock_.AdvanceTime(TimeDelta::ms(2000));
3754
3755 // Insert frame should trigger high BW and release quality limitation.
3756 timestamp_ms += kFrameIntervalMs;
3757 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3758 WaitForEncodedFrame(timestamp_ms);
3759 VerifyFpsMaxResolutionMax(source.sink_wants());
3760
3761 // Frame should not be adapted.
3762 timestamp_ms += kFrameIntervalMs;
3763 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3764 WaitForEncodedFrame(kWidth, kHeight);
3765 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3766
3767 video_stream_encoder_->Stop();
3768}
3769
mflodmancc3d4422017-08-03 08:27:51 -07003770TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003771 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3772 const int kTooSmallWidth = 10;
3773 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003774 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003775 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3776 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003777
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003778 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003779 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003780 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003781 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003782 VerifyNoLimitation(source.sink_wants());
3783 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3784
3785 // Trigger adapt down, too small frame, expect no change.
3786 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003787 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003788 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003789 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003790 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3791 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3792
mflodmancc3d4422017-08-03 08:27:51 -07003793 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003794}
3795
mflodmancc3d4422017-08-03 08:27:51 -07003796TEST_F(VideoStreamEncoderTest,
3797 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003798 const int kTooSmallWidth = 10;
3799 const int kTooSmallHeight = 10;
3800 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003801 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003802 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3803 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003804
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003805 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003806 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003807 video_stream_encoder_->SetSource(&source,
3808 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003809 VerifyNoLimitation(source.sink_wants());
3810 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3811 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3812
3813 // Trigger adapt down, expect limited framerate.
3814 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003815 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003816 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003817 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3818 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3819 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3820 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3821
3822 // Trigger adapt down, too small frame, expect no change.
3823 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003824 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003825 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003826 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3827 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3828 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3829 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3830
mflodmancc3d4422017-08-03 08:27:51 -07003831 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003832}
3833
mflodmancc3d4422017-08-03 08:27:51 -07003834TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003835 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003836 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003837 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3838 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003839 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003840 const int kFrameWidth = 1280;
3841 const int kFrameHeight = 720;
3842 video_source_.IncomingCapturedFrame(
3843 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003844 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003845 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003846}
3847
sprangb1ca0732017-02-01 08:38:12 -08003848// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003849TEST_F(VideoStreamEncoderTest,
3850 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003851 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003852 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3853 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003854
3855 const int kFrameWidth = 1280;
3856 const int kFrameHeight = 720;
3857 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003858 // requested by
3859 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003860 video_source_.set_adaptation_enabled(true);
3861
3862 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003863 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003864 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003865
3866 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003867 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003868 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003869 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003870 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003871
asaperssonfab67072017-04-04 05:51:49 -07003872 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003873 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003874 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003875 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003876 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003877
mflodmancc3d4422017-08-03 08:27:51 -07003878 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003879}
sprangfe627f32017-03-29 08:24:59 -07003880
mflodmancc3d4422017-08-03 08:27:51 -07003881TEST_F(VideoStreamEncoderTest,
3882 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003883 const int kFrameWidth = 1280;
3884 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003885
Erik Språng4c6ca302019-04-08 15:14:01 +02003886 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003887 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3888 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003889 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003890 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003891 video_source_.set_adaptation_enabled(true);
3892
sprang4847ae62017-06-27 07:06:52 -07003893 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003894
3895 video_source_.IncomingCapturedFrame(
3896 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003897 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003898
3899 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003900 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003901
3902 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003903 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003904 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003905 video_source_.IncomingCapturedFrame(
3906 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003907 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003908 }
3909
3910 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003911 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003912 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003913 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003914 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003915 video_source_.IncomingCapturedFrame(
3916 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003917 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003918 ++num_frames_dropped;
3919 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003920 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003921 }
3922 }
3923
sprang4847ae62017-06-27 07:06:52 -07003924 // Add some slack to account for frames dropped by the frame dropper.
3925 const int kErrorMargin = 1;
3926 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003927 kErrorMargin);
3928
3929 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003930 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003931 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003932 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003933 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003934 video_source_.IncomingCapturedFrame(
3935 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003936 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003937 ++num_frames_dropped;
3938 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003939 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003940 }
3941 }
sprang4847ae62017-06-27 07:06:52 -07003942 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003943 kErrorMargin);
3944
3945 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003946 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003947 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003948 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003949 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003950 video_source_.IncomingCapturedFrame(
3951 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003952 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003953 ++num_frames_dropped;
3954 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003955 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003956 }
3957 }
sprang4847ae62017-06-27 07:06:52 -07003958 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003959 kErrorMargin);
3960
3961 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003962 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003963 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003964 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003965 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003966 video_source_.IncomingCapturedFrame(
3967 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003968 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003969 ++num_frames_dropped;
3970 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003971 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003972 }
3973 }
3974 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3975
mflodmancc3d4422017-08-03 08:27:51 -07003976 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003977}
3978
mflodmancc3d4422017-08-03 08:27:51 -07003979TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003980 const int kFramerateFps = 5;
3981 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003982 const int kFrameWidth = 1280;
3983 const int kFrameHeight = 720;
3984
sprang4847ae62017-06-27 07:06:52 -07003985 // Reconfigure encoder with two temporal layers and screensharing, which will
3986 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003987 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003988
Erik Språng4c6ca302019-04-08 15:14:01 +02003989 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003990 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3991 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003992 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003993 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003994 video_source_.set_adaptation_enabled(true);
3995
sprang4847ae62017-06-27 07:06:52 -07003996 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003997
3998 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003999 rtc::VideoSinkWants last_wants;
4000 do {
4001 last_wants = video_source_.sink_wants();
4002
sprangc5d62e22017-04-02 23:53:04 -07004003 // Insert frames to get a new fps estimate...
4004 for (int j = 0; j < kFramerateFps; ++j) {
4005 video_source_.IncomingCapturedFrame(
4006 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004007 if (video_source_.last_sent_width()) {
4008 sink_.WaitForEncodedFrame(timestamp_ms);
4009 }
sprangc5d62e22017-04-02 23:53:04 -07004010 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02004011 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004012 }
4013 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004014 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004015 } while (video_source_.sink_wants().max_framerate_fps <
4016 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004017
Jonathan Yubc771b72017-12-08 17:04:29 -08004018 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004019
mflodmancc3d4422017-08-03 08:27:51 -07004020 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004021}
asaperssonf7e294d2017-06-13 23:25:22 -07004022
mflodmancc3d4422017-08-03 08:27:51 -07004023TEST_F(VideoStreamEncoderTest,
4024 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004025 const int kWidth = 1280;
4026 const int kHeight = 720;
4027 const int64_t kFrameIntervalMs = 150;
4028 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004029 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004030 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4031 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004032
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004033 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004034 AdaptingFrameForwarder source;
4035 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004036 video_stream_encoder_->SetSource(&source,
4037 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004038 timestamp_ms += kFrameIntervalMs;
4039 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004040 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004041 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004042 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4043 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4044 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4045
4046 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004047 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004048 timestamp_ms += kFrameIntervalMs;
4049 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004050 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004051 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4052 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4053 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4054 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4055
4056 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004057 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004058 timestamp_ms += kFrameIntervalMs;
4059 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004060 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004061 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4062 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4063 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4064 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4065
4066 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004067 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004068 timestamp_ms += kFrameIntervalMs;
4069 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004070 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004071 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4072 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4073 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4074 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4075
4076 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004077 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004078 timestamp_ms += kFrameIntervalMs;
4079 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004080 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004081 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4082 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4083 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4084 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4085
4086 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004087 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004088 timestamp_ms += kFrameIntervalMs;
4089 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004090 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004091 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4092 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4093 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4094 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4095
4096 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004097 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004098 timestamp_ms += kFrameIntervalMs;
4099 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004100 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004101 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4102 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4103 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4104 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4105
4106 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004107 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004108 timestamp_ms += kFrameIntervalMs;
4109 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004110 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004111 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4112 rtc::VideoSinkWants last_wants = source.sink_wants();
4113 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4114 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4115 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4116
4117 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004118 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004119 timestamp_ms += kFrameIntervalMs;
4120 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004121 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004122 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4123 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4124 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4125 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4126
4127 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004128 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004129 timestamp_ms += kFrameIntervalMs;
4130 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004131 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004132 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4133 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4134 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4135 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4136
4137 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004138 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004139 timestamp_ms += kFrameIntervalMs;
4140 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004141 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004142 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4143 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4144 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4145 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4146
4147 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004148 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004149 timestamp_ms += kFrameIntervalMs;
4150 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004151 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004152 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4153 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4154 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4155 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4156
4157 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004158 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004159 timestamp_ms += kFrameIntervalMs;
4160 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004161 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004162 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4163 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4164 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4165 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4166
4167 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004168 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004169 timestamp_ms += kFrameIntervalMs;
4170 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004171 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004172 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4173 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4174 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4175 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4176
4177 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004178 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004179 timestamp_ms += kFrameIntervalMs;
4180 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004181 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004182 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4183 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4184 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4185 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4186
Åsa Persson30ab0152019-08-27 12:22:33 +02004187 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004188 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004189 timestamp_ms += kFrameIntervalMs;
4190 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004191 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004192 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004193 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004194 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4195 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4196 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4197
4198 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004199 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004200 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004201 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4202
mflodmancc3d4422017-08-03 08:27:51 -07004203 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004204}
4205
mflodmancc3d4422017-08-03 08:27:51 -07004206TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004207 const int kWidth = 1280;
4208 const int kHeight = 720;
4209 const int64_t kFrameIntervalMs = 150;
4210 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004211 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004212 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4213 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004214
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004215 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004216 AdaptingFrameForwarder source;
4217 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004218 video_stream_encoder_->SetSource(&source,
4219 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004220 timestamp_ms += kFrameIntervalMs;
4221 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004222 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004223 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004224 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4225 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4226 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4227 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4228 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4229 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4230
4231 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004232 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004233 timestamp_ms += kFrameIntervalMs;
4234 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004235 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004236 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4237 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4238 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4239 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4240 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4241 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4242 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4243
4244 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004245 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004246 timestamp_ms += kFrameIntervalMs;
4247 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004248 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004249 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4250 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4251 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4252 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4253 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4254 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4255 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4256
4257 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004258 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004259 timestamp_ms += kFrameIntervalMs;
4260 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004261 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004262 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4263 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4264 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4265 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4266 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4267 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4268 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4269
4270 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004271 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004272 timestamp_ms += kFrameIntervalMs;
4273 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004274 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004275 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4276 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4277 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4278 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4279 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4280 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4281 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4282
4283 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004284 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004285 timestamp_ms += kFrameIntervalMs;
4286 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004287 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004288 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4289 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4290 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4291 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4292 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4293 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4294 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4295
4296 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004297 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004298 timestamp_ms += kFrameIntervalMs;
4299 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004300 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004301 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004302 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004303 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4304 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4305 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4306 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4307 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4308 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4309
4310 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004311 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004312 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004313 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4314 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4315
mflodmancc3d4422017-08-03 08:27:51 -07004316 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004317}
4318
mflodmancc3d4422017-08-03 08:27:51 -07004319TEST_F(VideoStreamEncoderTest,
4320 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004321 const int kWidth = 640;
4322 const int kHeight = 360;
4323 const int kFpsLimit = 15;
4324 const int64_t kFrameIntervalMs = 150;
4325 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004326 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004327 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4328 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004329
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004330 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004331 AdaptingFrameForwarder source;
4332 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004333 video_stream_encoder_->SetSource(&source,
4334 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004335 timestamp_ms += kFrameIntervalMs;
4336 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004337 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004338 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004339 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4340 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4341 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4342 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4343 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4344 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4345
4346 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004347 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004348 timestamp_ms += kFrameIntervalMs;
4349 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004350 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004351 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4352 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4353 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4354 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4355 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4356 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4357 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4358
4359 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004360 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004361 timestamp_ms += kFrameIntervalMs;
4362 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004363 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004364 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4365 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4366 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4367 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4368 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4369 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4370 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4371
4372 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004373 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004374 timestamp_ms += kFrameIntervalMs;
4375 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004376 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004377 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4378 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4379 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4380 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4381 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4382 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4383 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4384
4385 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004386 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004387 timestamp_ms += kFrameIntervalMs;
4388 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004389 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004390 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004391 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4392 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4393 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4394 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4395 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4396 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4397
4398 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004399 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004400 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004401 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4402 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4403
mflodmancc3d4422017-08-03 08:27:51 -07004404 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004405}
4406
mflodmancc3d4422017-08-03 08:27:51 -07004407TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004408 const int kFrameWidth = 1920;
4409 const int kFrameHeight = 1080;
4410 // 3/4 of 1920.
4411 const int kAdaptedFrameWidth = 1440;
4412 // 3/4 of 1080 rounded down to multiple of 4.
4413 const int kAdaptedFrameHeight = 808;
4414 const int kFramerate = 24;
4415
Erik Språng4c6ca302019-04-08 15:14:01 +02004416 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004417 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4418 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004419 // Trigger reconfigure encoder (without resetting the entire instance).
4420 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004421 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004422 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4423 video_encoder_config.number_of_streams = 1;
4424 video_encoder_config.video_stream_factory =
4425 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004426 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004427 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004428 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004429
4430 video_source_.set_adaptation_enabled(true);
4431
4432 video_source_.IncomingCapturedFrame(
4433 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004434 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004435
4436 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004437 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004438 video_source_.IncomingCapturedFrame(
4439 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004440 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004441
mflodmancc3d4422017-08-03 08:27:51 -07004442 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004443}
4444
mflodmancc3d4422017-08-03 08:27:51 -07004445TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004446 const int kFrameWidth = 1280;
4447 const int kFrameHeight = 720;
4448 const int kLowFps = 2;
4449 const int kHighFps = 30;
4450
Erik Språng4c6ca302019-04-08 15:14:01 +02004451 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004452 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4453 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004454
4455 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4456 max_framerate_ = kLowFps;
4457
4458 // Insert 2 seconds of 2fps video.
4459 for (int i = 0; i < kLowFps * 2; ++i) {
4460 video_source_.IncomingCapturedFrame(
4461 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4462 WaitForEncodedFrame(timestamp_ms);
4463 timestamp_ms += 1000 / kLowFps;
4464 }
4465
4466 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004467 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004468 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4469 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004470 video_source_.IncomingCapturedFrame(
4471 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4472 WaitForEncodedFrame(timestamp_ms);
4473 timestamp_ms += 1000 / kLowFps;
4474
4475 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4476
4477 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004478 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004479 const int kFrameIntervalMs = 1000 / kHighFps;
4480 max_framerate_ = kHighFps;
4481 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4482 video_source_.IncomingCapturedFrame(
4483 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4484 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4485 // be dropped if the encoder hans't been updated with the new higher target
4486 // framerate yet, causing it to overshoot the target bitrate and then
4487 // suffering the wrath of the media optimizer.
4488 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4489 timestamp_ms += kFrameIntervalMs;
4490 }
4491
4492 // Don expect correct measurement just yet, but it should be higher than
4493 // before.
4494 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4495
mflodmancc3d4422017-08-03 08:27:51 -07004496 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004497}
4498
mflodmancc3d4422017-08-03 08:27:51 -07004499TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004500 const int kFrameWidth = 1280;
4501 const int kFrameHeight = 720;
4502 const int kTargetBitrateBps = 1000000;
4503
4504 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004505 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004506 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004507 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4508 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004509 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004510
4511 // Insert a first video frame, causes another bitrate update.
4512 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4513 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4514 video_source_.IncomingCapturedFrame(
4515 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4516 WaitForEncodedFrame(timestamp_ms);
4517
4518 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004519 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4520 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004521
4522 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004523 timestamp_ms += kProcessIntervalMs;
4524 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004525
4526 // Bitrate observer should not be called.
4527 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4528 video_source_.IncomingCapturedFrame(
4529 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4530 ExpectDroppedFrame();
4531
mflodmancc3d4422017-08-03 08:27:51 -07004532 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004533}
ilnik6b826ef2017-06-16 06:53:48 -07004534
Niels Möller4db138e2018-04-19 09:04:13 +02004535TEST_F(VideoStreamEncoderTest,
4536 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4537 const int kFrameWidth = 1280;
4538 const int kFrameHeight = 720;
4539 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004540 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004541 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4542 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004543 video_source_.IncomingCapturedFrame(
4544 CreateFrame(1, kFrameWidth, kFrameHeight));
4545 WaitForEncodedFrame(1);
4546 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4547 .low_encode_usage_threshold_percent,
4548 default_options.low_encode_usage_threshold_percent);
4549 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4550 .high_encode_usage_threshold_percent,
4551 default_options.high_encode_usage_threshold_percent);
4552 video_stream_encoder_->Stop();
4553}
4554
4555TEST_F(VideoStreamEncoderTest,
4556 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4557 const int kFrameWidth = 1280;
4558 const int kFrameHeight = 720;
4559 CpuOveruseOptions hardware_options;
4560 hardware_options.low_encode_usage_threshold_percent = 150;
4561 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004562 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004563
Erik Språng4c6ca302019-04-08 15:14:01 +02004564 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004565 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4566 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004567 video_source_.IncomingCapturedFrame(
4568 CreateFrame(1, kFrameWidth, kFrameHeight));
4569 WaitForEncodedFrame(1);
4570 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4571 .low_encode_usage_threshold_percent,
4572 hardware_options.low_encode_usage_threshold_percent);
4573 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4574 .high_encode_usage_threshold_percent,
4575 hardware_options.high_encode_usage_threshold_percent);
4576 video_stream_encoder_->Stop();
4577}
4578
Niels Möller6bb5ab92019-01-11 11:11:10 +01004579TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4580 const int kFrameWidth = 320;
4581 const int kFrameHeight = 240;
4582 const int kFps = 30;
4583 const int kTargetBitrateBps = 120000;
4584 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4585
Erik Språng4c6ca302019-04-08 15:14:01 +02004586 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004587 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4588 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004589
4590 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4591 max_framerate_ = kFps;
4592
4593 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4594 fake_encoder_.SimulateOvershoot(1.0);
4595 int num_dropped = 0;
4596 for (int i = 0; i < kNumFramesInRun; ++i) {
4597 video_source_.IncomingCapturedFrame(
4598 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4599 // Wait up to two frame durations for a frame to arrive.
4600 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4601 ++num_dropped;
4602 }
4603 timestamp_ms += 1000 / kFps;
4604 }
4605
Erik Språnga8d48ab2019-02-08 14:17:40 +01004606 // Framerate should be measured to be near the expected target rate.
4607 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4608
4609 // Frame drops should be within 5% of expected 0%.
4610 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004611
4612 // Make encoder produce frames at double the expected bitrate during 3 seconds
4613 // of video, verify number of drops. Rate needs to be slightly changed in
4614 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004615 double overshoot_factor = 2.0;
4616 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4617 // With bitrate adjuster, when need to overshoot even more to trigger
4618 // frame dropping.
4619 overshoot_factor *= 2;
4620 }
4621 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004622 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004623 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004624 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004625 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004626 num_dropped = 0;
4627 for (int i = 0; i < kNumFramesInRun; ++i) {
4628 video_source_.IncomingCapturedFrame(
4629 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4630 // Wait up to two frame durations for a frame to arrive.
4631 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4632 ++num_dropped;
4633 }
4634 timestamp_ms += 1000 / kFps;
4635 }
4636
Erik Språng4c6ca302019-04-08 15:14:01 +02004637 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004638 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4639 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004640
4641 // Target framerate should be still be near the expected target, despite
4642 // the frame drops.
4643 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4644
4645 // Frame drops should be within 5% of expected 50%.
4646 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004647
4648 video_stream_encoder_->Stop();
4649}
4650
4651TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4652 const int kFrameWidth = 320;
4653 const int kFrameHeight = 240;
4654 const int kActualInputFps = 24;
4655 const int kTargetBitrateBps = 120000;
4656
4657 ASSERT_GT(max_framerate_, kActualInputFps);
4658
4659 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4660 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004661 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004662 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4663 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004664
4665 // Insert 3 seconds of video, with an input fps lower than configured max.
4666 for (int i = 0; i < kActualInputFps * 3; ++i) {
4667 video_source_.IncomingCapturedFrame(
4668 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4669 // Wait up to two frame durations for a frame to arrive.
4670 WaitForEncodedFrame(timestamp_ms);
4671 timestamp_ms += 1000 / kActualInputFps;
4672 }
4673
4674 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4675
4676 video_stream_encoder_->Stop();
4677}
4678
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004679TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4680 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004681 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004682 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4683 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004684
4685 fake_encoder_.BlockNextEncode();
4686 video_source_.IncomingCapturedFrame(
4687 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4688 WaitForEncodedFrame(1);
4689 // On the very first frame full update should be forced.
4690 rect = fake_encoder_.GetLastUpdateRect();
4691 EXPECT_EQ(rect.offset_x, 0);
4692 EXPECT_EQ(rect.offset_y, 0);
4693 EXPECT_EQ(rect.height, codec_height_);
4694 EXPECT_EQ(rect.width, codec_width_);
4695 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4696 // call to ContinueEncode.
4697 video_source_.IncomingCapturedFrame(
4698 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4699 ExpectDroppedFrame();
4700 video_source_.IncomingCapturedFrame(
4701 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4702 ExpectDroppedFrame();
4703 fake_encoder_.ContinueEncode();
4704 WaitForEncodedFrame(3);
4705 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4706 rect = fake_encoder_.GetLastUpdateRect();
4707 EXPECT_EQ(rect.offset_x, 1);
4708 EXPECT_EQ(rect.offset_y, 0);
4709 EXPECT_EQ(rect.width, 10);
4710 EXPECT_EQ(rect.height, 1);
4711
4712 video_source_.IncomingCapturedFrame(
4713 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4714 WaitForEncodedFrame(4);
4715 // Previous frame was encoded, so no accumulation should happen.
4716 rect = fake_encoder_.GetLastUpdateRect();
4717 EXPECT_EQ(rect.offset_x, 0);
4718 EXPECT_EQ(rect.offset_y, 0);
4719 EXPECT_EQ(rect.width, 1);
4720 EXPECT_EQ(rect.height, 1);
4721
4722 video_stream_encoder_->Stop();
4723}
4724
Erik Språngd7329ca2019-02-21 21:19:53 +01004725TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004726 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004727 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4728 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004729
4730 // First frame is always keyframe.
4731 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4732 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004733 EXPECT_THAT(
4734 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004735 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004736
4737 // Insert delta frame.
4738 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4739 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004740 EXPECT_THAT(
4741 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004742 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004743
4744 // Request next frame be a key-frame.
4745 video_stream_encoder_->SendKeyFrame();
4746 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4747 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004748 EXPECT_THAT(
4749 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004750 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004751
4752 video_stream_encoder_->Stop();
4753}
4754
4755TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4756 // Setup simulcast with three streams.
4757 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004758 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004759 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004760 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004761 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004762 // Wait for all three layers before triggering event.
4763 sink_.SetNumExpectedLayers(3);
4764
4765 // First frame is always keyframe.
4766 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4767 WaitForEncodedFrame(1);
4768 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004769 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4770 VideoFrameType::kVideoFrameKey,
4771 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004772
4773 // Insert delta frame.
4774 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4775 WaitForEncodedFrame(2);
4776 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004777 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4778 VideoFrameType::kVideoFrameDelta,
4779 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004780
4781 // Request next frame be a key-frame.
4782 // Only first stream is configured to produce key-frame.
4783 video_stream_encoder_->SendKeyFrame();
4784 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4785 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004786
4787 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4788 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004789 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004790 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004791 VideoFrameType::kVideoFrameKey,
4792 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004793
4794 video_stream_encoder_->Stop();
4795}
4796
4797TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4798 // Configure internal source factory and setup test again.
4799 encoder_factory_.SetHasInternalSource(true);
4800 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004801 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004802 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4803 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004804
4805 // Call encoder directly, simulating internal source where encoded frame
4806 // callback in VideoStreamEncoder is called despite no OnFrame().
4807 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4808 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004809 EXPECT_THAT(
4810 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004811 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004812
Niels Möller8f7ce222019-03-21 15:43:58 +01004813 const std::vector<VideoFrameType> kDeltaFrame = {
4814 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004815 // Need to set timestamp manually since manually for injected frame.
4816 VideoFrame frame = CreateFrame(101, nullptr);
4817 frame.set_timestamp(101);
4818 fake_encoder_.InjectFrame(frame, false);
4819 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004820 EXPECT_THAT(
4821 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004822 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004823
4824 // Request key-frame. The forces a dummy frame down into the encoder.
4825 fake_encoder_.ExpectNullFrame();
4826 video_stream_encoder_->SendKeyFrame();
4827 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004828 EXPECT_THAT(
4829 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004830 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004831
4832 video_stream_encoder_->Stop();
4833}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004834
4835TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4836 // Configure internal source factory and setup test again.
4837 encoder_factory_.SetHasInternalSource(true);
4838 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004839 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004840 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4841 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004842
4843 int64_t timestamp = 1;
4844 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004845 image.SetEncodedData(
4846 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004847 image.capture_time_ms_ = ++timestamp;
4848 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4849 const int64_t kEncodeFinishDelayMs = 10;
4850 image.timing_.encode_start_ms = timestamp;
4851 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4852 fake_encoder_.InjectEncodedImage(image);
4853 // Wait for frame without incrementing clock.
4854 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4855 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4856 // capture timestamp should be kEncodeFinishDelayMs in the past.
4857 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4858 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4859 kEncodeFinishDelayMs);
4860
4861 video_stream_encoder_->Stop();
4862}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004863
4864TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4865 // Configure internal source factory and setup test again.
4866 encoder_factory_.SetHasInternalSource(true);
4867 ResetEncoder("H264", 1, 1, 1, false);
4868
4869 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4870 image._frameType = VideoFrameType::kVideoFrameKey;
4871
4872 CodecSpecificInfo codec_specific_info;
4873 codec_specific_info.codecType = kVideoCodecH264;
4874
4875 RTPFragmentationHeader fragmentation;
4876 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4877 fragmentation.fragmentationOffset[0] = 4;
4878 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4879
4880 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4881 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4882
4883 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4884 testing::ElementsAreArray(optimal_sps));
4885 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4886 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4887 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4888 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4889
4890 video_stream_encoder_->Stop();
4891}
4892
4893TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4894 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4895 0x00, 0x00, 0x03, 0x03, 0xF4,
4896 0x05, 0x03, 0xC7, 0xC0};
4897
4898 // Configure internal source factory and setup test again.
4899 encoder_factory_.SetHasInternalSource(true);
4900 ResetEncoder("H264", 1, 1, 1, false);
4901
4902 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4903 image._frameType = VideoFrameType::kVideoFrameKey;
4904
4905 CodecSpecificInfo codec_specific_info;
4906 codec_specific_info.codecType = kVideoCodecH264;
4907
4908 RTPFragmentationHeader fragmentation;
4909 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4910 fragmentation.fragmentationOffset[0] = 4;
4911 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4912
4913 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4914 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4915
4916 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4917 testing::ElementsAreArray(optimal_sps));
4918 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4919 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4920 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4921 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4922
4923 video_stream_encoder_->Stop();
4924}
4925
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004926TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4927 const int kFrameWidth = 1280;
4928 const int kFrameHeight = 720;
4929 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4930
4931 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004932 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4933 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004934 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4935
4936 // Insert a first video frame. It should be dropped because of downscale in
4937 // resolution.
4938 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4939 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4940 frame.set_rotation(kVideoRotation_270);
4941 video_source_.IncomingCapturedFrame(frame);
4942
4943 ExpectDroppedFrame();
4944
4945 // Second frame is downscaled.
4946 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4947 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4948 frame.set_rotation(kVideoRotation_90);
4949 video_source_.IncomingCapturedFrame(frame);
4950
4951 WaitForEncodedFrame(timestamp_ms);
4952 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4953
4954 // Insert another frame, also downscaled.
4955 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4956 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4957 frame.set_rotation(kVideoRotation_180);
4958 video_source_.IncomingCapturedFrame(frame);
4959
4960 WaitForEncodedFrame(timestamp_ms);
4961 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4962
4963 video_stream_encoder_->Stop();
4964}
4965
Erik Språng5056af02019-09-02 15:53:11 +02004966TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4967 const int kFrameWidth = 320;
4968 const int kFrameHeight = 180;
4969
4970 // Initial rate.
4971 video_stream_encoder_->OnBitrateUpdated(
4972 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004973 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004974 /*link_allocation=*/DataRate::kbps(300),
4975 /*fraction_lost=*/0,
4976 /*rtt_ms=*/0);
4977
4978 // Insert a first video frame so that encoder gets configured.
4979 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4980 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4981 frame.set_rotation(kVideoRotation_270);
4982 video_source_.IncomingCapturedFrame(frame);
4983 WaitForEncodedFrame(timestamp_ms);
4984
4985 // Set a target rate below the minimum allowed by the codec settings.
4986 VideoCodec codec_config = fake_encoder_.codec_config();
4987 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4988 DataRate target_rate = min_rate - DataRate::kbps(1);
4989 video_stream_encoder_->OnBitrateUpdated(
4990 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004991 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004992 /*link_allocation=*/target_rate,
4993 /*fraction_lost=*/0,
4994 /*rtt_ms=*/0);
4995 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4996
4997 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4998 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4999 ASSERT_TRUE(rate_settings.has_value());
5000 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5001 EXPECT_EQ(min_rate, allocation_sum);
5002 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5003
5004 video_stream_encoder_->Stop();
5005}
5006
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005007TEST_F(VideoStreamEncoderTest, EncoderRatesPropegatedOnReconfigure) {
5008 video_stream_encoder_->OnBitrateUpdated(
5009 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
5010 DataRate::bps(kTargetBitrateBps), 0, 0);
5011 // Capture a frame and wait for it to synchronize with the encoder thread.
5012 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5013 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5014 WaitForEncodedFrame(1);
5015
5016 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5017 ASSERT_TRUE(prev_rate_settings.has_value());
5018 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5019 kDefaultFramerate);
5020
5021 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5022 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5023 timestamp_ms += 1000 / kDefaultFramerate;
5024 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5025 WaitForEncodedFrame(timestamp_ms);
5026 }
5027 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5028 kDefaultFramerate);
5029 // Capture larger frame to trigger a reconfigure.
5030 codec_height_ *= 2;
5031 codec_width_ *= 2;
5032 timestamp_ms += 1000 / kDefaultFramerate;
5033 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5034 WaitForEncodedFrame(timestamp_ms);
5035
5036 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5037 auto current_rate_settings =
5038 fake_encoder_.GetAndResetLastRateControlSettings();
5039 // Ensure we have actually reconfigured twice
5040 // The rate settings should have been set again even though
5041 // they haven't changed.
5042 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005043 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005044
5045 video_stream_encoder_->Stop();
5046}
5047
philipeld9cc8c02019-09-16 14:53:40 +02005048struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5049 MOCK_METHOD0(RequestEncoderFallback, void());
5050 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
5051};
5052
5053TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5054 constexpr int kDontCare = 100;
5055
5056 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5057 video_send_config_.encoder_settings.encoder_switch_request_callback =
5058 &switch_callback;
5059 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5060 encoder_config.codec_type = kVideoCodecVP8;
5061 webrtc::test::ScopedFieldTrials field_trial(
5062 "WebRTC-NetworkCondition-EncoderSwitch/"
5063 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5064 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5065
5066 // Reset encoder for new configuration to take effect.
5067 ConfigureEncoder(std::move(encoder_config));
5068
5069 // Send one frame to trigger ReconfigureEncoder.
5070 video_source_.IncomingCapturedFrame(
5071 CreateFrame(kDontCare, kDontCare, kDontCare));
5072
5073 using Config = EncoderSwitchRequestCallback::Config;
5074 EXPECT_CALL(switch_callback,
5075 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5076 Field(&Config::param, "ping"),
5077 Field(&Config::value, "pong"))));
5078
5079 video_stream_encoder_->OnBitrateUpdated(
5080 /*target_bitrate=*/DataRate::kbps(50),
5081 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5082 /*link_allocation=*/DataRate::kbps(kDontCare),
5083 /*fraction_lost=*/0,
5084 /*rtt_ms=*/0);
5085
5086 video_stream_encoder_->Stop();
5087}
5088
5089TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5090 constexpr int kSufficientBitrateToNotDrop = 1000;
5091 constexpr int kHighRes = 500;
5092 constexpr int kLowRes = 100;
5093
5094 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5095 video_send_config_.encoder_settings.encoder_switch_request_callback =
5096 &switch_callback;
5097 webrtc::test::ScopedFieldTrials field_trial(
5098 "WebRTC-NetworkCondition-EncoderSwitch/"
5099 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5100 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5101 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5102 encoder_config.codec_type = kVideoCodecH264;
5103
5104 // Reset encoder for new configuration to take effect.
5105 ConfigureEncoder(std::move(encoder_config));
5106
5107 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5108 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5109 // not fail.
5110 video_stream_encoder_->OnBitrateUpdated(
5111 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5112 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5113 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5114 /*fraction_lost=*/0,
5115 /*rtt_ms=*/0);
5116
5117 // Send one frame to trigger ReconfigureEncoder.
5118 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5119 WaitForEncodedFrame(1);
5120
5121 using Config = EncoderSwitchRequestCallback::Config;
5122 EXPECT_CALL(switch_callback,
5123 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5124 Field(&Config::param, "ping"),
5125 Field(&Config::value, "pong"))));
5126
5127 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5128 WaitForEncodedFrame(2);
5129
5130 video_stream_encoder_->Stop();
5131}
5132
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005133TEST_F(VideoStreamEncoderTest,
5134 AllocationPropegratedToEncoderWhenTargetRateChanged) {
5135 const int kFrameWidth = 320;
5136 const int kFrameHeight = 180;
5137
5138 // Set initial rate.
5139 auto rate = DataRate::kbps(100);
5140 video_stream_encoder_->OnBitrateUpdated(
5141 /*target_bitrate=*/rate,
5142 /*stable_target_bitrate=*/rate,
5143 /*link_allocation=*/rate,
5144 /*fraction_lost=*/0,
5145 /*rtt_ms=*/0);
5146
5147 // Insert a first video frame so that encoder gets configured.
5148 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5149 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5150 frame.set_rotation(kVideoRotation_270);
5151 video_source_.IncomingCapturedFrame(frame);
5152 WaitForEncodedFrame(timestamp_ms);
5153 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5154
5155 // Change of target bitrate propagates to the encoder.
5156 auto new_stable_rate = rate - DataRate::kbps(5);
5157 video_stream_encoder_->OnBitrateUpdated(
5158 /*target_bitrate=*/new_stable_rate,
5159 /*stable_target_bitrate=*/new_stable_rate,
5160 /*link_allocation=*/rate,
5161 /*fraction_lost=*/0,
5162 /*rtt_ms=*/0);
5163 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5164 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5165 video_stream_encoder_->Stop();
5166}
5167
5168TEST_F(VideoStreamEncoderTest,
5169 AllocationNotPropegratedToEncoderWhenTargetRateUnchanged) {
5170 const int kFrameWidth = 320;
5171 const int kFrameHeight = 180;
5172
5173 // Set initial rate.
5174 auto rate = DataRate::kbps(100);
5175 video_stream_encoder_->OnBitrateUpdated(
5176 /*target_bitrate=*/rate,
5177 /*stable_target_bitrate=*/rate,
5178 /*link_allocation=*/rate,
5179 /*fraction_lost=*/0,
5180 /*rtt_ms=*/0);
5181
5182 // Insert a first video frame so that encoder gets configured.
5183 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5184 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5185 frame.set_rotation(kVideoRotation_270);
5186 video_source_.IncomingCapturedFrame(frame);
5187 WaitForEncodedFrame(timestamp_ms);
5188 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5189
5190 // Set a higher target rate without changing the link_allocation. Should not
5191 // reset encoder's rate.
5192 auto new_stable_rate = rate - DataRate::kbps(5);
5193 video_stream_encoder_->OnBitrateUpdated(
5194 /*target_bitrate=*/rate,
5195 /*stable_target_bitrate=*/new_stable_rate,
5196 /*link_allocation=*/rate,
5197 /*fraction_lost=*/0,
5198 /*rtt_ms=*/0);
5199 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5200 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5201 video_stream_encoder_->Stop();
5202}
5203
perkj26091b12016-09-01 01:17:40 -07005204} // namespace webrtc