blob: 52d98f4d72e5b190e820325eef6986dd33e41ef3 [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;
58const int kMinFramerateFps = 2;
59const int kMinBalancedFramerateFps = 7;
60const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080061const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010062const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020063const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010064const uint32_t kSimulcastTargetBitrateBps = 3150000;
65const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080066const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070067const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020068const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020069const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020070const VideoEncoder::ResolutionBitrateLimits
71 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
72const VideoEncoder::ResolutionBitrateLimits
73 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080074
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020075uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
76 0x00, 0x00, 0x03, 0x03, 0xF4,
77 0x05, 0x03, 0xC7, 0xE0, 0x1B,
78 0x41, 0x10, 0x8D, 0x00};
79
perkj803d97f2016-11-01 11:45:46 -070080class TestBuffer : public webrtc::I420Buffer {
81 public:
82 TestBuffer(rtc::Event* event, int width, int height)
83 : I420Buffer(width, height), event_(event) {}
84
85 private:
86 friend class rtc::RefCountedObject<TestBuffer>;
87 ~TestBuffer() override {
88 if (event_)
89 event_->Set();
90 }
91 rtc::Event* const event_;
92};
93
Noah Richards51db4212019-06-12 06:59:12 -070094// A fake native buffer that can't be converted to I420.
95class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
96 public:
97 FakeNativeBuffer(rtc::Event* event, int width, int height)
98 : event_(event), width_(width), height_(height) {}
99 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
100 int width() const override { return width_; }
101 int height() const override { return height_; }
102 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
103 return nullptr;
104 }
105
106 private:
107 friend class rtc::RefCountedObject<FakeNativeBuffer>;
108 ~FakeNativeBuffer() override {
109 if (event_)
110 event_->Set();
111 }
112 rtc::Event* const event_;
113 const int width_;
114 const int height_;
115};
116
Niels Möller7dc26b72017-12-06 10:27:48 +0100117class CpuOveruseDetectorProxy : public OveruseFrameDetector {
118 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200119 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
120 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100121 last_target_framerate_fps_(-1) {}
122 virtual ~CpuOveruseDetectorProxy() {}
123
124 void OnTargetFramerateUpdated(int framerate_fps) override {
125 rtc::CritScope cs(&lock_);
126 last_target_framerate_fps_ = framerate_fps;
127 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
128 }
129
130 int GetLastTargetFramerate() {
131 rtc::CritScope cs(&lock_);
132 return last_target_framerate_fps_;
133 }
134
Niels Möller4db138e2018-04-19 09:04:13 +0200135 CpuOveruseOptions GetOptions() { return options_; }
136
Niels Möller7dc26b72017-12-06 10:27:48 +0100137 private:
138 rtc::CriticalSection lock_;
139 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
140};
141
mflodmancc3d4422017-08-03 08:27:51 -0700142class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700143 public:
Niels Möller213618e2018-07-24 09:29:58 +0200144 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200145 const VideoStreamEncoderSettings& settings,
146 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100147 : VideoStreamEncoder(Clock::GetRealTimeClock(),
148 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200149 stats_proxy,
150 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200151 std::unique_ptr<OveruseFrameDetector>(
152 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100153 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200154 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700155
sprangb1ca0732017-02-01 08:38:12 -0800156 void PostTaskAndWait(bool down, AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200157 PostTaskAndWait(down, reason, /*expected_results=*/true);
158 }
159
160 void PostTaskAndWait(bool down, AdaptReason reason, bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100161 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200162 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200163 if (down)
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200164 EXPECT_EQ(expected_results, AdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200165 else
166 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700167 event.Set();
168 });
perkj070ba852017-02-16 15:46:27 -0800169 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700170 }
171
kthelgason2fc52542017-03-03 00:24:41 -0800172 // This is used as a synchronisation mechanism, to make sure that the
173 // encoder queue is not blocked before we start sending it frames.
174 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100175 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200176 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800177 ASSERT_TRUE(event.Wait(5000));
178 }
179
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200180 void TriggerCpuOveruse() {
181 PostTaskAndWait(/*down=*/true, AdaptReason::kCpu);
182 }
kthelgason876222f2016-11-29 01:44:11 -0800183
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200184 void TriggerCpuNormalUsage() {
185 PostTaskAndWait(/*down=*/false, AdaptReason::kCpu);
186 }
kthelgason876222f2016-11-29 01:44:11 -0800187
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200188 void TriggerQualityLow() {
189 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality);
190 }
kthelgason876222f2016-11-29 01:44:11 -0800191
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200192 void TriggerQualityLowExpectFalse() {
193 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality,
194 /*expected_results=*/false);
195 }
196
197 void TriggerQualityHigh() {
198 PostTaskAndWait(/*down=*/false, AdaptReason::kQuality);
199 }
sprangfda496a2017-06-15 04:21:07 -0700200
Niels Möller7dc26b72017-12-06 10:27:48 +0100201 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700202};
203
asapersson5f7226f2016-11-25 04:37:00 -0800204class VideoStreamFactory
205 : public VideoEncoderConfig::VideoStreamFactoryInterface {
206 public:
sprangfda496a2017-06-15 04:21:07 -0700207 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
208 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800209 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700210 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800211 }
212
213 private:
214 std::vector<VideoStream> CreateEncoderStreams(
215 int width,
216 int height,
217 const VideoEncoderConfig& encoder_config) override {
218 std::vector<VideoStream> streams =
219 test::CreateVideoStreams(width, height, encoder_config);
220 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100221 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700222 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800223 }
224 return streams;
225 }
sprangfda496a2017-06-15 04:21:07 -0700226
asapersson5f7226f2016-11-25 04:37:00 -0800227 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700228 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800229};
230
Noah Richards51db4212019-06-12 06:59:12 -0700231// Simulates simulcast behavior and makes highest stream resolutions divisible
232// by 4.
233class CroppingVideoStreamFactory
234 : public VideoEncoderConfig::VideoStreamFactoryInterface {
235 public:
236 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
237 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
238 EXPECT_GT(num_temporal_layers, 0u);
239 EXPECT_GT(framerate, 0);
240 }
241
242 private:
243 std::vector<VideoStream> CreateEncoderStreams(
244 int width,
245 int height,
246 const VideoEncoderConfig& encoder_config) override {
247 std::vector<VideoStream> streams = test::CreateVideoStreams(
248 width - width % 4, height - height % 4, encoder_config);
249 for (VideoStream& stream : streams) {
250 stream.num_temporal_layers = num_temporal_layers_;
251 stream.max_framerate = framerate_;
252 }
253 return streams;
254 }
255
256 const size_t num_temporal_layers_;
257 const int framerate_;
258};
259
sprangb1ca0732017-02-01 08:38:12 -0800260class AdaptingFrameForwarder : public test::FrameForwarder {
261 public:
262 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700263 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800264
265 void set_adaptation_enabled(bool enabled) {
266 rtc::CritScope cs(&crit_);
267 adaptation_enabled_ = enabled;
268 }
269
asaperssonfab67072017-04-04 05:51:49 -0700270 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800271 rtc::CritScope cs(&crit_);
272 return adaptation_enabled_;
273 }
274
asapersson09f05612017-05-15 23:40:18 -0700275 rtc::VideoSinkWants last_wants() const {
276 rtc::CritScope cs(&crit_);
277 return last_wants_;
278 }
279
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200280 absl::optional<int> last_sent_width() const { return last_width_; }
281 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800282
sprangb1ca0732017-02-01 08:38:12 -0800283 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
284 int cropped_width = 0;
285 int cropped_height = 0;
286 int out_width = 0;
287 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700288 if (adaption_enabled()) {
289 if (adapter_.AdaptFrameResolution(
290 video_frame.width(), video_frame.height(),
291 video_frame.timestamp_us() * 1000, &cropped_width,
292 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100293 VideoFrame adapted_frame =
294 VideoFrame::Builder()
295 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
296 nullptr, out_width, out_height))
297 .set_timestamp_rtp(99)
298 .set_timestamp_ms(99)
299 .set_rotation(kVideoRotation_0)
300 .build();
sprangc5d62e22017-04-02 23:53:04 -0700301 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
302 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800303 last_width_.emplace(adapted_frame.width());
304 last_height_.emplace(adapted_frame.height());
305 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200306 last_width_ = absl::nullopt;
307 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700308 }
sprangb1ca0732017-02-01 08:38:12 -0800309 } else {
310 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800311 last_width_.emplace(video_frame.width());
312 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800313 }
314 }
315
316 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
317 const rtc::VideoSinkWants& wants) override {
318 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700319 last_wants_ = sink_wants();
sprangc5d62e22017-04-02 23:53:04 -0700320 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
321 wants.max_pixel_count,
322 wants.max_framerate_fps);
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)
485 .set_update_rect({offset_x, 0, 1, 1})
486 .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_) {
686 info.scaling_settings =
687 VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame);
688 }
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
mflodmancc3d4422017-08-03 08:27:51 -07003707TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003708 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3709 const int kTooSmallWidth = 10;
3710 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003711 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003712 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3713 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003714
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003715 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003716 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003717 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003718 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003719 VerifyNoLimitation(source.sink_wants());
3720 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3721
3722 // Trigger adapt down, too small frame, expect no change.
3723 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003724 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003725 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003726 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003727 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3728 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3729
mflodmancc3d4422017-08-03 08:27:51 -07003730 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003731}
3732
mflodmancc3d4422017-08-03 08:27:51 -07003733TEST_F(VideoStreamEncoderTest,
3734 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003735 const int kTooSmallWidth = 10;
3736 const int kTooSmallHeight = 10;
3737 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003738 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003739 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3740 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003741
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003742 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003743 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003744 video_stream_encoder_->SetSource(&source,
3745 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003746 VerifyNoLimitation(source.sink_wants());
3747 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3748 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3749
3750 // Trigger adapt down, expect limited framerate.
3751 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003752 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003753 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003754 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3755 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3756 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3757 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3758
3759 // Trigger adapt down, too small frame, expect no change.
3760 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003761 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003762 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003763 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3764 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3765 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3766 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3767
mflodmancc3d4422017-08-03 08:27:51 -07003768 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003769}
3770
mflodmancc3d4422017-08-03 08:27:51 -07003771TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003772 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003773 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003774 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3775 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003776 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003777 const int kFrameWidth = 1280;
3778 const int kFrameHeight = 720;
3779 video_source_.IncomingCapturedFrame(
3780 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003781 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003782 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003783}
3784
sprangb1ca0732017-02-01 08:38:12 -08003785// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003786TEST_F(VideoStreamEncoderTest,
3787 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003788 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003789 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3790 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003791
3792 const int kFrameWidth = 1280;
3793 const int kFrameHeight = 720;
3794 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003795 // requested by
3796 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003797 video_source_.set_adaptation_enabled(true);
3798
3799 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003800 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003801 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003802
3803 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003804 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003805 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003806 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003807 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003808
asaperssonfab67072017-04-04 05:51:49 -07003809 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003810 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003811 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003812 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003813 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003814
mflodmancc3d4422017-08-03 08:27:51 -07003815 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003816}
sprangfe627f32017-03-29 08:24:59 -07003817
mflodmancc3d4422017-08-03 08:27:51 -07003818TEST_F(VideoStreamEncoderTest,
3819 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003820 const int kFrameWidth = 1280;
3821 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003822
Erik Språng4c6ca302019-04-08 15:14:01 +02003823 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003824 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3825 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003826 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003827 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003828 video_source_.set_adaptation_enabled(true);
3829
sprang4847ae62017-06-27 07:06:52 -07003830 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003831
3832 video_source_.IncomingCapturedFrame(
3833 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003834 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003835
3836 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003837 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003838
3839 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003840 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003841 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003842 video_source_.IncomingCapturedFrame(
3843 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003844 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003845 }
3846
3847 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003848 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003849 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003850 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003851 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003852 video_source_.IncomingCapturedFrame(
3853 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003854 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003855 ++num_frames_dropped;
3856 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003857 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003858 }
3859 }
3860
sprang4847ae62017-06-27 07:06:52 -07003861 // Add some slack to account for frames dropped by the frame dropper.
3862 const int kErrorMargin = 1;
3863 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003864 kErrorMargin);
3865
3866 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003867 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003868 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003869 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003870 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003871 video_source_.IncomingCapturedFrame(
3872 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003873 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003874 ++num_frames_dropped;
3875 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003876 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003877 }
3878 }
sprang4847ae62017-06-27 07:06:52 -07003879 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003880 kErrorMargin);
3881
3882 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003883 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003884 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003885 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003886 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003887 video_source_.IncomingCapturedFrame(
3888 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003889 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003890 ++num_frames_dropped;
3891 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003892 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003893 }
3894 }
sprang4847ae62017-06-27 07:06:52 -07003895 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003896 kErrorMargin);
3897
3898 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003899 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003900 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003901 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003902 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003903 video_source_.IncomingCapturedFrame(
3904 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003905 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003906 ++num_frames_dropped;
3907 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003908 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003909 }
3910 }
3911 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3912
mflodmancc3d4422017-08-03 08:27:51 -07003913 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003914}
3915
mflodmancc3d4422017-08-03 08:27:51 -07003916TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003917 const int kFramerateFps = 5;
3918 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003919 const int kFrameWidth = 1280;
3920 const int kFrameHeight = 720;
3921
sprang4847ae62017-06-27 07:06:52 -07003922 // Reconfigure encoder with two temporal layers and screensharing, which will
3923 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003924 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003925
Erik Språng4c6ca302019-04-08 15:14:01 +02003926 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003927 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3928 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003929 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003930 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003931 video_source_.set_adaptation_enabled(true);
3932
sprang4847ae62017-06-27 07:06:52 -07003933 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003934
3935 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003936 rtc::VideoSinkWants last_wants;
3937 do {
3938 last_wants = video_source_.sink_wants();
3939
sprangc5d62e22017-04-02 23:53:04 -07003940 // Insert frames to get a new fps estimate...
3941 for (int j = 0; j < kFramerateFps; ++j) {
3942 video_source_.IncomingCapturedFrame(
3943 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08003944 if (video_source_.last_sent_width()) {
3945 sink_.WaitForEncodedFrame(timestamp_ms);
3946 }
sprangc5d62e22017-04-02 23:53:04 -07003947 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02003948 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07003949 }
3950 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07003951 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08003952 } while (video_source_.sink_wants().max_framerate_fps <
3953 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07003954
Jonathan Yubc771b72017-12-08 17:04:29 -08003955 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07003956
mflodmancc3d4422017-08-03 08:27:51 -07003957 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003958}
asaperssonf7e294d2017-06-13 23:25:22 -07003959
mflodmancc3d4422017-08-03 08:27:51 -07003960TEST_F(VideoStreamEncoderTest,
3961 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003962 const int kWidth = 1280;
3963 const int kHeight = 720;
3964 const int64_t kFrameIntervalMs = 150;
3965 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003966 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003967 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3968 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003969
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003970 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003971 AdaptingFrameForwarder source;
3972 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003973 video_stream_encoder_->SetSource(&source,
3974 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003975 timestamp_ms += kFrameIntervalMs;
3976 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003977 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003978 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003979 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3980 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3981 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3982
3983 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003984 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003985 timestamp_ms += kFrameIntervalMs;
3986 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003987 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003988 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
3989 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3990 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3991 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3992
3993 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003994 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003995 timestamp_ms += kFrameIntervalMs;
3996 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003997 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003998 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
3999 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4000 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4001 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4002
4003 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004004 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004005 timestamp_ms += kFrameIntervalMs;
4006 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004007 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004008 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4009 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4010 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4011 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4012
4013 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004014 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004015 timestamp_ms += kFrameIntervalMs;
4016 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004017 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004018 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4019 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4020 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4021 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4022
4023 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004024 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004025 timestamp_ms += kFrameIntervalMs;
4026 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004027 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004028 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4029 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4030 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4031 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4032
4033 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004034 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004035 timestamp_ms += kFrameIntervalMs;
4036 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004037 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004038 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4039 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4040 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4041 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4042
4043 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004044 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004045 timestamp_ms += kFrameIntervalMs;
4046 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004047 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004048 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4049 rtc::VideoSinkWants last_wants = source.sink_wants();
4050 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4051 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4052 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4053
4054 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004055 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004056 timestamp_ms += kFrameIntervalMs;
4057 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004058 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004059 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4060 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4061 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4062 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4063
4064 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004065 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004066 timestamp_ms += kFrameIntervalMs;
4067 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004068 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004069 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4070 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4071 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4072 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4073
4074 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004075 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004076 timestamp_ms += kFrameIntervalMs;
4077 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004078 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004079 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4080 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4081 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4082 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4083
4084 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004085 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004086 timestamp_ms += kFrameIntervalMs;
4087 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004088 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004089 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4090 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4091 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4092 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4093
4094 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004095 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004096 timestamp_ms += kFrameIntervalMs;
4097 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004098 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004099 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4100 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4101 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4102 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4103
4104 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004105 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004106 timestamp_ms += kFrameIntervalMs;
4107 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004108 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004109 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4110 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4111 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4112 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4113
4114 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004115 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004116 timestamp_ms += kFrameIntervalMs;
4117 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004118 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004119 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4120 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4121 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4122 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4123
Åsa Persson30ab0152019-08-27 12:22:33 +02004124 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004125 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004126 timestamp_ms += kFrameIntervalMs;
4127 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004128 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004129 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004130 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004131 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4132 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4133 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4134
4135 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004136 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004137 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004138 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4139
mflodmancc3d4422017-08-03 08:27:51 -07004140 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004141}
4142
mflodmancc3d4422017-08-03 08:27:51 -07004143TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004144 const int kWidth = 1280;
4145 const int kHeight = 720;
4146 const int64_t kFrameIntervalMs = 150;
4147 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004148 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004149 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4150 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004151
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004152 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004153 AdaptingFrameForwarder source;
4154 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004155 video_stream_encoder_->SetSource(&source,
4156 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004157 timestamp_ms += kFrameIntervalMs;
4158 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004159 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004160 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004161 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4162 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4163 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4164 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4165 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4166 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4167
4168 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004169 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004170 timestamp_ms += kFrameIntervalMs;
4171 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004172 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004173 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4174 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4175 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4176 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4177 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4178 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4179 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4180
4181 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004182 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004183 timestamp_ms += kFrameIntervalMs;
4184 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004185 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004186 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4187 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4188 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4189 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4190 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4191 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4192 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4193
4194 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004195 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004196 timestamp_ms += kFrameIntervalMs;
4197 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004198 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004199 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4200 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4201 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4202 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4203 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4204 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4205 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4206
4207 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004208 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004209 timestamp_ms += kFrameIntervalMs;
4210 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004211 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004212 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4213 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4214 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4215 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4216 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4217 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4218 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4219
4220 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004221 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004222 timestamp_ms += kFrameIntervalMs;
4223 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004224 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004225 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4226 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4227 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4228 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4229 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4230 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4231 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4232
4233 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004234 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004235 timestamp_ms += kFrameIntervalMs;
4236 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004237 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004238 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004239 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004240 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4241 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4242 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4243 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4244 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4245 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4246
4247 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004248 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004249 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004250 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4251 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4252
mflodmancc3d4422017-08-03 08:27:51 -07004253 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004254}
4255
mflodmancc3d4422017-08-03 08:27:51 -07004256TEST_F(VideoStreamEncoderTest,
4257 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004258 const int kWidth = 640;
4259 const int kHeight = 360;
4260 const int kFpsLimit = 15;
4261 const int64_t kFrameIntervalMs = 150;
4262 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004263 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004264 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4265 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004266
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004267 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004268 AdaptingFrameForwarder source;
4269 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004270 video_stream_encoder_->SetSource(&source,
4271 webrtc::DegradationPreference::BALANCED);
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(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004275 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004276 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4277 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4278 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4279 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4280 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4281 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4282
4283 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004284 video_stream_encoder_->TriggerCpuOveruse();
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 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4289 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4290 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4291 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4292 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4293 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4294 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4295
4296 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004297 video_stream_encoder_->TriggerQualityLow();
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004301 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4302 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4303 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4304 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4305 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4306 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4307 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4308
4309 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004310 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004311 timestamp_ms += kFrameIntervalMs;
4312 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004313 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004314 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4315 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4316 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4317 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4318 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4319 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4320 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4321
4322 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004323 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004324 timestamp_ms += kFrameIntervalMs;
4325 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004326 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004327 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004328 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4329 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4330 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4331 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4332 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4333 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4334
4335 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004336 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004337 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004338 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4339 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4340
mflodmancc3d4422017-08-03 08:27:51 -07004341 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004342}
4343
mflodmancc3d4422017-08-03 08:27:51 -07004344TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004345 const int kFrameWidth = 1920;
4346 const int kFrameHeight = 1080;
4347 // 3/4 of 1920.
4348 const int kAdaptedFrameWidth = 1440;
4349 // 3/4 of 1080 rounded down to multiple of 4.
4350 const int kAdaptedFrameHeight = 808;
4351 const int kFramerate = 24;
4352
Erik Språng4c6ca302019-04-08 15:14:01 +02004353 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004354 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4355 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004356 // Trigger reconfigure encoder (without resetting the entire instance).
4357 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004358 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004359 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4360 video_encoder_config.number_of_streams = 1;
4361 video_encoder_config.video_stream_factory =
4362 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004363 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004364 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004365 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004366
4367 video_source_.set_adaptation_enabled(true);
4368
4369 video_source_.IncomingCapturedFrame(
4370 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004371 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004372
4373 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004374 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004375 video_source_.IncomingCapturedFrame(
4376 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004377 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004378
mflodmancc3d4422017-08-03 08:27:51 -07004379 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004380}
4381
mflodmancc3d4422017-08-03 08:27:51 -07004382TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004383 const int kFrameWidth = 1280;
4384 const int kFrameHeight = 720;
4385 const int kLowFps = 2;
4386 const int kHighFps = 30;
4387
Erik Språng4c6ca302019-04-08 15:14:01 +02004388 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004389 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4390 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004391
4392 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4393 max_framerate_ = kLowFps;
4394
4395 // Insert 2 seconds of 2fps video.
4396 for (int i = 0; i < kLowFps * 2; ++i) {
4397 video_source_.IncomingCapturedFrame(
4398 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4399 WaitForEncodedFrame(timestamp_ms);
4400 timestamp_ms += 1000 / kLowFps;
4401 }
4402
4403 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004404 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004405 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4406 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004407 video_source_.IncomingCapturedFrame(
4408 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4409 WaitForEncodedFrame(timestamp_ms);
4410 timestamp_ms += 1000 / kLowFps;
4411
4412 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4413
4414 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004415 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004416 const int kFrameIntervalMs = 1000 / kHighFps;
4417 max_framerate_ = kHighFps;
4418 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4419 video_source_.IncomingCapturedFrame(
4420 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4421 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4422 // be dropped if the encoder hans't been updated with the new higher target
4423 // framerate yet, causing it to overshoot the target bitrate and then
4424 // suffering the wrath of the media optimizer.
4425 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4426 timestamp_ms += kFrameIntervalMs;
4427 }
4428
4429 // Don expect correct measurement just yet, but it should be higher than
4430 // before.
4431 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4432
mflodmancc3d4422017-08-03 08:27:51 -07004433 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004434}
4435
mflodmancc3d4422017-08-03 08:27:51 -07004436TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004437 const int kFrameWidth = 1280;
4438 const int kFrameHeight = 720;
4439 const int kTargetBitrateBps = 1000000;
4440
4441 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004442 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004443 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004444 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4445 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004446 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004447
4448 // Insert a first video frame, causes another bitrate update.
4449 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4450 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4451 video_source_.IncomingCapturedFrame(
4452 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4453 WaitForEncodedFrame(timestamp_ms);
4454
4455 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004456 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4457 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004458
4459 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004460 timestamp_ms += kProcessIntervalMs;
4461 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004462
4463 // Bitrate observer should not be called.
4464 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4465 video_source_.IncomingCapturedFrame(
4466 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4467 ExpectDroppedFrame();
4468
mflodmancc3d4422017-08-03 08:27:51 -07004469 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004470}
ilnik6b826ef2017-06-16 06:53:48 -07004471
Niels Möller4db138e2018-04-19 09:04:13 +02004472TEST_F(VideoStreamEncoderTest,
4473 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4474 const int kFrameWidth = 1280;
4475 const int kFrameHeight = 720;
4476 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004477 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004478 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4479 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004480 video_source_.IncomingCapturedFrame(
4481 CreateFrame(1, kFrameWidth, kFrameHeight));
4482 WaitForEncodedFrame(1);
4483 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4484 .low_encode_usage_threshold_percent,
4485 default_options.low_encode_usage_threshold_percent);
4486 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4487 .high_encode_usage_threshold_percent,
4488 default_options.high_encode_usage_threshold_percent);
4489 video_stream_encoder_->Stop();
4490}
4491
4492TEST_F(VideoStreamEncoderTest,
4493 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4494 const int kFrameWidth = 1280;
4495 const int kFrameHeight = 720;
4496 CpuOveruseOptions hardware_options;
4497 hardware_options.low_encode_usage_threshold_percent = 150;
4498 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004499 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004500
Erik Språng4c6ca302019-04-08 15:14:01 +02004501 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004502 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4503 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004504 video_source_.IncomingCapturedFrame(
4505 CreateFrame(1, kFrameWidth, kFrameHeight));
4506 WaitForEncodedFrame(1);
4507 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4508 .low_encode_usage_threshold_percent,
4509 hardware_options.low_encode_usage_threshold_percent);
4510 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4511 .high_encode_usage_threshold_percent,
4512 hardware_options.high_encode_usage_threshold_percent);
4513 video_stream_encoder_->Stop();
4514}
4515
Niels Möller6bb5ab92019-01-11 11:11:10 +01004516TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4517 const int kFrameWidth = 320;
4518 const int kFrameHeight = 240;
4519 const int kFps = 30;
4520 const int kTargetBitrateBps = 120000;
4521 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4522
Erik Språng4c6ca302019-04-08 15:14:01 +02004523 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004524 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4525 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004526
4527 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4528 max_framerate_ = kFps;
4529
4530 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4531 fake_encoder_.SimulateOvershoot(1.0);
4532 int num_dropped = 0;
4533 for (int i = 0; i < kNumFramesInRun; ++i) {
4534 video_source_.IncomingCapturedFrame(
4535 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4536 // Wait up to two frame durations for a frame to arrive.
4537 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4538 ++num_dropped;
4539 }
4540 timestamp_ms += 1000 / kFps;
4541 }
4542
Erik Språnga8d48ab2019-02-08 14:17:40 +01004543 // Framerate should be measured to be near the expected target rate.
4544 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4545
4546 // Frame drops should be within 5% of expected 0%.
4547 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004548
4549 // Make encoder produce frames at double the expected bitrate during 3 seconds
4550 // of video, verify number of drops. Rate needs to be slightly changed in
4551 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004552 double overshoot_factor = 2.0;
4553 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4554 // With bitrate adjuster, when need to overshoot even more to trigger
4555 // frame dropping.
4556 overshoot_factor *= 2;
4557 }
4558 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004559 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004560 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004561 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004562 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004563 num_dropped = 0;
4564 for (int i = 0; i < kNumFramesInRun; ++i) {
4565 video_source_.IncomingCapturedFrame(
4566 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4567 // Wait up to two frame durations for a frame to arrive.
4568 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4569 ++num_dropped;
4570 }
4571 timestamp_ms += 1000 / kFps;
4572 }
4573
Erik Språng4c6ca302019-04-08 15:14:01 +02004574 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004575 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4576 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004577
4578 // Target framerate should be still be near the expected target, despite
4579 // the frame drops.
4580 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4581
4582 // Frame drops should be within 5% of expected 50%.
4583 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004584
4585 video_stream_encoder_->Stop();
4586}
4587
4588TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4589 const int kFrameWidth = 320;
4590 const int kFrameHeight = 240;
4591 const int kActualInputFps = 24;
4592 const int kTargetBitrateBps = 120000;
4593
4594 ASSERT_GT(max_framerate_, kActualInputFps);
4595
4596 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4597 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004598 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004599 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4600 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004601
4602 // Insert 3 seconds of video, with an input fps lower than configured max.
4603 for (int i = 0; i < kActualInputFps * 3; ++i) {
4604 video_source_.IncomingCapturedFrame(
4605 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4606 // Wait up to two frame durations for a frame to arrive.
4607 WaitForEncodedFrame(timestamp_ms);
4608 timestamp_ms += 1000 / kActualInputFps;
4609 }
4610
4611 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4612
4613 video_stream_encoder_->Stop();
4614}
4615
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004616TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4617 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004618 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004619 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4620 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004621
4622 fake_encoder_.BlockNextEncode();
4623 video_source_.IncomingCapturedFrame(
4624 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4625 WaitForEncodedFrame(1);
4626 // On the very first frame full update should be forced.
4627 rect = fake_encoder_.GetLastUpdateRect();
4628 EXPECT_EQ(rect.offset_x, 0);
4629 EXPECT_EQ(rect.offset_y, 0);
4630 EXPECT_EQ(rect.height, codec_height_);
4631 EXPECT_EQ(rect.width, codec_width_);
4632 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4633 // call to ContinueEncode.
4634 video_source_.IncomingCapturedFrame(
4635 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4636 ExpectDroppedFrame();
4637 video_source_.IncomingCapturedFrame(
4638 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4639 ExpectDroppedFrame();
4640 fake_encoder_.ContinueEncode();
4641 WaitForEncodedFrame(3);
4642 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4643 rect = fake_encoder_.GetLastUpdateRect();
4644 EXPECT_EQ(rect.offset_x, 1);
4645 EXPECT_EQ(rect.offset_y, 0);
4646 EXPECT_EQ(rect.width, 10);
4647 EXPECT_EQ(rect.height, 1);
4648
4649 video_source_.IncomingCapturedFrame(
4650 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4651 WaitForEncodedFrame(4);
4652 // Previous frame was encoded, so no accumulation should happen.
4653 rect = fake_encoder_.GetLastUpdateRect();
4654 EXPECT_EQ(rect.offset_x, 0);
4655 EXPECT_EQ(rect.offset_y, 0);
4656 EXPECT_EQ(rect.width, 1);
4657 EXPECT_EQ(rect.height, 1);
4658
4659 video_stream_encoder_->Stop();
4660}
4661
Erik Språngd7329ca2019-02-21 21:19:53 +01004662TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004663 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004664 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4665 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004666
4667 // First frame is always keyframe.
4668 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4669 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004670 EXPECT_THAT(
4671 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004672 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004673
4674 // Insert delta frame.
4675 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4676 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004677 EXPECT_THAT(
4678 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004679 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004680
4681 // Request next frame be a key-frame.
4682 video_stream_encoder_->SendKeyFrame();
4683 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4684 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004685 EXPECT_THAT(
4686 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004687 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004688
4689 video_stream_encoder_->Stop();
4690}
4691
4692TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4693 // Setup simulcast with three streams.
4694 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004695 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004696 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004697 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004698 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004699 // Wait for all three layers before triggering event.
4700 sink_.SetNumExpectedLayers(3);
4701
4702 // First frame is always keyframe.
4703 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4704 WaitForEncodedFrame(1);
4705 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004706 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4707 VideoFrameType::kVideoFrameKey,
4708 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004709
4710 // Insert delta frame.
4711 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4712 WaitForEncodedFrame(2);
4713 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004714 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4715 VideoFrameType::kVideoFrameDelta,
4716 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004717
4718 // Request next frame be a key-frame.
4719 // Only first stream is configured to produce key-frame.
4720 video_stream_encoder_->SendKeyFrame();
4721 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4722 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004723
4724 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4725 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004726 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004727 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004728 VideoFrameType::kVideoFrameKey,
4729 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004730
4731 video_stream_encoder_->Stop();
4732}
4733
4734TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4735 // Configure internal source factory and setup test again.
4736 encoder_factory_.SetHasInternalSource(true);
4737 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004738 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004739 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4740 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004741
4742 // Call encoder directly, simulating internal source where encoded frame
4743 // callback in VideoStreamEncoder is called despite no OnFrame().
4744 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4745 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004746 EXPECT_THAT(
4747 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004748 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004749
Niels Möller8f7ce222019-03-21 15:43:58 +01004750 const std::vector<VideoFrameType> kDeltaFrame = {
4751 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004752 // Need to set timestamp manually since manually for injected frame.
4753 VideoFrame frame = CreateFrame(101, nullptr);
4754 frame.set_timestamp(101);
4755 fake_encoder_.InjectFrame(frame, false);
4756 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004757 EXPECT_THAT(
4758 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004759 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004760
4761 // Request key-frame. The forces a dummy frame down into the encoder.
4762 fake_encoder_.ExpectNullFrame();
4763 video_stream_encoder_->SendKeyFrame();
4764 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004765 EXPECT_THAT(
4766 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004767 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004768
4769 video_stream_encoder_->Stop();
4770}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004771
4772TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4773 // Configure internal source factory and setup test again.
4774 encoder_factory_.SetHasInternalSource(true);
4775 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004776 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004777 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4778 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004779
4780 int64_t timestamp = 1;
4781 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004782 image.SetEncodedData(
4783 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004784 image.capture_time_ms_ = ++timestamp;
4785 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4786 const int64_t kEncodeFinishDelayMs = 10;
4787 image.timing_.encode_start_ms = timestamp;
4788 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4789 fake_encoder_.InjectEncodedImage(image);
4790 // Wait for frame without incrementing clock.
4791 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4792 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4793 // capture timestamp should be kEncodeFinishDelayMs in the past.
4794 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4795 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4796 kEncodeFinishDelayMs);
4797
4798 video_stream_encoder_->Stop();
4799}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004800
4801TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4802 // Configure internal source factory and setup test again.
4803 encoder_factory_.SetHasInternalSource(true);
4804 ResetEncoder("H264", 1, 1, 1, false);
4805
4806 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4807 image._frameType = VideoFrameType::kVideoFrameKey;
4808
4809 CodecSpecificInfo codec_specific_info;
4810 codec_specific_info.codecType = kVideoCodecH264;
4811
4812 RTPFragmentationHeader fragmentation;
4813 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4814 fragmentation.fragmentationOffset[0] = 4;
4815 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4816
4817 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4818 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4819
4820 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4821 testing::ElementsAreArray(optimal_sps));
4822 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4823 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4824 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4825 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4826
4827 video_stream_encoder_->Stop();
4828}
4829
4830TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4831 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4832 0x00, 0x00, 0x03, 0x03, 0xF4,
4833 0x05, 0x03, 0xC7, 0xC0};
4834
4835 // Configure internal source factory and setup test again.
4836 encoder_factory_.SetHasInternalSource(true);
4837 ResetEncoder("H264", 1, 1, 1, false);
4838
4839 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4840 image._frameType = VideoFrameType::kVideoFrameKey;
4841
4842 CodecSpecificInfo codec_specific_info;
4843 codec_specific_info.codecType = kVideoCodecH264;
4844
4845 RTPFragmentationHeader fragmentation;
4846 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4847 fragmentation.fragmentationOffset[0] = 4;
4848 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4849
4850 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4851 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4852
4853 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4854 testing::ElementsAreArray(optimal_sps));
4855 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4856 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4857 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4858 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4859
4860 video_stream_encoder_->Stop();
4861}
4862
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004863TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4864 const int kFrameWidth = 1280;
4865 const int kFrameHeight = 720;
4866 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4867
4868 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004869 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4870 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004871 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4872
4873 // Insert a first video frame. It should be dropped because of downscale in
4874 // resolution.
4875 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4876 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4877 frame.set_rotation(kVideoRotation_270);
4878 video_source_.IncomingCapturedFrame(frame);
4879
4880 ExpectDroppedFrame();
4881
4882 // Second frame is downscaled.
4883 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4884 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4885 frame.set_rotation(kVideoRotation_90);
4886 video_source_.IncomingCapturedFrame(frame);
4887
4888 WaitForEncodedFrame(timestamp_ms);
4889 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4890
4891 // Insert another frame, also downscaled.
4892 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4893 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4894 frame.set_rotation(kVideoRotation_180);
4895 video_source_.IncomingCapturedFrame(frame);
4896
4897 WaitForEncodedFrame(timestamp_ms);
4898 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4899
4900 video_stream_encoder_->Stop();
4901}
4902
Erik Språng5056af02019-09-02 15:53:11 +02004903TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4904 const int kFrameWidth = 320;
4905 const int kFrameHeight = 180;
4906
4907 // Initial rate.
4908 video_stream_encoder_->OnBitrateUpdated(
4909 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004910 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004911 /*link_allocation=*/DataRate::kbps(300),
4912 /*fraction_lost=*/0,
4913 /*rtt_ms=*/0);
4914
4915 // Insert a first video frame so that encoder gets configured.
4916 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4917 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4918 frame.set_rotation(kVideoRotation_270);
4919 video_source_.IncomingCapturedFrame(frame);
4920 WaitForEncodedFrame(timestamp_ms);
4921
4922 // Set a target rate below the minimum allowed by the codec settings.
4923 VideoCodec codec_config = fake_encoder_.codec_config();
4924 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4925 DataRate target_rate = min_rate - DataRate::kbps(1);
4926 video_stream_encoder_->OnBitrateUpdated(
4927 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004928 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004929 /*link_allocation=*/target_rate,
4930 /*fraction_lost=*/0,
4931 /*rtt_ms=*/0);
4932 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4933
4934 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4935 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4936 ASSERT_TRUE(rate_settings.has_value());
4937 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
4938 EXPECT_EQ(min_rate, allocation_sum);
4939 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
4940
4941 video_stream_encoder_->Stop();
4942}
4943
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02004944TEST_F(VideoStreamEncoderTest, EncoderRatesPropegatedOnReconfigure) {
4945 video_stream_encoder_->OnBitrateUpdated(
4946 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4947 DataRate::bps(kTargetBitrateBps), 0, 0);
4948 // Capture a frame and wait for it to synchronize with the encoder thread.
4949 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4950 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4951 WaitForEncodedFrame(1);
4952
4953 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4954 ASSERT_TRUE(prev_rate_settings.has_value());
4955 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
4956 kDefaultFramerate);
4957
4958 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
4959 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
4960 timestamp_ms += 1000 / kDefaultFramerate;
4961 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4962 WaitForEncodedFrame(timestamp_ms);
4963 }
4964 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
4965 kDefaultFramerate);
4966 // Capture larger frame to trigger a reconfigure.
4967 codec_height_ *= 2;
4968 codec_width_ *= 2;
4969 timestamp_ms += 1000 / kDefaultFramerate;
4970 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4971 WaitForEncodedFrame(timestamp_ms);
4972
4973 EXPECT_EQ(2, sink_.number_of_reconfigurations());
4974 auto current_rate_settings =
4975 fake_encoder_.GetAndResetLastRateControlSettings();
4976 // Ensure we have actually reconfigured twice
4977 // The rate settings should have been set again even though
4978 // they haven't changed.
4979 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02004980 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02004981
4982 video_stream_encoder_->Stop();
4983}
4984
philipeld9cc8c02019-09-16 14:53:40 +02004985struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
4986 MOCK_METHOD0(RequestEncoderFallback, void());
4987 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
4988};
4989
4990TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
4991 constexpr int kDontCare = 100;
4992
4993 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4994 video_send_config_.encoder_settings.encoder_switch_request_callback =
4995 &switch_callback;
4996 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4997 encoder_config.codec_type = kVideoCodecVP8;
4998 webrtc::test::ScopedFieldTrials field_trial(
4999 "WebRTC-NetworkCondition-EncoderSwitch/"
5000 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5001 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5002
5003 // Reset encoder for new configuration to take effect.
5004 ConfigureEncoder(std::move(encoder_config));
5005
5006 // Send one frame to trigger ReconfigureEncoder.
5007 video_source_.IncomingCapturedFrame(
5008 CreateFrame(kDontCare, kDontCare, kDontCare));
5009
5010 using Config = EncoderSwitchRequestCallback::Config;
5011 EXPECT_CALL(switch_callback,
5012 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5013 Field(&Config::param, "ping"),
5014 Field(&Config::value, "pong"))));
5015
5016 video_stream_encoder_->OnBitrateUpdated(
5017 /*target_bitrate=*/DataRate::kbps(50),
5018 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5019 /*link_allocation=*/DataRate::kbps(kDontCare),
5020 /*fraction_lost=*/0,
5021 /*rtt_ms=*/0);
5022
5023 video_stream_encoder_->Stop();
5024}
5025
5026TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5027 constexpr int kSufficientBitrateToNotDrop = 1000;
5028 constexpr int kHighRes = 500;
5029 constexpr int kLowRes = 100;
5030
5031 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5032 video_send_config_.encoder_settings.encoder_switch_request_callback =
5033 &switch_callback;
5034 webrtc::test::ScopedFieldTrials field_trial(
5035 "WebRTC-NetworkCondition-EncoderSwitch/"
5036 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5037 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5038 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5039 encoder_config.codec_type = kVideoCodecH264;
5040
5041 // Reset encoder for new configuration to take effect.
5042 ConfigureEncoder(std::move(encoder_config));
5043
5044 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5045 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5046 // not fail.
5047 video_stream_encoder_->OnBitrateUpdated(
5048 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5049 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5050 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5051 /*fraction_lost=*/0,
5052 /*rtt_ms=*/0);
5053
5054 // Send one frame to trigger ReconfigureEncoder.
5055 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5056 WaitForEncodedFrame(1);
5057
5058 using Config = EncoderSwitchRequestCallback::Config;
5059 EXPECT_CALL(switch_callback,
5060 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
5061 Field(&Config::param, "ping"),
5062 Field(&Config::value, "pong"))));
5063
5064 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5065 WaitForEncodedFrame(2);
5066
5067 video_stream_encoder_->Stop();
5068}
5069
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005070TEST_F(VideoStreamEncoderTest,
5071 AllocationPropegratedToEncoderWhenTargetRateChanged) {
5072 const int kFrameWidth = 320;
5073 const int kFrameHeight = 180;
5074
5075 // Set initial rate.
5076 auto rate = DataRate::kbps(100);
5077 video_stream_encoder_->OnBitrateUpdated(
5078 /*target_bitrate=*/rate,
5079 /*stable_target_bitrate=*/rate,
5080 /*link_allocation=*/rate,
5081 /*fraction_lost=*/0,
5082 /*rtt_ms=*/0);
5083
5084 // Insert a first video frame so that encoder gets configured.
5085 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5086 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5087 frame.set_rotation(kVideoRotation_270);
5088 video_source_.IncomingCapturedFrame(frame);
5089 WaitForEncodedFrame(timestamp_ms);
5090 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5091
5092 // Change of target bitrate propagates to the encoder.
5093 auto new_stable_rate = rate - DataRate::kbps(5);
5094 video_stream_encoder_->OnBitrateUpdated(
5095 /*target_bitrate=*/new_stable_rate,
5096 /*stable_target_bitrate=*/new_stable_rate,
5097 /*link_allocation=*/rate,
5098 /*fraction_lost=*/0,
5099 /*rtt_ms=*/0);
5100 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5101 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5102 video_stream_encoder_->Stop();
5103}
5104
5105TEST_F(VideoStreamEncoderTest,
5106 AllocationNotPropegratedToEncoderWhenTargetRateUnchanged) {
5107 const int kFrameWidth = 320;
5108 const int kFrameHeight = 180;
5109
5110 // Set initial rate.
5111 auto rate = DataRate::kbps(100);
5112 video_stream_encoder_->OnBitrateUpdated(
5113 /*target_bitrate=*/rate,
5114 /*stable_target_bitrate=*/rate,
5115 /*link_allocation=*/rate,
5116 /*fraction_lost=*/0,
5117 /*rtt_ms=*/0);
5118
5119 // Insert a first video frame so that encoder gets configured.
5120 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5121 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5122 frame.set_rotation(kVideoRotation_270);
5123 video_source_.IncomingCapturedFrame(frame);
5124 WaitForEncodedFrame(timestamp_ms);
5125 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5126
5127 // Set a higher target rate without changing the link_allocation. Should not
5128 // reset encoder's rate.
5129 auto new_stable_rate = rate - DataRate::kbps(5);
5130 video_stream_encoder_->OnBitrateUpdated(
5131 /*target_bitrate=*/rate,
5132 /*stable_target_bitrate=*/new_stable_rate,
5133 /*link_allocation=*/rate,
5134 /*fraction_lost=*/0,
5135 /*rtt_ms=*/0);
5136 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5137 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5138 video_stream_encoder_->Stop();
5139}
5140
perkj26091b12016-09-01 01:17:40 -07005141} // namespace webrtc