blob: bde799b8d39b2b965600a7a069f09d4c7b59522c [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010033#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010037#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020038#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "system_wrappers/include/sleep.h"
40#include "test/encoder_settings.h"
41#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020042#include "test/field_trial.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010043#include "test/frame_forwarder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gmock.h"
45#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020046#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070048
49namespace webrtc {
50
sprangb1ca0732017-02-01 08:38:12 -080051using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080052using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020053using ::testing::AllOf;
54using ::testing::Field;
philipel9b058032020-02-10 11:30:00 +010055using ::testing::Matcher;
56using ::testing::NiceMock;
57using ::testing::Return;
philipeld9cc8c02019-09-16 14:53:40 +020058using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080059
perkj803d97f2016-11-01 11:45:46 -070060namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020061const int kMinPixelsPerFrame = 320 * 180;
Åsa Perssone644a032019-11-08 15:56:00 +010062const int kQpLow = 1;
63const int kQpHigh = 2;
Åsa Persson8c1bf952018-09-13 10:42:19 +020064const int kMinFramerateFps = 2;
65const int kMinBalancedFramerateFps = 7;
66const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080067const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010068const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020069const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010070const uint32_t kSimulcastTargetBitrateBps = 3150000;
71const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080072const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070073const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020074const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020075const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020076const VideoEncoder::ResolutionBitrateLimits
77 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
78const VideoEncoder::ResolutionBitrateLimits
79 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080080
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020081uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
82 0x00, 0x00, 0x03, 0x03, 0xF4,
83 0x05, 0x03, 0xC7, 0xE0, 0x1B,
84 0x41, 0x10, 0x8D, 0x00};
85
perkj803d97f2016-11-01 11:45:46 -070086class TestBuffer : public webrtc::I420Buffer {
87 public:
88 TestBuffer(rtc::Event* event, int width, int height)
89 : I420Buffer(width, height), event_(event) {}
90
91 private:
92 friend class rtc::RefCountedObject<TestBuffer>;
93 ~TestBuffer() override {
94 if (event_)
95 event_->Set();
96 }
97 rtc::Event* const event_;
98};
99
Noah Richards51db4212019-06-12 06:59:12 -0700100// A fake native buffer that can't be converted to I420.
101class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
102 public:
103 FakeNativeBuffer(rtc::Event* event, int width, int height)
104 : event_(event), width_(width), height_(height) {}
105 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
106 int width() const override { return width_; }
107 int height() const override { return height_; }
108 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
109 return nullptr;
110 }
111
112 private:
113 friend class rtc::RefCountedObject<FakeNativeBuffer>;
114 ~FakeNativeBuffer() override {
115 if (event_)
116 event_->Set();
117 }
118 rtc::Event* const event_;
119 const int width_;
120 const int height_;
121};
122
Niels Möller7dc26b72017-12-06 10:27:48 +0100123class CpuOveruseDetectorProxy : public OveruseFrameDetector {
124 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200125 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
126 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100127 last_target_framerate_fps_(-1) {}
128 virtual ~CpuOveruseDetectorProxy() {}
129
130 void OnTargetFramerateUpdated(int framerate_fps) override {
131 rtc::CritScope cs(&lock_);
132 last_target_framerate_fps_ = framerate_fps;
133 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
134 }
135
136 int GetLastTargetFramerate() {
137 rtc::CritScope cs(&lock_);
138 return last_target_framerate_fps_;
139 }
140
Niels Möller4db138e2018-04-19 09:04:13 +0200141 CpuOveruseOptions GetOptions() { return options_; }
142
Niels Möller7dc26b72017-12-06 10:27:48 +0100143 private:
144 rtc::CriticalSection lock_;
145 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
146};
147
mflodmancc3d4422017-08-03 08:27:51 -0700148class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700149 public:
Niels Möller213618e2018-07-24 09:29:58 +0200150 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200151 const VideoStreamEncoderSettings& settings,
152 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100153 : VideoStreamEncoder(Clock::GetRealTimeClock(),
154 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200155 stats_proxy,
156 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200157 std::unique_ptr<OveruseFrameDetector>(
158 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100159 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200160 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700161
Henrik Boströmb08882b2020-01-07 10:11:17 +0100162 void PostTaskAndWait(bool down,
163 AdaptationObserverInterface::AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200164 PostTaskAndWait(down, reason, /*expected_results=*/true);
165 }
166
Henrik Boströmb08882b2020-01-07 10:11:17 +0100167 void PostTaskAndWait(bool down,
168 AdaptationObserverInterface::AdaptReason reason,
169 bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100170 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200171 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200172 if (down)
Henrik Boström7875c992020-02-06 10:35:00 +0100173 EXPECT_EQ(expected_results, OnResourceOveruseForTesting(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200174 else
Henrik Boström7875c992020-02-06 10:35:00 +0100175 OnResourceUnderuseForTesting(reason);
perkj803d97f2016-11-01 11:45:46 -0700176 event.Set();
177 });
perkj070ba852017-02-16 15:46:27 -0800178 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700179 }
180
kthelgason2fc52542017-03-03 00:24:41 -0800181 // This is used as a synchronisation mechanism, to make sure that the
182 // encoder queue is not blocked before we start sending it frames.
183 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100184 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200185 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800186 ASSERT_TRUE(event.Wait(5000));
187 }
188
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200189 void TriggerCpuOveruse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100190 PostTaskAndWait(/*down=*/true,
191 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200192 }
kthelgason876222f2016-11-29 01:44:11 -0800193
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200194 void TriggerCpuNormalUsage() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100195 PostTaskAndWait(/*down=*/false,
196 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200197 }
kthelgason876222f2016-11-29 01:44:11 -0800198
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200199 void TriggerQualityLow() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100200 PostTaskAndWait(/*down=*/true,
201 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200202 }
kthelgason876222f2016-11-29 01:44:11 -0800203
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200204 void TriggerQualityLowExpectFalse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100205 PostTaskAndWait(/*down=*/true,
206 AdaptationObserverInterface::AdaptReason::kQuality,
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200207 /*expected_results=*/false);
208 }
209
210 void TriggerQualityHigh() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100211 PostTaskAndWait(/*down=*/false,
212 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200213 }
sprangfda496a2017-06-15 04:21:07 -0700214
Niels Möller7dc26b72017-12-06 10:27:48 +0100215 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700216};
217
asapersson5f7226f2016-11-25 04:37:00 -0800218class VideoStreamFactory
219 : public VideoEncoderConfig::VideoStreamFactoryInterface {
220 public:
sprangfda496a2017-06-15 04:21:07 -0700221 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
222 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800223 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700224 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800225 }
226
227 private:
228 std::vector<VideoStream> CreateEncoderStreams(
229 int width,
230 int height,
231 const VideoEncoderConfig& encoder_config) override {
232 std::vector<VideoStream> streams =
233 test::CreateVideoStreams(width, height, encoder_config);
234 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100235 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700236 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800237 }
238 return streams;
239 }
sprangfda496a2017-06-15 04:21:07 -0700240
asapersson5f7226f2016-11-25 04:37:00 -0800241 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700242 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800243};
244
Noah Richards51db4212019-06-12 06:59:12 -0700245// Simulates simulcast behavior and makes highest stream resolutions divisible
246// by 4.
247class CroppingVideoStreamFactory
248 : public VideoEncoderConfig::VideoStreamFactoryInterface {
249 public:
250 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
251 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
252 EXPECT_GT(num_temporal_layers, 0u);
253 EXPECT_GT(framerate, 0);
254 }
255
256 private:
257 std::vector<VideoStream> CreateEncoderStreams(
258 int width,
259 int height,
260 const VideoEncoderConfig& encoder_config) override {
261 std::vector<VideoStream> streams = test::CreateVideoStreams(
262 width - width % 4, height - height % 4, encoder_config);
263 for (VideoStream& stream : streams) {
264 stream.num_temporal_layers = num_temporal_layers_;
265 stream.max_framerate = framerate_;
266 }
267 return streams;
268 }
269
270 const size_t num_temporal_layers_;
271 const int framerate_;
272};
273
sprangb1ca0732017-02-01 08:38:12 -0800274class AdaptingFrameForwarder : public test::FrameForwarder {
275 public:
276 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700277 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800278
279 void set_adaptation_enabled(bool enabled) {
280 rtc::CritScope cs(&crit_);
281 adaptation_enabled_ = enabled;
282 }
283
asaperssonfab67072017-04-04 05:51:49 -0700284 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800285 rtc::CritScope cs(&crit_);
286 return adaptation_enabled_;
287 }
288
asapersson09f05612017-05-15 23:40:18 -0700289 rtc::VideoSinkWants last_wants() const {
290 rtc::CritScope cs(&crit_);
291 return last_wants_;
292 }
293
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200294 absl::optional<int> last_sent_width() const { return last_width_; }
295 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800296
sprangb1ca0732017-02-01 08:38:12 -0800297 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
298 int cropped_width = 0;
299 int cropped_height = 0;
300 int out_width = 0;
301 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700302 if (adaption_enabled()) {
303 if (adapter_.AdaptFrameResolution(
304 video_frame.width(), video_frame.height(),
305 video_frame.timestamp_us() * 1000, &cropped_width,
306 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100307 VideoFrame adapted_frame =
308 VideoFrame::Builder()
309 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
310 nullptr, out_width, out_height))
311 .set_timestamp_rtp(99)
312 .set_timestamp_ms(99)
313 .set_rotation(kVideoRotation_0)
314 .build();
sprangc5d62e22017-04-02 23:53:04 -0700315 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100316 if (video_frame.has_update_rect()) {
317 adapted_frame.set_update_rect(
318 video_frame.update_rect().ScaleWithFrame(
319 video_frame.width(), video_frame.height(), 0, 0,
320 video_frame.width(), video_frame.height(), out_width,
321 out_height));
322 }
sprangc5d62e22017-04-02 23:53:04 -0700323 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800324 last_width_.emplace(adapted_frame.width());
325 last_height_.emplace(adapted_frame.height());
326 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200327 last_width_ = absl::nullopt;
328 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700329 }
sprangb1ca0732017-02-01 08:38:12 -0800330 } else {
331 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800332 last_width_.emplace(video_frame.width());
333 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800334 }
335 }
336
337 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
338 const rtc::VideoSinkWants& wants) override {
339 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700340 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100341 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800342 test::FrameForwarder::AddOrUpdateSink(sink, wants);
343 }
sprangb1ca0732017-02-01 08:38:12 -0800344 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700345 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
346 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200347 absl::optional<int> last_width_;
348 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800349};
sprangc5d62e22017-04-02 23:53:04 -0700350
Niels Möller213618e2018-07-24 09:29:58 +0200351// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700352class MockableSendStatisticsProxy : public SendStatisticsProxy {
353 public:
354 MockableSendStatisticsProxy(Clock* clock,
355 const VideoSendStream::Config& config,
356 VideoEncoderConfig::ContentType content_type)
357 : SendStatisticsProxy(clock, config, content_type) {}
358
359 VideoSendStream::Stats GetStats() override {
360 rtc::CritScope cs(&lock_);
361 if (mock_stats_)
362 return *mock_stats_;
363 return SendStatisticsProxy::GetStats();
364 }
365
Niels Möller213618e2018-07-24 09:29:58 +0200366 int GetInputFrameRate() const override {
367 rtc::CritScope cs(&lock_);
368 if (mock_stats_)
369 return mock_stats_->input_frame_rate;
370 return SendStatisticsProxy::GetInputFrameRate();
371 }
sprangc5d62e22017-04-02 23:53:04 -0700372 void SetMockStats(const VideoSendStream::Stats& stats) {
373 rtc::CritScope cs(&lock_);
374 mock_stats_.emplace(stats);
375 }
376
377 void ResetMockStats() {
378 rtc::CritScope cs(&lock_);
379 mock_stats_.reset();
380 }
381
382 private:
383 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200384 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700385};
386
sprang4847ae62017-06-27 07:06:52 -0700387class MockBitrateObserver : public VideoBitrateAllocationObserver {
388 public:
Erik Språng566124a2018-04-23 12:32:22 +0200389 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700390};
391
philipel9b058032020-02-10 11:30:00 +0100392class MockEncoderSelector
393 : public VideoEncoderFactory::EncoderSelectorInterface {
394 public:
395 MOCK_METHOD1(OnCurrentEncoder, void(const SdpVideoFormat& format));
396 MOCK_METHOD1(OnEncodingBitrate,
397 absl::optional<SdpVideoFormat>(const DataRate& rate));
398 MOCK_METHOD0(OnEncoderBroken, absl::optional<SdpVideoFormat>());
399};
400
perkj803d97f2016-11-01 11:45:46 -0700401} // namespace
402
mflodmancc3d4422017-08-03 08:27:51 -0700403class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700404 public:
405 static const int kDefaultTimeoutMs = 30 * 1000;
406
mflodmancc3d4422017-08-03 08:27:51 -0700407 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700408 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700409 codec_width_(320),
410 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200411 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200412 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700413 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200414 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700415 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700416 Clock::GetRealTimeClock(),
417 video_send_config_,
418 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700419 sink_(&fake_encoder_) {}
420
421 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700422 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700423 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200424 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800425 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200426 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200427 video_send_config_.rtp.payload_name = "FAKE";
428 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700429
Per512ecb32016-09-23 15:52:06 +0200430 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200431 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700432 video_encoder_config.video_stream_factory =
433 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100434 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700435
436 // Framerate limit is specified by the VideoStreamFactory.
437 std::vector<VideoStream> streams =
438 video_encoder_config.video_stream_factory->CreateEncoderStreams(
439 codec_width_, codec_height_, video_encoder_config);
440 max_framerate_ = streams[0].max_framerate;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100441 fake_clock_.SetTime(Timestamp::Micros(1234));
sprang4847ae62017-06-27 07:06:52 -0700442
Niels Möllerf1338562018-04-26 09:51:47 +0200443 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800444 }
445
Niels Möllerf1338562018-04-26 09:51:47 +0200446 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700447 if (video_stream_encoder_)
448 video_stream_encoder_->Stop();
449 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200450 stats_proxy_.get(), video_send_config_.encoder_settings,
451 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700452 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
453 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700454 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700455 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
456 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200457 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700458 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800459 }
460
461 void ResetEncoder(const std::string& payload_name,
462 size_t num_streams,
463 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700464 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700465 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200466 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800467
468 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200469 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800470 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100471 video_encoder_config.max_bitrate_bps =
472 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800473 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700474 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
475 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700476 video_encoder_config.content_type =
477 screenshare ? VideoEncoderConfig::ContentType::kScreen
478 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700479 if (payload_name == "VP9") {
480 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
481 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
482 video_encoder_config.encoder_specific_settings =
483 new rtc::RefCountedObject<
484 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
485 }
Niels Möllerf1338562018-04-26 09:51:47 +0200486 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700487 }
488
sprang57c2fff2017-01-16 06:24:02 -0800489 VideoFrame CreateFrame(int64_t ntp_time_ms,
490 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100491 VideoFrame frame =
492 VideoFrame::Builder()
493 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
494 destruction_event, codec_width_, codec_height_))
495 .set_timestamp_rtp(99)
496 .set_timestamp_ms(99)
497 .set_rotation(kVideoRotation_0)
498 .build();
sprang57c2fff2017-01-16 06:24:02 -0800499 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700500 return frame;
501 }
502
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100503 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
504 rtc::Event* destruction_event,
505 int offset_x) const {
506 VideoFrame frame =
507 VideoFrame::Builder()
508 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
509 destruction_event, codec_width_, codec_height_))
510 .set_timestamp_rtp(99)
511 .set_timestamp_ms(99)
512 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100513 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100514 .build();
515 frame.set_ntp_time_ms(ntp_time_ms);
516 return frame;
517 }
518
sprang57c2fff2017-01-16 06:24:02 -0800519 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100520 VideoFrame frame =
521 VideoFrame::Builder()
522 .set_video_frame_buffer(
523 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
524 .set_timestamp_rtp(99)
525 .set_timestamp_ms(99)
526 .set_rotation(kVideoRotation_0)
527 .build();
sprang57c2fff2017-01-16 06:24:02 -0800528 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700529 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700530 return frame;
531 }
532
Noah Richards51db4212019-06-12 06:59:12 -0700533 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
534 rtc::Event* destruction_event,
535 int width,
536 int height) const {
537 VideoFrame frame =
538 VideoFrame::Builder()
539 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
540 destruction_event, width, height))
541 .set_timestamp_rtp(99)
542 .set_timestamp_ms(99)
543 .set_rotation(kVideoRotation_0)
544 .build();
545 frame.set_ntp_time_ms(ntp_time_ms);
546 return frame;
547 }
548
549 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
550 rtc::Event* destruction_event) const {
551 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
552 codec_height_);
553 }
554
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100555 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
556 MockBitrateObserver bitrate_observer;
557 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
558
559 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
560 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200561 video_stream_encoder_->OnBitrateUpdated(
562 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +0100563 DataRate::bps(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(
Florent Castellia8336d32019-09-09 13:36:55 +02001172 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001173 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001174 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001175 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001176 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001177 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001178 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001179}
1180
mflodmancc3d4422017-08-03 08:27:51 -07001181TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001182 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001183 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001184 // The encoder will cache up to one frame for a short duration. Adding two
1185 // frames means that the first frame will be dropped and the second frame will
1186 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001187 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001188 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001189 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001190
Erik Språng4c6ca302019-04-08 15:14:01 +02001191 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001192 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001193 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001194
Sebastian Janssona3177052018-04-10 13:05:49 +02001195 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001196 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001197 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1198
1199 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001200 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001201}
1202
mflodmancc3d4422017-08-03 08:27:51 -07001203TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001204 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001205 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001206 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001207 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001208 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001209
Florent Castellia8336d32019-09-09 13:36:55 +02001210 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
Ying Wang9b881ab2020-02-07 14:29:32 +01001211 DataRate::bps(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001212 // The encoder will cache up to one frame for a short duration. Adding two
1213 // frames means that the first frame will be dropped and the second frame will
1214 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001215 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001216 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001217
Erik Språng4c6ca302019-04-08 15:14:01 +02001218 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001219 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001220 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001221 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001222 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1223 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001224 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001225}
1226
mflodmancc3d4422017-08-03 08:27:51 -07001227TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001228 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001229 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001230 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001231 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001232 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001233
1234 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001235 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001236
perkja49cbd32016-09-16 07:53:41 -07001237 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001238 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001239 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001240}
1241
mflodmancc3d4422017-08-03 08:27:51 -07001242TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001243 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001244 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001245 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001246
perkja49cbd32016-09-16 07:53:41 -07001247 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001248 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001249
mflodmancc3d4422017-08-03 08:27:51 -07001250 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001251 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001252 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001253 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1254 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001255}
1256
mflodmancc3d4422017-08-03 08:27:51 -07001257TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001258 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001259 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001260 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001261
1262 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001263 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001264 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001265 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1266 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001267 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1268 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001269 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001270 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001271
mflodmancc3d4422017-08-03 08:27:51 -07001272 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001273}
1274
Noah Richards51db4212019-06-12 06:59:12 -07001275TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1276 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001277 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001278 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001279
1280 rtc::Event frame_destroyed_event;
1281 video_source_.IncomingCapturedFrame(
1282 CreateFakeNativeFrame(1, &frame_destroyed_event));
1283 ExpectDroppedFrame();
1284 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1285 video_stream_encoder_->Stop();
1286}
1287
1288TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1289 // Use the cropping factory.
1290 video_encoder_config_.video_stream_factory =
1291 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1292 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1293 kMaxPayloadLength);
1294 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1295
1296 // Capture a frame at codec_width_/codec_height_.
1297 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001298 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001299 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001300 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1301 WaitForEncodedFrame(1);
1302 // The encoder will have been configured once.
1303 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1304 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1305 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1306
1307 // Now send in a fake frame that needs to be cropped as the width/height
1308 // aren't divisible by 4 (see CreateEncoderStreams above).
1309 rtc::Event frame_destroyed_event;
1310 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1311 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1312 ExpectDroppedFrame();
1313 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1314 video_stream_encoder_->Stop();
1315}
1316
Ying Wang9b881ab2020-02-07 14:29:32 +01001317TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1318 video_stream_encoder_->OnBitrateUpdated(
1319 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1320 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
1321 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1322 WaitForEncodedFrame(1);
1323
1324 video_stream_encoder_->OnBitrateUpdated(
1325 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1326 DataRate::bps(kTargetBitrateBps), 0, 0, 0.5);
1327 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1328 // frames. Adding two frames means that the first frame will be dropped and
1329 // the second frame will be sent to the encoder.
1330 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1331 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1332 WaitForEncodedFrame(3);
1333 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1334 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1335 WaitForEncodedFrame(5);
1336 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1337 video_stream_encoder_->Stop();
1338}
1339
mflodmancc3d4422017-08-03 08:27:51 -07001340TEST_F(VideoStreamEncoderTest,
1341 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001342 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001343 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001344 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001345 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001346
1347 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001348 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001349 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001350 // The encoder will have been configured once when the first frame is
1351 // received.
1352 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001353
1354 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001355 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001356 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001357 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001358 kMaxPayloadLength);
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(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001362 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001363 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001364 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001365
mflodmancc3d4422017-08-03 08:27:51 -07001366 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001367}
1368
mflodmancc3d4422017-08-03 08:27:51 -07001369TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001370 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001371 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001372 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001373
1374 // Capture a frame and wait for it to synchronize with the encoder thread.
1375 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001376 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001377 // The encoder will have been configured once.
1378 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001379 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1380 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1381
1382 codec_width_ *= 2;
1383 codec_height_ *= 2;
1384 // Capture a frame with a higher resolution and wait for it to synchronize
1385 // with the encoder thread.
1386 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001387 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001388 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1389 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001390 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001391
mflodmancc3d4422017-08-03 08:27:51 -07001392 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001393}
1394
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001395TEST_F(VideoStreamEncoderTest,
1396 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1397 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001398 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001399 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001400
1401 // Capture a frame and wait for it to synchronize with the encoder thread.
1402 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1403 WaitForEncodedFrame(1);
1404
1405 VideoEncoderConfig video_encoder_config;
1406 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1407 // Changing the max payload data length recreates encoder.
1408 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1409 kMaxPayloadLength / 2);
1410
1411 // Capture a frame and wait for it to synchronize with the encoder thread.
1412 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1413 WaitForEncodedFrame(2);
1414 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1415
1416 video_stream_encoder_->Stop();
1417}
1418
Sergey Silkin5ee69672019-07-02 14:18:34 +02001419TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1420 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001421 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001422 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001423
1424 VideoEncoderConfig video_encoder_config;
1425 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1426 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1427 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1428 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1429 kMaxPayloadLength);
1430
1431 // Capture a frame and wait for it to synchronize with the encoder thread.
1432 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1433 WaitForEncodedFrame(1);
1434 // The encoder will have been configured once when the first frame is
1435 // received.
1436 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1437 EXPECT_EQ(kTargetBitrateBps,
1438 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1439 EXPECT_EQ(kStartBitrateBps,
1440 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1441
Sergey Silkin6456e352019-07-08 17:56:40 +02001442 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1443 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001444 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1445 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1446 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1447 kMaxPayloadLength);
1448
1449 // Capture a frame and wait for it to synchronize with the encoder thread.
1450 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1451 WaitForEncodedFrame(2);
1452 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1453 // Bitrate limits have changed - rate allocator should be reconfigured,
1454 // encoder should not be reconfigured.
1455 EXPECT_EQ(kTargetBitrateBps * 2,
1456 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1457 EXPECT_EQ(kStartBitrateBps * 2,
1458 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1459 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1460
1461 video_stream_encoder_->Stop();
1462}
1463
Sergey Silkin6456e352019-07-08 17:56:40 +02001464TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001465 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001466 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001467 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001468 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001469
Sergey Silkincd02eba2020-01-20 14:48:40 +01001470 const uint32_t kMinEncBitrateKbps = 100;
1471 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001472 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001473 /*frame_size_pixels=*/codec_width_ * codec_height_,
1474 /*min_start_bitrate_bps=*/0,
1475 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1476 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001477 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1478
Sergey Silkincd02eba2020-01-20 14:48:40 +01001479 VideoEncoderConfig video_encoder_config;
1480 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1481 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1482 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1483 (kMinEncBitrateKbps + 1) * 1000;
1484 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1485 kMaxPayloadLength);
1486
1487 // When both encoder and app provide bitrate limits, the intersection of
1488 // provided sets should be used.
1489 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1490 WaitForEncodedFrame(1);
1491 EXPECT_EQ(kMaxEncBitrateKbps,
1492 bitrate_allocator_factory_.codec_config().maxBitrate);
1493 EXPECT_EQ(kMinEncBitrateKbps + 1,
1494 bitrate_allocator_factory_.codec_config().minBitrate);
1495
1496 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1497 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1498 (kMinEncBitrateKbps - 1) * 1000;
1499 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1500 kMaxPayloadLength);
1501 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001502 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001503 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001504 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001505 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001506 bitrate_allocator_factory_.codec_config().minBitrate);
1507
Sergey Silkincd02eba2020-01-20 14:48:40 +01001508 video_stream_encoder_->Stop();
1509}
1510
1511TEST_F(VideoStreamEncoderTest,
1512 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1513 video_stream_encoder_->OnBitrateUpdated(
1514 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001515 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001516
1517 const uint32_t kMinAppBitrateKbps = 100;
1518 const uint32_t kMaxAppBitrateKbps = 200;
1519 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1520 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1521 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1522 /*frame_size_pixels=*/codec_width_ * codec_height_,
1523 /*min_start_bitrate_bps=*/0,
1524 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1525 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1526 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1527
1528 VideoEncoderConfig video_encoder_config;
1529 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1530 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1531 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1532 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001533 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1534 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001535
Sergey Silkincd02eba2020-01-20 14:48:40 +01001536 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1537 WaitForEncodedFrame(1);
1538 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001539 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001540 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001541 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001542
1543 video_stream_encoder_->Stop();
1544}
1545
1546TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001547 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001548 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001549 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001550 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001551
1552 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001553 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001554 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001555 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001556 fake_encoder_.SetResolutionBitrateLimits(
1557 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1558
1559 VideoEncoderConfig video_encoder_config;
1560 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1561 video_encoder_config.max_bitrate_bps = 0;
1562 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1563 kMaxPayloadLength);
1564
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001565 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001566 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1567 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001568 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1569 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001570 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1571 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1572
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001573 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001574 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1575 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001576 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1577 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001578 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1579 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1580
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001581 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001582 // encoder for 360p should be used.
1583 video_source_.IncomingCapturedFrame(
1584 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1585 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001586 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1587 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001588 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1589 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1590
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001591 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001592 // ignored.
1593 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1594 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001595 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1596 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001597 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1598 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001599 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1600 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001601 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1602 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1603
1604 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1605 // for 270p should be used.
1606 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1607 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001608 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1609 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001610 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1611 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1612
1613 video_stream_encoder_->Stop();
1614}
1615
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001616TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1617 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001618 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001619 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001620
1621 VideoEncoderConfig video_encoder_config;
1622 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1623 video_encoder_config.max_bitrate_bps = 0;
1624 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1625 kMaxPayloadLength);
1626
1627 // Encode 720p frame to get the default encoder target bitrate.
1628 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1629 WaitForEncodedFrame(1);
1630 const uint32_t kDefaultTargetBitrateFor720pKbps =
1631 bitrate_allocator_factory_.codec_config()
1632 .simulcastStream[0]
1633 .targetBitrate;
1634
1635 // Set the max recommended encoder bitrate to something lower than the default
1636 // target bitrate.
1637 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1638 1280 * 720, 10 * 1000, 10 * 1000,
1639 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1640 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1641
1642 // Change resolution to trigger encoder reinitialization.
1643 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1644 WaitForEncodedFrame(2);
1645 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1646 WaitForEncodedFrame(3);
1647
1648 // Ensure the target bitrate is capped by the max bitrate.
1649 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1650 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1651 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1652 .simulcastStream[0]
1653 .targetBitrate *
1654 1000,
1655 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1656
1657 video_stream_encoder_->Stop();
1658}
1659
mflodmancc3d4422017-08-03 08:27:51 -07001660TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001661 EXPECT_TRUE(video_source_.has_sinks());
1662 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001663 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001664 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001665 EXPECT_FALSE(video_source_.has_sinks());
1666 EXPECT_TRUE(new_video_source.has_sinks());
1667
mflodmancc3d4422017-08-03 08:27:51 -07001668 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001669}
1670
mflodmancc3d4422017-08-03 08:27:51 -07001671TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001672 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001673 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001674 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001675 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001676}
1677
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001678TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1679 constexpr int kRequestedResolutionAlignment = 7;
1680 video_source_.set_adaptation_enabled(true);
1681 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1682 video_stream_encoder_->OnBitrateUpdated(
1683 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001684 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001685
1686 // On the 1st frame, we should have initialized the encoder and
1687 // asked for its resolution requirements.
1688 video_source_.IncomingCapturedFrame(
1689 CreateFrame(1, codec_width_, codec_height_));
1690 WaitForEncodedFrame(1);
1691 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1692 kRequestedResolutionAlignment);
1693
1694 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1695 // (It's up the to the encoder to potentially drop the previous frame,
1696 // to avoid coding back-to-back keyframes.)
1697 video_source_.IncomingCapturedFrame(
1698 CreateFrame(2, codec_width_, codec_height_));
1699 WaitForEncodedFrame(2);
1700 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1701
1702 video_stream_encoder_->Stop();
1703}
1704
Jonathan Yubc771b72017-12-08 17:04:29 -08001705TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1706 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001707 const int kWidth = 1280;
1708 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001709
1710 // We rely on the automatic resolution adaptation, but we handle framerate
1711 // adaptation manually by mocking the stats proxy.
1712 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001713
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001714 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001715 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001716 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001717 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001718 video_stream_encoder_->SetSource(&video_source_,
1719 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001720 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001721 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001722 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001723 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1724
Jonathan Yubc771b72017-12-08 17:04:29 -08001725 // Adapt down as far as possible.
1726 rtc::VideoSinkWants last_wants;
1727 int64_t t = 1;
1728 int loop_count = 0;
1729 do {
1730 ++loop_count;
1731 last_wants = video_source_.sink_wants();
1732
1733 // Simulate the framerate we've been asked to adapt to.
1734 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1735 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1736 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1737 mock_stats.input_frame_rate = fps;
1738 stats_proxy_->SetMockStats(mock_stats);
1739
1740 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1741 sink_.WaitForEncodedFrame(t);
1742 t += frame_interval_ms;
1743
mflodmancc3d4422017-08-03 08:27:51 -07001744 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001745 VerifyBalancedModeFpsRange(
1746 video_source_.sink_wants(),
1747 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1748 } while (video_source_.sink_wants().max_pixel_count <
1749 last_wants.max_pixel_count ||
1750 video_source_.sink_wants().max_framerate_fps <
1751 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001752
Jonathan Yubc771b72017-12-08 17:04:29 -08001753 // Verify that we've adapted all the way down.
1754 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001755 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001756 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1757 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001758 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001759 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1760 *video_source_.last_sent_height());
1761 EXPECT_EQ(kMinBalancedFramerateFps,
1762 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001763
Jonathan Yubc771b72017-12-08 17:04:29 -08001764 // Adapt back up the same number of times we adapted down.
1765 for (int i = 0; i < loop_count - 1; ++i) {
1766 last_wants = video_source_.sink_wants();
1767
1768 // Simulate the framerate we've been asked to adapt to.
1769 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1770 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1771 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1772 mock_stats.input_frame_rate = fps;
1773 stats_proxy_->SetMockStats(mock_stats);
1774
1775 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1776 sink_.WaitForEncodedFrame(t);
1777 t += frame_interval_ms;
1778
mflodmancc3d4422017-08-03 08:27:51 -07001779 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001780 VerifyBalancedModeFpsRange(
1781 video_source_.sink_wants(),
1782 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1783 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1784 last_wants.max_pixel_count ||
1785 video_source_.sink_wants().max_framerate_fps >
1786 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001787 }
1788
Åsa Persson8c1bf952018-09-13 10:42:19 +02001789 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001790 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001791 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001792 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1793 EXPECT_EQ((loop_count - 1) * 2,
1794 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001795
mflodmancc3d4422017-08-03 08:27:51 -07001796 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001797}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001798
mflodmancc3d4422017-08-03 08:27:51 -07001799TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001800 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001801 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001802 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001803 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001804
sprangc5d62e22017-04-02 23:53:04 -07001805 const int kFrameWidth = 1280;
1806 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001807
Åsa Persson8c1bf952018-09-13 10:42:19 +02001808 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001809
kthelgason5e13d412016-12-01 03:59:51 -08001810 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001811 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001812 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001813 frame_timestamp += kFrameIntervalMs;
1814
perkj803d97f2016-11-01 11:45:46 -07001815 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001816 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001817 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001818 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001819 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001820 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001821
asapersson0944a802017-04-07 00:57:58 -07001822 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001823 // wanted resolution.
1824 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1825 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1826 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001827 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001828
1829 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001830 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001831 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001832 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001833 // Give the encoder queue time to process the change in degradation preference
1834 // by waiting for an encoded frame.
1835 new_video_source.IncomingCapturedFrame(
1836 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1837 sink_.WaitForEncodedFrame(frame_timestamp);
1838 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001839 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001840 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001841
sprangc5d62e22017-04-02 23:53:04 -07001842 // Force an input frame rate to be available, or the adaptation call won't
1843 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001844 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001845 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001846 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001847 stats_proxy_->SetMockStats(stats);
1848
mflodmancc3d4422017-08-03 08:27:51 -07001849 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001850 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001851 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001852 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001853 frame_timestamp += kFrameIntervalMs;
1854
1855 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001856 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001857 EXPECT_EQ(std::numeric_limits<int>::max(),
1858 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001859 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001860
asapersson02465b82017-04-10 01:12:52 -07001861 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001862 video_stream_encoder_->SetSource(&new_video_source,
1863 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001864 // Give the encoder queue time to process the change in degradation preference
1865 // by waiting for an encoded frame.
1866 new_video_source.IncomingCapturedFrame(
1867 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1868 sink_.WaitForEncodedFrame(frame_timestamp);
1869 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001870 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001871
mflodmancc3d4422017-08-03 08:27:51 -07001872 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001873 new_video_source.IncomingCapturedFrame(
1874 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 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001879 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001880
1881 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001882 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001883 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001884 // Give the encoder queue time to process the change in degradation preference
1885 // by waiting for an encoded frame.
1886 new_video_source.IncomingCapturedFrame(
1887 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1888 sink_.WaitForEncodedFrame(frame_timestamp);
1889 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001890 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1891 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001892 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001893 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001894
1895 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001896 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001897 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001898 // Give the encoder queue time to process the change in degradation preference
1899 // by waiting for an encoded frame.
1900 new_video_source.IncomingCapturedFrame(
1901 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1902 sink_.WaitForEncodedFrame(frame_timestamp);
1903 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001904 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1905 EXPECT_EQ(std::numeric_limits<int>::max(),
1906 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001907 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001908
mflodmancc3d4422017-08-03 08:27:51 -07001909 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001910}
1911
mflodmancc3d4422017-08-03 08:27:51 -07001912TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001913 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001914 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001915 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001916
asaperssonfab67072017-04-04 05:51:49 -07001917 const int kWidth = 1280;
1918 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001919 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001920 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001921 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1922 EXPECT_FALSE(stats.bw_limited_resolution);
1923 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1924
1925 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001926 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001927 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001928 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001929
1930 stats = stats_proxy_->GetStats();
1931 EXPECT_TRUE(stats.bw_limited_resolution);
1932 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1933
1934 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001935 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001936 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001937 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001938
1939 stats = stats_proxy_->GetStats();
1940 EXPECT_FALSE(stats.bw_limited_resolution);
1941 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1942 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1943
mflodmancc3d4422017-08-03 08:27:51 -07001944 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001945}
1946
mflodmancc3d4422017-08-03 08:27:51 -07001947TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001948 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001949 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001950 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001951
1952 const int kWidth = 1280;
1953 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001954 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001955 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001956 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1957 EXPECT_FALSE(stats.cpu_limited_resolution);
1958 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1959
1960 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001961 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001962 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001963 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001964
1965 stats = stats_proxy_->GetStats();
1966 EXPECT_TRUE(stats.cpu_limited_resolution);
1967 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1968
1969 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001970 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001971 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001972 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001973
1974 stats = stats_proxy_->GetStats();
1975 EXPECT_FALSE(stats.cpu_limited_resolution);
1976 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001977 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001978
mflodmancc3d4422017-08-03 08:27:51 -07001979 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001980}
1981
mflodmancc3d4422017-08-03 08:27:51 -07001982TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001983 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001984 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01001985 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001986
asaperssonfab67072017-04-04 05:51:49 -07001987 const int kWidth = 1280;
1988 const int kHeight = 720;
1989 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001990 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001991 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001992 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001993 EXPECT_FALSE(stats.cpu_limited_resolution);
1994 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1995
asaperssonfab67072017-04-04 05:51:49 -07001996 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001997 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001998 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001999 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002000 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002001 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002002 EXPECT_TRUE(stats.cpu_limited_resolution);
2003 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2004
2005 // Set new source with adaptation still enabled.
2006 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002007 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002008 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002009
asaperssonfab67072017-04-04 05:51:49 -07002010 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002011 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002012 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002013 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002014 EXPECT_TRUE(stats.cpu_limited_resolution);
2015 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2016
2017 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002018 video_stream_encoder_->SetSource(&new_video_source,
2019 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002020
asaperssonfab67072017-04-04 05:51:49 -07002021 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002022 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002023 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002024 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002025 EXPECT_FALSE(stats.cpu_limited_resolution);
2026 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2027
2028 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002029 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002030 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002031
asaperssonfab67072017-04-04 05:51:49 -07002032 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002033 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002034 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002035 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002036 EXPECT_TRUE(stats.cpu_limited_resolution);
2037 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2038
asaperssonfab67072017-04-04 05:51:49 -07002039 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002040 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002041 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002042 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002043 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002044 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002045 EXPECT_FALSE(stats.cpu_limited_resolution);
2046 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002047 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002048
mflodmancc3d4422017-08-03 08:27:51 -07002049 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002050}
2051
mflodmancc3d4422017-08-03 08:27:51 -07002052TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002053 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002054 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002055 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002056
asaperssonfab67072017-04-04 05:51:49 -07002057 const int kWidth = 1280;
2058 const int kHeight = 720;
2059 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002060 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002061 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002062 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002063 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002064 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002065
2066 // Set new source with adaptation still enabled.
2067 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002068 video_stream_encoder_->SetSource(&new_video_source,
2069 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002070
asaperssonfab67072017-04-04 05:51:49 -07002071 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002072 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002073 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002074 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002075 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002076 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002077
asaperssonfab67072017-04-04 05:51:49 -07002078 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002079 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002080 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002081 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002082 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002083 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002084 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002085 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002086
asaperssonfab67072017-04-04 05:51:49 -07002087 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002088 video_stream_encoder_->SetSource(&new_video_source,
2089 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002090
asaperssonfab67072017-04-04 05:51:49 -07002091 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002092 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002093 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002094 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002095 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002096 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002097
asapersson02465b82017-04-10 01:12:52 -07002098 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002099 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002100 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002101
asaperssonfab67072017-04-04 05:51:49 -07002102 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002103 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002104 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002105 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002106 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002107 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2108 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002109
mflodmancc3d4422017-08-03 08:27:51 -07002110 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002111}
2112
mflodmancc3d4422017-08-03 08:27:51 -07002113TEST_F(VideoStreamEncoderTest,
2114 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002115 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002116 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002117 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002118
2119 const int kWidth = 1280;
2120 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002121 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002122 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002123 video_source_.IncomingCapturedFrame(
2124 CreateFrame(timestamp_ms, kWidth, kHeight));
2125 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002126 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2127 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2128 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2129
2130 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002131 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002132 timestamp_ms += kFrameIntervalMs;
2133 video_source_.IncomingCapturedFrame(
2134 CreateFrame(timestamp_ms, kWidth, kHeight));
2135 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002136 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2137 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2138 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2139
2140 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002141 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002142 timestamp_ms += kFrameIntervalMs;
2143 video_source_.IncomingCapturedFrame(
2144 CreateFrame(timestamp_ms, kWidth, kHeight));
2145 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002146 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2147 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2148 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2149
Niels Möller4db138e2018-04-19 09:04:13 +02002150 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002151 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002152
2153 VideoEncoderConfig video_encoder_config;
2154 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2155 // Make format different, to force recreation of encoder.
2156 video_encoder_config.video_format.parameters["foo"] = "foo";
2157 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002158 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002159 timestamp_ms += kFrameIntervalMs;
2160 video_source_.IncomingCapturedFrame(
2161 CreateFrame(timestamp_ms, kWidth, kHeight));
2162 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002163 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2164 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2165 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2166
mflodmancc3d4422017-08-03 08:27:51 -07002167 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002168}
2169
mflodmancc3d4422017-08-03 08:27:51 -07002170TEST_F(VideoStreamEncoderTest,
2171 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002172 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002173 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002174 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002175
asapersson0944a802017-04-07 00:57:58 -07002176 const int kWidth = 1280;
2177 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002178 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002179
asaperssonfab67072017-04-04 05:51:49 -07002180 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002181 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002182 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002183 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002184 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002185 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2186
asapersson02465b82017-04-10 01:12:52 -07002187 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002188 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002189 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002190 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002191 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002192 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002193 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002194 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2195
2196 // Set new source with adaptation still enabled.
2197 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002198 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002199 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002200
2201 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002202 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002203 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002204 stats = stats_proxy_->GetStats();
2205 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002206 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002207 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2208
sprangc5d62e22017-04-02 23:53:04 -07002209 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002210 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002211 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002212 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002213 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002214 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002215 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002216 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002217 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002218 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002219 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2220
sprangc5d62e22017-04-02 23:53:04 -07002221 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002222 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002223 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2224 mock_stats.input_frame_rate = 30;
2225 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002226 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002227 stats_proxy_->ResetMockStats();
2228
2229 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002230 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002231 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002232
2233 // Framerate now adapted.
2234 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002235 EXPECT_FALSE(stats.cpu_limited_resolution);
2236 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002237 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2238
2239 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002240 video_stream_encoder_->SetSource(&new_video_source,
2241 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002242 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002243 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002244 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002245
2246 stats = stats_proxy_->GetStats();
2247 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002248 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002249 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2250
2251 // Try to trigger overuse. Should not succeed.
2252 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002253 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002254 stats_proxy_->ResetMockStats();
2255
2256 stats = stats_proxy_->GetStats();
2257 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002258 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002259 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2260
2261 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002262 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002263 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002264 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002265 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002266 stats = stats_proxy_->GetStats();
2267 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002268 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002269 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002270
2271 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002272 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002273 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002274 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002275 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(3, stats.number_of_cpu_adapt_changes);
2279
2280 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002281 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002282 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002283 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002284 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002285 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002286 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002287 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002288 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002289 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002290 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2291
2292 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002293 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002294 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002295 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002296 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002297 stats = stats_proxy_->GetStats();
2298 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002299 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002300 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002301 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002302
mflodmancc3d4422017-08-03 08:27:51 -07002303 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002304}
2305
mflodmancc3d4422017-08-03 08:27:51 -07002306TEST_F(VideoStreamEncoderTest,
2307 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002308 const int kWidth = 1280;
2309 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002310 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002311 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002312 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002313
asaperssonfab67072017-04-04 05:51:49 -07002314 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002315 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002316
asaperssonfab67072017-04-04 05:51:49 -07002317 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002318 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002319
asaperssonfab67072017-04-04 05:51:49 -07002320 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002321 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002322
asaperssonfab67072017-04-04 05:51:49 -07002323 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002324 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002325
kthelgason876222f2016-11-29 01:44:11 -08002326 // Expect a scale down.
2327 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002328 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002329
asapersson02465b82017-04-10 01:12:52 -07002330 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002331 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002332 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002333 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002334
asaperssonfab67072017-04-04 05:51:49 -07002335 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002336 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002337 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002338 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002339
asaperssonfab67072017-04-04 05:51:49 -07002340 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002341 EXPECT_EQ(std::numeric_limits<int>::max(),
2342 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002343
asaperssonfab67072017-04-04 05:51:49 -07002344 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002345 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002346 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002347 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002348
asapersson02465b82017-04-10 01:12:52 -07002349 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002350 EXPECT_EQ(std::numeric_limits<int>::max(),
2351 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002352
mflodmancc3d4422017-08-03 08:27:51 -07002353 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002354}
2355
mflodmancc3d4422017-08-03 08:27:51 -07002356TEST_F(VideoStreamEncoderTest,
2357 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002358 const int kWidth = 1280;
2359 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002360 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002361 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002362 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002363
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002364 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002365 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002366 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002367 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002368
2369 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002370 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002371 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002372 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2373 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2374
2375 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002376 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002377 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002378 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2379 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2380 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2381
2382 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002383 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002384 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2385 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2386 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2387
mflodmancc3d4422017-08-03 08:27:51 -07002388 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002389}
2390
mflodmancc3d4422017-08-03 08:27:51 -07002391TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002392 const int kWidth = 1280;
2393 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002394 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002395 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002396 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002397
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002398 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002399 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002400 video_stream_encoder_->SetSource(&source,
2401 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002402 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2403 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002404 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002405
2406 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002407 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002408 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2409 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2410 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2411 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2412
2413 // Trigger adapt down for same input resolution, expect no change.
2414 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2415 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002416 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002417 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2418 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2419 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2420
2421 // Trigger adapt down for larger input resolution, expect no change.
2422 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2423 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002424 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002425 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2426 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2427 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2428
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002430}
2431
mflodmancc3d4422017-08-03 08:27:51 -07002432TEST_F(VideoStreamEncoderTest,
2433 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002434 const int kWidth = 1280;
2435 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002436 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002437 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002438 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002439
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002440 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002441 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002442 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002443 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002444
2445 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002446 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002447 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002448 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2449 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2450
2451 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002452 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002453 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002454 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2455 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2456
mflodmancc3d4422017-08-03 08:27:51 -07002457 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002458}
2459
mflodmancc3d4422017-08-03 08:27:51 -07002460TEST_F(VideoStreamEncoderTest,
2461 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002462 const int kWidth = 1280;
2463 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002464 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002465 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002466 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002467
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002468 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002469 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002470 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002471 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002472
2473 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002474 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002475 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002476 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002477 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2478
2479 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002480 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002481 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002482 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002483 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2484
mflodmancc3d4422017-08-03 08:27:51 -07002485 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002486}
2487
mflodmancc3d4422017-08-03 08:27:51 -07002488TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002489 const int kWidth = 1280;
2490 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002491 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002492 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002493 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002494
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002495 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002496 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002497 video_stream_encoder_->SetSource(&source,
2498 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002499
2500 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2501 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002502 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002503 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2504 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2505 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2506
2507 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002508 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002509 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002510 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2511 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2512 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2513
mflodmancc3d4422017-08-03 08:27:51 -07002514 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002515}
2516
mflodmancc3d4422017-08-03 08:27:51 -07002517TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002518 const int kWidth = 1280;
2519 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002520 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002521 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002522 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002523
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002524 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002525 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002526 video_stream_encoder_->SetSource(&source,
2527 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002528
2529 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2530 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002531 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002532 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2533 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2534 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2535
2536 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002537 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002538 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002539 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2540 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2541 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2542
mflodmancc3d4422017-08-03 08:27:51 -07002543 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002544}
2545
mflodmancc3d4422017-08-03 08:27:51 -07002546TEST_F(VideoStreamEncoderTest,
2547 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002548 const int kWidth = 1280;
2549 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002550 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002551 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002552 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002553
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002554 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002555 AdaptingFrameForwarder source;
2556 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002557 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002558 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002559
2560 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002561 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002562 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002563 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2564 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2565
2566 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002567 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002568 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002569 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002570 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002571 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2572 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2573
2574 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002575 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002576 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002577 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2578 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2579 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2580
mflodmancc3d4422017-08-03 08:27:51 -07002581 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002582}
2583
mflodmancc3d4422017-08-03 08:27:51 -07002584TEST_F(VideoStreamEncoderTest,
2585 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002586 const int kWidth = 1280;
2587 const int kHeight = 720;
2588 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002589 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002590 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002591 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002592
2593 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2594 stats.input_frame_rate = kInputFps;
2595 stats_proxy_->SetMockStats(stats);
2596
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002597 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002598 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2599 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002600 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002601
2602 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002603 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002604 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2605 sink_.WaitForEncodedFrame(2);
2606 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2607
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002608 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002609 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002610 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002611 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002612 // Give the encoder queue time to process the change in degradation preference
2613 // by waiting for an encoded frame.
2614 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2615 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002616 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002617
2618 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002619 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002620 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2621 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002622 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2623
2624 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002625 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002626 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002627
mflodmancc3d4422017-08-03 08:27:51 -07002628 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002629}
2630
mflodmancc3d4422017-08-03 08:27:51 -07002631TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002632 const int kWidth = 1280;
2633 const int kHeight = 720;
2634 const size_t kNumFrames = 10;
2635
Erik Språng4c6ca302019-04-08 15:14:01 +02002636 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002637 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002638 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002639
asaperssond0de2952017-04-21 01:47:31 -07002640 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002641 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002642 video_source_.set_adaptation_enabled(true);
2643
2644 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2645 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2646
2647 int downscales = 0;
2648 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002649 video_source_.IncomingCapturedFrame(
2650 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2651 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002652
asaperssonfab67072017-04-04 05:51:49 -07002653 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002654 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002655 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002656 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002657
2658 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2659 ++downscales;
2660
2661 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2662 EXPECT_EQ(downscales,
2663 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2664 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002665 }
mflodmancc3d4422017-08-03 08:27:51 -07002666 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002667}
2668
mflodmancc3d4422017-08-03 08:27:51 -07002669TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002670 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2671 const int kWidth = 1280;
2672 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002673 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002674 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002675 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002676
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002677 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002678 AdaptingFrameForwarder source;
2679 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002680 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002681 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002682
Åsa Persson8c1bf952018-09-13 10:42:19 +02002683 int64_t timestamp_ms = kFrameIntervalMs;
2684 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002685 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002686 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002687 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2688 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2689
2690 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002691 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002692 timestamp_ms += kFrameIntervalMs;
2693 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2694 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002695 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002696 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2697 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2698
2699 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002700 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002701 timestamp_ms += kFrameIntervalMs;
2702 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002703 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002704 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002705 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2706 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2707
2708 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002709 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002710 timestamp_ms += kFrameIntervalMs;
2711 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2712 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002713 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002714 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2715 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2716
2717 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002718 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002719 timestamp_ms += kFrameIntervalMs;
2720 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002721 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002722 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002723 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2724 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2725
mflodmancc3d4422017-08-03 08:27:51 -07002726 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002727}
2728
mflodmancc3d4422017-08-03 08:27:51 -07002729TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002730 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2731 const int kWidth = 1280;
2732 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002733 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002734 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002735 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002736
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002737 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002738 AdaptingFrameForwarder source;
2739 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002740 video_stream_encoder_->SetSource(&source,
2741 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002742
Åsa Persson8c1bf952018-09-13 10:42:19 +02002743 int64_t timestamp_ms = kFrameIntervalMs;
2744 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002745 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002746 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002747 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2748 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2749
2750 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002751 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002752 timestamp_ms += kFrameIntervalMs;
2753 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2754 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002755 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2756 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2757 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2758
2759 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002760 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002761 timestamp_ms += kFrameIntervalMs;
2762 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002763 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002764 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002765 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2766 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2767
2768 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002769 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002770 timestamp_ms += kFrameIntervalMs;
2771 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2772 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002773 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2774 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2775 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2776
2777 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002778 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002779 timestamp_ms += kFrameIntervalMs;
2780 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002781 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002782 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002783 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2784 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2785
mflodmancc3d4422017-08-03 08:27:51 -07002786 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002787}
2788
Sergey Silkin41c650b2019-10-14 13:12:19 +02002789TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2790 fake_encoder_.SetResolutionBitrateLimits(
2791 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2792
2793 video_stream_encoder_->OnBitrateUpdated(
2794 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2795 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002796 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002797
2798 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2799 AdaptingFrameForwarder source;
2800 source.set_adaptation_enabled(true);
2801 video_stream_encoder_->SetSource(
2802 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2803
2804 // Insert 720p frame.
2805 int64_t timestamp_ms = kFrameIntervalMs;
2806 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2807 WaitForEncodedFrame(1280, 720);
2808
2809 // Reduce bitrate and trigger adapt down.
2810 video_stream_encoder_->OnBitrateUpdated(
2811 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2812 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002813 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002814 video_stream_encoder_->TriggerQualityLow();
2815
2816 // Insert 720p frame. It should be downscaled and encoded.
2817 timestamp_ms += kFrameIntervalMs;
2818 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2819 WaitForEncodedFrame(960, 540);
2820
2821 // Trigger adapt up. Higher resolution should not be requested duo to lack
2822 // of bitrate.
2823 video_stream_encoder_->TriggerQualityHigh();
2824 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2825
2826 // Increase bitrate.
2827 video_stream_encoder_->OnBitrateUpdated(
2828 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2829 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002830 DataRate::bps(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002831
2832 // Trigger adapt up. Higher resolution should be requested.
2833 video_stream_encoder_->TriggerQualityHigh();
2834 VerifyFpsMaxResolutionMax(source.sink_wants());
2835
2836 video_stream_encoder_->Stop();
2837}
2838
2839TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2840 fake_encoder_.SetResolutionBitrateLimits(
2841 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2842
2843 // Set bitrate equal to min bitrate of 540p.
2844 video_stream_encoder_->OnBitrateUpdated(
2845 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2846 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01002847 DataRate::bps(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0, 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002848
2849 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2850 AdaptingFrameForwarder source;
2851 source.set_adaptation_enabled(true);
2852 video_stream_encoder_->SetSource(
2853 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2854
2855 // Insert 720p frame. It should be dropped and lower resolution should be
2856 // requested.
2857 int64_t timestamp_ms = kFrameIntervalMs;
2858 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2859 ExpectDroppedFrame();
2860 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2861
2862 // Insert 720p frame. It should be downscaled and encoded.
2863 timestamp_ms += kFrameIntervalMs;
2864 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2865 WaitForEncodedFrame(960, 540);
2866
2867 video_stream_encoder_->Stop();
2868}
2869
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002870class BalancedDegradationTest : public VideoStreamEncoderTest {
2871 protected:
2872 void SetupTest() {
2873 // Reset encoder for field trials to take effect.
2874 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002875 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002876
2877 // Enable BALANCED preference.
2878 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002879 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2880 }
2881
2882 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01002883 video_stream_encoder_->OnBitrateUpdated(
2884 DataRate::bps(bitrate_bps), DataRate::bps(bitrate_bps),
2885 DataRate::bps(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002886 }
2887
Åsa Persson45b176f2019-09-30 11:19:05 +02002888 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002889 timestamp_ms_ += kFrameIntervalMs;
2890 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002891 }
2892
2893 void InsertFrameAndWaitForEncoded() {
2894 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002895 sink_.WaitForEncodedFrame(timestamp_ms_);
2896 }
2897
2898 const int kWidth = 640; // pixels:640x360=230400
2899 const int kHeight = 360;
2900 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2901 int64_t timestamp_ms_ = 0;
2902 AdaptingFrameForwarder source_;
2903};
2904
2905TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2906 test::ScopedFieldTrials field_trials(
2907 "WebRTC-Video-BalancedDegradationSettings/"
2908 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2909 SetupTest();
2910
2911 // Force input frame rate.
2912 const int kInputFps = 24;
2913 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2914 stats.input_frame_rate = kInputFps;
2915 stats_proxy_->SetMockStats(stats);
2916
Åsa Persson45b176f2019-09-30 11:19:05 +02002917 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002918 VerifyFpsMaxResolutionMax(source_.sink_wants());
2919
2920 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2921 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2922 video_stream_encoder_->TriggerQualityLowExpectFalse();
2923 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2924
2925 video_stream_encoder_->Stop();
2926}
2927
2928TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2929 test::ScopedFieldTrials field_trials(
2930 "WebRTC-Video-BalancedDegradationSettings/"
2931 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2932 SetupTest();
2933
2934 // Force input frame rate.
2935 const int kInputFps = 25;
2936 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2937 stats.input_frame_rate = kInputFps;
2938 stats_proxy_->SetMockStats(stats);
2939
Åsa Persson45b176f2019-09-30 11:19:05 +02002940 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002941 VerifyFpsMaxResolutionMax(source_.sink_wants());
2942
2943 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2944 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2945 video_stream_encoder_->TriggerQualityLow();
2946 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2947
2948 video_stream_encoder_->Stop();
2949}
2950
2951TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2952 test::ScopedFieldTrials field_trials(
2953 "WebRTC-Video-BalancedDegradationSettings/"
2954 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2955 SetupTest();
2956
2957 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2958
Åsa Persson45b176f2019-09-30 11:19:05 +02002959 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002960 VerifyFpsMaxResolutionMax(source_.sink_wants());
2961
2962 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2963 video_stream_encoder_->TriggerQualityLow();
2964 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2965
2966 video_stream_encoder_->Stop();
2967}
2968
Åsa Perssonccfb3402019-09-25 15:13:04 +02002969TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002970 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002971 "WebRTC-Video-BalancedDegradationSettings/"
2972 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002973 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002974
Åsa Persson1b247f12019-08-14 17:26:39 +02002975 const int kMinBitrateBps = 425000;
2976 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002977 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002978
Åsa Persson45b176f2019-09-30 11:19:05 +02002979 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002980 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002981 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2982
2983 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2984 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002985 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002986 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002987 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2988
2989 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2990 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002991 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002992 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002993 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2994
Åsa Persson30ab0152019-08-27 12:22:33 +02002995 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2996 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002997 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002998 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2999 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02003000 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3001
3002 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02003003 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003004 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003005 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02003006
Åsa Persson30ab0152019-08-27 12:22:33 +02003007 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003008 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003009 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003010 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003011 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003012 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3013
3014 video_stream_encoder_->Stop();
3015}
3016
Åsa Perssonccfb3402019-09-25 15:13:04 +02003017TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003018 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3019 test::ScopedFieldTrials field_trials(
3020 "WebRTC-Video-BalancedDegradationSettings/"
3021 "pixels:57600|129600|230400,fps:7|24|24/");
3022 SetupTest();
3023 OnBitrateUpdated(kLowTargetBitrateBps);
3024
3025 VerifyNoLimitation(source_.sink_wants());
3026
3027 // Insert frame, expect scaled down:
3028 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3029 InsertFrame();
3030 EXPECT_FALSE(WaitForFrame(1000));
3031 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3032 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3033
3034 // Insert frame, expect scaled down:
3035 // resolution (320x180@24fps).
3036 InsertFrame();
3037 EXPECT_FALSE(WaitForFrame(1000));
3038 EXPECT_LT(source_.sink_wants().max_pixel_count,
3039 source_.last_wants().max_pixel_count);
3040 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3041
3042 // Frame should not be dropped (min pixels per frame reached).
3043 InsertFrameAndWaitForEncoded();
3044
3045 video_stream_encoder_->Stop();
3046}
3047
3048TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003049 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003050 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003051 "WebRTC-Video-BalancedDegradationSettings/"
3052 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003053 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003054
Åsa Persson30ab0152019-08-27 12:22:33 +02003055 const int kResolutionMinBitrateBps = 435000;
3056 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003057 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003058
Åsa Persson45b176f2019-09-30 11:19:05 +02003059 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003060 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003061 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3062
3063 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3064 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003065 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003066 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003067 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3068
3069 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3070 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003071 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003072 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003073 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3074
3075 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3076 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003077 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003078 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003079 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3080
Åsa Persson30ab0152019-08-27 12:22:33 +02003081 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3082 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003083 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003084 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003085 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3086
3087 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3088 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003089 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003090 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3091
3092 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003093 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003094 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003095 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003096 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003097 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3098
3099 video_stream_encoder_->Stop();
3100}
3101
Åsa Perssonccfb3402019-09-25 15:13:04 +02003102TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003103 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003104 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003105 "WebRTC-Video-BalancedDegradationSettings/"
3106 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003107 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003108
Åsa Persson30ab0152019-08-27 12:22:33 +02003109 const int kMinBitrateBps = 425000;
3110 const int kTooLowMinBitrateBps = 424000;
3111 const int kResolutionMinBitrateBps = 435000;
3112 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003113 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003114
Åsa Persson45b176f2019-09-30 11:19:05 +02003115 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003116 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003117 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3118
3119 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3120 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003121 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003122 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003123 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3124
3125 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3126 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003127 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003128 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003129 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3130
3131 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3132 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003133 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003134 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003135 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3136
3137 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3138 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003139 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003140 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3141
3142 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003143 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003144 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003145 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003146 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003147 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3148
3149 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003150 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003151 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003152 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003153 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3154
3155 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003156 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003157 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003158 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003159 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003160 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3161
Åsa Persson1b247f12019-08-14 17:26:39 +02003162 video_stream_encoder_->Stop();
3163}
3164
mflodmancc3d4422017-08-03 08:27:51 -07003165TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003166 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3167 const int kWidth = 1280;
3168 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003169 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003170 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003171 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003172
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003173 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003174 AdaptingFrameForwarder source;
3175 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003176 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003177 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003178
Åsa Persson8c1bf952018-09-13 10:42:19 +02003179 int64_t timestamp_ms = kFrameIntervalMs;
3180 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003181 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003182 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003183 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3184 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3185 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3186 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3187
3188 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003189 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003190 timestamp_ms += kFrameIntervalMs;
3191 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3192 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003193 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003194 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3195 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3196 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3197 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3198
3199 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003200 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003201 timestamp_ms += kFrameIntervalMs;
3202 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3203 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003204 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003205 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3206 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3207 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3208 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3209
Jonathan Yubc771b72017-12-08 17:04:29 -08003210 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003211 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003212 timestamp_ms += kFrameIntervalMs;
3213 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3214 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003215 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003216 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3217 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003218 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003219 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3220
Jonathan Yubc771b72017-12-08 17:04:29 -08003221 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003222 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003223 timestamp_ms += kFrameIntervalMs;
3224 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3225 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003226 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003227 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003228 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3229 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3230 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3231 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3232
Jonathan Yubc771b72017-12-08 17:04:29 -08003233 // Trigger quality adapt down, expect no change (min resolution reached).
3234 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003235 timestamp_ms += kFrameIntervalMs;
3236 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3237 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003238 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3239 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3240 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3241 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3242 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3243
3244 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003245 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003246 timestamp_ms += kFrameIntervalMs;
3247 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3248 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003249 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003250 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3251 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3252 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3253 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3254
3255 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3256 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003257 timestamp_ms += kFrameIntervalMs;
3258 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3259 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003260 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3261 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3262 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3263 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3264 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3265
3266 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3267 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003268 timestamp_ms += kFrameIntervalMs;
3269 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3270 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003271 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003272 last_wants = source.sink_wants();
3273 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3274 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003275 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003276 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3277
3278 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003279 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003280 timestamp_ms += kFrameIntervalMs;
3281 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3282 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003283 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003284 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3285 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003286 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003287 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3288
3289 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003290 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003291 timestamp_ms += kFrameIntervalMs;
3292 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003293 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003294 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003295 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003296 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3297 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003298 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003299 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003300
mflodmancc3d4422017-08-03 08:27:51 -07003301 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003302}
3303
mflodmancc3d4422017-08-03 08:27:51 -07003304TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003305 const int kWidth = 640;
3306 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003307
Erik Språng4c6ca302019-04-08 15:14:01 +02003308 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003309 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003310 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003311
perkj803d97f2016-11-01 11:45:46 -07003312 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003313 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003314 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003315 }
3316
mflodmancc3d4422017-08-03 08:27:51 -07003317 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003318 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003319 video_source_.IncomingCapturedFrame(CreateFrame(
3320 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003321 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003322 }
3323
mflodmancc3d4422017-08-03 08:27:51 -07003324 video_stream_encoder_->Stop();
3325 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003326 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003327
Ying Wangef3998f2019-12-09 13:06:53 +01003328 EXPECT_METRIC_EQ(
3329 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3330 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003331 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3332}
3333
mflodmancc3d4422017-08-03 08:27:51 -07003334TEST_F(VideoStreamEncoderTest,
3335 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003336 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003337 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003338 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003339 const int kWidth = 640;
3340 const int kHeight = 360;
3341
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003342 video_stream_encoder_->SetSource(&video_source_,
3343 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003344
3345 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3346 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003347 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003348 }
3349
mflodmancc3d4422017-08-03 08:27:51 -07003350 video_stream_encoder_->Stop();
3351 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003352 stats_proxy_.reset();
3353
3354 EXPECT_EQ(0,
3355 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3356}
3357
mflodmancc3d4422017-08-03 08:27:51 -07003358TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003359 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003360 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003361
3362 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003363 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003364 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003365 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3366 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003367
sprang57c2fff2017-01-16 06:24:02 -08003368 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003369 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003370 video_stream_encoder_->OnBitrateUpdated(
3371 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003372 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003373
sprang57c2fff2017-01-16 06:24:02 -08003374 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003375 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3376 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003377 VideoBitrateAllocation bitrate_allocation =
3378 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003379 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003380 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003381 // TODO(srte): The use of millisecs here looks like an error, but the tests
3382 // fails using seconds, this should be investigated.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003383 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003384
3385 // Not called on second frame.
3386 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3387 .Times(0);
3388 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003389 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3390 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003391 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003392
3393 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003394 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3395 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003396 const int64_t start_time_ms = rtc::TimeMillis();
3397 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3398 video_source_.IncomingCapturedFrame(
3399 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3400 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003401 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003402 }
3403
3404 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003405 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003406
mflodmancc3d4422017-08-03 08:27:51 -07003407 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003408}
3409
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003410TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3411 // 2 TLs configured, temporal layers supported by encoder.
3412 const int kNumTemporalLayers = 2;
3413 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3414 fake_encoder_.SetTemporalLayersSupported(0, true);
3415
3416 // Bitrate allocated across temporal layers.
3417 const int kTl0Bps = kTargetBitrateBps *
3418 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003419 kNumTemporalLayers, /*temporal_id*/ 0,
3420 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003421 const int kTl1Bps = kTargetBitrateBps *
3422 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003423 kNumTemporalLayers, /*temporal_id*/ 1,
3424 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003425 VideoBitrateAllocation expected_bitrate;
3426 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3427 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3428
3429 VerifyAllocatedBitrate(expected_bitrate);
3430 video_stream_encoder_->Stop();
3431}
3432
3433TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3434 // 2 TLs configured, temporal layers not supported by encoder.
3435 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3436 fake_encoder_.SetTemporalLayersSupported(0, false);
3437
3438 // Temporal layers not supported by the encoder.
3439 // Total bitrate should be at ti:0.
3440 VideoBitrateAllocation expected_bitrate;
3441 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3442
3443 VerifyAllocatedBitrate(expected_bitrate);
3444 video_stream_encoder_->Stop();
3445}
3446
3447TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3448 // 2 TLs configured, temporal layers only supported for first stream.
3449 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3450 fake_encoder_.SetTemporalLayersSupported(0, true);
3451 fake_encoder_.SetTemporalLayersSupported(1, false);
3452
3453 const int kS0Bps = 150000;
3454 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003455 kS0Bps *
3456 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3457 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003458 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003459 kS0Bps *
3460 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3461 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003462 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3463 // Temporal layers not supported by si:1.
3464 VideoBitrateAllocation expected_bitrate;
3465 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3466 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3467 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3468
3469 VerifyAllocatedBitrate(expected_bitrate);
3470 video_stream_encoder_->Stop();
3471}
3472
Niels Möller7dc26b72017-12-06 10:27:48 +01003473TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3474 const int kFrameWidth = 1280;
3475 const int kFrameHeight = 720;
3476 const int kFramerate = 24;
3477
Erik Språng4c6ca302019-04-08 15:14:01 +02003478 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003479 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003480 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003481 test::FrameForwarder source;
3482 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003483 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003484
3485 // Insert a single frame, triggering initial configuration.
3486 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3487 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3488
3489 EXPECT_EQ(
3490 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3491 kDefaultFramerate);
3492
3493 // Trigger reconfigure encoder (without resetting the entire instance).
3494 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003495 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003496 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3497 video_encoder_config.number_of_streams = 1;
3498 video_encoder_config.video_stream_factory =
3499 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3500 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003501 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003502 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3503
3504 // Detector should be updated with fps limit from codec config.
3505 EXPECT_EQ(
3506 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3507 kFramerate);
3508
3509 // Trigger overuse, max framerate should be reduced.
3510 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3511 stats.input_frame_rate = kFramerate;
3512 stats_proxy_->SetMockStats(stats);
3513 video_stream_encoder_->TriggerCpuOveruse();
3514 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3515 int adapted_framerate =
3516 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3517 EXPECT_LT(adapted_framerate, kFramerate);
3518
3519 // Trigger underuse, max framerate should go back to codec configured fps.
3520 // Set extra low fps, to make sure it's actually reset, not just incremented.
3521 stats = stats_proxy_->GetStats();
3522 stats.input_frame_rate = adapted_framerate / 2;
3523 stats_proxy_->SetMockStats(stats);
3524 video_stream_encoder_->TriggerCpuNormalUsage();
3525 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3526 EXPECT_EQ(
3527 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3528 kFramerate);
3529
3530 video_stream_encoder_->Stop();
3531}
3532
3533TEST_F(VideoStreamEncoderTest,
3534 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3535 const int kFrameWidth = 1280;
3536 const int kFrameHeight = 720;
3537 const int kLowFramerate = 15;
3538 const int kHighFramerate = 25;
3539
Erik Språng4c6ca302019-04-08 15:14:01 +02003540 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003541 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003542 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003543 test::FrameForwarder source;
3544 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003545 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003546
3547 // Trigger initial configuration.
3548 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003549 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003550 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3551 video_encoder_config.number_of_streams = 1;
3552 video_encoder_config.video_stream_factory =
3553 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3554 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3555 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003556 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003557 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3558
3559 EXPECT_EQ(
3560 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3561 kLowFramerate);
3562
3563 // Trigger overuse, max framerate should be reduced.
3564 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3565 stats.input_frame_rate = kLowFramerate;
3566 stats_proxy_->SetMockStats(stats);
3567 video_stream_encoder_->TriggerCpuOveruse();
3568 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3569 int adapted_framerate =
3570 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3571 EXPECT_LT(adapted_framerate, kLowFramerate);
3572
3573 // Reconfigure the encoder with a new (higher max framerate), max fps should
3574 // still respect the adaptation.
3575 video_encoder_config.video_stream_factory =
3576 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3577 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3578 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003579 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003580 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3581
3582 EXPECT_EQ(
3583 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3584 adapted_framerate);
3585
3586 // Trigger underuse, max framerate should go back to codec configured fps.
3587 stats = stats_proxy_->GetStats();
3588 stats.input_frame_rate = adapted_framerate;
3589 stats_proxy_->SetMockStats(stats);
3590 video_stream_encoder_->TriggerCpuNormalUsage();
3591 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3592 EXPECT_EQ(
3593 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3594 kHighFramerate);
3595
3596 video_stream_encoder_->Stop();
3597}
3598
mflodmancc3d4422017-08-03 08:27:51 -07003599TEST_F(VideoStreamEncoderTest,
3600 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003601 const int kFrameWidth = 1280;
3602 const int kFrameHeight = 720;
3603 const int kFramerate = 24;
3604
Erik Språng4c6ca302019-04-08 15:14:01 +02003605 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003606 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003607 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003608 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003609 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003610 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003611
3612 // Trigger initial configuration.
3613 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003614 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003615 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3616 video_encoder_config.number_of_streams = 1;
3617 video_encoder_config.video_stream_factory =
3618 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3619 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003620 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003621 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003622 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003623
Niels Möller7dc26b72017-12-06 10:27:48 +01003624 EXPECT_EQ(
3625 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3626 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003627
3628 // Trigger overuse, max framerate should be reduced.
3629 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3630 stats.input_frame_rate = kFramerate;
3631 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003632 video_stream_encoder_->TriggerCpuOveruse();
3633 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003634 int adapted_framerate =
3635 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003636 EXPECT_LT(adapted_framerate, kFramerate);
3637
3638 // Change degradation preference to not enable framerate scaling. Target
3639 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003640 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003641 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003642 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003643 EXPECT_EQ(
3644 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3645 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003646
mflodmancc3d4422017-08-03 08:27:51 -07003647 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003648}
3649
mflodmancc3d4422017-08-03 08:27:51 -07003650TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003651 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003652 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003653 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003654 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003655 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003656 const int kWidth = 640;
3657 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003658
asaperssonfab67072017-04-04 05:51:49 -07003659 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003660
3661 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003662 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003663
3664 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003665 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003666
sprangc5d62e22017-04-02 23:53:04 -07003667 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003668
asaperssonfab67072017-04-04 05:51:49 -07003669 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003670 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003671 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003672
3673 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003674 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003675
sprangc5d62e22017-04-02 23:53:04 -07003676 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003677
mflodmancc3d4422017-08-03 08:27:51 -07003678 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003679}
3680
mflodmancc3d4422017-08-03 08:27:51 -07003681TEST_F(VideoStreamEncoderTest,
3682 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003683 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003684 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003685 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003686 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003687 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003688 const int kWidth = 640;
3689 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003690
3691 // We expect the n initial frames to get dropped.
3692 int i;
3693 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003694 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003695 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003696 }
3697 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003698 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003699 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003700
3701 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003702 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003703
mflodmancc3d4422017-08-03 08:27:51 -07003704 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003705}
3706
mflodmancc3d4422017-08-03 08:27:51 -07003707TEST_F(VideoStreamEncoderTest,
3708 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003709 const int kWidth = 640;
3710 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003711 video_stream_encoder_->OnBitrateUpdated(
3712 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003713 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003714
3715 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003716 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003717 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003718
asaperssonfab67072017-04-04 05:51:49 -07003719 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003720 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003721 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003722
mflodmancc3d4422017-08-03 08:27:51 -07003723 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003724}
3725
mflodmancc3d4422017-08-03 08:27:51 -07003726TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003727 const int kWidth = 640;
3728 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003729 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003730
3731 VideoEncoderConfig video_encoder_config;
3732 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3733 // Make format different, to force recreation of encoder.
3734 video_encoder_config.video_format.parameters["foo"] = "foo";
3735 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003736 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003737 video_stream_encoder_->OnBitrateUpdated(
3738 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003739 DataRate::bps(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003740
kthelgasonb83797b2017-02-14 11:57:25 -08003741 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003742 video_stream_encoder_->SetSource(&video_source_,
3743 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003744
asaperssonfab67072017-04-04 05:51:49 -07003745 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003746 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003747 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003748
mflodmancc3d4422017-08-03 08:27:51 -07003749 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003750 fake_encoder_.SetQualityScaling(true);
3751}
3752
Åsa Persson139f4dc2019-08-02 09:29:58 +02003753TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3754 webrtc::test::ScopedFieldTrials field_trials(
3755 "WebRTC-Video-QualityScalerSettings/"
3756 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3757 // Reset encoder for field trials to take effect.
3758 ConfigureEncoder(video_encoder_config_.Copy());
3759 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3760 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3761 const int kWidth = 640;
3762 const int kHeight = 360;
3763
3764 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003765 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003766 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003767 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3768 // Frame should not be dropped.
3769 WaitForEncodedFrame(1);
3770
3771 video_stream_encoder_->OnBitrateUpdated(
3772 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003773 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003774 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003775 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3776 // Frame should not be dropped.
3777 WaitForEncodedFrame(2);
3778
3779 video_stream_encoder_->OnBitrateUpdated(
3780 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003781 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003782 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003783 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3784 // Expect to drop this frame, the wait should time out.
3785 ExpectDroppedFrame();
3786
3787 // Expect the sink_wants to specify a scaled frame.
3788 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3789 video_stream_encoder_->Stop();
3790}
3791
Åsa Perssone644a032019-11-08 15:56:00 +01003792TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3793 webrtc::test::ScopedFieldTrials field_trials(
3794 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3795
3796 // Reset encoder for field trials to take effect.
3797 VideoEncoderConfig config = video_encoder_config_.Copy();
3798 config.max_bitrate_bps = kTargetBitrateBps;
3799 ConfigureEncoder(std::move(config));
3800 fake_encoder_.SetQp(kQpLow);
3801
3802 // Enable MAINTAIN_FRAMERATE preference.
3803 AdaptingFrameForwarder source;
3804 source.set_adaptation_enabled(true);
3805 video_stream_encoder_->SetSource(&source,
3806 DegradationPreference::MAINTAIN_FRAMERATE);
3807
3808 // Start at low bitrate.
3809 const int kLowBitrateBps = 200000;
Ying Wang9b881ab2020-02-07 14:29:32 +01003810 video_stream_encoder_->OnBitrateUpdated(
3811 DataRate::bps(kLowBitrateBps), DataRate::bps(kLowBitrateBps),
3812 DataRate::bps(kLowBitrateBps), 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003813
3814 // Expect first frame to be dropped and resolution to be limited.
3815 const int kWidth = 1280;
3816 const int kHeight = 720;
3817 const int64_t kFrameIntervalMs = 100;
3818 int64_t timestamp_ms = kFrameIntervalMs;
3819 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3820 ExpectDroppedFrame();
3821 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3822
3823 // Increase bitrate to encoder max.
3824 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(config.max_bitrate_bps),
3825 DataRate::bps(config.max_bitrate_bps),
3826 DataRate::bps(config.max_bitrate_bps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003827 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003828
3829 // Insert frames and advance |min_duration_ms|.
3830 for (size_t i = 1; i <= 10; i++) {
3831 timestamp_ms += kFrameIntervalMs;
3832 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3833 WaitForEncodedFrame(timestamp_ms);
3834 }
3835 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3836 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3837
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003838 fake_clock_.AdvanceTime(TimeDelta::Millis(2000));
Åsa Perssone644a032019-11-08 15:56:00 +01003839
3840 // Insert frame should trigger high BW and release quality limitation.
3841 timestamp_ms += kFrameIntervalMs;
3842 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3843 WaitForEncodedFrame(timestamp_ms);
3844 VerifyFpsMaxResolutionMax(source.sink_wants());
3845
3846 // Frame should not be adapted.
3847 timestamp_ms += kFrameIntervalMs;
3848 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3849 WaitForEncodedFrame(kWidth, kHeight);
3850 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3851
3852 video_stream_encoder_->Stop();
3853}
3854
mflodmancc3d4422017-08-03 08:27:51 -07003855TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003856 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3857 const int kTooSmallWidth = 10;
3858 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003859 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003860 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003861 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003862
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003863 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003864 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003865 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003866 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003867 VerifyNoLimitation(source.sink_wants());
3868 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3869
3870 // Trigger adapt down, too small frame, expect no change.
3871 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003872 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003873 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003874 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003875 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3876 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3877
mflodmancc3d4422017-08-03 08:27:51 -07003878 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003879}
3880
mflodmancc3d4422017-08-03 08:27:51 -07003881TEST_F(VideoStreamEncoderTest,
3882 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003883 const int kTooSmallWidth = 10;
3884 const int kTooSmallHeight = 10;
3885 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003886 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003887 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003888 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003889
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003890 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003891 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003892 video_stream_encoder_->SetSource(&source,
3893 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003894 VerifyNoLimitation(source.sink_wants());
3895 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3896 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3897
3898 // Trigger adapt down, expect limited framerate.
3899 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003900 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003901 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003902 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3903 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3904 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3905 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3906
3907 // Trigger adapt down, too small frame, expect no change.
3908 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003909 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003910 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003911 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3912 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3913 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3914 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3915
mflodmancc3d4422017-08-03 08:27:51 -07003916 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003917}
3918
mflodmancc3d4422017-08-03 08:27:51 -07003919TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003920 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003921 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003922 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003923 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003924 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003925 const int kFrameWidth = 1280;
3926 const int kFrameHeight = 720;
3927 video_source_.IncomingCapturedFrame(
3928 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003929 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003930 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003931}
3932
sprangb1ca0732017-02-01 08:38:12 -08003933// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003934TEST_F(VideoStreamEncoderTest,
3935 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003936 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003937 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003938 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003939
3940 const int kFrameWidth = 1280;
3941 const int kFrameHeight = 720;
3942 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003943 // requested by
3944 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003945 video_source_.set_adaptation_enabled(true);
3946
3947 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003948 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003949 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003950
3951 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003952 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003953 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003954 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003955 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003956
asaperssonfab67072017-04-04 05:51:49 -07003957 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003958 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003959 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003960 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003961 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003962
mflodmancc3d4422017-08-03 08:27:51 -07003963 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003964}
sprangfe627f32017-03-29 08:24:59 -07003965
mflodmancc3d4422017-08-03 08:27:51 -07003966TEST_F(VideoStreamEncoderTest,
3967 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003968 const int kFrameWidth = 1280;
3969 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003970
Erik Språng4c6ca302019-04-08 15:14:01 +02003971 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003972 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01003973 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003974 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003975 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003976 video_source_.set_adaptation_enabled(true);
3977
sprang4847ae62017-06-27 07:06:52 -07003978 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003979
3980 video_source_.IncomingCapturedFrame(
3981 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003982 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003983
3984 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003985 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003986
3987 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003988 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003989 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003990 video_source_.IncomingCapturedFrame(
3991 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003992 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003993 }
3994
3995 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003996 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003997 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003998 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003999 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004000 video_source_.IncomingCapturedFrame(
4001 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004002 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004003 ++num_frames_dropped;
4004 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004005 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004006 }
4007 }
4008
sprang4847ae62017-06-27 07:06:52 -07004009 // Add some slack to account for frames dropped by the frame dropper.
4010 const int kErrorMargin = 1;
4011 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004012 kErrorMargin);
4013
4014 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004015 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004016 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004017 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004018 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004019 video_source_.IncomingCapturedFrame(
4020 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004021 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004022 ++num_frames_dropped;
4023 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004024 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004025 }
4026 }
sprang4847ae62017-06-27 07:06:52 -07004027 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004028 kErrorMargin);
4029
4030 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004031 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004032 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004033 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004034 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004035 video_source_.IncomingCapturedFrame(
4036 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004037 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004038 ++num_frames_dropped;
4039 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004040 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004041 }
4042 }
sprang4847ae62017-06-27 07:06:52 -07004043 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004044 kErrorMargin);
4045
4046 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004047 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004048 num_frames_dropped = 0;
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 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004054 ++num_frames_dropped;
4055 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004056 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004057 }
4058 }
4059 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4060
mflodmancc3d4422017-08-03 08:27:51 -07004061 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004062}
4063
mflodmancc3d4422017-08-03 08:27:51 -07004064TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004065 const int kFramerateFps = 5;
4066 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004067 const int kFrameWidth = 1280;
4068 const int kFrameHeight = 720;
4069
sprang4847ae62017-06-27 07:06:52 -07004070 // Reconfigure encoder with two temporal layers and screensharing, which will
4071 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004072 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004073
Erik Språng4c6ca302019-04-08 15:14:01 +02004074 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004075 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004076 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004077 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004078 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004079 video_source_.set_adaptation_enabled(true);
4080
sprang4847ae62017-06-27 07:06:52 -07004081 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004082
4083 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004084 rtc::VideoSinkWants last_wants;
4085 do {
4086 last_wants = video_source_.sink_wants();
4087
sprangc5d62e22017-04-02 23:53:04 -07004088 // Insert frames to get a new fps estimate...
4089 for (int j = 0; j < kFramerateFps; ++j) {
4090 video_source_.IncomingCapturedFrame(
4091 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004092 if (video_source_.last_sent_width()) {
4093 sink_.WaitForEncodedFrame(timestamp_ms);
4094 }
sprangc5d62e22017-04-02 23:53:04 -07004095 timestamp_ms += kFrameIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004096 fake_clock_.AdvanceTime(TimeDelta::Millis(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004097 }
4098 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004099 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004100 } while (video_source_.sink_wants().max_framerate_fps <
4101 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004102
Jonathan Yubc771b72017-12-08 17:04:29 -08004103 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004104
mflodmancc3d4422017-08-03 08:27:51 -07004105 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004106}
asaperssonf7e294d2017-06-13 23:25:22 -07004107
mflodmancc3d4422017-08-03 08:27:51 -07004108TEST_F(VideoStreamEncoderTest,
4109 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004110 const int kWidth = 1280;
4111 const int kHeight = 720;
4112 const int64_t kFrameIntervalMs = 150;
4113 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004114 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004115 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004116 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004117
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004118 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004119 AdaptingFrameForwarder source;
4120 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004121 video_stream_encoder_->SetSource(&source,
4122 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004123 timestamp_ms += kFrameIntervalMs;
4124 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004125 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004126 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004127 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4128 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4129 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4130
4131 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004132 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004133 timestamp_ms += kFrameIntervalMs;
4134 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004135 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004136 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4137 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4138 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4139 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4140
4141 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004142 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004143 timestamp_ms += kFrameIntervalMs;
4144 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004145 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004146 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4147 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4148 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4149 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4150
4151 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004152 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004153 timestamp_ms += kFrameIntervalMs;
4154 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004155 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004156 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4157 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4158 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4159 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4160
4161 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004162 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004163 timestamp_ms += kFrameIntervalMs;
4164 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004165 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004166 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4167 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4168 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4169 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4170
4171 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004172 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004173 timestamp_ms += kFrameIntervalMs;
4174 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004175 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004176 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4177 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4178 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4179 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4180
4181 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004182 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004183 timestamp_ms += kFrameIntervalMs;
4184 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004185 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004186 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4187 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4188 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4189 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4190
4191 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004192 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004193 timestamp_ms += kFrameIntervalMs;
4194 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004195 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004196 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4197 rtc::VideoSinkWants last_wants = source.sink_wants();
4198 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4199 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4200 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4201
4202 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004203 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004204 timestamp_ms += kFrameIntervalMs;
4205 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004206 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004207 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4208 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4209 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4210 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4211
4212 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004213 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004214 timestamp_ms += kFrameIntervalMs;
4215 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004216 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004217 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4218 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4219 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4220 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4221
4222 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004223 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004224 timestamp_ms += kFrameIntervalMs;
4225 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004226 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004227 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4228 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4229 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4230 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4231
4232 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004233 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004234 timestamp_ms += kFrameIntervalMs;
4235 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004236 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004237 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4238 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4239 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4240 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4241
4242 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004243 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004244 timestamp_ms += kFrameIntervalMs;
4245 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004246 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004247 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4248 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4249 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4250 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4251
4252 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004253 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004254 timestamp_ms += kFrameIntervalMs;
4255 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004256 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004257 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4258 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4259 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4260 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4261
4262 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004263 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004264 timestamp_ms += kFrameIntervalMs;
4265 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004266 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004267 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4268 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4269 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4270 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4271
Åsa Persson30ab0152019-08-27 12:22:33 +02004272 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004273 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004274 timestamp_ms += kFrameIntervalMs;
4275 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004276 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004277 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004278 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004279 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4280 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4281 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4282
4283 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004284 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004285 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004286 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4287
mflodmancc3d4422017-08-03 08:27:51 -07004288 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004289}
4290
mflodmancc3d4422017-08-03 08:27:51 -07004291TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004292 const int kWidth = 1280;
4293 const int kHeight = 720;
4294 const int64_t kFrameIntervalMs = 150;
4295 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004296 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004297 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004298 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004299
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004300 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004301 AdaptingFrameForwarder source;
4302 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004303 video_stream_encoder_->SetSource(&source,
4304 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004305 timestamp_ms += kFrameIntervalMs;
4306 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004307 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004308 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004309 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4310 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4311 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4312 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4313 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4314 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4315
4316 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004317 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004318 timestamp_ms += kFrameIntervalMs;
4319 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004320 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004321 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4322 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4323 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4324 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4325 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4326 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4327 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4328
4329 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004330 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004331 timestamp_ms += kFrameIntervalMs;
4332 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004333 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004334 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4335 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4336 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4337 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4338 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4339 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4340 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4341
4342 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004343 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004344 timestamp_ms += kFrameIntervalMs;
4345 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004346 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004347 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4348 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4349 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4350 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4351 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4352 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4353 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4354
4355 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004356 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004357 timestamp_ms += kFrameIntervalMs;
4358 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004359 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004360 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4361 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4362 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4363 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4364 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4365 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4366 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4367
4368 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004369 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004370 timestamp_ms += kFrameIntervalMs;
4371 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004372 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004373 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4374 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4375 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4376 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4377 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4378 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4379 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4380
4381 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004382 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004383 timestamp_ms += kFrameIntervalMs;
4384 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004385 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004386 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004387 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004388 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4389 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4390 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4391 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4392 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4393 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4394
4395 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004396 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004397 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004398 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4399 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4400
mflodmancc3d4422017-08-03 08:27:51 -07004401 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004402}
4403
mflodmancc3d4422017-08-03 08:27:51 -07004404TEST_F(VideoStreamEncoderTest,
4405 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004406 const int kWidth = 640;
4407 const int kHeight = 360;
4408 const int kFpsLimit = 15;
4409 const int64_t kFrameIntervalMs = 150;
4410 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004411 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004412 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004413 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004414
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004415 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004416 AdaptingFrameForwarder source;
4417 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004418 video_stream_encoder_->SetSource(&source,
4419 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004420 timestamp_ms += kFrameIntervalMs;
4421 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004422 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004423 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004424 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4425 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4426 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4427 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4428 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4429 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4430
4431 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004432 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004433 timestamp_ms += kFrameIntervalMs;
4434 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004435 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004436 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4437 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4438 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4439 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4440 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4441 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4442 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4443
4444 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004445 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004446 timestamp_ms += kFrameIntervalMs;
4447 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004448 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004449 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4450 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4451 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4452 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4453 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4454 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4455 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4456
4457 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004458 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004459 timestamp_ms += kFrameIntervalMs;
4460 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004461 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004462 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4463 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4464 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4465 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4466 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4467 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4468 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4469
4470 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004471 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004472 timestamp_ms += kFrameIntervalMs;
4473 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004474 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004475 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004476 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4477 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4478 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4479 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4480 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4481 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4482
4483 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004484 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004485 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004486 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4487 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4488
mflodmancc3d4422017-08-03 08:27:51 -07004489 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004490}
4491
mflodmancc3d4422017-08-03 08:27:51 -07004492TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004493 const int kFrameWidth = 1920;
4494 const int kFrameHeight = 1080;
4495 // 3/4 of 1920.
4496 const int kAdaptedFrameWidth = 1440;
4497 // 3/4 of 1080 rounded down to multiple of 4.
4498 const int kAdaptedFrameHeight = 808;
4499 const int kFramerate = 24;
4500
Erik Språng4c6ca302019-04-08 15:14:01 +02004501 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004502 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004503 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004504 // Trigger reconfigure encoder (without resetting the entire instance).
4505 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004506 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004507 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4508 video_encoder_config.number_of_streams = 1;
4509 video_encoder_config.video_stream_factory =
4510 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004511 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004512 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004513 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004514
4515 video_source_.set_adaptation_enabled(true);
4516
4517 video_source_.IncomingCapturedFrame(
4518 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004519 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004520
4521 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004522 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004523 video_source_.IncomingCapturedFrame(
4524 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004525 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004526
mflodmancc3d4422017-08-03 08:27:51 -07004527 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004528}
4529
mflodmancc3d4422017-08-03 08:27:51 -07004530TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004531 const int kFrameWidth = 1280;
4532 const int kFrameHeight = 720;
4533 const int kLowFps = 2;
4534 const int kHighFps = 30;
4535
Erik Språng4c6ca302019-04-08 15:14:01 +02004536 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004537 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004538 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004539
4540 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4541 max_framerate_ = kLowFps;
4542
4543 // Insert 2 seconds of 2fps video.
4544 for (int i = 0; i < kLowFps * 2; ++i) {
4545 video_source_.IncomingCapturedFrame(
4546 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4547 WaitForEncodedFrame(timestamp_ms);
4548 timestamp_ms += 1000 / kLowFps;
4549 }
4550
4551 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004552 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004553 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004554 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004555 video_source_.IncomingCapturedFrame(
4556 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4557 WaitForEncodedFrame(timestamp_ms);
4558 timestamp_ms += 1000 / kLowFps;
4559
4560 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4561
4562 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004563 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004564 const int kFrameIntervalMs = 1000 / kHighFps;
4565 max_framerate_ = kHighFps;
4566 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4567 video_source_.IncomingCapturedFrame(
4568 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4569 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4570 // be dropped if the encoder hans't been updated with the new higher target
4571 // framerate yet, causing it to overshoot the target bitrate and then
4572 // suffering the wrath of the media optimizer.
4573 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4574 timestamp_ms += kFrameIntervalMs;
4575 }
4576
4577 // Don expect correct measurement just yet, but it should be higher than
4578 // before.
4579 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4580
mflodmancc3d4422017-08-03 08:27:51 -07004581 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004582}
4583
mflodmancc3d4422017-08-03 08:27:51 -07004584TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004585 const int kFrameWidth = 1280;
4586 const int kFrameHeight = 720;
4587 const int kTargetBitrateBps = 1000000;
4588
4589 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004590 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004591 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004592 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004593 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004594 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004595
4596 // Insert a first video frame, causes another bitrate update.
4597 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4598 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4599 video_source_.IncomingCapturedFrame(
4600 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4601 WaitForEncodedFrame(timestamp_ms);
4602
4603 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004604 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
Ying Wang9b881ab2020-02-07 14:29:32 +01004605 DataRate::bps(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004606
4607 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004608 timestamp_ms += kProcessIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004609 fake_clock_.AdvanceTime(TimeDelta::Millis(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004610
4611 // Bitrate observer should not be called.
4612 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4613 video_source_.IncomingCapturedFrame(
4614 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4615 ExpectDroppedFrame();
4616
mflodmancc3d4422017-08-03 08:27:51 -07004617 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004618}
ilnik6b826ef2017-06-16 06:53:48 -07004619
Niels Möller4db138e2018-04-19 09:04:13 +02004620TEST_F(VideoStreamEncoderTest,
4621 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4622 const int kFrameWidth = 1280;
4623 const int kFrameHeight = 720;
4624 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004625 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004626 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004627 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004628 video_source_.IncomingCapturedFrame(
4629 CreateFrame(1, kFrameWidth, kFrameHeight));
4630 WaitForEncodedFrame(1);
4631 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4632 .low_encode_usage_threshold_percent,
4633 default_options.low_encode_usage_threshold_percent);
4634 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4635 .high_encode_usage_threshold_percent,
4636 default_options.high_encode_usage_threshold_percent);
4637 video_stream_encoder_->Stop();
4638}
4639
4640TEST_F(VideoStreamEncoderTest,
4641 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4642 const int kFrameWidth = 1280;
4643 const int kFrameHeight = 720;
4644 CpuOveruseOptions hardware_options;
4645 hardware_options.low_encode_usage_threshold_percent = 150;
4646 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004647 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004648
Erik Språng4c6ca302019-04-08 15:14:01 +02004649 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004650 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004651 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004652 video_source_.IncomingCapturedFrame(
4653 CreateFrame(1, kFrameWidth, kFrameHeight));
4654 WaitForEncodedFrame(1);
4655 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4656 .low_encode_usage_threshold_percent,
4657 hardware_options.low_encode_usage_threshold_percent);
4658 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4659 .high_encode_usage_threshold_percent,
4660 hardware_options.high_encode_usage_threshold_percent);
4661 video_stream_encoder_->Stop();
4662}
4663
Niels Möller6bb5ab92019-01-11 11:11:10 +01004664TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4665 const int kFrameWidth = 320;
4666 const int kFrameHeight = 240;
4667 const int kFps = 30;
4668 const int kTargetBitrateBps = 120000;
4669 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4670
Erik Språng4c6ca302019-04-08 15:14:01 +02004671 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004672 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004673 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004674
4675 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4676 max_framerate_ = kFps;
4677
4678 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4679 fake_encoder_.SimulateOvershoot(1.0);
4680 int num_dropped = 0;
4681 for (int i = 0; i < kNumFramesInRun; ++i) {
4682 video_source_.IncomingCapturedFrame(
4683 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4684 // Wait up to two frame durations for a frame to arrive.
4685 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4686 ++num_dropped;
4687 }
4688 timestamp_ms += 1000 / kFps;
4689 }
4690
Erik Språnga8d48ab2019-02-08 14:17:40 +01004691 // Framerate should be measured to be near the expected target rate.
4692 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4693
4694 // Frame drops should be within 5% of expected 0%.
4695 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004696
4697 // Make encoder produce frames at double the expected bitrate during 3 seconds
4698 // of video, verify number of drops. Rate needs to be slightly changed in
4699 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004700 double overshoot_factor = 2.0;
4701 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4702 // With bitrate adjuster, when need to overshoot even more to trigger
4703 // frame dropping.
4704 overshoot_factor *= 2;
4705 }
4706 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004707 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004708 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004709 DataRate::bps(kTargetBitrateBps + 1000),
Ying Wang9b881ab2020-02-07 14:29:32 +01004710 DataRate::bps(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004711 num_dropped = 0;
4712 for (int i = 0; i < kNumFramesInRun; ++i) {
4713 video_source_.IncomingCapturedFrame(
4714 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4715 // Wait up to two frame durations for a frame to arrive.
4716 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4717 ++num_dropped;
4718 }
4719 timestamp_ms += 1000 / kFps;
4720 }
4721
Erik Språng4c6ca302019-04-08 15:14:01 +02004722 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004723 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004724 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004725
4726 // Target framerate should be still be near the expected target, despite
4727 // the frame drops.
4728 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4729
4730 // Frame drops should be within 5% of expected 50%.
4731 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004732
4733 video_stream_encoder_->Stop();
4734}
4735
4736TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4737 const int kFrameWidth = 320;
4738 const int kFrameHeight = 240;
4739 const int kActualInputFps = 24;
4740 const int kTargetBitrateBps = 120000;
4741
4742 ASSERT_GT(max_framerate_, kActualInputFps);
4743
4744 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4745 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004746 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004747 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004748 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004749
4750 // Insert 3 seconds of video, with an input fps lower than configured max.
4751 for (int i = 0; i < kActualInputFps * 3; ++i) {
4752 video_source_.IncomingCapturedFrame(
4753 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4754 // Wait up to two frame durations for a frame to arrive.
4755 WaitForEncodedFrame(timestamp_ms);
4756 timestamp_ms += 1000 / kActualInputFps;
4757 }
4758
4759 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4760
4761 video_stream_encoder_->Stop();
4762}
4763
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004764TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4765 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004766 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004767 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004768 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004769
4770 fake_encoder_.BlockNextEncode();
4771 video_source_.IncomingCapturedFrame(
4772 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4773 WaitForEncodedFrame(1);
4774 // On the very first frame full update should be forced.
4775 rect = fake_encoder_.GetLastUpdateRect();
4776 EXPECT_EQ(rect.offset_x, 0);
4777 EXPECT_EQ(rect.offset_y, 0);
4778 EXPECT_EQ(rect.height, codec_height_);
4779 EXPECT_EQ(rect.width, codec_width_);
4780 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4781 // call to ContinueEncode.
4782 video_source_.IncomingCapturedFrame(
4783 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4784 ExpectDroppedFrame();
4785 video_source_.IncomingCapturedFrame(
4786 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4787 ExpectDroppedFrame();
4788 fake_encoder_.ContinueEncode();
4789 WaitForEncodedFrame(3);
4790 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4791 rect = fake_encoder_.GetLastUpdateRect();
4792 EXPECT_EQ(rect.offset_x, 1);
4793 EXPECT_EQ(rect.offset_y, 0);
4794 EXPECT_EQ(rect.width, 10);
4795 EXPECT_EQ(rect.height, 1);
4796
4797 video_source_.IncomingCapturedFrame(
4798 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4799 WaitForEncodedFrame(4);
4800 // Previous frame was encoded, so no accumulation should happen.
4801 rect = fake_encoder_.GetLastUpdateRect();
4802 EXPECT_EQ(rect.offset_x, 0);
4803 EXPECT_EQ(rect.offset_y, 0);
4804 EXPECT_EQ(rect.width, 1);
4805 EXPECT_EQ(rect.height, 1);
4806
4807 video_stream_encoder_->Stop();
4808}
4809
Erik Språngd7329ca2019-02-21 21:19:53 +01004810TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004811 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004812 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004813 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004814
4815 // First frame is always keyframe.
4816 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4817 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004818 EXPECT_THAT(
4819 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004820 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004821
4822 // Insert delta frame.
4823 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4824 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004825 EXPECT_THAT(
4826 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004827 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004828
4829 // Request next frame be a key-frame.
4830 video_stream_encoder_->SendKeyFrame();
4831 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4832 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004833 EXPECT_THAT(
4834 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004835 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004836
4837 video_stream_encoder_->Stop();
4838}
4839
4840TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4841 // Setup simulcast with three streams.
4842 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004843 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004844 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004845 DataRate::bps(kSimulcastTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004846 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004847 // Wait for all three layers before triggering event.
4848 sink_.SetNumExpectedLayers(3);
4849
4850 // First frame is always keyframe.
4851 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4852 WaitForEncodedFrame(1);
4853 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004854 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4855 VideoFrameType::kVideoFrameKey,
4856 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004857
4858 // Insert delta frame.
4859 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4860 WaitForEncodedFrame(2);
4861 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004862 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4863 VideoFrameType::kVideoFrameDelta,
4864 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004865
4866 // Request next frame be a key-frame.
4867 // Only first stream is configured to produce key-frame.
4868 video_stream_encoder_->SendKeyFrame();
4869 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4870 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004871
4872 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4873 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004874 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004875 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004876 VideoFrameType::kVideoFrameKey,
4877 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004878
4879 video_stream_encoder_->Stop();
4880}
4881
4882TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4883 // Configure internal source factory and setup test again.
4884 encoder_factory_.SetHasInternalSource(true);
4885 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004886 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004887 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004888 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004889
4890 // Call encoder directly, simulating internal source where encoded frame
4891 // callback in VideoStreamEncoder is called despite no OnFrame().
4892 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4893 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004894 EXPECT_THAT(
4895 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004896 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004897
Niels Möller8f7ce222019-03-21 15:43:58 +01004898 const std::vector<VideoFrameType> kDeltaFrame = {
4899 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004900 // Need to set timestamp manually since manually for injected frame.
4901 VideoFrame frame = CreateFrame(101, nullptr);
4902 frame.set_timestamp(101);
4903 fake_encoder_.InjectFrame(frame, false);
4904 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004905 EXPECT_THAT(
4906 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004907 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004908
4909 // Request key-frame. The forces a dummy frame down into the encoder.
4910 fake_encoder_.ExpectNullFrame();
4911 video_stream_encoder_->SendKeyFrame();
4912 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004913 EXPECT_THAT(
4914 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004915 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004916
4917 video_stream_encoder_->Stop();
4918}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004919
4920TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4921 // Configure internal source factory and setup test again.
4922 encoder_factory_.SetHasInternalSource(true);
4923 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004924 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004925 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01004926 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004927
4928 int64_t timestamp = 1;
4929 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004930 image.SetEncodedData(
4931 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004932 image.capture_time_ms_ = ++timestamp;
4933 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4934 const int64_t kEncodeFinishDelayMs = 10;
4935 image.timing_.encode_start_ms = timestamp;
4936 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4937 fake_encoder_.InjectEncodedImage(image);
4938 // Wait for frame without incrementing clock.
4939 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4940 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4941 // capture timestamp should be kEncodeFinishDelayMs in the past.
4942 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4943 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4944 kEncodeFinishDelayMs);
4945
4946 video_stream_encoder_->Stop();
4947}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004948
4949TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4950 // Configure internal source factory and setup test again.
4951 encoder_factory_.SetHasInternalSource(true);
4952 ResetEncoder("H264", 1, 1, 1, false);
4953
4954 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4955 image._frameType = VideoFrameType::kVideoFrameKey;
4956
4957 CodecSpecificInfo codec_specific_info;
4958 codec_specific_info.codecType = kVideoCodecH264;
4959
4960 RTPFragmentationHeader fragmentation;
4961 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4962 fragmentation.fragmentationOffset[0] = 4;
4963 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4964
4965 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4966 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4967
4968 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4969 testing::ElementsAreArray(optimal_sps));
4970 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4971 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4972 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4973 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4974
4975 video_stream_encoder_->Stop();
4976}
4977
4978TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4979 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4980 0x00, 0x00, 0x03, 0x03, 0xF4,
4981 0x05, 0x03, 0xC7, 0xC0};
4982
4983 // Configure internal source factory and setup test again.
4984 encoder_factory_.SetHasInternalSource(true);
4985 ResetEncoder("H264", 1, 1, 1, false);
4986
4987 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4988 image._frameType = VideoFrameType::kVideoFrameKey;
4989
4990 CodecSpecificInfo codec_specific_info;
4991 codec_specific_info.codecType = kVideoCodecH264;
4992
4993 RTPFragmentationHeader fragmentation;
4994 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4995 fragmentation.fragmentationOffset[0] = 4;
4996 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4997
4998 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4999 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5000
5001 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5002 testing::ElementsAreArray(optimal_sps));
5003 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5004 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5005 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5006 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5007
5008 video_stream_encoder_->Stop();
5009}
5010
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005011TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5012 const int kFrameWidth = 1280;
5013 const int kFrameHeight = 720;
5014 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5015
5016 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02005017 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005018 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005019 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5020
5021 // Insert a first video frame. It should be dropped because of downscale in
5022 // resolution.
5023 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5024 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5025 frame.set_rotation(kVideoRotation_270);
5026 video_source_.IncomingCapturedFrame(frame);
5027
5028 ExpectDroppedFrame();
5029
5030 // Second frame is downscaled.
5031 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5032 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5033 frame.set_rotation(kVideoRotation_90);
5034 video_source_.IncomingCapturedFrame(frame);
5035
5036 WaitForEncodedFrame(timestamp_ms);
5037 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5038
5039 // Insert another frame, also downscaled.
5040 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5041 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5042 frame.set_rotation(kVideoRotation_180);
5043 video_source_.IncomingCapturedFrame(frame);
5044
5045 WaitForEncodedFrame(timestamp_ms);
5046 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5047
5048 video_stream_encoder_->Stop();
5049}
5050
Erik Språng5056af02019-09-02 15:53:11 +02005051TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5052 const int kFrameWidth = 320;
5053 const int kFrameHeight = 180;
5054
5055 // Initial rate.
5056 video_stream_encoder_->OnBitrateUpdated(
5057 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02005058 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02005059 /*link_allocation=*/DataRate::kbps(300),
5060 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005061 /*rtt_ms=*/0,
5062 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005063
5064 // Insert a first video frame so that encoder gets configured.
5065 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5066 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5067 frame.set_rotation(kVideoRotation_270);
5068 video_source_.IncomingCapturedFrame(frame);
5069 WaitForEncodedFrame(timestamp_ms);
5070
5071 // Set a target rate below the minimum allowed by the codec settings.
5072 VideoCodec codec_config = fake_encoder_.codec_config();
5073 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
5074 DataRate target_rate = min_rate - DataRate::kbps(1);
5075 video_stream_encoder_->OnBitrateUpdated(
5076 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005077 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005078 /*link_allocation=*/target_rate,
5079 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005080 /*rtt_ms=*/0,
5081 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005082 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5083
5084 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5085 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5086 ASSERT_TRUE(rate_settings.has_value());
5087 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
5088 EXPECT_EQ(min_rate, allocation_sum);
5089 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5090
5091 video_stream_encoder_->Stop();
5092}
5093
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005094TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005095 video_stream_encoder_->OnBitrateUpdated(
5096 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005097 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005098 // Capture a frame and wait for it to synchronize with the encoder thread.
5099 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5100 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5101 WaitForEncodedFrame(1);
5102
5103 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5104 ASSERT_TRUE(prev_rate_settings.has_value());
5105 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5106 kDefaultFramerate);
5107
5108 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5109 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5110 timestamp_ms += 1000 / kDefaultFramerate;
5111 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5112 WaitForEncodedFrame(timestamp_ms);
5113 }
5114 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5115 kDefaultFramerate);
5116 // Capture larger frame to trigger a reconfigure.
5117 codec_height_ *= 2;
5118 codec_width_ *= 2;
5119 timestamp_ms += 1000 / kDefaultFramerate;
5120 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5121 WaitForEncodedFrame(timestamp_ms);
5122
5123 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5124 auto current_rate_settings =
5125 fake_encoder_.GetAndResetLastRateControlSettings();
5126 // Ensure we have actually reconfigured twice
5127 // The rate settings should have been set again even though
5128 // they haven't changed.
5129 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005130 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005131
5132 video_stream_encoder_->Stop();
5133}
5134
philipeld9cc8c02019-09-16 14:53:40 +02005135struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5136 MOCK_METHOD0(RequestEncoderFallback, void());
5137 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
philipel9b058032020-02-10 11:30:00 +01005138 MOCK_METHOD1(RequestEncoderSwitch,
5139 void(const webrtc::SdpVideoFormat& format));
philipeld9cc8c02019-09-16 14:53:40 +02005140};
5141
5142TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5143 constexpr int kDontCare = 100;
5144
5145 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5146 video_send_config_.encoder_settings.encoder_switch_request_callback =
5147 &switch_callback;
5148 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5149 encoder_config.codec_type = kVideoCodecVP8;
5150 webrtc::test::ScopedFieldTrials field_trial(
5151 "WebRTC-NetworkCondition-EncoderSwitch/"
5152 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5153 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5154
5155 // Reset encoder for new configuration to take effect.
5156 ConfigureEncoder(std::move(encoder_config));
5157
5158 // Send one frame to trigger ReconfigureEncoder.
5159 video_source_.IncomingCapturedFrame(
5160 CreateFrame(kDontCare, kDontCare, kDontCare));
5161
5162 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005163 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5164 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005165 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005166 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005167
5168 video_stream_encoder_->OnBitrateUpdated(
5169 /*target_bitrate=*/DataRate::kbps(50),
5170 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5171 /*link_allocation=*/DataRate::kbps(kDontCare),
5172 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005173 /*rtt_ms=*/0,
5174 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005175
5176 video_stream_encoder_->Stop();
5177}
5178
5179TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5180 constexpr int kSufficientBitrateToNotDrop = 1000;
5181 constexpr int kHighRes = 500;
5182 constexpr int kLowRes = 100;
5183
5184 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5185 video_send_config_.encoder_settings.encoder_switch_request_callback =
5186 &switch_callback;
5187 webrtc::test::ScopedFieldTrials field_trial(
5188 "WebRTC-NetworkCondition-EncoderSwitch/"
5189 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5190 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5191 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5192 encoder_config.codec_type = kVideoCodecH264;
5193
5194 // Reset encoder for new configuration to take effect.
5195 ConfigureEncoder(std::move(encoder_config));
5196
5197 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5198 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5199 // not fail.
5200 video_stream_encoder_->OnBitrateUpdated(
5201 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5202 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5203 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5204 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005205 /*rtt_ms=*/0,
5206 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005207
5208 // Send one frame to trigger ReconfigureEncoder.
5209 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5210 WaitForEncodedFrame(1);
5211
5212 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005213 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5214 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005215 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005216 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005217
5218 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5219 WaitForEncodedFrame(2);
5220
5221 video_stream_encoder_->Stop();
5222}
5223
philipel9b058032020-02-10 11:30:00 +01005224TEST_F(VideoStreamEncoderTest, EncoderSelectorCurrentEncoderIsSignaled) {
5225 constexpr int kDontCare = 100;
5226 StrictMock<MockEncoderSelector> encoder_selector;
5227 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5228 &fake_encoder_, &encoder_selector);
5229 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5230
5231 // Reset encoder for new configuration to take effect.
5232 ConfigureEncoder(video_encoder_config_.Copy());
5233
5234 EXPECT_CALL(encoder_selector, OnCurrentEncoder(_));
5235
5236 video_source_.IncomingCapturedFrame(
5237 CreateFrame(kDontCare, kDontCare, kDontCare));
5238 video_stream_encoder_->Stop();
5239
5240 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5241 // to it's factory, so in order for the encoder instance in the
5242 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5243 // reset the |video_stream_encoder_| here.
5244 video_stream_encoder_.reset();
5245}
5246
5247TEST_F(VideoStreamEncoderTest, EncoderSelectorBitrateSwitch) {
5248 constexpr int kDontCare = 100;
5249
5250 NiceMock<MockEncoderSelector> encoder_selector;
5251 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5252 video_send_config_.encoder_settings.encoder_switch_request_callback =
5253 &switch_callback;
5254 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5255 &fake_encoder_, &encoder_selector);
5256 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5257
5258 // Reset encoder for new configuration to take effect.
5259 ConfigureEncoder(video_encoder_config_.Copy());
5260
5261 ON_CALL(encoder_selector, OnEncodingBitrate(_))
5262 .WillByDefault(Return(SdpVideoFormat("AV1")));
5263 EXPECT_CALL(switch_callback,
5264 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(
5265 Field(&SdpVideoFormat::name, "AV1"))));
5266
5267 video_stream_encoder_->OnBitrateUpdated(
5268 /*target_bitrate=*/DataRate::kbps(50),
5269 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
5270 /*link_allocation=*/DataRate::kbps(kDontCare),
5271 /*fraction_lost=*/0,
5272 /*rtt_ms=*/0,
5273 /*cwnd_reduce_ratio=*/0);
5274
5275 video_stream_encoder_->Stop();
5276}
5277
5278TEST_F(VideoStreamEncoderTest, EncoderSelectorBrokenEncoderSwitch) {
5279 constexpr int kSufficientBitrateToNotDrop = 1000;
5280 constexpr int kDontCare = 100;
5281
5282 NiceMock<MockVideoEncoder> video_encoder;
5283 NiceMock<MockEncoderSelector> encoder_selector;
5284 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5285 video_send_config_.encoder_settings.encoder_switch_request_callback =
5286 &switch_callback;
5287 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5288 &video_encoder, &encoder_selector);
5289 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5290
5291 // Reset encoder for new configuration to take effect.
5292 ConfigureEncoder(video_encoder_config_.Copy());
5293
5294 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5295 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5296 // not fail.
5297 video_stream_encoder_->OnBitrateUpdated(
5298 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5299 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5300 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
5301 /*fraction_lost=*/0,
5302 /*rtt_ms=*/0,
5303 /*cwnd_reduce_ratio=*/0);
5304
5305 ON_CALL(video_encoder, Encode(_, _))
5306 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ENCODER_FAILURE));
5307 ON_CALL(encoder_selector, OnEncoderBroken())
5308 .WillByDefault(Return(SdpVideoFormat("AV2")));
5309
5310 rtc::Event encode_attempted;
5311 EXPECT_CALL(switch_callback,
5312 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(_)))
5313 .WillOnce([&encode_attempted](const SdpVideoFormat& format) {
5314 EXPECT_EQ(format.name, "AV2");
5315 encode_attempted.Set();
5316 });
5317
5318 video_source_.IncomingCapturedFrame(CreateFrame(1, kDontCare, kDontCare));
5319 encode_attempted.Wait(3000);
5320
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
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005330TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005331 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005332 const int kFrameWidth = 320;
5333 const int kFrameHeight = 180;
5334
5335 // Set initial rate.
5336 auto rate = DataRate::kbps(100);
5337 video_stream_encoder_->OnBitrateUpdated(
5338 /*target_bitrate=*/rate,
5339 /*stable_target_bitrate=*/rate,
5340 /*link_allocation=*/rate,
5341 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005342 /*rtt_ms=*/0,
5343 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005344
5345 // Insert a first video frame so that encoder gets configured.
5346 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5347 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5348 frame.set_rotation(kVideoRotation_270);
5349 video_source_.IncomingCapturedFrame(frame);
5350 WaitForEncodedFrame(timestamp_ms);
5351 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5352
5353 // Change of target bitrate propagates to the encoder.
5354 auto new_stable_rate = rate - DataRate::kbps(5);
5355 video_stream_encoder_->OnBitrateUpdated(
5356 /*target_bitrate=*/new_stable_rate,
5357 /*stable_target_bitrate=*/new_stable_rate,
5358 /*link_allocation=*/rate,
5359 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005360 /*rtt_ms=*/0,
5361 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005362 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5363 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5364 video_stream_encoder_->Stop();
5365}
5366
5367TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005368 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005369 const int kFrameWidth = 320;
5370 const int kFrameHeight = 180;
5371
5372 // Set initial rate.
5373 auto rate = DataRate::kbps(100);
5374 video_stream_encoder_->OnBitrateUpdated(
5375 /*target_bitrate=*/rate,
5376 /*stable_target_bitrate=*/rate,
5377 /*link_allocation=*/rate,
5378 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005379 /*rtt_ms=*/0,
5380 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005381
5382 // Insert a first video frame so that encoder gets configured.
5383 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5384 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5385 frame.set_rotation(kVideoRotation_270);
5386 video_source_.IncomingCapturedFrame(frame);
5387 WaitForEncodedFrame(timestamp_ms);
5388 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5389
5390 // Set a higher target rate without changing the link_allocation. Should not
5391 // reset encoder's rate.
5392 auto new_stable_rate = rate - DataRate::kbps(5);
5393 video_stream_encoder_->OnBitrateUpdated(
5394 /*target_bitrate=*/rate,
5395 /*stable_target_bitrate=*/new_stable_rate,
5396 /*link_allocation=*/rate,
5397 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005398 /*rtt_ms=*/0,
5399 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005400 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5401 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5402 video_stream_encoder_->Stop();
5403}
5404
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005405TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5406 test::ScopedFieldTrials field_trials(
5407 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5408 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5409 const int kFramerateFps = 30;
5410 const int kWidth = 1920;
5411 const int kHeight = 1080;
5412 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5413 // Works on screenshare mode.
5414 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5415 // We rely on the automatic resolution adaptation, but we handle framerate
5416 // adaptation manually by mocking the stats proxy.
5417 video_source_.set_adaptation_enabled(true);
5418
5419 // BALANCED degradation preference is required for this feature.
5420 video_stream_encoder_->OnBitrateUpdated(
5421 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
Ying Wang9b881ab2020-02-07 14:29:32 +01005422 DataRate::bps(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005423 video_stream_encoder_->SetSource(&video_source_,
5424 webrtc::DegradationPreference::BALANCED);
5425 VerifyNoLimitation(video_source_.sink_wants());
5426
5427 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5428 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5429
5430 // Pass enough frames with the full update to trigger animation detection.
5431 for (int i = 0; i < kNumFrames; ++i) {
5432 int64_t timestamp_ms =
5433 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5434 frame.set_ntp_time_ms(timestamp_ms);
5435 frame.set_timestamp_us(timestamp_ms * 1000);
5436 video_source_.IncomingCapturedFrame(frame);
5437 WaitForEncodedFrame(timestamp_ms);
5438 }
5439
5440 // Resolution should be limited.
5441 rtc::VideoSinkWants expected;
5442 expected.max_framerate_fps = kFramerateFps;
5443 expected.max_pixel_count = 1280 * 720 + 1;
5444 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5445
5446 // Pass one frame with no known update.
5447 // Resolution cap should be removed immediately.
5448 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5449 frame.set_ntp_time_ms(timestamp_ms);
5450 frame.set_timestamp_us(timestamp_ms * 1000);
5451 frame.clear_update_rect();
5452
5453 video_source_.IncomingCapturedFrame(frame);
5454 WaitForEncodedFrame(timestamp_ms);
5455
5456 // Resolution should be unlimited now.
5457 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5458
5459 video_stream_encoder_->Stop();
5460}
5461
perkj26091b12016-09-01 01:17:40 -07005462} // namespace webrtc