blob: a307e4a3eed6c3f91a61e2b776024bcd4ebd0643 [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"
philipel9b058032020-02-10 11:30:00 +010021#include "api/test/mock_video_encoder.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080022#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video/i420_buffer.h"
Erik Språngf93eda12019-01-16 17:10:57 +010024#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020025#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020026#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010027#include "api/video_codecs/vp8_temporal_layers_factory.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020028#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070029#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020031#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010032#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020037#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "system_wrappers/include/sleep.h"
39#include "test/encoder_settings.h"
40#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020041#include "test/field_trial.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010042#include "test/frame_forwarder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/gmock.h"
44#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020045#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070047
48namespace webrtc {
49
sprangb1ca0732017-02-01 08:38:12 -080050using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080051using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020052using ::testing::AllOf;
53using ::testing::Field;
philipel9b058032020-02-10 11:30:00 +010054using ::testing::Matcher;
55using ::testing::NiceMock;
56using ::testing::Return;
philipeld9cc8c02019-09-16 14:53:40 +020057using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080058
perkj803d97f2016-11-01 11:45:46 -070059namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020060const int kMinPixelsPerFrame = 320 * 180;
Åsa Perssone644a032019-11-08 15:56:00 +010061const int kQpLow = 1;
62const int kQpHigh = 2;
Åsa Persson8c1bf952018-09-13 10:42:19 +020063const int kMinFramerateFps = 2;
64const int kMinBalancedFramerateFps = 7;
65const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080066const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010067const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020068const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010069const uint32_t kSimulcastTargetBitrateBps = 3150000;
70const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080071const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070072const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020073const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020074const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020075const VideoEncoder::ResolutionBitrateLimits
76 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
77const VideoEncoder::ResolutionBitrateLimits
78 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080079
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020080uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
81 0x00, 0x00, 0x03, 0x03, 0xF4,
82 0x05, 0x03, 0xC7, 0xE0, 0x1B,
83 0x41, 0x10, 0x8D, 0x00};
84
perkj803d97f2016-11-01 11:45:46 -070085class TestBuffer : public webrtc::I420Buffer {
86 public:
87 TestBuffer(rtc::Event* event, int width, int height)
88 : I420Buffer(width, height), event_(event) {}
89
90 private:
91 friend class rtc::RefCountedObject<TestBuffer>;
92 ~TestBuffer() override {
93 if (event_)
94 event_->Set();
95 }
96 rtc::Event* const event_;
97};
98
Noah Richards51db4212019-06-12 06:59:12 -070099// A fake native buffer that can't be converted to I420.
100class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
101 public:
102 FakeNativeBuffer(rtc::Event* event, int width, int height)
103 : event_(event), width_(width), height_(height) {}
104 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
105 int width() const override { return width_; }
106 int height() const override { return height_; }
107 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
108 return nullptr;
109 }
110
111 private:
112 friend class rtc::RefCountedObject<FakeNativeBuffer>;
113 ~FakeNativeBuffer() override {
114 if (event_)
115 event_->Set();
116 }
117 rtc::Event* const event_;
118 const int width_;
119 const int height_;
120};
121
Niels Möller7dc26b72017-12-06 10:27:48 +0100122class CpuOveruseDetectorProxy : public OveruseFrameDetector {
123 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200124 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
125 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100126 last_target_framerate_fps_(-1) {}
127 virtual ~CpuOveruseDetectorProxy() {}
128
129 void OnTargetFramerateUpdated(int framerate_fps) override {
130 rtc::CritScope cs(&lock_);
131 last_target_framerate_fps_ = framerate_fps;
132 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
133 }
134
135 int GetLastTargetFramerate() {
136 rtc::CritScope cs(&lock_);
137 return last_target_framerate_fps_;
138 }
139
Niels Möller4db138e2018-04-19 09:04:13 +0200140 CpuOveruseOptions GetOptions() { return options_; }
141
Niels Möller7dc26b72017-12-06 10:27:48 +0100142 private:
143 rtc::CriticalSection lock_;
144 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
145};
146
mflodmancc3d4422017-08-03 08:27:51 -0700147class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700148 public:
Niels Möller213618e2018-07-24 09:29:58 +0200149 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200150 const VideoStreamEncoderSettings& settings,
151 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100152 : VideoStreamEncoder(Clock::GetRealTimeClock(),
153 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200154 stats_proxy,
155 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200156 std::unique_ptr<OveruseFrameDetector>(
157 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100158 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200159 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700160
Henrik Boströmb08882b2020-01-07 10:11:17 +0100161 void PostTaskAndWait(bool down,
162 AdaptationObserverInterface::AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200163 PostTaskAndWait(down, reason, /*expected_results=*/true);
164 }
165
Henrik Boströmb08882b2020-01-07 10:11:17 +0100166 void PostTaskAndWait(bool down,
167 AdaptationObserverInterface::AdaptReason reason,
168 bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100169 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200170 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200171 if (down)
Henrik Boström7875c992020-02-06 10:35:00 +0100172 EXPECT_EQ(expected_results, OnResourceOveruseForTesting(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200173 else
Henrik Boström7875c992020-02-06 10:35:00 +0100174 OnResourceUnderuseForTesting(reason);
perkj803d97f2016-11-01 11:45:46 -0700175 event.Set();
176 });
perkj070ba852017-02-16 15:46:27 -0800177 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700178 }
179
kthelgason2fc52542017-03-03 00:24:41 -0800180 // This is used as a synchronisation mechanism, to make sure that the
181 // encoder queue is not blocked before we start sending it frames.
182 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100183 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200184 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800185 ASSERT_TRUE(event.Wait(5000));
186 }
187
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200188 void TriggerCpuOveruse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100189 PostTaskAndWait(/*down=*/true,
190 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200191 }
kthelgason876222f2016-11-29 01:44:11 -0800192
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200193 void TriggerCpuNormalUsage() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100194 PostTaskAndWait(/*down=*/false,
195 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200196 }
kthelgason876222f2016-11-29 01:44:11 -0800197
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200198 void TriggerQualityLow() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100199 PostTaskAndWait(/*down=*/true,
200 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200201 }
kthelgason876222f2016-11-29 01:44:11 -0800202
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200203 void TriggerQualityLowExpectFalse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100204 PostTaskAndWait(/*down=*/true,
205 AdaptationObserverInterface::AdaptReason::kQuality,
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200206 /*expected_results=*/false);
207 }
208
209 void TriggerQualityHigh() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100210 PostTaskAndWait(/*down=*/false,
211 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200212 }
sprangfda496a2017-06-15 04:21:07 -0700213
Niels Möller7dc26b72017-12-06 10:27:48 +0100214 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700215};
216
asapersson5f7226f2016-11-25 04:37:00 -0800217class VideoStreamFactory
218 : public VideoEncoderConfig::VideoStreamFactoryInterface {
219 public:
sprangfda496a2017-06-15 04:21:07 -0700220 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
221 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800222 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700223 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800224 }
225
226 private:
227 std::vector<VideoStream> CreateEncoderStreams(
228 int width,
229 int height,
230 const VideoEncoderConfig& encoder_config) override {
231 std::vector<VideoStream> streams =
232 test::CreateVideoStreams(width, height, encoder_config);
233 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100234 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700235 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800236 }
237 return streams;
238 }
sprangfda496a2017-06-15 04:21:07 -0700239
asapersson5f7226f2016-11-25 04:37:00 -0800240 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700241 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800242};
243
Noah Richards51db4212019-06-12 06:59:12 -0700244// Simulates simulcast behavior and makes highest stream resolutions divisible
245// by 4.
246class CroppingVideoStreamFactory
247 : public VideoEncoderConfig::VideoStreamFactoryInterface {
248 public:
249 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
250 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
251 EXPECT_GT(num_temporal_layers, 0u);
252 EXPECT_GT(framerate, 0);
253 }
254
255 private:
256 std::vector<VideoStream> CreateEncoderStreams(
257 int width,
258 int height,
259 const VideoEncoderConfig& encoder_config) override {
260 std::vector<VideoStream> streams = test::CreateVideoStreams(
261 width - width % 4, height - height % 4, encoder_config);
262 for (VideoStream& stream : streams) {
263 stream.num_temporal_layers = num_temporal_layers_;
264 stream.max_framerate = framerate_;
265 }
266 return streams;
267 }
268
269 const size_t num_temporal_layers_;
270 const int framerate_;
271};
272
sprangb1ca0732017-02-01 08:38:12 -0800273class AdaptingFrameForwarder : public test::FrameForwarder {
274 public:
275 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700276 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800277
278 void set_adaptation_enabled(bool enabled) {
279 rtc::CritScope cs(&crit_);
280 adaptation_enabled_ = enabled;
281 }
282
asaperssonfab67072017-04-04 05:51:49 -0700283 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800284 rtc::CritScope cs(&crit_);
285 return adaptation_enabled_;
286 }
287
asapersson09f05612017-05-15 23:40:18 -0700288 rtc::VideoSinkWants last_wants() const {
289 rtc::CritScope cs(&crit_);
290 return last_wants_;
291 }
292
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200293 absl::optional<int> last_sent_width() const { return last_width_; }
294 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800295
sprangb1ca0732017-02-01 08:38:12 -0800296 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
297 int cropped_width = 0;
298 int cropped_height = 0;
299 int out_width = 0;
300 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700301 if (adaption_enabled()) {
302 if (adapter_.AdaptFrameResolution(
303 video_frame.width(), video_frame.height(),
304 video_frame.timestamp_us() * 1000, &cropped_width,
305 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100306 VideoFrame adapted_frame =
307 VideoFrame::Builder()
308 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
309 nullptr, out_width, out_height))
310 .set_timestamp_rtp(99)
311 .set_timestamp_ms(99)
312 .set_rotation(kVideoRotation_0)
313 .build();
sprangc5d62e22017-04-02 23:53:04 -0700314 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100315 if (video_frame.has_update_rect()) {
316 adapted_frame.set_update_rect(
317 video_frame.update_rect().ScaleWithFrame(
318 video_frame.width(), video_frame.height(), 0, 0,
319 video_frame.width(), video_frame.height(), out_width,
320 out_height));
321 }
sprangc5d62e22017-04-02 23:53:04 -0700322 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800323 last_width_.emplace(adapted_frame.width());
324 last_height_.emplace(adapted_frame.height());
325 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200326 last_width_ = absl::nullopt;
327 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700328 }
sprangb1ca0732017-02-01 08:38:12 -0800329 } else {
330 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800331 last_width_.emplace(video_frame.width());
332 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800333 }
334 }
335
336 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
337 const rtc::VideoSinkWants& wants) override {
338 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700339 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100340 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800341 test::FrameForwarder::AddOrUpdateSink(sink, wants);
342 }
sprangb1ca0732017-02-01 08:38:12 -0800343 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700344 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
345 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200346 absl::optional<int> last_width_;
347 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800348};
sprangc5d62e22017-04-02 23:53:04 -0700349
Niels Möller213618e2018-07-24 09:29:58 +0200350// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700351class MockableSendStatisticsProxy : public SendStatisticsProxy {
352 public:
353 MockableSendStatisticsProxy(Clock* clock,
354 const VideoSendStream::Config& config,
355 VideoEncoderConfig::ContentType content_type)
356 : SendStatisticsProxy(clock, config, content_type) {}
357
358 VideoSendStream::Stats GetStats() override {
359 rtc::CritScope cs(&lock_);
360 if (mock_stats_)
361 return *mock_stats_;
362 return SendStatisticsProxy::GetStats();
363 }
364
Niels Möller213618e2018-07-24 09:29:58 +0200365 int GetInputFrameRate() const override {
366 rtc::CritScope cs(&lock_);
367 if (mock_stats_)
368 return mock_stats_->input_frame_rate;
369 return SendStatisticsProxy::GetInputFrameRate();
370 }
sprangc5d62e22017-04-02 23:53:04 -0700371 void SetMockStats(const VideoSendStream::Stats& stats) {
372 rtc::CritScope cs(&lock_);
373 mock_stats_.emplace(stats);
374 }
375
376 void ResetMockStats() {
377 rtc::CritScope cs(&lock_);
378 mock_stats_.reset();
379 }
380
381 private:
382 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200383 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700384};
385
sprang4847ae62017-06-27 07:06:52 -0700386class MockBitrateObserver : public VideoBitrateAllocationObserver {
387 public:
Erik Språng566124a2018-04-23 12:32:22 +0200388 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700389};
390
philipel9b058032020-02-10 11:30:00 +0100391class MockEncoderSelector
392 : public VideoEncoderFactory::EncoderSelectorInterface {
393 public:
394 MOCK_METHOD1(OnCurrentEncoder, void(const SdpVideoFormat& format));
395 MOCK_METHOD1(OnEncodingBitrate,
396 absl::optional<SdpVideoFormat>(const DataRate& rate));
397 MOCK_METHOD0(OnEncoderBroken, absl::optional<SdpVideoFormat>());
398};
399
perkj803d97f2016-11-01 11:45:46 -0700400} // namespace
401
mflodmancc3d4422017-08-03 08:27:51 -0700402class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700403 public:
404 static const int kDefaultTimeoutMs = 30 * 1000;
405
mflodmancc3d4422017-08-03 08:27:51 -0700406 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700407 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700408 codec_width_(320),
409 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200410 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200411 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700412 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200413 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700414 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700415 Clock::GetRealTimeClock(),
416 video_send_config_,
417 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700418 sink_(&fake_encoder_) {}
419
420 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700421 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700422 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200423 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800424 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200425 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200426 video_send_config_.rtp.payload_name = "FAKE";
427 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700428
Per512ecb32016-09-23 15:52:06 +0200429 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200430 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700431 video_encoder_config.video_stream_factory =
432 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100433 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700434
435 // Framerate limit is specified by the VideoStreamFactory.
436 std::vector<VideoStream> streams =
437 video_encoder_config.video_stream_factory->CreateEncoderStreams(
438 codec_width_, codec_height_, video_encoder_config);
439 max_framerate_ = streams[0].max_framerate;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100440 fake_clock_.SetTime(Timestamp::Micros(1234));
sprang4847ae62017-06-27 07:06:52 -0700441
Niels Möllerf1338562018-04-26 09:51:47 +0200442 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800443 }
444
Niels Möllerf1338562018-04-26 09:51:47 +0200445 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700446 if (video_stream_encoder_)
447 video_stream_encoder_->Stop();
448 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200449 stats_proxy_.get(), video_send_config_.encoder_settings,
450 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700451 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
452 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700453 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700454 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
455 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200456 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700457 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800458 }
459
460 void ResetEncoder(const std::string& payload_name,
461 size_t num_streams,
462 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700463 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700464 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200465 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800466
467 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200468 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800469 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100470 video_encoder_config.max_bitrate_bps =
471 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800472 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700473 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
474 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700475 video_encoder_config.content_type =
476 screenshare ? VideoEncoderConfig::ContentType::kScreen
477 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700478 if (payload_name == "VP9") {
479 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
480 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
481 video_encoder_config.encoder_specific_settings =
482 new rtc::RefCountedObject<
483 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
484 }
Niels Möllerf1338562018-04-26 09:51:47 +0200485 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700486 }
487
sprang57c2fff2017-01-16 06:24:02 -0800488 VideoFrame CreateFrame(int64_t ntp_time_ms,
489 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100490 VideoFrame frame =
491 VideoFrame::Builder()
492 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
493 destruction_event, codec_width_, codec_height_))
494 .set_timestamp_rtp(99)
495 .set_timestamp_ms(99)
496 .set_rotation(kVideoRotation_0)
497 .build();
sprang57c2fff2017-01-16 06:24:02 -0800498 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700499 return frame;
500 }
501
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100502 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
503 rtc::Event* destruction_event,
504 int offset_x) const {
505 VideoFrame frame =
506 VideoFrame::Builder()
507 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
508 destruction_event, codec_width_, codec_height_))
509 .set_timestamp_rtp(99)
510 .set_timestamp_ms(99)
511 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100512 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100513 .build();
514 frame.set_ntp_time_ms(ntp_time_ms);
515 return frame;
516 }
517
sprang57c2fff2017-01-16 06:24:02 -0800518 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100519 VideoFrame frame =
520 VideoFrame::Builder()
521 .set_video_frame_buffer(
522 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
523 .set_timestamp_rtp(99)
524 .set_timestamp_ms(99)
525 .set_rotation(kVideoRotation_0)
526 .build();
sprang57c2fff2017-01-16 06:24:02 -0800527 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700528 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700529 return frame;
530 }
531
Noah Richards51db4212019-06-12 06:59:12 -0700532 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
533 rtc::Event* destruction_event,
534 int width,
535 int height) const {
536 VideoFrame frame =
537 VideoFrame::Builder()
538 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
539 destruction_event, width, height))
540 .set_timestamp_rtp(99)
541 .set_timestamp_ms(99)
542 .set_rotation(kVideoRotation_0)
543 .build();
544 frame.set_ntp_time_ms(ntp_time_ms);
545 return frame;
546 }
547
548 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
549 rtc::Event* destruction_event) const {
550 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
551 codec_height_);
552 }
553
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100554 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
555 MockBitrateObserver bitrate_observer;
556 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
557
558 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
559 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200560 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100561 DataRate::BitsPerSec(kTargetBitrateBps),
562 DataRate::BitsPerSec(kTargetBitrateBps),
563 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100564
565 video_source_.IncomingCapturedFrame(
566 CreateFrame(1, codec_width_, codec_height_));
567 WaitForEncodedFrame(1);
568 }
569
asapersson02465b82017-04-10 01:12:52 -0700570 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700571 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700572 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
573 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700574 }
575
asapersson09f05612017-05-15 23:40:18 -0700576 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
577 const rtc::VideoSinkWants& wants2) {
578 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
579 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
580 }
581
Åsa Persson8c1bf952018-09-13 10:42:19 +0200582 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
583 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
584 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
585 EXPECT_FALSE(wants.target_pixel_count);
586 }
587
asapersson09f05612017-05-15 23:40:18 -0700588 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
589 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200590 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700591 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
592 EXPECT_GT(wants1.max_pixel_count, 0);
593 }
594
595 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
596 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200597 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700598 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
599 }
600
asaperssonf7e294d2017-06-13 23:25:22 -0700601 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
602 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200603 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700604 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
605 }
606
607 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
608 const rtc::VideoSinkWants& wants2) {
609 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
610 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
611 }
612
613 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
614 const rtc::VideoSinkWants& wants2) {
615 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
616 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
617 }
618
619 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
620 const rtc::VideoSinkWants& wants2) {
621 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
622 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
623 EXPECT_GT(wants1.max_pixel_count, 0);
624 }
625
626 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
627 const rtc::VideoSinkWants& wants2) {
628 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
629 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
630 }
631
asapersson09f05612017-05-15 23:40:18 -0700632 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
633 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200634 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700635 EXPECT_LT(wants.max_pixel_count, pixel_count);
636 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700637 }
638
639 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
640 EXPECT_LT(wants.max_framerate_fps, fps);
641 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
642 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700643 }
644
asaperssonf7e294d2017-06-13 23:25:22 -0700645 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
646 int expected_fps) {
647 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
648 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
649 EXPECT_FALSE(wants.target_pixel_count);
650 }
651
Jonathan Yubc771b72017-12-08 17:04:29 -0800652 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
653 int last_frame_pixels) {
654 // Balanced mode should always scale FPS to the desired range before
655 // attempting to scale resolution.
656 int fps_limit = wants.max_framerate_fps;
657 if (last_frame_pixels <= 320 * 240) {
658 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
659 } else if (last_frame_pixels <= 480 * 270) {
660 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
661 } else if (last_frame_pixels <= 640 * 480) {
662 EXPECT_LE(15, fps_limit);
663 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200664 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800665 }
666 }
667
sprang4847ae62017-06-27 07:06:52 -0700668 void WaitForEncodedFrame(int64_t expected_ntp_time) {
669 sink_.WaitForEncodedFrame(expected_ntp_time);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100670 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700671 }
672
673 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
674 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100675 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700676 return ok;
677 }
678
679 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
680 sink_.WaitForEncodedFrame(expected_width, expected_height);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100681 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700682 }
683
684 void ExpectDroppedFrame() {
685 sink_.ExpectDroppedFrame();
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100686 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700687 }
688
689 bool WaitForFrame(int64_t timeout_ms) {
690 bool ok = sink_.WaitForFrame(timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100691 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700692 return ok;
693 }
694
perkj26091b12016-09-01 01:17:40 -0700695 class TestEncoder : public test::FakeEncoder {
696 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100697 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700698
asaperssonfab67072017-04-04 05:51:49 -0700699 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800700 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700701 return config_;
702 }
703
704 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800705 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700706 block_next_encode_ = true;
707 }
708
Erik Språngaed30702018-11-05 12:57:17 +0100709 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800710 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100711 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100712 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100713 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100714 info.scaling_settings = VideoEncoder::ScalingSettings(
715 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100716 }
717 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100718 for (int i = 0; i < kMaxSpatialLayers; ++i) {
719 if (temporal_layers_supported_[i]) {
720 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
721 info.fps_allocation[i].resize(num_layers);
722 }
723 }
Erik Språngaed30702018-11-05 12:57:17 +0100724 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200725
726 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100727 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100728 return info;
kthelgason876222f2016-11-29 01:44:11 -0800729 }
730
Erik Språngb7cb7b52019-02-26 15:52:33 +0100731 int32_t RegisterEncodeCompleteCallback(
732 EncodedImageCallback* callback) override {
733 rtc::CritScope lock(&local_crit_sect_);
734 encoded_image_callback_ = callback;
735 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
736 }
737
perkjfa10b552016-10-02 23:45:26 -0700738 void ContinueEncode() { continue_encode_event_.Set(); }
739
740 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
741 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800742 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700743 EXPECT_EQ(timestamp_, timestamp);
744 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
745 }
746
kthelgason2fc52542017-03-03 00:24:41 -0800747 void SetQualityScaling(bool b) {
748 rtc::CritScope lock(&local_crit_sect_);
749 quality_scaling_ = b;
750 }
kthelgasonad9010c2017-02-14 00:46:51 -0800751
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100752 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
753 rtc::CritScope lock(&local_crit_sect_);
754 requested_resolution_alignment_ = requested_resolution_alignment;
755 }
756
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100757 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
758 rtc::CritScope lock(&local_crit_sect_);
759 is_hardware_accelerated_ = is_hardware_accelerated;
760 }
761
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100762 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
763 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
764 rtc::CritScope lock(&local_crit_sect_);
765 temporal_layers_supported_[spatial_idx] = supported;
766 }
767
Sergey Silkin6456e352019-07-08 17:56:40 +0200768 void SetResolutionBitrateLimits(
769 std::vector<ResolutionBitrateLimits> thresholds) {
770 rtc::CritScope cs(&local_crit_sect_);
771 resolution_bitrate_limits_ = thresholds;
772 }
773
sprangfe627f32017-03-29 08:24:59 -0700774 void ForceInitEncodeFailure(bool force_failure) {
775 rtc::CritScope lock(&local_crit_sect_);
776 force_init_encode_failed_ = force_failure;
777 }
778
Niels Möller6bb5ab92019-01-11 11:11:10 +0100779 void SimulateOvershoot(double rate_factor) {
780 rtc::CritScope lock(&local_crit_sect_);
781 rate_factor_ = rate_factor;
782 }
783
Erik Språngd7329ca2019-02-21 21:19:53 +0100784 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100785 rtc::CritScope lock(&local_crit_sect_);
786 return last_framerate_;
787 }
788
Erik Språngd7329ca2019-02-21 21:19:53 +0100789 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100790 rtc::CritScope lock(&local_crit_sect_);
791 return last_update_rect_;
792 }
793
Niels Möller87e2d782019-03-07 10:18:23 +0100794 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100795 rtc::CritScope lock(&local_crit_sect_);
796 return last_frame_types_;
797 }
798
799 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100800 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100801 keyframe ? VideoFrameType::kVideoFrameKey
802 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100803 {
804 rtc::CritScope lock(&local_crit_sect_);
805 last_frame_types_ = frame_type;
806 }
Niels Möllerb859b322019-03-07 12:40:01 +0100807 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100808 }
809
Erik Språngb7cb7b52019-02-26 15:52:33 +0100810 void InjectEncodedImage(const EncodedImage& image) {
811 rtc::CritScope lock(&local_crit_sect_);
812 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
813 }
814
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200815 void InjectEncodedImage(const EncodedImage& image,
816 const CodecSpecificInfo* codec_specific_info,
817 const RTPFragmentationHeader* fragmentation) {
818 rtc::CritScope lock(&local_crit_sect_);
819 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
820 fragmentation);
821 }
822
Erik Språngd7329ca2019-02-21 21:19:53 +0100823 void ExpectNullFrame() {
824 rtc::CritScope lock(&local_crit_sect_);
825 expect_null_frame_ = true;
826 }
827
Erik Språng5056af02019-09-02 15:53:11 +0200828 absl::optional<VideoEncoder::RateControlParameters>
829 GetAndResetLastRateControlSettings() {
830 auto settings = last_rate_control_settings_;
831 last_rate_control_settings_.reset();
832 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100833 }
834
Sergey Silkin5ee69672019-07-02 14:18:34 +0200835 int GetNumEncoderInitializations() const {
836 rtc::CritScope lock(&local_crit_sect_);
837 return num_encoder_initializations_;
838 }
839
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200840 int GetNumSetRates() const {
841 rtc::CritScope lock(&local_crit_sect_);
842 return num_set_rates_;
843 }
844
perkjfa10b552016-10-02 23:45:26 -0700845 private:
perkj26091b12016-09-01 01:17:40 -0700846 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100847 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700848 bool block_encode;
849 {
brandtre78d2662017-01-16 05:57:16 -0800850 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100851 if (expect_null_frame_) {
852 EXPECT_EQ(input_image.timestamp(), 0u);
853 EXPECT_EQ(input_image.width(), 1);
854 last_frame_types_ = *frame_types;
855 expect_null_frame_ = false;
856 } else {
857 EXPECT_GT(input_image.timestamp(), timestamp_);
858 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
859 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
860 }
perkj26091b12016-09-01 01:17:40 -0700861
862 timestamp_ = input_image.timestamp();
863 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700864 last_input_width_ = input_image.width();
865 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700866 block_encode = block_next_encode_;
867 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100868 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100869 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700870 }
Niels Möllerb859b322019-03-07 12:40:01 +0100871 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700872 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700873 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700874 return result;
875 }
876
sprangfe627f32017-03-29 08:24:59 -0700877 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200878 const Settings& settings) override {
879 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200880
sprangfe627f32017-03-29 08:24:59 -0700881 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100882 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200883
884 ++num_encoder_initializations_;
885
Erik Språng82fad3d2018-03-21 09:57:23 +0100886 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700887 // Simulate setting up temporal layers, in order to validate the life
888 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100889 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200890 frame_buffer_controller_ =
891 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700892 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100893 if (force_init_encode_failed_) {
894 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700895 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100896 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100897
Erik Språngb7cb7b52019-02-26 15:52:33 +0100898 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700899 return res;
900 }
901
Erik Språngb7cb7b52019-02-26 15:52:33 +0100902 int32_t Release() override {
903 rtc::CritScope lock(&local_crit_sect_);
904 EXPECT_NE(initialized_, EncoderState::kUninitialized);
905 initialized_ = EncoderState::kUninitialized;
906 return FakeEncoder::Release();
907 }
908
Erik Språng16cb8f52019-04-12 13:59:09 +0200909 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100910 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200911 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100912 VideoBitrateAllocation adjusted_rate_allocation;
913 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
914 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200915 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100916 adjusted_rate_allocation.SetBitrate(
917 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200918 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100919 rate_factor_));
920 }
921 }
922 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200923 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200924 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200925 RateControlParameters adjusted_paramters = parameters;
926 adjusted_paramters.bitrate = adjusted_rate_allocation;
927 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100928 }
929
brandtre78d2662017-01-16 05:57:16 -0800930 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100931 enum class EncoderState {
932 kUninitialized,
933 kInitializationFailed,
934 kInitialized
935 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
936 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700937 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700938 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700939 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
940 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
941 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
942 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
943 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100944 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100945 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100946 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700947 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100948 absl::optional<bool>
949 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
950 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700951 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100952 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
953 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200954 absl::optional<VideoEncoder::RateControlParameters>
955 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100956 VideoFrame::UpdateRect last_update_rect_
957 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100958 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100959 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100960 EncodedImageCallback* encoded_image_callback_
961 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200962 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200963 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200964 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
965 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200966 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700967 };
968
mflodmancc3d4422017-08-03 08:27:51 -0700969 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700970 public:
971 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100972 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700973
perkj26091b12016-09-01 01:17:40 -0700974 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700975 EXPECT_TRUE(
976 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
977 }
978
979 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
980 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700981 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700982 if (!encoded_frame_event_.Wait(timeout_ms))
983 return false;
perkj26091b12016-09-01 01:17:40 -0700984 {
985 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800986 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700987 }
988 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700989 return true;
perkj26091b12016-09-01 01:17:40 -0700990 }
991
sprangb1ca0732017-02-01 08:38:12 -0800992 void WaitForEncodedFrame(uint32_t expected_width,
993 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700994 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100995 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700996 }
997
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100998 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700999 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -08001000 uint32_t width = 0;
1001 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -08001002 {
1003 rtc::CritScope lock(&crit_);
1004 width = last_width_;
1005 height = last_height_;
1006 }
1007 EXPECT_EQ(expected_height, height);
1008 EXPECT_EQ(expected_width, width);
1009 }
1010
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001011 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
1012 int width = 0;
1013 int height = 0;
1014 {
1015 rtc::CritScope lock(&crit_);
1016 width = last_width_;
1017 height = last_height_;
1018 }
1019 EXPECT_EQ(width % resolution_alignment, 0);
1020 EXPECT_EQ(height % resolution_alignment, 0);
1021 }
1022
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001023 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1024 VideoRotation rotation;
1025 {
1026 rtc::CritScope lock(&crit_);
1027 rotation = last_rotation_;
1028 }
1029 EXPECT_EQ(expected_rotation, rotation);
1030 }
1031
kthelgason2fc52542017-03-03 00:24:41 -08001032 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001033
sprangc5d62e22017-04-02 23:53:04 -07001034 bool WaitForFrame(int64_t timeout_ms) {
1035 return encoded_frame_event_.Wait(timeout_ms);
1036 }
1037
perkj26091b12016-09-01 01:17:40 -07001038 void SetExpectNoFrames() {
1039 rtc::CritScope lock(&crit_);
1040 expect_frames_ = false;
1041 }
1042
asaperssonfab67072017-04-04 05:51:49 -07001043 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001044 rtc::CritScope lock(&crit_);
1045 return number_of_reconfigurations_;
1046 }
1047
asaperssonfab67072017-04-04 05:51:49 -07001048 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001049 rtc::CritScope lock(&crit_);
1050 return min_transmit_bitrate_bps_;
1051 }
1052
Erik Språngd7329ca2019-02-21 21:19:53 +01001053 void SetNumExpectedLayers(size_t num_layers) {
1054 rtc::CritScope lock(&crit_);
1055 num_expected_layers_ = num_layers;
1056 }
1057
Erik Språngb7cb7b52019-02-26 15:52:33 +01001058 int64_t GetLastCaptureTimeMs() const {
1059 rtc::CritScope lock(&crit_);
1060 return last_capture_time_ms_;
1061 }
1062
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001063 std::vector<uint8_t> GetLastEncodedImageData() {
1064 rtc::CritScope lock(&crit_);
1065 return std::move(last_encoded_image_data_);
1066 }
1067
1068 RTPFragmentationHeader GetLastFragmentation() {
1069 rtc::CritScope lock(&crit_);
1070 return std::move(last_fragmentation_);
1071 }
1072
perkj26091b12016-09-01 01:17:40 -07001073 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001074 Result OnEncodedImage(
1075 const EncodedImage& encoded_image,
1076 const CodecSpecificInfo* codec_specific_info,
1077 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001078 rtc::CritScope lock(&crit_);
1079 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001080 last_encoded_image_data_ = std::vector<uint8_t>(
1081 encoded_image.data(), encoded_image.data() + encoded_image.size());
1082 if (fragmentation) {
1083 last_fragmentation_.CopyFrom(*fragmentation);
1084 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001085 uint32_t timestamp = encoded_image.Timestamp();
1086 if (last_timestamp_ != timestamp) {
1087 num_received_layers_ = 1;
1088 } else {
1089 ++num_received_layers_;
1090 }
1091 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001092 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001093 last_width_ = encoded_image._encodedWidth;
1094 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001095 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001096 if (num_received_layers_ == num_expected_layers_) {
1097 encoded_frame_event_.Set();
1098 }
sprangb1ca0732017-02-01 08:38:12 -08001099 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001100 }
1101
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001102 void OnEncoderConfigurationChanged(
1103 std::vector<VideoStream> streams,
1104 VideoEncoderConfig::ContentType content_type,
1105 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001106 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001107 ++number_of_reconfigurations_;
1108 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1109 }
1110
perkj26091b12016-09-01 01:17:40 -07001111 rtc::CriticalSection crit_;
1112 TestEncoder* test_encoder_;
1113 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001114 std::vector<uint8_t> last_encoded_image_data_;
1115 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001116 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001117 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001118 uint32_t last_height_ = 0;
1119 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001120 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001121 size_t num_expected_layers_ = 1;
1122 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001123 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001124 int number_of_reconfigurations_ = 0;
1125 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001126 };
1127
Sergey Silkin5ee69672019-07-02 14:18:34 +02001128 class VideoBitrateAllocatorProxyFactory
1129 : public VideoBitrateAllocatorFactory {
1130 public:
1131 VideoBitrateAllocatorProxyFactory()
1132 : bitrate_allocator_factory_(
1133 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1134
1135 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1136 const VideoCodec& codec) override {
1137 rtc::CritScope lock(&crit_);
1138 codec_config_ = codec;
1139 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1140 }
1141
1142 VideoCodec codec_config() const {
1143 rtc::CritScope lock(&crit_);
1144 return codec_config_;
1145 }
1146
1147 private:
1148 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1149
1150 rtc::CriticalSection crit_;
1151 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1152 };
1153
perkj26091b12016-09-01 01:17:40 -07001154 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001155 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001156 int codec_width_;
1157 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001158 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001159 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001160 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001161 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001162 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001163 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001164 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001165 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001166 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001167 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001168};
1169
mflodmancc3d4422017-08-03 08:27:51 -07001170TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001171 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001172 DataRate::BitsPerSec(kTargetBitrateBps),
1173 DataRate::BitsPerSec(kTargetBitrateBps),
1174 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001175 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001176 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001177 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001178 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001179 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001180}
1181
mflodmancc3d4422017-08-03 08:27:51 -07001182TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001183 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001184 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001185 // The encoder will cache up to one frame for a short duration. Adding two
1186 // frames means that the first frame will be dropped and the second frame will
1187 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001188 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001189 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001190 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001191
Erik Språng4c6ca302019-04-08 15:14:01 +02001192 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001193 DataRate::BitsPerSec(kTargetBitrateBps),
1194 DataRate::BitsPerSec(kTargetBitrateBps),
1195 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001196
Sebastian Janssona3177052018-04-10 13:05:49 +02001197 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001198 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001199 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1200
1201 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001202 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001203}
1204
mflodmancc3d4422017-08-03 08:27:51 -07001205TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001206 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001207 DataRate::BitsPerSec(kTargetBitrateBps),
1208 DataRate::BitsPerSec(kTargetBitrateBps),
1209 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001210 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001211 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001212
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001213 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
1214 DataRate::BitsPerSec(0),
1215 DataRate::BitsPerSec(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001216 // The encoder will cache up to one frame for a short duration. Adding two
1217 // frames means that the first frame will be dropped and the second frame will
1218 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001219 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001220 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001221
Erik Språng4c6ca302019-04-08 15:14:01 +02001222 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001223 DataRate::BitsPerSec(kTargetBitrateBps),
1224 DataRate::BitsPerSec(kTargetBitrateBps),
1225 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001226 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001227 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1228 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001229 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001230}
1231
mflodmancc3d4422017-08-03 08:27:51 -07001232TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001233 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001234 DataRate::BitsPerSec(kTargetBitrateBps),
1235 DataRate::BitsPerSec(kTargetBitrateBps),
1236 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001237 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001238 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001239
1240 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001241 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001242
perkja49cbd32016-09-16 07:53:41 -07001243 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001244 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001245 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001246}
1247
mflodmancc3d4422017-08-03 08:27:51 -07001248TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001249 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001250 DataRate::BitsPerSec(kTargetBitrateBps),
1251 DataRate::BitsPerSec(kTargetBitrateBps),
1252 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001253
perkja49cbd32016-09-16 07:53:41 -07001254 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001255 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001256
mflodmancc3d4422017-08-03 08:27:51 -07001257 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001258 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001259 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001260 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1261 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001262}
1263
mflodmancc3d4422017-08-03 08:27:51 -07001264TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001265 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001266 DataRate::BitsPerSec(kTargetBitrateBps),
1267 DataRate::BitsPerSec(kTargetBitrateBps),
1268 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001269
1270 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001271 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001272 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001273 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1274 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001275 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1276 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001277 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001278 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001279
mflodmancc3d4422017-08-03 08:27:51 -07001280 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001281}
1282
Noah Richards51db4212019-06-12 06:59:12 -07001283TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1284 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001285 DataRate::BitsPerSec(kTargetBitrateBps),
1286 DataRate::BitsPerSec(kTargetBitrateBps),
1287 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001288
1289 rtc::Event frame_destroyed_event;
1290 video_source_.IncomingCapturedFrame(
1291 CreateFakeNativeFrame(1, &frame_destroyed_event));
1292 ExpectDroppedFrame();
1293 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1294 video_stream_encoder_->Stop();
1295}
1296
1297TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1298 // Use the cropping factory.
1299 video_encoder_config_.video_stream_factory =
1300 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1301 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1302 kMaxPayloadLength);
1303 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1304
1305 // Capture a frame at codec_width_/codec_height_.
1306 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001307 DataRate::BitsPerSec(kTargetBitrateBps),
1308 DataRate::BitsPerSec(kTargetBitrateBps),
1309 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001310 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1311 WaitForEncodedFrame(1);
1312 // The encoder will have been configured once.
1313 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1314 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1315 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1316
1317 // Now send in a fake frame that needs to be cropped as the width/height
1318 // aren't divisible by 4 (see CreateEncoderStreams above).
1319 rtc::Event frame_destroyed_event;
1320 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1321 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1322 ExpectDroppedFrame();
1323 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1324 video_stream_encoder_->Stop();
1325}
1326
Ying Wang9b881ab2020-02-07 14:29:32 +01001327TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1328 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001329 DataRate::BitsPerSec(kTargetBitrateBps),
1330 DataRate::BitsPerSec(kTargetBitrateBps),
1331 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ying Wang9b881ab2020-02-07 14:29:32 +01001332 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1333 WaitForEncodedFrame(1);
1334
1335 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001336 DataRate::BitsPerSec(kTargetBitrateBps),
1337 DataRate::BitsPerSec(kTargetBitrateBps),
1338 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0.5);
Ying Wang9b881ab2020-02-07 14:29:32 +01001339 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1340 // frames. Adding two frames means that the first frame will be dropped and
1341 // the second frame will be sent to the encoder.
1342 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1343 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1344 WaitForEncodedFrame(3);
1345 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1346 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1347 WaitForEncodedFrame(5);
1348 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1349 video_stream_encoder_->Stop();
1350}
1351
mflodmancc3d4422017-08-03 08:27:51 -07001352TEST_F(VideoStreamEncoderTest,
1353 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001354 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001355 DataRate::BitsPerSec(kTargetBitrateBps),
1356 DataRate::BitsPerSec(kTargetBitrateBps),
1357 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001358 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001359
1360 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001361 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001362 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001363 // The encoder will have been configured once when the first frame is
1364 // received.
1365 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001366
1367 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001368 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001369 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001370 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001371 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001372
1373 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001374 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001375 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001376 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001377 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001378
mflodmancc3d4422017-08-03 08:27:51 -07001379 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001380}
1381
mflodmancc3d4422017-08-03 08:27:51 -07001382TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001383 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001384 DataRate::BitsPerSec(kTargetBitrateBps),
1385 DataRate::BitsPerSec(kTargetBitrateBps),
1386 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001387
1388 // Capture a frame and wait for it to synchronize with the encoder thread.
1389 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001390 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001391 // The encoder will have been configured once.
1392 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001393 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1394 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1395
1396 codec_width_ *= 2;
1397 codec_height_ *= 2;
1398 // Capture a frame with a higher resolution and wait for it to synchronize
1399 // with the encoder thread.
1400 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001401 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001402 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1403 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001404 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001405
mflodmancc3d4422017-08-03 08:27:51 -07001406 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001407}
1408
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001409TEST_F(VideoStreamEncoderTest,
1410 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1411 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001412 DataRate::BitsPerSec(kTargetBitrateBps),
1413 DataRate::BitsPerSec(kTargetBitrateBps),
1414 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001415
1416 // Capture a frame and wait for it to synchronize with the encoder thread.
1417 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1418 WaitForEncodedFrame(1);
1419
1420 VideoEncoderConfig video_encoder_config;
1421 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1422 // Changing the max payload data length recreates encoder.
1423 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1424 kMaxPayloadLength / 2);
1425
1426 // Capture a frame and wait for it to synchronize with the encoder thread.
1427 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1428 WaitForEncodedFrame(2);
1429 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1430
1431 video_stream_encoder_->Stop();
1432}
1433
Sergey Silkin5ee69672019-07-02 14:18:34 +02001434TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1435 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001436 DataRate::BitsPerSec(kTargetBitrateBps),
1437 DataRate::BitsPerSec(kTargetBitrateBps),
1438 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001439
1440 VideoEncoderConfig video_encoder_config;
1441 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1442 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1443 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1444 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1445 kMaxPayloadLength);
1446
1447 // Capture a frame and wait for it to synchronize with the encoder thread.
1448 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1449 WaitForEncodedFrame(1);
1450 // The encoder will have been configured once when the first frame is
1451 // received.
1452 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1453 EXPECT_EQ(kTargetBitrateBps,
1454 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1455 EXPECT_EQ(kStartBitrateBps,
1456 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1457
Sergey Silkin6456e352019-07-08 17:56:40 +02001458 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1459 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001460 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1461 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1462 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1463 kMaxPayloadLength);
1464
1465 // Capture a frame and wait for it to synchronize with the encoder thread.
1466 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1467 WaitForEncodedFrame(2);
1468 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1469 // Bitrate limits have changed - rate allocator should be reconfigured,
1470 // encoder should not be reconfigured.
1471 EXPECT_EQ(kTargetBitrateBps * 2,
1472 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1473 EXPECT_EQ(kStartBitrateBps * 2,
1474 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1475 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1476
1477 video_stream_encoder_->Stop();
1478}
1479
Sergey Silkin6456e352019-07-08 17:56:40 +02001480TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001481 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001482 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001483 DataRate::BitsPerSec(kTargetBitrateBps),
1484 DataRate::BitsPerSec(kTargetBitrateBps),
1485 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001486
Sergey Silkincd02eba2020-01-20 14:48:40 +01001487 const uint32_t kMinEncBitrateKbps = 100;
1488 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001489 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001490 /*frame_size_pixels=*/codec_width_ * codec_height_,
1491 /*min_start_bitrate_bps=*/0,
1492 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1493 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001494 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1495
Sergey Silkincd02eba2020-01-20 14:48:40 +01001496 VideoEncoderConfig video_encoder_config;
1497 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1498 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1499 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1500 (kMinEncBitrateKbps + 1) * 1000;
1501 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1502 kMaxPayloadLength);
1503
1504 // When both encoder and app provide bitrate limits, the intersection of
1505 // provided sets should be used.
1506 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1507 WaitForEncodedFrame(1);
1508 EXPECT_EQ(kMaxEncBitrateKbps,
1509 bitrate_allocator_factory_.codec_config().maxBitrate);
1510 EXPECT_EQ(kMinEncBitrateKbps + 1,
1511 bitrate_allocator_factory_.codec_config().minBitrate);
1512
1513 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1514 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1515 (kMinEncBitrateKbps - 1) * 1000;
1516 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1517 kMaxPayloadLength);
1518 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001519 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001520 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001521 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001522 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001523 bitrate_allocator_factory_.codec_config().minBitrate);
1524
Sergey Silkincd02eba2020-01-20 14:48:40 +01001525 video_stream_encoder_->Stop();
1526}
1527
1528TEST_F(VideoStreamEncoderTest,
1529 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1530 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001531 DataRate::BitsPerSec(kTargetBitrateBps),
1532 DataRate::BitsPerSec(kTargetBitrateBps),
1533 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001534
1535 const uint32_t kMinAppBitrateKbps = 100;
1536 const uint32_t kMaxAppBitrateKbps = 200;
1537 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1538 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1539 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1540 /*frame_size_pixels=*/codec_width_ * codec_height_,
1541 /*min_start_bitrate_bps=*/0,
1542 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1543 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1544 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1545
1546 VideoEncoderConfig video_encoder_config;
1547 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1548 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1549 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1550 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001551 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1552 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001553
Sergey Silkincd02eba2020-01-20 14:48:40 +01001554 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1555 WaitForEncodedFrame(1);
1556 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001557 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001558 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001559 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001560
1561 video_stream_encoder_->Stop();
1562}
1563
1564TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001565 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001566 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001567 DataRate::BitsPerSec(kTargetBitrateBps),
1568 DataRate::BitsPerSec(kTargetBitrateBps),
1569 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001570
1571 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001572 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001573 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001574 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001575 fake_encoder_.SetResolutionBitrateLimits(
1576 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1577
1578 VideoEncoderConfig video_encoder_config;
1579 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1580 video_encoder_config.max_bitrate_bps = 0;
1581 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1582 kMaxPayloadLength);
1583
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001584 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001585 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1586 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001587 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1588 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001589 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1590 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1591
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001592 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001593 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1594 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001595 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1596 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001597 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1598 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1599
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001600 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001601 // encoder for 360p should be used.
1602 video_source_.IncomingCapturedFrame(
1603 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1604 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001605 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1606 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001607 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1608 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1609
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001610 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001611 // ignored.
1612 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1613 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001614 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1615 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001616 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1617 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001618 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1619 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001620 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1621 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1622
1623 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1624 // for 270p should be used.
1625 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1626 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001627 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1628 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001629 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1630 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1631
1632 video_stream_encoder_->Stop();
1633}
1634
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001635TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1636 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001637 DataRate::BitsPerSec(kTargetBitrateBps),
1638 DataRate::BitsPerSec(kTargetBitrateBps),
1639 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001640
1641 VideoEncoderConfig video_encoder_config;
1642 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1643 video_encoder_config.max_bitrate_bps = 0;
1644 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1645 kMaxPayloadLength);
1646
1647 // Encode 720p frame to get the default encoder target bitrate.
1648 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1649 WaitForEncodedFrame(1);
1650 const uint32_t kDefaultTargetBitrateFor720pKbps =
1651 bitrate_allocator_factory_.codec_config()
1652 .simulcastStream[0]
1653 .targetBitrate;
1654
1655 // Set the max recommended encoder bitrate to something lower than the default
1656 // target bitrate.
1657 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1658 1280 * 720, 10 * 1000, 10 * 1000,
1659 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1660 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1661
1662 // Change resolution to trigger encoder reinitialization.
1663 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1664 WaitForEncodedFrame(2);
1665 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1666 WaitForEncodedFrame(3);
1667
1668 // Ensure the target bitrate is capped by the max bitrate.
1669 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1670 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1671 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1672 .simulcastStream[0]
1673 .targetBitrate *
1674 1000,
1675 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1676
1677 video_stream_encoder_->Stop();
1678}
1679
mflodmancc3d4422017-08-03 08:27:51 -07001680TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001681 EXPECT_TRUE(video_source_.has_sinks());
1682 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001683 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001684 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001685 EXPECT_FALSE(video_source_.has_sinks());
1686 EXPECT_TRUE(new_video_source.has_sinks());
1687
mflodmancc3d4422017-08-03 08:27:51 -07001688 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001689}
1690
mflodmancc3d4422017-08-03 08:27:51 -07001691TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001692 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001693 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001694 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001695 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001696}
1697
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001698TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1699 constexpr int kRequestedResolutionAlignment = 7;
1700 video_source_.set_adaptation_enabled(true);
1701 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1702 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001703 DataRate::BitsPerSec(kTargetBitrateBps),
1704 DataRate::BitsPerSec(kTargetBitrateBps),
1705 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001706
1707 // On the 1st frame, we should have initialized the encoder and
1708 // asked for its resolution requirements.
1709 video_source_.IncomingCapturedFrame(
1710 CreateFrame(1, codec_width_, codec_height_));
1711 WaitForEncodedFrame(1);
1712 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1713 kRequestedResolutionAlignment);
1714
1715 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1716 // (It's up the to the encoder to potentially drop the previous frame,
1717 // to avoid coding back-to-back keyframes.)
1718 video_source_.IncomingCapturedFrame(
1719 CreateFrame(2, codec_width_, codec_height_));
1720 WaitForEncodedFrame(2);
1721 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1722
1723 video_stream_encoder_->Stop();
1724}
1725
Jonathan Yubc771b72017-12-08 17:04:29 -08001726TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1727 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001728 const int kWidth = 1280;
1729 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001730
1731 // We rely on the automatic resolution adaptation, but we handle framerate
1732 // adaptation manually by mocking the stats proxy.
1733 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001734
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001735 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001736 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001737 DataRate::BitsPerSec(kTargetBitrateBps),
1738 DataRate::BitsPerSec(kTargetBitrateBps),
1739 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001740 video_stream_encoder_->SetSource(&video_source_,
1741 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001742 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001743 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001744 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001745 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1746
Jonathan Yubc771b72017-12-08 17:04:29 -08001747 // Adapt down as far as possible.
1748 rtc::VideoSinkWants last_wants;
1749 int64_t t = 1;
1750 int loop_count = 0;
1751 do {
1752 ++loop_count;
1753 last_wants = video_source_.sink_wants();
1754
1755 // Simulate the framerate we've been asked to adapt to.
1756 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1757 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1758 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1759 mock_stats.input_frame_rate = fps;
1760 stats_proxy_->SetMockStats(mock_stats);
1761
1762 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1763 sink_.WaitForEncodedFrame(t);
1764 t += frame_interval_ms;
1765
mflodmancc3d4422017-08-03 08:27:51 -07001766 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001767 VerifyBalancedModeFpsRange(
1768 video_source_.sink_wants(),
1769 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1770 } while (video_source_.sink_wants().max_pixel_count <
1771 last_wants.max_pixel_count ||
1772 video_source_.sink_wants().max_framerate_fps <
1773 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001774
Jonathan Yubc771b72017-12-08 17:04:29 -08001775 // Verify that we've adapted all the way down.
1776 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001777 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001778 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1779 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001780 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001781 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1782 *video_source_.last_sent_height());
1783 EXPECT_EQ(kMinBalancedFramerateFps,
1784 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001785
Jonathan Yubc771b72017-12-08 17:04:29 -08001786 // Adapt back up the same number of times we adapted down.
1787 for (int i = 0; i < loop_count - 1; ++i) {
1788 last_wants = video_source_.sink_wants();
1789
1790 // Simulate the framerate we've been asked to adapt to.
1791 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1792 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1793 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1794 mock_stats.input_frame_rate = fps;
1795 stats_proxy_->SetMockStats(mock_stats);
1796
1797 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1798 sink_.WaitForEncodedFrame(t);
1799 t += frame_interval_ms;
1800
mflodmancc3d4422017-08-03 08:27:51 -07001801 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001802 VerifyBalancedModeFpsRange(
1803 video_source_.sink_wants(),
1804 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1805 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1806 last_wants.max_pixel_count ||
1807 video_source_.sink_wants().max_framerate_fps >
1808 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001809 }
1810
Åsa Persson8c1bf952018-09-13 10:42:19 +02001811 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001812 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001813 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001814 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1815 EXPECT_EQ((loop_count - 1) * 2,
1816 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001817
mflodmancc3d4422017-08-03 08:27:51 -07001818 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001819}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001820
mflodmancc3d4422017-08-03 08:27:51 -07001821TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001822 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001823 DataRate::BitsPerSec(kTargetBitrateBps),
1824 DataRate::BitsPerSec(kTargetBitrateBps),
1825 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001826 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001827
sprangc5d62e22017-04-02 23:53:04 -07001828 const int kFrameWidth = 1280;
1829 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001830
Åsa Persson8c1bf952018-09-13 10:42:19 +02001831 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001832
kthelgason5e13d412016-12-01 03:59:51 -08001833 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001834 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001835 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001836 frame_timestamp += kFrameIntervalMs;
1837
perkj803d97f2016-11-01 11:45:46 -07001838 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001839 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001840 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001841 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001842 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001843 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001844
asapersson0944a802017-04-07 00:57:58 -07001845 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001846 // wanted resolution.
1847 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1848 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1849 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001850 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001851
1852 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001853 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001854 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001855 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001856 // Give the encoder queue time to process the change in degradation preference
1857 // by waiting for an encoded frame.
1858 new_video_source.IncomingCapturedFrame(
1859 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1860 sink_.WaitForEncodedFrame(frame_timestamp);
1861 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001862 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001863 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001864
sprangc5d62e22017-04-02 23:53:04 -07001865 // Force an input frame rate to be available, or the adaptation call won't
1866 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001867 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001868 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001869 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001870 stats_proxy_->SetMockStats(stats);
1871
mflodmancc3d4422017-08-03 08:27:51 -07001872 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001873 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001874 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001875 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001876 frame_timestamp += kFrameIntervalMs;
1877
1878 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001879 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001880 EXPECT_EQ(std::numeric_limits<int>::max(),
1881 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001882 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001883
asapersson02465b82017-04-10 01:12:52 -07001884 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001885 video_stream_encoder_->SetSource(&new_video_source,
1886 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001887 // Give the encoder queue time to process the change in degradation preference
1888 // by waiting for an encoded frame.
1889 new_video_source.IncomingCapturedFrame(
1890 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1891 sink_.WaitForEncodedFrame(frame_timestamp);
1892 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001893 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001894
mflodmancc3d4422017-08-03 08:27:51 -07001895 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001896 new_video_source.IncomingCapturedFrame(
1897 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001898 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001899 frame_timestamp += kFrameIntervalMs;
1900
1901 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001902 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001903
1904 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001905 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001906 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001907 // Give the encoder queue time to process the change in degradation preference
1908 // by waiting for an encoded frame.
1909 new_video_source.IncomingCapturedFrame(
1910 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1911 sink_.WaitForEncodedFrame(frame_timestamp);
1912 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001913 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1914 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001915 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001916 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001917
1918 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
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_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001921 // Give the encoder queue time to process the change in degradation preference
1922 // by waiting for an encoded frame.
1923 new_video_source.IncomingCapturedFrame(
1924 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1925 sink_.WaitForEncodedFrame(frame_timestamp);
1926 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001927 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1928 EXPECT_EQ(std::numeric_limits<int>::max(),
1929 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001930 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001931
mflodmancc3d4422017-08-03 08:27:51 -07001932 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001933}
1934
mflodmancc3d4422017-08-03 08:27:51 -07001935TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001936 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001937 DataRate::BitsPerSec(kTargetBitrateBps),
1938 DataRate::BitsPerSec(kTargetBitrateBps),
1939 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001940
asaperssonfab67072017-04-04 05:51:49 -07001941 const int kWidth = 1280;
1942 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001943 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001944 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001945 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1946 EXPECT_FALSE(stats.bw_limited_resolution);
1947 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1948
1949 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001950 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001951 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001952 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001953
1954 stats = stats_proxy_->GetStats();
1955 EXPECT_TRUE(stats.bw_limited_resolution);
1956 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1957
1958 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001959 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001960 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001961 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001962
1963 stats = stats_proxy_->GetStats();
1964 EXPECT_FALSE(stats.bw_limited_resolution);
1965 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1966 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1967
mflodmancc3d4422017-08-03 08:27:51 -07001968 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001969}
1970
mflodmancc3d4422017-08-03 08:27:51 -07001971TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001972 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001973 DataRate::BitsPerSec(kTargetBitrateBps),
1974 DataRate::BitsPerSec(kTargetBitrateBps),
1975 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001976
1977 const int kWidth = 1280;
1978 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001979 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001980 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001981 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1982 EXPECT_FALSE(stats.cpu_limited_resolution);
1983 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1984
1985 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001986 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001987 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001988 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001989
1990 stats = stats_proxy_->GetStats();
1991 EXPECT_TRUE(stats.cpu_limited_resolution);
1992 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1993
1994 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001995 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001996 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001997 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001998
1999 stats = stats_proxy_->GetStats();
2000 EXPECT_FALSE(stats.cpu_limited_resolution);
2001 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07002002 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002003
mflodmancc3d4422017-08-03 08:27:51 -07002004 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002005}
2006
mflodmancc3d4422017-08-03 08:27:51 -07002007TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002008 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002009 DataRate::BitsPerSec(kTargetBitrateBps),
2010 DataRate::BitsPerSec(kTargetBitrateBps),
2011 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002012
asaperssonfab67072017-04-04 05:51:49 -07002013 const int kWidth = 1280;
2014 const int kHeight = 720;
2015 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002016 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002017 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002018 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002019 EXPECT_FALSE(stats.cpu_limited_resolution);
2020 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2021
asaperssonfab67072017-04-04 05:51:49 -07002022 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002023 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002024 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002025 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002026 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002027 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002028 EXPECT_TRUE(stats.cpu_limited_resolution);
2029 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2030
2031 // Set new source with adaptation still enabled.
2032 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002033 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002034 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002035
asaperssonfab67072017-04-04 05:51:49 -07002036 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002037 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002038 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002039 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002040 EXPECT_TRUE(stats.cpu_limited_resolution);
2041 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2042
2043 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002044 video_stream_encoder_->SetSource(&new_video_source,
2045 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002046
asaperssonfab67072017-04-04 05:51:49 -07002047 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002048 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002049 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002050 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002051 EXPECT_FALSE(stats.cpu_limited_resolution);
2052 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2053
2054 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002055 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002056 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002057
asaperssonfab67072017-04-04 05:51:49 -07002058 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002059 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002060 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002061 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002062 EXPECT_TRUE(stats.cpu_limited_resolution);
2063 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2064
asaperssonfab67072017-04-04 05:51:49 -07002065 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002066 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002067 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002068 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002069 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002070 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002071 EXPECT_FALSE(stats.cpu_limited_resolution);
2072 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002073 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002074
mflodmancc3d4422017-08-03 08:27:51 -07002075 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002076}
2077
mflodmancc3d4422017-08-03 08:27:51 -07002078TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002079 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002080 DataRate::BitsPerSec(kTargetBitrateBps),
2081 DataRate::BitsPerSec(kTargetBitrateBps),
2082 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002083
asaperssonfab67072017-04-04 05:51:49 -07002084 const int kWidth = 1280;
2085 const int kHeight = 720;
2086 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002087 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002088 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002089 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002090 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002091 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002092
2093 // Set new source with adaptation still enabled.
2094 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002095 video_stream_encoder_->SetSource(&new_video_source,
2096 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002097
asaperssonfab67072017-04-04 05:51:49 -07002098 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002099 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002100 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002101 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002102 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002103 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002104
asaperssonfab67072017-04-04 05:51:49 -07002105 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002106 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002107 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002108 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002109 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002110 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002111 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002112 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002113
asaperssonfab67072017-04-04 05:51:49 -07002114 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002115 video_stream_encoder_->SetSource(&new_video_source,
2116 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002117
asaperssonfab67072017-04-04 05:51:49 -07002118 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002119 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002120 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002121 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002122 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002123 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002124
asapersson02465b82017-04-10 01:12:52 -07002125 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002126 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002127 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002128
asaperssonfab67072017-04-04 05:51:49 -07002129 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002130 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002131 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002132 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002133 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002134 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2135 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002136
mflodmancc3d4422017-08-03 08:27:51 -07002137 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002138}
2139
mflodmancc3d4422017-08-03 08:27:51 -07002140TEST_F(VideoStreamEncoderTest,
2141 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002142 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002143 DataRate::BitsPerSec(kTargetBitrateBps),
2144 DataRate::BitsPerSec(kTargetBitrateBps),
2145 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002146
2147 const int kWidth = 1280;
2148 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002149 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002150 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002151 video_source_.IncomingCapturedFrame(
2152 CreateFrame(timestamp_ms, kWidth, kHeight));
2153 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002154 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2155 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2156 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2157
2158 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002159 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002160 timestamp_ms += kFrameIntervalMs;
2161 video_source_.IncomingCapturedFrame(
2162 CreateFrame(timestamp_ms, kWidth, kHeight));
2163 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002164 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2165 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2166 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2167
2168 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002169 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002170 timestamp_ms += kFrameIntervalMs;
2171 video_source_.IncomingCapturedFrame(
2172 CreateFrame(timestamp_ms, kWidth, kHeight));
2173 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002174 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2175 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2176 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2177
Niels Möller4db138e2018-04-19 09:04:13 +02002178 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002179 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002180
2181 VideoEncoderConfig video_encoder_config;
2182 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2183 // Make format different, to force recreation of encoder.
2184 video_encoder_config.video_format.parameters["foo"] = "foo";
2185 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002186 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002187 timestamp_ms += kFrameIntervalMs;
2188 video_source_.IncomingCapturedFrame(
2189 CreateFrame(timestamp_ms, kWidth, kHeight));
2190 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002191 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2192 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2193 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2194
mflodmancc3d4422017-08-03 08:27:51 -07002195 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002196}
2197
mflodmancc3d4422017-08-03 08:27:51 -07002198TEST_F(VideoStreamEncoderTest,
2199 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002200 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002201 DataRate::BitsPerSec(kTargetBitrateBps),
2202 DataRate::BitsPerSec(kTargetBitrateBps),
2203 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002204
asapersson0944a802017-04-07 00:57:58 -07002205 const int kWidth = 1280;
2206 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002207 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002208
asaperssonfab67072017-04-04 05:51:49 -07002209 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002210 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002211 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002212 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002213 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002214 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2215
asapersson02465b82017-04-10 01:12:52 -07002216 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002217 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002218 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002219 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002220 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002221 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002222 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002223 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2224
2225 // Set new source with adaptation still enabled.
2226 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002227 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002228 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002229
2230 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002231 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002232 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002233 stats = stats_proxy_->GetStats();
2234 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002235 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002236 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2237
sprangc5d62e22017-04-02 23:53:04 -07002238 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002239 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002240 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002241 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002242 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002243 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002244 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002245 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002246 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002247 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002248 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2249
sprangc5d62e22017-04-02 23:53:04 -07002250 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002251 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002252 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2253 mock_stats.input_frame_rate = 30;
2254 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002255 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002256 stats_proxy_->ResetMockStats();
2257
2258 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002259 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002260 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002261
2262 // Framerate now adapted.
2263 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002264 EXPECT_FALSE(stats.cpu_limited_resolution);
2265 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002266 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2267
2268 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002269 video_stream_encoder_->SetSource(&new_video_source,
2270 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002271 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002272 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002273 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002274
2275 stats = stats_proxy_->GetStats();
2276 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002277 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002278 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2279
2280 // Try to trigger overuse. Should not succeed.
2281 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002282 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002283 stats_proxy_->ResetMockStats();
2284
2285 stats = stats_proxy_->GetStats();
2286 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002287 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002288 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2289
2290 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002291 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002292 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002293 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002294 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002295 stats = stats_proxy_->GetStats();
2296 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002297 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002298 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002299
2300 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002301 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002302 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002303 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002304 stats = stats_proxy_->GetStats();
2305 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002306 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002307 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2308
2309 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002310 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002311 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002312 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002313 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002314 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002315 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002316 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002317 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002318 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002319 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2320
2321 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002322 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002323 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002324 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002325 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002326 stats = stats_proxy_->GetStats();
2327 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002328 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002329 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002330 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002331
mflodmancc3d4422017-08-03 08:27:51 -07002332 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002333}
2334
mflodmancc3d4422017-08-03 08:27:51 -07002335TEST_F(VideoStreamEncoderTest,
2336 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002337 const int kWidth = 1280;
2338 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002339 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002340 DataRate::BitsPerSec(kTargetBitrateBps),
2341 DataRate::BitsPerSec(kTargetBitrateBps),
2342 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002343
asaperssonfab67072017-04-04 05:51:49 -07002344 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002345 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002346
asaperssonfab67072017-04-04 05:51:49 -07002347 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002348 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002349
asaperssonfab67072017-04-04 05:51:49 -07002350 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002351 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002352
asaperssonfab67072017-04-04 05:51:49 -07002353 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002354 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002355
kthelgason876222f2016-11-29 01:44:11 -08002356 // Expect a scale down.
2357 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002358 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002359
asapersson02465b82017-04-10 01:12:52 -07002360 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002361 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002362 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002363 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002364
asaperssonfab67072017-04-04 05:51:49 -07002365 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002366 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002367 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002368 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002369
asaperssonfab67072017-04-04 05:51:49 -07002370 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002371 EXPECT_EQ(std::numeric_limits<int>::max(),
2372 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002373
asaperssonfab67072017-04-04 05:51:49 -07002374 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002375 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002376 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002377 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002378
asapersson02465b82017-04-10 01:12:52 -07002379 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002380 EXPECT_EQ(std::numeric_limits<int>::max(),
2381 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002382
mflodmancc3d4422017-08-03 08:27:51 -07002383 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002384}
2385
mflodmancc3d4422017-08-03 08:27:51 -07002386TEST_F(VideoStreamEncoderTest,
2387 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002388 const int kWidth = 1280;
2389 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002390 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002391 DataRate::BitsPerSec(kTargetBitrateBps),
2392 DataRate::BitsPerSec(kTargetBitrateBps),
2393 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002394
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002395 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002396 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002397 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002398 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002399
2400 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002401 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002402 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002403 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2404 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2405
2406 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002407 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002408 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002409 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2410 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2411 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2412
2413 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002414 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002415 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2416 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2417 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2418
mflodmancc3d4422017-08-03 08:27:51 -07002419 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002420}
2421
mflodmancc3d4422017-08-03 08:27:51 -07002422TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002423 const int kWidth = 1280;
2424 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002425 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002426 DataRate::BitsPerSec(kTargetBitrateBps),
2427 DataRate::BitsPerSec(kTargetBitrateBps),
2428 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002429
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002430 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002431 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002432 video_stream_encoder_->SetSource(&source,
2433 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002434 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2435 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002436 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002437
2438 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002439 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002440 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2441 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2442 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2443 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2444
2445 // Trigger adapt down for same input resolution, expect no change.
2446 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2447 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002448 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002449 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2450 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2451 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2452
2453 // Trigger adapt down for larger input resolution, expect no change.
2454 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2455 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002456 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002457 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2458 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2459 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2460
mflodmancc3d4422017-08-03 08:27:51 -07002461 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002462}
2463
mflodmancc3d4422017-08-03 08:27:51 -07002464TEST_F(VideoStreamEncoderTest,
2465 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002466 const int kWidth = 1280;
2467 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002468 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002469 DataRate::BitsPerSec(kTargetBitrateBps),
2470 DataRate::BitsPerSec(kTargetBitrateBps),
2471 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002472
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002473 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002474 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002475 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002476 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002477
2478 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002479 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002480 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002481 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2482 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2483
2484 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002485 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002486 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002487 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2488 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2489
mflodmancc3d4422017-08-03 08:27:51 -07002490 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002491}
2492
mflodmancc3d4422017-08-03 08:27:51 -07002493TEST_F(VideoStreamEncoderTest,
2494 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002495 const int kWidth = 1280;
2496 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002497 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002498 DataRate::BitsPerSec(kTargetBitrateBps),
2499 DataRate::BitsPerSec(kTargetBitrateBps),
2500 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002501
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002502 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002503 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002504 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002505 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002506
2507 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002508 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002509 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002510 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002511 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2512
2513 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002514 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002515 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002516 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002517 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2518
mflodmancc3d4422017-08-03 08:27:51 -07002519 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002520}
2521
mflodmancc3d4422017-08-03 08:27:51 -07002522TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002523 const int kWidth = 1280;
2524 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002525 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002526 DataRate::BitsPerSec(kTargetBitrateBps),
2527 DataRate::BitsPerSec(kTargetBitrateBps),
2528 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002529
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002530 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002531 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002532 video_stream_encoder_->SetSource(&source,
2533 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002534
2535 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2536 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002537 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002538 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2539 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2540 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2541
2542 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002543 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002544 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002545 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2546 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2547 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2548
mflodmancc3d4422017-08-03 08:27:51 -07002549 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002550}
2551
mflodmancc3d4422017-08-03 08:27:51 -07002552TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002553 const int kWidth = 1280;
2554 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002555 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002556 DataRate::BitsPerSec(kTargetBitrateBps),
2557 DataRate::BitsPerSec(kTargetBitrateBps),
2558 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002559
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002560 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002561 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002562 video_stream_encoder_->SetSource(&source,
2563 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002564
2565 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2566 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002567 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002568 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2569 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2570 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2571
2572 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002573 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002574 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002575 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2576 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2577 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2578
mflodmancc3d4422017-08-03 08:27:51 -07002579 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002580}
2581
mflodmancc3d4422017-08-03 08:27:51 -07002582TEST_F(VideoStreamEncoderTest,
2583 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002584 const int kWidth = 1280;
2585 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002586 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002587 DataRate::BitsPerSec(kTargetBitrateBps),
2588 DataRate::BitsPerSec(kTargetBitrateBps),
2589 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002590
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002591 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002592 AdaptingFrameForwarder source;
2593 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002594 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002595 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002596
2597 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002598 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002599 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002600 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2601 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2602
2603 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002604 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002605 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002606 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002607 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002608 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2609 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2610
2611 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002612 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002613 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002614 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2615 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2616 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2617
mflodmancc3d4422017-08-03 08:27:51 -07002618 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002619}
2620
mflodmancc3d4422017-08-03 08:27:51 -07002621TEST_F(VideoStreamEncoderTest,
2622 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002623 const int kWidth = 1280;
2624 const int kHeight = 720;
2625 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002626 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002627 DataRate::BitsPerSec(kTargetBitrateBps),
2628 DataRate::BitsPerSec(kTargetBitrateBps),
2629 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002630
2631 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2632 stats.input_frame_rate = kInputFps;
2633 stats_proxy_->SetMockStats(stats);
2634
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002635 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002636 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2637 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002638 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002639
2640 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002641 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002642 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2643 sink_.WaitForEncodedFrame(2);
2644 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2645
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002646 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002647 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002648 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002649 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002650 // Give the encoder queue time to process the change in degradation preference
2651 // by waiting for an encoded frame.
2652 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2653 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002654 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002655
2656 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002657 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002658 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2659 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002660 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2661
2662 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002663 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002664 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002665
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002667}
2668
mflodmancc3d4422017-08-03 08:27:51 -07002669TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002670 const int kWidth = 1280;
2671 const int kHeight = 720;
2672 const size_t kNumFrames = 10;
2673
Erik Språng4c6ca302019-04-08 15:14:01 +02002674 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002675 DataRate::BitsPerSec(kTargetBitrateBps),
2676 DataRate::BitsPerSec(kTargetBitrateBps),
2677 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002678
asaperssond0de2952017-04-21 01:47:31 -07002679 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002680 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002681 video_source_.set_adaptation_enabled(true);
2682
2683 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2684 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2685
2686 int downscales = 0;
2687 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002688 video_source_.IncomingCapturedFrame(
2689 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2690 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002691
asaperssonfab67072017-04-04 05:51:49 -07002692 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002693 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002694 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002695 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002696
2697 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2698 ++downscales;
2699
2700 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2701 EXPECT_EQ(downscales,
2702 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2703 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002704 }
mflodmancc3d4422017-08-03 08:27:51 -07002705 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002706}
2707
mflodmancc3d4422017-08-03 08:27:51 -07002708TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002709 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2710 const int kWidth = 1280;
2711 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002712 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002713 DataRate::BitsPerSec(kTargetBitrateBps),
2714 DataRate::BitsPerSec(kTargetBitrateBps),
2715 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002716
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002717 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002718 AdaptingFrameForwarder source;
2719 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002720 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002721 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002722
Åsa Persson8c1bf952018-09-13 10:42:19 +02002723 int64_t timestamp_ms = kFrameIntervalMs;
2724 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002725 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002726 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002727 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2728 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2729
2730 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002731 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002732 timestamp_ms += kFrameIntervalMs;
2733 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2734 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002735 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002736 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2737 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2738
2739 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002740 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002741 timestamp_ms += kFrameIntervalMs;
2742 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002743 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002744 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002745 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2746 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2747
2748 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002749 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002750 timestamp_ms += kFrameIntervalMs;
2751 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2752 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002753 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002754 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2755 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2756
2757 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002758 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002759 timestamp_ms += kFrameIntervalMs;
2760 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002761 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002762 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002763 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2764 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2765
mflodmancc3d4422017-08-03 08:27:51 -07002766 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002767}
2768
mflodmancc3d4422017-08-03 08:27:51 -07002769TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002770 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2771 const int kWidth = 1280;
2772 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002773 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002774 DataRate::BitsPerSec(kTargetBitrateBps),
2775 DataRate::BitsPerSec(kTargetBitrateBps),
2776 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002777
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002778 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002779 AdaptingFrameForwarder source;
2780 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002781 video_stream_encoder_->SetSource(&source,
2782 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002783
Åsa Persson8c1bf952018-09-13 10:42:19 +02002784 int64_t timestamp_ms = kFrameIntervalMs;
2785 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002786 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002787 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002788 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2789 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2790
2791 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002792 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002793 timestamp_ms += kFrameIntervalMs;
2794 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2795 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002796 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2797 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2798 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2799
2800 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002801 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002802 timestamp_ms += kFrameIntervalMs;
2803 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002804 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002805 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002806 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2807 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2808
2809 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002810 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002811 timestamp_ms += kFrameIntervalMs;
2812 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2813 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002814 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2815 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2816 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2817
2818 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002819 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002820 timestamp_ms += kFrameIntervalMs;
2821 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002822 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002823 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002824 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2825 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2826
mflodmancc3d4422017-08-03 08:27:51 -07002827 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002828}
2829
Sergey Silkin41c650b2019-10-14 13:12:19 +02002830TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2831 fake_encoder_.SetResolutionBitrateLimits(
2832 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2833
2834 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002835 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2836 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2837 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2838 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002839
2840 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2841 AdaptingFrameForwarder source;
2842 source.set_adaptation_enabled(true);
2843 video_stream_encoder_->SetSource(
2844 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2845
2846 // Insert 720p frame.
2847 int64_t timestamp_ms = kFrameIntervalMs;
2848 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2849 WaitForEncodedFrame(1280, 720);
2850
2851 // Reduce bitrate and trigger adapt down.
2852 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002853 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2854 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2855 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2856 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002857 video_stream_encoder_->TriggerQualityLow();
2858
2859 // Insert 720p frame. It should be downscaled and encoded.
2860 timestamp_ms += kFrameIntervalMs;
2861 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2862 WaitForEncodedFrame(960, 540);
2863
2864 // Trigger adapt up. Higher resolution should not be requested duo to lack
2865 // of bitrate.
2866 video_stream_encoder_->TriggerQualityHigh();
2867 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2868
2869 // Increase bitrate.
2870 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002871 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2872 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2873 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2874 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002875
2876 // Trigger adapt up. Higher resolution should be requested.
2877 video_stream_encoder_->TriggerQualityHigh();
2878 VerifyFpsMaxResolutionMax(source.sink_wants());
2879
2880 video_stream_encoder_->Stop();
2881}
2882
2883TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2884 fake_encoder_.SetResolutionBitrateLimits(
2885 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2886
2887 // Set bitrate equal to min bitrate of 540p.
2888 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002889 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2890 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2891 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2892 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002893
2894 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2895 AdaptingFrameForwarder source;
2896 source.set_adaptation_enabled(true);
2897 video_stream_encoder_->SetSource(
2898 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2899
2900 // Insert 720p frame. It should be dropped and lower resolution should be
2901 // requested.
2902 int64_t timestamp_ms = kFrameIntervalMs;
2903 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2904 ExpectDroppedFrame();
2905 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2906
2907 // Insert 720p frame. It should be downscaled and encoded.
2908 timestamp_ms += kFrameIntervalMs;
2909 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2910 WaitForEncodedFrame(960, 540);
2911
2912 video_stream_encoder_->Stop();
2913}
2914
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002915class BalancedDegradationTest : public VideoStreamEncoderTest {
2916 protected:
2917 void SetupTest() {
2918 // Reset encoder for field trials to take effect.
2919 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002920 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002921
2922 // Enable BALANCED preference.
2923 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002924 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2925 }
2926
2927 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01002928 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002929 DataRate::BitsPerSec(bitrate_bps), DataRate::BitsPerSec(bitrate_bps),
2930 DataRate::BitsPerSec(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002931 }
2932
Åsa Persson45b176f2019-09-30 11:19:05 +02002933 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002934 timestamp_ms_ += kFrameIntervalMs;
2935 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002936 }
2937
2938 void InsertFrameAndWaitForEncoded() {
2939 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002940 sink_.WaitForEncodedFrame(timestamp_ms_);
2941 }
2942
2943 const int kWidth = 640; // pixels:640x360=230400
2944 const int kHeight = 360;
2945 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2946 int64_t timestamp_ms_ = 0;
2947 AdaptingFrameForwarder source_;
2948};
2949
2950TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2951 test::ScopedFieldTrials field_trials(
2952 "WebRTC-Video-BalancedDegradationSettings/"
2953 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2954 SetupTest();
2955
2956 // Force input frame rate.
2957 const int kInputFps = 24;
2958 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2959 stats.input_frame_rate = kInputFps;
2960 stats_proxy_->SetMockStats(stats);
2961
Åsa Persson45b176f2019-09-30 11:19:05 +02002962 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002963 VerifyFpsMaxResolutionMax(source_.sink_wants());
2964
2965 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2966 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2967 video_stream_encoder_->TriggerQualityLowExpectFalse();
2968 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2969
2970 video_stream_encoder_->Stop();
2971}
2972
2973TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2974 test::ScopedFieldTrials field_trials(
2975 "WebRTC-Video-BalancedDegradationSettings/"
2976 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2977 SetupTest();
2978
2979 // Force input frame rate.
2980 const int kInputFps = 25;
2981 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2982 stats.input_frame_rate = kInputFps;
2983 stats_proxy_->SetMockStats(stats);
2984
Åsa Persson45b176f2019-09-30 11:19:05 +02002985 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002986 VerifyFpsMaxResolutionMax(source_.sink_wants());
2987
2988 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2989 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2990 video_stream_encoder_->TriggerQualityLow();
2991 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2992
2993 video_stream_encoder_->Stop();
2994}
2995
2996TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2997 test::ScopedFieldTrials field_trials(
2998 "WebRTC-Video-BalancedDegradationSettings/"
2999 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
3000 SetupTest();
3001
3002 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
3003
Åsa Persson45b176f2019-09-30 11:19:05 +02003004 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003005 VerifyFpsMaxResolutionMax(source_.sink_wants());
3006
3007 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
3008 video_stream_encoder_->TriggerQualityLow();
3009 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
3010
3011 video_stream_encoder_->Stop();
3012}
3013
Åsa Perssonccfb3402019-09-25 15:13:04 +02003014TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003015 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02003016 "WebRTC-Video-BalancedDegradationSettings/"
3017 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003018 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02003019
Åsa Persson1b247f12019-08-14 17:26:39 +02003020 const int kMinBitrateBps = 425000;
3021 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003022 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003023
Åsa Persson45b176f2019-09-30 11:19:05 +02003024 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003025 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003026 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3027
3028 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3029 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003030 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003031 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02003032 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3033
3034 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3035 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003036 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003037 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003038 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3039
Åsa Persson30ab0152019-08-27 12:22:33 +02003040 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3041 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003042 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003043 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
3044 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02003045 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3046
3047 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02003048 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003049 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003050 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02003051
Åsa Persson30ab0152019-08-27 12:22:33 +02003052 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003053 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003054 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003055 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003056 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003057 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3058
3059 video_stream_encoder_->Stop();
3060}
3061
Åsa Perssonccfb3402019-09-25 15:13:04 +02003062TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003063 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3064 test::ScopedFieldTrials field_trials(
3065 "WebRTC-Video-BalancedDegradationSettings/"
3066 "pixels:57600|129600|230400,fps:7|24|24/");
3067 SetupTest();
3068 OnBitrateUpdated(kLowTargetBitrateBps);
3069
3070 VerifyNoLimitation(source_.sink_wants());
3071
3072 // Insert frame, expect scaled down:
3073 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3074 InsertFrame();
3075 EXPECT_FALSE(WaitForFrame(1000));
3076 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3077 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3078
3079 // Insert frame, expect scaled down:
3080 // resolution (320x180@24fps).
3081 InsertFrame();
3082 EXPECT_FALSE(WaitForFrame(1000));
3083 EXPECT_LT(source_.sink_wants().max_pixel_count,
3084 source_.last_wants().max_pixel_count);
3085 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3086
3087 // Frame should not be dropped (min pixels per frame reached).
3088 InsertFrameAndWaitForEncoded();
3089
3090 video_stream_encoder_->Stop();
3091}
3092
3093TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003094 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003095 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003096 "WebRTC-Video-BalancedDegradationSettings/"
3097 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003098 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003099
Åsa Persson30ab0152019-08-27 12:22:33 +02003100 const int kResolutionMinBitrateBps = 435000;
3101 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003102 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003103
Åsa Persson45b176f2019-09-30 11:19:05 +02003104 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003105 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003106 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3107
3108 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3109 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003110 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003111 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003112 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3113
3114 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3115 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003116 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003117 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003118 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3119
3120 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3121 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003122 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003123 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003124 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3125
Åsa Persson30ab0152019-08-27 12:22:33 +02003126 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3127 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003128 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003129 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003130 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3131
3132 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3133 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003134 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003135 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3136
3137 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003138 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003139 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003140 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003141 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003142 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3143
3144 video_stream_encoder_->Stop();
3145}
3146
Åsa Perssonccfb3402019-09-25 15:13:04 +02003147TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003148 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003149 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003150 "WebRTC-Video-BalancedDegradationSettings/"
3151 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003152 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003153
Åsa Persson30ab0152019-08-27 12:22:33 +02003154 const int kMinBitrateBps = 425000;
3155 const int kTooLowMinBitrateBps = 424000;
3156 const int kResolutionMinBitrateBps = 435000;
3157 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003158 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003159
Åsa Persson45b176f2019-09-30 11:19:05 +02003160 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003161 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003162 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3163
3164 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3165 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003166 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003167 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003168 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3169
3170 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3171 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003172 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003173 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003174 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3175
3176 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3177 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003178 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003179 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003180 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3181
3182 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3183 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003184 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003185 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3186
3187 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003188 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003189 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003190 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003191 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003192 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3193
3194 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003195 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003196 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003197 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003198 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3199
3200 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003201 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003202 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003203 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003204 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003205 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3206
Åsa Persson1b247f12019-08-14 17:26:39 +02003207 video_stream_encoder_->Stop();
3208}
3209
mflodmancc3d4422017-08-03 08:27:51 -07003210TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003211 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3212 const int kWidth = 1280;
3213 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003214 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003215 DataRate::BitsPerSec(kTargetBitrateBps),
3216 DataRate::BitsPerSec(kTargetBitrateBps),
3217 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003218
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003219 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003220 AdaptingFrameForwarder source;
3221 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003222 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003223 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003224
Åsa Persson8c1bf952018-09-13 10:42:19 +02003225 int64_t timestamp_ms = kFrameIntervalMs;
3226 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003227 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003228 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003229 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3230 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3231 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3232 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3233
3234 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003235 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003236 timestamp_ms += kFrameIntervalMs;
3237 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3238 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003239 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003240 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3241 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3242 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3243 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3244
3245 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003246 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003247 timestamp_ms += kFrameIntervalMs;
3248 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3249 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003250 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003251 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3252 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3253 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3254 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3255
Jonathan Yubc771b72017-12-08 17:04:29 -08003256 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003257 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003258 timestamp_ms += kFrameIntervalMs;
3259 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3260 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003261 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003262 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3263 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003264 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003265 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3266
Jonathan Yubc771b72017-12-08 17:04:29 -08003267 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003268 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003269 timestamp_ms += kFrameIntervalMs;
3270 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3271 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003272 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003273 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003274 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3275 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3276 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3277 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3278
Jonathan Yubc771b72017-12-08 17:04:29 -08003279 // Trigger quality adapt down, expect no change (min resolution reached).
3280 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003281 timestamp_ms += kFrameIntervalMs;
3282 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3283 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003284 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3285 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3286 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3287 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3288 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3289
3290 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003291 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003292 timestamp_ms += kFrameIntervalMs;
3293 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3294 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003295 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003296 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3297 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3298 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3299 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3300
3301 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3302 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003303 timestamp_ms += kFrameIntervalMs;
3304 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3305 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003306 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3307 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3308 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3309 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3310 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3311
3312 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3313 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003314 timestamp_ms += kFrameIntervalMs;
3315 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3316 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003317 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003318 last_wants = source.sink_wants();
3319 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3320 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003321 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003322 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3323
3324 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003325 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003326 timestamp_ms += kFrameIntervalMs;
3327 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3328 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003329 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003330 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3331 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003332 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003333 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3334
3335 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003336 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003337 timestamp_ms += kFrameIntervalMs;
3338 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003339 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003340 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003341 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003342 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3343 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003344 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003345 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003346
mflodmancc3d4422017-08-03 08:27:51 -07003347 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003348}
3349
mflodmancc3d4422017-08-03 08:27:51 -07003350TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003351 const int kWidth = 640;
3352 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003353
Erik Språng4c6ca302019-04-08 15:14:01 +02003354 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003355 DataRate::BitsPerSec(kTargetBitrateBps),
3356 DataRate::BitsPerSec(kTargetBitrateBps),
3357 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003358
perkj803d97f2016-11-01 11:45:46 -07003359 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003360 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003361 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003362 }
3363
mflodmancc3d4422017-08-03 08:27:51 -07003364 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003365 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003366 video_source_.IncomingCapturedFrame(CreateFrame(
3367 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003368 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003369 }
3370
mflodmancc3d4422017-08-03 08:27:51 -07003371 video_stream_encoder_->Stop();
3372 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003373 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003374
Ying Wangef3998f2019-12-09 13:06:53 +01003375 EXPECT_METRIC_EQ(
3376 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3377 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003378 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3379}
3380
mflodmancc3d4422017-08-03 08:27:51 -07003381TEST_F(VideoStreamEncoderTest,
3382 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003383 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003384 DataRate::BitsPerSec(kTargetBitrateBps),
3385 DataRate::BitsPerSec(kTargetBitrateBps),
3386 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003387 const int kWidth = 640;
3388 const int kHeight = 360;
3389
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003390 video_stream_encoder_->SetSource(&video_source_,
3391 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003392
3393 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3394 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003395 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003396 }
3397
mflodmancc3d4422017-08-03 08:27:51 -07003398 video_stream_encoder_->Stop();
3399 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003400 stats_proxy_.reset();
3401
3402 EXPECT_EQ(0,
3403 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3404}
3405
mflodmancc3d4422017-08-03 08:27:51 -07003406TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003407 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003408 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003409
3410 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003411 const VideoBitrateAllocation expected_bitrate =
Mirta Dvornicic6799d732020-02-12 15:36:49 +01003412 SimulcastRateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003413 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3414 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003415
sprang57c2fff2017-01-16 06:24:02 -08003416 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003417 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003418 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003419 DataRate::BitsPerSec(kLowTargetBitrateBps),
3420 DataRate::BitsPerSec(kLowTargetBitrateBps),
3421 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003422
sprang57c2fff2017-01-16 06:24:02 -08003423 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003424 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3425 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003426 VideoBitrateAllocation bitrate_allocation =
3427 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003428 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003429 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003430 // TODO(srte): The use of millisecs here looks like an error, but the tests
3431 // fails using seconds, this should be investigated.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003432 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003433
3434 // Not called on second frame.
3435 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3436 .Times(0);
3437 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003438 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3439 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003440 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003441
3442 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003443 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3444 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003445 const int64_t start_time_ms = rtc::TimeMillis();
3446 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3447 video_source_.IncomingCapturedFrame(
3448 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3449 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003450 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003451 }
3452
3453 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003454 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003455
mflodmancc3d4422017-08-03 08:27:51 -07003456 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003457}
3458
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003459TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3460 // 2 TLs configured, temporal layers supported by encoder.
3461 const int kNumTemporalLayers = 2;
3462 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3463 fake_encoder_.SetTemporalLayersSupported(0, true);
3464
3465 // Bitrate allocated across temporal layers.
3466 const int kTl0Bps = kTargetBitrateBps *
3467 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003468 kNumTemporalLayers, /*temporal_id*/ 0,
3469 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003470 const int kTl1Bps = kTargetBitrateBps *
3471 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003472 kNumTemporalLayers, /*temporal_id*/ 1,
3473 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003474 VideoBitrateAllocation expected_bitrate;
3475 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3476 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3477
3478 VerifyAllocatedBitrate(expected_bitrate);
3479 video_stream_encoder_->Stop();
3480}
3481
3482TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3483 // 2 TLs configured, temporal layers not supported by encoder.
3484 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3485 fake_encoder_.SetTemporalLayersSupported(0, false);
3486
3487 // Temporal layers not supported by the encoder.
3488 // Total bitrate should be at ti:0.
3489 VideoBitrateAllocation expected_bitrate;
3490 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3491
3492 VerifyAllocatedBitrate(expected_bitrate);
3493 video_stream_encoder_->Stop();
3494}
3495
3496TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3497 // 2 TLs configured, temporal layers only supported for first stream.
3498 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3499 fake_encoder_.SetTemporalLayersSupported(0, true);
3500 fake_encoder_.SetTemporalLayersSupported(1, false);
3501
3502 const int kS0Bps = 150000;
3503 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003504 kS0Bps *
3505 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3506 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003507 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003508 kS0Bps *
3509 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3510 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003511 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3512 // Temporal layers not supported by si:1.
3513 VideoBitrateAllocation expected_bitrate;
3514 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3515 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3516 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3517
3518 VerifyAllocatedBitrate(expected_bitrate);
3519 video_stream_encoder_->Stop();
3520}
3521
Niels Möller7dc26b72017-12-06 10:27:48 +01003522TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3523 const int kFrameWidth = 1280;
3524 const int kFrameHeight = 720;
3525 const int kFramerate = 24;
3526
Erik Språng4c6ca302019-04-08 15:14:01 +02003527 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003528 DataRate::BitsPerSec(kTargetBitrateBps),
3529 DataRate::BitsPerSec(kTargetBitrateBps),
3530 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003531 test::FrameForwarder source;
3532 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003533 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003534
3535 // Insert a single frame, triggering initial configuration.
3536 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3537 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3538
3539 EXPECT_EQ(
3540 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3541 kDefaultFramerate);
3542
3543 // Trigger reconfigure encoder (without resetting the entire instance).
3544 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003545 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003546 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3547 video_encoder_config.number_of_streams = 1;
3548 video_encoder_config.video_stream_factory =
3549 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3550 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003551 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003552 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3553
3554 // Detector should be updated with fps limit from codec config.
3555 EXPECT_EQ(
3556 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3557 kFramerate);
3558
3559 // Trigger overuse, max framerate should be reduced.
3560 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3561 stats.input_frame_rate = kFramerate;
3562 stats_proxy_->SetMockStats(stats);
3563 video_stream_encoder_->TriggerCpuOveruse();
3564 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3565 int adapted_framerate =
3566 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3567 EXPECT_LT(adapted_framerate, kFramerate);
3568
3569 // Trigger underuse, max framerate should go back to codec configured fps.
3570 // Set extra low fps, to make sure it's actually reset, not just incremented.
3571 stats = stats_proxy_->GetStats();
3572 stats.input_frame_rate = adapted_framerate / 2;
3573 stats_proxy_->SetMockStats(stats);
3574 video_stream_encoder_->TriggerCpuNormalUsage();
3575 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3576 EXPECT_EQ(
3577 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3578 kFramerate);
3579
3580 video_stream_encoder_->Stop();
3581}
3582
3583TEST_F(VideoStreamEncoderTest,
3584 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3585 const int kFrameWidth = 1280;
3586 const int kFrameHeight = 720;
3587 const int kLowFramerate = 15;
3588 const int kHighFramerate = 25;
3589
Erik Språng4c6ca302019-04-08 15:14:01 +02003590 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003591 DataRate::BitsPerSec(kTargetBitrateBps),
3592 DataRate::BitsPerSec(kTargetBitrateBps),
3593 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003594 test::FrameForwarder source;
3595 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003596 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003597
3598 // Trigger initial configuration.
3599 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003600 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003601 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3602 video_encoder_config.number_of_streams = 1;
3603 video_encoder_config.video_stream_factory =
3604 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3605 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3606 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003607 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003608 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3609
3610 EXPECT_EQ(
3611 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3612 kLowFramerate);
3613
3614 // Trigger overuse, max framerate should be reduced.
3615 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3616 stats.input_frame_rate = kLowFramerate;
3617 stats_proxy_->SetMockStats(stats);
3618 video_stream_encoder_->TriggerCpuOveruse();
3619 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3620 int adapted_framerate =
3621 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3622 EXPECT_LT(adapted_framerate, kLowFramerate);
3623
3624 // Reconfigure the encoder with a new (higher max framerate), max fps should
3625 // still respect the adaptation.
3626 video_encoder_config.video_stream_factory =
3627 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3628 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3629 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003630 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003631 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3632
3633 EXPECT_EQ(
3634 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3635 adapted_framerate);
3636
3637 // Trigger underuse, max framerate should go back to codec configured fps.
3638 stats = stats_proxy_->GetStats();
3639 stats.input_frame_rate = adapted_framerate;
3640 stats_proxy_->SetMockStats(stats);
3641 video_stream_encoder_->TriggerCpuNormalUsage();
3642 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3643 EXPECT_EQ(
3644 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3645 kHighFramerate);
3646
3647 video_stream_encoder_->Stop();
3648}
3649
mflodmancc3d4422017-08-03 08:27:51 -07003650TEST_F(VideoStreamEncoderTest,
3651 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003652 const int kFrameWidth = 1280;
3653 const int kFrameHeight = 720;
3654 const int kFramerate = 24;
3655
Erik Språng4c6ca302019-04-08 15:14:01 +02003656 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003657 DataRate::BitsPerSec(kTargetBitrateBps),
3658 DataRate::BitsPerSec(kTargetBitrateBps),
3659 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003660 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003661 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003662 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003663
3664 // Trigger initial configuration.
3665 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003666 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003667 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3668 video_encoder_config.number_of_streams = 1;
3669 video_encoder_config.video_stream_factory =
3670 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3671 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003672 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003673 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003674 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003675
Niels Möller7dc26b72017-12-06 10:27:48 +01003676 EXPECT_EQ(
3677 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3678 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003679
3680 // Trigger overuse, max framerate should be reduced.
3681 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3682 stats.input_frame_rate = kFramerate;
3683 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003684 video_stream_encoder_->TriggerCpuOveruse();
3685 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003686 int adapted_framerate =
3687 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003688 EXPECT_LT(adapted_framerate, kFramerate);
3689
3690 // Change degradation preference to not enable framerate scaling. Target
3691 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003692 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003693 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003694 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003695 EXPECT_EQ(
3696 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3697 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003698
mflodmancc3d4422017-08-03 08:27:51 -07003699 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003700}
3701
mflodmancc3d4422017-08-03 08:27:51 -07003702TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003703 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003704 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003705 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3706 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3707 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003708 const int kWidth = 640;
3709 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003710
asaperssonfab67072017-04-04 05:51:49 -07003711 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003712
3713 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003714 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003715
3716 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003717 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003718
sprangc5d62e22017-04-02 23:53:04 -07003719 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003720
asaperssonfab67072017-04-04 05:51:49 -07003721 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003722 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003723 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003724
3725 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003726 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003727
sprangc5d62e22017-04-02 23:53:04 -07003728 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003729
mflodmancc3d4422017-08-03 08:27:51 -07003730 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003731}
3732
mflodmancc3d4422017-08-03 08:27:51 -07003733TEST_F(VideoStreamEncoderTest,
3734 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003735 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003736 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003737 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3738 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3739 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003740 const int kWidth = 640;
3741 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003742
3743 // We expect the n initial frames to get dropped.
3744 int i;
3745 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003746 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003747 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003748 }
3749 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003750 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003751 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003752
3753 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003754 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003755
mflodmancc3d4422017-08-03 08:27:51 -07003756 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003757}
3758
mflodmancc3d4422017-08-03 08:27:51 -07003759TEST_F(VideoStreamEncoderTest,
3760 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003761 const int kWidth = 640;
3762 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003763 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003764 DataRate::BitsPerSec(kLowTargetBitrateBps),
3765 DataRate::BitsPerSec(kLowTargetBitrateBps),
3766 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003767
3768 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003769 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003770 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003771
asaperssonfab67072017-04-04 05:51:49 -07003772 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003773 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003774 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003775
mflodmancc3d4422017-08-03 08:27:51 -07003776 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003777}
3778
mflodmancc3d4422017-08-03 08:27:51 -07003779TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003780 const int kWidth = 640;
3781 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003782 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003783
3784 VideoEncoderConfig video_encoder_config;
3785 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3786 // Make format different, to force recreation of encoder.
3787 video_encoder_config.video_format.parameters["foo"] = "foo";
3788 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003789 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003790 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003791 DataRate::BitsPerSec(kLowTargetBitrateBps),
3792 DataRate::BitsPerSec(kLowTargetBitrateBps),
3793 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003794
kthelgasonb83797b2017-02-14 11:57:25 -08003795 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003796 video_stream_encoder_->SetSource(&video_source_,
3797 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003798
asaperssonfab67072017-04-04 05:51:49 -07003799 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003800 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003801 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003802
mflodmancc3d4422017-08-03 08:27:51 -07003803 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003804 fake_encoder_.SetQualityScaling(true);
3805}
3806
Åsa Persson139f4dc2019-08-02 09:29:58 +02003807TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3808 webrtc::test::ScopedFieldTrials field_trials(
3809 "WebRTC-Video-QualityScalerSettings/"
3810 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3811 // Reset encoder for field trials to take effect.
3812 ConfigureEncoder(video_encoder_config_.Copy());
3813 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3814 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3815 const int kWidth = 640;
3816 const int kHeight = 360;
3817
3818 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003819 DataRate::BitsPerSec(kTargetBitrateBps),
3820 DataRate::BitsPerSec(kTargetBitrateBps),
3821 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003822 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3823 // Frame should not be dropped.
3824 WaitForEncodedFrame(1);
3825
3826 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003827 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3828 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3829 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003830 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3831 // Frame should not be dropped.
3832 WaitForEncodedFrame(2);
3833
3834 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003835 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3836 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3837 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003838 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3839 // Expect to drop this frame, the wait should time out.
3840 ExpectDroppedFrame();
3841
3842 // Expect the sink_wants to specify a scaled frame.
3843 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3844 video_stream_encoder_->Stop();
3845}
3846
Åsa Perssone644a032019-11-08 15:56:00 +01003847TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3848 webrtc::test::ScopedFieldTrials field_trials(
3849 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3850
3851 // Reset encoder for field trials to take effect.
3852 VideoEncoderConfig config = video_encoder_config_.Copy();
3853 config.max_bitrate_bps = kTargetBitrateBps;
3854 ConfigureEncoder(std::move(config));
3855 fake_encoder_.SetQp(kQpLow);
3856
3857 // Enable MAINTAIN_FRAMERATE preference.
3858 AdaptingFrameForwarder source;
3859 source.set_adaptation_enabled(true);
3860 video_stream_encoder_->SetSource(&source,
3861 DegradationPreference::MAINTAIN_FRAMERATE);
3862
3863 // Start at low bitrate.
3864 const int kLowBitrateBps = 200000;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003865 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(kLowBitrateBps),
3866 DataRate::BitsPerSec(kLowBitrateBps),
3867 DataRate::BitsPerSec(kLowBitrateBps),
3868 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003869
3870 // Expect first frame to be dropped and resolution to be limited.
3871 const int kWidth = 1280;
3872 const int kHeight = 720;
3873 const int64_t kFrameIntervalMs = 100;
3874 int64_t timestamp_ms = kFrameIntervalMs;
3875 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3876 ExpectDroppedFrame();
3877 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3878
3879 // Increase bitrate to encoder max.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003880 video_stream_encoder_->OnBitrateUpdated(
3881 DataRate::BitsPerSec(config.max_bitrate_bps),
3882 DataRate::BitsPerSec(config.max_bitrate_bps),
3883 DataRate::BitsPerSec(config.max_bitrate_bps), 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003884
3885 // Insert frames and advance |min_duration_ms|.
3886 for (size_t i = 1; i <= 10; i++) {
3887 timestamp_ms += kFrameIntervalMs;
3888 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3889 WaitForEncodedFrame(timestamp_ms);
3890 }
3891 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3892 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3893
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003894 fake_clock_.AdvanceTime(TimeDelta::Millis(2000));
Åsa Perssone644a032019-11-08 15:56:00 +01003895
3896 // Insert frame should trigger high BW and release quality limitation.
3897 timestamp_ms += kFrameIntervalMs;
3898 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3899 WaitForEncodedFrame(timestamp_ms);
3900 VerifyFpsMaxResolutionMax(source.sink_wants());
3901
3902 // Frame should not be adapted.
3903 timestamp_ms += kFrameIntervalMs;
3904 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3905 WaitForEncodedFrame(kWidth, kHeight);
3906 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3907
3908 video_stream_encoder_->Stop();
3909}
3910
mflodmancc3d4422017-08-03 08:27:51 -07003911TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003912 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3913 const int kTooSmallWidth = 10;
3914 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003915 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003916 DataRate::BitsPerSec(kTargetBitrateBps),
3917 DataRate::BitsPerSec(kTargetBitrateBps),
3918 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003919
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003920 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003921 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003922 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003923 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003924 VerifyNoLimitation(source.sink_wants());
3925 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3926
3927 // Trigger adapt down, too small frame, expect no change.
3928 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003929 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003930 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003931 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003932 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3933 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3934
mflodmancc3d4422017-08-03 08:27:51 -07003935 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003936}
3937
mflodmancc3d4422017-08-03 08:27:51 -07003938TEST_F(VideoStreamEncoderTest,
3939 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003940 const int kTooSmallWidth = 10;
3941 const int kTooSmallHeight = 10;
3942 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003943 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003944 DataRate::BitsPerSec(kTargetBitrateBps),
3945 DataRate::BitsPerSec(kTargetBitrateBps),
3946 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003947
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003948 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003949 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003950 video_stream_encoder_->SetSource(&source,
3951 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003952 VerifyNoLimitation(source.sink_wants());
3953 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3954 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3955
3956 // Trigger adapt down, expect limited framerate.
3957 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003958 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003959 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003960 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3961 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3962 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3963 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3964
3965 // Trigger adapt down, too small frame, expect no change.
3966 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003967 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003968 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003969 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3970 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3971 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3972 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3973
mflodmancc3d4422017-08-03 08:27:51 -07003974 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003975}
3976
mflodmancc3d4422017-08-03 08:27:51 -07003977TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003978 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003979 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003980 DataRate::BitsPerSec(kTargetBitrateBps),
3981 DataRate::BitsPerSec(kTargetBitrateBps),
3982 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003983 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003984 const int kFrameWidth = 1280;
3985 const int kFrameHeight = 720;
3986 video_source_.IncomingCapturedFrame(
3987 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003988 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003989 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003990}
3991
sprangb1ca0732017-02-01 08:38:12 -08003992// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003993TEST_F(VideoStreamEncoderTest,
3994 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003995 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003996 DataRate::BitsPerSec(kTargetBitrateBps),
3997 DataRate::BitsPerSec(kTargetBitrateBps),
3998 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003999
4000 const int kFrameWidth = 1280;
4001 const int kFrameHeight = 720;
4002 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07004003 // requested by
4004 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08004005 video_source_.set_adaptation_enabled(true);
4006
4007 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004008 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004009 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004010
4011 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004012 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08004013 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004014 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004015 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08004016
asaperssonfab67072017-04-04 05:51:49 -07004017 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07004018 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08004019 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004020 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004021 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004022
mflodmancc3d4422017-08-03 08:27:51 -07004023 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08004024}
sprangfe627f32017-03-29 08:24:59 -07004025
mflodmancc3d4422017-08-03 08:27:51 -07004026TEST_F(VideoStreamEncoderTest,
4027 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07004028 const int kFrameWidth = 1280;
4029 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07004030
Erik Språng4c6ca302019-04-08 15:14:01 +02004031 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004032 DataRate::BitsPerSec(kTargetBitrateBps),
4033 DataRate::BitsPerSec(kTargetBitrateBps),
4034 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004035 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004036 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004037 video_source_.set_adaptation_enabled(true);
4038
sprang4847ae62017-06-27 07:06:52 -07004039 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004040
4041 video_source_.IncomingCapturedFrame(
4042 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004043 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004044
4045 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07004046 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004047
4048 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07004049 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004050 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004051 video_source_.IncomingCapturedFrame(
4052 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004053 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004054 }
4055
4056 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07004057 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004058 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004059 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004060 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004061 video_source_.IncomingCapturedFrame(
4062 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004063 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004064 ++num_frames_dropped;
4065 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004066 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004067 }
4068 }
4069
sprang4847ae62017-06-27 07:06:52 -07004070 // Add some slack to account for frames dropped by the frame dropper.
4071 const int kErrorMargin = 1;
4072 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004073 kErrorMargin);
4074
4075 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004076 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004077 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004078 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004079 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004080 video_source_.IncomingCapturedFrame(
4081 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004082 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004083 ++num_frames_dropped;
4084 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004085 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004086 }
4087 }
sprang4847ae62017-06-27 07:06:52 -07004088 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004089 kErrorMargin);
4090
4091 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004092 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004093 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004094 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004095 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004096 video_source_.IncomingCapturedFrame(
4097 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004098 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004099 ++num_frames_dropped;
4100 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004101 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004102 }
4103 }
sprang4847ae62017-06-27 07:06:52 -07004104 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004105 kErrorMargin);
4106
4107 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004108 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004109 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004110 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004111 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004112 video_source_.IncomingCapturedFrame(
4113 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004114 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004115 ++num_frames_dropped;
4116 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004117 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004118 }
4119 }
4120 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4121
mflodmancc3d4422017-08-03 08:27:51 -07004122 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004123}
4124
mflodmancc3d4422017-08-03 08:27:51 -07004125TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004126 const int kFramerateFps = 5;
4127 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004128 const int kFrameWidth = 1280;
4129 const int kFrameHeight = 720;
4130
sprang4847ae62017-06-27 07:06:52 -07004131 // Reconfigure encoder with two temporal layers and screensharing, which will
4132 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004133 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004134
Erik Språng4c6ca302019-04-08 15:14:01 +02004135 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004136 DataRate::BitsPerSec(kTargetBitrateBps),
4137 DataRate::BitsPerSec(kTargetBitrateBps),
4138 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004139 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004140 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004141 video_source_.set_adaptation_enabled(true);
4142
sprang4847ae62017-06-27 07:06:52 -07004143 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004144
4145 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004146 rtc::VideoSinkWants last_wants;
4147 do {
4148 last_wants = video_source_.sink_wants();
4149
sprangc5d62e22017-04-02 23:53:04 -07004150 // Insert frames to get a new fps estimate...
4151 for (int j = 0; j < kFramerateFps; ++j) {
4152 video_source_.IncomingCapturedFrame(
4153 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004154 if (video_source_.last_sent_width()) {
4155 sink_.WaitForEncodedFrame(timestamp_ms);
4156 }
sprangc5d62e22017-04-02 23:53:04 -07004157 timestamp_ms += kFrameIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004158 fake_clock_.AdvanceTime(TimeDelta::Millis(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004159 }
4160 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004161 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004162 } while (video_source_.sink_wants().max_framerate_fps <
4163 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004164
Jonathan Yubc771b72017-12-08 17:04:29 -08004165 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004166
mflodmancc3d4422017-08-03 08:27:51 -07004167 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004168}
asaperssonf7e294d2017-06-13 23:25:22 -07004169
mflodmancc3d4422017-08-03 08:27:51 -07004170TEST_F(VideoStreamEncoderTest,
4171 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004172 const int kWidth = 1280;
4173 const int kHeight = 720;
4174 const int64_t kFrameIntervalMs = 150;
4175 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004176 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004177 DataRate::BitsPerSec(kTargetBitrateBps),
4178 DataRate::BitsPerSec(kTargetBitrateBps),
4179 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004180
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004181 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004182 AdaptingFrameForwarder source;
4183 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004184 video_stream_encoder_->SetSource(&source,
4185 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004186 timestamp_ms += kFrameIntervalMs;
4187 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004188 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004189 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004190 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4191 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4192 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4193
4194 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
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 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4200 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4201 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4202 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4203
4204 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004205 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004206 timestamp_ms += kFrameIntervalMs;
4207 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004208 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004209 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4210 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4211 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4212 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4213
4214 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004215 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004216 timestamp_ms += kFrameIntervalMs;
4217 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004218 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004219 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4220 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4221 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4222 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4223
4224 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004225 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004226 timestamp_ms += kFrameIntervalMs;
4227 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004228 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004229 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4230 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4231 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4232 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4233
4234 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004235 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004236 timestamp_ms += kFrameIntervalMs;
4237 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004238 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004239 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4240 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4241 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4242 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4243
4244 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004245 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004246 timestamp_ms += kFrameIntervalMs;
4247 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004248 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004249 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4250 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4251 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4252 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4253
4254 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004255 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004256 timestamp_ms += kFrameIntervalMs;
4257 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004258 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004259 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4260 rtc::VideoSinkWants last_wants = source.sink_wants();
4261 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4262 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4263 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4264
4265 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004266 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004267 timestamp_ms += kFrameIntervalMs;
4268 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004269 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004270 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4271 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4272 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4273 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4274
4275 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004276 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004277 timestamp_ms += kFrameIntervalMs;
4278 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004279 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004280 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4281 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4282 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4283 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4284
4285 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004286 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004287 timestamp_ms += kFrameIntervalMs;
4288 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004289 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004290 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4291 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4292 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4293 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4294
4295 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004296 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004297 timestamp_ms += kFrameIntervalMs;
4298 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004299 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004300 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4301 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4302 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4303 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4304
4305 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004306 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004307 timestamp_ms += kFrameIntervalMs;
4308 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004309 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004310 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4311 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4312 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4313 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4314
4315 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004316 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004317 timestamp_ms += kFrameIntervalMs;
4318 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004319 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004320 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4321 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4322 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4323 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4324
4325 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004326 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004327 timestamp_ms += kFrameIntervalMs;
4328 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004329 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004330 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4331 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4332 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4333 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4334
Åsa Persson30ab0152019-08-27 12:22:33 +02004335 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004336 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004337 timestamp_ms += kFrameIntervalMs;
4338 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004339 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004340 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004341 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004342 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4343 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4344 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4345
4346 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004347 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004348 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004349 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4350
mflodmancc3d4422017-08-03 08:27:51 -07004351 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004352}
4353
mflodmancc3d4422017-08-03 08:27:51 -07004354TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004355 const int kWidth = 1280;
4356 const int kHeight = 720;
4357 const int64_t kFrameIntervalMs = 150;
4358 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004359 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004360 DataRate::BitsPerSec(kTargetBitrateBps),
4361 DataRate::BitsPerSec(kTargetBitrateBps),
4362 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004363
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004364 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004365 AdaptingFrameForwarder source;
4366 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004367 video_stream_encoder_->SetSource(&source,
4368 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004369 timestamp_ms += kFrameIntervalMs;
4370 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004371 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004372 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004373 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4374 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4375 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4376 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4377 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4378 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4379
4380 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004381 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004382 timestamp_ms += kFrameIntervalMs;
4383 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004384 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004385 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4386 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4387 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4388 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4389 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4390 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4391 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4392
4393 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004394 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004395 timestamp_ms += kFrameIntervalMs;
4396 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004397 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004398 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4399 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4400 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4401 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4402 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4403 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4404 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4405
4406 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004407 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004408 timestamp_ms += kFrameIntervalMs;
4409 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004410 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004411 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4412 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4413 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4414 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4415 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4416 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4417 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4418
4419 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004420 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004421 timestamp_ms += kFrameIntervalMs;
4422 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004423 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004424 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4425 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4426 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4427 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4428 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4429 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4430 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4431
4432 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004433 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004434 timestamp_ms += kFrameIntervalMs;
4435 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004436 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004437 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4438 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4439 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4440 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4441 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4442 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4443 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4444
4445 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004446 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004447 timestamp_ms += kFrameIntervalMs;
4448 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004449 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004450 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004451 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004452 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4453 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4454 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4455 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4456 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4457 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4458
4459 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004460 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004461 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004462 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4463 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4464
mflodmancc3d4422017-08-03 08:27:51 -07004465 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004466}
4467
mflodmancc3d4422017-08-03 08:27:51 -07004468TEST_F(VideoStreamEncoderTest,
4469 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004470 const int kWidth = 640;
4471 const int kHeight = 360;
4472 const int kFpsLimit = 15;
4473 const int64_t kFrameIntervalMs = 150;
4474 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004475 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004476 DataRate::BitsPerSec(kTargetBitrateBps),
4477 DataRate::BitsPerSec(kTargetBitrateBps),
4478 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004479
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004480 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004481 AdaptingFrameForwarder source;
4482 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004483 video_stream_encoder_->SetSource(&source,
4484 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004485 timestamp_ms += kFrameIntervalMs;
4486 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004487 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004488 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004489 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4490 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4491 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4492 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4493 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4494 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4495
4496 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004497 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004498 timestamp_ms += kFrameIntervalMs;
4499 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004500 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004501 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4502 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4503 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4504 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4505 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4506 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4507 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4508
4509 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004510 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004511 timestamp_ms += kFrameIntervalMs;
4512 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004513 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004514 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4515 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4516 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4517 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4518 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4519 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4520 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4521
4522 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004523 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004524 timestamp_ms += kFrameIntervalMs;
4525 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004526 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004527 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4528 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4529 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4530 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4531 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4532 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4533 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4534
4535 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004536 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004537 timestamp_ms += kFrameIntervalMs;
4538 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004539 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004540 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004541 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4542 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4543 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4544 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4545 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4546 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4547
4548 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004549 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004550 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004551 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4552 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4553
mflodmancc3d4422017-08-03 08:27:51 -07004554 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004555}
4556
mflodmancc3d4422017-08-03 08:27:51 -07004557TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004558 const int kFrameWidth = 1920;
4559 const int kFrameHeight = 1080;
4560 // 3/4 of 1920.
4561 const int kAdaptedFrameWidth = 1440;
4562 // 3/4 of 1080 rounded down to multiple of 4.
4563 const int kAdaptedFrameHeight = 808;
4564 const int kFramerate = 24;
4565
Erik Språng4c6ca302019-04-08 15:14:01 +02004566 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004567 DataRate::BitsPerSec(kTargetBitrateBps),
4568 DataRate::BitsPerSec(kTargetBitrateBps),
4569 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004570 // Trigger reconfigure encoder (without resetting the entire instance).
4571 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004572 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004573 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4574 video_encoder_config.number_of_streams = 1;
4575 video_encoder_config.video_stream_factory =
4576 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004577 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004578 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004579 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004580
4581 video_source_.set_adaptation_enabled(true);
4582
4583 video_source_.IncomingCapturedFrame(
4584 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004585 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004586
4587 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004588 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004589 video_source_.IncomingCapturedFrame(
4590 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004591 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004592
mflodmancc3d4422017-08-03 08:27:51 -07004593 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004594}
4595
mflodmancc3d4422017-08-03 08:27:51 -07004596TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004597 const int kFrameWidth = 1280;
4598 const int kFrameHeight = 720;
4599 const int kLowFps = 2;
4600 const int kHighFps = 30;
4601
Erik Språng4c6ca302019-04-08 15:14:01 +02004602 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004603 DataRate::BitsPerSec(kTargetBitrateBps),
4604 DataRate::BitsPerSec(kTargetBitrateBps),
4605 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004606
4607 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4608 max_framerate_ = kLowFps;
4609
4610 // Insert 2 seconds of 2fps video.
4611 for (int i = 0; i < kLowFps * 2; ++i) {
4612 video_source_.IncomingCapturedFrame(
4613 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4614 WaitForEncodedFrame(timestamp_ms);
4615 timestamp_ms += 1000 / kLowFps;
4616 }
4617
4618 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004619 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004620 DataRate::BitsPerSec(kTargetBitrateBps),
4621 DataRate::BitsPerSec(kTargetBitrateBps),
4622 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004623 video_source_.IncomingCapturedFrame(
4624 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4625 WaitForEncodedFrame(timestamp_ms);
4626 timestamp_ms += 1000 / kLowFps;
4627
4628 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4629
4630 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004631 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004632 const int kFrameIntervalMs = 1000 / kHighFps;
4633 max_framerate_ = kHighFps;
4634 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4635 video_source_.IncomingCapturedFrame(
4636 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4637 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4638 // be dropped if the encoder hans't been updated with the new higher target
4639 // framerate yet, causing it to overshoot the target bitrate and then
4640 // suffering the wrath of the media optimizer.
4641 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4642 timestamp_ms += kFrameIntervalMs;
4643 }
4644
4645 // Don expect correct measurement just yet, but it should be higher than
4646 // before.
4647 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4648
mflodmancc3d4422017-08-03 08:27:51 -07004649 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004650}
4651
mflodmancc3d4422017-08-03 08:27:51 -07004652TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004653 const int kFrameWidth = 1280;
4654 const int kFrameHeight = 720;
4655 const int kTargetBitrateBps = 1000000;
4656
4657 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004658 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004659 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004660 DataRate::BitsPerSec(kTargetBitrateBps),
4661 DataRate::BitsPerSec(kTargetBitrateBps),
4662 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004663 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004664
4665 // Insert a first video frame, causes another bitrate update.
4666 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4667 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4668 video_source_.IncomingCapturedFrame(
4669 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4670 WaitForEncodedFrame(timestamp_ms);
4671
4672 // Next, simulate video suspension due to pacer queue overrun.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004673 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
4674 DataRate::BitsPerSec(0),
4675 DataRate::BitsPerSec(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004676
4677 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004678 timestamp_ms += kProcessIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004679 fake_clock_.AdvanceTime(TimeDelta::Millis(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004680
4681 // Bitrate observer should not be called.
4682 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4683 video_source_.IncomingCapturedFrame(
4684 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4685 ExpectDroppedFrame();
4686
mflodmancc3d4422017-08-03 08:27:51 -07004687 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004688}
ilnik6b826ef2017-06-16 06:53:48 -07004689
Niels Möller4db138e2018-04-19 09:04:13 +02004690TEST_F(VideoStreamEncoderTest,
4691 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4692 const int kFrameWidth = 1280;
4693 const int kFrameHeight = 720;
4694 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004695 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004696 DataRate::BitsPerSec(kTargetBitrateBps),
4697 DataRate::BitsPerSec(kTargetBitrateBps),
4698 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004699 video_source_.IncomingCapturedFrame(
4700 CreateFrame(1, kFrameWidth, kFrameHeight));
4701 WaitForEncodedFrame(1);
4702 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4703 .low_encode_usage_threshold_percent,
4704 default_options.low_encode_usage_threshold_percent);
4705 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4706 .high_encode_usage_threshold_percent,
4707 default_options.high_encode_usage_threshold_percent);
4708 video_stream_encoder_->Stop();
4709}
4710
4711TEST_F(VideoStreamEncoderTest,
4712 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4713 const int kFrameWidth = 1280;
4714 const int kFrameHeight = 720;
4715 CpuOveruseOptions hardware_options;
4716 hardware_options.low_encode_usage_threshold_percent = 150;
4717 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004718 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004719
Erik Språng4c6ca302019-04-08 15:14:01 +02004720 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004721 DataRate::BitsPerSec(kTargetBitrateBps),
4722 DataRate::BitsPerSec(kTargetBitrateBps),
4723 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004724 video_source_.IncomingCapturedFrame(
4725 CreateFrame(1, kFrameWidth, kFrameHeight));
4726 WaitForEncodedFrame(1);
4727 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4728 .low_encode_usage_threshold_percent,
4729 hardware_options.low_encode_usage_threshold_percent);
4730 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4731 .high_encode_usage_threshold_percent,
4732 hardware_options.high_encode_usage_threshold_percent);
4733 video_stream_encoder_->Stop();
4734}
4735
Niels Möller6bb5ab92019-01-11 11:11:10 +01004736TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4737 const int kFrameWidth = 320;
4738 const int kFrameHeight = 240;
4739 const int kFps = 30;
4740 const int kTargetBitrateBps = 120000;
4741 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4742
Erik Språng4c6ca302019-04-08 15:14:01 +02004743 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004744 DataRate::BitsPerSec(kTargetBitrateBps),
4745 DataRate::BitsPerSec(kTargetBitrateBps),
4746 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004747
4748 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4749 max_framerate_ = kFps;
4750
4751 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4752 fake_encoder_.SimulateOvershoot(1.0);
4753 int num_dropped = 0;
4754 for (int i = 0; i < kNumFramesInRun; ++i) {
4755 video_source_.IncomingCapturedFrame(
4756 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4757 // Wait up to two frame durations for a frame to arrive.
4758 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4759 ++num_dropped;
4760 }
4761 timestamp_ms += 1000 / kFps;
4762 }
4763
Erik Språnga8d48ab2019-02-08 14:17:40 +01004764 // Framerate should be measured to be near the expected target rate.
4765 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4766
4767 // Frame drops should be within 5% of expected 0%.
4768 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004769
4770 // Make encoder produce frames at double the expected bitrate during 3 seconds
4771 // of video, verify number of drops. Rate needs to be slightly changed in
4772 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004773 double overshoot_factor = 2.0;
4774 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4775 // With bitrate adjuster, when need to overshoot even more to trigger
4776 // frame dropping.
4777 overshoot_factor *= 2;
4778 }
4779 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004780 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004781 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4782 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4783 DataRate::BitsPerSec(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004784 num_dropped = 0;
4785 for (int i = 0; i < kNumFramesInRun; ++i) {
4786 video_source_.IncomingCapturedFrame(
4787 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4788 // Wait up to two frame durations for a frame to arrive.
4789 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4790 ++num_dropped;
4791 }
4792 timestamp_ms += 1000 / kFps;
4793 }
4794
Erik Språng4c6ca302019-04-08 15:14:01 +02004795 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004796 DataRate::BitsPerSec(kTargetBitrateBps),
4797 DataRate::BitsPerSec(kTargetBitrateBps),
4798 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004799
4800 // Target framerate should be still be near the expected target, despite
4801 // the frame drops.
4802 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4803
4804 // Frame drops should be within 5% of expected 50%.
4805 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004806
4807 video_stream_encoder_->Stop();
4808}
4809
4810TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4811 const int kFrameWidth = 320;
4812 const int kFrameHeight = 240;
4813 const int kActualInputFps = 24;
4814 const int kTargetBitrateBps = 120000;
4815
4816 ASSERT_GT(max_framerate_, kActualInputFps);
4817
4818 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4819 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004820 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004821 DataRate::BitsPerSec(kTargetBitrateBps),
4822 DataRate::BitsPerSec(kTargetBitrateBps),
4823 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004824
4825 // Insert 3 seconds of video, with an input fps lower than configured max.
4826 for (int i = 0; i < kActualInputFps * 3; ++i) {
4827 video_source_.IncomingCapturedFrame(
4828 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4829 // Wait up to two frame durations for a frame to arrive.
4830 WaitForEncodedFrame(timestamp_ms);
4831 timestamp_ms += 1000 / kActualInputFps;
4832 }
4833
4834 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4835
4836 video_stream_encoder_->Stop();
4837}
4838
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004839TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4840 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004841 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004842 DataRate::BitsPerSec(kTargetBitrateBps),
4843 DataRate::BitsPerSec(kTargetBitrateBps),
4844 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004845
4846 fake_encoder_.BlockNextEncode();
4847 video_source_.IncomingCapturedFrame(
4848 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4849 WaitForEncodedFrame(1);
4850 // On the very first frame full update should be forced.
4851 rect = fake_encoder_.GetLastUpdateRect();
4852 EXPECT_EQ(rect.offset_x, 0);
4853 EXPECT_EQ(rect.offset_y, 0);
4854 EXPECT_EQ(rect.height, codec_height_);
4855 EXPECT_EQ(rect.width, codec_width_);
4856 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4857 // call to ContinueEncode.
4858 video_source_.IncomingCapturedFrame(
4859 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4860 ExpectDroppedFrame();
4861 video_source_.IncomingCapturedFrame(
4862 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4863 ExpectDroppedFrame();
4864 fake_encoder_.ContinueEncode();
4865 WaitForEncodedFrame(3);
4866 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4867 rect = fake_encoder_.GetLastUpdateRect();
4868 EXPECT_EQ(rect.offset_x, 1);
4869 EXPECT_EQ(rect.offset_y, 0);
4870 EXPECT_EQ(rect.width, 10);
4871 EXPECT_EQ(rect.height, 1);
4872
4873 video_source_.IncomingCapturedFrame(
4874 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4875 WaitForEncodedFrame(4);
4876 // Previous frame was encoded, so no accumulation should happen.
4877 rect = fake_encoder_.GetLastUpdateRect();
4878 EXPECT_EQ(rect.offset_x, 0);
4879 EXPECT_EQ(rect.offset_y, 0);
4880 EXPECT_EQ(rect.width, 1);
4881 EXPECT_EQ(rect.height, 1);
4882
4883 video_stream_encoder_->Stop();
4884}
4885
Erik Språngd7329ca2019-02-21 21:19:53 +01004886TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004887 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004888 DataRate::BitsPerSec(kTargetBitrateBps),
4889 DataRate::BitsPerSec(kTargetBitrateBps),
4890 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004891
4892 // First frame is always keyframe.
4893 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4894 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004895 EXPECT_THAT(
4896 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004897 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004898
4899 // Insert delta frame.
4900 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4901 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004902 EXPECT_THAT(
4903 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004904 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004905
4906 // Request next frame be a key-frame.
4907 video_stream_encoder_->SendKeyFrame();
4908 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4909 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004910 EXPECT_THAT(
4911 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004912 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004913
4914 video_stream_encoder_->Stop();
4915}
4916
4917TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4918 // Setup simulcast with three streams.
4919 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004920 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004921 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
4922 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
4923 DataRate::BitsPerSec(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004924 // Wait for all three layers before triggering event.
4925 sink_.SetNumExpectedLayers(3);
4926
4927 // First frame is always keyframe.
4928 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4929 WaitForEncodedFrame(1);
4930 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004931 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4932 VideoFrameType::kVideoFrameKey,
4933 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004934
4935 // Insert delta frame.
4936 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4937 WaitForEncodedFrame(2);
4938 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004939 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4940 VideoFrameType::kVideoFrameDelta,
4941 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004942
4943 // Request next frame be a key-frame.
4944 // Only first stream is configured to produce key-frame.
4945 video_stream_encoder_->SendKeyFrame();
4946 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4947 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004948
4949 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4950 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004951 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004952 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004953 VideoFrameType::kVideoFrameKey,
4954 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004955
4956 video_stream_encoder_->Stop();
4957}
4958
4959TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4960 // Configure internal source factory and setup test again.
4961 encoder_factory_.SetHasInternalSource(true);
4962 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004963 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004964 DataRate::BitsPerSec(kTargetBitrateBps),
4965 DataRate::BitsPerSec(kTargetBitrateBps),
4966 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004967
4968 // Call encoder directly, simulating internal source where encoded frame
4969 // callback in VideoStreamEncoder is called despite no OnFrame().
4970 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4971 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004972 EXPECT_THAT(
4973 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004974 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004975
Niels Möller8f7ce222019-03-21 15:43:58 +01004976 const std::vector<VideoFrameType> kDeltaFrame = {
4977 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004978 // Need to set timestamp manually since manually for injected frame.
4979 VideoFrame frame = CreateFrame(101, nullptr);
4980 frame.set_timestamp(101);
4981 fake_encoder_.InjectFrame(frame, false);
4982 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004983 EXPECT_THAT(
4984 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004985 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004986
4987 // Request key-frame. The forces a dummy frame down into the encoder.
4988 fake_encoder_.ExpectNullFrame();
4989 video_stream_encoder_->SendKeyFrame();
4990 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004991 EXPECT_THAT(
4992 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004993 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004994
4995 video_stream_encoder_->Stop();
4996}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004997
4998TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4999 // Configure internal source factory and setup test again.
5000 encoder_factory_.SetHasInternalSource(true);
5001 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005002 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005003 DataRate::BitsPerSec(kTargetBitrateBps),
5004 DataRate::BitsPerSec(kTargetBitrateBps),
5005 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01005006
5007 int64_t timestamp = 1;
5008 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02005009 image.SetEncodedData(
5010 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01005011 image.capture_time_ms_ = ++timestamp;
5012 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
5013 const int64_t kEncodeFinishDelayMs = 10;
5014 image.timing_.encode_start_ms = timestamp;
5015 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
5016 fake_encoder_.InjectEncodedImage(image);
5017 // Wait for frame without incrementing clock.
5018 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5019 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
5020 // capture timestamp should be kEncodeFinishDelayMs in the past.
5021 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
5022 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
5023 kEncodeFinishDelayMs);
5024
5025 video_stream_encoder_->Stop();
5026}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02005027
5028TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
5029 // Configure internal source factory and setup test again.
5030 encoder_factory_.SetHasInternalSource(true);
5031 ResetEncoder("H264", 1, 1, 1, false);
5032
5033 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
5034 image._frameType = VideoFrameType::kVideoFrameKey;
5035
5036 CodecSpecificInfo codec_specific_info;
5037 codec_specific_info.codecType = kVideoCodecH264;
5038
5039 RTPFragmentationHeader fragmentation;
5040 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5041 fragmentation.fragmentationOffset[0] = 4;
5042 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
5043
5044 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5045 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5046
5047 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5048 testing::ElementsAreArray(optimal_sps));
5049 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5050 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5051 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5052 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5053
5054 video_stream_encoder_->Stop();
5055}
5056
5057TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
5058 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
5059 0x00, 0x00, 0x03, 0x03, 0xF4,
5060 0x05, 0x03, 0xC7, 0xC0};
5061
5062 // Configure internal source factory and setup test again.
5063 encoder_factory_.SetHasInternalSource(true);
5064 ResetEncoder("H264", 1, 1, 1, false);
5065
5066 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
5067 image._frameType = VideoFrameType::kVideoFrameKey;
5068
5069 CodecSpecificInfo codec_specific_info;
5070 codec_specific_info.codecType = kVideoCodecH264;
5071
5072 RTPFragmentationHeader fragmentation;
5073 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5074 fragmentation.fragmentationOffset[0] = 4;
5075 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
5076
5077 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5078 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5079
5080 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5081 testing::ElementsAreArray(optimal_sps));
5082 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5083 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5084 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5085 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5086
5087 video_stream_encoder_->Stop();
5088}
5089
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005090TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5091 const int kFrameWidth = 1280;
5092 const int kFrameHeight = 720;
5093 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5094
5095 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005096 DataRate::BitsPerSec(kTargetBitrateBps),
5097 DataRate::BitsPerSec(kTargetBitrateBps),
5098 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005099 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5100
5101 // Insert a first video frame. It should be dropped because of downscale in
5102 // resolution.
5103 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5104 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5105 frame.set_rotation(kVideoRotation_270);
5106 video_source_.IncomingCapturedFrame(frame);
5107
5108 ExpectDroppedFrame();
5109
5110 // Second frame is downscaled.
5111 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5112 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5113 frame.set_rotation(kVideoRotation_90);
5114 video_source_.IncomingCapturedFrame(frame);
5115
5116 WaitForEncodedFrame(timestamp_ms);
5117 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5118
5119 // Insert another frame, also downscaled.
5120 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5121 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5122 frame.set_rotation(kVideoRotation_180);
5123 video_source_.IncomingCapturedFrame(frame);
5124
5125 WaitForEncodedFrame(timestamp_ms);
5126 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5127
5128 video_stream_encoder_->Stop();
5129}
5130
Erik Språng5056af02019-09-02 15:53:11 +02005131TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5132 const int kFrameWidth = 320;
5133 const int kFrameHeight = 180;
5134
5135 // Initial rate.
5136 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005137 /*target_bitrate=*/DataRate::KilobitsPerSec(300),
5138 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(300),
5139 /*link_allocation=*/DataRate::KilobitsPerSec(300),
Erik Språng5056af02019-09-02 15:53:11 +02005140 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005141 /*rtt_ms=*/0,
5142 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005143
5144 // Insert a first video frame so that encoder gets configured.
5145 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5146 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5147 frame.set_rotation(kVideoRotation_270);
5148 video_source_.IncomingCapturedFrame(frame);
5149 WaitForEncodedFrame(timestamp_ms);
5150
5151 // Set a target rate below the minimum allowed by the codec settings.
5152 VideoCodec codec_config = fake_encoder_.codec_config();
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005153 DataRate min_rate = DataRate::KilobitsPerSec(codec_config.minBitrate);
5154 DataRate target_rate = min_rate - DataRate::KilobitsPerSec(1);
Erik Språng5056af02019-09-02 15:53:11 +02005155 video_stream_encoder_->OnBitrateUpdated(
5156 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005157 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005158 /*link_allocation=*/target_rate,
5159 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005160 /*rtt_ms=*/0,
5161 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005162 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5163
5164 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5165 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5166 ASSERT_TRUE(rate_settings.has_value());
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005167 DataRate allocation_sum =
5168 DataRate::BitsPerSec(rate_settings->bitrate.get_sum_bps());
Erik Språng5056af02019-09-02 15:53:11 +02005169 EXPECT_EQ(min_rate, allocation_sum);
5170 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5171
5172 video_stream_encoder_->Stop();
5173}
5174
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005175TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005176 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005177 DataRate::BitsPerSec(kTargetBitrateBps),
5178 DataRate::BitsPerSec(kTargetBitrateBps),
5179 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005180 // Capture a frame and wait for it to synchronize with the encoder thread.
5181 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5182 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5183 WaitForEncodedFrame(1);
5184
5185 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5186 ASSERT_TRUE(prev_rate_settings.has_value());
5187 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5188 kDefaultFramerate);
5189
5190 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5191 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5192 timestamp_ms += 1000 / kDefaultFramerate;
5193 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5194 WaitForEncodedFrame(timestamp_ms);
5195 }
5196 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5197 kDefaultFramerate);
5198 // Capture larger frame to trigger a reconfigure.
5199 codec_height_ *= 2;
5200 codec_width_ *= 2;
5201 timestamp_ms += 1000 / kDefaultFramerate;
5202 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5203 WaitForEncodedFrame(timestamp_ms);
5204
5205 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5206 auto current_rate_settings =
5207 fake_encoder_.GetAndResetLastRateControlSettings();
5208 // Ensure we have actually reconfigured twice
5209 // The rate settings should have been set again even though
5210 // they haven't changed.
5211 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005212 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005213
5214 video_stream_encoder_->Stop();
5215}
5216
philipeld9cc8c02019-09-16 14:53:40 +02005217struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5218 MOCK_METHOD0(RequestEncoderFallback, void());
5219 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
philipel9b058032020-02-10 11:30:00 +01005220 MOCK_METHOD1(RequestEncoderSwitch,
5221 void(const webrtc::SdpVideoFormat& format));
philipeld9cc8c02019-09-16 14:53:40 +02005222};
5223
5224TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5225 constexpr int kDontCare = 100;
5226
5227 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5228 video_send_config_.encoder_settings.encoder_switch_request_callback =
5229 &switch_callback;
5230 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5231 encoder_config.codec_type = kVideoCodecVP8;
5232 webrtc::test::ScopedFieldTrials field_trial(
5233 "WebRTC-NetworkCondition-EncoderSwitch/"
5234 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5235 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5236
5237 // Reset encoder for new configuration to take effect.
5238 ConfigureEncoder(std::move(encoder_config));
5239
5240 // Send one frame to trigger ReconfigureEncoder.
5241 video_source_.IncomingCapturedFrame(
5242 CreateFrame(kDontCare, kDontCare, kDontCare));
5243
5244 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005245 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5246 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005247 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005248 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005249
5250 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005251 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5252 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5253 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipeld9cc8c02019-09-16 14:53:40 +02005254 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005255 /*rtt_ms=*/0,
5256 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005257
5258 video_stream_encoder_->Stop();
5259}
5260
5261TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5262 constexpr int kSufficientBitrateToNotDrop = 1000;
5263 constexpr int kHighRes = 500;
5264 constexpr int kLowRes = 100;
5265
5266 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5267 video_send_config_.encoder_settings.encoder_switch_request_callback =
5268 &switch_callback;
5269 webrtc::test::ScopedFieldTrials field_trial(
5270 "WebRTC-NetworkCondition-EncoderSwitch/"
5271 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5272 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5273 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5274 encoder_config.codec_type = kVideoCodecH264;
5275
5276 // Reset encoder for new configuration to take effect.
5277 ConfigureEncoder(std::move(encoder_config));
5278
5279 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5280 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5281 // not fail.
5282 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005283 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5284 /*stable_target_bitrate=*/
5285 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5286 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipeld9cc8c02019-09-16 14:53:40 +02005287 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005288 /*rtt_ms=*/0,
5289 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005290
5291 // Send one frame to trigger ReconfigureEncoder.
5292 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5293 WaitForEncodedFrame(1);
5294
5295 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005296 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5297 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005298 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005299 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005300
5301 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5302 WaitForEncodedFrame(2);
5303
5304 video_stream_encoder_->Stop();
5305}
5306
philipel9b058032020-02-10 11:30:00 +01005307TEST_F(VideoStreamEncoderTest, EncoderSelectorCurrentEncoderIsSignaled) {
5308 constexpr int kDontCare = 100;
5309 StrictMock<MockEncoderSelector> encoder_selector;
5310 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5311 &fake_encoder_, &encoder_selector);
5312 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5313
5314 // Reset encoder for new configuration to take effect.
5315 ConfigureEncoder(video_encoder_config_.Copy());
5316
5317 EXPECT_CALL(encoder_selector, OnCurrentEncoder(_));
5318
5319 video_source_.IncomingCapturedFrame(
5320 CreateFrame(kDontCare, kDontCare, kDontCare));
5321 video_stream_encoder_->Stop();
5322
5323 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5324 // to it's factory, so in order for the encoder instance in the
5325 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5326 // reset the |video_stream_encoder_| here.
5327 video_stream_encoder_.reset();
5328}
5329
5330TEST_F(VideoStreamEncoderTest, EncoderSelectorBitrateSwitch) {
5331 constexpr int kDontCare = 100;
5332
5333 NiceMock<MockEncoderSelector> encoder_selector;
5334 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5335 video_send_config_.encoder_settings.encoder_switch_request_callback =
5336 &switch_callback;
5337 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5338 &fake_encoder_, &encoder_selector);
5339 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5340
5341 // Reset encoder for new configuration to take effect.
5342 ConfigureEncoder(video_encoder_config_.Copy());
5343
5344 ON_CALL(encoder_selector, OnEncodingBitrate(_))
5345 .WillByDefault(Return(SdpVideoFormat("AV1")));
5346 EXPECT_CALL(switch_callback,
5347 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(
5348 Field(&SdpVideoFormat::name, "AV1"))));
5349
5350 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005351 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5352 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5353 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipel9b058032020-02-10 11:30:00 +01005354 /*fraction_lost=*/0,
5355 /*rtt_ms=*/0,
5356 /*cwnd_reduce_ratio=*/0);
5357
5358 video_stream_encoder_->Stop();
5359}
5360
5361TEST_F(VideoStreamEncoderTest, EncoderSelectorBrokenEncoderSwitch) {
5362 constexpr int kSufficientBitrateToNotDrop = 1000;
5363 constexpr int kDontCare = 100;
5364
5365 NiceMock<MockVideoEncoder> video_encoder;
5366 NiceMock<MockEncoderSelector> encoder_selector;
5367 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5368 video_send_config_.encoder_settings.encoder_switch_request_callback =
5369 &switch_callback;
5370 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5371 &video_encoder, &encoder_selector);
5372 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5373
5374 // Reset encoder for new configuration to take effect.
5375 ConfigureEncoder(video_encoder_config_.Copy());
5376
5377 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5378 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5379 // not fail.
5380 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005381 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5382 /*stable_target_bitrate=*/
5383 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5384 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipel9b058032020-02-10 11:30:00 +01005385 /*fraction_lost=*/0,
5386 /*rtt_ms=*/0,
5387 /*cwnd_reduce_ratio=*/0);
5388
5389 ON_CALL(video_encoder, Encode(_, _))
5390 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ENCODER_FAILURE));
5391 ON_CALL(encoder_selector, OnEncoderBroken())
5392 .WillByDefault(Return(SdpVideoFormat("AV2")));
5393
5394 rtc::Event encode_attempted;
5395 EXPECT_CALL(switch_callback,
5396 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(_)))
5397 .WillOnce([&encode_attempted](const SdpVideoFormat& format) {
5398 EXPECT_EQ(format.name, "AV2");
5399 encode_attempted.Set();
5400 });
5401
5402 video_source_.IncomingCapturedFrame(CreateFrame(1, kDontCare, kDontCare));
5403 encode_attempted.Wait(3000);
5404
5405 video_stream_encoder_->Stop();
5406
5407 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5408 // to it's factory, so in order for the encoder instance in the
5409 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5410 // reset the |video_stream_encoder_| here.
5411 video_stream_encoder_.reset();
5412}
5413
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005414TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005415 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005416 const int kFrameWidth = 320;
5417 const int kFrameHeight = 180;
5418
5419 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005420 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005421 video_stream_encoder_->OnBitrateUpdated(
5422 /*target_bitrate=*/rate,
5423 /*stable_target_bitrate=*/rate,
5424 /*link_allocation=*/rate,
5425 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005426 /*rtt_ms=*/0,
5427 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005428
5429 // Insert a first video frame so that encoder gets configured.
5430 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5431 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5432 frame.set_rotation(kVideoRotation_270);
5433 video_source_.IncomingCapturedFrame(frame);
5434 WaitForEncodedFrame(timestamp_ms);
5435 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5436
5437 // Change of target bitrate propagates to the encoder.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005438 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005439 video_stream_encoder_->OnBitrateUpdated(
5440 /*target_bitrate=*/new_stable_rate,
5441 /*stable_target_bitrate=*/new_stable_rate,
5442 /*link_allocation=*/rate,
5443 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005444 /*rtt_ms=*/0,
5445 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005446 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5447 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5448 video_stream_encoder_->Stop();
5449}
5450
5451TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005452 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005453 const int kFrameWidth = 320;
5454 const int kFrameHeight = 180;
5455
5456 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005457 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005458 video_stream_encoder_->OnBitrateUpdated(
5459 /*target_bitrate=*/rate,
5460 /*stable_target_bitrate=*/rate,
5461 /*link_allocation=*/rate,
5462 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005463 /*rtt_ms=*/0,
5464 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005465
5466 // Insert a first video frame so that encoder gets configured.
5467 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5468 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5469 frame.set_rotation(kVideoRotation_270);
5470 video_source_.IncomingCapturedFrame(frame);
5471 WaitForEncodedFrame(timestamp_ms);
5472 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5473
5474 // Set a higher target rate without changing the link_allocation. Should not
5475 // reset encoder's rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005476 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005477 video_stream_encoder_->OnBitrateUpdated(
5478 /*target_bitrate=*/rate,
5479 /*stable_target_bitrate=*/new_stable_rate,
5480 /*link_allocation=*/rate,
5481 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005482 /*rtt_ms=*/0,
5483 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005484 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5485 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5486 video_stream_encoder_->Stop();
5487}
5488
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005489TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5490 test::ScopedFieldTrials field_trials(
5491 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5492 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5493 const int kFramerateFps = 30;
5494 const int kWidth = 1920;
5495 const int kHeight = 1080;
5496 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5497 // Works on screenshare mode.
5498 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5499 // We rely on the automatic resolution adaptation, but we handle framerate
5500 // adaptation manually by mocking the stats proxy.
5501 video_source_.set_adaptation_enabled(true);
5502
5503 // BALANCED degradation preference is required for this feature.
5504 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005505 DataRate::BitsPerSec(kTargetBitrateBps),
5506 DataRate::BitsPerSec(kTargetBitrateBps),
5507 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005508 video_stream_encoder_->SetSource(&video_source_,
5509 webrtc::DegradationPreference::BALANCED);
5510 VerifyNoLimitation(video_source_.sink_wants());
5511
5512 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5513 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5514
5515 // Pass enough frames with the full update to trigger animation detection.
5516 for (int i = 0; i < kNumFrames; ++i) {
5517 int64_t timestamp_ms =
5518 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5519 frame.set_ntp_time_ms(timestamp_ms);
5520 frame.set_timestamp_us(timestamp_ms * 1000);
5521 video_source_.IncomingCapturedFrame(frame);
5522 WaitForEncodedFrame(timestamp_ms);
5523 }
5524
5525 // Resolution should be limited.
5526 rtc::VideoSinkWants expected;
5527 expected.max_framerate_fps = kFramerateFps;
5528 expected.max_pixel_count = 1280 * 720 + 1;
5529 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5530
5531 // Pass one frame with no known update.
5532 // Resolution cap should be removed immediately.
5533 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5534 frame.set_ntp_time_ms(timestamp_ms);
5535 frame.set_timestamp_us(timestamp_ms * 1000);
5536 frame.clear_update_rect();
5537
5538 video_source_.IncomingCapturedFrame(frame);
5539 WaitForEncodedFrame(timestamp_ms);
5540
5541 // Resolution should be unlimited now.
5542 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5543
5544 video_stream_encoder_->Stop();
5545}
5546
perkj26091b12016-09-01 01:17:40 -07005547} // namespace webrtc