blob: a8e51e891a2d7bb065295c20052adff5032bffc3 [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"
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +010028#include "call/adaptation/test/fake_resource.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020029#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070030#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020032#include "modules/video_coding/codecs/vp9/include/vp9_globals.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)),
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100160 task_queue_factory),
161 fake_cpu_resource_(
162 std::make_unique<FakeResource>(ResourceUsageState::kStable,
163 "FakeResource[CPU]")),
164 fake_quality_resource_(
165 std::make_unique<FakeResource>(ResourceUsageState::kStable,
166 "FakeResource[QP]")) {
167 InjectAdaptationResource(
168 fake_quality_resource_.get(),
169 AdaptationObserverInterface::AdaptReason::kQuality);
170 InjectAdaptationResource(fake_cpu_resource_.get(),
171 AdaptationObserverInterface::AdaptReason::kCpu);
172 }
perkj803d97f2016-11-01 11:45:46 -0700173
Henrik Boströmb08882b2020-01-07 10:11:17 +0100174 void PostTaskAndWait(bool down,
175 AdaptationObserverInterface::AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200176 PostTaskAndWait(down, reason, /*expected_results=*/true);
177 }
178
Henrik Boströmb08882b2020-01-07 10:11:17 +0100179 void PostTaskAndWait(bool down,
180 AdaptationObserverInterface::AdaptReason reason,
181 bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100182 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200183 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100184 ResourceUsageState usage_state =
185 down ? ResourceUsageState::kOveruse : ResourceUsageState::kUnderuse;
186
187 FakeResource* resource = nullptr;
188 switch (reason) {
189 case AdaptationObserverInterface::kQuality:
190 resource = fake_quality_resource_.get();
191 break;
192 case AdaptationObserverInterface::kCpu:
193 resource = fake_cpu_resource_.get();
194 break;
195 default:
196 RTC_NOTREACHED();
197 }
198
199 resource->set_usage_state(usage_state);
200 if (!expected_results) {
201 ASSERT_EQ(AdaptationObserverInterface::kQuality, reason)
202 << "We can only assert adaptation result for quality resources";
203 EXPECT_EQ(
204 ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency,
205 resource->last_response());
206 } else {
207 EXPECT_EQ(ResourceListenerResponse::kNothing,
208 resource->last_response());
209 }
210
perkj803d97f2016-11-01 11:45:46 -0700211 event.Set();
212 });
perkj070ba852017-02-16 15:46:27 -0800213 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700214 }
215
kthelgason2fc52542017-03-03 00:24:41 -0800216 // This is used as a synchronisation mechanism, to make sure that the
217 // encoder queue is not blocked before we start sending it frames.
218 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100219 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200220 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800221 ASSERT_TRUE(event.Wait(5000));
222 }
223
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200224 void TriggerCpuOveruse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100225 PostTaskAndWait(/*down=*/true,
226 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200227 }
kthelgason876222f2016-11-29 01:44:11 -0800228
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200229 void TriggerCpuNormalUsage() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100230 PostTaskAndWait(/*down=*/false,
231 AdaptationObserverInterface::AdaptReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200232 }
kthelgason876222f2016-11-29 01:44:11 -0800233
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200234 void TriggerQualityLow() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100235 PostTaskAndWait(/*down=*/true,
236 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200237 }
kthelgason876222f2016-11-29 01:44:11 -0800238
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200239 void TriggerQualityLowExpectFalse() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100240 PostTaskAndWait(/*down=*/true,
241 AdaptationObserverInterface::AdaptReason::kQuality,
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200242 /*expected_results=*/false);
243 }
244
245 void TriggerQualityHigh() {
Henrik Boströmb08882b2020-01-07 10:11:17 +0100246 PostTaskAndWait(/*down=*/false,
247 AdaptationObserverInterface::AdaptReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200248 }
sprangfda496a2017-06-15 04:21:07 -0700249
Niels Möller7dc26b72017-12-06 10:27:48 +0100250 CpuOveruseDetectorProxy* overuse_detector_proxy_;
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100251 std::unique_ptr<FakeResource> fake_cpu_resource_;
252 std::unique_ptr<FakeResource> fake_quality_resource_;
perkj803d97f2016-11-01 11:45:46 -0700253};
254
asapersson5f7226f2016-11-25 04:37:00 -0800255class VideoStreamFactory
256 : public VideoEncoderConfig::VideoStreamFactoryInterface {
257 public:
sprangfda496a2017-06-15 04:21:07 -0700258 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
259 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800260 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700261 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800262 }
263
264 private:
265 std::vector<VideoStream> CreateEncoderStreams(
266 int width,
267 int height,
268 const VideoEncoderConfig& encoder_config) override {
269 std::vector<VideoStream> streams =
270 test::CreateVideoStreams(width, height, encoder_config);
271 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100272 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700273 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800274 }
275 return streams;
276 }
sprangfda496a2017-06-15 04:21:07 -0700277
asapersson5f7226f2016-11-25 04:37:00 -0800278 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700279 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800280};
281
Noah Richards51db4212019-06-12 06:59:12 -0700282// Simulates simulcast behavior and makes highest stream resolutions divisible
283// by 4.
284class CroppingVideoStreamFactory
285 : public VideoEncoderConfig::VideoStreamFactoryInterface {
286 public:
287 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
288 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
289 EXPECT_GT(num_temporal_layers, 0u);
290 EXPECT_GT(framerate, 0);
291 }
292
293 private:
294 std::vector<VideoStream> CreateEncoderStreams(
295 int width,
296 int height,
297 const VideoEncoderConfig& encoder_config) override {
298 std::vector<VideoStream> streams = test::CreateVideoStreams(
299 width - width % 4, height - height % 4, encoder_config);
300 for (VideoStream& stream : streams) {
301 stream.num_temporal_layers = num_temporal_layers_;
302 stream.max_framerate = framerate_;
303 }
304 return streams;
305 }
306
307 const size_t num_temporal_layers_;
308 const int framerate_;
309};
310
sprangb1ca0732017-02-01 08:38:12 -0800311class AdaptingFrameForwarder : public test::FrameForwarder {
312 public:
313 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700314 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800315
316 void set_adaptation_enabled(bool enabled) {
317 rtc::CritScope cs(&crit_);
318 adaptation_enabled_ = enabled;
319 }
320
asaperssonfab67072017-04-04 05:51:49 -0700321 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800322 rtc::CritScope cs(&crit_);
323 return adaptation_enabled_;
324 }
325
asapersson09f05612017-05-15 23:40:18 -0700326 rtc::VideoSinkWants last_wants() const {
327 rtc::CritScope cs(&crit_);
328 return last_wants_;
329 }
330
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200331 absl::optional<int> last_sent_width() const { return last_width_; }
332 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800333
sprangb1ca0732017-02-01 08:38:12 -0800334 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
335 int cropped_width = 0;
336 int cropped_height = 0;
337 int out_width = 0;
338 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700339 if (adaption_enabled()) {
340 if (adapter_.AdaptFrameResolution(
341 video_frame.width(), video_frame.height(),
342 video_frame.timestamp_us() * 1000, &cropped_width,
343 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100344 VideoFrame adapted_frame =
345 VideoFrame::Builder()
346 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
347 nullptr, out_width, out_height))
348 .set_timestamp_rtp(99)
349 .set_timestamp_ms(99)
350 .set_rotation(kVideoRotation_0)
351 .build();
sprangc5d62e22017-04-02 23:53:04 -0700352 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100353 if (video_frame.has_update_rect()) {
354 adapted_frame.set_update_rect(
355 video_frame.update_rect().ScaleWithFrame(
356 video_frame.width(), video_frame.height(), 0, 0,
357 video_frame.width(), video_frame.height(), out_width,
358 out_height));
359 }
sprangc5d62e22017-04-02 23:53:04 -0700360 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800361 last_width_.emplace(adapted_frame.width());
362 last_height_.emplace(adapted_frame.height());
363 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200364 last_width_ = absl::nullopt;
365 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700366 }
sprangb1ca0732017-02-01 08:38:12 -0800367 } else {
368 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800369 last_width_.emplace(video_frame.width());
370 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800371 }
372 }
373
374 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
375 const rtc::VideoSinkWants& wants) override {
376 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700377 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100378 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800379 test::FrameForwarder::AddOrUpdateSink(sink, wants);
380 }
sprangb1ca0732017-02-01 08:38:12 -0800381 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700382 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
383 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200384 absl::optional<int> last_width_;
385 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800386};
sprangc5d62e22017-04-02 23:53:04 -0700387
Niels Möller213618e2018-07-24 09:29:58 +0200388// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700389class MockableSendStatisticsProxy : public SendStatisticsProxy {
390 public:
391 MockableSendStatisticsProxy(Clock* clock,
392 const VideoSendStream::Config& config,
393 VideoEncoderConfig::ContentType content_type)
394 : SendStatisticsProxy(clock, config, content_type) {}
395
396 VideoSendStream::Stats GetStats() override {
397 rtc::CritScope cs(&lock_);
398 if (mock_stats_)
399 return *mock_stats_;
400 return SendStatisticsProxy::GetStats();
401 }
402
Niels Möller213618e2018-07-24 09:29:58 +0200403 int GetInputFrameRate() const override {
404 rtc::CritScope cs(&lock_);
405 if (mock_stats_)
406 return mock_stats_->input_frame_rate;
407 return SendStatisticsProxy::GetInputFrameRate();
408 }
sprangc5d62e22017-04-02 23:53:04 -0700409 void SetMockStats(const VideoSendStream::Stats& stats) {
410 rtc::CritScope cs(&lock_);
411 mock_stats_.emplace(stats);
412 }
413
414 void ResetMockStats() {
415 rtc::CritScope cs(&lock_);
416 mock_stats_.reset();
417 }
418
419 private:
420 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200421 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700422};
423
sprang4847ae62017-06-27 07:06:52 -0700424class MockBitrateObserver : public VideoBitrateAllocationObserver {
425 public:
Erik Språng566124a2018-04-23 12:32:22 +0200426 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700427};
428
philipel9b058032020-02-10 11:30:00 +0100429class MockEncoderSelector
430 : public VideoEncoderFactory::EncoderSelectorInterface {
431 public:
432 MOCK_METHOD1(OnCurrentEncoder, void(const SdpVideoFormat& format));
433 MOCK_METHOD1(OnEncodingBitrate,
434 absl::optional<SdpVideoFormat>(const DataRate& rate));
435 MOCK_METHOD0(OnEncoderBroken, absl::optional<SdpVideoFormat>());
436};
437
perkj803d97f2016-11-01 11:45:46 -0700438} // namespace
439
mflodmancc3d4422017-08-03 08:27:51 -0700440class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700441 public:
442 static const int kDefaultTimeoutMs = 30 * 1000;
443
mflodmancc3d4422017-08-03 08:27:51 -0700444 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700445 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700446 codec_width_(320),
447 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200448 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200449 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700450 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200451 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700452 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700453 Clock::GetRealTimeClock(),
454 video_send_config_,
455 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700456 sink_(&fake_encoder_) {}
457
458 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700459 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700460 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200461 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800462 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200463 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200464 video_send_config_.rtp.payload_name = "FAKE";
465 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700466
Per512ecb32016-09-23 15:52:06 +0200467 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200468 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700469 video_encoder_config.video_stream_factory =
470 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100471 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700472
473 // Framerate limit is specified by the VideoStreamFactory.
474 std::vector<VideoStream> streams =
475 video_encoder_config.video_stream_factory->CreateEncoderStreams(
476 codec_width_, codec_height_, video_encoder_config);
477 max_framerate_ = streams[0].max_framerate;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100478 fake_clock_.SetTime(Timestamp::Micros(1234));
sprang4847ae62017-06-27 07:06:52 -0700479
Niels Möllerf1338562018-04-26 09:51:47 +0200480 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800481 }
482
Niels Möllerf1338562018-04-26 09:51:47 +0200483 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700484 if (video_stream_encoder_)
485 video_stream_encoder_->Stop();
486 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200487 stats_proxy_.get(), video_send_config_.encoder_settings,
488 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700489 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
490 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700491 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700492 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
493 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200494 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700495 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800496 }
497
498 void ResetEncoder(const std::string& payload_name,
499 size_t num_streams,
500 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700501 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700502 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200503 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800504
505 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200506 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800507 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100508 video_encoder_config.max_bitrate_bps =
509 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800510 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700511 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
512 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700513 video_encoder_config.content_type =
514 screenshare ? VideoEncoderConfig::ContentType::kScreen
515 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700516 if (payload_name == "VP9") {
517 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
518 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
519 video_encoder_config.encoder_specific_settings =
520 new rtc::RefCountedObject<
521 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
522 }
Niels Möllerf1338562018-04-26 09:51:47 +0200523 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700524 }
525
sprang57c2fff2017-01-16 06:24:02 -0800526 VideoFrame CreateFrame(int64_t ntp_time_ms,
527 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100528 VideoFrame frame =
529 VideoFrame::Builder()
530 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
531 destruction_event, codec_width_, codec_height_))
532 .set_timestamp_rtp(99)
533 .set_timestamp_ms(99)
534 .set_rotation(kVideoRotation_0)
535 .build();
sprang57c2fff2017-01-16 06:24:02 -0800536 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700537 return frame;
538 }
539
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100540 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
541 rtc::Event* destruction_event,
542 int offset_x) const {
543 VideoFrame frame =
544 VideoFrame::Builder()
545 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
546 destruction_event, codec_width_, codec_height_))
547 .set_timestamp_rtp(99)
548 .set_timestamp_ms(99)
549 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100550 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100551 .build();
552 frame.set_ntp_time_ms(ntp_time_ms);
553 return frame;
554 }
555
sprang57c2fff2017-01-16 06:24:02 -0800556 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100557 VideoFrame frame =
558 VideoFrame::Builder()
559 .set_video_frame_buffer(
560 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
561 .set_timestamp_rtp(99)
562 .set_timestamp_ms(99)
563 .set_rotation(kVideoRotation_0)
564 .build();
sprang57c2fff2017-01-16 06:24:02 -0800565 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700566 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700567 return frame;
568 }
569
Noah Richards51db4212019-06-12 06:59:12 -0700570 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
571 rtc::Event* destruction_event,
572 int width,
573 int height) const {
574 VideoFrame frame =
575 VideoFrame::Builder()
576 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
577 destruction_event, width, height))
578 .set_timestamp_rtp(99)
579 .set_timestamp_ms(99)
580 .set_rotation(kVideoRotation_0)
581 .build();
582 frame.set_ntp_time_ms(ntp_time_ms);
583 return frame;
584 }
585
586 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
587 rtc::Event* destruction_event) const {
588 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
589 codec_height_);
590 }
591
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100592 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
593 MockBitrateObserver bitrate_observer;
594 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
595
596 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
597 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200598 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100599 DataRate::BitsPerSec(kTargetBitrateBps),
600 DataRate::BitsPerSec(kTargetBitrateBps),
601 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100602
603 video_source_.IncomingCapturedFrame(
604 CreateFrame(1, codec_width_, codec_height_));
605 WaitForEncodedFrame(1);
606 }
607
asapersson02465b82017-04-10 01:12:52 -0700608 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700609 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700610 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
611 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700612 }
613
asapersson09f05612017-05-15 23:40:18 -0700614 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
615 const rtc::VideoSinkWants& wants2) {
616 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
617 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
618 }
619
Åsa Persson8c1bf952018-09-13 10:42:19 +0200620 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
621 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
622 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
623 EXPECT_FALSE(wants.target_pixel_count);
624 }
625
asapersson09f05612017-05-15 23:40:18 -0700626 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
627 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200628 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700629 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
630 EXPECT_GT(wants1.max_pixel_count, 0);
631 }
632
633 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
634 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200635 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700636 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
637 }
638
asaperssonf7e294d2017-06-13 23:25:22 -0700639 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
640 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200641 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700642 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
643 }
644
645 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
646 const rtc::VideoSinkWants& wants2) {
647 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
648 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
649 }
650
651 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
652 const rtc::VideoSinkWants& wants2) {
653 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
654 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
655 }
656
657 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
658 const rtc::VideoSinkWants& wants2) {
659 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
660 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
661 EXPECT_GT(wants1.max_pixel_count, 0);
662 }
663
664 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
665 const rtc::VideoSinkWants& wants2) {
666 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
667 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
668 }
669
asapersson09f05612017-05-15 23:40:18 -0700670 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
671 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200672 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700673 EXPECT_LT(wants.max_pixel_count, pixel_count);
674 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700675 }
676
677 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
678 EXPECT_LT(wants.max_framerate_fps, fps);
679 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
680 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700681 }
682
asaperssonf7e294d2017-06-13 23:25:22 -0700683 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
684 int expected_fps) {
685 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
686 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
687 EXPECT_FALSE(wants.target_pixel_count);
688 }
689
Jonathan Yubc771b72017-12-08 17:04:29 -0800690 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
691 int last_frame_pixels) {
692 // Balanced mode should always scale FPS to the desired range before
693 // attempting to scale resolution.
694 int fps_limit = wants.max_framerate_fps;
695 if (last_frame_pixels <= 320 * 240) {
696 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
697 } else if (last_frame_pixels <= 480 * 270) {
698 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
699 } else if (last_frame_pixels <= 640 * 480) {
700 EXPECT_LE(15, fps_limit);
701 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200702 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800703 }
704 }
705
sprang4847ae62017-06-27 07:06:52 -0700706 void WaitForEncodedFrame(int64_t expected_ntp_time) {
707 sink_.WaitForEncodedFrame(expected_ntp_time);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100708 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700709 }
710
711 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
712 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100713 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700714 return ok;
715 }
716
717 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
718 sink_.WaitForEncodedFrame(expected_width, expected_height);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100719 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700720 }
721
722 void ExpectDroppedFrame() {
723 sink_.ExpectDroppedFrame();
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100724 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700725 }
726
727 bool WaitForFrame(int64_t timeout_ms) {
728 bool ok = sink_.WaitForFrame(timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100729 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700730 return ok;
731 }
732
perkj26091b12016-09-01 01:17:40 -0700733 class TestEncoder : public test::FakeEncoder {
734 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100735 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700736
asaperssonfab67072017-04-04 05:51:49 -0700737 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800738 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700739 return config_;
740 }
741
742 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800743 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700744 block_next_encode_ = true;
745 }
746
Erik Språngaed30702018-11-05 12:57:17 +0100747 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800748 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100749 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100750 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100751 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100752 info.scaling_settings = VideoEncoder::ScalingSettings(
753 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100754 }
755 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100756 for (int i = 0; i < kMaxSpatialLayers; ++i) {
757 if (temporal_layers_supported_[i]) {
758 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
759 info.fps_allocation[i].resize(num_layers);
760 }
761 }
Erik Språngaed30702018-11-05 12:57:17 +0100762 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200763
764 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100765 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100766 return info;
kthelgason876222f2016-11-29 01:44:11 -0800767 }
768
Erik Språngb7cb7b52019-02-26 15:52:33 +0100769 int32_t RegisterEncodeCompleteCallback(
770 EncodedImageCallback* callback) override {
771 rtc::CritScope lock(&local_crit_sect_);
772 encoded_image_callback_ = callback;
773 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
774 }
775
perkjfa10b552016-10-02 23:45:26 -0700776 void ContinueEncode() { continue_encode_event_.Set(); }
777
778 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
779 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800780 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700781 EXPECT_EQ(timestamp_, timestamp);
782 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
783 }
784
kthelgason2fc52542017-03-03 00:24:41 -0800785 void SetQualityScaling(bool b) {
786 rtc::CritScope lock(&local_crit_sect_);
787 quality_scaling_ = b;
788 }
kthelgasonad9010c2017-02-14 00:46:51 -0800789
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100790 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
791 rtc::CritScope lock(&local_crit_sect_);
792 requested_resolution_alignment_ = requested_resolution_alignment;
793 }
794
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100795 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
796 rtc::CritScope lock(&local_crit_sect_);
797 is_hardware_accelerated_ = is_hardware_accelerated;
798 }
799
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100800 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
801 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
802 rtc::CritScope lock(&local_crit_sect_);
803 temporal_layers_supported_[spatial_idx] = supported;
804 }
805
Sergey Silkin6456e352019-07-08 17:56:40 +0200806 void SetResolutionBitrateLimits(
807 std::vector<ResolutionBitrateLimits> thresholds) {
808 rtc::CritScope cs(&local_crit_sect_);
809 resolution_bitrate_limits_ = thresholds;
810 }
811
sprangfe627f32017-03-29 08:24:59 -0700812 void ForceInitEncodeFailure(bool force_failure) {
813 rtc::CritScope lock(&local_crit_sect_);
814 force_init_encode_failed_ = force_failure;
815 }
816
Niels Möller6bb5ab92019-01-11 11:11:10 +0100817 void SimulateOvershoot(double rate_factor) {
818 rtc::CritScope lock(&local_crit_sect_);
819 rate_factor_ = rate_factor;
820 }
821
Erik Språngd7329ca2019-02-21 21:19:53 +0100822 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100823 rtc::CritScope lock(&local_crit_sect_);
824 return last_framerate_;
825 }
826
Erik Språngd7329ca2019-02-21 21:19:53 +0100827 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100828 rtc::CritScope lock(&local_crit_sect_);
829 return last_update_rect_;
830 }
831
Niels Möller87e2d782019-03-07 10:18:23 +0100832 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100833 rtc::CritScope lock(&local_crit_sect_);
834 return last_frame_types_;
835 }
836
837 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100838 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100839 keyframe ? VideoFrameType::kVideoFrameKey
840 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100841 {
842 rtc::CritScope lock(&local_crit_sect_);
843 last_frame_types_ = frame_type;
844 }
Niels Möllerb859b322019-03-07 12:40:01 +0100845 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100846 }
847
Erik Språngb7cb7b52019-02-26 15:52:33 +0100848 void InjectEncodedImage(const EncodedImage& image) {
849 rtc::CritScope lock(&local_crit_sect_);
850 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
851 }
852
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200853 void InjectEncodedImage(const EncodedImage& image,
854 const CodecSpecificInfo* codec_specific_info,
855 const RTPFragmentationHeader* fragmentation) {
856 rtc::CritScope lock(&local_crit_sect_);
857 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
858 fragmentation);
859 }
860
Erik Språngd7329ca2019-02-21 21:19:53 +0100861 void ExpectNullFrame() {
862 rtc::CritScope lock(&local_crit_sect_);
863 expect_null_frame_ = true;
864 }
865
Erik Språng5056af02019-09-02 15:53:11 +0200866 absl::optional<VideoEncoder::RateControlParameters>
867 GetAndResetLastRateControlSettings() {
868 auto settings = last_rate_control_settings_;
869 last_rate_control_settings_.reset();
870 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100871 }
872
Sergey Silkin5ee69672019-07-02 14:18:34 +0200873 int GetNumEncoderInitializations() const {
874 rtc::CritScope lock(&local_crit_sect_);
875 return num_encoder_initializations_;
876 }
877
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200878 int GetNumSetRates() const {
879 rtc::CritScope lock(&local_crit_sect_);
880 return num_set_rates_;
881 }
882
perkjfa10b552016-10-02 23:45:26 -0700883 private:
perkj26091b12016-09-01 01:17:40 -0700884 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100885 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700886 bool block_encode;
887 {
brandtre78d2662017-01-16 05:57:16 -0800888 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100889 if (expect_null_frame_) {
890 EXPECT_EQ(input_image.timestamp(), 0u);
891 EXPECT_EQ(input_image.width(), 1);
892 last_frame_types_ = *frame_types;
893 expect_null_frame_ = false;
894 } else {
895 EXPECT_GT(input_image.timestamp(), timestamp_);
896 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
897 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
898 }
perkj26091b12016-09-01 01:17:40 -0700899
900 timestamp_ = input_image.timestamp();
901 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700902 last_input_width_ = input_image.width();
903 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700904 block_encode = block_next_encode_;
905 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100906 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100907 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700908 }
Niels Möllerb859b322019-03-07 12:40:01 +0100909 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700910 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700911 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700912 return result;
913 }
914
sprangfe627f32017-03-29 08:24:59 -0700915 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200916 const Settings& settings) override {
917 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200918
sprangfe627f32017-03-29 08:24:59 -0700919 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100920 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200921
922 ++num_encoder_initializations_;
923
Erik Språng82fad3d2018-03-21 09:57:23 +0100924 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700925 // Simulate setting up temporal layers, in order to validate the life
926 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100927 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200928 frame_buffer_controller_ =
929 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700930 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100931 if (force_init_encode_failed_) {
932 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700933 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100934 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100935
Erik Språngb7cb7b52019-02-26 15:52:33 +0100936 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700937 return res;
938 }
939
Erik Språngb7cb7b52019-02-26 15:52:33 +0100940 int32_t Release() override {
941 rtc::CritScope lock(&local_crit_sect_);
942 EXPECT_NE(initialized_, EncoderState::kUninitialized);
943 initialized_ = EncoderState::kUninitialized;
944 return FakeEncoder::Release();
945 }
946
Erik Språng16cb8f52019-04-12 13:59:09 +0200947 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100948 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200949 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100950 VideoBitrateAllocation adjusted_rate_allocation;
951 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
952 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200953 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100954 adjusted_rate_allocation.SetBitrate(
955 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200956 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100957 rate_factor_));
958 }
959 }
960 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200961 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200962 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200963 RateControlParameters adjusted_paramters = parameters;
964 adjusted_paramters.bitrate = adjusted_rate_allocation;
965 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100966 }
967
brandtre78d2662017-01-16 05:57:16 -0800968 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100969 enum class EncoderState {
970 kUninitialized,
971 kInitializationFailed,
972 kInitialized
973 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
974 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700975 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700976 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700977 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
978 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
979 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
980 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
981 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100982 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100983 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100984 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700985 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100986 absl::optional<bool>
987 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
988 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700989 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100990 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
991 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200992 absl::optional<VideoEncoder::RateControlParameters>
993 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100994 VideoFrame::UpdateRect last_update_rect_
995 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100996 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100997 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100998 EncodedImageCallback* encoded_image_callback_
999 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +02001000 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001001 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001002 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
1003 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02001004 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -07001005 };
1006
mflodmancc3d4422017-08-03 08:27:51 -07001007 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -07001008 public:
1009 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +01001010 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -07001011
perkj26091b12016-09-01 01:17:40 -07001012 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -07001013 EXPECT_TRUE(
1014 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
1015 }
1016
1017 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
1018 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -07001019 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -07001020 if (!encoded_frame_event_.Wait(timeout_ms))
1021 return false;
perkj26091b12016-09-01 01:17:40 -07001022 {
1023 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -08001024 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -07001025 }
1026 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -07001027 return true;
perkj26091b12016-09-01 01:17:40 -07001028 }
1029
sprangb1ca0732017-02-01 08:38:12 -08001030 void WaitForEncodedFrame(uint32_t expected_width,
1031 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -07001032 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001033 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -07001034 }
1035
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001036 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -07001037 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -08001038 uint32_t width = 0;
1039 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -08001040 {
1041 rtc::CritScope lock(&crit_);
1042 width = last_width_;
1043 height = last_height_;
1044 }
1045 EXPECT_EQ(expected_height, height);
1046 EXPECT_EQ(expected_width, width);
1047 }
1048
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001049 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
1050 int width = 0;
1051 int height = 0;
1052 {
1053 rtc::CritScope lock(&crit_);
1054 width = last_width_;
1055 height = last_height_;
1056 }
1057 EXPECT_EQ(width % resolution_alignment, 0);
1058 EXPECT_EQ(height % resolution_alignment, 0);
1059 }
1060
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001061 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1062 VideoRotation rotation;
1063 {
1064 rtc::CritScope lock(&crit_);
1065 rotation = last_rotation_;
1066 }
1067 EXPECT_EQ(expected_rotation, rotation);
1068 }
1069
kthelgason2fc52542017-03-03 00:24:41 -08001070 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001071
sprangc5d62e22017-04-02 23:53:04 -07001072 bool WaitForFrame(int64_t timeout_ms) {
1073 return encoded_frame_event_.Wait(timeout_ms);
1074 }
1075
perkj26091b12016-09-01 01:17:40 -07001076 void SetExpectNoFrames() {
1077 rtc::CritScope lock(&crit_);
1078 expect_frames_ = false;
1079 }
1080
asaperssonfab67072017-04-04 05:51:49 -07001081 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001082 rtc::CritScope lock(&crit_);
1083 return number_of_reconfigurations_;
1084 }
1085
asaperssonfab67072017-04-04 05:51:49 -07001086 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001087 rtc::CritScope lock(&crit_);
1088 return min_transmit_bitrate_bps_;
1089 }
1090
Erik Språngd7329ca2019-02-21 21:19:53 +01001091 void SetNumExpectedLayers(size_t num_layers) {
1092 rtc::CritScope lock(&crit_);
1093 num_expected_layers_ = num_layers;
1094 }
1095
Erik Språngb7cb7b52019-02-26 15:52:33 +01001096 int64_t GetLastCaptureTimeMs() const {
1097 rtc::CritScope lock(&crit_);
1098 return last_capture_time_ms_;
1099 }
1100
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001101 std::vector<uint8_t> GetLastEncodedImageData() {
1102 rtc::CritScope lock(&crit_);
1103 return std::move(last_encoded_image_data_);
1104 }
1105
1106 RTPFragmentationHeader GetLastFragmentation() {
1107 rtc::CritScope lock(&crit_);
1108 return std::move(last_fragmentation_);
1109 }
1110
perkj26091b12016-09-01 01:17:40 -07001111 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001112 Result OnEncodedImage(
1113 const EncodedImage& encoded_image,
1114 const CodecSpecificInfo* codec_specific_info,
1115 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001116 rtc::CritScope lock(&crit_);
1117 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001118 last_encoded_image_data_ = std::vector<uint8_t>(
1119 encoded_image.data(), encoded_image.data() + encoded_image.size());
1120 if (fragmentation) {
1121 last_fragmentation_.CopyFrom(*fragmentation);
1122 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001123 uint32_t timestamp = encoded_image.Timestamp();
1124 if (last_timestamp_ != timestamp) {
1125 num_received_layers_ = 1;
1126 } else {
1127 ++num_received_layers_;
1128 }
1129 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001130 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001131 last_width_ = encoded_image._encodedWidth;
1132 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001133 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001134 if (num_received_layers_ == num_expected_layers_) {
1135 encoded_frame_event_.Set();
1136 }
sprangb1ca0732017-02-01 08:38:12 -08001137 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001138 }
1139
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001140 void OnEncoderConfigurationChanged(
1141 std::vector<VideoStream> streams,
1142 VideoEncoderConfig::ContentType content_type,
1143 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001144 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001145 ++number_of_reconfigurations_;
1146 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1147 }
1148
perkj26091b12016-09-01 01:17:40 -07001149 rtc::CriticalSection crit_;
1150 TestEncoder* test_encoder_;
1151 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001152 std::vector<uint8_t> last_encoded_image_data_;
1153 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001154 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001155 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001156 uint32_t last_height_ = 0;
1157 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001158 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001159 size_t num_expected_layers_ = 1;
1160 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001161 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001162 int number_of_reconfigurations_ = 0;
1163 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001164 };
1165
Sergey Silkin5ee69672019-07-02 14:18:34 +02001166 class VideoBitrateAllocatorProxyFactory
1167 : public VideoBitrateAllocatorFactory {
1168 public:
1169 VideoBitrateAllocatorProxyFactory()
1170 : bitrate_allocator_factory_(
1171 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1172
1173 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1174 const VideoCodec& codec) override {
1175 rtc::CritScope lock(&crit_);
1176 codec_config_ = codec;
1177 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1178 }
1179
1180 VideoCodec codec_config() const {
1181 rtc::CritScope lock(&crit_);
1182 return codec_config_;
1183 }
1184
1185 private:
1186 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1187
1188 rtc::CriticalSection crit_;
1189 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1190 };
1191
perkj26091b12016-09-01 01:17:40 -07001192 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001193 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001194 int codec_width_;
1195 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001196 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001197 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001198 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001199 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001200 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001201 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001202 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001203 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001204 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001205 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001206};
1207
mflodmancc3d4422017-08-03 08:27:51 -07001208TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001209 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001210 DataRate::BitsPerSec(kTargetBitrateBps),
1211 DataRate::BitsPerSec(kTargetBitrateBps),
1212 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001213 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001214 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001215 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001216 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001217 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001218}
1219
mflodmancc3d4422017-08-03 08:27:51 -07001220TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001221 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001222 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001223 // The encoder will cache up to one frame for a short duration. Adding two
1224 // frames means that the first frame will be dropped and the second frame will
1225 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001226 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001227 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001228 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001229
Erik Språng4c6ca302019-04-08 15:14:01 +02001230 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001231 DataRate::BitsPerSec(kTargetBitrateBps),
1232 DataRate::BitsPerSec(kTargetBitrateBps),
1233 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001234
Sebastian Janssona3177052018-04-10 13:05:49 +02001235 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001236 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001237 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1238
1239 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001240 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001241}
1242
mflodmancc3d4422017-08-03 08:27:51 -07001243TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001244 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001245 DataRate::BitsPerSec(kTargetBitrateBps),
1246 DataRate::BitsPerSec(kTargetBitrateBps),
1247 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001248 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001249 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001250
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001251 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
1252 DataRate::BitsPerSec(0),
1253 DataRate::BitsPerSec(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001254 // The encoder will cache up to one frame for a short duration. Adding two
1255 // frames means that the first frame will be dropped and the second frame will
1256 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001257 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001258 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001259
Erik Språng4c6ca302019-04-08 15:14:01 +02001260 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001261 DataRate::BitsPerSec(kTargetBitrateBps),
1262 DataRate::BitsPerSec(kTargetBitrateBps),
1263 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001264 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001265 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1266 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001267 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001268}
1269
mflodmancc3d4422017-08-03 08:27:51 -07001270TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001271 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001272 DataRate::BitsPerSec(kTargetBitrateBps),
1273 DataRate::BitsPerSec(kTargetBitrateBps),
1274 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001275 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001276 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001277
1278 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001279 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001280
perkja49cbd32016-09-16 07:53:41 -07001281 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001282 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001283 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001284}
1285
mflodmancc3d4422017-08-03 08:27:51 -07001286TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001287 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001288 DataRate::BitsPerSec(kTargetBitrateBps),
1289 DataRate::BitsPerSec(kTargetBitrateBps),
1290 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001291
perkja49cbd32016-09-16 07:53:41 -07001292 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001293 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001294
mflodmancc3d4422017-08-03 08:27:51 -07001295 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001296 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001297 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001298 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1299 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001300}
1301
mflodmancc3d4422017-08-03 08:27:51 -07001302TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001303 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001304 DataRate::BitsPerSec(kTargetBitrateBps),
1305 DataRate::BitsPerSec(kTargetBitrateBps),
1306 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001307
1308 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001309 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001310 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001311 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1312 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001313 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1314 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001315 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001316 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001317
mflodmancc3d4422017-08-03 08:27:51 -07001318 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001319}
1320
Noah Richards51db4212019-06-12 06:59:12 -07001321TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1322 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001323 DataRate::BitsPerSec(kTargetBitrateBps),
1324 DataRate::BitsPerSec(kTargetBitrateBps),
1325 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001326
1327 rtc::Event frame_destroyed_event;
1328 video_source_.IncomingCapturedFrame(
1329 CreateFakeNativeFrame(1, &frame_destroyed_event));
1330 ExpectDroppedFrame();
1331 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1332 video_stream_encoder_->Stop();
1333}
1334
1335TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1336 // Use the cropping factory.
1337 video_encoder_config_.video_stream_factory =
1338 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1339 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1340 kMaxPayloadLength);
1341 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1342
1343 // Capture a frame at codec_width_/codec_height_.
1344 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001345 DataRate::BitsPerSec(kTargetBitrateBps),
1346 DataRate::BitsPerSec(kTargetBitrateBps),
1347 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001348 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1349 WaitForEncodedFrame(1);
1350 // The encoder will have been configured once.
1351 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1352 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1353 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1354
1355 // Now send in a fake frame that needs to be cropped as the width/height
1356 // aren't divisible by 4 (see CreateEncoderStreams above).
1357 rtc::Event frame_destroyed_event;
1358 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1359 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1360 ExpectDroppedFrame();
1361 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1362 video_stream_encoder_->Stop();
1363}
1364
Ying Wang9b881ab2020-02-07 14:29:32 +01001365TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1366 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001367 DataRate::BitsPerSec(kTargetBitrateBps),
1368 DataRate::BitsPerSec(kTargetBitrateBps),
1369 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ying Wang9b881ab2020-02-07 14:29:32 +01001370 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1371 WaitForEncodedFrame(1);
1372
1373 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001374 DataRate::BitsPerSec(kTargetBitrateBps),
1375 DataRate::BitsPerSec(kTargetBitrateBps),
1376 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0.5);
Ying Wang9b881ab2020-02-07 14:29:32 +01001377 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1378 // frames. Adding two frames means that the first frame will be dropped and
1379 // the second frame will be sent to the encoder.
1380 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1381 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1382 WaitForEncodedFrame(3);
1383 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1384 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1385 WaitForEncodedFrame(5);
1386 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1387 video_stream_encoder_->Stop();
1388}
1389
mflodmancc3d4422017-08-03 08:27:51 -07001390TEST_F(VideoStreamEncoderTest,
1391 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001392 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001393 DataRate::BitsPerSec(kTargetBitrateBps),
1394 DataRate::BitsPerSec(kTargetBitrateBps),
1395 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001396 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001397
1398 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001399 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001400 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001401 // The encoder will have been configured once when the first frame is
1402 // received.
1403 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001404
1405 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001406 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001407 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001408 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001409 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001410
1411 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001412 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001413 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001414 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001415 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001416
mflodmancc3d4422017-08-03 08:27:51 -07001417 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001418}
1419
mflodmancc3d4422017-08-03 08:27:51 -07001420TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001421 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001422 DataRate::BitsPerSec(kTargetBitrateBps),
1423 DataRate::BitsPerSec(kTargetBitrateBps),
1424 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001425
1426 // Capture a frame and wait for it to synchronize with the encoder thread.
1427 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001428 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001429 // The encoder will have been configured once.
1430 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001431 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1432 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1433
1434 codec_width_ *= 2;
1435 codec_height_ *= 2;
1436 // Capture a frame with a higher resolution and wait for it to synchronize
1437 // with the encoder thread.
1438 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001439 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001440 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1441 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001442 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001443
mflodmancc3d4422017-08-03 08:27:51 -07001444 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001445}
1446
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001447TEST_F(VideoStreamEncoderTest,
1448 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1449 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001450 DataRate::BitsPerSec(kTargetBitrateBps),
1451 DataRate::BitsPerSec(kTargetBitrateBps),
1452 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001453
1454 // Capture a frame and wait for it to synchronize with the encoder thread.
1455 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1456 WaitForEncodedFrame(1);
1457
1458 VideoEncoderConfig video_encoder_config;
1459 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1460 // Changing the max payload data length recreates encoder.
1461 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1462 kMaxPayloadLength / 2);
1463
1464 // Capture a frame and wait for it to synchronize with the encoder thread.
1465 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1466 WaitForEncodedFrame(2);
1467 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1468
1469 video_stream_encoder_->Stop();
1470}
1471
Sergey Silkin5ee69672019-07-02 14:18:34 +02001472TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1473 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001474 DataRate::BitsPerSec(kTargetBitrateBps),
1475 DataRate::BitsPerSec(kTargetBitrateBps),
1476 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001477
1478 VideoEncoderConfig video_encoder_config;
1479 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1480 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1481 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1482 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1483 kMaxPayloadLength);
1484
1485 // Capture a frame and wait for it to synchronize with the encoder thread.
1486 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1487 WaitForEncodedFrame(1);
1488 // The encoder will have been configured once when the first frame is
1489 // received.
1490 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1491 EXPECT_EQ(kTargetBitrateBps,
1492 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1493 EXPECT_EQ(kStartBitrateBps,
1494 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1495
Sergey Silkin6456e352019-07-08 17:56:40 +02001496 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1497 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001498 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1499 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1500 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1501 kMaxPayloadLength);
1502
1503 // Capture a frame and wait for it to synchronize with the encoder thread.
1504 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1505 WaitForEncodedFrame(2);
1506 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1507 // Bitrate limits have changed - rate allocator should be reconfigured,
1508 // encoder should not be reconfigured.
1509 EXPECT_EQ(kTargetBitrateBps * 2,
1510 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1511 EXPECT_EQ(kStartBitrateBps * 2,
1512 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1513 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1514
1515 video_stream_encoder_->Stop();
1516}
1517
Sergey Silkin6456e352019-07-08 17:56:40 +02001518TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001519 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001520 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001521 DataRate::BitsPerSec(kTargetBitrateBps),
1522 DataRate::BitsPerSec(kTargetBitrateBps),
1523 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001524
Sergey Silkincd02eba2020-01-20 14:48:40 +01001525 const uint32_t kMinEncBitrateKbps = 100;
1526 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001527 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001528 /*frame_size_pixels=*/codec_width_ * codec_height_,
1529 /*min_start_bitrate_bps=*/0,
1530 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1531 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001532 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1533
Sergey Silkincd02eba2020-01-20 14:48:40 +01001534 VideoEncoderConfig video_encoder_config;
1535 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1536 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1537 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1538 (kMinEncBitrateKbps + 1) * 1000;
1539 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1540 kMaxPayloadLength);
1541
1542 // When both encoder and app provide bitrate limits, the intersection of
1543 // provided sets should be used.
1544 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1545 WaitForEncodedFrame(1);
1546 EXPECT_EQ(kMaxEncBitrateKbps,
1547 bitrate_allocator_factory_.codec_config().maxBitrate);
1548 EXPECT_EQ(kMinEncBitrateKbps + 1,
1549 bitrate_allocator_factory_.codec_config().minBitrate);
1550
1551 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1552 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1553 (kMinEncBitrateKbps - 1) * 1000;
1554 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1555 kMaxPayloadLength);
1556 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001557 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001558 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001559 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001560 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001561 bitrate_allocator_factory_.codec_config().minBitrate);
1562
Sergey Silkincd02eba2020-01-20 14:48:40 +01001563 video_stream_encoder_->Stop();
1564}
1565
1566TEST_F(VideoStreamEncoderTest,
1567 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1568 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001569 DataRate::BitsPerSec(kTargetBitrateBps),
1570 DataRate::BitsPerSec(kTargetBitrateBps),
1571 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001572
1573 const uint32_t kMinAppBitrateKbps = 100;
1574 const uint32_t kMaxAppBitrateKbps = 200;
1575 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1576 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1577 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1578 /*frame_size_pixels=*/codec_width_ * codec_height_,
1579 /*min_start_bitrate_bps=*/0,
1580 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1581 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1582 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1583
1584 VideoEncoderConfig video_encoder_config;
1585 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1586 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1587 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1588 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001589 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1590 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001591
Sergey Silkincd02eba2020-01-20 14:48:40 +01001592 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1593 WaitForEncodedFrame(1);
1594 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001595 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001596 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001597 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001598
1599 video_stream_encoder_->Stop();
1600}
1601
1602TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001603 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001604 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001605 DataRate::BitsPerSec(kTargetBitrateBps),
1606 DataRate::BitsPerSec(kTargetBitrateBps),
1607 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001608
1609 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001610 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001611 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001612 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001613 fake_encoder_.SetResolutionBitrateLimits(
1614 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1615
1616 VideoEncoderConfig video_encoder_config;
1617 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1618 video_encoder_config.max_bitrate_bps = 0;
1619 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1620 kMaxPayloadLength);
1621
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001622 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001623 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1624 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001625 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1626 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001627 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1628 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1629
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001630 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001631 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1632 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001633 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1634 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001635 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1636 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1637
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001638 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001639 // encoder for 360p should be used.
1640 video_source_.IncomingCapturedFrame(
1641 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1642 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001643 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1644 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001645 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1646 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1647
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001648 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001649 // ignored.
1650 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1651 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001652 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1653 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001654 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1655 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001656 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1657 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001658 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1659 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1660
1661 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1662 // for 270p should be used.
1663 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1664 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001665 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1666 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001667 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1668 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1669
1670 video_stream_encoder_->Stop();
1671}
1672
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001673TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1674 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001675 DataRate::BitsPerSec(kTargetBitrateBps),
1676 DataRate::BitsPerSec(kTargetBitrateBps),
1677 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001678
1679 VideoEncoderConfig video_encoder_config;
1680 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1681 video_encoder_config.max_bitrate_bps = 0;
1682 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1683 kMaxPayloadLength);
1684
1685 // Encode 720p frame to get the default encoder target bitrate.
1686 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1687 WaitForEncodedFrame(1);
1688 const uint32_t kDefaultTargetBitrateFor720pKbps =
1689 bitrate_allocator_factory_.codec_config()
1690 .simulcastStream[0]
1691 .targetBitrate;
1692
1693 // Set the max recommended encoder bitrate to something lower than the default
1694 // target bitrate.
1695 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1696 1280 * 720, 10 * 1000, 10 * 1000,
1697 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1698 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1699
1700 // Change resolution to trigger encoder reinitialization.
1701 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1702 WaitForEncodedFrame(2);
1703 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1704 WaitForEncodedFrame(3);
1705
1706 // Ensure the target bitrate is capped by the max bitrate.
1707 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1708 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1709 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1710 .simulcastStream[0]
1711 .targetBitrate *
1712 1000,
1713 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1714
1715 video_stream_encoder_->Stop();
1716}
1717
mflodmancc3d4422017-08-03 08:27:51 -07001718TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001719 EXPECT_TRUE(video_source_.has_sinks());
1720 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001721 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001722 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001723 EXPECT_FALSE(video_source_.has_sinks());
1724 EXPECT_TRUE(new_video_source.has_sinks());
1725
mflodmancc3d4422017-08-03 08:27:51 -07001726 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001727}
1728
mflodmancc3d4422017-08-03 08:27:51 -07001729TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001730 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001731 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001732 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001733 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001734}
1735
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001736TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1737 constexpr int kRequestedResolutionAlignment = 7;
1738 video_source_.set_adaptation_enabled(true);
1739 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1740 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001741 DataRate::BitsPerSec(kTargetBitrateBps),
1742 DataRate::BitsPerSec(kTargetBitrateBps),
1743 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001744
1745 // On the 1st frame, we should have initialized the encoder and
1746 // asked for its resolution requirements.
1747 video_source_.IncomingCapturedFrame(
1748 CreateFrame(1, codec_width_, codec_height_));
1749 WaitForEncodedFrame(1);
1750 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1751 kRequestedResolutionAlignment);
1752
1753 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1754 // (It's up the to the encoder to potentially drop the previous frame,
1755 // to avoid coding back-to-back keyframes.)
1756 video_source_.IncomingCapturedFrame(
1757 CreateFrame(2, codec_width_, codec_height_));
1758 WaitForEncodedFrame(2);
1759 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1760
1761 video_stream_encoder_->Stop();
1762}
1763
Jonathan Yubc771b72017-12-08 17:04:29 -08001764TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1765 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001766 const int kWidth = 1280;
1767 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001768
1769 // We rely on the automatic resolution adaptation, but we handle framerate
1770 // adaptation manually by mocking the stats proxy.
1771 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001772
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001773 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001774 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001775 DataRate::BitsPerSec(kTargetBitrateBps),
1776 DataRate::BitsPerSec(kTargetBitrateBps),
1777 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001778 video_stream_encoder_->SetSource(&video_source_,
1779 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001780 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001781 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001782 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001783 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1784
Jonathan Yubc771b72017-12-08 17:04:29 -08001785 // Adapt down as far as possible.
1786 rtc::VideoSinkWants last_wants;
1787 int64_t t = 1;
1788 int loop_count = 0;
1789 do {
1790 ++loop_count;
1791 last_wants = video_source_.sink_wants();
1792
1793 // Simulate the framerate we've been asked to adapt to.
1794 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1795 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1796 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1797 mock_stats.input_frame_rate = fps;
1798 stats_proxy_->SetMockStats(mock_stats);
1799
1800 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1801 sink_.WaitForEncodedFrame(t);
1802 t += frame_interval_ms;
1803
mflodmancc3d4422017-08-03 08:27:51 -07001804 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001805 VerifyBalancedModeFpsRange(
1806 video_source_.sink_wants(),
1807 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1808 } while (video_source_.sink_wants().max_pixel_count <
1809 last_wants.max_pixel_count ||
1810 video_source_.sink_wants().max_framerate_fps <
1811 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001812
Jonathan Yubc771b72017-12-08 17:04:29 -08001813 // Verify that we've adapted all the way down.
1814 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001815 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001816 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1817 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001818 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001819 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1820 *video_source_.last_sent_height());
1821 EXPECT_EQ(kMinBalancedFramerateFps,
1822 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001823
Jonathan Yubc771b72017-12-08 17:04:29 -08001824 // Adapt back up the same number of times we adapted down.
1825 for (int i = 0; i < loop_count - 1; ++i) {
1826 last_wants = video_source_.sink_wants();
1827
1828 // Simulate the framerate we've been asked to adapt to.
1829 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1830 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1831 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1832 mock_stats.input_frame_rate = fps;
1833 stats_proxy_->SetMockStats(mock_stats);
1834
1835 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1836 sink_.WaitForEncodedFrame(t);
1837 t += frame_interval_ms;
1838
mflodmancc3d4422017-08-03 08:27:51 -07001839 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001840 VerifyBalancedModeFpsRange(
1841 video_source_.sink_wants(),
1842 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1843 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1844 last_wants.max_pixel_count ||
1845 video_source_.sink_wants().max_framerate_fps >
1846 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001847 }
1848
Åsa Persson8c1bf952018-09-13 10:42:19 +02001849 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001850 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001851 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001852 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1853 EXPECT_EQ((loop_count - 1) * 2,
1854 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001855
mflodmancc3d4422017-08-03 08:27:51 -07001856 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001857}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001858
mflodmancc3d4422017-08-03 08:27:51 -07001859TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001860 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001861 DataRate::BitsPerSec(kTargetBitrateBps),
1862 DataRate::BitsPerSec(kTargetBitrateBps),
1863 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001864 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001865
sprangc5d62e22017-04-02 23:53:04 -07001866 const int kFrameWidth = 1280;
1867 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001868
Åsa Persson8c1bf952018-09-13 10:42:19 +02001869 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001870
kthelgason5e13d412016-12-01 03:59:51 -08001871 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001872 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001873 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001874 frame_timestamp += kFrameIntervalMs;
1875
perkj803d97f2016-11-01 11:45:46 -07001876 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001877 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001878 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001879 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001880 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001881 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001882
asapersson0944a802017-04-07 00:57:58 -07001883 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001884 // wanted resolution.
1885 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1886 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1887 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001888 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001889
1890 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001891 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001892 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001893 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001894 // Give the encoder queue time to process the change in degradation preference
1895 // by waiting for an encoded frame.
1896 new_video_source.IncomingCapturedFrame(
1897 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1898 sink_.WaitForEncodedFrame(frame_timestamp);
1899 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001900 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001901 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001902
sprangc5d62e22017-04-02 23:53:04 -07001903 // Force an input frame rate to be available, or the adaptation call won't
1904 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001905 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001906 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001907 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001908 stats_proxy_->SetMockStats(stats);
1909
mflodmancc3d4422017-08-03 08:27:51 -07001910 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001911 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001912 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001913 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001914 frame_timestamp += kFrameIntervalMs;
1915
1916 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001917 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001918 EXPECT_EQ(std::numeric_limits<int>::max(),
1919 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001920 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001921
asapersson02465b82017-04-10 01:12:52 -07001922 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001923 video_stream_encoder_->SetSource(&new_video_source,
1924 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001925 // Give the encoder queue time to process the change in degradation preference
1926 // by waiting for an encoded frame.
1927 new_video_source.IncomingCapturedFrame(
1928 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1929 sink_.WaitForEncodedFrame(frame_timestamp);
1930 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001931 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001932
mflodmancc3d4422017-08-03 08:27:51 -07001933 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001934 new_video_source.IncomingCapturedFrame(
1935 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001936 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001937 frame_timestamp += kFrameIntervalMs;
1938
1939 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001940 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001941
1942 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001943 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001944 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001945 // Give the encoder queue time to process the change in degradation preference
1946 // by waiting for an encoded frame.
1947 new_video_source.IncomingCapturedFrame(
1948 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1949 sink_.WaitForEncodedFrame(frame_timestamp);
1950 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001951 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1952 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001953 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001954 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001955
1956 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001957 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001958 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001959 // Give the encoder queue time to process the change in degradation preference
1960 // by waiting for an encoded frame.
1961 new_video_source.IncomingCapturedFrame(
1962 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1963 sink_.WaitForEncodedFrame(frame_timestamp);
1964 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001965 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1966 EXPECT_EQ(std::numeric_limits<int>::max(),
1967 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001968 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001969
mflodmancc3d4422017-08-03 08:27:51 -07001970 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001971}
1972
mflodmancc3d4422017-08-03 08:27:51 -07001973TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001974 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001975 DataRate::BitsPerSec(kTargetBitrateBps),
1976 DataRate::BitsPerSec(kTargetBitrateBps),
1977 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001978
asaperssonfab67072017-04-04 05:51:49 -07001979 const int kWidth = 1280;
1980 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001981 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001982 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001983 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1984 EXPECT_FALSE(stats.bw_limited_resolution);
1985 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1986
1987 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001988 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001989 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001990 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001991
1992 stats = stats_proxy_->GetStats();
1993 EXPECT_TRUE(stats.bw_limited_resolution);
1994 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1995
1996 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001997 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001998 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001999 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07002000
2001 stats = stats_proxy_->GetStats();
2002 EXPECT_FALSE(stats.bw_limited_resolution);
2003 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
2004 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2005
mflodmancc3d4422017-08-03 08:27:51 -07002006 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07002007}
2008
mflodmancc3d4422017-08-03 08:27:51 -07002009TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002010 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002011 DataRate::BitsPerSec(kTargetBitrateBps),
2012 DataRate::BitsPerSec(kTargetBitrateBps),
2013 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07002014
2015 const int kWidth = 1280;
2016 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07002017 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002018 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07002019 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2020 EXPECT_FALSE(stats.cpu_limited_resolution);
2021 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2022
2023 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002024 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002025 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002026 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07002027
2028 stats = stats_proxy_->GetStats();
2029 EXPECT_TRUE(stats.cpu_limited_resolution);
2030 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2031
2032 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002033 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002034 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002035 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07002036
2037 stats = stats_proxy_->GetStats();
2038 EXPECT_FALSE(stats.cpu_limited_resolution);
2039 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07002040 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002041
mflodmancc3d4422017-08-03 08:27:51 -07002042 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002043}
2044
mflodmancc3d4422017-08-03 08:27:51 -07002045TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002046 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002047 DataRate::BitsPerSec(kTargetBitrateBps),
2048 DataRate::BitsPerSec(kTargetBitrateBps),
2049 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002050
asaperssonfab67072017-04-04 05:51:49 -07002051 const int kWidth = 1280;
2052 const int kHeight = 720;
2053 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002054 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002055 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002056 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002057 EXPECT_FALSE(stats.cpu_limited_resolution);
2058 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2059
asaperssonfab67072017-04-04 05:51:49 -07002060 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002061 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002062 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002063 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002064 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002065 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002066 EXPECT_TRUE(stats.cpu_limited_resolution);
2067 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2068
2069 // Set new source with adaptation still enabled.
2070 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002071 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002072 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002073
asaperssonfab67072017-04-04 05:51:49 -07002074 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002075 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002076 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002077 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002078 EXPECT_TRUE(stats.cpu_limited_resolution);
2079 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2080
2081 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002082 video_stream_encoder_->SetSource(&new_video_source,
2083 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002084
asaperssonfab67072017-04-04 05:51:49 -07002085 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002086 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002087 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002088 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002089 EXPECT_FALSE(stats.cpu_limited_resolution);
2090 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2091
2092 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002093 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002094 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002095
asaperssonfab67072017-04-04 05:51:49 -07002096 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002097 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002098 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002099 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002100 EXPECT_TRUE(stats.cpu_limited_resolution);
2101 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2102
asaperssonfab67072017-04-04 05:51:49 -07002103 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002104 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002105 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002106 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002107 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002108 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002109 EXPECT_FALSE(stats.cpu_limited_resolution);
2110 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002111 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002112
mflodmancc3d4422017-08-03 08:27:51 -07002113 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002114}
2115
mflodmancc3d4422017-08-03 08:27:51 -07002116TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002117 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002118 DataRate::BitsPerSec(kTargetBitrateBps),
2119 DataRate::BitsPerSec(kTargetBitrateBps),
2120 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002121
asaperssonfab67072017-04-04 05:51:49 -07002122 const int kWidth = 1280;
2123 const int kHeight = 720;
2124 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002125 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002126 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002127 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002128 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002129 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002130
2131 // Set new source with adaptation still enabled.
2132 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002133 video_stream_encoder_->SetSource(&new_video_source,
2134 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002135
asaperssonfab67072017-04-04 05:51:49 -07002136 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002137 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002138 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002139 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002140 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002141 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002142
asaperssonfab67072017-04-04 05:51:49 -07002143 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002144 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002145 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002146 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002147 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002148 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002149 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002150 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002151
asaperssonfab67072017-04-04 05:51:49 -07002152 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002153 video_stream_encoder_->SetSource(&new_video_source,
2154 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002155
asaperssonfab67072017-04-04 05:51:49 -07002156 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002157 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002158 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002159 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002160 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002161 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002162
asapersson02465b82017-04-10 01:12:52 -07002163 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002164 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002165 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002166
asaperssonfab67072017-04-04 05:51:49 -07002167 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002168 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002169 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002170 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002171 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002172 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2173 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002174
mflodmancc3d4422017-08-03 08:27:51 -07002175 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002176}
2177
mflodmancc3d4422017-08-03 08:27:51 -07002178TEST_F(VideoStreamEncoderTest,
2179 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002180 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002181 DataRate::BitsPerSec(kTargetBitrateBps),
2182 DataRate::BitsPerSec(kTargetBitrateBps),
2183 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002184
2185 const int kWidth = 1280;
2186 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002187 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002188 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002189 video_source_.IncomingCapturedFrame(
2190 CreateFrame(timestamp_ms, kWidth, kHeight));
2191 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002192 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2193 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2194 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2195
2196 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002197 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002198 timestamp_ms += kFrameIntervalMs;
2199 video_source_.IncomingCapturedFrame(
2200 CreateFrame(timestamp_ms, kWidth, kHeight));
2201 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002202 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2203 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2204 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2205
2206 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002207 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002208 timestamp_ms += kFrameIntervalMs;
2209 video_source_.IncomingCapturedFrame(
2210 CreateFrame(timestamp_ms, kWidth, kHeight));
2211 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002212 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2213 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2214 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2215
Niels Möller4db138e2018-04-19 09:04:13 +02002216 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002217 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002218
2219 VideoEncoderConfig video_encoder_config;
2220 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2221 // Make format different, to force recreation of encoder.
2222 video_encoder_config.video_format.parameters["foo"] = "foo";
2223 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002224 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002225 timestamp_ms += kFrameIntervalMs;
2226 video_source_.IncomingCapturedFrame(
2227 CreateFrame(timestamp_ms, kWidth, kHeight));
2228 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002229 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2230 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2231 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2232
mflodmancc3d4422017-08-03 08:27:51 -07002233 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002234}
2235
mflodmancc3d4422017-08-03 08:27:51 -07002236TEST_F(VideoStreamEncoderTest,
2237 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002238 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002239 DataRate::BitsPerSec(kTargetBitrateBps),
2240 DataRate::BitsPerSec(kTargetBitrateBps),
2241 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002242
asapersson0944a802017-04-07 00:57:58 -07002243 const int kWidth = 1280;
2244 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002245 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002246
asaperssonfab67072017-04-04 05:51:49 -07002247 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002248 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002249 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002250 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002251 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002252 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2253
asapersson02465b82017-04-10 01:12:52 -07002254 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002255 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002256 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002257 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002258 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002259 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002260 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002261 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2262
2263 // Set new source with adaptation still enabled.
2264 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002265 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002266 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002267
2268 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002269 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002270 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002271 stats = stats_proxy_->GetStats();
2272 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002273 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002274 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2275
sprangc5d62e22017-04-02 23:53:04 -07002276 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002277 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002278 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002279 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002280 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002281 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002282 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002283 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002284 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002285 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002286 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2287
sprangc5d62e22017-04-02 23:53:04 -07002288 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002289 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002290 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2291 mock_stats.input_frame_rate = 30;
2292 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002293 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002294 stats_proxy_->ResetMockStats();
2295
2296 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002297 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002298 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002299
2300 // Framerate now adapted.
2301 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002302 EXPECT_FALSE(stats.cpu_limited_resolution);
2303 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002304 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2305
2306 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002307 video_stream_encoder_->SetSource(&new_video_source,
2308 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002309 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002310 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002311 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002312
2313 stats = stats_proxy_->GetStats();
2314 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002315 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002316 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2317
2318 // Try to trigger overuse. Should not succeed.
2319 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002320 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002321 stats_proxy_->ResetMockStats();
2322
2323 stats = stats_proxy_->GetStats();
2324 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002325 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002326 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2327
2328 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002329 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002330 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002331 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002332 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002333 stats = stats_proxy_->GetStats();
2334 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002335 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002336 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002337
2338 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002339 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002340 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002341 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002342 stats = stats_proxy_->GetStats();
2343 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002344 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002345 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2346
2347 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002348 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002349 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002350 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002351 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002352 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002353 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002354 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002355 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002356 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002357 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2358
2359 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002360 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002361 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002362 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002363 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002364 stats = stats_proxy_->GetStats();
2365 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002366 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002367 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002368 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002369
mflodmancc3d4422017-08-03 08:27:51 -07002370 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002371}
2372
mflodmancc3d4422017-08-03 08:27:51 -07002373TEST_F(VideoStreamEncoderTest,
2374 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002375 const int kWidth = 1280;
2376 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002377 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002378 DataRate::BitsPerSec(kTargetBitrateBps),
2379 DataRate::BitsPerSec(kTargetBitrateBps),
2380 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002381
asaperssonfab67072017-04-04 05:51:49 -07002382 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002383 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002384
asaperssonfab67072017-04-04 05:51:49 -07002385 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002386 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002387
asaperssonfab67072017-04-04 05:51:49 -07002388 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002389 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002390
asaperssonfab67072017-04-04 05:51:49 -07002391 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002392 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002393
kthelgason876222f2016-11-29 01:44:11 -08002394 // Expect a scale down.
2395 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002396 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002397
asapersson02465b82017-04-10 01:12:52 -07002398 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002399 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002400 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002401 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002402
asaperssonfab67072017-04-04 05:51:49 -07002403 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002404 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002405 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002406 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002407
asaperssonfab67072017-04-04 05:51:49 -07002408 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002409 EXPECT_EQ(std::numeric_limits<int>::max(),
2410 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002411
asaperssonfab67072017-04-04 05:51:49 -07002412 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002413 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002414 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002415 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002416
asapersson02465b82017-04-10 01:12:52 -07002417 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002418 EXPECT_EQ(std::numeric_limits<int>::max(),
2419 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002420
mflodmancc3d4422017-08-03 08:27:51 -07002421 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002422}
2423
mflodmancc3d4422017-08-03 08:27:51 -07002424TEST_F(VideoStreamEncoderTest,
2425 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002426 const int kWidth = 1280;
2427 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002428 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002429 DataRate::BitsPerSec(kTargetBitrateBps),
2430 DataRate::BitsPerSec(kTargetBitrateBps),
2431 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002432
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002433 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002434 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002435 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002436 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002437
2438 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002439 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002440 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002441 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2442 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2443
2444 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002445 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002446 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002447 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2448 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2449 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2450
2451 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002452 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002453 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2454 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2455 EXPECT_EQ(1, 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, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002461 const int kWidth = 1280;
2462 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002463 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002464 DataRate::BitsPerSec(kTargetBitrateBps),
2465 DataRate::BitsPerSec(kTargetBitrateBps),
2466 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002467
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002468 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002469 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002470 video_stream_encoder_->SetSource(&source,
2471 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002472 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2473 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002474 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002475
2476 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002477 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002478 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2479 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2480 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2481 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2482
2483 // Trigger adapt down for same input resolution, expect no change.
2484 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2485 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002486 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002487 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2488 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2489 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2490
2491 // Trigger adapt down for larger input resolution, expect no change.
2492 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2493 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002494 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002495 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2496 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2497 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2498
mflodmancc3d4422017-08-03 08:27:51 -07002499 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002500}
2501
mflodmancc3d4422017-08-03 08:27:51 -07002502TEST_F(VideoStreamEncoderTest,
2503 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002504 const int kWidth = 1280;
2505 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002506 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002507 DataRate::BitsPerSec(kTargetBitrateBps),
2508 DataRate::BitsPerSec(kTargetBitrateBps),
2509 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002510
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002511 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002512 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002513 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002514 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002515
2516 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002517 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002518 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002519 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2520 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2521
2522 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002523 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002524 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002525 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2526 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2527
mflodmancc3d4422017-08-03 08:27:51 -07002528 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002529}
2530
mflodmancc3d4422017-08-03 08:27:51 -07002531TEST_F(VideoStreamEncoderTest,
2532 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002533 const int kWidth = 1280;
2534 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002535 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002536 DataRate::BitsPerSec(kTargetBitrateBps),
2537 DataRate::BitsPerSec(kTargetBitrateBps),
2538 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002539
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002540 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002541 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002542 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002543 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002544
2545 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002546 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002547 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002548 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002549 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2550
2551 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002552 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002553 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002554 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002555 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2556
mflodmancc3d4422017-08-03 08:27:51 -07002557 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002558}
2559
mflodmancc3d4422017-08-03 08:27:51 -07002560TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002561 const int kWidth = 1280;
2562 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002563 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002564 DataRate::BitsPerSec(kTargetBitrateBps),
2565 DataRate::BitsPerSec(kTargetBitrateBps),
2566 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002567
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002568 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002569 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002570 video_stream_encoder_->SetSource(&source,
2571 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002572
2573 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2574 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002575 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002576 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2577 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2578 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2579
2580 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002581 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002582 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002583 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2584 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2585 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2586
mflodmancc3d4422017-08-03 08:27:51 -07002587 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002588}
2589
mflodmancc3d4422017-08-03 08:27:51 -07002590TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002591 const int kWidth = 1280;
2592 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002593 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002594 DataRate::BitsPerSec(kTargetBitrateBps),
2595 DataRate::BitsPerSec(kTargetBitrateBps),
2596 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002597
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002598 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002599 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002600 video_stream_encoder_->SetSource(&source,
2601 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002602
2603 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2604 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002605 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002606 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2607 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2608 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2609
2610 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002611 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002612 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002613 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2614 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2615 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2616
mflodmancc3d4422017-08-03 08:27:51 -07002617 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002618}
2619
mflodmancc3d4422017-08-03 08:27:51 -07002620TEST_F(VideoStreamEncoderTest,
2621 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002622 const int kWidth = 1280;
2623 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002624 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002625 DataRate::BitsPerSec(kTargetBitrateBps),
2626 DataRate::BitsPerSec(kTargetBitrateBps),
2627 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002628
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002629 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002630 AdaptingFrameForwarder source;
2631 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002632 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002633 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002634
2635 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002636 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002637 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002638 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2639 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2640
2641 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002642 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002643 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002644 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002645 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002646 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2647 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2648
2649 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002650 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002651 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002652 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2653 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2654 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2655
mflodmancc3d4422017-08-03 08:27:51 -07002656 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002657}
2658
mflodmancc3d4422017-08-03 08:27:51 -07002659TEST_F(VideoStreamEncoderTest,
2660 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002661 const int kWidth = 1280;
2662 const int kHeight = 720;
2663 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002664 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002665 DataRate::BitsPerSec(kTargetBitrateBps),
2666 DataRate::BitsPerSec(kTargetBitrateBps),
2667 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002668
2669 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2670 stats.input_frame_rate = kInputFps;
2671 stats_proxy_->SetMockStats(stats);
2672
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002673 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002674 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2675 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002676 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002677
2678 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002679 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002680 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2681 sink_.WaitForEncodedFrame(2);
2682 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2683
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002684 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002685 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002686 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002687 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002688 // Give the encoder queue time to process the change in degradation preference
2689 // by waiting for an encoded frame.
2690 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2691 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002692 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002693
2694 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002695 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002696 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2697 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002698 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2699
2700 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002701 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002702 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002703
mflodmancc3d4422017-08-03 08:27:51 -07002704 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002705}
2706
mflodmancc3d4422017-08-03 08:27:51 -07002707TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002708 const int kWidth = 1280;
2709 const int kHeight = 720;
2710 const size_t kNumFrames = 10;
2711
Erik Språng4c6ca302019-04-08 15:14:01 +02002712 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002713 DataRate::BitsPerSec(kTargetBitrateBps),
2714 DataRate::BitsPerSec(kTargetBitrateBps),
2715 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002716
asaperssond0de2952017-04-21 01:47:31 -07002717 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002718 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002719 video_source_.set_adaptation_enabled(true);
2720
2721 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2722 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2723
2724 int downscales = 0;
2725 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002726 video_source_.IncomingCapturedFrame(
2727 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2728 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002729
asaperssonfab67072017-04-04 05:51:49 -07002730 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002731 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002732 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002733 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002734
2735 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2736 ++downscales;
2737
2738 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2739 EXPECT_EQ(downscales,
2740 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2741 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002742 }
mflodmancc3d4422017-08-03 08:27:51 -07002743 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002744}
2745
mflodmancc3d4422017-08-03 08:27:51 -07002746TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002747 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2748 const int kWidth = 1280;
2749 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002750 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002751 DataRate::BitsPerSec(kTargetBitrateBps),
2752 DataRate::BitsPerSec(kTargetBitrateBps),
2753 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002754
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002755 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002756 AdaptingFrameForwarder source;
2757 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002758 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002759 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002760
Åsa Persson8c1bf952018-09-13 10:42:19 +02002761 int64_t timestamp_ms = kFrameIntervalMs;
2762 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002763 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002764 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002765 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2766 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2767
2768 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002769 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002770 timestamp_ms += kFrameIntervalMs;
2771 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2772 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002773 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002774 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2775 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2776
2777 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002778 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002779 timestamp_ms += kFrameIntervalMs;
2780 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002781 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002782 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002783 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2784 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2785
2786 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002787 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002788 timestamp_ms += kFrameIntervalMs;
2789 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2790 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002791 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002792 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2793 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2794
2795 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002796 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002797 timestamp_ms += kFrameIntervalMs;
2798 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002799 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002800 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002801 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2802 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2803
mflodmancc3d4422017-08-03 08:27:51 -07002804 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002805}
2806
mflodmancc3d4422017-08-03 08:27:51 -07002807TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002808 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2809 const int kWidth = 1280;
2810 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002811 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002812 DataRate::BitsPerSec(kTargetBitrateBps),
2813 DataRate::BitsPerSec(kTargetBitrateBps),
2814 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002815
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002816 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002817 AdaptingFrameForwarder source;
2818 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002819 video_stream_encoder_->SetSource(&source,
2820 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002821
Åsa Persson8c1bf952018-09-13 10:42:19 +02002822 int64_t timestamp_ms = kFrameIntervalMs;
2823 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002824 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002825 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002826 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2827 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2828
2829 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002830 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002831 timestamp_ms += kFrameIntervalMs;
2832 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2833 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002834 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2835 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2836 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2837
2838 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002839 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002840 timestamp_ms += kFrameIntervalMs;
2841 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002842 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002843 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002844 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2845 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2846
2847 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002848 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002849 timestamp_ms += kFrameIntervalMs;
2850 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2851 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002852 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2853 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2854 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2855
2856 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002857 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002858 timestamp_ms += kFrameIntervalMs;
2859 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002860 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002861 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002862 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2863 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2864
mflodmancc3d4422017-08-03 08:27:51 -07002865 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002866}
2867
Sergey Silkin41c650b2019-10-14 13:12:19 +02002868TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2869 fake_encoder_.SetResolutionBitrateLimits(
2870 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2871
2872 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002873 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2874 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2875 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2876 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002877
2878 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2879 AdaptingFrameForwarder source;
2880 source.set_adaptation_enabled(true);
2881 video_stream_encoder_->SetSource(
2882 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2883
2884 // Insert 720p frame.
2885 int64_t timestamp_ms = kFrameIntervalMs;
2886 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2887 WaitForEncodedFrame(1280, 720);
2888
2889 // Reduce bitrate and trigger adapt down.
2890 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002891 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2892 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2893 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2894 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002895 video_stream_encoder_->TriggerQualityLow();
2896
2897 // Insert 720p frame. It should be downscaled and encoded.
2898 timestamp_ms += kFrameIntervalMs;
2899 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2900 WaitForEncodedFrame(960, 540);
2901
2902 // Trigger adapt up. Higher resolution should not be requested duo to lack
2903 // of bitrate.
2904 video_stream_encoder_->TriggerQualityHigh();
2905 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2906
2907 // Increase bitrate.
2908 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002909 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2910 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2911 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2912 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002913
2914 // Trigger adapt up. Higher resolution should be requested.
2915 video_stream_encoder_->TriggerQualityHigh();
2916 VerifyFpsMaxResolutionMax(source.sink_wants());
2917
2918 video_stream_encoder_->Stop();
2919}
2920
2921TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2922 fake_encoder_.SetResolutionBitrateLimits(
2923 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2924
2925 // Set bitrate equal to min bitrate of 540p.
2926 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002927 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2928 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2929 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2930 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002931
2932 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2933 AdaptingFrameForwarder source;
2934 source.set_adaptation_enabled(true);
2935 video_stream_encoder_->SetSource(
2936 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2937
2938 // Insert 720p frame. It should be dropped and lower resolution should be
2939 // requested.
2940 int64_t timestamp_ms = kFrameIntervalMs;
2941 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2942 ExpectDroppedFrame();
2943 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2944
2945 // Insert 720p frame. It should be downscaled and encoded.
2946 timestamp_ms += kFrameIntervalMs;
2947 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2948 WaitForEncodedFrame(960, 540);
2949
2950 video_stream_encoder_->Stop();
2951}
2952
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002953class BalancedDegradationTest : public VideoStreamEncoderTest {
2954 protected:
2955 void SetupTest() {
2956 // Reset encoder for field trials to take effect.
2957 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002958 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002959
2960 // Enable BALANCED preference.
2961 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002962 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2963 }
2964
2965 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01002966 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002967 DataRate::BitsPerSec(bitrate_bps), DataRate::BitsPerSec(bitrate_bps),
2968 DataRate::BitsPerSec(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002969 }
2970
Åsa Persson45b176f2019-09-30 11:19:05 +02002971 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002972 timestamp_ms_ += kFrameIntervalMs;
2973 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002974 }
2975
2976 void InsertFrameAndWaitForEncoded() {
2977 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002978 sink_.WaitForEncodedFrame(timestamp_ms_);
2979 }
2980
2981 const int kWidth = 640; // pixels:640x360=230400
2982 const int kHeight = 360;
2983 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2984 int64_t timestamp_ms_ = 0;
2985 AdaptingFrameForwarder source_;
2986};
2987
2988TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2989 test::ScopedFieldTrials field_trials(
2990 "WebRTC-Video-BalancedDegradationSettings/"
2991 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2992 SetupTest();
2993
2994 // Force input frame rate.
2995 const int kInputFps = 24;
2996 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2997 stats.input_frame_rate = kInputFps;
2998 stats_proxy_->SetMockStats(stats);
2999
Åsa Persson45b176f2019-09-30 11:19:05 +02003000 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003001 VerifyFpsMaxResolutionMax(source_.sink_wants());
3002
3003 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3004 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
3005 video_stream_encoder_->TriggerQualityLowExpectFalse();
3006 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3007
3008 video_stream_encoder_->Stop();
3009}
3010
3011TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
3012 test::ScopedFieldTrials field_trials(
3013 "WebRTC-Video-BalancedDegradationSettings/"
3014 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
3015 SetupTest();
3016
3017 // Force input frame rate.
3018 const int kInputFps = 25;
3019 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3020 stats.input_frame_rate = kInputFps;
3021 stats_proxy_->SetMockStats(stats);
3022
Åsa Persson45b176f2019-09-30 11:19:05 +02003023 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003024 VerifyFpsMaxResolutionMax(source_.sink_wants());
3025
3026 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3027 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
3028 video_stream_encoder_->TriggerQualityLow();
3029 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3030
3031 video_stream_encoder_->Stop();
3032}
3033
3034TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
3035 test::ScopedFieldTrials field_trials(
3036 "WebRTC-Video-BalancedDegradationSettings/"
3037 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
3038 SetupTest();
3039
3040 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
3041
Åsa Persson45b176f2019-09-30 11:19:05 +02003042 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003043 VerifyFpsMaxResolutionMax(source_.sink_wants());
3044
3045 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
3046 video_stream_encoder_->TriggerQualityLow();
3047 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
3048
3049 video_stream_encoder_->Stop();
3050}
3051
Åsa Perssonccfb3402019-09-25 15:13:04 +02003052TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003053 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02003054 "WebRTC-Video-BalancedDegradationSettings/"
3055 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003056 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02003057
Åsa Persson1b247f12019-08-14 17:26:39 +02003058 const int kMinBitrateBps = 425000;
3059 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003060 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003061
Åsa Persson45b176f2019-09-30 11:19:05 +02003062 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003063 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003064 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3065
3066 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3067 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003068 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003069 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02003070 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3071
3072 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3073 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003074 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003075 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003076 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3077
Åsa Persson30ab0152019-08-27 12:22:33 +02003078 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3079 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003080 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003081 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
3082 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02003083 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3084
3085 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02003086 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003087 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003088 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02003089
Åsa Persson30ab0152019-08-27 12:22:33 +02003090 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003091 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003092 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003093 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003094 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003095 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3096
3097 video_stream_encoder_->Stop();
3098}
3099
Åsa Perssonccfb3402019-09-25 15:13:04 +02003100TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003101 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3102 test::ScopedFieldTrials field_trials(
3103 "WebRTC-Video-BalancedDegradationSettings/"
3104 "pixels:57600|129600|230400,fps:7|24|24/");
3105 SetupTest();
3106 OnBitrateUpdated(kLowTargetBitrateBps);
3107
3108 VerifyNoLimitation(source_.sink_wants());
3109
3110 // Insert frame, expect scaled down:
3111 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3112 InsertFrame();
3113 EXPECT_FALSE(WaitForFrame(1000));
3114 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3115 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3116
3117 // Insert frame, expect scaled down:
3118 // resolution (320x180@24fps).
3119 InsertFrame();
3120 EXPECT_FALSE(WaitForFrame(1000));
3121 EXPECT_LT(source_.sink_wants().max_pixel_count,
3122 source_.last_wants().max_pixel_count);
3123 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3124
3125 // Frame should not be dropped (min pixels per frame reached).
3126 InsertFrameAndWaitForEncoded();
3127
3128 video_stream_encoder_->Stop();
3129}
3130
3131TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003132 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003133 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003134 "WebRTC-Video-BalancedDegradationSettings/"
3135 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003136 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003137
Åsa Persson30ab0152019-08-27 12:22:33 +02003138 const int kResolutionMinBitrateBps = 435000;
3139 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003140 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003141
Åsa Persson45b176f2019-09-30 11:19:05 +02003142 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003143 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003144 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3145
3146 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3147 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003148 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003149 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003150 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3151
3152 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3153 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003154 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003155 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003156 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3157
3158 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3159 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003160 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003161 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003162 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3163
Åsa Persson30ab0152019-08-27 12:22:33 +02003164 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3165 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003166 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003167 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003168 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3169
3170 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3171 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003172 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003173 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3174
3175 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003176 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003177 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003178 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003179 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003180 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3181
3182 video_stream_encoder_->Stop();
3183}
3184
Åsa Perssonccfb3402019-09-25 15:13:04 +02003185TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003186 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003187 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003188 "WebRTC-Video-BalancedDegradationSettings/"
3189 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003190 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003191
Åsa Persson30ab0152019-08-27 12:22:33 +02003192 const int kMinBitrateBps = 425000;
3193 const int kTooLowMinBitrateBps = 424000;
3194 const int kResolutionMinBitrateBps = 435000;
3195 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003196 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003197
Åsa Persson45b176f2019-09-30 11:19:05 +02003198 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003199 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003200 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3201
3202 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3203 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003204 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003205 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003206 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3207
3208 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3209 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003210 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003211 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003212 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3213
3214 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3215 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003216 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003217 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003218 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3219
3220 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3221 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003222 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003223 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3224
3225 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003226 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003227 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003228 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003229 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003230 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3231
3232 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003233 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003234 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003235 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003236 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3237
3238 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003239 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003240 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003241 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003242 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003243 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3244
Åsa Persson1b247f12019-08-14 17:26:39 +02003245 video_stream_encoder_->Stop();
3246}
3247
mflodmancc3d4422017-08-03 08:27:51 -07003248TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003249 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3250 const int kWidth = 1280;
3251 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003252 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003253 DataRate::BitsPerSec(kTargetBitrateBps),
3254 DataRate::BitsPerSec(kTargetBitrateBps),
3255 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003256
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003257 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003258 AdaptingFrameForwarder source;
3259 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003260 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003261 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003262
Åsa Persson8c1bf952018-09-13 10:42:19 +02003263 int64_t timestamp_ms = kFrameIntervalMs;
3264 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003265 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003266 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003267 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3268 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3269 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3270 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3271
3272 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003273 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003274 timestamp_ms += kFrameIntervalMs;
3275 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3276 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003277 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003278 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3279 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3280 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3281 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3282
3283 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003284 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003285 timestamp_ms += kFrameIntervalMs;
3286 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3287 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003288 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003289 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3290 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3291 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3292 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3293
Jonathan Yubc771b72017-12-08 17:04:29 -08003294 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003295 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003296 timestamp_ms += kFrameIntervalMs;
3297 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3298 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003299 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003300 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3301 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003302 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003303 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3304
Jonathan Yubc771b72017-12-08 17:04:29 -08003305 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003306 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003307 timestamp_ms += kFrameIntervalMs;
3308 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3309 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003310 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003311 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003312 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3313 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3314 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3315 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3316
Jonathan Yubc771b72017-12-08 17:04:29 -08003317 // Trigger quality adapt down, expect no change (min resolution reached).
3318 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003319 timestamp_ms += kFrameIntervalMs;
3320 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3321 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003322 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3323 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3324 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3325 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3326 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3327
3328 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003329 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003330 timestamp_ms += kFrameIntervalMs;
3331 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3332 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003333 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003334 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3335 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3336 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3337 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3338
3339 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3340 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003341 timestamp_ms += kFrameIntervalMs;
3342 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3343 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003344 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3345 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3346 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3347 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3348 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3349
3350 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3351 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003352 timestamp_ms += kFrameIntervalMs;
3353 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3354 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003355 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003356 last_wants = source.sink_wants();
3357 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3358 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003359 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003360 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3361
3362 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003363 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003364 timestamp_ms += kFrameIntervalMs;
3365 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3366 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003367 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003368 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3369 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003370 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003371 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3372
3373 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003374 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003375 timestamp_ms += kFrameIntervalMs;
3376 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003377 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003378 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003379 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003380 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3381 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003382 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003383 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003384
mflodmancc3d4422017-08-03 08:27:51 -07003385 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003386}
3387
mflodmancc3d4422017-08-03 08:27:51 -07003388TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003389 const int kWidth = 640;
3390 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003391
Erik Språng4c6ca302019-04-08 15:14:01 +02003392 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003393 DataRate::BitsPerSec(kTargetBitrateBps),
3394 DataRate::BitsPerSec(kTargetBitrateBps),
3395 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003396
perkj803d97f2016-11-01 11:45:46 -07003397 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003398 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003399 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003400 }
3401
mflodmancc3d4422017-08-03 08:27:51 -07003402 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003403 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003404 video_source_.IncomingCapturedFrame(CreateFrame(
3405 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003406 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003407 }
3408
mflodmancc3d4422017-08-03 08:27:51 -07003409 video_stream_encoder_->Stop();
3410 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003411 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003412
Ying Wangef3998f2019-12-09 13:06:53 +01003413 EXPECT_METRIC_EQ(
3414 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3415 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003416 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3417}
3418
mflodmancc3d4422017-08-03 08:27:51 -07003419TEST_F(VideoStreamEncoderTest,
3420 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003421 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003422 DataRate::BitsPerSec(kTargetBitrateBps),
3423 DataRate::BitsPerSec(kTargetBitrateBps),
3424 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003425 const int kWidth = 640;
3426 const int kHeight = 360;
3427
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003428 video_stream_encoder_->SetSource(&video_source_,
3429 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003430
3431 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3432 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003433 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003434 }
3435
mflodmancc3d4422017-08-03 08:27:51 -07003436 video_stream_encoder_->Stop();
3437 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003438 stats_proxy_.reset();
3439
3440 EXPECT_EQ(0,
3441 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3442}
3443
mflodmancc3d4422017-08-03 08:27:51 -07003444TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003445 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003446 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003447
3448 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003449 const VideoBitrateAllocation expected_bitrate =
Mirta Dvornicic6799d732020-02-12 15:36:49 +01003450 SimulcastRateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003451 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3452 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003453
sprang57c2fff2017-01-16 06:24:02 -08003454 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003455 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003456 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003457 DataRate::BitsPerSec(kLowTargetBitrateBps),
3458 DataRate::BitsPerSec(kLowTargetBitrateBps),
3459 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003460
sprang57c2fff2017-01-16 06:24:02 -08003461 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003462 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3463 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003464 VideoBitrateAllocation bitrate_allocation =
3465 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003466 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003467 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003468 // TODO(srte): The use of millisecs here looks like an error, but the tests
3469 // fails using seconds, this should be investigated.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003470 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003471
3472 // Not called on second frame.
3473 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3474 .Times(0);
3475 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003476 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3477 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003478 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003479
3480 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003481 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3482 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003483 const int64_t start_time_ms = rtc::TimeMillis();
3484 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3485 video_source_.IncomingCapturedFrame(
3486 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3487 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003488 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003489 }
3490
3491 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003492 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003493
mflodmancc3d4422017-08-03 08:27:51 -07003494 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003495}
3496
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003497TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3498 // 2 TLs configured, temporal layers supported by encoder.
3499 const int kNumTemporalLayers = 2;
3500 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3501 fake_encoder_.SetTemporalLayersSupported(0, true);
3502
3503 // Bitrate allocated across temporal layers.
3504 const int kTl0Bps = kTargetBitrateBps *
3505 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003506 kNumTemporalLayers, /*temporal_id*/ 0,
3507 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003508 const int kTl1Bps = kTargetBitrateBps *
3509 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003510 kNumTemporalLayers, /*temporal_id*/ 1,
3511 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003512 VideoBitrateAllocation expected_bitrate;
3513 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3514 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3515
3516 VerifyAllocatedBitrate(expected_bitrate);
3517 video_stream_encoder_->Stop();
3518}
3519
3520TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3521 // 2 TLs configured, temporal layers not supported by encoder.
3522 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3523 fake_encoder_.SetTemporalLayersSupported(0, false);
3524
3525 // Temporal layers not supported by the encoder.
3526 // Total bitrate should be at ti:0.
3527 VideoBitrateAllocation expected_bitrate;
3528 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3529
3530 VerifyAllocatedBitrate(expected_bitrate);
3531 video_stream_encoder_->Stop();
3532}
3533
3534TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3535 // 2 TLs configured, temporal layers only supported for first stream.
3536 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3537 fake_encoder_.SetTemporalLayersSupported(0, true);
3538 fake_encoder_.SetTemporalLayersSupported(1, false);
3539
3540 const int kS0Bps = 150000;
3541 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003542 kS0Bps *
3543 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3544 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003545 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003546 kS0Bps *
3547 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3548 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003549 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3550 // Temporal layers not supported by si:1.
3551 VideoBitrateAllocation expected_bitrate;
3552 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3553 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3554 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3555
3556 VerifyAllocatedBitrate(expected_bitrate);
3557 video_stream_encoder_->Stop();
3558}
3559
Niels Möller7dc26b72017-12-06 10:27:48 +01003560TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3561 const int kFrameWidth = 1280;
3562 const int kFrameHeight = 720;
3563 const int kFramerate = 24;
3564
Erik Språng4c6ca302019-04-08 15:14:01 +02003565 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003566 DataRate::BitsPerSec(kTargetBitrateBps),
3567 DataRate::BitsPerSec(kTargetBitrateBps),
3568 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003569 test::FrameForwarder source;
3570 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003571 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003572
3573 // Insert a single frame, triggering initial configuration.
3574 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3575 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3576
3577 EXPECT_EQ(
3578 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3579 kDefaultFramerate);
3580
3581 // Trigger reconfigure encoder (without resetting the entire instance).
3582 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003583 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003584 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3585 video_encoder_config.number_of_streams = 1;
3586 video_encoder_config.video_stream_factory =
3587 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3588 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003589 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003590 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3591
3592 // Detector should be updated with fps limit from codec config.
3593 EXPECT_EQ(
3594 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3595 kFramerate);
3596
3597 // Trigger overuse, max framerate should be reduced.
3598 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3599 stats.input_frame_rate = kFramerate;
3600 stats_proxy_->SetMockStats(stats);
3601 video_stream_encoder_->TriggerCpuOveruse();
3602 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3603 int adapted_framerate =
3604 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3605 EXPECT_LT(adapted_framerate, kFramerate);
3606
3607 // Trigger underuse, max framerate should go back to codec configured fps.
3608 // Set extra low fps, to make sure it's actually reset, not just incremented.
3609 stats = stats_proxy_->GetStats();
3610 stats.input_frame_rate = adapted_framerate / 2;
3611 stats_proxy_->SetMockStats(stats);
3612 video_stream_encoder_->TriggerCpuNormalUsage();
3613 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3614 EXPECT_EQ(
3615 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3616 kFramerate);
3617
3618 video_stream_encoder_->Stop();
3619}
3620
3621TEST_F(VideoStreamEncoderTest,
3622 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3623 const int kFrameWidth = 1280;
3624 const int kFrameHeight = 720;
3625 const int kLowFramerate = 15;
3626 const int kHighFramerate = 25;
3627
Erik Språng4c6ca302019-04-08 15:14:01 +02003628 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003629 DataRate::BitsPerSec(kTargetBitrateBps),
3630 DataRate::BitsPerSec(kTargetBitrateBps),
3631 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003632 test::FrameForwarder source;
3633 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003634 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003635
3636 // Trigger initial configuration.
3637 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003638 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003639 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3640 video_encoder_config.number_of_streams = 1;
3641 video_encoder_config.video_stream_factory =
3642 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3643 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3644 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003645 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003646 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3647
3648 EXPECT_EQ(
3649 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3650 kLowFramerate);
3651
3652 // Trigger overuse, max framerate should be reduced.
3653 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3654 stats.input_frame_rate = kLowFramerate;
3655 stats_proxy_->SetMockStats(stats);
3656 video_stream_encoder_->TriggerCpuOveruse();
3657 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3658 int adapted_framerate =
3659 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3660 EXPECT_LT(adapted_framerate, kLowFramerate);
3661
3662 // Reconfigure the encoder with a new (higher max framerate), max fps should
3663 // still respect the adaptation.
3664 video_encoder_config.video_stream_factory =
3665 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3666 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3667 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003668 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003669 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3670
3671 EXPECT_EQ(
3672 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3673 adapted_framerate);
3674
3675 // Trigger underuse, max framerate should go back to codec configured fps.
3676 stats = stats_proxy_->GetStats();
3677 stats.input_frame_rate = adapted_framerate;
3678 stats_proxy_->SetMockStats(stats);
3679 video_stream_encoder_->TriggerCpuNormalUsage();
3680 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3681 EXPECT_EQ(
3682 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3683 kHighFramerate);
3684
3685 video_stream_encoder_->Stop();
3686}
3687
mflodmancc3d4422017-08-03 08:27:51 -07003688TEST_F(VideoStreamEncoderTest,
3689 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003690 const int kFrameWidth = 1280;
3691 const int kFrameHeight = 720;
3692 const int kFramerate = 24;
3693
Erik Språng4c6ca302019-04-08 15:14:01 +02003694 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003695 DataRate::BitsPerSec(kTargetBitrateBps),
3696 DataRate::BitsPerSec(kTargetBitrateBps),
3697 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003698 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003699 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003700 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003701
3702 // Trigger initial configuration.
3703 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003704 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003705 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3706 video_encoder_config.number_of_streams = 1;
3707 video_encoder_config.video_stream_factory =
3708 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3709 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003710 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003711 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003712 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003713
Niels Möller7dc26b72017-12-06 10:27:48 +01003714 EXPECT_EQ(
3715 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3716 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003717
3718 // Trigger overuse, max framerate should be reduced.
3719 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3720 stats.input_frame_rate = kFramerate;
3721 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003722 video_stream_encoder_->TriggerCpuOveruse();
3723 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003724 int adapted_framerate =
3725 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003726 EXPECT_LT(adapted_framerate, kFramerate);
3727
3728 // Change degradation preference to not enable framerate scaling. Target
3729 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003730 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003731 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003732 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003733 EXPECT_EQ(
3734 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3735 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003736
mflodmancc3d4422017-08-03 08:27:51 -07003737 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003738}
3739
mflodmancc3d4422017-08-03 08:27:51 -07003740TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003741 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003742 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003743 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3744 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3745 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003746 const int kWidth = 640;
3747 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003748
asaperssonfab67072017-04-04 05:51:49 -07003749 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003750
3751 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003752 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003753
3754 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003755 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003756
sprangc5d62e22017-04-02 23:53:04 -07003757 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003758
asaperssonfab67072017-04-04 05:51:49 -07003759 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003760 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003761 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003762
3763 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003764 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003765
sprangc5d62e22017-04-02 23:53:04 -07003766 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003767
mflodmancc3d4422017-08-03 08:27:51 -07003768 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003769}
3770
mflodmancc3d4422017-08-03 08:27:51 -07003771TEST_F(VideoStreamEncoderTest,
3772 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003773 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003774 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003775 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3776 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3777 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003778 const int kWidth = 640;
3779 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003780
3781 // We expect the n initial frames to get dropped.
3782 int i;
3783 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003784 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003785 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003786 }
3787 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003788 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003789 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003790
3791 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003792 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003793
mflodmancc3d4422017-08-03 08:27:51 -07003794 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003795}
3796
mflodmancc3d4422017-08-03 08:27:51 -07003797TEST_F(VideoStreamEncoderTest,
3798 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003799 const int kWidth = 640;
3800 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003801 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003802 DataRate::BitsPerSec(kLowTargetBitrateBps),
3803 DataRate::BitsPerSec(kLowTargetBitrateBps),
3804 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003805
3806 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003807 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003808 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003809
asaperssonfab67072017-04-04 05:51:49 -07003810 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003811 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003812 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003813
mflodmancc3d4422017-08-03 08:27:51 -07003814 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003815}
3816
mflodmancc3d4422017-08-03 08:27:51 -07003817TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003818 const int kWidth = 640;
3819 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003820 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003821
3822 VideoEncoderConfig video_encoder_config;
3823 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3824 // Make format different, to force recreation of encoder.
3825 video_encoder_config.video_format.parameters["foo"] = "foo";
3826 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003827 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003828 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003829 DataRate::BitsPerSec(kLowTargetBitrateBps),
3830 DataRate::BitsPerSec(kLowTargetBitrateBps),
3831 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003832
kthelgasonb83797b2017-02-14 11:57:25 -08003833 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003834 video_stream_encoder_->SetSource(&video_source_,
3835 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003836
asaperssonfab67072017-04-04 05:51:49 -07003837 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003838 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003839 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003840
mflodmancc3d4422017-08-03 08:27:51 -07003841 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003842 fake_encoder_.SetQualityScaling(true);
3843}
3844
Åsa Persson139f4dc2019-08-02 09:29:58 +02003845TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3846 webrtc::test::ScopedFieldTrials field_trials(
3847 "WebRTC-Video-QualityScalerSettings/"
3848 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3849 // Reset encoder for field trials to take effect.
3850 ConfigureEncoder(video_encoder_config_.Copy());
3851 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3852 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3853 const int kWidth = 640;
3854 const int kHeight = 360;
3855
3856 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003857 DataRate::BitsPerSec(kTargetBitrateBps),
3858 DataRate::BitsPerSec(kTargetBitrateBps),
3859 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003860 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3861 // Frame should not be dropped.
3862 WaitForEncodedFrame(1);
3863
3864 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003865 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3866 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3867 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003868 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3869 // Frame should not be dropped.
3870 WaitForEncodedFrame(2);
3871
3872 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003873 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3874 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3875 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003876 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3877 // Expect to drop this frame, the wait should time out.
3878 ExpectDroppedFrame();
3879
3880 // Expect the sink_wants to specify a scaled frame.
3881 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3882 video_stream_encoder_->Stop();
3883}
3884
Åsa Perssone644a032019-11-08 15:56:00 +01003885TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3886 webrtc::test::ScopedFieldTrials field_trials(
3887 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3888
3889 // Reset encoder for field trials to take effect.
3890 VideoEncoderConfig config = video_encoder_config_.Copy();
3891 config.max_bitrate_bps = kTargetBitrateBps;
3892 ConfigureEncoder(std::move(config));
3893 fake_encoder_.SetQp(kQpLow);
3894
3895 // Enable MAINTAIN_FRAMERATE preference.
3896 AdaptingFrameForwarder source;
3897 source.set_adaptation_enabled(true);
3898 video_stream_encoder_->SetSource(&source,
3899 DegradationPreference::MAINTAIN_FRAMERATE);
3900
3901 // Start at low bitrate.
3902 const int kLowBitrateBps = 200000;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003903 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(kLowBitrateBps),
3904 DataRate::BitsPerSec(kLowBitrateBps),
3905 DataRate::BitsPerSec(kLowBitrateBps),
3906 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003907
3908 // Expect first frame to be dropped and resolution to be limited.
3909 const int kWidth = 1280;
3910 const int kHeight = 720;
3911 const int64_t kFrameIntervalMs = 100;
3912 int64_t timestamp_ms = kFrameIntervalMs;
3913 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3914 ExpectDroppedFrame();
3915 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3916
3917 // Increase bitrate to encoder max.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003918 video_stream_encoder_->OnBitrateUpdated(
3919 DataRate::BitsPerSec(config.max_bitrate_bps),
3920 DataRate::BitsPerSec(config.max_bitrate_bps),
3921 DataRate::BitsPerSec(config.max_bitrate_bps), 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003922
3923 // Insert frames and advance |min_duration_ms|.
3924 for (size_t i = 1; i <= 10; i++) {
3925 timestamp_ms += kFrameIntervalMs;
3926 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3927 WaitForEncodedFrame(timestamp_ms);
3928 }
3929 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3930 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3931
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003932 fake_clock_.AdvanceTime(TimeDelta::Millis(2000));
Åsa Perssone644a032019-11-08 15:56:00 +01003933
3934 // Insert frame should trigger high BW and release quality limitation.
3935 timestamp_ms += kFrameIntervalMs;
3936 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3937 WaitForEncodedFrame(timestamp_ms);
3938 VerifyFpsMaxResolutionMax(source.sink_wants());
3939
3940 // Frame should not be adapted.
3941 timestamp_ms += kFrameIntervalMs;
3942 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3943 WaitForEncodedFrame(kWidth, kHeight);
3944 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3945
3946 video_stream_encoder_->Stop();
3947}
3948
mflodmancc3d4422017-08-03 08:27:51 -07003949TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003950 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3951 const int kTooSmallWidth = 10;
3952 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003953 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003954 DataRate::BitsPerSec(kTargetBitrateBps),
3955 DataRate::BitsPerSec(kTargetBitrateBps),
3956 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003957
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003958 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003959 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003960 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003961 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003962 VerifyNoLimitation(source.sink_wants());
3963 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3964
3965 // Trigger adapt down, too small frame, expect no change.
3966 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003967 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003968 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003969 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003970 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3971 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3972
mflodmancc3d4422017-08-03 08:27:51 -07003973 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003974}
3975
mflodmancc3d4422017-08-03 08:27:51 -07003976TEST_F(VideoStreamEncoderTest,
3977 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003978 const int kTooSmallWidth = 10;
3979 const int kTooSmallHeight = 10;
3980 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003981 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003982 DataRate::BitsPerSec(kTargetBitrateBps),
3983 DataRate::BitsPerSec(kTargetBitrateBps),
3984 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003985
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003986 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003987 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003988 video_stream_encoder_->SetSource(&source,
3989 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003990 VerifyNoLimitation(source.sink_wants());
3991 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3992 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3993
3994 // Trigger adapt down, expect limited framerate.
3995 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003996 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003997 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003998 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3999 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4000 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4001 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4002
4003 // Trigger adapt down, too small frame, expect no change.
4004 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004005 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07004006 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004007 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4008 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4009 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4010 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4011
mflodmancc3d4422017-08-03 08:27:51 -07004012 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004013}
4014
mflodmancc3d4422017-08-03 08:27:51 -07004015TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07004016 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02004017 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004018 DataRate::BitsPerSec(kTargetBitrateBps),
4019 DataRate::BitsPerSec(kTargetBitrateBps),
4020 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02004021 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07004022 const int kFrameWidth = 1280;
4023 const int kFrameHeight = 720;
4024 video_source_.IncomingCapturedFrame(
4025 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004026 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07004027 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07004028}
4029
sprangb1ca0732017-02-01 08:38:12 -08004030// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07004031TEST_F(VideoStreamEncoderTest,
4032 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004033 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004034 DataRate::BitsPerSec(kTargetBitrateBps),
4035 DataRate::BitsPerSec(kTargetBitrateBps),
4036 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08004037
4038 const int kFrameWidth = 1280;
4039 const int kFrameHeight = 720;
4040 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07004041 // requested by
4042 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08004043 video_source_.set_adaptation_enabled(true);
4044
4045 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004046 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004047 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004048
4049 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004050 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08004051 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004052 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004053 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08004054
asaperssonfab67072017-04-04 05:51:49 -07004055 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07004056 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08004057 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004058 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004059 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004060
mflodmancc3d4422017-08-03 08:27:51 -07004061 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08004062}
sprangfe627f32017-03-29 08:24:59 -07004063
mflodmancc3d4422017-08-03 08:27:51 -07004064TEST_F(VideoStreamEncoderTest,
4065 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07004066 const int kFrameWidth = 1280;
4067 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07004068
Erik Språng4c6ca302019-04-08 15:14:01 +02004069 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004070 DataRate::BitsPerSec(kTargetBitrateBps),
4071 DataRate::BitsPerSec(kTargetBitrateBps),
4072 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004073 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004074 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004075 video_source_.set_adaptation_enabled(true);
4076
sprang4847ae62017-06-27 07:06:52 -07004077 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004078
4079 video_source_.IncomingCapturedFrame(
4080 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004081 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004082
4083 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07004084 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004085
4086 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07004087 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004088 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004089 video_source_.IncomingCapturedFrame(
4090 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004091 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004092 }
4093
4094 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07004095 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004096 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004097 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004098 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004099 video_source_.IncomingCapturedFrame(
4100 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004101 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004102 ++num_frames_dropped;
4103 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004104 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004105 }
4106 }
4107
sprang4847ae62017-06-27 07:06:52 -07004108 // Add some slack to account for frames dropped by the frame dropper.
4109 const int kErrorMargin = 1;
4110 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004111 kErrorMargin);
4112
4113 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004114 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004115 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004116 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004117 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004118 video_source_.IncomingCapturedFrame(
4119 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004120 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004121 ++num_frames_dropped;
4122 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004123 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004124 }
4125 }
sprang4847ae62017-06-27 07:06:52 -07004126 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004127 kErrorMargin);
4128
4129 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004130 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004131 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004132 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004133 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004134 video_source_.IncomingCapturedFrame(
4135 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004136 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004137 ++num_frames_dropped;
4138 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004139 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004140 }
4141 }
sprang4847ae62017-06-27 07:06:52 -07004142 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004143 kErrorMargin);
4144
4145 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004146 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004147 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004148 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004149 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004150 video_source_.IncomingCapturedFrame(
4151 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004152 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004153 ++num_frames_dropped;
4154 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004155 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004156 }
4157 }
4158 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4159
mflodmancc3d4422017-08-03 08:27:51 -07004160 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004161}
4162
mflodmancc3d4422017-08-03 08:27:51 -07004163TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004164 const int kFramerateFps = 5;
4165 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004166 const int kFrameWidth = 1280;
4167 const int kFrameHeight = 720;
4168
sprang4847ae62017-06-27 07:06:52 -07004169 // Reconfigure encoder with two temporal layers and screensharing, which will
4170 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004171 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004172
Erik Språng4c6ca302019-04-08 15:14:01 +02004173 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004174 DataRate::BitsPerSec(kTargetBitrateBps),
4175 DataRate::BitsPerSec(kTargetBitrateBps),
4176 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004177 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004178 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004179 video_source_.set_adaptation_enabled(true);
4180
sprang4847ae62017-06-27 07:06:52 -07004181 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004182
4183 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004184 rtc::VideoSinkWants last_wants;
4185 do {
4186 last_wants = video_source_.sink_wants();
4187
sprangc5d62e22017-04-02 23:53:04 -07004188 // Insert frames to get a new fps estimate...
4189 for (int j = 0; j < kFramerateFps; ++j) {
4190 video_source_.IncomingCapturedFrame(
4191 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004192 if (video_source_.last_sent_width()) {
4193 sink_.WaitForEncodedFrame(timestamp_ms);
4194 }
sprangc5d62e22017-04-02 23:53:04 -07004195 timestamp_ms += kFrameIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004196 fake_clock_.AdvanceTime(TimeDelta::Millis(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004197 }
4198 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004199 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004200 } while (video_source_.sink_wants().max_framerate_fps <
4201 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004202
Jonathan Yubc771b72017-12-08 17:04:29 -08004203 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004204
mflodmancc3d4422017-08-03 08:27:51 -07004205 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004206}
asaperssonf7e294d2017-06-13 23:25:22 -07004207
mflodmancc3d4422017-08-03 08:27:51 -07004208TEST_F(VideoStreamEncoderTest,
4209 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004210 const int kWidth = 1280;
4211 const int kHeight = 720;
4212 const int64_t kFrameIntervalMs = 150;
4213 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004214 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004215 DataRate::BitsPerSec(kTargetBitrateBps),
4216 DataRate::BitsPerSec(kTargetBitrateBps),
4217 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004218
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004219 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004220 AdaptingFrameForwarder source;
4221 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004222 video_stream_encoder_->SetSource(&source,
4223 webrtc::DegradationPreference::BALANCED);
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(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004227 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004228 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4229 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4230 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4231
4232 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004233 video_stream_encoder_->TriggerQualityLow();
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 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4238 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4239 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4240 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4241
4242 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004243 video_stream_encoder_->TriggerQualityLow();
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 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4248 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4249 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4250 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4251
4252 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004253 video_stream_encoder_->TriggerQualityLow();
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 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4258 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4259 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4260 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4261
4262 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004263 video_stream_encoder_->TriggerQualityLow();
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 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4268 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4269 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4270 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4271
4272 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004273 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004274 timestamp_ms += kFrameIntervalMs;
4275 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004276 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004277 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4278 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4279 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4280 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4281
4282 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004283 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004284 timestamp_ms += kFrameIntervalMs;
4285 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004286 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004287 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4288 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4289 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4290 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4291
4292 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004293 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004294 timestamp_ms += kFrameIntervalMs;
4295 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004296 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004297 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4298 rtc::VideoSinkWants last_wants = source.sink_wants();
4299 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4300 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4301 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4302
4303 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004304 video_stream_encoder_->TriggerQualityLow();
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004308 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4309 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4310 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4311 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4312
4313 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004314 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004315 timestamp_ms += kFrameIntervalMs;
4316 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004317 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004318 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4319 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4320 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4321 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4322
4323 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004324 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004325 timestamp_ms += kFrameIntervalMs;
4326 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004327 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004328 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4329 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4330 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4331 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4332
4333 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004334 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004335 timestamp_ms += kFrameIntervalMs;
4336 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004337 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004338 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4339 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4340 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4341 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4342
4343 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004344 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004345 timestamp_ms += kFrameIntervalMs;
4346 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004347 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004348 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4349 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4350 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4351 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4352
4353 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004354 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004355 timestamp_ms += kFrameIntervalMs;
4356 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004357 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004358 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4359 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4360 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4361 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4362
4363 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004364 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004365 timestamp_ms += kFrameIntervalMs;
4366 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004367 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004368 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4369 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4370 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4371 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4372
Åsa Persson30ab0152019-08-27 12:22:33 +02004373 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004374 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004375 timestamp_ms += kFrameIntervalMs;
4376 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004377 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004378 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004379 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004380 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4381 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4382 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4383
4384 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004385 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004386 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004387 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4388
mflodmancc3d4422017-08-03 08:27:51 -07004389 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004390}
4391
mflodmancc3d4422017-08-03 08:27:51 -07004392TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004393 const int kWidth = 1280;
4394 const int kHeight = 720;
4395 const int64_t kFrameIntervalMs = 150;
4396 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004397 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004398 DataRate::BitsPerSec(kTargetBitrateBps),
4399 DataRate::BitsPerSec(kTargetBitrateBps),
4400 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004401
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004402 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004403 AdaptingFrameForwarder source;
4404 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004405 video_stream_encoder_->SetSource(&source,
4406 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004407 timestamp_ms += kFrameIntervalMs;
4408 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004409 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004410 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004411 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4412 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4413 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4414 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4415 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4416 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4417
4418 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004419 video_stream_encoder_->TriggerCpuOveruse();
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004423 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4424 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4425 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4426 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4427 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4428 EXPECT_EQ(1, 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 resolution (640x360@30fps).
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 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4437 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4438 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4439 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4440 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4441 EXPECT_EQ(2, 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 reduced fps (640x360@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 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4450 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4451 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4452 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4453 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4454 EXPECT_EQ(2, 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 increased fps (640x360@30fps).
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 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4463 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4464 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4465 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4466 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4467 EXPECT_EQ(3, 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 upscaled resolution (960x540@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);
asaperssonf7e294d2017-06-13 23:25:22 -07004475 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4476 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4477 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4478 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4479 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4480 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4481 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4482
4483 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004484 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004485 timestamp_ms += kFrameIntervalMs;
4486 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004487 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004488 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004489 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004490 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4491 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4492 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4493 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4494 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4495 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4496
4497 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004498 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004499 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004500 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4501 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4502
mflodmancc3d4422017-08-03 08:27:51 -07004503 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004504}
4505
mflodmancc3d4422017-08-03 08:27:51 -07004506TEST_F(VideoStreamEncoderTest,
4507 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004508 const int kWidth = 640;
4509 const int kHeight = 360;
4510 const int kFpsLimit = 15;
4511 const int64_t kFrameIntervalMs = 150;
4512 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004513 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004514 DataRate::BitsPerSec(kTargetBitrateBps),
4515 DataRate::BitsPerSec(kTargetBitrateBps),
4516 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004517
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004518 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004519 AdaptingFrameForwarder source;
4520 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004521 video_stream_encoder_->SetSource(&source,
4522 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004523 timestamp_ms += kFrameIntervalMs;
4524 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004525 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004526 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004527 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4528 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4529 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4530 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4531 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4532 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4533
4534 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004535 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004536 timestamp_ms += kFrameIntervalMs;
4537 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004538 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004539 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4540 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4541 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4542 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4543 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4544 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4545 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4546
4547 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004548 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004549 timestamp_ms += kFrameIntervalMs;
4550 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004551 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004552 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4553 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4554 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4555 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4556 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4557 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4558 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4559
4560 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004561 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004562 timestamp_ms += kFrameIntervalMs;
4563 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004564 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004565 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4566 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4567 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4568 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4569 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4570 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4571 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4572
4573 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004574 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004575 timestamp_ms += kFrameIntervalMs;
4576 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004577 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004578 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004579 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4580 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4581 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4582 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4583 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4584 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4585
4586 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004587 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004588 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004589 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4590 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4591
mflodmancc3d4422017-08-03 08:27:51 -07004592 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004593}
4594
mflodmancc3d4422017-08-03 08:27:51 -07004595TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004596 const int kFrameWidth = 1920;
4597 const int kFrameHeight = 1080;
4598 // 3/4 of 1920.
4599 const int kAdaptedFrameWidth = 1440;
4600 // 3/4 of 1080 rounded down to multiple of 4.
4601 const int kAdaptedFrameHeight = 808;
4602 const int kFramerate = 24;
4603
Erik Språng4c6ca302019-04-08 15:14:01 +02004604 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004605 DataRate::BitsPerSec(kTargetBitrateBps),
4606 DataRate::BitsPerSec(kTargetBitrateBps),
4607 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004608 // Trigger reconfigure encoder (without resetting the entire instance).
4609 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004610 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004611 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4612 video_encoder_config.number_of_streams = 1;
4613 video_encoder_config.video_stream_factory =
4614 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004615 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004616 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004617 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004618
4619 video_source_.set_adaptation_enabled(true);
4620
4621 video_source_.IncomingCapturedFrame(
4622 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004623 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004624
4625 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004626 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004627 video_source_.IncomingCapturedFrame(
4628 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004629 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004630
mflodmancc3d4422017-08-03 08:27:51 -07004631 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004632}
4633
mflodmancc3d4422017-08-03 08:27:51 -07004634TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004635 const int kFrameWidth = 1280;
4636 const int kFrameHeight = 720;
4637 const int kLowFps = 2;
4638 const int kHighFps = 30;
4639
Erik Språng4c6ca302019-04-08 15:14:01 +02004640 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004641 DataRate::BitsPerSec(kTargetBitrateBps),
4642 DataRate::BitsPerSec(kTargetBitrateBps),
4643 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004644
4645 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4646 max_framerate_ = kLowFps;
4647
4648 // Insert 2 seconds of 2fps video.
4649 for (int i = 0; i < kLowFps * 2; ++i) {
4650 video_source_.IncomingCapturedFrame(
4651 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4652 WaitForEncodedFrame(timestamp_ms);
4653 timestamp_ms += 1000 / kLowFps;
4654 }
4655
4656 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004657 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004658 DataRate::BitsPerSec(kTargetBitrateBps),
4659 DataRate::BitsPerSec(kTargetBitrateBps),
4660 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004661 video_source_.IncomingCapturedFrame(
4662 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4663 WaitForEncodedFrame(timestamp_ms);
4664 timestamp_ms += 1000 / kLowFps;
4665
4666 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4667
4668 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004669 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004670 const int kFrameIntervalMs = 1000 / kHighFps;
4671 max_framerate_ = kHighFps;
4672 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4673 video_source_.IncomingCapturedFrame(
4674 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4675 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4676 // be dropped if the encoder hans't been updated with the new higher target
4677 // framerate yet, causing it to overshoot the target bitrate and then
4678 // suffering the wrath of the media optimizer.
4679 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4680 timestamp_ms += kFrameIntervalMs;
4681 }
4682
4683 // Don expect correct measurement just yet, but it should be higher than
4684 // before.
4685 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4686
mflodmancc3d4422017-08-03 08:27:51 -07004687 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004688}
4689
mflodmancc3d4422017-08-03 08:27:51 -07004690TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004691 const int kFrameWidth = 1280;
4692 const int kFrameHeight = 720;
4693 const int kTargetBitrateBps = 1000000;
4694
4695 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004696 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004697 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004698 DataRate::BitsPerSec(kTargetBitrateBps),
4699 DataRate::BitsPerSec(kTargetBitrateBps),
4700 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004701 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004702
4703 // Insert a first video frame, causes another bitrate update.
4704 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4705 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4706 video_source_.IncomingCapturedFrame(
4707 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4708 WaitForEncodedFrame(timestamp_ms);
4709
4710 // Next, simulate video suspension due to pacer queue overrun.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004711 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
4712 DataRate::BitsPerSec(0),
4713 DataRate::BitsPerSec(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004714
4715 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004716 timestamp_ms += kProcessIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004717 fake_clock_.AdvanceTime(TimeDelta::Millis(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004718
4719 // Bitrate observer should not be called.
4720 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4721 video_source_.IncomingCapturedFrame(
4722 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4723 ExpectDroppedFrame();
4724
mflodmancc3d4422017-08-03 08:27:51 -07004725 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004726}
ilnik6b826ef2017-06-16 06:53:48 -07004727
Niels Möller4db138e2018-04-19 09:04:13 +02004728TEST_F(VideoStreamEncoderTest,
4729 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4730 const int kFrameWidth = 1280;
4731 const int kFrameHeight = 720;
4732 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004733 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004734 DataRate::BitsPerSec(kTargetBitrateBps),
4735 DataRate::BitsPerSec(kTargetBitrateBps),
4736 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004737 video_source_.IncomingCapturedFrame(
4738 CreateFrame(1, kFrameWidth, kFrameHeight));
4739 WaitForEncodedFrame(1);
4740 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4741 .low_encode_usage_threshold_percent,
4742 default_options.low_encode_usage_threshold_percent);
4743 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4744 .high_encode_usage_threshold_percent,
4745 default_options.high_encode_usage_threshold_percent);
4746 video_stream_encoder_->Stop();
4747}
4748
4749TEST_F(VideoStreamEncoderTest,
4750 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4751 const int kFrameWidth = 1280;
4752 const int kFrameHeight = 720;
4753 CpuOveruseOptions hardware_options;
4754 hardware_options.low_encode_usage_threshold_percent = 150;
4755 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004756 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004757
Erik Språng4c6ca302019-04-08 15:14:01 +02004758 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004759 DataRate::BitsPerSec(kTargetBitrateBps),
4760 DataRate::BitsPerSec(kTargetBitrateBps),
4761 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004762 video_source_.IncomingCapturedFrame(
4763 CreateFrame(1, kFrameWidth, kFrameHeight));
4764 WaitForEncodedFrame(1);
4765 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4766 .low_encode_usage_threshold_percent,
4767 hardware_options.low_encode_usage_threshold_percent);
4768 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4769 .high_encode_usage_threshold_percent,
4770 hardware_options.high_encode_usage_threshold_percent);
4771 video_stream_encoder_->Stop();
4772}
4773
Niels Möller6bb5ab92019-01-11 11:11:10 +01004774TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4775 const int kFrameWidth = 320;
4776 const int kFrameHeight = 240;
4777 const int kFps = 30;
4778 const int kTargetBitrateBps = 120000;
4779 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4780
Erik Språng4c6ca302019-04-08 15:14:01 +02004781 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004782 DataRate::BitsPerSec(kTargetBitrateBps),
4783 DataRate::BitsPerSec(kTargetBitrateBps),
4784 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004785
4786 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4787 max_framerate_ = kFps;
4788
4789 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4790 fake_encoder_.SimulateOvershoot(1.0);
4791 int num_dropped = 0;
4792 for (int i = 0; i < kNumFramesInRun; ++i) {
4793 video_source_.IncomingCapturedFrame(
4794 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4795 // Wait up to two frame durations for a frame to arrive.
4796 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4797 ++num_dropped;
4798 }
4799 timestamp_ms += 1000 / kFps;
4800 }
4801
Erik Språnga8d48ab2019-02-08 14:17:40 +01004802 // Framerate should be measured to be near the expected target rate.
4803 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4804
4805 // Frame drops should be within 5% of expected 0%.
4806 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004807
4808 // Make encoder produce frames at double the expected bitrate during 3 seconds
4809 // of video, verify number of drops. Rate needs to be slightly changed in
4810 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004811 double overshoot_factor = 2.0;
4812 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4813 // With bitrate adjuster, when need to overshoot even more to trigger
4814 // frame dropping.
4815 overshoot_factor *= 2;
4816 }
4817 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004818 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004819 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4820 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4821 DataRate::BitsPerSec(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004822 num_dropped = 0;
4823 for (int i = 0; i < kNumFramesInRun; ++i) {
4824 video_source_.IncomingCapturedFrame(
4825 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4826 // Wait up to two frame durations for a frame to arrive.
4827 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4828 ++num_dropped;
4829 }
4830 timestamp_ms += 1000 / kFps;
4831 }
4832
Erik Språng4c6ca302019-04-08 15:14:01 +02004833 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004834 DataRate::BitsPerSec(kTargetBitrateBps),
4835 DataRate::BitsPerSec(kTargetBitrateBps),
4836 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004837
4838 // Target framerate should be still be near the expected target, despite
4839 // the frame drops.
4840 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4841
4842 // Frame drops should be within 5% of expected 50%.
4843 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004844
4845 video_stream_encoder_->Stop();
4846}
4847
4848TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4849 const int kFrameWidth = 320;
4850 const int kFrameHeight = 240;
4851 const int kActualInputFps = 24;
4852 const int kTargetBitrateBps = 120000;
4853
4854 ASSERT_GT(max_framerate_, kActualInputFps);
4855
4856 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4857 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004858 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004859 DataRate::BitsPerSec(kTargetBitrateBps),
4860 DataRate::BitsPerSec(kTargetBitrateBps),
4861 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004862
4863 // Insert 3 seconds of video, with an input fps lower than configured max.
4864 for (int i = 0; i < kActualInputFps * 3; ++i) {
4865 video_source_.IncomingCapturedFrame(
4866 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4867 // Wait up to two frame durations for a frame to arrive.
4868 WaitForEncodedFrame(timestamp_ms);
4869 timestamp_ms += 1000 / kActualInputFps;
4870 }
4871
4872 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4873
4874 video_stream_encoder_->Stop();
4875}
4876
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004877TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4878 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004879 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004880 DataRate::BitsPerSec(kTargetBitrateBps),
4881 DataRate::BitsPerSec(kTargetBitrateBps),
4882 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004883
4884 fake_encoder_.BlockNextEncode();
4885 video_source_.IncomingCapturedFrame(
4886 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4887 WaitForEncodedFrame(1);
4888 // On the very first frame full update should be forced.
4889 rect = fake_encoder_.GetLastUpdateRect();
4890 EXPECT_EQ(rect.offset_x, 0);
4891 EXPECT_EQ(rect.offset_y, 0);
4892 EXPECT_EQ(rect.height, codec_height_);
4893 EXPECT_EQ(rect.width, codec_width_);
4894 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4895 // call to ContinueEncode.
4896 video_source_.IncomingCapturedFrame(
4897 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4898 ExpectDroppedFrame();
4899 video_source_.IncomingCapturedFrame(
4900 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4901 ExpectDroppedFrame();
4902 fake_encoder_.ContinueEncode();
4903 WaitForEncodedFrame(3);
4904 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4905 rect = fake_encoder_.GetLastUpdateRect();
4906 EXPECT_EQ(rect.offset_x, 1);
4907 EXPECT_EQ(rect.offset_y, 0);
4908 EXPECT_EQ(rect.width, 10);
4909 EXPECT_EQ(rect.height, 1);
4910
4911 video_source_.IncomingCapturedFrame(
4912 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4913 WaitForEncodedFrame(4);
4914 // Previous frame was encoded, so no accumulation should happen.
4915 rect = fake_encoder_.GetLastUpdateRect();
4916 EXPECT_EQ(rect.offset_x, 0);
4917 EXPECT_EQ(rect.offset_y, 0);
4918 EXPECT_EQ(rect.width, 1);
4919 EXPECT_EQ(rect.height, 1);
4920
4921 video_stream_encoder_->Stop();
4922}
4923
Erik Språngd7329ca2019-02-21 21:19:53 +01004924TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004925 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004926 DataRate::BitsPerSec(kTargetBitrateBps),
4927 DataRate::BitsPerSec(kTargetBitrateBps),
4928 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004929
4930 // First frame is always keyframe.
4931 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4932 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004933 EXPECT_THAT(
4934 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004935 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004936
4937 // Insert delta frame.
4938 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4939 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004940 EXPECT_THAT(
4941 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004942 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004943
4944 // Request next frame be a key-frame.
4945 video_stream_encoder_->SendKeyFrame();
4946 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4947 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004948 EXPECT_THAT(
4949 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004950 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004951
4952 video_stream_encoder_->Stop();
4953}
4954
4955TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4956 // Setup simulcast with three streams.
4957 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004958 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004959 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
4960 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
4961 DataRate::BitsPerSec(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004962 // Wait for all three layers before triggering event.
4963 sink_.SetNumExpectedLayers(3);
4964
4965 // First frame is always keyframe.
4966 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4967 WaitForEncodedFrame(1);
4968 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004969 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4970 VideoFrameType::kVideoFrameKey,
4971 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004972
4973 // Insert delta frame.
4974 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4975 WaitForEncodedFrame(2);
4976 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004977 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4978 VideoFrameType::kVideoFrameDelta,
4979 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004980
4981 // Request next frame be a key-frame.
4982 // Only first stream is configured to produce key-frame.
4983 video_stream_encoder_->SendKeyFrame();
4984 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4985 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004986
4987 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4988 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004989 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004990 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004991 VideoFrameType::kVideoFrameKey,
4992 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004993
4994 video_stream_encoder_->Stop();
4995}
4996
4997TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4998 // Configure internal source factory and setup test again.
4999 encoder_factory_.SetHasInternalSource(true);
5000 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005001 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005002 DataRate::BitsPerSec(kTargetBitrateBps),
5003 DataRate::BitsPerSec(kTargetBitrateBps),
5004 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01005005
5006 // Call encoder directly, simulating internal source where encoded frame
5007 // callback in VideoStreamEncoder is called despite no OnFrame().
5008 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
5009 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005010 EXPECT_THAT(
5011 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005012 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005013
Niels Möller8f7ce222019-03-21 15:43:58 +01005014 const std::vector<VideoFrameType> kDeltaFrame = {
5015 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01005016 // Need to set timestamp manually since manually for injected frame.
5017 VideoFrame frame = CreateFrame(101, nullptr);
5018 frame.set_timestamp(101);
5019 fake_encoder_.InjectFrame(frame, false);
5020 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005021 EXPECT_THAT(
5022 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005023 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005024
5025 // Request key-frame. The forces a dummy frame down into the encoder.
5026 fake_encoder_.ExpectNullFrame();
5027 video_stream_encoder_->SendKeyFrame();
5028 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005029 EXPECT_THAT(
5030 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005031 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005032
5033 video_stream_encoder_->Stop();
5034}
Erik Språngb7cb7b52019-02-26 15:52:33 +01005035
5036TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
5037 // Configure internal source factory and setup test again.
5038 encoder_factory_.SetHasInternalSource(true);
5039 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005040 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005041 DataRate::BitsPerSec(kTargetBitrateBps),
5042 DataRate::BitsPerSec(kTargetBitrateBps),
5043 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01005044
5045 int64_t timestamp = 1;
5046 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02005047 image.SetEncodedData(
5048 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01005049 image.capture_time_ms_ = ++timestamp;
5050 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
5051 const int64_t kEncodeFinishDelayMs = 10;
5052 image.timing_.encode_start_ms = timestamp;
5053 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
5054 fake_encoder_.InjectEncodedImage(image);
5055 // Wait for frame without incrementing clock.
5056 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5057 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
5058 // capture timestamp should be kEncodeFinishDelayMs in the past.
5059 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
5060 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
5061 kEncodeFinishDelayMs);
5062
5063 video_stream_encoder_->Stop();
5064}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02005065
5066TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
5067 // Configure internal source factory and setup test again.
5068 encoder_factory_.SetHasInternalSource(true);
5069 ResetEncoder("H264", 1, 1, 1, false);
5070
5071 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
5072 image._frameType = VideoFrameType::kVideoFrameKey;
5073
5074 CodecSpecificInfo codec_specific_info;
5075 codec_specific_info.codecType = kVideoCodecH264;
5076
5077 RTPFragmentationHeader fragmentation;
5078 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5079 fragmentation.fragmentationOffset[0] = 4;
5080 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
5081
5082 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5083 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5084
5085 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5086 testing::ElementsAreArray(optimal_sps));
5087 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5088 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5089 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5090 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5091
5092 video_stream_encoder_->Stop();
5093}
5094
5095TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
5096 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
5097 0x00, 0x00, 0x03, 0x03, 0xF4,
5098 0x05, 0x03, 0xC7, 0xC0};
5099
5100 // Configure internal source factory and setup test again.
5101 encoder_factory_.SetHasInternalSource(true);
5102 ResetEncoder("H264", 1, 1, 1, false);
5103
5104 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
5105 image._frameType = VideoFrameType::kVideoFrameKey;
5106
5107 CodecSpecificInfo codec_specific_info;
5108 codec_specific_info.codecType = kVideoCodecH264;
5109
5110 RTPFragmentationHeader fragmentation;
5111 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5112 fragmentation.fragmentationOffset[0] = 4;
5113 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
5114
5115 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5116 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5117
5118 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5119 testing::ElementsAreArray(optimal_sps));
5120 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5121 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5122 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5123 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5124
5125 video_stream_encoder_->Stop();
5126}
5127
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005128TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5129 const int kFrameWidth = 1280;
5130 const int kFrameHeight = 720;
5131 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5132
5133 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005134 DataRate::BitsPerSec(kTargetBitrateBps),
5135 DataRate::BitsPerSec(kTargetBitrateBps),
5136 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005137 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5138
5139 // Insert a first video frame. It should be dropped because of downscale in
5140 // resolution.
5141 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5142 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5143 frame.set_rotation(kVideoRotation_270);
5144 video_source_.IncomingCapturedFrame(frame);
5145
5146 ExpectDroppedFrame();
5147
5148 // Second frame is downscaled.
5149 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5150 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5151 frame.set_rotation(kVideoRotation_90);
5152 video_source_.IncomingCapturedFrame(frame);
5153
5154 WaitForEncodedFrame(timestamp_ms);
5155 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5156
5157 // Insert another frame, also downscaled.
5158 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5159 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5160 frame.set_rotation(kVideoRotation_180);
5161 video_source_.IncomingCapturedFrame(frame);
5162
5163 WaitForEncodedFrame(timestamp_ms);
5164 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5165
5166 video_stream_encoder_->Stop();
5167}
5168
Erik Språng5056af02019-09-02 15:53:11 +02005169TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5170 const int kFrameWidth = 320;
5171 const int kFrameHeight = 180;
5172
5173 // Initial rate.
5174 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005175 /*target_bitrate=*/DataRate::KilobitsPerSec(300),
5176 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(300),
5177 /*link_allocation=*/DataRate::KilobitsPerSec(300),
Erik Språng5056af02019-09-02 15:53:11 +02005178 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005179 /*rtt_ms=*/0,
5180 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005181
5182 // Insert a first video frame so that encoder gets configured.
5183 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5184 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5185 frame.set_rotation(kVideoRotation_270);
5186 video_source_.IncomingCapturedFrame(frame);
5187 WaitForEncodedFrame(timestamp_ms);
5188
5189 // Set a target rate below the minimum allowed by the codec settings.
5190 VideoCodec codec_config = fake_encoder_.codec_config();
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005191 DataRate min_rate = DataRate::KilobitsPerSec(codec_config.minBitrate);
5192 DataRate target_rate = min_rate - DataRate::KilobitsPerSec(1);
Erik Språng5056af02019-09-02 15:53:11 +02005193 video_stream_encoder_->OnBitrateUpdated(
5194 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005195 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005196 /*link_allocation=*/target_rate,
5197 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005198 /*rtt_ms=*/0,
5199 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005200 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5201
5202 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5203 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5204 ASSERT_TRUE(rate_settings.has_value());
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005205 DataRate allocation_sum =
5206 DataRate::BitsPerSec(rate_settings->bitrate.get_sum_bps());
Erik Språng5056af02019-09-02 15:53:11 +02005207 EXPECT_EQ(min_rate, allocation_sum);
5208 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5209
5210 video_stream_encoder_->Stop();
5211}
5212
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005213TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005214 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005215 DataRate::BitsPerSec(kTargetBitrateBps),
5216 DataRate::BitsPerSec(kTargetBitrateBps),
5217 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005218 // Capture a frame and wait for it to synchronize with the encoder thread.
5219 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5220 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5221 WaitForEncodedFrame(1);
5222
5223 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5224 ASSERT_TRUE(prev_rate_settings.has_value());
5225 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5226 kDefaultFramerate);
5227
5228 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5229 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5230 timestamp_ms += 1000 / kDefaultFramerate;
5231 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5232 WaitForEncodedFrame(timestamp_ms);
5233 }
5234 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5235 kDefaultFramerate);
5236 // Capture larger frame to trigger a reconfigure.
5237 codec_height_ *= 2;
5238 codec_width_ *= 2;
5239 timestamp_ms += 1000 / kDefaultFramerate;
5240 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5241 WaitForEncodedFrame(timestamp_ms);
5242
5243 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5244 auto current_rate_settings =
5245 fake_encoder_.GetAndResetLastRateControlSettings();
5246 // Ensure we have actually reconfigured twice
5247 // The rate settings should have been set again even though
5248 // they haven't changed.
5249 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005250 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005251
5252 video_stream_encoder_->Stop();
5253}
5254
philipeld9cc8c02019-09-16 14:53:40 +02005255struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5256 MOCK_METHOD0(RequestEncoderFallback, void());
5257 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
philipel9b058032020-02-10 11:30:00 +01005258 MOCK_METHOD1(RequestEncoderSwitch,
5259 void(const webrtc::SdpVideoFormat& format));
philipeld9cc8c02019-09-16 14:53:40 +02005260};
5261
5262TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5263 constexpr int kDontCare = 100;
5264
5265 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5266 video_send_config_.encoder_settings.encoder_switch_request_callback =
5267 &switch_callback;
5268 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5269 encoder_config.codec_type = kVideoCodecVP8;
5270 webrtc::test::ScopedFieldTrials field_trial(
5271 "WebRTC-NetworkCondition-EncoderSwitch/"
5272 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5273 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5274
5275 // Reset encoder for new configuration to take effect.
5276 ConfigureEncoder(std::move(encoder_config));
5277
5278 // Send one frame to trigger ReconfigureEncoder.
5279 video_source_.IncomingCapturedFrame(
5280 CreateFrame(kDontCare, kDontCare, kDontCare));
5281
5282 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005283 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5284 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005285 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005286 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005287
5288 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005289 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5290 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5291 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipeld9cc8c02019-09-16 14:53:40 +02005292 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005293 /*rtt_ms=*/0,
5294 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005295
5296 video_stream_encoder_->Stop();
5297}
5298
Mirta Dvornicic5ed40cf2020-02-21 16:35:51 +01005299TEST_F(VideoStreamEncoderTest, VideoSuspendedNoEncoderSwitch) {
5300 constexpr int kDontCare = 100;
5301
5302 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5303 video_send_config_.encoder_settings.encoder_switch_request_callback =
5304 &switch_callback;
5305 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5306 encoder_config.codec_type = kVideoCodecVP8;
5307 webrtc::test::ScopedFieldTrials field_trial(
5308 "WebRTC-NetworkCondition-EncoderSwitch/"
5309 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5310 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5311
5312 // Reset encoder for new configuration to take effect.
5313 ConfigureEncoder(std::move(encoder_config));
5314
5315 // Send one frame to trigger ReconfigureEncoder.
5316 video_source_.IncomingCapturedFrame(
5317 CreateFrame(kDontCare, kDontCare, kDontCare));
5318
5319 using Config = EncoderSwitchRequestCallback::Config;
5320 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(_)))
5321 .Times(0);
5322
5323 video_stream_encoder_->OnBitrateUpdated(
5324 /*target_bitrate=*/DataRate::KilobitsPerSec(0),
5325 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(0),
5326 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
5327 /*fraction_lost=*/0,
5328 /*rtt_ms=*/0,
5329 /*cwnd_reduce_ratio=*/0);
5330
5331 video_stream_encoder_->Stop();
5332}
5333
philipeld9cc8c02019-09-16 14:53:40 +02005334TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5335 constexpr int kSufficientBitrateToNotDrop = 1000;
5336 constexpr int kHighRes = 500;
5337 constexpr int kLowRes = 100;
5338
5339 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5340 video_send_config_.encoder_settings.encoder_switch_request_callback =
5341 &switch_callback;
5342 webrtc::test::ScopedFieldTrials field_trial(
5343 "WebRTC-NetworkCondition-EncoderSwitch/"
5344 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5345 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5346 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5347 encoder_config.codec_type = kVideoCodecH264;
5348
5349 // Reset encoder for new configuration to take effect.
5350 ConfigureEncoder(std::move(encoder_config));
5351
5352 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5353 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5354 // not fail.
5355 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005356 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5357 /*stable_target_bitrate=*/
5358 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5359 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipeld9cc8c02019-09-16 14:53:40 +02005360 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005361 /*rtt_ms=*/0,
5362 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005363
5364 // Send one frame to trigger ReconfigureEncoder.
5365 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5366 WaitForEncodedFrame(1);
5367
5368 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005369 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5370 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005371 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005372 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005373
5374 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5375 WaitForEncodedFrame(2);
5376
5377 video_stream_encoder_->Stop();
5378}
5379
philipel9b058032020-02-10 11:30:00 +01005380TEST_F(VideoStreamEncoderTest, EncoderSelectorCurrentEncoderIsSignaled) {
5381 constexpr int kDontCare = 100;
5382 StrictMock<MockEncoderSelector> encoder_selector;
5383 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5384 &fake_encoder_, &encoder_selector);
5385 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5386
5387 // Reset encoder for new configuration to take effect.
5388 ConfigureEncoder(video_encoder_config_.Copy());
5389
5390 EXPECT_CALL(encoder_selector, OnCurrentEncoder(_));
5391
5392 video_source_.IncomingCapturedFrame(
5393 CreateFrame(kDontCare, kDontCare, kDontCare));
5394 video_stream_encoder_->Stop();
5395
5396 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5397 // to it's factory, so in order for the encoder instance in the
5398 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5399 // reset the |video_stream_encoder_| here.
5400 video_stream_encoder_.reset();
5401}
5402
5403TEST_F(VideoStreamEncoderTest, EncoderSelectorBitrateSwitch) {
5404 constexpr int kDontCare = 100;
5405
5406 NiceMock<MockEncoderSelector> encoder_selector;
5407 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5408 video_send_config_.encoder_settings.encoder_switch_request_callback =
5409 &switch_callback;
5410 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5411 &fake_encoder_, &encoder_selector);
5412 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5413
5414 // Reset encoder for new configuration to take effect.
5415 ConfigureEncoder(video_encoder_config_.Copy());
5416
5417 ON_CALL(encoder_selector, OnEncodingBitrate(_))
5418 .WillByDefault(Return(SdpVideoFormat("AV1")));
5419 EXPECT_CALL(switch_callback,
5420 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(
5421 Field(&SdpVideoFormat::name, "AV1"))));
5422
5423 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005424 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5425 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5426 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipel9b058032020-02-10 11:30:00 +01005427 /*fraction_lost=*/0,
5428 /*rtt_ms=*/0,
5429 /*cwnd_reduce_ratio=*/0);
5430
5431 video_stream_encoder_->Stop();
5432}
5433
5434TEST_F(VideoStreamEncoderTest, EncoderSelectorBrokenEncoderSwitch) {
5435 constexpr int kSufficientBitrateToNotDrop = 1000;
5436 constexpr int kDontCare = 100;
5437
5438 NiceMock<MockVideoEncoder> video_encoder;
5439 NiceMock<MockEncoderSelector> encoder_selector;
5440 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5441 video_send_config_.encoder_settings.encoder_switch_request_callback =
5442 &switch_callback;
5443 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5444 &video_encoder, &encoder_selector);
5445 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5446
5447 // Reset encoder for new configuration to take effect.
5448 ConfigureEncoder(video_encoder_config_.Copy());
5449
5450 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5451 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5452 // not fail.
5453 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005454 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5455 /*stable_target_bitrate=*/
5456 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5457 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipel9b058032020-02-10 11:30:00 +01005458 /*fraction_lost=*/0,
5459 /*rtt_ms=*/0,
5460 /*cwnd_reduce_ratio=*/0);
5461
5462 ON_CALL(video_encoder, Encode(_, _))
5463 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ENCODER_FAILURE));
5464 ON_CALL(encoder_selector, OnEncoderBroken())
5465 .WillByDefault(Return(SdpVideoFormat("AV2")));
5466
5467 rtc::Event encode_attempted;
5468 EXPECT_CALL(switch_callback,
5469 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(_)))
5470 .WillOnce([&encode_attempted](const SdpVideoFormat& format) {
5471 EXPECT_EQ(format.name, "AV2");
5472 encode_attempted.Set();
5473 });
5474
5475 video_source_.IncomingCapturedFrame(CreateFrame(1, kDontCare, kDontCare));
5476 encode_attempted.Wait(3000);
5477
5478 video_stream_encoder_->Stop();
5479
5480 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5481 // to it's factory, so in order for the encoder instance in the
5482 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5483 // reset the |video_stream_encoder_| here.
5484 video_stream_encoder_.reset();
5485}
5486
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005487TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005488 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005489 const int kFrameWidth = 320;
5490 const int kFrameHeight = 180;
5491
5492 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005493 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005494 video_stream_encoder_->OnBitrateUpdated(
5495 /*target_bitrate=*/rate,
5496 /*stable_target_bitrate=*/rate,
5497 /*link_allocation=*/rate,
5498 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005499 /*rtt_ms=*/0,
5500 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005501
5502 // Insert a first video frame so that encoder gets configured.
5503 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5504 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5505 frame.set_rotation(kVideoRotation_270);
5506 video_source_.IncomingCapturedFrame(frame);
5507 WaitForEncodedFrame(timestamp_ms);
5508 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5509
5510 // Change of target bitrate propagates to the encoder.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005511 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005512 video_stream_encoder_->OnBitrateUpdated(
5513 /*target_bitrate=*/new_stable_rate,
5514 /*stable_target_bitrate=*/new_stable_rate,
5515 /*link_allocation=*/rate,
5516 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005517 /*rtt_ms=*/0,
5518 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005519 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5520 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5521 video_stream_encoder_->Stop();
5522}
5523
5524TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005525 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005526 const int kFrameWidth = 320;
5527 const int kFrameHeight = 180;
5528
5529 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005530 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005531 video_stream_encoder_->OnBitrateUpdated(
5532 /*target_bitrate=*/rate,
5533 /*stable_target_bitrate=*/rate,
5534 /*link_allocation=*/rate,
5535 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005536 /*rtt_ms=*/0,
5537 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005538
5539 // Insert a first video frame so that encoder gets configured.
5540 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5541 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5542 frame.set_rotation(kVideoRotation_270);
5543 video_source_.IncomingCapturedFrame(frame);
5544 WaitForEncodedFrame(timestamp_ms);
5545 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5546
5547 // Set a higher target rate without changing the link_allocation. Should not
5548 // reset encoder's rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005549 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005550 video_stream_encoder_->OnBitrateUpdated(
5551 /*target_bitrate=*/rate,
5552 /*stable_target_bitrate=*/new_stable_rate,
5553 /*link_allocation=*/rate,
5554 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005555 /*rtt_ms=*/0,
5556 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005557 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5558 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5559 video_stream_encoder_->Stop();
5560}
5561
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005562TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5563 test::ScopedFieldTrials field_trials(
5564 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5565 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5566 const int kFramerateFps = 30;
5567 const int kWidth = 1920;
5568 const int kHeight = 1080;
5569 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5570 // Works on screenshare mode.
5571 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5572 // We rely on the automatic resolution adaptation, but we handle framerate
5573 // adaptation manually by mocking the stats proxy.
5574 video_source_.set_adaptation_enabled(true);
5575
5576 // BALANCED degradation preference is required for this feature.
5577 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005578 DataRate::BitsPerSec(kTargetBitrateBps),
5579 DataRate::BitsPerSec(kTargetBitrateBps),
5580 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005581 video_stream_encoder_->SetSource(&video_source_,
5582 webrtc::DegradationPreference::BALANCED);
5583 VerifyNoLimitation(video_source_.sink_wants());
5584
5585 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5586 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5587
5588 // Pass enough frames with the full update to trigger animation detection.
5589 for (int i = 0; i < kNumFrames; ++i) {
5590 int64_t timestamp_ms =
5591 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5592 frame.set_ntp_time_ms(timestamp_ms);
5593 frame.set_timestamp_us(timestamp_ms * 1000);
5594 video_source_.IncomingCapturedFrame(frame);
5595 WaitForEncodedFrame(timestamp_ms);
5596 }
5597
5598 // Resolution should be limited.
5599 rtc::VideoSinkWants expected;
5600 expected.max_framerate_fps = kFramerateFps;
5601 expected.max_pixel_count = 1280 * 720 + 1;
5602 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5603
5604 // Pass one frame with no known update.
5605 // Resolution cap should be removed immediately.
5606 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5607 frame.set_ntp_time_ms(timestamp_ms);
5608 frame.set_timestamp_us(timestamp_ms * 1000);
5609 frame.clear_update_rect();
5610
5611 video_source_.IncomingCapturedFrame(frame);
5612 WaitForEncodedFrame(timestamp_ms);
5613
5614 // Resolution should be unlimited now.
5615 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5616
5617 video_stream_encoder_->Stop();
5618}
5619
perkj26091b12016-09-01 01:17:40 -07005620} // namespace webrtc