blob: 9d6925e9c702d894dc3134bb0d346eacd757f888 [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));
Mirta Dvornicic4f34d782020-02-26 13:01:19 +0100433 MOCK_METHOD1(OnAvailableBitrate,
philipel9b058032020-02-10 11:30:00 +0100434 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) {
Henrik Boström60383832020-02-28 09:03:53 +0100696 EXPECT_LE(7, fps_limit);
697 EXPECT_LE(fps_limit, 10);
Jonathan Yubc771b72017-12-08 17:04:29 -0800698 } else if (last_frame_pixels <= 480 * 270) {
Henrik Boström60383832020-02-28 09:03:53 +0100699 EXPECT_LE(10, fps_limit);
700 EXPECT_LE(fps_limit, 15);
Jonathan Yubc771b72017-12-08 17:04:29 -0800701 } else if (last_frame_pixels <= 640 * 480) {
702 EXPECT_LE(15, fps_limit);
703 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200704 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800705 }
706 }
707
sprang4847ae62017-06-27 07:06:52 -0700708 void WaitForEncodedFrame(int64_t expected_ntp_time) {
709 sink_.WaitForEncodedFrame(expected_ntp_time);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100710 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700711 }
712
713 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
714 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100715 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700716 return ok;
717 }
718
719 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
720 sink_.WaitForEncodedFrame(expected_width, expected_height);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100721 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700722 }
723
724 void ExpectDroppedFrame() {
725 sink_.ExpectDroppedFrame();
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100726 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700727 }
728
729 bool WaitForFrame(int64_t timeout_ms) {
730 bool ok = sink_.WaitForFrame(timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100731 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700732 return ok;
733 }
734
perkj26091b12016-09-01 01:17:40 -0700735 class TestEncoder : public test::FakeEncoder {
736 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100737 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700738
asaperssonfab67072017-04-04 05:51:49 -0700739 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800740 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700741 return config_;
742 }
743
744 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800745 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700746 block_next_encode_ = true;
747 }
748
Erik Språngaed30702018-11-05 12:57:17 +0100749 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800750 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100751 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100752 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100753 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100754 info.scaling_settings = VideoEncoder::ScalingSettings(
755 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100756 }
757 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100758 for (int i = 0; i < kMaxSpatialLayers; ++i) {
759 if (temporal_layers_supported_[i]) {
760 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
761 info.fps_allocation[i].resize(num_layers);
762 }
763 }
Erik Språngaed30702018-11-05 12:57:17 +0100764 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200765
766 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100767 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100768 return info;
kthelgason876222f2016-11-29 01:44:11 -0800769 }
770
Erik Språngb7cb7b52019-02-26 15:52:33 +0100771 int32_t RegisterEncodeCompleteCallback(
772 EncodedImageCallback* callback) override {
773 rtc::CritScope lock(&local_crit_sect_);
774 encoded_image_callback_ = callback;
775 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
776 }
777
perkjfa10b552016-10-02 23:45:26 -0700778 void ContinueEncode() { continue_encode_event_.Set(); }
779
780 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
781 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800782 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700783 EXPECT_EQ(timestamp_, timestamp);
784 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
785 }
786
kthelgason2fc52542017-03-03 00:24:41 -0800787 void SetQualityScaling(bool b) {
788 rtc::CritScope lock(&local_crit_sect_);
789 quality_scaling_ = b;
790 }
kthelgasonad9010c2017-02-14 00:46:51 -0800791
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100792 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
793 rtc::CritScope lock(&local_crit_sect_);
794 requested_resolution_alignment_ = requested_resolution_alignment;
795 }
796
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100797 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
798 rtc::CritScope lock(&local_crit_sect_);
799 is_hardware_accelerated_ = is_hardware_accelerated;
800 }
801
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100802 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
803 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
804 rtc::CritScope lock(&local_crit_sect_);
805 temporal_layers_supported_[spatial_idx] = supported;
806 }
807
Sergey Silkin6456e352019-07-08 17:56:40 +0200808 void SetResolutionBitrateLimits(
809 std::vector<ResolutionBitrateLimits> thresholds) {
810 rtc::CritScope cs(&local_crit_sect_);
811 resolution_bitrate_limits_ = thresholds;
812 }
813
sprangfe627f32017-03-29 08:24:59 -0700814 void ForceInitEncodeFailure(bool force_failure) {
815 rtc::CritScope lock(&local_crit_sect_);
816 force_init_encode_failed_ = force_failure;
817 }
818
Niels Möller6bb5ab92019-01-11 11:11:10 +0100819 void SimulateOvershoot(double rate_factor) {
820 rtc::CritScope lock(&local_crit_sect_);
821 rate_factor_ = rate_factor;
822 }
823
Erik Språngd7329ca2019-02-21 21:19:53 +0100824 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100825 rtc::CritScope lock(&local_crit_sect_);
826 return last_framerate_;
827 }
828
Erik Språngd7329ca2019-02-21 21:19:53 +0100829 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100830 rtc::CritScope lock(&local_crit_sect_);
831 return last_update_rect_;
832 }
833
Niels Möller87e2d782019-03-07 10:18:23 +0100834 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100835 rtc::CritScope lock(&local_crit_sect_);
836 return last_frame_types_;
837 }
838
839 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100840 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100841 keyframe ? VideoFrameType::kVideoFrameKey
842 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100843 {
844 rtc::CritScope lock(&local_crit_sect_);
845 last_frame_types_ = frame_type;
846 }
Niels Möllerb859b322019-03-07 12:40:01 +0100847 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100848 }
849
Erik Språngb7cb7b52019-02-26 15:52:33 +0100850 void InjectEncodedImage(const EncodedImage& image) {
851 rtc::CritScope lock(&local_crit_sect_);
852 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
853 }
854
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200855 void InjectEncodedImage(const EncodedImage& image,
856 const CodecSpecificInfo* codec_specific_info,
857 const RTPFragmentationHeader* fragmentation) {
858 rtc::CritScope lock(&local_crit_sect_);
859 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
860 fragmentation);
861 }
862
Erik Språngd7329ca2019-02-21 21:19:53 +0100863 void ExpectNullFrame() {
864 rtc::CritScope lock(&local_crit_sect_);
865 expect_null_frame_ = true;
866 }
867
Erik Språng5056af02019-09-02 15:53:11 +0200868 absl::optional<VideoEncoder::RateControlParameters>
869 GetAndResetLastRateControlSettings() {
870 auto settings = last_rate_control_settings_;
871 last_rate_control_settings_.reset();
872 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100873 }
874
Sergey Silkin5ee69672019-07-02 14:18:34 +0200875 int GetNumEncoderInitializations() const {
876 rtc::CritScope lock(&local_crit_sect_);
877 return num_encoder_initializations_;
878 }
879
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200880 int GetNumSetRates() const {
881 rtc::CritScope lock(&local_crit_sect_);
882 return num_set_rates_;
883 }
884
perkjfa10b552016-10-02 23:45:26 -0700885 private:
perkj26091b12016-09-01 01:17:40 -0700886 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100887 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700888 bool block_encode;
889 {
brandtre78d2662017-01-16 05:57:16 -0800890 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100891 if (expect_null_frame_) {
892 EXPECT_EQ(input_image.timestamp(), 0u);
893 EXPECT_EQ(input_image.width(), 1);
894 last_frame_types_ = *frame_types;
895 expect_null_frame_ = false;
896 } else {
897 EXPECT_GT(input_image.timestamp(), timestamp_);
898 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
899 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
900 }
perkj26091b12016-09-01 01:17:40 -0700901
902 timestamp_ = input_image.timestamp();
903 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700904 last_input_width_ = input_image.width();
905 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700906 block_encode = block_next_encode_;
907 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100908 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100909 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700910 }
Niels Möllerb859b322019-03-07 12:40:01 +0100911 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700912 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700913 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700914 return result;
915 }
916
sprangfe627f32017-03-29 08:24:59 -0700917 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200918 const Settings& settings) override {
919 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200920
sprangfe627f32017-03-29 08:24:59 -0700921 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100922 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200923
924 ++num_encoder_initializations_;
925
Erik Språng82fad3d2018-03-21 09:57:23 +0100926 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700927 // Simulate setting up temporal layers, in order to validate the life
928 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100929 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200930 frame_buffer_controller_ =
931 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700932 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100933 if (force_init_encode_failed_) {
934 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700935 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100936 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100937
Erik Språngb7cb7b52019-02-26 15:52:33 +0100938 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700939 return res;
940 }
941
Erik Språngb7cb7b52019-02-26 15:52:33 +0100942 int32_t Release() override {
943 rtc::CritScope lock(&local_crit_sect_);
944 EXPECT_NE(initialized_, EncoderState::kUninitialized);
945 initialized_ = EncoderState::kUninitialized;
946 return FakeEncoder::Release();
947 }
948
Erik Språng16cb8f52019-04-12 13:59:09 +0200949 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100950 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200951 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100952 VideoBitrateAllocation adjusted_rate_allocation;
953 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
954 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200955 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100956 adjusted_rate_allocation.SetBitrate(
957 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200958 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100959 rate_factor_));
960 }
961 }
962 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200963 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200964 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200965 RateControlParameters adjusted_paramters = parameters;
966 adjusted_paramters.bitrate = adjusted_rate_allocation;
967 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100968 }
969
brandtre78d2662017-01-16 05:57:16 -0800970 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100971 enum class EncoderState {
972 kUninitialized,
973 kInitializationFailed,
974 kInitialized
975 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
976 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700977 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700978 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700979 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
980 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
981 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
982 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
983 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100984 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100985 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100986 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700987 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100988 absl::optional<bool>
989 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
990 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700991 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100992 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
993 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200994 absl::optional<VideoEncoder::RateControlParameters>
995 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100996 VideoFrame::UpdateRect last_update_rect_
997 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100998 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100999 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001000 EncodedImageCallback* encoded_image_callback_
1001 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +02001002 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001003 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001004 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
1005 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02001006 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -07001007 };
1008
mflodmancc3d4422017-08-03 08:27:51 -07001009 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -07001010 public:
1011 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +01001012 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -07001013
perkj26091b12016-09-01 01:17:40 -07001014 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -07001015 EXPECT_TRUE(
1016 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
1017 }
1018
1019 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
1020 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -07001021 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -07001022 if (!encoded_frame_event_.Wait(timeout_ms))
1023 return false;
perkj26091b12016-09-01 01:17:40 -07001024 {
1025 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -08001026 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -07001027 }
1028 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -07001029 return true;
perkj26091b12016-09-01 01:17:40 -07001030 }
1031
sprangb1ca0732017-02-01 08:38:12 -08001032 void WaitForEncodedFrame(uint32_t expected_width,
1033 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -07001034 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001035 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -07001036 }
1037
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001038 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -07001039 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -08001040 uint32_t width = 0;
1041 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -08001042 {
1043 rtc::CritScope lock(&crit_);
1044 width = last_width_;
1045 height = last_height_;
1046 }
1047 EXPECT_EQ(expected_height, height);
1048 EXPECT_EQ(expected_width, width);
1049 }
1050
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001051 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
1052 int width = 0;
1053 int height = 0;
1054 {
1055 rtc::CritScope lock(&crit_);
1056 width = last_width_;
1057 height = last_height_;
1058 }
1059 EXPECT_EQ(width % resolution_alignment, 0);
1060 EXPECT_EQ(height % resolution_alignment, 0);
1061 }
1062
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001063 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1064 VideoRotation rotation;
1065 {
1066 rtc::CritScope lock(&crit_);
1067 rotation = last_rotation_;
1068 }
1069 EXPECT_EQ(expected_rotation, rotation);
1070 }
1071
kthelgason2fc52542017-03-03 00:24:41 -08001072 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001073
sprangc5d62e22017-04-02 23:53:04 -07001074 bool WaitForFrame(int64_t timeout_ms) {
1075 return encoded_frame_event_.Wait(timeout_ms);
1076 }
1077
perkj26091b12016-09-01 01:17:40 -07001078 void SetExpectNoFrames() {
1079 rtc::CritScope lock(&crit_);
1080 expect_frames_ = false;
1081 }
1082
asaperssonfab67072017-04-04 05:51:49 -07001083 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001084 rtc::CritScope lock(&crit_);
1085 return number_of_reconfigurations_;
1086 }
1087
asaperssonfab67072017-04-04 05:51:49 -07001088 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001089 rtc::CritScope lock(&crit_);
1090 return min_transmit_bitrate_bps_;
1091 }
1092
Erik Språngd7329ca2019-02-21 21:19:53 +01001093 void SetNumExpectedLayers(size_t num_layers) {
1094 rtc::CritScope lock(&crit_);
1095 num_expected_layers_ = num_layers;
1096 }
1097
Erik Språngb7cb7b52019-02-26 15:52:33 +01001098 int64_t GetLastCaptureTimeMs() const {
1099 rtc::CritScope lock(&crit_);
1100 return last_capture_time_ms_;
1101 }
1102
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001103 std::vector<uint8_t> GetLastEncodedImageData() {
1104 rtc::CritScope lock(&crit_);
1105 return std::move(last_encoded_image_data_);
1106 }
1107
1108 RTPFragmentationHeader GetLastFragmentation() {
1109 rtc::CritScope lock(&crit_);
1110 return std::move(last_fragmentation_);
1111 }
1112
perkj26091b12016-09-01 01:17:40 -07001113 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001114 Result OnEncodedImage(
1115 const EncodedImage& encoded_image,
1116 const CodecSpecificInfo* codec_specific_info,
1117 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001118 rtc::CritScope lock(&crit_);
1119 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001120 last_encoded_image_data_ = std::vector<uint8_t>(
1121 encoded_image.data(), encoded_image.data() + encoded_image.size());
1122 if (fragmentation) {
1123 last_fragmentation_.CopyFrom(*fragmentation);
1124 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001125 uint32_t timestamp = encoded_image.Timestamp();
1126 if (last_timestamp_ != timestamp) {
1127 num_received_layers_ = 1;
1128 } else {
1129 ++num_received_layers_;
1130 }
1131 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001132 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001133 last_width_ = encoded_image._encodedWidth;
1134 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001135 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001136 if (num_received_layers_ == num_expected_layers_) {
1137 encoded_frame_event_.Set();
1138 }
sprangb1ca0732017-02-01 08:38:12 -08001139 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001140 }
1141
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001142 void OnEncoderConfigurationChanged(
1143 std::vector<VideoStream> streams,
1144 VideoEncoderConfig::ContentType content_type,
1145 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001146 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001147 ++number_of_reconfigurations_;
1148 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1149 }
1150
perkj26091b12016-09-01 01:17:40 -07001151 rtc::CriticalSection crit_;
1152 TestEncoder* test_encoder_;
1153 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001154 std::vector<uint8_t> last_encoded_image_data_;
1155 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001156 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001157 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001158 uint32_t last_height_ = 0;
1159 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001160 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001161 size_t num_expected_layers_ = 1;
1162 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001163 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001164 int number_of_reconfigurations_ = 0;
1165 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001166 };
1167
Sergey Silkin5ee69672019-07-02 14:18:34 +02001168 class VideoBitrateAllocatorProxyFactory
1169 : public VideoBitrateAllocatorFactory {
1170 public:
1171 VideoBitrateAllocatorProxyFactory()
1172 : bitrate_allocator_factory_(
1173 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1174
1175 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1176 const VideoCodec& codec) override {
1177 rtc::CritScope lock(&crit_);
1178 codec_config_ = codec;
1179 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1180 }
1181
1182 VideoCodec codec_config() const {
1183 rtc::CritScope lock(&crit_);
1184 return codec_config_;
1185 }
1186
1187 private:
1188 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1189
1190 rtc::CriticalSection crit_;
1191 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1192 };
1193
perkj26091b12016-09-01 01:17:40 -07001194 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001195 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001196 int codec_width_;
1197 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001198 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001199 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001200 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001201 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001202 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001203 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001204 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001205 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001206 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001207 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001208};
1209
mflodmancc3d4422017-08-03 08:27:51 -07001210TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001211 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001212 DataRate::BitsPerSec(kTargetBitrateBps),
1213 DataRate::BitsPerSec(kTargetBitrateBps),
1214 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001215 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001216 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001217 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001218 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001219 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001220}
1221
mflodmancc3d4422017-08-03 08:27:51 -07001222TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001223 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001224 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001225 // The encoder will cache up to one frame for a short duration. Adding two
1226 // frames means that the first frame will be dropped and the second frame will
1227 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001228 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001229 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001230 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001231
Erik Språng4c6ca302019-04-08 15:14:01 +02001232 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001233 DataRate::BitsPerSec(kTargetBitrateBps),
1234 DataRate::BitsPerSec(kTargetBitrateBps),
1235 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001236
Sebastian Janssona3177052018-04-10 13:05:49 +02001237 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001238 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001239 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1240
1241 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001242 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001243}
1244
mflodmancc3d4422017-08-03 08:27:51 -07001245TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001246 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001247 DataRate::BitsPerSec(kTargetBitrateBps),
1248 DataRate::BitsPerSec(kTargetBitrateBps),
1249 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001250 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001251 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001252
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001253 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
1254 DataRate::BitsPerSec(0),
1255 DataRate::BitsPerSec(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001256 // The encoder will cache up to one frame for a short duration. Adding two
1257 // frames means that the first frame will be dropped and the second frame will
1258 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001259 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001260 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001261
Erik Språng4c6ca302019-04-08 15:14:01 +02001262 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001263 DataRate::BitsPerSec(kTargetBitrateBps),
1264 DataRate::BitsPerSec(kTargetBitrateBps),
1265 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001266 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001267 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1268 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001269 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001270}
1271
mflodmancc3d4422017-08-03 08:27:51 -07001272TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001273 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001274 DataRate::BitsPerSec(kTargetBitrateBps),
1275 DataRate::BitsPerSec(kTargetBitrateBps),
1276 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001277 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001278 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001279
1280 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001281 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001282
perkja49cbd32016-09-16 07:53:41 -07001283 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001284 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001285 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001286}
1287
mflodmancc3d4422017-08-03 08:27:51 -07001288TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001289 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001290 DataRate::BitsPerSec(kTargetBitrateBps),
1291 DataRate::BitsPerSec(kTargetBitrateBps),
1292 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001293
perkja49cbd32016-09-16 07:53:41 -07001294 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001295 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001296
mflodmancc3d4422017-08-03 08:27:51 -07001297 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001298 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001299 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001300 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1301 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001302}
1303
mflodmancc3d4422017-08-03 08:27:51 -07001304TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001305 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001306 DataRate::BitsPerSec(kTargetBitrateBps),
1307 DataRate::BitsPerSec(kTargetBitrateBps),
1308 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001309
1310 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001311 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001312 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001313 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1314 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001315 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1316 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001317 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001318 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001319
mflodmancc3d4422017-08-03 08:27:51 -07001320 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001321}
1322
Noah Richards51db4212019-06-12 06:59:12 -07001323TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1324 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001325 DataRate::BitsPerSec(kTargetBitrateBps),
1326 DataRate::BitsPerSec(kTargetBitrateBps),
1327 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001328
1329 rtc::Event frame_destroyed_event;
1330 video_source_.IncomingCapturedFrame(
1331 CreateFakeNativeFrame(1, &frame_destroyed_event));
1332 ExpectDroppedFrame();
1333 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1334 video_stream_encoder_->Stop();
1335}
1336
1337TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1338 // Use the cropping factory.
1339 video_encoder_config_.video_stream_factory =
1340 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1341 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1342 kMaxPayloadLength);
1343 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1344
1345 // Capture a frame at codec_width_/codec_height_.
1346 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001347 DataRate::BitsPerSec(kTargetBitrateBps),
1348 DataRate::BitsPerSec(kTargetBitrateBps),
1349 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001350 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1351 WaitForEncodedFrame(1);
1352 // The encoder will have been configured once.
1353 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1354 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1355 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1356
1357 // Now send in a fake frame that needs to be cropped as the width/height
1358 // aren't divisible by 4 (see CreateEncoderStreams above).
1359 rtc::Event frame_destroyed_event;
1360 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1361 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1362 ExpectDroppedFrame();
1363 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1364 video_stream_encoder_->Stop();
1365}
1366
Ying Wang9b881ab2020-02-07 14:29:32 +01001367TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1368 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001369 DataRate::BitsPerSec(kTargetBitrateBps),
1370 DataRate::BitsPerSec(kTargetBitrateBps),
1371 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ying Wang9b881ab2020-02-07 14:29:32 +01001372 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1373 WaitForEncodedFrame(1);
1374
1375 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001376 DataRate::BitsPerSec(kTargetBitrateBps),
1377 DataRate::BitsPerSec(kTargetBitrateBps),
1378 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0.5);
Ying Wang9b881ab2020-02-07 14:29:32 +01001379 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1380 // frames. Adding two frames means that the first frame will be dropped and
1381 // the second frame will be sent to the encoder.
1382 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1383 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1384 WaitForEncodedFrame(3);
1385 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1386 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1387 WaitForEncodedFrame(5);
1388 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1389 video_stream_encoder_->Stop();
1390}
1391
mflodmancc3d4422017-08-03 08:27:51 -07001392TEST_F(VideoStreamEncoderTest,
1393 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001394 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001395 DataRate::BitsPerSec(kTargetBitrateBps),
1396 DataRate::BitsPerSec(kTargetBitrateBps),
1397 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001398 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001399
1400 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001401 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001402 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001403 // The encoder will have been configured once when the first frame is
1404 // received.
1405 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001406
1407 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001408 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001409 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001410 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001411 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001412
1413 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001414 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001415 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001416 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001417 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001418
mflodmancc3d4422017-08-03 08:27:51 -07001419 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001420}
1421
mflodmancc3d4422017-08-03 08:27:51 -07001422TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001423 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001424 DataRate::BitsPerSec(kTargetBitrateBps),
1425 DataRate::BitsPerSec(kTargetBitrateBps),
1426 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001427
1428 // Capture a frame and wait for it to synchronize with the encoder thread.
1429 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001430 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001431 // The encoder will have been configured once.
1432 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001433 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1434 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1435
1436 codec_width_ *= 2;
1437 codec_height_ *= 2;
1438 // Capture a frame with a higher resolution and wait for it to synchronize
1439 // with the encoder thread.
1440 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001441 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001442 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1443 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001444 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001445
mflodmancc3d4422017-08-03 08:27:51 -07001446 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001447}
1448
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001449TEST_F(VideoStreamEncoderTest,
1450 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1451 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001452 DataRate::BitsPerSec(kTargetBitrateBps),
1453 DataRate::BitsPerSec(kTargetBitrateBps),
1454 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001455
1456 // Capture a frame and wait for it to synchronize with the encoder thread.
1457 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1458 WaitForEncodedFrame(1);
1459
1460 VideoEncoderConfig video_encoder_config;
1461 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1462 // Changing the max payload data length recreates encoder.
1463 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1464 kMaxPayloadLength / 2);
1465
1466 // Capture a frame and wait for it to synchronize with the encoder thread.
1467 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1468 WaitForEncodedFrame(2);
1469 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1470
1471 video_stream_encoder_->Stop();
1472}
1473
Sergey Silkin5ee69672019-07-02 14:18:34 +02001474TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1475 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001476 DataRate::BitsPerSec(kTargetBitrateBps),
1477 DataRate::BitsPerSec(kTargetBitrateBps),
1478 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001479
1480 VideoEncoderConfig video_encoder_config;
1481 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1482 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1483 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1484 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1485 kMaxPayloadLength);
1486
1487 // Capture a frame and wait for it to synchronize with the encoder thread.
1488 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1489 WaitForEncodedFrame(1);
1490 // The encoder will have been configured once when the first frame is
1491 // received.
1492 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1493 EXPECT_EQ(kTargetBitrateBps,
1494 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1495 EXPECT_EQ(kStartBitrateBps,
1496 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1497
Sergey Silkin6456e352019-07-08 17:56:40 +02001498 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1499 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001500 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1501 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1502 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1503 kMaxPayloadLength);
1504
1505 // Capture a frame and wait for it to synchronize with the encoder thread.
1506 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1507 WaitForEncodedFrame(2);
1508 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1509 // Bitrate limits have changed - rate allocator should be reconfigured,
1510 // encoder should not be reconfigured.
1511 EXPECT_EQ(kTargetBitrateBps * 2,
1512 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1513 EXPECT_EQ(kStartBitrateBps * 2,
1514 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1515 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1516
1517 video_stream_encoder_->Stop();
1518}
1519
Sergey Silkin6456e352019-07-08 17:56:40 +02001520TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001521 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001522 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001523 DataRate::BitsPerSec(kTargetBitrateBps),
1524 DataRate::BitsPerSec(kTargetBitrateBps),
1525 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001526
Sergey Silkincd02eba2020-01-20 14:48:40 +01001527 const uint32_t kMinEncBitrateKbps = 100;
1528 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001529 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001530 /*frame_size_pixels=*/codec_width_ * codec_height_,
1531 /*min_start_bitrate_bps=*/0,
1532 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1533 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001534 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1535
Sergey Silkincd02eba2020-01-20 14:48:40 +01001536 VideoEncoderConfig video_encoder_config;
1537 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1538 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1539 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1540 (kMinEncBitrateKbps + 1) * 1000;
1541 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1542 kMaxPayloadLength);
1543
1544 // When both encoder and app provide bitrate limits, the intersection of
1545 // provided sets should be used.
1546 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1547 WaitForEncodedFrame(1);
1548 EXPECT_EQ(kMaxEncBitrateKbps,
1549 bitrate_allocator_factory_.codec_config().maxBitrate);
1550 EXPECT_EQ(kMinEncBitrateKbps + 1,
1551 bitrate_allocator_factory_.codec_config().minBitrate);
1552
1553 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1554 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1555 (kMinEncBitrateKbps - 1) * 1000;
1556 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1557 kMaxPayloadLength);
1558 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001559 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001560 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001561 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001562 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001563 bitrate_allocator_factory_.codec_config().minBitrate);
1564
Sergey Silkincd02eba2020-01-20 14:48:40 +01001565 video_stream_encoder_->Stop();
1566}
1567
1568TEST_F(VideoStreamEncoderTest,
1569 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1570 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001571 DataRate::BitsPerSec(kTargetBitrateBps),
1572 DataRate::BitsPerSec(kTargetBitrateBps),
1573 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001574
1575 const uint32_t kMinAppBitrateKbps = 100;
1576 const uint32_t kMaxAppBitrateKbps = 200;
1577 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1578 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1579 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1580 /*frame_size_pixels=*/codec_width_ * codec_height_,
1581 /*min_start_bitrate_bps=*/0,
1582 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1583 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1584 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1585
1586 VideoEncoderConfig video_encoder_config;
1587 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1588 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1589 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1590 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001591 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1592 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001593
Sergey Silkincd02eba2020-01-20 14:48:40 +01001594 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1595 WaitForEncodedFrame(1);
1596 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001597 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001598 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001599 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001600
1601 video_stream_encoder_->Stop();
1602}
1603
1604TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001605 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001606 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001607 DataRate::BitsPerSec(kTargetBitrateBps),
1608 DataRate::BitsPerSec(kTargetBitrateBps),
1609 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001610
1611 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001612 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001613 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001614 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001615 fake_encoder_.SetResolutionBitrateLimits(
1616 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1617
1618 VideoEncoderConfig video_encoder_config;
1619 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1620 video_encoder_config.max_bitrate_bps = 0;
1621 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1622 kMaxPayloadLength);
1623
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001624 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001625 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1626 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001627 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1628 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001629 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1630 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1631
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001632 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001633 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1634 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001635 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1636 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001637 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1638 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1639
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001640 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001641 // encoder for 360p should be used.
1642 video_source_.IncomingCapturedFrame(
1643 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1644 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001645 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1646 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001647 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1648 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1649
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001650 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001651 // ignored.
1652 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1653 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001654 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1655 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001656 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1657 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001658 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1659 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001660 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1661 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1662
1663 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1664 // for 270p should be used.
1665 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1666 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001667 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1668 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001669 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1670 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1671
1672 video_stream_encoder_->Stop();
1673}
1674
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001675TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1676 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001677 DataRate::BitsPerSec(kTargetBitrateBps),
1678 DataRate::BitsPerSec(kTargetBitrateBps),
1679 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001680
1681 VideoEncoderConfig video_encoder_config;
1682 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1683 video_encoder_config.max_bitrate_bps = 0;
1684 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1685 kMaxPayloadLength);
1686
1687 // Encode 720p frame to get the default encoder target bitrate.
1688 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1689 WaitForEncodedFrame(1);
1690 const uint32_t kDefaultTargetBitrateFor720pKbps =
1691 bitrate_allocator_factory_.codec_config()
1692 .simulcastStream[0]
1693 .targetBitrate;
1694
1695 // Set the max recommended encoder bitrate to something lower than the default
1696 // target bitrate.
1697 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1698 1280 * 720, 10 * 1000, 10 * 1000,
1699 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1700 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1701
1702 // Change resolution to trigger encoder reinitialization.
1703 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1704 WaitForEncodedFrame(2);
1705 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1706 WaitForEncodedFrame(3);
1707
1708 // Ensure the target bitrate is capped by the max bitrate.
1709 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1710 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1711 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1712 .simulcastStream[0]
1713 .targetBitrate *
1714 1000,
1715 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1716
1717 video_stream_encoder_->Stop();
1718}
1719
mflodmancc3d4422017-08-03 08:27:51 -07001720TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001721 EXPECT_TRUE(video_source_.has_sinks());
1722 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001723 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001724 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001725 EXPECT_FALSE(video_source_.has_sinks());
1726 EXPECT_TRUE(new_video_source.has_sinks());
1727
mflodmancc3d4422017-08-03 08:27:51 -07001728 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001729}
1730
mflodmancc3d4422017-08-03 08:27:51 -07001731TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001732 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001733 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001734 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001735 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001736}
1737
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001738TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1739 constexpr int kRequestedResolutionAlignment = 7;
1740 video_source_.set_adaptation_enabled(true);
1741 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1742 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001743 DataRate::BitsPerSec(kTargetBitrateBps),
1744 DataRate::BitsPerSec(kTargetBitrateBps),
1745 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001746
1747 // On the 1st frame, we should have initialized the encoder and
1748 // asked for its resolution requirements.
1749 video_source_.IncomingCapturedFrame(
1750 CreateFrame(1, codec_width_, codec_height_));
1751 WaitForEncodedFrame(1);
1752 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1753 kRequestedResolutionAlignment);
1754
1755 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1756 // (It's up the to the encoder to potentially drop the previous frame,
1757 // to avoid coding back-to-back keyframes.)
1758 video_source_.IncomingCapturedFrame(
1759 CreateFrame(2, codec_width_, codec_height_));
1760 WaitForEncodedFrame(2);
1761 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1762
1763 video_stream_encoder_->Stop();
1764}
1765
Jonathan Yubc771b72017-12-08 17:04:29 -08001766TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1767 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001768 const int kWidth = 1280;
1769 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001770
1771 // We rely on the automatic resolution adaptation, but we handle framerate
1772 // adaptation manually by mocking the stats proxy.
1773 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001774
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001775 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001776 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001777 DataRate::BitsPerSec(kTargetBitrateBps),
1778 DataRate::BitsPerSec(kTargetBitrateBps),
1779 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001780 video_stream_encoder_->SetSource(&video_source_,
1781 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001782 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001783 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001784 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001785 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1786
Jonathan Yubc771b72017-12-08 17:04:29 -08001787 // Adapt down as far as possible.
1788 rtc::VideoSinkWants last_wants;
1789 int64_t t = 1;
1790 int loop_count = 0;
1791 do {
1792 ++loop_count;
1793 last_wants = video_source_.sink_wants();
1794
1795 // Simulate the framerate we've been asked to adapt to.
1796 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1797 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1798 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1799 mock_stats.input_frame_rate = fps;
1800 stats_proxy_->SetMockStats(mock_stats);
1801
1802 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1803 sink_.WaitForEncodedFrame(t);
1804 t += frame_interval_ms;
1805
mflodmancc3d4422017-08-03 08:27:51 -07001806 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001807 VerifyBalancedModeFpsRange(
1808 video_source_.sink_wants(),
1809 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1810 } while (video_source_.sink_wants().max_pixel_count <
1811 last_wants.max_pixel_count ||
1812 video_source_.sink_wants().max_framerate_fps <
1813 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001814
Jonathan Yubc771b72017-12-08 17:04:29 -08001815 // Verify that we've adapted all the way down.
1816 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001817 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001818 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1819 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001820 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001821 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1822 *video_source_.last_sent_height());
1823 EXPECT_EQ(kMinBalancedFramerateFps,
1824 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001825
Jonathan Yubc771b72017-12-08 17:04:29 -08001826 // Adapt back up the same number of times we adapted down.
1827 for (int i = 0; i < loop_count - 1; ++i) {
1828 last_wants = video_source_.sink_wants();
1829
1830 // Simulate the framerate we've been asked to adapt to.
1831 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1832 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1833 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1834 mock_stats.input_frame_rate = fps;
1835 stats_proxy_->SetMockStats(mock_stats);
1836
1837 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1838 sink_.WaitForEncodedFrame(t);
1839 t += frame_interval_ms;
1840
mflodmancc3d4422017-08-03 08:27:51 -07001841 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001842 VerifyBalancedModeFpsRange(
1843 video_source_.sink_wants(),
1844 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1845 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1846 last_wants.max_pixel_count ||
1847 video_source_.sink_wants().max_framerate_fps >
1848 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001849 }
1850
Åsa Persson8c1bf952018-09-13 10:42:19 +02001851 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001852 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001853 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001854 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1855 EXPECT_EQ((loop_count - 1) * 2,
1856 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001857
mflodmancc3d4422017-08-03 08:27:51 -07001858 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001859}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001860
mflodmancc3d4422017-08-03 08:27:51 -07001861TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001862 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001863 DataRate::BitsPerSec(kTargetBitrateBps),
1864 DataRate::BitsPerSec(kTargetBitrateBps),
1865 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001866 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001867
sprangc5d62e22017-04-02 23:53:04 -07001868 const int kFrameWidth = 1280;
1869 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001870
Åsa Persson8c1bf952018-09-13 10:42:19 +02001871 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001872
kthelgason5e13d412016-12-01 03:59:51 -08001873 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001874 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001875 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001876 frame_timestamp += kFrameIntervalMs;
1877
perkj803d97f2016-11-01 11:45:46 -07001878 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001879 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001880 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001881 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001882 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001883 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001884
asapersson0944a802017-04-07 00:57:58 -07001885 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001886 // wanted resolution.
1887 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1888 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1889 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001890 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001891
1892 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001893 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001894 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001895 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001896 // Give the encoder queue time to process the change in degradation preference
1897 // by waiting for an encoded frame.
1898 new_video_source.IncomingCapturedFrame(
1899 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1900 sink_.WaitForEncodedFrame(frame_timestamp);
1901 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001902 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001903 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001904
sprangc5d62e22017-04-02 23:53:04 -07001905 // Force an input frame rate to be available, or the adaptation call won't
1906 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001907 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001908 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001909 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001910 stats_proxy_->SetMockStats(stats);
1911
mflodmancc3d4422017-08-03 08:27:51 -07001912 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001913 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001914 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001915 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001916 frame_timestamp += kFrameIntervalMs;
1917
1918 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001919 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001920 EXPECT_EQ(std::numeric_limits<int>::max(),
1921 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001922 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001923
asapersson02465b82017-04-10 01:12:52 -07001924 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001925 video_stream_encoder_->SetSource(&new_video_source,
1926 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001927 // Give the encoder queue time to process the change in degradation preference
1928 // by waiting for an encoded frame.
1929 new_video_source.IncomingCapturedFrame(
1930 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1931 sink_.WaitForEncodedFrame(frame_timestamp);
1932 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001933 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001934
mflodmancc3d4422017-08-03 08:27:51 -07001935 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001936 new_video_source.IncomingCapturedFrame(
1937 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001938 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001939 frame_timestamp += kFrameIntervalMs;
1940
1941 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001942 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001943
1944 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001945 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001946 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001947 // Give the encoder queue time to process the change in degradation preference
1948 // by waiting for an encoded frame.
1949 new_video_source.IncomingCapturedFrame(
1950 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1951 sink_.WaitForEncodedFrame(frame_timestamp);
1952 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001953 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1954 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001955 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001956 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001957
1958 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001959 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001960 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001961 // Give the encoder queue time to process the change in degradation preference
1962 // by waiting for an encoded frame.
1963 new_video_source.IncomingCapturedFrame(
1964 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1965 sink_.WaitForEncodedFrame(frame_timestamp);
1966 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001967 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1968 EXPECT_EQ(std::numeric_limits<int>::max(),
1969 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001970 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001971
mflodmancc3d4422017-08-03 08:27:51 -07001972 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001973}
1974
mflodmancc3d4422017-08-03 08:27:51 -07001975TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001976 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001977 DataRate::BitsPerSec(kTargetBitrateBps),
1978 DataRate::BitsPerSec(kTargetBitrateBps),
1979 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001980
asaperssonfab67072017-04-04 05:51:49 -07001981 const int kWidth = 1280;
1982 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001983 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001984 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001985 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1986 EXPECT_FALSE(stats.bw_limited_resolution);
1987 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1988
1989 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001990 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001991 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001992 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001993
1994 stats = stats_proxy_->GetStats();
1995 EXPECT_TRUE(stats.bw_limited_resolution);
1996 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1997
1998 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001999 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002000 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002001 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07002002
2003 stats = stats_proxy_->GetStats();
2004 EXPECT_FALSE(stats.bw_limited_resolution);
2005 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
2006 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2007
mflodmancc3d4422017-08-03 08:27:51 -07002008 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07002009}
2010
mflodmancc3d4422017-08-03 08:27:51 -07002011TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002012 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002013 DataRate::BitsPerSec(kTargetBitrateBps),
2014 DataRate::BitsPerSec(kTargetBitrateBps),
2015 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07002016
2017 const int kWidth = 1280;
2018 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07002019 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002020 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07002021 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2022 EXPECT_FALSE(stats.cpu_limited_resolution);
2023 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2024
2025 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002026 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002027 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002028 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07002029
2030 stats = stats_proxy_->GetStats();
2031 EXPECT_TRUE(stats.cpu_limited_resolution);
2032 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2033
2034 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002035 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002036 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002037 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07002038
2039 stats = stats_proxy_->GetStats();
2040 EXPECT_FALSE(stats.cpu_limited_resolution);
2041 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07002042 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002043
mflodmancc3d4422017-08-03 08:27:51 -07002044 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002045}
2046
mflodmancc3d4422017-08-03 08:27:51 -07002047TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002048 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002049 DataRate::BitsPerSec(kTargetBitrateBps),
2050 DataRate::BitsPerSec(kTargetBitrateBps),
2051 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002052
asaperssonfab67072017-04-04 05:51:49 -07002053 const int kWidth = 1280;
2054 const int kHeight = 720;
2055 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002056 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002057 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002058 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002059 EXPECT_FALSE(stats.cpu_limited_resolution);
2060 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2061
asaperssonfab67072017-04-04 05:51:49 -07002062 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002063 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002064 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002065 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002066 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002067 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002068 EXPECT_TRUE(stats.cpu_limited_resolution);
2069 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2070
2071 // Set new source with adaptation still enabled.
2072 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002073 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002074 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002075
asaperssonfab67072017-04-04 05:51:49 -07002076 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002077 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002078 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002079 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002080 EXPECT_TRUE(stats.cpu_limited_resolution);
2081 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2082
2083 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002084 video_stream_encoder_->SetSource(&new_video_source,
2085 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002086
asaperssonfab67072017-04-04 05:51:49 -07002087 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002088 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002089 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002090 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002091 EXPECT_FALSE(stats.cpu_limited_resolution);
2092 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2093
2094 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002095 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002096 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002097
asaperssonfab67072017-04-04 05:51:49 -07002098 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002099 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002100 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002101 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002102 EXPECT_TRUE(stats.cpu_limited_resolution);
2103 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2104
asaperssonfab67072017-04-04 05:51:49 -07002105 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002106 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002107 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002108 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002109 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002110 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002111 EXPECT_FALSE(stats.cpu_limited_resolution);
2112 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002113 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002114
mflodmancc3d4422017-08-03 08:27:51 -07002115 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002116}
2117
mflodmancc3d4422017-08-03 08:27:51 -07002118TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002119 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002120 DataRate::BitsPerSec(kTargetBitrateBps),
2121 DataRate::BitsPerSec(kTargetBitrateBps),
2122 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002123
asaperssonfab67072017-04-04 05:51:49 -07002124 const int kWidth = 1280;
2125 const int kHeight = 720;
2126 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002127 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002128 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002129 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002130 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002131 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002132
2133 // Set new source with adaptation still enabled.
2134 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002135 video_stream_encoder_->SetSource(&new_video_source,
2136 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002137
asaperssonfab67072017-04-04 05:51:49 -07002138 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002139 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002140 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002141 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002142 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002143 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002144
asaperssonfab67072017-04-04 05:51:49 -07002145 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002146 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002147 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002148 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002149 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002150 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002151 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002152 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002153
asaperssonfab67072017-04-04 05:51:49 -07002154 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002155 video_stream_encoder_->SetSource(&new_video_source,
2156 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002157
asaperssonfab67072017-04-04 05:51:49 -07002158 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002159 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002160 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002161 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002162 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002163 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002164
asapersson02465b82017-04-10 01:12:52 -07002165 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002166 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002167 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002168
asaperssonfab67072017-04-04 05:51:49 -07002169 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002170 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002171 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002172 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002173 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002174 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2175 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002176
mflodmancc3d4422017-08-03 08:27:51 -07002177 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002178}
2179
mflodmancc3d4422017-08-03 08:27:51 -07002180TEST_F(VideoStreamEncoderTest,
2181 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002182 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002183 DataRate::BitsPerSec(kTargetBitrateBps),
2184 DataRate::BitsPerSec(kTargetBitrateBps),
2185 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002186
2187 const int kWidth = 1280;
2188 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002189 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002190 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002191 video_source_.IncomingCapturedFrame(
2192 CreateFrame(timestamp_ms, kWidth, kHeight));
2193 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002194 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2195 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2196 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2197
2198 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002199 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002200 timestamp_ms += kFrameIntervalMs;
2201 video_source_.IncomingCapturedFrame(
2202 CreateFrame(timestamp_ms, kWidth, kHeight));
2203 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002204 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2205 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2206 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2207
2208 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002209 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002210 timestamp_ms += kFrameIntervalMs;
2211 video_source_.IncomingCapturedFrame(
2212 CreateFrame(timestamp_ms, kWidth, kHeight));
2213 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002214 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2215 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2216 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2217
Niels Möller4db138e2018-04-19 09:04:13 +02002218 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002219 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002220
2221 VideoEncoderConfig video_encoder_config;
2222 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2223 // Make format different, to force recreation of encoder.
2224 video_encoder_config.video_format.parameters["foo"] = "foo";
2225 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002226 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002227 timestamp_ms += kFrameIntervalMs;
2228 video_source_.IncomingCapturedFrame(
2229 CreateFrame(timestamp_ms, kWidth, kHeight));
2230 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002231 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2232 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2233 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2234
mflodmancc3d4422017-08-03 08:27:51 -07002235 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002236}
2237
mflodmancc3d4422017-08-03 08:27:51 -07002238TEST_F(VideoStreamEncoderTest,
Evan Shrubsole33be9df2020-03-05 18:39:32 +01002239 StatsTracksCpuAdaptationStatsWhenSwitchingSource_Balanced) {
2240 video_stream_encoder_->OnBitrateUpdated(
2241 DataRate::BitsPerSec(kTargetBitrateBps),
2242 DataRate::BitsPerSec(kTargetBitrateBps),
2243 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
2244
2245 const int kWidth = 1280;
2246 const int kHeight = 720;
2247 int sequence = 1;
2248
2249 // Enable BALANCED preference, no initial limitation.
2250 test::FrameForwarder source;
2251 video_stream_encoder_->SetSource(&source,
2252 webrtc::DegradationPreference::BALANCED);
2253 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2254 WaitForEncodedFrame(sequence++);
2255 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2256 EXPECT_FALSE(stats.cpu_limited_resolution);
2257 EXPECT_FALSE(stats.cpu_limited_framerate);
2258 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2259
2260 // Trigger CPU overuse, should now adapt down.
2261 video_stream_encoder_->TriggerCpuOveruse();
2262 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2263 WaitForEncodedFrame(sequence++);
2264 stats = stats_proxy_->GetStats();
2265 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2266
2267 // Set new degradation preference should clear restrictions since we changed
2268 // from BALANCED.
2269 video_stream_encoder_->SetSource(
2270 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2271 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2272 WaitForEncodedFrame(sequence++);
2273 stats = stats_proxy_->GetStats();
2274 EXPECT_FALSE(stats.cpu_limited_resolution);
2275 EXPECT_FALSE(stats.cpu_limited_framerate);
2276 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2277
2278 // Force an input frame rate to be available, or the adaptation call won't
2279 // know what framerate to adapt from.
2280 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2281 mock_stats.input_frame_rate = 30;
2282 stats_proxy_->SetMockStats(mock_stats);
2283 video_stream_encoder_->TriggerCpuOveruse();
2284 stats_proxy_->ResetMockStats();
2285 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2286 WaitForEncodedFrame(sequence++);
2287
2288 // We have now adapted once.
2289 stats = stats_proxy_->GetStats();
2290 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2291
2292 // Back to BALANCED, should clear the restrictions again.
2293 video_stream_encoder_->SetSource(&source,
2294 webrtc::DegradationPreference::BALANCED);
2295 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2296 WaitForEncodedFrame(sequence++);
2297 stats = stats_proxy_->GetStats();
2298 EXPECT_FALSE(stats.cpu_limited_resolution);
2299 EXPECT_FALSE(stats.cpu_limited_framerate);
2300 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2301
2302 video_stream_encoder_->Stop();
2303}
2304
2305TEST_F(VideoStreamEncoderTest,
mflodmancc3d4422017-08-03 08:27:51 -07002306 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002307 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002308 DataRate::BitsPerSec(kTargetBitrateBps),
2309 DataRate::BitsPerSec(kTargetBitrateBps),
2310 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002311
asapersson0944a802017-04-07 00:57:58 -07002312 const int kWidth = 1280;
2313 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002314 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002315
asaperssonfab67072017-04-04 05:51:49 -07002316 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002317 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002318 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002319 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002320 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002321 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2322
asapersson02465b82017-04-10 01:12:52 -07002323 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002324 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002325 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002326 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002327 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002328 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002329 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002330 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2331
2332 // Set new source with adaptation still enabled.
2333 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002334 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002335 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002336
2337 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002338 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002339 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002340 stats = stats_proxy_->GetStats();
2341 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002342 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002343 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2344
sprangc5d62e22017-04-02 23:53:04 -07002345 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002346 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002347 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002348 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002349 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002350 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002351 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002352 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002353 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002354 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002355 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2356
sprangc5d62e22017-04-02 23:53:04 -07002357 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002358 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002359 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2360 mock_stats.input_frame_rate = 30;
2361 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002362 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002363 stats_proxy_->ResetMockStats();
2364
2365 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002366 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002367 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002368
2369 // Framerate now adapted.
2370 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002371 EXPECT_FALSE(stats.cpu_limited_resolution);
2372 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002373 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2374
2375 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002376 video_stream_encoder_->SetSource(&new_video_source,
2377 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002378 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002379 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002380 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002381
2382 stats = stats_proxy_->GetStats();
2383 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002384 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002385 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2386
2387 // Try to trigger overuse. Should not succeed.
2388 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002389 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002390 stats_proxy_->ResetMockStats();
2391
2392 stats = stats_proxy_->GetStats();
2393 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002394 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002395 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2396
2397 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002398 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002399 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002400 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002401 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002402 stats = stats_proxy_->GetStats();
2403 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002404 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002405 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002406
2407 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002408 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002409 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002410 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002411 stats = stats_proxy_->GetStats();
2412 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002413 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002414 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2415
2416 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002417 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002418 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002419 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002420 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002421 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002422 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002423 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002424 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002425 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002426 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2427
2428 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002430 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002431 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002432 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002433 stats = stats_proxy_->GetStats();
2434 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002435 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002436 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002437 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002438
mflodmancc3d4422017-08-03 08:27:51 -07002439 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002440}
2441
mflodmancc3d4422017-08-03 08:27:51 -07002442TEST_F(VideoStreamEncoderTest,
2443 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002444 const int kWidth = 1280;
2445 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002446 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002447 DataRate::BitsPerSec(kTargetBitrateBps),
2448 DataRate::BitsPerSec(kTargetBitrateBps),
2449 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002450
asaperssonfab67072017-04-04 05:51:49 -07002451 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002452 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002453
asaperssonfab67072017-04-04 05:51:49 -07002454 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002455 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002456
asaperssonfab67072017-04-04 05:51:49 -07002457 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002458 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002459
asaperssonfab67072017-04-04 05:51:49 -07002460 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002461 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002462
kthelgason876222f2016-11-29 01:44:11 -08002463 // Expect a scale down.
2464 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002465 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002466
asapersson02465b82017-04-10 01:12:52 -07002467 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002468 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002469 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002470 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002471
asaperssonfab67072017-04-04 05:51:49 -07002472 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002473 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002474 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002475 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002476
asaperssonfab67072017-04-04 05:51:49 -07002477 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002478 EXPECT_EQ(std::numeric_limits<int>::max(),
2479 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002480
asaperssonfab67072017-04-04 05:51:49 -07002481 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002482 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002483 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002484 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002485
asapersson02465b82017-04-10 01:12:52 -07002486 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002487 EXPECT_EQ(std::numeric_limits<int>::max(),
2488 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002489
mflodmancc3d4422017-08-03 08:27:51 -07002490 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002491}
2492
mflodmancc3d4422017-08-03 08:27:51 -07002493TEST_F(VideoStreamEncoderTest,
2494 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002495 const int kWidth = 1280;
2496 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002497 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002498 DataRate::BitsPerSec(kTargetBitrateBps),
2499 DataRate::BitsPerSec(kTargetBitrateBps),
2500 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002501
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002502 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002503 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002504 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002505 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002506
2507 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002508 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002509 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002510 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2511 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2512
2513 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002514 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002515 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002516 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2517 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2518 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2519
2520 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002521 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002522 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2523 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2524 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2525
mflodmancc3d4422017-08-03 08:27:51 -07002526 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002527}
2528
mflodmancc3d4422017-08-03 08:27:51 -07002529TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002530 const int kWidth = 1280;
2531 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002532 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002533 DataRate::BitsPerSec(kTargetBitrateBps),
2534 DataRate::BitsPerSec(kTargetBitrateBps),
2535 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002536
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002537 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002538 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002539 video_stream_encoder_->SetSource(&source,
2540 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002541 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2542 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002543 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002544
2545 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002546 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002547 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2548 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2549 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2550 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2551
2552 // Trigger adapt down for same input resolution, expect no change.
2553 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2554 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002555 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002556 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2557 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2558 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2559
2560 // Trigger adapt down for larger input resolution, expect no change.
2561 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2562 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002563 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002564 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2565 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2566 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2567
mflodmancc3d4422017-08-03 08:27:51 -07002568 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002569}
2570
mflodmancc3d4422017-08-03 08:27:51 -07002571TEST_F(VideoStreamEncoderTest,
2572 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002573 const int kWidth = 1280;
2574 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002575 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002576 DataRate::BitsPerSec(kTargetBitrateBps),
2577 DataRate::BitsPerSec(kTargetBitrateBps),
2578 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002579
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002580 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002581 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002582 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002583 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002584
2585 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002586 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002587 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002588 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2589 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2590
2591 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002592 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002593 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002594 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2595 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2596
mflodmancc3d4422017-08-03 08:27:51 -07002597 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002598}
2599
mflodmancc3d4422017-08-03 08:27:51 -07002600TEST_F(VideoStreamEncoderTest,
2601 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002602 const int kWidth = 1280;
2603 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002604 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002605 DataRate::BitsPerSec(kTargetBitrateBps),
2606 DataRate::BitsPerSec(kTargetBitrateBps),
2607 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002608
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002609 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002610 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002611 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002612 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002613
2614 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002615 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002616 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002617 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002618 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2619
2620 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002621 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002622 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002623 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002624 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2625
mflodmancc3d4422017-08-03 08:27:51 -07002626 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002627}
2628
mflodmancc3d4422017-08-03 08:27:51 -07002629TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002630 const int kWidth = 1280;
2631 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002632 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002633 DataRate::BitsPerSec(kTargetBitrateBps),
2634 DataRate::BitsPerSec(kTargetBitrateBps),
2635 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002636
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002637 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002638 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002639 video_stream_encoder_->SetSource(&source,
2640 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002641
2642 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2643 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002644 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002645 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2646 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2647 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2648
2649 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002650 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002651 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002652 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2653 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2654 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2655
mflodmancc3d4422017-08-03 08:27:51 -07002656 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002657}
2658
mflodmancc3d4422017-08-03 08:27:51 -07002659TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002660 const int kWidth = 1280;
2661 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002662 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002663 DataRate::BitsPerSec(kTargetBitrateBps),
2664 DataRate::BitsPerSec(kTargetBitrateBps),
2665 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002666
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002667 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002668 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002669 video_stream_encoder_->SetSource(&source,
2670 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002671
2672 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2673 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002674 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002675 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2676 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2677 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2678
2679 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002680 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002681 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002682 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2683 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2684 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2685
mflodmancc3d4422017-08-03 08:27:51 -07002686 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002687}
2688
mflodmancc3d4422017-08-03 08:27:51 -07002689TEST_F(VideoStreamEncoderTest,
2690 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002691 const int kWidth = 1280;
2692 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002693 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002694 DataRate::BitsPerSec(kTargetBitrateBps),
2695 DataRate::BitsPerSec(kTargetBitrateBps),
2696 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002697
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002698 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002699 AdaptingFrameForwarder source;
2700 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002701 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002702 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002703
2704 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002705 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002706 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002707 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2708 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2709
2710 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002711 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002712 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002713 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002714 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002715 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2716 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2717
2718 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002719 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002720 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002721 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2722 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2723 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2724
mflodmancc3d4422017-08-03 08:27:51 -07002725 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002726}
2727
mflodmancc3d4422017-08-03 08:27:51 -07002728TEST_F(VideoStreamEncoderTest,
2729 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002730 const int kWidth = 1280;
2731 const int kHeight = 720;
2732 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002733 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002734 DataRate::BitsPerSec(kTargetBitrateBps),
2735 DataRate::BitsPerSec(kTargetBitrateBps),
2736 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002737
2738 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2739 stats.input_frame_rate = kInputFps;
2740 stats_proxy_->SetMockStats(stats);
2741
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002742 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002743 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2744 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002745 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002746
2747 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002748 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002749 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2750 sink_.WaitForEncodedFrame(2);
2751 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2752
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002753 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002754 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002755 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002756 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002757 // Give the encoder queue time to process the change in degradation preference
2758 // by waiting for an encoded frame.
2759 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2760 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002761 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002762
2763 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002764 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002765 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2766 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002767 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2768
2769 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002770 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002771 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002772
mflodmancc3d4422017-08-03 08:27:51 -07002773 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002774}
2775
mflodmancc3d4422017-08-03 08:27:51 -07002776TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002777 const int kWidth = 1280;
2778 const int kHeight = 720;
2779 const size_t kNumFrames = 10;
2780
Erik Språng4c6ca302019-04-08 15:14:01 +02002781 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002782 DataRate::BitsPerSec(kTargetBitrateBps),
2783 DataRate::BitsPerSec(kTargetBitrateBps),
2784 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002785
asaperssond0de2952017-04-21 01:47:31 -07002786 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002787 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002788 video_source_.set_adaptation_enabled(true);
2789
2790 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2791 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2792
2793 int downscales = 0;
2794 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002795 video_source_.IncomingCapturedFrame(
2796 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2797 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002798
asaperssonfab67072017-04-04 05:51:49 -07002799 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002800 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002801 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002802 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002803
2804 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2805 ++downscales;
2806
2807 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2808 EXPECT_EQ(downscales,
2809 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2810 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002811 }
mflodmancc3d4422017-08-03 08:27:51 -07002812 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002813}
2814
mflodmancc3d4422017-08-03 08:27:51 -07002815TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002816 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2817 const int kWidth = 1280;
2818 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002819 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002820 DataRate::BitsPerSec(kTargetBitrateBps),
2821 DataRate::BitsPerSec(kTargetBitrateBps),
2822 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002823
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002824 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002825 AdaptingFrameForwarder source;
2826 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002827 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002828 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002829
Åsa Persson8c1bf952018-09-13 10:42:19 +02002830 int64_t timestamp_ms = kFrameIntervalMs;
2831 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002832 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002833 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002834 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2835 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2836
2837 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002838 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002839 timestamp_ms += kFrameIntervalMs;
2840 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2841 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002842 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002843 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2844 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2845
2846 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002847 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002848 timestamp_ms += kFrameIntervalMs;
2849 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002850 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002851 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002852 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2853 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2854
2855 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002856 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002857 timestamp_ms += kFrameIntervalMs;
2858 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2859 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002860 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002861 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2862 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2863
2864 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002865 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002866 timestamp_ms += kFrameIntervalMs;
2867 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002868 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002869 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002870 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2871 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2872
mflodmancc3d4422017-08-03 08:27:51 -07002873 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002874}
2875
mflodmancc3d4422017-08-03 08:27:51 -07002876TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002877 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2878 const int kWidth = 1280;
2879 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002880 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002881 DataRate::BitsPerSec(kTargetBitrateBps),
2882 DataRate::BitsPerSec(kTargetBitrateBps),
2883 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002884
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002885 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002886 AdaptingFrameForwarder source;
2887 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002888 video_stream_encoder_->SetSource(&source,
2889 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002890
Åsa Persson8c1bf952018-09-13 10:42:19 +02002891 int64_t timestamp_ms = kFrameIntervalMs;
2892 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002893 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002894 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002895 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2896 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2897
2898 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002899 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002900 timestamp_ms += kFrameIntervalMs;
2901 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2902 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002903 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2904 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2905 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2906
2907 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002908 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002909 timestamp_ms += kFrameIntervalMs;
2910 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002911 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002912 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002913 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2914 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2915
2916 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002917 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002918 timestamp_ms += kFrameIntervalMs;
2919 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2920 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002921 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2922 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2923 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2924
2925 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002926 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002927 timestamp_ms += kFrameIntervalMs;
2928 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002929 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002930 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002931 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2932 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2933
mflodmancc3d4422017-08-03 08:27:51 -07002934 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002935}
2936
Sergey Silkin41c650b2019-10-14 13:12:19 +02002937TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2938 fake_encoder_.SetResolutionBitrateLimits(
2939 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2940
2941 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002942 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2943 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2944 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2945 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002946
2947 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2948 AdaptingFrameForwarder source;
2949 source.set_adaptation_enabled(true);
2950 video_stream_encoder_->SetSource(
2951 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2952
2953 // Insert 720p frame.
2954 int64_t timestamp_ms = kFrameIntervalMs;
2955 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2956 WaitForEncodedFrame(1280, 720);
2957
2958 // Reduce bitrate and trigger adapt down.
2959 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002960 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2961 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2962 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2963 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002964 video_stream_encoder_->TriggerQualityLow();
2965
2966 // Insert 720p frame. It should be downscaled and encoded.
2967 timestamp_ms += kFrameIntervalMs;
2968 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2969 WaitForEncodedFrame(960, 540);
2970
2971 // Trigger adapt up. Higher resolution should not be requested duo to lack
2972 // of bitrate.
2973 video_stream_encoder_->TriggerQualityHigh();
2974 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2975
2976 // Increase bitrate.
2977 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002978 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2979 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2980 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2981 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002982
2983 // Trigger adapt up. Higher resolution should be requested.
2984 video_stream_encoder_->TriggerQualityHigh();
2985 VerifyFpsMaxResolutionMax(source.sink_wants());
2986
2987 video_stream_encoder_->Stop();
2988}
2989
2990TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2991 fake_encoder_.SetResolutionBitrateLimits(
2992 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2993
2994 // Set bitrate equal to min bitrate of 540p.
2995 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002996 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2997 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2998 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2999 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02003000
3001 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
3002 AdaptingFrameForwarder source;
3003 source.set_adaptation_enabled(true);
3004 video_stream_encoder_->SetSource(
3005 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
3006
3007 // Insert 720p frame. It should be dropped and lower resolution should be
3008 // requested.
3009 int64_t timestamp_ms = kFrameIntervalMs;
3010 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
3011 ExpectDroppedFrame();
3012 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
3013
3014 // Insert 720p frame. It should be downscaled and encoded.
3015 timestamp_ms += kFrameIntervalMs;
3016 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
3017 WaitForEncodedFrame(960, 540);
3018
3019 video_stream_encoder_->Stop();
3020}
3021
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003022class BalancedDegradationTest : public VideoStreamEncoderTest {
3023 protected:
3024 void SetupTest() {
3025 // Reset encoder for field trials to take effect.
3026 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02003027 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003028
3029 // Enable BALANCED preference.
3030 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02003031 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
3032 }
3033
3034 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01003035 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003036 DataRate::BitsPerSec(bitrate_bps), DataRate::BitsPerSec(bitrate_bps),
3037 DataRate::BitsPerSec(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003038 }
3039
Åsa Persson45b176f2019-09-30 11:19:05 +02003040 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003041 timestamp_ms_ += kFrameIntervalMs;
3042 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02003043 }
3044
3045 void InsertFrameAndWaitForEncoded() {
3046 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003047 sink_.WaitForEncodedFrame(timestamp_ms_);
3048 }
3049
3050 const int kWidth = 640; // pixels:640x360=230400
3051 const int kHeight = 360;
3052 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
3053 int64_t timestamp_ms_ = 0;
3054 AdaptingFrameForwarder source_;
3055};
3056
3057TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
3058 test::ScopedFieldTrials field_trials(
3059 "WebRTC-Video-BalancedDegradationSettings/"
3060 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
3061 SetupTest();
3062
3063 // Force input frame rate.
3064 const int kInputFps = 24;
3065 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3066 stats.input_frame_rate = kInputFps;
3067 stats_proxy_->SetMockStats(stats);
3068
Åsa Persson45b176f2019-09-30 11:19:05 +02003069 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003070 VerifyFpsMaxResolutionMax(source_.sink_wants());
3071
3072 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3073 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
3074 video_stream_encoder_->TriggerQualityLowExpectFalse();
3075 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3076
3077 video_stream_encoder_->Stop();
3078}
3079
3080TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
3081 test::ScopedFieldTrials field_trials(
3082 "WebRTC-Video-BalancedDegradationSettings/"
3083 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
3084 SetupTest();
3085
3086 // Force input frame rate.
3087 const int kInputFps = 25;
3088 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3089 stats.input_frame_rate = kInputFps;
3090 stats_proxy_->SetMockStats(stats);
3091
Åsa Persson45b176f2019-09-30 11:19:05 +02003092 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003093 VerifyFpsMaxResolutionMax(source_.sink_wants());
3094
3095 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3096 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
3097 video_stream_encoder_->TriggerQualityLow();
3098 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3099
3100 video_stream_encoder_->Stop();
3101}
3102
3103TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
3104 test::ScopedFieldTrials field_trials(
3105 "WebRTC-Video-BalancedDegradationSettings/"
3106 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
3107 SetupTest();
3108
3109 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
3110
Åsa Persson45b176f2019-09-30 11:19:05 +02003111 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003112 VerifyFpsMaxResolutionMax(source_.sink_wants());
3113
3114 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
3115 video_stream_encoder_->TriggerQualityLow();
3116 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
3117
3118 video_stream_encoder_->Stop();
3119}
3120
Åsa Perssonccfb3402019-09-25 15:13:04 +02003121TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003122 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02003123 "WebRTC-Video-BalancedDegradationSettings/"
3124 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003125 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02003126
Åsa Persson1b247f12019-08-14 17:26:39 +02003127 const int kMinBitrateBps = 425000;
3128 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003129 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003130
Åsa Persson45b176f2019-09-30 11:19:05 +02003131 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003132 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003133 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3134
3135 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3136 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003137 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003138 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02003139 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3140
3141 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3142 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003143 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003144 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003145 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3146
Åsa Persson30ab0152019-08-27 12:22:33 +02003147 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3148 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003149 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003150 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
3151 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02003152 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3153
3154 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02003155 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003156 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003157 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02003158
Åsa Persson30ab0152019-08-27 12:22:33 +02003159 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003160 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003161 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003162 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003163 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003164 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3165
3166 video_stream_encoder_->Stop();
3167}
3168
Åsa Perssonccfb3402019-09-25 15:13:04 +02003169TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003170 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3171 test::ScopedFieldTrials field_trials(
3172 "WebRTC-Video-BalancedDegradationSettings/"
3173 "pixels:57600|129600|230400,fps:7|24|24/");
3174 SetupTest();
3175 OnBitrateUpdated(kLowTargetBitrateBps);
3176
3177 VerifyNoLimitation(source_.sink_wants());
3178
3179 // Insert frame, expect scaled down:
3180 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3181 InsertFrame();
3182 EXPECT_FALSE(WaitForFrame(1000));
3183 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3184 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3185
3186 // Insert frame, expect scaled down:
3187 // resolution (320x180@24fps).
3188 InsertFrame();
3189 EXPECT_FALSE(WaitForFrame(1000));
3190 EXPECT_LT(source_.sink_wants().max_pixel_count,
3191 source_.last_wants().max_pixel_count);
3192 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3193
3194 // Frame should not be dropped (min pixels per frame reached).
3195 InsertFrameAndWaitForEncoded();
3196
3197 video_stream_encoder_->Stop();
3198}
3199
3200TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003201 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003202 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003203 "WebRTC-Video-BalancedDegradationSettings/"
3204 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003205 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003206
Åsa Persson30ab0152019-08-27 12:22:33 +02003207 const int kResolutionMinBitrateBps = 435000;
3208 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003209 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003210
Åsa Persson45b176f2019-09-30 11:19:05 +02003211 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003212 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003213 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3214
3215 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3216 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003217 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003218 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003219 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3220
3221 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3222 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003223 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003224 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003225 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3226
3227 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3228 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003229 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003230 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003231 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3232
Åsa Persson30ab0152019-08-27 12:22:33 +02003233 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3234 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003235 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003236 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003237 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3238
3239 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3240 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003241 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003242 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3243
3244 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003245 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003246 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003247 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003248 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003249 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3250
3251 video_stream_encoder_->Stop();
3252}
3253
Åsa Perssonccfb3402019-09-25 15:13:04 +02003254TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003255 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003256 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003257 "WebRTC-Video-BalancedDegradationSettings/"
3258 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003259 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003260
Åsa Persson30ab0152019-08-27 12:22:33 +02003261 const int kMinBitrateBps = 425000;
3262 const int kTooLowMinBitrateBps = 424000;
3263 const int kResolutionMinBitrateBps = 435000;
3264 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003265 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003266
Åsa Persson45b176f2019-09-30 11:19:05 +02003267 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003268 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003269 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3270
3271 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3272 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003273 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003274 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003275 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3276
3277 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3278 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003279 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003280 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003281 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3282
3283 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3284 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003285 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003286 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003287 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3288
3289 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3290 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003291 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003292 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3293
3294 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003295 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003296 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003297 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003298 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003299 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3300
3301 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003302 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003303 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003304 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003305 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3306
3307 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003308 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003309 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003310 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003311 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003312 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3313
Åsa Persson1b247f12019-08-14 17:26:39 +02003314 video_stream_encoder_->Stop();
3315}
3316
mflodmancc3d4422017-08-03 08:27:51 -07003317TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003318 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3319 const int kWidth = 1280;
3320 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003321 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003322 DataRate::BitsPerSec(kTargetBitrateBps),
3323 DataRate::BitsPerSec(kTargetBitrateBps),
3324 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003325
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003326 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003327 AdaptingFrameForwarder source;
3328 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003329 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003330 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003331
Åsa Persson8c1bf952018-09-13 10:42:19 +02003332 int64_t timestamp_ms = kFrameIntervalMs;
3333 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003334 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003335 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003336 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3337 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3338 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3339 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3340
3341 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003342 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003343 timestamp_ms += kFrameIntervalMs;
3344 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3345 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003346 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003347 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3348 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3349 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3350 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3351
3352 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003353 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003354 timestamp_ms += kFrameIntervalMs;
3355 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3356 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003357 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003358 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3359 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3360 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3361 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3362
Jonathan Yubc771b72017-12-08 17:04:29 -08003363 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003364 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003365 timestamp_ms += kFrameIntervalMs;
3366 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3367 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003368 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003369 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3370 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003371 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003372 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3373
Jonathan Yubc771b72017-12-08 17:04:29 -08003374 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003375 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003376 timestamp_ms += kFrameIntervalMs;
3377 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3378 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003379 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003380 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003381 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3382 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3383 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3384 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3385
Jonathan Yubc771b72017-12-08 17:04:29 -08003386 // Trigger quality adapt down, expect no change (min resolution reached).
3387 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003388 timestamp_ms += kFrameIntervalMs;
3389 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3390 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003391 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3392 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3393 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3394 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3395 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3396
3397 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003398 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003399 timestamp_ms += kFrameIntervalMs;
3400 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3401 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003402 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003403 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3404 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3405 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3406 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3407
3408 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3409 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003410 timestamp_ms += kFrameIntervalMs;
3411 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3412 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003413 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3414 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3415 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3416 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3417 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3418
3419 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3420 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003421 timestamp_ms += kFrameIntervalMs;
3422 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3423 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003424 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003425 last_wants = source.sink_wants();
3426 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3427 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003428 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003429 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3430
3431 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003432 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003433 timestamp_ms += kFrameIntervalMs;
3434 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3435 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003436 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003437 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3438 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003439 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003440 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3441
3442 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003443 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003444 timestamp_ms += kFrameIntervalMs;
3445 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003446 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003447 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003448 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003449 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3450 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003451 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003452 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003453
mflodmancc3d4422017-08-03 08:27:51 -07003454 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003455}
3456
mflodmancc3d4422017-08-03 08:27:51 -07003457TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003458 const int kWidth = 640;
3459 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003460
Erik Språng4c6ca302019-04-08 15:14:01 +02003461 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003462 DataRate::BitsPerSec(kTargetBitrateBps),
3463 DataRate::BitsPerSec(kTargetBitrateBps),
3464 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003465
perkj803d97f2016-11-01 11:45:46 -07003466 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003467 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003468 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003469 }
3470
mflodmancc3d4422017-08-03 08:27:51 -07003471 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003472 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003473 video_source_.IncomingCapturedFrame(CreateFrame(
3474 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003475 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003476 }
3477
mflodmancc3d4422017-08-03 08:27:51 -07003478 video_stream_encoder_->Stop();
3479 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003480 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003481
Ying Wangef3998f2019-12-09 13:06:53 +01003482 EXPECT_METRIC_EQ(
3483 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3484 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003485 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3486}
3487
mflodmancc3d4422017-08-03 08:27:51 -07003488TEST_F(VideoStreamEncoderTest,
3489 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003490 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003491 DataRate::BitsPerSec(kTargetBitrateBps),
3492 DataRate::BitsPerSec(kTargetBitrateBps),
3493 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003494 const int kWidth = 640;
3495 const int kHeight = 360;
3496
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003497 video_stream_encoder_->SetSource(&video_source_,
3498 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003499
3500 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3501 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003502 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003503 }
3504
mflodmancc3d4422017-08-03 08:27:51 -07003505 video_stream_encoder_->Stop();
3506 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003507 stats_proxy_.reset();
3508
3509 EXPECT_EQ(0,
3510 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3511}
3512
mflodmancc3d4422017-08-03 08:27:51 -07003513TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003514 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003515 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003516
3517 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003518 const VideoBitrateAllocation expected_bitrate =
Mirta Dvornicic6799d732020-02-12 15:36:49 +01003519 SimulcastRateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003520 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3521 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003522
sprang57c2fff2017-01-16 06:24:02 -08003523 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003524 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003525 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003526 DataRate::BitsPerSec(kLowTargetBitrateBps),
3527 DataRate::BitsPerSec(kLowTargetBitrateBps),
3528 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003529
sprang57c2fff2017-01-16 06:24:02 -08003530 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003531 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3532 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003533 VideoBitrateAllocation bitrate_allocation =
3534 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003535 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003536 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003537 // TODO(srte): The use of millisecs here looks like an error, but the tests
3538 // fails using seconds, this should be investigated.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003539 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003540
3541 // Not called on second frame.
3542 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3543 .Times(0);
3544 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003545 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3546 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003547 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003548
3549 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003550 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3551 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003552 const int64_t start_time_ms = rtc::TimeMillis();
3553 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3554 video_source_.IncomingCapturedFrame(
3555 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3556 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003557 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003558 }
3559
3560 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003561 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003562
mflodmancc3d4422017-08-03 08:27:51 -07003563 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003564}
3565
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003566TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3567 // 2 TLs configured, temporal layers supported by encoder.
3568 const int kNumTemporalLayers = 2;
3569 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3570 fake_encoder_.SetTemporalLayersSupported(0, true);
3571
3572 // Bitrate allocated across temporal layers.
3573 const int kTl0Bps = kTargetBitrateBps *
3574 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003575 kNumTemporalLayers, /*temporal_id*/ 0,
3576 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003577 const int kTl1Bps = kTargetBitrateBps *
3578 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003579 kNumTemporalLayers, /*temporal_id*/ 1,
3580 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003581 VideoBitrateAllocation expected_bitrate;
3582 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3583 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3584
3585 VerifyAllocatedBitrate(expected_bitrate);
3586 video_stream_encoder_->Stop();
3587}
3588
3589TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3590 // 2 TLs configured, temporal layers not supported by encoder.
3591 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3592 fake_encoder_.SetTemporalLayersSupported(0, false);
3593
3594 // Temporal layers not supported by the encoder.
3595 // Total bitrate should be at ti:0.
3596 VideoBitrateAllocation expected_bitrate;
3597 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3598
3599 VerifyAllocatedBitrate(expected_bitrate);
3600 video_stream_encoder_->Stop();
3601}
3602
3603TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3604 // 2 TLs configured, temporal layers only supported for first stream.
3605 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3606 fake_encoder_.SetTemporalLayersSupported(0, true);
3607 fake_encoder_.SetTemporalLayersSupported(1, false);
3608
3609 const int kS0Bps = 150000;
3610 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003611 kS0Bps *
3612 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3613 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003614 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003615 kS0Bps *
3616 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3617 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003618 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3619 // Temporal layers not supported by si:1.
3620 VideoBitrateAllocation expected_bitrate;
3621 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3622 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3623 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3624
3625 VerifyAllocatedBitrate(expected_bitrate);
3626 video_stream_encoder_->Stop();
3627}
3628
Niels Möller7dc26b72017-12-06 10:27:48 +01003629TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3630 const int kFrameWidth = 1280;
3631 const int kFrameHeight = 720;
3632 const int kFramerate = 24;
3633
Erik Språng4c6ca302019-04-08 15:14:01 +02003634 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003635 DataRate::BitsPerSec(kTargetBitrateBps),
3636 DataRate::BitsPerSec(kTargetBitrateBps),
3637 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003638 test::FrameForwarder source;
3639 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003640 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003641
3642 // Insert a single frame, triggering initial configuration.
3643 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3644 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3645
3646 EXPECT_EQ(
3647 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3648 kDefaultFramerate);
3649
3650 // Trigger reconfigure encoder (without resetting the entire instance).
3651 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003652 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003653 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3654 video_encoder_config.number_of_streams = 1;
3655 video_encoder_config.video_stream_factory =
3656 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3657 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003658 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003659 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3660
3661 // Detector should be updated with fps limit from codec config.
3662 EXPECT_EQ(
3663 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3664 kFramerate);
3665
3666 // Trigger overuse, max framerate should be reduced.
3667 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3668 stats.input_frame_rate = kFramerate;
3669 stats_proxy_->SetMockStats(stats);
3670 video_stream_encoder_->TriggerCpuOveruse();
3671 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3672 int adapted_framerate =
3673 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3674 EXPECT_LT(adapted_framerate, kFramerate);
3675
3676 // Trigger underuse, max framerate should go back to codec configured fps.
3677 // Set extra low fps, to make sure it's actually reset, not just incremented.
3678 stats = stats_proxy_->GetStats();
3679 stats.input_frame_rate = adapted_framerate / 2;
3680 stats_proxy_->SetMockStats(stats);
3681 video_stream_encoder_->TriggerCpuNormalUsage();
3682 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3683 EXPECT_EQ(
3684 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3685 kFramerate);
3686
3687 video_stream_encoder_->Stop();
3688}
3689
3690TEST_F(VideoStreamEncoderTest,
3691 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3692 const int kFrameWidth = 1280;
3693 const int kFrameHeight = 720;
3694 const int kLowFramerate = 15;
3695 const int kHighFramerate = 25;
3696
Erik Språng4c6ca302019-04-08 15:14:01 +02003697 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003698 DataRate::BitsPerSec(kTargetBitrateBps),
3699 DataRate::BitsPerSec(kTargetBitrateBps),
3700 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003701 test::FrameForwarder source;
3702 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003703 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003704
3705 // Trigger initial configuration.
3706 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003707 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003708 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3709 video_encoder_config.number_of_streams = 1;
3710 video_encoder_config.video_stream_factory =
3711 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3712 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3713 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003714 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003715 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3716
3717 EXPECT_EQ(
3718 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3719 kLowFramerate);
3720
3721 // Trigger overuse, max framerate should be reduced.
3722 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3723 stats.input_frame_rate = kLowFramerate;
3724 stats_proxy_->SetMockStats(stats);
3725 video_stream_encoder_->TriggerCpuOveruse();
3726 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3727 int adapted_framerate =
3728 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3729 EXPECT_LT(adapted_framerate, kLowFramerate);
3730
3731 // Reconfigure the encoder with a new (higher max framerate), max fps should
3732 // still respect the adaptation.
3733 video_encoder_config.video_stream_factory =
3734 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3735 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3736 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003737 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003738 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3739
3740 EXPECT_EQ(
3741 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3742 adapted_framerate);
3743
3744 // Trigger underuse, max framerate should go back to codec configured fps.
3745 stats = stats_proxy_->GetStats();
3746 stats.input_frame_rate = adapted_framerate;
3747 stats_proxy_->SetMockStats(stats);
3748 video_stream_encoder_->TriggerCpuNormalUsage();
3749 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3750 EXPECT_EQ(
3751 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3752 kHighFramerate);
3753
3754 video_stream_encoder_->Stop();
3755}
3756
mflodmancc3d4422017-08-03 08:27:51 -07003757TEST_F(VideoStreamEncoderTest,
3758 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003759 const int kFrameWidth = 1280;
3760 const int kFrameHeight = 720;
3761 const int kFramerate = 24;
3762
Erik Språng4c6ca302019-04-08 15:14:01 +02003763 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003764 DataRate::BitsPerSec(kTargetBitrateBps),
3765 DataRate::BitsPerSec(kTargetBitrateBps),
3766 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003767 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003768 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003769 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003770
3771 // Trigger initial configuration.
3772 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003773 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003774 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3775 video_encoder_config.number_of_streams = 1;
3776 video_encoder_config.video_stream_factory =
3777 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3778 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003779 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003780 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003781 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003782
Niels Möller7dc26b72017-12-06 10:27:48 +01003783 EXPECT_EQ(
3784 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3785 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003786
3787 // Trigger overuse, max framerate should be reduced.
3788 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3789 stats.input_frame_rate = kFramerate;
3790 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003791 video_stream_encoder_->TriggerCpuOveruse();
3792 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003793 int adapted_framerate =
3794 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003795 EXPECT_LT(adapted_framerate, kFramerate);
3796
3797 // Change degradation preference to not enable framerate scaling. Target
3798 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003799 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003800 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003801 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003802 EXPECT_EQ(
3803 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3804 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003805
mflodmancc3d4422017-08-03 08:27:51 -07003806 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003807}
3808
mflodmancc3d4422017-08-03 08:27:51 -07003809TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003810 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003811 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003812 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3813 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3814 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003815 const int kWidth = 640;
3816 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003817
asaperssonfab67072017-04-04 05:51:49 -07003818 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003819
3820 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003821 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003822
3823 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003824 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003825
sprangc5d62e22017-04-02 23:53:04 -07003826 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003827
asaperssonfab67072017-04-04 05:51:49 -07003828 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003829 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003830 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003831
3832 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003833 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003834
sprangc5d62e22017-04-02 23:53:04 -07003835 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003836
mflodmancc3d4422017-08-03 08:27:51 -07003837 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003838}
3839
mflodmancc3d4422017-08-03 08:27:51 -07003840TEST_F(VideoStreamEncoderTest,
3841 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003842 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003843 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003844 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3845 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3846 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003847 const int kWidth = 640;
3848 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003849
3850 // We expect the n initial frames to get dropped.
3851 int i;
3852 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003853 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003854 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003855 }
3856 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003857 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003858 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003859
3860 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003861 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003862
mflodmancc3d4422017-08-03 08:27:51 -07003863 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003864}
3865
mflodmancc3d4422017-08-03 08:27:51 -07003866TEST_F(VideoStreamEncoderTest,
3867 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003868 const int kWidth = 640;
3869 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003870 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003871 DataRate::BitsPerSec(kLowTargetBitrateBps),
3872 DataRate::BitsPerSec(kLowTargetBitrateBps),
3873 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003874
3875 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003876 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003877 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003878
asaperssonfab67072017-04-04 05:51:49 -07003879 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003880 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003881 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003882
mflodmancc3d4422017-08-03 08:27:51 -07003883 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003884}
3885
mflodmancc3d4422017-08-03 08:27:51 -07003886TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003887 const int kWidth = 640;
3888 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003889 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003890
3891 VideoEncoderConfig video_encoder_config;
3892 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3893 // Make format different, to force recreation of encoder.
3894 video_encoder_config.video_format.parameters["foo"] = "foo";
3895 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003896 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003897 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003898 DataRate::BitsPerSec(kLowTargetBitrateBps),
3899 DataRate::BitsPerSec(kLowTargetBitrateBps),
3900 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003901
kthelgasonb83797b2017-02-14 11:57:25 -08003902 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003903 video_stream_encoder_->SetSource(&video_source_,
3904 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003905
asaperssonfab67072017-04-04 05:51:49 -07003906 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003907 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003908 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003909
mflodmancc3d4422017-08-03 08:27:51 -07003910 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003911 fake_encoder_.SetQualityScaling(true);
3912}
3913
Åsa Persson139f4dc2019-08-02 09:29:58 +02003914TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3915 webrtc::test::ScopedFieldTrials field_trials(
3916 "WebRTC-Video-QualityScalerSettings/"
3917 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3918 // Reset encoder for field trials to take effect.
3919 ConfigureEncoder(video_encoder_config_.Copy());
3920 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3921 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3922 const int kWidth = 640;
3923 const int kHeight = 360;
3924
3925 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003926 DataRate::BitsPerSec(kTargetBitrateBps),
3927 DataRate::BitsPerSec(kTargetBitrateBps),
3928 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003929 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3930 // Frame should not be dropped.
3931 WaitForEncodedFrame(1);
3932
3933 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003934 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3935 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3936 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003937 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3938 // Frame should not be dropped.
3939 WaitForEncodedFrame(2);
3940
3941 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003942 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3943 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3944 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003945 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3946 // Expect to drop this frame, the wait should time out.
3947 ExpectDroppedFrame();
3948
3949 // Expect the sink_wants to specify a scaled frame.
3950 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3951 video_stream_encoder_->Stop();
3952}
3953
Åsa Perssone644a032019-11-08 15:56:00 +01003954TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3955 webrtc::test::ScopedFieldTrials field_trials(
3956 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3957
3958 // Reset encoder for field trials to take effect.
3959 VideoEncoderConfig config = video_encoder_config_.Copy();
3960 config.max_bitrate_bps = kTargetBitrateBps;
3961 ConfigureEncoder(std::move(config));
3962 fake_encoder_.SetQp(kQpLow);
3963
3964 // Enable MAINTAIN_FRAMERATE preference.
3965 AdaptingFrameForwarder source;
3966 source.set_adaptation_enabled(true);
3967 video_stream_encoder_->SetSource(&source,
3968 DegradationPreference::MAINTAIN_FRAMERATE);
3969
3970 // Start at low bitrate.
3971 const int kLowBitrateBps = 200000;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003972 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(kLowBitrateBps),
3973 DataRate::BitsPerSec(kLowBitrateBps),
3974 DataRate::BitsPerSec(kLowBitrateBps),
3975 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003976
3977 // Expect first frame to be dropped and resolution to be limited.
3978 const int kWidth = 1280;
3979 const int kHeight = 720;
3980 const int64_t kFrameIntervalMs = 100;
3981 int64_t timestamp_ms = kFrameIntervalMs;
3982 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3983 ExpectDroppedFrame();
3984 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3985
3986 // Increase bitrate to encoder max.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003987 video_stream_encoder_->OnBitrateUpdated(
3988 DataRate::BitsPerSec(config.max_bitrate_bps),
3989 DataRate::BitsPerSec(config.max_bitrate_bps),
3990 DataRate::BitsPerSec(config.max_bitrate_bps), 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003991
3992 // Insert frames and advance |min_duration_ms|.
3993 for (size_t i = 1; i <= 10; i++) {
3994 timestamp_ms += kFrameIntervalMs;
3995 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3996 WaitForEncodedFrame(timestamp_ms);
3997 }
3998 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3999 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
4000
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004001 fake_clock_.AdvanceTime(TimeDelta::Millis(2000));
Åsa Perssone644a032019-11-08 15:56:00 +01004002
4003 // Insert frame should trigger high BW and release quality limitation.
4004 timestamp_ms += kFrameIntervalMs;
4005 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
4006 WaitForEncodedFrame(timestamp_ms);
4007 VerifyFpsMaxResolutionMax(source.sink_wants());
4008
4009 // Frame should not be adapted.
4010 timestamp_ms += kFrameIntervalMs;
4011 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
4012 WaitForEncodedFrame(kWidth, kHeight);
4013 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4014
4015 video_stream_encoder_->Stop();
4016}
4017
mflodmancc3d4422017-08-03 08:27:51 -07004018TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07004019 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
4020 const int kTooSmallWidth = 10;
4021 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02004022 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004023 DataRate::BitsPerSec(kTargetBitrateBps),
4024 DataRate::BitsPerSec(kTargetBitrateBps),
4025 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07004026
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004027 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07004028 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07004029 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004030 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07004031 VerifyNoLimitation(source.sink_wants());
4032 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4033
4034 // Trigger adapt down, too small frame, expect no change.
4035 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004036 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07004037 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004038 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07004039 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4040 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4041
mflodmancc3d4422017-08-03 08:27:51 -07004042 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07004043}
4044
mflodmancc3d4422017-08-03 08:27:51 -07004045TEST_F(VideoStreamEncoderTest,
4046 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004047 const int kTooSmallWidth = 10;
4048 const int kTooSmallHeight = 10;
4049 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02004050 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004051 DataRate::BitsPerSec(kTargetBitrateBps),
4052 DataRate::BitsPerSec(kTargetBitrateBps),
4053 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004054
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004055 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004056 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004057 video_stream_encoder_->SetSource(&source,
4058 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004059 VerifyNoLimitation(source.sink_wants());
4060 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4061 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4062
4063 // Trigger adapt down, expect limited framerate.
4064 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004065 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07004066 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004067 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4068 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4069 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4070 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4071
4072 // Trigger adapt down, too small frame, expect no change.
4073 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004074 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07004075 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004076 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4077 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4078 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4079 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4080
mflodmancc3d4422017-08-03 08:27:51 -07004081 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004082}
4083
mflodmancc3d4422017-08-03 08:27:51 -07004084TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07004085 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02004086 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004087 DataRate::BitsPerSec(kTargetBitrateBps),
4088 DataRate::BitsPerSec(kTargetBitrateBps),
4089 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02004090 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07004091 const int kFrameWidth = 1280;
4092 const int kFrameHeight = 720;
4093 video_source_.IncomingCapturedFrame(
4094 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004095 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07004096 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07004097}
4098
sprangb1ca0732017-02-01 08:38:12 -08004099// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07004100TEST_F(VideoStreamEncoderTest,
4101 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004102 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004103 DataRate::BitsPerSec(kTargetBitrateBps),
4104 DataRate::BitsPerSec(kTargetBitrateBps),
4105 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08004106
4107 const int kFrameWidth = 1280;
4108 const int kFrameHeight = 720;
4109 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07004110 // requested by
4111 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08004112 video_source_.set_adaptation_enabled(true);
4113
4114 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004115 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004116 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004117
4118 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004119 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08004120 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004121 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004122 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08004123
asaperssonfab67072017-04-04 05:51:49 -07004124 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07004125 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08004126 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004127 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004128 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004129
mflodmancc3d4422017-08-03 08:27:51 -07004130 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08004131}
sprangfe627f32017-03-29 08:24:59 -07004132
mflodmancc3d4422017-08-03 08:27:51 -07004133TEST_F(VideoStreamEncoderTest,
4134 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07004135 const int kFrameWidth = 1280;
4136 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07004137
Erik Språng4c6ca302019-04-08 15:14:01 +02004138 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004139 DataRate::BitsPerSec(kTargetBitrateBps),
4140 DataRate::BitsPerSec(kTargetBitrateBps),
4141 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004142 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004143 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004144 video_source_.set_adaptation_enabled(true);
4145
sprang4847ae62017-06-27 07:06:52 -07004146 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004147
4148 video_source_.IncomingCapturedFrame(
4149 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004150 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004151
4152 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07004153 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004154
4155 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07004156 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004157 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004158 video_source_.IncomingCapturedFrame(
4159 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004160 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004161 }
4162
4163 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07004164 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004165 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004166 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004167 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004168 video_source_.IncomingCapturedFrame(
4169 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004170 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004171 ++num_frames_dropped;
4172 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004173 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004174 }
4175 }
4176
sprang4847ae62017-06-27 07:06:52 -07004177 // Add some slack to account for frames dropped by the frame dropper.
4178 const int kErrorMargin = 1;
4179 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004180 kErrorMargin);
4181
4182 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004183 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004184 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004185 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004186 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004187 video_source_.IncomingCapturedFrame(
4188 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004189 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004190 ++num_frames_dropped;
4191 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004192 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004193 }
4194 }
sprang4847ae62017-06-27 07:06:52 -07004195 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004196 kErrorMargin);
4197
4198 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004199 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004200 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004201 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004202 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004203 video_source_.IncomingCapturedFrame(
4204 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004205 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004206 ++num_frames_dropped;
4207 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004208 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004209 }
4210 }
sprang4847ae62017-06-27 07:06:52 -07004211 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004212 kErrorMargin);
4213
4214 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004215 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004216 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004217 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004218 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004219 video_source_.IncomingCapturedFrame(
4220 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004221 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004222 ++num_frames_dropped;
4223 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004224 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004225 }
4226 }
4227 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4228
mflodmancc3d4422017-08-03 08:27:51 -07004229 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004230}
4231
mflodmancc3d4422017-08-03 08:27:51 -07004232TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004233 const int kFramerateFps = 5;
4234 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004235 const int kFrameWidth = 1280;
4236 const int kFrameHeight = 720;
4237
sprang4847ae62017-06-27 07:06:52 -07004238 // Reconfigure encoder with two temporal layers and screensharing, which will
4239 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004240 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004241
Erik Språng4c6ca302019-04-08 15:14:01 +02004242 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004243 DataRate::BitsPerSec(kTargetBitrateBps),
4244 DataRate::BitsPerSec(kTargetBitrateBps),
4245 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004246 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004247 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004248 video_source_.set_adaptation_enabled(true);
4249
sprang4847ae62017-06-27 07:06:52 -07004250 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004251
4252 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004253 rtc::VideoSinkWants last_wants;
4254 do {
4255 last_wants = video_source_.sink_wants();
4256
sprangc5d62e22017-04-02 23:53:04 -07004257 // Insert frames to get a new fps estimate...
4258 for (int j = 0; j < kFramerateFps; ++j) {
4259 video_source_.IncomingCapturedFrame(
4260 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004261 if (video_source_.last_sent_width()) {
4262 sink_.WaitForEncodedFrame(timestamp_ms);
4263 }
sprangc5d62e22017-04-02 23:53:04 -07004264 timestamp_ms += kFrameIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004265 fake_clock_.AdvanceTime(TimeDelta::Millis(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004266 }
4267 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004268 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004269 } while (video_source_.sink_wants().max_framerate_fps <
4270 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004271
Jonathan Yubc771b72017-12-08 17:04:29 -08004272 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004273
mflodmancc3d4422017-08-03 08:27:51 -07004274 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004275}
asaperssonf7e294d2017-06-13 23:25:22 -07004276
mflodmancc3d4422017-08-03 08:27:51 -07004277TEST_F(VideoStreamEncoderTest,
4278 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004279 const int kWidth = 1280;
4280 const int kHeight = 720;
4281 const int64_t kFrameIntervalMs = 150;
4282 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004283 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004284 DataRate::BitsPerSec(kTargetBitrateBps),
4285 DataRate::BitsPerSec(kTargetBitrateBps),
4286 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004287
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004288 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004289 AdaptingFrameForwarder source;
4290 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004291 video_stream_encoder_->SetSource(&source,
4292 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004293 timestamp_ms += kFrameIntervalMs;
4294 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004295 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004296 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004297 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4298 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4299 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4300
4301 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004302 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004303 timestamp_ms += kFrameIntervalMs;
4304 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004305 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004306 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4307 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4308 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4309 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4310
4311 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004312 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004313 timestamp_ms += kFrameIntervalMs;
4314 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004315 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004316 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4317 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4318 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4319 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4320
4321 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004322 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004323 timestamp_ms += kFrameIntervalMs;
4324 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004325 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004326 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4327 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4328 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4329 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4330
4331 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004332 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004333 timestamp_ms += kFrameIntervalMs;
4334 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004335 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004336 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4337 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4338 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4339 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4340
4341 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004342 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004343 timestamp_ms += kFrameIntervalMs;
4344 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004345 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004346 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4347 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4348 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4349 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4350
4351 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004352 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004353 timestamp_ms += kFrameIntervalMs;
4354 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004355 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004356 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4357 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4358 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4359 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4360
4361 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004362 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004363 timestamp_ms += kFrameIntervalMs;
4364 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004365 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004366 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4367 rtc::VideoSinkWants last_wants = source.sink_wants();
4368 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4369 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4370 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4371
4372 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004373 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004374 timestamp_ms += kFrameIntervalMs;
4375 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004376 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004377 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4378 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4379 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4380 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4381
4382 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004383 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004384 timestamp_ms += kFrameIntervalMs;
4385 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004386 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004387 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4388 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4389 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4390 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4391
4392 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004393 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004394 timestamp_ms += kFrameIntervalMs;
4395 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004396 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004397 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4398 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4399 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4400 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4401
4402 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004403 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004404 timestamp_ms += kFrameIntervalMs;
4405 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004406 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004407 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4408 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4409 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4410 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4411
4412 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004413 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004414 timestamp_ms += kFrameIntervalMs;
4415 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004416 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004417 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4418 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4419 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4420 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4421
4422 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004423 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004424 timestamp_ms += kFrameIntervalMs;
4425 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004426 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004427 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4428 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4429 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4430 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4431
4432 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004433 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004434 timestamp_ms += kFrameIntervalMs;
4435 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004436 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004437 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4438 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4439 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4440 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4441
Åsa Persson30ab0152019-08-27 12:22:33 +02004442 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004443 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004444 timestamp_ms += kFrameIntervalMs;
4445 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004446 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004447 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004448 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004449 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4450 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4451 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4452
4453 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004454 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004455 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004456 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4457
mflodmancc3d4422017-08-03 08:27:51 -07004458 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004459}
4460
mflodmancc3d4422017-08-03 08:27:51 -07004461TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004462 const int kWidth = 1280;
4463 const int kHeight = 720;
4464 const int64_t kFrameIntervalMs = 150;
4465 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004466 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004467 DataRate::BitsPerSec(kTargetBitrateBps),
4468 DataRate::BitsPerSec(kTargetBitrateBps),
4469 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004470
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004471 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004472 AdaptingFrameForwarder source;
4473 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004474 video_stream_encoder_->SetSource(&source,
4475 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004476 timestamp_ms += kFrameIntervalMs;
4477 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004478 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004479 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004480 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4481 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4482 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4483 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4484 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4485 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4486
4487 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004488 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004489 timestamp_ms += kFrameIntervalMs;
4490 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004491 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004492 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4493 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4494 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4495 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4496 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4497 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4498 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4499
4500 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004501 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004502 timestamp_ms += kFrameIntervalMs;
4503 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004504 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004505 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4506 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4507 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4508 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4509 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4510 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4511 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4512
4513 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004514 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004515 timestamp_ms += kFrameIntervalMs;
4516 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004517 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004518 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4519 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4520 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4521 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4522 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4523 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4524 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4525
4526 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004527 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004528 timestamp_ms += kFrameIntervalMs;
4529 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004530 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004531 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4532 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4533 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4534 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4535 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4536 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4537 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4538
4539 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004540 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004541 timestamp_ms += kFrameIntervalMs;
4542 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004543 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004544 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4545 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4546 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4547 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4548 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4549 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4550 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4551
4552 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004553 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004554 timestamp_ms += kFrameIntervalMs;
4555 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004556 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004557 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004558 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004559 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4560 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4561 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4562 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4563 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4564 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4565
4566 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004567 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004568 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004569 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4570 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4571
mflodmancc3d4422017-08-03 08:27:51 -07004572 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004573}
4574
mflodmancc3d4422017-08-03 08:27:51 -07004575TEST_F(VideoStreamEncoderTest,
4576 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004577 const int kWidth = 640;
4578 const int kHeight = 360;
4579 const int kFpsLimit = 15;
4580 const int64_t kFrameIntervalMs = 150;
4581 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004582 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004583 DataRate::BitsPerSec(kTargetBitrateBps),
4584 DataRate::BitsPerSec(kTargetBitrateBps),
4585 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004586
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004587 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004588 AdaptingFrameForwarder source;
4589 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004590 video_stream_encoder_->SetSource(&source,
4591 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004592 timestamp_ms += kFrameIntervalMs;
4593 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004594 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004595 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004596 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4597 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4598 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4599 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4600 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4601 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4602
4603 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004604 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004605 timestamp_ms += kFrameIntervalMs;
4606 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004607 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004608 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4609 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4610 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4611 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4612 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4613 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4614 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4615
4616 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004617 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004618 timestamp_ms += kFrameIntervalMs;
4619 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004620 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004621 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4622 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4623 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4624 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4625 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4626 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4627 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4628
4629 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004630 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004631 timestamp_ms += kFrameIntervalMs;
4632 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004633 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004634 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4635 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4636 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4637 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4638 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4639 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4640 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4641
4642 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004643 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004644 timestamp_ms += kFrameIntervalMs;
4645 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004646 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004647 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004648 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4649 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4650 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4651 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4652 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4653 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4654
4655 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004656 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004657 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004658 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4659 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4660
mflodmancc3d4422017-08-03 08:27:51 -07004661 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004662}
4663
mflodmancc3d4422017-08-03 08:27:51 -07004664TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004665 const int kFrameWidth = 1920;
4666 const int kFrameHeight = 1080;
4667 // 3/4 of 1920.
4668 const int kAdaptedFrameWidth = 1440;
4669 // 3/4 of 1080 rounded down to multiple of 4.
4670 const int kAdaptedFrameHeight = 808;
4671 const int kFramerate = 24;
4672
Erik Språng4c6ca302019-04-08 15:14:01 +02004673 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004674 DataRate::BitsPerSec(kTargetBitrateBps),
4675 DataRate::BitsPerSec(kTargetBitrateBps),
4676 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004677 // Trigger reconfigure encoder (without resetting the entire instance).
4678 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004679 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004680 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4681 video_encoder_config.number_of_streams = 1;
4682 video_encoder_config.video_stream_factory =
4683 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004684 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004685 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004686 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004687
4688 video_source_.set_adaptation_enabled(true);
4689
4690 video_source_.IncomingCapturedFrame(
4691 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004692 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004693
4694 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004695 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004696 video_source_.IncomingCapturedFrame(
4697 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004698 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004699
mflodmancc3d4422017-08-03 08:27:51 -07004700 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004701}
4702
mflodmancc3d4422017-08-03 08:27:51 -07004703TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004704 const int kFrameWidth = 1280;
4705 const int kFrameHeight = 720;
4706 const int kLowFps = 2;
4707 const int kHighFps = 30;
4708
Erik Språng4c6ca302019-04-08 15:14:01 +02004709 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004710 DataRate::BitsPerSec(kTargetBitrateBps),
4711 DataRate::BitsPerSec(kTargetBitrateBps),
4712 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004713
4714 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4715 max_framerate_ = kLowFps;
4716
4717 // Insert 2 seconds of 2fps video.
4718 for (int i = 0; i < kLowFps * 2; ++i) {
4719 video_source_.IncomingCapturedFrame(
4720 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4721 WaitForEncodedFrame(timestamp_ms);
4722 timestamp_ms += 1000 / kLowFps;
4723 }
4724
4725 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004726 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004727 DataRate::BitsPerSec(kTargetBitrateBps),
4728 DataRate::BitsPerSec(kTargetBitrateBps),
4729 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004730 video_source_.IncomingCapturedFrame(
4731 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4732 WaitForEncodedFrame(timestamp_ms);
4733 timestamp_ms += 1000 / kLowFps;
4734
4735 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4736
4737 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004738 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004739 const int kFrameIntervalMs = 1000 / kHighFps;
4740 max_framerate_ = kHighFps;
4741 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4742 video_source_.IncomingCapturedFrame(
4743 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4744 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4745 // be dropped if the encoder hans't been updated with the new higher target
4746 // framerate yet, causing it to overshoot the target bitrate and then
4747 // suffering the wrath of the media optimizer.
4748 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4749 timestamp_ms += kFrameIntervalMs;
4750 }
4751
4752 // Don expect correct measurement just yet, but it should be higher than
4753 // before.
4754 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4755
mflodmancc3d4422017-08-03 08:27:51 -07004756 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004757}
4758
mflodmancc3d4422017-08-03 08:27:51 -07004759TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004760 const int kFrameWidth = 1280;
4761 const int kFrameHeight = 720;
4762 const int kTargetBitrateBps = 1000000;
4763
4764 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004765 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004766 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004767 DataRate::BitsPerSec(kTargetBitrateBps),
4768 DataRate::BitsPerSec(kTargetBitrateBps),
4769 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004770 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004771
4772 // Insert a first video frame, causes another bitrate update.
4773 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4774 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4775 video_source_.IncomingCapturedFrame(
4776 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4777 WaitForEncodedFrame(timestamp_ms);
4778
4779 // Next, simulate video suspension due to pacer queue overrun.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004780 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
4781 DataRate::BitsPerSec(0),
4782 DataRate::BitsPerSec(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004783
4784 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004785 timestamp_ms += kProcessIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004786 fake_clock_.AdvanceTime(TimeDelta::Millis(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004787
4788 // Bitrate observer should not be called.
4789 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4790 video_source_.IncomingCapturedFrame(
4791 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4792 ExpectDroppedFrame();
4793
mflodmancc3d4422017-08-03 08:27:51 -07004794 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004795}
ilnik6b826ef2017-06-16 06:53:48 -07004796
Niels Möller4db138e2018-04-19 09:04:13 +02004797TEST_F(VideoStreamEncoderTest,
4798 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4799 const int kFrameWidth = 1280;
4800 const int kFrameHeight = 720;
4801 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004802 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004803 DataRate::BitsPerSec(kTargetBitrateBps),
4804 DataRate::BitsPerSec(kTargetBitrateBps),
4805 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004806 video_source_.IncomingCapturedFrame(
4807 CreateFrame(1, kFrameWidth, kFrameHeight));
4808 WaitForEncodedFrame(1);
4809 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4810 .low_encode_usage_threshold_percent,
4811 default_options.low_encode_usage_threshold_percent);
4812 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4813 .high_encode_usage_threshold_percent,
4814 default_options.high_encode_usage_threshold_percent);
4815 video_stream_encoder_->Stop();
4816}
4817
4818TEST_F(VideoStreamEncoderTest,
4819 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4820 const int kFrameWidth = 1280;
4821 const int kFrameHeight = 720;
4822 CpuOveruseOptions hardware_options;
4823 hardware_options.low_encode_usage_threshold_percent = 150;
4824 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004825 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004826
Erik Språng4c6ca302019-04-08 15:14:01 +02004827 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004828 DataRate::BitsPerSec(kTargetBitrateBps),
4829 DataRate::BitsPerSec(kTargetBitrateBps),
4830 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004831 video_source_.IncomingCapturedFrame(
4832 CreateFrame(1, kFrameWidth, kFrameHeight));
4833 WaitForEncodedFrame(1);
4834 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4835 .low_encode_usage_threshold_percent,
4836 hardware_options.low_encode_usage_threshold_percent);
4837 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4838 .high_encode_usage_threshold_percent,
4839 hardware_options.high_encode_usage_threshold_percent);
4840 video_stream_encoder_->Stop();
4841}
4842
Niels Möller6bb5ab92019-01-11 11:11:10 +01004843TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4844 const int kFrameWidth = 320;
4845 const int kFrameHeight = 240;
4846 const int kFps = 30;
4847 const int kTargetBitrateBps = 120000;
4848 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4849
Erik Språng4c6ca302019-04-08 15:14:01 +02004850 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004851 DataRate::BitsPerSec(kTargetBitrateBps),
4852 DataRate::BitsPerSec(kTargetBitrateBps),
4853 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004854
4855 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4856 max_framerate_ = kFps;
4857
4858 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4859 fake_encoder_.SimulateOvershoot(1.0);
4860 int num_dropped = 0;
4861 for (int i = 0; i < kNumFramesInRun; ++i) {
4862 video_source_.IncomingCapturedFrame(
4863 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4864 // Wait up to two frame durations for a frame to arrive.
4865 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4866 ++num_dropped;
4867 }
4868 timestamp_ms += 1000 / kFps;
4869 }
4870
Erik Språnga8d48ab2019-02-08 14:17:40 +01004871 // Framerate should be measured to be near the expected target rate.
4872 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4873
4874 // Frame drops should be within 5% of expected 0%.
4875 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004876
4877 // Make encoder produce frames at double the expected bitrate during 3 seconds
4878 // of video, verify number of drops. Rate needs to be slightly changed in
4879 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004880 double overshoot_factor = 2.0;
4881 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4882 // With bitrate adjuster, when need to overshoot even more to trigger
4883 // frame dropping.
4884 overshoot_factor *= 2;
4885 }
4886 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004887 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004888 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4889 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4890 DataRate::BitsPerSec(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004891 num_dropped = 0;
4892 for (int i = 0; i < kNumFramesInRun; ++i) {
4893 video_source_.IncomingCapturedFrame(
4894 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4895 // Wait up to two frame durations for a frame to arrive.
4896 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4897 ++num_dropped;
4898 }
4899 timestamp_ms += 1000 / kFps;
4900 }
4901
Erik Språng4c6ca302019-04-08 15:14:01 +02004902 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004903 DataRate::BitsPerSec(kTargetBitrateBps),
4904 DataRate::BitsPerSec(kTargetBitrateBps),
4905 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004906
4907 // Target framerate should be still be near the expected target, despite
4908 // the frame drops.
4909 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4910
4911 // Frame drops should be within 5% of expected 50%.
4912 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004913
4914 video_stream_encoder_->Stop();
4915}
4916
4917TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4918 const int kFrameWidth = 320;
4919 const int kFrameHeight = 240;
4920 const int kActualInputFps = 24;
4921 const int kTargetBitrateBps = 120000;
4922
4923 ASSERT_GT(max_framerate_, kActualInputFps);
4924
4925 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4926 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004927 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004928 DataRate::BitsPerSec(kTargetBitrateBps),
4929 DataRate::BitsPerSec(kTargetBitrateBps),
4930 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004931
4932 // Insert 3 seconds of video, with an input fps lower than configured max.
4933 for (int i = 0; i < kActualInputFps * 3; ++i) {
4934 video_source_.IncomingCapturedFrame(
4935 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4936 // Wait up to two frame durations for a frame to arrive.
4937 WaitForEncodedFrame(timestamp_ms);
4938 timestamp_ms += 1000 / kActualInputFps;
4939 }
4940
4941 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4942
4943 video_stream_encoder_->Stop();
4944}
4945
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004946TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4947 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004948 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004949 DataRate::BitsPerSec(kTargetBitrateBps),
4950 DataRate::BitsPerSec(kTargetBitrateBps),
4951 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004952
4953 fake_encoder_.BlockNextEncode();
4954 video_source_.IncomingCapturedFrame(
4955 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4956 WaitForEncodedFrame(1);
4957 // On the very first frame full update should be forced.
4958 rect = fake_encoder_.GetLastUpdateRect();
4959 EXPECT_EQ(rect.offset_x, 0);
4960 EXPECT_EQ(rect.offset_y, 0);
4961 EXPECT_EQ(rect.height, codec_height_);
4962 EXPECT_EQ(rect.width, codec_width_);
4963 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4964 // call to ContinueEncode.
4965 video_source_.IncomingCapturedFrame(
4966 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4967 ExpectDroppedFrame();
4968 video_source_.IncomingCapturedFrame(
4969 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4970 ExpectDroppedFrame();
4971 fake_encoder_.ContinueEncode();
4972 WaitForEncodedFrame(3);
4973 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4974 rect = fake_encoder_.GetLastUpdateRect();
4975 EXPECT_EQ(rect.offset_x, 1);
4976 EXPECT_EQ(rect.offset_y, 0);
4977 EXPECT_EQ(rect.width, 10);
4978 EXPECT_EQ(rect.height, 1);
4979
4980 video_source_.IncomingCapturedFrame(
4981 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4982 WaitForEncodedFrame(4);
4983 // Previous frame was encoded, so no accumulation should happen.
4984 rect = fake_encoder_.GetLastUpdateRect();
4985 EXPECT_EQ(rect.offset_x, 0);
4986 EXPECT_EQ(rect.offset_y, 0);
4987 EXPECT_EQ(rect.width, 1);
4988 EXPECT_EQ(rect.height, 1);
4989
4990 video_stream_encoder_->Stop();
4991}
4992
Erik Språngd7329ca2019-02-21 21:19:53 +01004993TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004994 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004995 DataRate::BitsPerSec(kTargetBitrateBps),
4996 DataRate::BitsPerSec(kTargetBitrateBps),
4997 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004998
4999 // First frame is always keyframe.
5000 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
5001 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01005002 EXPECT_THAT(
5003 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005004 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005005
5006 // Insert delta frame.
5007 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
5008 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01005009 EXPECT_THAT(
5010 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005011 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005012
5013 // Request next frame be a key-frame.
5014 video_stream_encoder_->SendKeyFrame();
5015 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
5016 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01005017 EXPECT_THAT(
5018 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005019 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005020
5021 video_stream_encoder_->Stop();
5022}
5023
5024TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
5025 // Setup simulcast with three streams.
5026 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01005027 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005028 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
5029 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
5030 DataRate::BitsPerSec(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01005031 // Wait for all three layers before triggering event.
5032 sink_.SetNumExpectedLayers(3);
5033
5034 // First frame is always keyframe.
5035 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
5036 WaitForEncodedFrame(1);
5037 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005038 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
5039 VideoFrameType::kVideoFrameKey,
5040 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005041
5042 // Insert delta frame.
5043 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
5044 WaitForEncodedFrame(2);
5045 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005046 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
5047 VideoFrameType::kVideoFrameDelta,
5048 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005049
5050 // Request next frame be a key-frame.
5051 // Only first stream is configured to produce key-frame.
5052 video_stream_encoder_->SendKeyFrame();
5053 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
5054 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02005055
5056 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
5057 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01005058 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005059 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02005060 VideoFrameType::kVideoFrameKey,
5061 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005062
5063 video_stream_encoder_->Stop();
5064}
5065
5066TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
5067 // Configure internal source factory and setup test again.
5068 encoder_factory_.SetHasInternalSource(true);
5069 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005070 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005071 DataRate::BitsPerSec(kTargetBitrateBps),
5072 DataRate::BitsPerSec(kTargetBitrateBps),
5073 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01005074
5075 // Call encoder directly, simulating internal source where encoded frame
5076 // callback in VideoStreamEncoder is called despite no OnFrame().
5077 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
5078 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005079 EXPECT_THAT(
5080 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005081 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005082
Niels Möller8f7ce222019-03-21 15:43:58 +01005083 const std::vector<VideoFrameType> kDeltaFrame = {
5084 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01005085 // Need to set timestamp manually since manually for injected frame.
5086 VideoFrame frame = CreateFrame(101, nullptr);
5087 frame.set_timestamp(101);
5088 fake_encoder_.InjectFrame(frame, false);
5089 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005090 EXPECT_THAT(
5091 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005092 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005093
5094 // Request key-frame. The forces a dummy frame down into the encoder.
5095 fake_encoder_.ExpectNullFrame();
5096 video_stream_encoder_->SendKeyFrame();
5097 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005098 EXPECT_THAT(
5099 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005100 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005101
5102 video_stream_encoder_->Stop();
5103}
Erik Språngb7cb7b52019-02-26 15:52:33 +01005104
5105TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
5106 // Configure internal source factory and setup test again.
5107 encoder_factory_.SetHasInternalSource(true);
5108 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005109 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005110 DataRate::BitsPerSec(kTargetBitrateBps),
5111 DataRate::BitsPerSec(kTargetBitrateBps),
5112 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01005113
5114 int64_t timestamp = 1;
5115 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02005116 image.SetEncodedData(
5117 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01005118 image.capture_time_ms_ = ++timestamp;
5119 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
5120 const int64_t kEncodeFinishDelayMs = 10;
5121 image.timing_.encode_start_ms = timestamp;
5122 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
5123 fake_encoder_.InjectEncodedImage(image);
5124 // Wait for frame without incrementing clock.
5125 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5126 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
5127 // capture timestamp should be kEncodeFinishDelayMs in the past.
5128 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
5129 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
5130 kEncodeFinishDelayMs);
5131
5132 video_stream_encoder_->Stop();
5133}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02005134
5135TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
5136 // Configure internal source factory and setup test again.
5137 encoder_factory_.SetHasInternalSource(true);
5138 ResetEncoder("H264", 1, 1, 1, false);
5139
5140 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
5141 image._frameType = VideoFrameType::kVideoFrameKey;
5142
5143 CodecSpecificInfo codec_specific_info;
5144 codec_specific_info.codecType = kVideoCodecH264;
5145
5146 RTPFragmentationHeader fragmentation;
5147 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5148 fragmentation.fragmentationOffset[0] = 4;
5149 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
5150
5151 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5152 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5153
5154 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5155 testing::ElementsAreArray(optimal_sps));
5156 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5157 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5158 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5159 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5160
5161 video_stream_encoder_->Stop();
5162}
5163
5164TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
5165 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
5166 0x00, 0x00, 0x03, 0x03, 0xF4,
5167 0x05, 0x03, 0xC7, 0xC0};
5168
5169 // Configure internal source factory and setup test again.
5170 encoder_factory_.SetHasInternalSource(true);
5171 ResetEncoder("H264", 1, 1, 1, false);
5172
5173 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
5174 image._frameType = VideoFrameType::kVideoFrameKey;
5175
5176 CodecSpecificInfo codec_specific_info;
5177 codec_specific_info.codecType = kVideoCodecH264;
5178
5179 RTPFragmentationHeader fragmentation;
5180 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5181 fragmentation.fragmentationOffset[0] = 4;
5182 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
5183
5184 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5185 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5186
5187 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5188 testing::ElementsAreArray(optimal_sps));
5189 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5190 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5191 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5192 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5193
5194 video_stream_encoder_->Stop();
5195}
5196
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005197TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5198 const int kFrameWidth = 1280;
5199 const int kFrameHeight = 720;
5200 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5201
5202 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005203 DataRate::BitsPerSec(kTargetBitrateBps),
5204 DataRate::BitsPerSec(kTargetBitrateBps),
5205 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005206 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5207
5208 // Insert a first video frame. It should be dropped because of downscale in
5209 // resolution.
5210 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5211 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5212 frame.set_rotation(kVideoRotation_270);
5213 video_source_.IncomingCapturedFrame(frame);
5214
5215 ExpectDroppedFrame();
5216
5217 // Second frame is downscaled.
5218 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5219 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5220 frame.set_rotation(kVideoRotation_90);
5221 video_source_.IncomingCapturedFrame(frame);
5222
5223 WaitForEncodedFrame(timestamp_ms);
5224 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5225
5226 // Insert another frame, also downscaled.
5227 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5228 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5229 frame.set_rotation(kVideoRotation_180);
5230 video_source_.IncomingCapturedFrame(frame);
5231
5232 WaitForEncodedFrame(timestamp_ms);
5233 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5234
5235 video_stream_encoder_->Stop();
5236}
5237
Erik Språng5056af02019-09-02 15:53:11 +02005238TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5239 const int kFrameWidth = 320;
5240 const int kFrameHeight = 180;
5241
5242 // Initial rate.
5243 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005244 /*target_bitrate=*/DataRate::KilobitsPerSec(300),
5245 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(300),
5246 /*link_allocation=*/DataRate::KilobitsPerSec(300),
Erik Språng5056af02019-09-02 15:53:11 +02005247 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005248 /*rtt_ms=*/0,
5249 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005250
5251 // Insert a first video frame so that encoder gets configured.
5252 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5253 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5254 frame.set_rotation(kVideoRotation_270);
5255 video_source_.IncomingCapturedFrame(frame);
5256 WaitForEncodedFrame(timestamp_ms);
5257
5258 // Set a target rate below the minimum allowed by the codec settings.
5259 VideoCodec codec_config = fake_encoder_.codec_config();
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005260 DataRate min_rate = DataRate::KilobitsPerSec(codec_config.minBitrate);
5261 DataRate target_rate = min_rate - DataRate::KilobitsPerSec(1);
Erik Språng5056af02019-09-02 15:53:11 +02005262 video_stream_encoder_->OnBitrateUpdated(
5263 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005264 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005265 /*link_allocation=*/target_rate,
5266 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005267 /*rtt_ms=*/0,
5268 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005269 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5270
5271 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5272 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5273 ASSERT_TRUE(rate_settings.has_value());
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005274 DataRate allocation_sum =
5275 DataRate::BitsPerSec(rate_settings->bitrate.get_sum_bps());
Erik Språng5056af02019-09-02 15:53:11 +02005276 EXPECT_EQ(min_rate, allocation_sum);
5277 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5278
5279 video_stream_encoder_->Stop();
5280}
5281
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005282TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005283 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005284 DataRate::BitsPerSec(kTargetBitrateBps),
5285 DataRate::BitsPerSec(kTargetBitrateBps),
5286 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005287 // Capture a frame and wait for it to synchronize with the encoder thread.
5288 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5289 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5290 WaitForEncodedFrame(1);
5291
5292 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5293 ASSERT_TRUE(prev_rate_settings.has_value());
5294 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5295 kDefaultFramerate);
5296
5297 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5298 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5299 timestamp_ms += 1000 / kDefaultFramerate;
5300 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5301 WaitForEncodedFrame(timestamp_ms);
5302 }
5303 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5304 kDefaultFramerate);
5305 // Capture larger frame to trigger a reconfigure.
5306 codec_height_ *= 2;
5307 codec_width_ *= 2;
5308 timestamp_ms += 1000 / kDefaultFramerate;
5309 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5310 WaitForEncodedFrame(timestamp_ms);
5311
5312 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5313 auto current_rate_settings =
5314 fake_encoder_.GetAndResetLastRateControlSettings();
5315 // Ensure we have actually reconfigured twice
5316 // The rate settings should have been set again even though
5317 // they haven't changed.
5318 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005319 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005320
5321 video_stream_encoder_->Stop();
5322}
5323
philipeld9cc8c02019-09-16 14:53:40 +02005324struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5325 MOCK_METHOD0(RequestEncoderFallback, void());
5326 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
philipel9b058032020-02-10 11:30:00 +01005327 MOCK_METHOD1(RequestEncoderSwitch,
5328 void(const webrtc::SdpVideoFormat& format));
philipeld9cc8c02019-09-16 14:53:40 +02005329};
5330
5331TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5332 constexpr int kDontCare = 100;
5333
5334 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5335 video_send_config_.encoder_settings.encoder_switch_request_callback =
5336 &switch_callback;
5337 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5338 encoder_config.codec_type = kVideoCodecVP8;
5339 webrtc::test::ScopedFieldTrials field_trial(
5340 "WebRTC-NetworkCondition-EncoderSwitch/"
5341 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5342 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5343
5344 // Reset encoder for new configuration to take effect.
5345 ConfigureEncoder(std::move(encoder_config));
5346
5347 // Send one frame to trigger ReconfigureEncoder.
5348 video_source_.IncomingCapturedFrame(
5349 CreateFrame(kDontCare, kDontCare, kDontCare));
5350
5351 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005352 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5353 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005354 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005355 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005356
5357 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005358 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5359 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5360 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipeld9cc8c02019-09-16 14:53:40 +02005361 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005362 /*rtt_ms=*/0,
5363 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005364
5365 video_stream_encoder_->Stop();
5366}
5367
Mirta Dvornicic5ed40cf2020-02-21 16:35:51 +01005368TEST_F(VideoStreamEncoderTest, VideoSuspendedNoEncoderSwitch) {
5369 constexpr int kDontCare = 100;
5370
5371 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5372 video_send_config_.encoder_settings.encoder_switch_request_callback =
5373 &switch_callback;
5374 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5375 encoder_config.codec_type = kVideoCodecVP8;
5376 webrtc::test::ScopedFieldTrials field_trial(
5377 "WebRTC-NetworkCondition-EncoderSwitch/"
5378 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5379 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5380
5381 // Reset encoder for new configuration to take effect.
5382 ConfigureEncoder(std::move(encoder_config));
5383
5384 // Send one frame to trigger ReconfigureEncoder.
5385 video_source_.IncomingCapturedFrame(
5386 CreateFrame(kDontCare, kDontCare, kDontCare));
5387
5388 using Config = EncoderSwitchRequestCallback::Config;
5389 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(_)))
5390 .Times(0);
5391
5392 video_stream_encoder_->OnBitrateUpdated(
5393 /*target_bitrate=*/DataRate::KilobitsPerSec(0),
5394 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(0),
5395 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
5396 /*fraction_lost=*/0,
5397 /*rtt_ms=*/0,
5398 /*cwnd_reduce_ratio=*/0);
5399
5400 video_stream_encoder_->Stop();
5401}
5402
philipeld9cc8c02019-09-16 14:53:40 +02005403TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5404 constexpr int kSufficientBitrateToNotDrop = 1000;
5405 constexpr int kHighRes = 500;
5406 constexpr int kLowRes = 100;
5407
5408 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5409 video_send_config_.encoder_settings.encoder_switch_request_callback =
5410 &switch_callback;
5411 webrtc::test::ScopedFieldTrials field_trial(
5412 "WebRTC-NetworkCondition-EncoderSwitch/"
5413 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5414 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5415 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5416 encoder_config.codec_type = kVideoCodecH264;
5417
5418 // Reset encoder for new configuration to take effect.
5419 ConfigureEncoder(std::move(encoder_config));
5420
5421 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5422 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5423 // not fail.
5424 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005425 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5426 /*stable_target_bitrate=*/
5427 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5428 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipeld9cc8c02019-09-16 14:53:40 +02005429 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005430 /*rtt_ms=*/0,
5431 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005432
5433 // Send one frame to trigger ReconfigureEncoder.
5434 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5435 WaitForEncodedFrame(1);
5436
5437 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005438 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5439 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005440 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005441 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005442
5443 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5444 WaitForEncodedFrame(2);
5445
5446 video_stream_encoder_->Stop();
5447}
5448
philipel9b058032020-02-10 11:30:00 +01005449TEST_F(VideoStreamEncoderTest, EncoderSelectorCurrentEncoderIsSignaled) {
5450 constexpr int kDontCare = 100;
5451 StrictMock<MockEncoderSelector> encoder_selector;
5452 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5453 &fake_encoder_, &encoder_selector);
5454 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5455
5456 // Reset encoder for new configuration to take effect.
5457 ConfigureEncoder(video_encoder_config_.Copy());
5458
5459 EXPECT_CALL(encoder_selector, OnCurrentEncoder(_));
5460
5461 video_source_.IncomingCapturedFrame(
5462 CreateFrame(kDontCare, kDontCare, kDontCare));
5463 video_stream_encoder_->Stop();
5464
5465 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5466 // to it's factory, so in order for the encoder instance in the
5467 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5468 // reset the |video_stream_encoder_| here.
5469 video_stream_encoder_.reset();
5470}
5471
5472TEST_F(VideoStreamEncoderTest, EncoderSelectorBitrateSwitch) {
5473 constexpr int kDontCare = 100;
5474
5475 NiceMock<MockEncoderSelector> encoder_selector;
5476 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5477 video_send_config_.encoder_settings.encoder_switch_request_callback =
5478 &switch_callback;
5479 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5480 &fake_encoder_, &encoder_selector);
5481 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5482
5483 // Reset encoder for new configuration to take effect.
5484 ConfigureEncoder(video_encoder_config_.Copy());
5485
Mirta Dvornicic4f34d782020-02-26 13:01:19 +01005486 ON_CALL(encoder_selector, OnAvailableBitrate(_))
philipel9b058032020-02-10 11:30:00 +01005487 .WillByDefault(Return(SdpVideoFormat("AV1")));
5488 EXPECT_CALL(switch_callback,
5489 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(
5490 Field(&SdpVideoFormat::name, "AV1"))));
5491
5492 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005493 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5494 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5495 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipel9b058032020-02-10 11:30:00 +01005496 /*fraction_lost=*/0,
5497 /*rtt_ms=*/0,
5498 /*cwnd_reduce_ratio=*/0);
5499
5500 video_stream_encoder_->Stop();
5501}
5502
5503TEST_F(VideoStreamEncoderTest, EncoderSelectorBrokenEncoderSwitch) {
5504 constexpr int kSufficientBitrateToNotDrop = 1000;
5505 constexpr int kDontCare = 100;
5506
5507 NiceMock<MockVideoEncoder> video_encoder;
5508 NiceMock<MockEncoderSelector> encoder_selector;
5509 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5510 video_send_config_.encoder_settings.encoder_switch_request_callback =
5511 &switch_callback;
5512 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5513 &video_encoder, &encoder_selector);
5514 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5515
5516 // Reset encoder for new configuration to take effect.
5517 ConfigureEncoder(video_encoder_config_.Copy());
5518
5519 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5520 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5521 // not fail.
5522 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005523 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5524 /*stable_target_bitrate=*/
5525 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5526 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipel9b058032020-02-10 11:30:00 +01005527 /*fraction_lost=*/0,
5528 /*rtt_ms=*/0,
5529 /*cwnd_reduce_ratio=*/0);
5530
5531 ON_CALL(video_encoder, Encode(_, _))
5532 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ENCODER_FAILURE));
5533 ON_CALL(encoder_selector, OnEncoderBroken())
5534 .WillByDefault(Return(SdpVideoFormat("AV2")));
5535
5536 rtc::Event encode_attempted;
5537 EXPECT_CALL(switch_callback,
5538 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(_)))
5539 .WillOnce([&encode_attempted](const SdpVideoFormat& format) {
5540 EXPECT_EQ(format.name, "AV2");
5541 encode_attempted.Set();
5542 });
5543
5544 video_source_.IncomingCapturedFrame(CreateFrame(1, kDontCare, kDontCare));
5545 encode_attempted.Wait(3000);
5546
5547 video_stream_encoder_->Stop();
5548
5549 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5550 // to it's factory, so in order for the encoder instance in the
5551 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5552 // reset the |video_stream_encoder_| here.
5553 video_stream_encoder_.reset();
5554}
5555
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005556TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005557 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005558 const int kFrameWidth = 320;
5559 const int kFrameHeight = 180;
5560
5561 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005562 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005563 video_stream_encoder_->OnBitrateUpdated(
5564 /*target_bitrate=*/rate,
5565 /*stable_target_bitrate=*/rate,
5566 /*link_allocation=*/rate,
5567 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005568 /*rtt_ms=*/0,
5569 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005570
5571 // Insert a first video frame so that encoder gets configured.
5572 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5573 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5574 frame.set_rotation(kVideoRotation_270);
5575 video_source_.IncomingCapturedFrame(frame);
5576 WaitForEncodedFrame(timestamp_ms);
5577 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5578
5579 // Change of target bitrate propagates to the encoder.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005580 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005581 video_stream_encoder_->OnBitrateUpdated(
5582 /*target_bitrate=*/new_stable_rate,
5583 /*stable_target_bitrate=*/new_stable_rate,
5584 /*link_allocation=*/rate,
5585 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005586 /*rtt_ms=*/0,
5587 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005588 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5589 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5590 video_stream_encoder_->Stop();
5591}
5592
5593TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005594 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005595 const int kFrameWidth = 320;
5596 const int kFrameHeight = 180;
5597
5598 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005599 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005600 video_stream_encoder_->OnBitrateUpdated(
5601 /*target_bitrate=*/rate,
5602 /*stable_target_bitrate=*/rate,
5603 /*link_allocation=*/rate,
5604 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005605 /*rtt_ms=*/0,
5606 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005607
5608 // Insert a first video frame so that encoder gets configured.
5609 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5610 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5611 frame.set_rotation(kVideoRotation_270);
5612 video_source_.IncomingCapturedFrame(frame);
5613 WaitForEncodedFrame(timestamp_ms);
5614 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5615
5616 // Set a higher target rate without changing the link_allocation. Should not
5617 // reset encoder's rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005618 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005619 video_stream_encoder_->OnBitrateUpdated(
5620 /*target_bitrate=*/rate,
5621 /*stable_target_bitrate=*/new_stable_rate,
5622 /*link_allocation=*/rate,
5623 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005624 /*rtt_ms=*/0,
5625 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005626 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5627 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5628 video_stream_encoder_->Stop();
5629}
5630
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005631TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5632 test::ScopedFieldTrials field_trials(
5633 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5634 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5635 const int kFramerateFps = 30;
5636 const int kWidth = 1920;
5637 const int kHeight = 1080;
5638 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5639 // Works on screenshare mode.
5640 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5641 // We rely on the automatic resolution adaptation, but we handle framerate
5642 // adaptation manually by mocking the stats proxy.
5643 video_source_.set_adaptation_enabled(true);
5644
5645 // BALANCED degradation preference is required for this feature.
5646 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005647 DataRate::BitsPerSec(kTargetBitrateBps),
5648 DataRate::BitsPerSec(kTargetBitrateBps),
5649 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005650 video_stream_encoder_->SetSource(&video_source_,
5651 webrtc::DegradationPreference::BALANCED);
5652 VerifyNoLimitation(video_source_.sink_wants());
5653
5654 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5655 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5656
5657 // Pass enough frames with the full update to trigger animation detection.
5658 for (int i = 0; i < kNumFrames; ++i) {
5659 int64_t timestamp_ms =
5660 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5661 frame.set_ntp_time_ms(timestamp_ms);
5662 frame.set_timestamp_us(timestamp_ms * 1000);
5663 video_source_.IncomingCapturedFrame(frame);
5664 WaitForEncodedFrame(timestamp_ms);
5665 }
5666
5667 // Resolution should be limited.
5668 rtc::VideoSinkWants expected;
5669 expected.max_framerate_fps = kFramerateFps;
5670 expected.max_pixel_count = 1280 * 720 + 1;
5671 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5672
5673 // Pass one frame with no known update.
5674 // Resolution cap should be removed immediately.
5675 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5676 frame.set_ntp_time_ms(timestamp_ms);
5677 frame.set_timestamp_us(timestamp_ms * 1000);
5678 frame.clear_update_rect();
5679
5680 video_source_.IncomingCapturedFrame(frame);
5681 WaitForEncodedFrame(timestamp_ms);
5682
5683 // Resolution should be unlimited now.
5684 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5685
5686 video_stream_encoder_->Stop();
5687}
5688
perkj26091b12016-09-01 01:17:40 -07005689} // namespace webrtc