blob: 44ac6e1390c56234aac26eb626bd45fef28276eb [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"
Evan Shrubsolece0a11d2020-04-16 11:36:55 +020024#include "api/video/video_adaptation_reason.h"
Erik Språngf93eda12019-01-16 17:10:57 +010025#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020026#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020027#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010028#include "api/video_codecs/vp8_temporal_layers_factory.h"
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +010029#include "call/adaptation/test/fake_resource.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020030#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070031#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020033#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010034#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010038#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020039#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "system_wrappers/include/sleep.h"
41#include "test/encoder_settings.h"
42#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020043#include "test/field_trial.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010044#include "test/frame_forwarder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "test/gmock.h"
46#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020047#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070049
50namespace webrtc {
51
Evan Shrubsolece0a11d2020-04-16 11:36:55 +020052using ScaleReason = VideoAdaptationReason;
sprang57c2fff2017-01-16 06:24:02 -080053using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020054using ::testing::AllOf;
55using ::testing::Field;
philipel9b058032020-02-10 11:30:00 +010056using ::testing::Matcher;
57using ::testing::NiceMock;
58using ::testing::Return;
philipeld9cc8c02019-09-16 14:53:40 +020059using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080060
perkj803d97f2016-11-01 11:45:46 -070061namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020062const int kMinPixelsPerFrame = 320 * 180;
Åsa Perssone644a032019-11-08 15:56:00 +010063const int kQpLow = 1;
64const int kQpHigh = 2;
Åsa Persson8c1bf952018-09-13 10:42:19 +020065const int kMinFramerateFps = 2;
66const int kMinBalancedFramerateFps = 7;
67const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080068const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010069const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020070const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010071const uint32_t kSimulcastTargetBitrateBps = 3150000;
72const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080073const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070074const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020075const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020076const int64_t kProcessIntervalMs = 1000;
Sergey Silkin41c650b2019-10-14 13:12:19 +020077const VideoEncoder::ResolutionBitrateLimits
78 kEncoderBitrateLimits540p(960 * 540, 100 * 1000, 100 * 1000, 2000 * 1000);
79const VideoEncoder::ResolutionBitrateLimits
80 kEncoderBitrateLimits720p(1280 * 720, 200 * 1000, 200 * 1000, 4000 * 1000);
asapersson5f7226f2016-11-25 04:37:00 -080081
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020082uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
83 0x00, 0x00, 0x03, 0x03, 0xF4,
84 0x05, 0x03, 0xC7, 0xE0, 0x1B,
85 0x41, 0x10, 0x8D, 0x00};
86
perkj803d97f2016-11-01 11:45:46 -070087class TestBuffer : public webrtc::I420Buffer {
88 public:
89 TestBuffer(rtc::Event* event, int width, int height)
90 : I420Buffer(width, height), event_(event) {}
91
92 private:
93 friend class rtc::RefCountedObject<TestBuffer>;
94 ~TestBuffer() override {
95 if (event_)
96 event_->Set();
97 }
98 rtc::Event* const event_;
99};
100
Noah Richards51db4212019-06-12 06:59:12 -0700101// A fake native buffer that can't be converted to I420.
102class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
103 public:
104 FakeNativeBuffer(rtc::Event* event, int width, int height)
105 : event_(event), width_(width), height_(height) {}
106 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
107 int width() const override { return width_; }
108 int height() const override { return height_; }
109 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
110 return nullptr;
111 }
112
113 private:
114 friend class rtc::RefCountedObject<FakeNativeBuffer>;
115 ~FakeNativeBuffer() override {
116 if (event_)
117 event_->Set();
118 }
119 rtc::Event* const event_;
120 const int width_;
121 const int height_;
122};
123
Niels Möller7dc26b72017-12-06 10:27:48 +0100124class CpuOveruseDetectorProxy : public OveruseFrameDetector {
125 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200126 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
127 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100128 last_target_framerate_fps_(-1) {}
129 virtual ~CpuOveruseDetectorProxy() {}
130
131 void OnTargetFramerateUpdated(int framerate_fps) override {
132 rtc::CritScope cs(&lock_);
133 last_target_framerate_fps_ = framerate_fps;
134 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
135 }
136
137 int GetLastTargetFramerate() {
138 rtc::CritScope cs(&lock_);
139 return last_target_framerate_fps_;
140 }
141
Niels Möller4db138e2018-04-19 09:04:13 +0200142 CpuOveruseOptions GetOptions() { return options_; }
143
Niels Möller7dc26b72017-12-06 10:27:48 +0100144 private:
145 rtc::CriticalSection lock_;
146 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
147};
148
mflodmancc3d4422017-08-03 08:27:51 -0700149class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700150 public:
Niels Möller213618e2018-07-24 09:29:58 +0200151 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200152 const VideoStreamEncoderSettings& settings,
153 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100154 : VideoStreamEncoder(Clock::GetRealTimeClock(),
155 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200156 stats_proxy,
157 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200158 std::unique_ptr<OveruseFrameDetector>(
159 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100160 new CpuOveruseDetectorProxy(stats_proxy)),
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100161 task_queue_factory),
162 fake_cpu_resource_(
163 std::make_unique<FakeResource>(ResourceUsageState::kStable,
164 "FakeResource[CPU]")),
165 fake_quality_resource_(
166 std::make_unique<FakeResource>(ResourceUsageState::kStable,
167 "FakeResource[QP]")) {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200168 InjectAdaptationResource(fake_quality_resource_.get(),
169 VideoAdaptationReason::kQuality);
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100170 InjectAdaptationResource(fake_cpu_resource_.get(),
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200171 VideoAdaptationReason::kCpu);
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100172 }
perkj803d97f2016-11-01 11:45:46 -0700173
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200174 void PostTaskAndWait(bool down, VideoAdaptationReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200175 PostTaskAndWait(down, reason, /*expected_results=*/true);
176 }
177
Henrik Boströmb08882b2020-01-07 10:11:17 +0100178 void PostTaskAndWait(bool down,
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200179 VideoAdaptationReason reason,
Henrik Boströmb08882b2020-01-07 10:11:17 +0100180 bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100181 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200182 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100183 ResourceUsageState usage_state =
184 down ? ResourceUsageState::kOveruse : ResourceUsageState::kUnderuse;
185
186 FakeResource* resource = nullptr;
187 switch (reason) {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200188 case VideoAdaptationReason::kQuality:
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100189 resource = fake_quality_resource_.get();
190 break;
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200191 case VideoAdaptationReason::kCpu:
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100192 resource = fake_cpu_resource_.get();
193 break;
194 default:
195 RTC_NOTREACHED();
196 }
197
198 resource->set_usage_state(usage_state);
199 if (!expected_results) {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200200 ASSERT_EQ(VideoAdaptationReason::kQuality, reason)
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100201 << "We can only assert adaptation result for quality resources";
202 EXPECT_EQ(
203 ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency,
204 resource->last_response());
205 } else {
206 EXPECT_EQ(ResourceListenerResponse::kNothing,
207 resource->last_response());
208 }
209
perkj803d97f2016-11-01 11:45:46 -0700210 event.Set();
211 });
perkj070ba852017-02-16 15:46:27 -0800212 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700213 }
214
kthelgason2fc52542017-03-03 00:24:41 -0800215 // This is used as a synchronisation mechanism, to make sure that the
216 // encoder queue is not blocked before we start sending it frames.
217 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100218 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200219 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800220 ASSERT_TRUE(event.Wait(5000));
221 }
222
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200223 void TriggerCpuOveruse() {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200224 PostTaskAndWait(/*down=*/true, VideoAdaptationReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200225 }
kthelgason876222f2016-11-29 01:44:11 -0800226
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200227 void TriggerCpuNormalUsage() {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200228 PostTaskAndWait(/*down=*/false, VideoAdaptationReason::kCpu);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200229 }
kthelgason876222f2016-11-29 01:44:11 -0800230
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200231 void TriggerQualityLow() {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200232 PostTaskAndWait(/*down=*/true, VideoAdaptationReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200233 }
kthelgason876222f2016-11-29 01:44:11 -0800234
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200235 void TriggerQualityLowExpectFalse() {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200236 PostTaskAndWait(/*down=*/true, VideoAdaptationReason::kQuality,
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200237 /*expected_results=*/false);
238 }
239
240 void TriggerQualityHigh() {
Evan Shrubsolece0a11d2020-04-16 11:36:55 +0200241 PostTaskAndWait(/*down=*/false, VideoAdaptationReason::kQuality);
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200242 }
sprangfda496a2017-06-15 04:21:07 -0700243
Niels Möller7dc26b72017-12-06 10:27:48 +0100244 CpuOveruseDetectorProxy* overuse_detector_proxy_;
Evan Shrubsoleaa6fbc12020-02-25 16:26:01 +0100245 std::unique_ptr<FakeResource> fake_cpu_resource_;
246 std::unique_ptr<FakeResource> fake_quality_resource_;
perkj803d97f2016-11-01 11:45:46 -0700247};
248
asapersson5f7226f2016-11-25 04:37:00 -0800249class VideoStreamFactory
250 : public VideoEncoderConfig::VideoStreamFactoryInterface {
251 public:
sprangfda496a2017-06-15 04:21:07 -0700252 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
253 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800254 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700255 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800256 }
257
258 private:
259 std::vector<VideoStream> CreateEncoderStreams(
260 int width,
261 int height,
262 const VideoEncoderConfig& encoder_config) override {
263 std::vector<VideoStream> streams =
264 test::CreateVideoStreams(width, height, encoder_config);
265 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100266 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700267 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800268 }
269 return streams;
270 }
sprangfda496a2017-06-15 04:21:07 -0700271
asapersson5f7226f2016-11-25 04:37:00 -0800272 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700273 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800274};
275
Noah Richards51db4212019-06-12 06:59:12 -0700276// Simulates simulcast behavior and makes highest stream resolutions divisible
277// by 4.
278class CroppingVideoStreamFactory
279 : public VideoEncoderConfig::VideoStreamFactoryInterface {
280 public:
281 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
282 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
283 EXPECT_GT(num_temporal_layers, 0u);
284 EXPECT_GT(framerate, 0);
285 }
286
287 private:
288 std::vector<VideoStream> CreateEncoderStreams(
289 int width,
290 int height,
291 const VideoEncoderConfig& encoder_config) override {
292 std::vector<VideoStream> streams = test::CreateVideoStreams(
293 width - width % 4, height - height % 4, encoder_config);
294 for (VideoStream& stream : streams) {
295 stream.num_temporal_layers = num_temporal_layers_;
296 stream.max_framerate = framerate_;
297 }
298 return streams;
299 }
300
301 const size_t num_temporal_layers_;
302 const int framerate_;
303};
304
sprangb1ca0732017-02-01 08:38:12 -0800305class AdaptingFrameForwarder : public test::FrameForwarder {
306 public:
307 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700308 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800309
310 void set_adaptation_enabled(bool enabled) {
311 rtc::CritScope cs(&crit_);
312 adaptation_enabled_ = enabled;
313 }
314
asaperssonfab67072017-04-04 05:51:49 -0700315 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800316 rtc::CritScope cs(&crit_);
317 return adaptation_enabled_;
318 }
319
asapersson09f05612017-05-15 23:40:18 -0700320 rtc::VideoSinkWants last_wants() const {
321 rtc::CritScope cs(&crit_);
322 return last_wants_;
323 }
324
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200325 absl::optional<int> last_sent_width() const { return last_width_; }
326 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800327
sprangb1ca0732017-02-01 08:38:12 -0800328 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
329 int cropped_width = 0;
330 int cropped_height = 0;
331 int out_width = 0;
332 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700333 if (adaption_enabled()) {
334 if (adapter_.AdaptFrameResolution(
335 video_frame.width(), video_frame.height(),
336 video_frame.timestamp_us() * 1000, &cropped_width,
337 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100338 VideoFrame adapted_frame =
339 VideoFrame::Builder()
340 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
341 nullptr, out_width, out_height))
342 .set_timestamp_rtp(99)
343 .set_timestamp_ms(99)
344 .set_rotation(kVideoRotation_0)
345 .build();
sprangc5d62e22017-04-02 23:53:04 -0700346 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +0100347 if (video_frame.has_update_rect()) {
348 adapted_frame.set_update_rect(
349 video_frame.update_rect().ScaleWithFrame(
350 video_frame.width(), video_frame.height(), 0, 0,
351 video_frame.width(), video_frame.height(), out_width,
352 out_height));
353 }
sprangc5d62e22017-04-02 23:53:04 -0700354 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800355 last_width_.emplace(adapted_frame.width());
356 last_height_.emplace(adapted_frame.height());
357 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200358 last_width_ = absl::nullopt;
359 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700360 }
sprangb1ca0732017-02-01 08:38:12 -0800361 } else {
362 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800363 last_width_.emplace(video_frame.width());
364 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800365 }
366 }
367
368 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
369 const rtc::VideoSinkWants& wants) override {
370 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700371 last_wants_ = sink_wants();
Rasmus Brandt287e4642019-11-15 16:56:01 +0100372 adapter_.OnSinkWants(wants);
sprangb1ca0732017-02-01 08:38:12 -0800373 test::FrameForwarder::AddOrUpdateSink(sink, wants);
374 }
sprangb1ca0732017-02-01 08:38:12 -0800375 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700376 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
377 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200378 absl::optional<int> last_width_;
379 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800380};
sprangc5d62e22017-04-02 23:53:04 -0700381
Niels Möller213618e2018-07-24 09:29:58 +0200382// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700383class MockableSendStatisticsProxy : public SendStatisticsProxy {
384 public:
385 MockableSendStatisticsProxy(Clock* clock,
386 const VideoSendStream::Config& config,
387 VideoEncoderConfig::ContentType content_type)
388 : SendStatisticsProxy(clock, config, content_type) {}
389
390 VideoSendStream::Stats GetStats() override {
391 rtc::CritScope cs(&lock_);
392 if (mock_stats_)
393 return *mock_stats_;
394 return SendStatisticsProxy::GetStats();
395 }
396
Niels Möller213618e2018-07-24 09:29:58 +0200397 int GetInputFrameRate() const override {
398 rtc::CritScope cs(&lock_);
399 if (mock_stats_)
400 return mock_stats_->input_frame_rate;
401 return SendStatisticsProxy::GetInputFrameRate();
402 }
sprangc5d62e22017-04-02 23:53:04 -0700403 void SetMockStats(const VideoSendStream::Stats& stats) {
404 rtc::CritScope cs(&lock_);
405 mock_stats_.emplace(stats);
406 }
407
408 void ResetMockStats() {
409 rtc::CritScope cs(&lock_);
410 mock_stats_.reset();
411 }
412
413 private:
414 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200415 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700416};
417
sprang4847ae62017-06-27 07:06:52 -0700418class MockBitrateObserver : public VideoBitrateAllocationObserver {
419 public:
Erik Språng566124a2018-04-23 12:32:22 +0200420 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700421};
422
philipel9b058032020-02-10 11:30:00 +0100423class MockEncoderSelector
424 : public VideoEncoderFactory::EncoderSelectorInterface {
425 public:
426 MOCK_METHOD1(OnCurrentEncoder, void(const SdpVideoFormat& format));
Mirta Dvornicic4f34d782020-02-26 13:01:19 +0100427 MOCK_METHOD1(OnAvailableBitrate,
philipel9b058032020-02-10 11:30:00 +0100428 absl::optional<SdpVideoFormat>(const DataRate& rate));
429 MOCK_METHOD0(OnEncoderBroken, absl::optional<SdpVideoFormat>());
430};
431
perkj803d97f2016-11-01 11:45:46 -0700432} // namespace
433
mflodmancc3d4422017-08-03 08:27:51 -0700434class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700435 public:
436 static const int kDefaultTimeoutMs = 30 * 1000;
437
mflodmancc3d4422017-08-03 08:27:51 -0700438 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700439 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700440 codec_width_(320),
441 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200442 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200443 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700444 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200445 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700446 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700447 Clock::GetRealTimeClock(),
448 video_send_config_,
449 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700450 sink_(&fake_encoder_) {}
451
452 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700453 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700454 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200455 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800456 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200457 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200458 video_send_config_.rtp.payload_name = "FAKE";
459 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700460
Per512ecb32016-09-23 15:52:06 +0200461 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200462 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700463 video_encoder_config.video_stream_factory =
464 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100465 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700466
467 // Framerate limit is specified by the VideoStreamFactory.
468 std::vector<VideoStream> streams =
469 video_encoder_config.video_stream_factory->CreateEncoderStreams(
470 codec_width_, codec_height_, video_encoder_config);
471 max_framerate_ = streams[0].max_framerate;
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100472 fake_clock_.SetTime(Timestamp::Micros(1234));
sprang4847ae62017-06-27 07:06:52 -0700473
Niels Möllerf1338562018-04-26 09:51:47 +0200474 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800475 }
476
Niels Möllerf1338562018-04-26 09:51:47 +0200477 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700478 if (video_stream_encoder_)
479 video_stream_encoder_->Stop();
480 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200481 stats_proxy_.get(), video_send_config_.encoder_settings,
482 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700483 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
484 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700485 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700486 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
487 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200488 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700489 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800490 }
491
492 void ResetEncoder(const std::string& payload_name,
493 size_t num_streams,
494 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700495 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700496 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200497 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800498
499 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200500 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800501 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100502 video_encoder_config.max_bitrate_bps =
503 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800504 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700505 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
506 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700507 video_encoder_config.content_type =
508 screenshare ? VideoEncoderConfig::ContentType::kScreen
509 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700510 if (payload_name == "VP9") {
511 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
512 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
513 video_encoder_config.encoder_specific_settings =
514 new rtc::RefCountedObject<
515 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
516 }
Niels Möllerf1338562018-04-26 09:51:47 +0200517 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700518 }
519
sprang57c2fff2017-01-16 06:24:02 -0800520 VideoFrame CreateFrame(int64_t ntp_time_ms,
521 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100522 VideoFrame frame =
523 VideoFrame::Builder()
524 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
525 destruction_event, codec_width_, codec_height_))
526 .set_timestamp_rtp(99)
527 .set_timestamp_ms(99)
528 .set_rotation(kVideoRotation_0)
529 .build();
sprang57c2fff2017-01-16 06:24:02 -0800530 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700531 return frame;
532 }
533
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100534 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
535 rtc::Event* destruction_event,
536 int offset_x) const {
537 VideoFrame frame =
538 VideoFrame::Builder()
539 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
540 destruction_event, codec_width_, codec_height_))
541 .set_timestamp_rtp(99)
542 .set_timestamp_ms(99)
543 .set_rotation(kVideoRotation_0)
Artem Titov5256d8b2019-12-02 10:34:12 +0100544 .set_update_rect(VideoFrame::UpdateRect{offset_x, 0, 1, 1})
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100545 .build();
546 frame.set_ntp_time_ms(ntp_time_ms);
547 return frame;
548 }
549
sprang57c2fff2017-01-16 06:24:02 -0800550 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100551 VideoFrame frame =
552 VideoFrame::Builder()
553 .set_video_frame_buffer(
554 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
555 .set_timestamp_rtp(99)
556 .set_timestamp_ms(99)
557 .set_rotation(kVideoRotation_0)
558 .build();
sprang57c2fff2017-01-16 06:24:02 -0800559 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700560 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700561 return frame;
562 }
563
Noah Richards51db4212019-06-12 06:59:12 -0700564 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
565 rtc::Event* destruction_event,
566 int width,
567 int height) const {
568 VideoFrame frame =
569 VideoFrame::Builder()
570 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
571 destruction_event, width, height))
572 .set_timestamp_rtp(99)
573 .set_timestamp_ms(99)
574 .set_rotation(kVideoRotation_0)
575 .build();
576 frame.set_ntp_time_ms(ntp_time_ms);
577 return frame;
578 }
579
580 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
581 rtc::Event* destruction_event) const {
582 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
583 codec_height_);
584 }
585
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100586 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
587 MockBitrateObserver bitrate_observer;
588 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
589
590 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
591 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200592 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100593 DataRate::BitsPerSec(kTargetBitrateBps),
594 DataRate::BitsPerSec(kTargetBitrateBps),
595 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100596
597 video_source_.IncomingCapturedFrame(
598 CreateFrame(1, codec_width_, codec_height_));
599 WaitForEncodedFrame(1);
600 }
601
asapersson02465b82017-04-10 01:12:52 -0700602 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700603 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700604 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
605 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700606 }
607
asapersson09f05612017-05-15 23:40:18 -0700608 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
609 const rtc::VideoSinkWants& wants2) {
610 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
611 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
612 }
613
Åsa Persson8c1bf952018-09-13 10:42:19 +0200614 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
615 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
616 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
617 EXPECT_FALSE(wants.target_pixel_count);
618 }
619
asapersson09f05612017-05-15 23:40:18 -0700620 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
621 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200622 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700623 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
624 EXPECT_GT(wants1.max_pixel_count, 0);
625 }
626
627 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
628 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200629 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700630 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
631 }
632
asaperssonf7e294d2017-06-13 23:25:22 -0700633 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
634 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200635 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700636 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
637 }
638
639 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
640 const rtc::VideoSinkWants& wants2) {
641 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
642 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
643 }
644
645 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
646 const rtc::VideoSinkWants& wants2) {
647 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
648 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
649 }
650
651 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
652 const rtc::VideoSinkWants& wants2) {
653 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
654 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
655 EXPECT_GT(wants1.max_pixel_count, 0);
656 }
657
658 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
659 const rtc::VideoSinkWants& wants2) {
660 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
661 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
662 }
663
asapersson09f05612017-05-15 23:40:18 -0700664 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
665 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200666 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700667 EXPECT_LT(wants.max_pixel_count, pixel_count);
668 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700669 }
670
671 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
672 EXPECT_LT(wants.max_framerate_fps, fps);
673 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
674 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700675 }
676
asaperssonf7e294d2017-06-13 23:25:22 -0700677 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
678 int expected_fps) {
679 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
680 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
681 EXPECT_FALSE(wants.target_pixel_count);
682 }
683
Jonathan Yubc771b72017-12-08 17:04:29 -0800684 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
685 int last_frame_pixels) {
686 // Balanced mode should always scale FPS to the desired range before
687 // attempting to scale resolution.
688 int fps_limit = wants.max_framerate_fps;
689 if (last_frame_pixels <= 320 * 240) {
Henrik Boström60383832020-02-28 09:03:53 +0100690 EXPECT_LE(7, fps_limit);
691 EXPECT_LE(fps_limit, 10);
Jonathan Yubc771b72017-12-08 17:04:29 -0800692 } else if (last_frame_pixels <= 480 * 270) {
Henrik Boström60383832020-02-28 09:03:53 +0100693 EXPECT_LE(10, fps_limit);
694 EXPECT_LE(fps_limit, 15);
Jonathan Yubc771b72017-12-08 17:04:29 -0800695 } else if (last_frame_pixels <= 640 * 480) {
696 EXPECT_LE(15, fps_limit);
697 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200698 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800699 }
700 }
701
sprang4847ae62017-06-27 07:06:52 -0700702 void WaitForEncodedFrame(int64_t expected_ntp_time) {
703 sink_.WaitForEncodedFrame(expected_ntp_time);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100704 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700705 }
706
707 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
708 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100709 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700710 return ok;
711 }
712
713 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
714 sink_.WaitForEncodedFrame(expected_width, expected_height);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100715 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700716 }
717
718 void ExpectDroppedFrame() {
719 sink_.ExpectDroppedFrame();
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100720 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700721 }
722
723 bool WaitForFrame(int64_t timeout_ms) {
724 bool ok = sink_.WaitForFrame(timeout_ms);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100725 fake_clock_.AdvanceTime(TimeDelta::Seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700726 return ok;
727 }
728
perkj26091b12016-09-01 01:17:40 -0700729 class TestEncoder : public test::FakeEncoder {
730 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100731 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700732
asaperssonfab67072017-04-04 05:51:49 -0700733 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800734 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700735 return config_;
736 }
737
738 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800739 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700740 block_next_encode_ = true;
741 }
742
Erik Språngaed30702018-11-05 12:57:17 +0100743 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800744 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100745 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100746 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100747 if (quality_scaling_) {
Åsa Perssone644a032019-11-08 15:56:00 +0100748 info.scaling_settings = VideoEncoder::ScalingSettings(
749 kQpLow, kQpHigh, kMinPixelsPerFrame);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100750 }
751 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100752 for (int i = 0; i < kMaxSpatialLayers; ++i) {
753 if (temporal_layers_supported_[i]) {
754 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
755 info.fps_allocation[i].resize(num_layers);
756 }
757 }
Erik Språngaed30702018-11-05 12:57:17 +0100758 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200759
760 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100761 info.requested_resolution_alignment = requested_resolution_alignment_;
Erik Språngaed30702018-11-05 12:57:17 +0100762 return info;
kthelgason876222f2016-11-29 01:44:11 -0800763 }
764
Erik Språngb7cb7b52019-02-26 15:52:33 +0100765 int32_t RegisterEncodeCompleteCallback(
766 EncodedImageCallback* callback) override {
767 rtc::CritScope lock(&local_crit_sect_);
768 encoded_image_callback_ = callback;
769 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
770 }
771
perkjfa10b552016-10-02 23:45:26 -0700772 void ContinueEncode() { continue_encode_event_.Set(); }
773
774 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
775 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800776 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700777 EXPECT_EQ(timestamp_, timestamp);
778 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
779 }
780
kthelgason2fc52542017-03-03 00:24:41 -0800781 void SetQualityScaling(bool b) {
782 rtc::CritScope lock(&local_crit_sect_);
783 quality_scaling_ = b;
784 }
kthelgasonad9010c2017-02-14 00:46:51 -0800785
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100786 void SetRequestedResolutionAlignment(int requested_resolution_alignment) {
787 rtc::CritScope lock(&local_crit_sect_);
788 requested_resolution_alignment_ = requested_resolution_alignment;
789 }
790
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100791 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
792 rtc::CritScope lock(&local_crit_sect_);
793 is_hardware_accelerated_ = is_hardware_accelerated;
794 }
795
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100796 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
797 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
798 rtc::CritScope lock(&local_crit_sect_);
799 temporal_layers_supported_[spatial_idx] = supported;
800 }
801
Sergey Silkin6456e352019-07-08 17:56:40 +0200802 void SetResolutionBitrateLimits(
803 std::vector<ResolutionBitrateLimits> thresholds) {
804 rtc::CritScope cs(&local_crit_sect_);
805 resolution_bitrate_limits_ = thresholds;
806 }
807
sprangfe627f32017-03-29 08:24:59 -0700808 void ForceInitEncodeFailure(bool force_failure) {
809 rtc::CritScope lock(&local_crit_sect_);
810 force_init_encode_failed_ = force_failure;
811 }
812
Niels Möller6bb5ab92019-01-11 11:11:10 +0100813 void SimulateOvershoot(double rate_factor) {
814 rtc::CritScope lock(&local_crit_sect_);
815 rate_factor_ = rate_factor;
816 }
817
Erik Språngd7329ca2019-02-21 21:19:53 +0100818 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100819 rtc::CritScope lock(&local_crit_sect_);
820 return last_framerate_;
821 }
822
Erik Språngd7329ca2019-02-21 21:19:53 +0100823 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100824 rtc::CritScope lock(&local_crit_sect_);
825 return last_update_rect_;
826 }
827
Niels Möller87e2d782019-03-07 10:18:23 +0100828 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100829 rtc::CritScope lock(&local_crit_sect_);
830 return last_frame_types_;
831 }
832
833 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100834 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100835 keyframe ? VideoFrameType::kVideoFrameKey
836 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100837 {
838 rtc::CritScope lock(&local_crit_sect_);
839 last_frame_types_ = frame_type;
840 }
Niels Möllerb859b322019-03-07 12:40:01 +0100841 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100842 }
843
Erik Språngb7cb7b52019-02-26 15:52:33 +0100844 void InjectEncodedImage(const EncodedImage& image) {
845 rtc::CritScope lock(&local_crit_sect_);
846 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
847 }
848
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200849 void InjectEncodedImage(const EncodedImage& image,
850 const CodecSpecificInfo* codec_specific_info,
851 const RTPFragmentationHeader* fragmentation) {
852 rtc::CritScope lock(&local_crit_sect_);
853 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
854 fragmentation);
855 }
856
Erik Språngd7329ca2019-02-21 21:19:53 +0100857 void ExpectNullFrame() {
858 rtc::CritScope lock(&local_crit_sect_);
859 expect_null_frame_ = true;
860 }
861
Erik Språng5056af02019-09-02 15:53:11 +0200862 absl::optional<VideoEncoder::RateControlParameters>
863 GetAndResetLastRateControlSettings() {
864 auto settings = last_rate_control_settings_;
865 last_rate_control_settings_.reset();
866 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100867 }
868
Sergey Silkin5ee69672019-07-02 14:18:34 +0200869 int GetNumEncoderInitializations() const {
870 rtc::CritScope lock(&local_crit_sect_);
871 return num_encoder_initializations_;
872 }
873
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200874 int GetNumSetRates() const {
875 rtc::CritScope lock(&local_crit_sect_);
876 return num_set_rates_;
877 }
878
perkjfa10b552016-10-02 23:45:26 -0700879 private:
perkj26091b12016-09-01 01:17:40 -0700880 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100881 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700882 bool block_encode;
883 {
brandtre78d2662017-01-16 05:57:16 -0800884 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100885 if (expect_null_frame_) {
886 EXPECT_EQ(input_image.timestamp(), 0u);
887 EXPECT_EQ(input_image.width(), 1);
888 last_frame_types_ = *frame_types;
889 expect_null_frame_ = false;
890 } else {
891 EXPECT_GT(input_image.timestamp(), timestamp_);
892 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
893 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
894 }
perkj26091b12016-09-01 01:17:40 -0700895
896 timestamp_ = input_image.timestamp();
897 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700898 last_input_width_ = input_image.width();
899 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700900 block_encode = block_next_encode_;
901 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100902 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100903 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700904 }
Niels Möllerb859b322019-03-07 12:40:01 +0100905 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700906 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700907 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700908 return result;
909 }
910
sprangfe627f32017-03-29 08:24:59 -0700911 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200912 const Settings& settings) override {
913 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200914
sprangfe627f32017-03-29 08:24:59 -0700915 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100916 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200917
918 ++num_encoder_initializations_;
919
Erik Språng82fad3d2018-03-21 09:57:23 +0100920 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700921 // Simulate setting up temporal layers, in order to validate the life
922 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100923 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200924 frame_buffer_controller_ =
925 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700926 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100927 if (force_init_encode_failed_) {
928 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700929 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100930 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100931
Erik Språngb7cb7b52019-02-26 15:52:33 +0100932 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700933 return res;
934 }
935
Erik Språngb7cb7b52019-02-26 15:52:33 +0100936 int32_t Release() override {
937 rtc::CritScope lock(&local_crit_sect_);
938 EXPECT_NE(initialized_, EncoderState::kUninitialized);
939 initialized_ = EncoderState::kUninitialized;
940 return FakeEncoder::Release();
941 }
942
Erik Språng16cb8f52019-04-12 13:59:09 +0200943 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100944 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200945 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100946 VideoBitrateAllocation adjusted_rate_allocation;
947 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
948 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200949 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100950 adjusted_rate_allocation.SetBitrate(
951 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200952 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100953 rate_factor_));
954 }
955 }
956 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200957 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200958 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200959 RateControlParameters adjusted_paramters = parameters;
960 adjusted_paramters.bitrate = adjusted_rate_allocation;
961 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100962 }
963
brandtre78d2662017-01-16 05:57:16 -0800964 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100965 enum class EncoderState {
966 kUninitialized,
967 kInitializationFailed,
968 kInitialized
969 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
970 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700971 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700972 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700973 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
974 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
975 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
976 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
977 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Rasmus Brandt5cad55b2019-12-19 09:47:11 +0100978 int requested_resolution_alignment_ RTC_GUARDED_BY(local_crit_sect_) = 1;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100979 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100980 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700981 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100982 absl::optional<bool>
983 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
984 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700985 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100986 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
987 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200988 absl::optional<VideoEncoder::RateControlParameters>
989 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100990 VideoFrame::UpdateRect last_update_rect_
991 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100992 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100993 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100994 EncodedImageCallback* encoded_image_callback_
995 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Evan Shrubsolefb862742020-03-16 16:18:36 +0100996 NiceMock<MockFecControllerOverride> fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200997 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200998 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
999 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02001000 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -07001001 };
1002
mflodmancc3d4422017-08-03 08:27:51 -07001003 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -07001004 public:
1005 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +01001006 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -07001007
perkj26091b12016-09-01 01:17:40 -07001008 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -07001009 EXPECT_TRUE(
1010 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
1011 }
1012
1013 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
1014 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -07001015 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -07001016 if (!encoded_frame_event_.Wait(timeout_ms))
1017 return false;
perkj26091b12016-09-01 01:17:40 -07001018 {
1019 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -08001020 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -07001021 }
1022 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -07001023 return true;
perkj26091b12016-09-01 01:17:40 -07001024 }
1025
sprangb1ca0732017-02-01 08:38:12 -08001026 void WaitForEncodedFrame(uint32_t expected_width,
1027 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -07001028 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001029 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -07001030 }
1031
Åsa Perssonc74d8da2017-12-04 14:13:56 +01001032 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -07001033 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -08001034 uint32_t width = 0;
1035 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -08001036 {
1037 rtc::CritScope lock(&crit_);
1038 width = last_width_;
1039 height = last_height_;
1040 }
1041 EXPECT_EQ(expected_height, height);
1042 EXPECT_EQ(expected_width, width);
1043 }
1044
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001045 void CheckLastFrameSizeIsMultipleOf(int resolution_alignment) {
1046 int width = 0;
1047 int height = 0;
1048 {
1049 rtc::CritScope lock(&crit_);
1050 width = last_width_;
1051 height = last_height_;
1052 }
1053 EXPECT_EQ(width % resolution_alignment, 0);
1054 EXPECT_EQ(height % resolution_alignment, 0);
1055 }
1056
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001057 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
1058 VideoRotation rotation;
1059 {
1060 rtc::CritScope lock(&crit_);
1061 rotation = last_rotation_;
1062 }
1063 EXPECT_EQ(expected_rotation, rotation);
1064 }
1065
kthelgason2fc52542017-03-03 00:24:41 -08001066 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -08001067
sprangc5d62e22017-04-02 23:53:04 -07001068 bool WaitForFrame(int64_t timeout_ms) {
1069 return encoded_frame_event_.Wait(timeout_ms);
1070 }
1071
perkj26091b12016-09-01 01:17:40 -07001072 void SetExpectNoFrames() {
1073 rtc::CritScope lock(&crit_);
1074 expect_frames_ = false;
1075 }
1076
asaperssonfab67072017-04-04 05:51:49 -07001077 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +02001078 rtc::CritScope lock(&crit_);
1079 return number_of_reconfigurations_;
1080 }
1081
asaperssonfab67072017-04-04 05:51:49 -07001082 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +02001083 rtc::CritScope lock(&crit_);
1084 return min_transmit_bitrate_bps_;
1085 }
1086
Erik Språngd7329ca2019-02-21 21:19:53 +01001087 void SetNumExpectedLayers(size_t num_layers) {
1088 rtc::CritScope lock(&crit_);
1089 num_expected_layers_ = num_layers;
1090 }
1091
Erik Språngb7cb7b52019-02-26 15:52:33 +01001092 int64_t GetLastCaptureTimeMs() const {
1093 rtc::CritScope lock(&crit_);
1094 return last_capture_time_ms_;
1095 }
1096
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001097 std::vector<uint8_t> GetLastEncodedImageData() {
1098 rtc::CritScope lock(&crit_);
1099 return std::move(last_encoded_image_data_);
1100 }
1101
1102 RTPFragmentationHeader GetLastFragmentation() {
1103 rtc::CritScope lock(&crit_);
1104 return std::move(last_fragmentation_);
1105 }
1106
perkj26091b12016-09-01 01:17:40 -07001107 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001108 Result OnEncodedImage(
1109 const EncodedImage& encoded_image,
1110 const CodecSpecificInfo* codec_specific_info,
1111 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001112 rtc::CritScope lock(&crit_);
1113 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001114 last_encoded_image_data_ = std::vector<uint8_t>(
1115 encoded_image.data(), encoded_image.data() + encoded_image.size());
1116 if (fragmentation) {
1117 last_fragmentation_.CopyFrom(*fragmentation);
1118 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001119 uint32_t timestamp = encoded_image.Timestamp();
1120 if (last_timestamp_ != timestamp) {
1121 num_received_layers_ = 1;
1122 } else {
1123 ++num_received_layers_;
1124 }
1125 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001126 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001127 last_width_ = encoded_image._encodedWidth;
1128 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001129 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001130 if (num_received_layers_ == num_expected_layers_) {
1131 encoded_frame_event_.Set();
1132 }
sprangb1ca0732017-02-01 08:38:12 -08001133 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001134 }
1135
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001136 void OnEncoderConfigurationChanged(
1137 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +02001138 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001139 VideoEncoderConfig::ContentType content_type,
1140 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001141 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001142 ++number_of_reconfigurations_;
1143 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1144 }
1145
perkj26091b12016-09-01 01:17:40 -07001146 rtc::CriticalSection crit_;
1147 TestEncoder* test_encoder_;
1148 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001149 std::vector<uint8_t> last_encoded_image_data_;
1150 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001151 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001152 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001153 uint32_t last_height_ = 0;
1154 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001155 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001156 size_t num_expected_layers_ = 1;
1157 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001158 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001159 int number_of_reconfigurations_ = 0;
1160 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001161 };
1162
Sergey Silkin5ee69672019-07-02 14:18:34 +02001163 class VideoBitrateAllocatorProxyFactory
1164 : public VideoBitrateAllocatorFactory {
1165 public:
1166 VideoBitrateAllocatorProxyFactory()
1167 : bitrate_allocator_factory_(
1168 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1169
1170 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1171 const VideoCodec& codec) override {
1172 rtc::CritScope lock(&crit_);
1173 codec_config_ = codec;
1174 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1175 }
1176
1177 VideoCodec codec_config() const {
1178 rtc::CritScope lock(&crit_);
1179 return codec_config_;
1180 }
1181
1182 private:
1183 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1184
1185 rtc::CriticalSection crit_;
1186 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1187 };
1188
perkj26091b12016-09-01 01:17:40 -07001189 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001190 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001191 int codec_width_;
1192 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001193 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001194 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001195 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001196 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001197 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001198 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001199 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001200 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001201 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001202 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001203};
1204
mflodmancc3d4422017-08-03 08:27:51 -07001205TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001206 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001207 DataRate::BitsPerSec(kTargetBitrateBps),
1208 DataRate::BitsPerSec(kTargetBitrateBps),
1209 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001210 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001211 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001212 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001213 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001214 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001215}
1216
mflodmancc3d4422017-08-03 08:27:51 -07001217TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001218 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001219 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001220 // The encoder will cache up to one frame for a short duration. Adding two
1221 // frames means that the first frame will be dropped and the second frame will
1222 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001223 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001224 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001225 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001226
Erik Språng4c6ca302019-04-08 15:14:01 +02001227 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001228 DataRate::BitsPerSec(kTargetBitrateBps),
1229 DataRate::BitsPerSec(kTargetBitrateBps),
1230 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001231
Sebastian Janssona3177052018-04-10 13:05:49 +02001232 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001233 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001234 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1235
1236 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001237 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001238}
1239
mflodmancc3d4422017-08-03 08:27:51 -07001240TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001241 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001242 DataRate::BitsPerSec(kTargetBitrateBps),
1243 DataRate::BitsPerSec(kTargetBitrateBps),
1244 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001245 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001246 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001247
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001248 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
1249 DataRate::BitsPerSec(0),
1250 DataRate::BitsPerSec(0), 0, 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001251 // The encoder will cache up to one frame for a short duration. Adding two
1252 // frames means that the first frame will be dropped and the second frame will
1253 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001254 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001255 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001256
Erik Språng4c6ca302019-04-08 15:14:01 +02001257 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001258 DataRate::BitsPerSec(kTargetBitrateBps),
1259 DataRate::BitsPerSec(kTargetBitrateBps),
1260 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001261 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001262 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1263 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001264 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001265}
1266
mflodmancc3d4422017-08-03 08:27:51 -07001267TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001268 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001269 DataRate::BitsPerSec(kTargetBitrateBps),
1270 DataRate::BitsPerSec(kTargetBitrateBps),
1271 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001272 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001273 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001274
1275 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001276 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001277
perkja49cbd32016-09-16 07:53:41 -07001278 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001279 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001280 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001281}
1282
mflodmancc3d4422017-08-03 08:27:51 -07001283TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001284 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001285 DataRate::BitsPerSec(kTargetBitrateBps),
1286 DataRate::BitsPerSec(kTargetBitrateBps),
1287 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001288
perkja49cbd32016-09-16 07:53:41 -07001289 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001290 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001291
mflodmancc3d4422017-08-03 08:27:51 -07001292 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001293 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001294 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001295 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1296 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001297}
1298
mflodmancc3d4422017-08-03 08:27:51 -07001299TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001300 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001301 DataRate::BitsPerSec(kTargetBitrateBps),
1302 DataRate::BitsPerSec(kTargetBitrateBps),
1303 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj26091b12016-09-01 01:17:40 -07001304
1305 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001306 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001307 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001308 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1309 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001310 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1311 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001312 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001313 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001314
mflodmancc3d4422017-08-03 08:27:51 -07001315 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001316}
1317
Noah Richards51db4212019-06-12 06:59:12 -07001318TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1319 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001320 DataRate::BitsPerSec(kTargetBitrateBps),
1321 DataRate::BitsPerSec(kTargetBitrateBps),
1322 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001323
1324 rtc::Event frame_destroyed_event;
1325 video_source_.IncomingCapturedFrame(
1326 CreateFakeNativeFrame(1, &frame_destroyed_event));
1327 ExpectDroppedFrame();
1328 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1329 video_stream_encoder_->Stop();
1330}
1331
1332TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1333 // Use the cropping factory.
1334 video_encoder_config_.video_stream_factory =
1335 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1336 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1337 kMaxPayloadLength);
1338 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1339
1340 // Capture a frame at codec_width_/codec_height_.
1341 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001342 DataRate::BitsPerSec(kTargetBitrateBps),
1343 DataRate::BitsPerSec(kTargetBitrateBps),
1344 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001345 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1346 WaitForEncodedFrame(1);
1347 // The encoder will have been configured once.
1348 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1349 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1350 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1351
1352 // Now send in a fake frame that needs to be cropped as the width/height
1353 // aren't divisible by 4 (see CreateEncoderStreams above).
1354 rtc::Event frame_destroyed_event;
1355 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1356 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1357 ExpectDroppedFrame();
1358 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1359 video_stream_encoder_->Stop();
1360}
1361
Ying Wang9b881ab2020-02-07 14:29:32 +01001362TEST_F(VideoStreamEncoderTest, DropsFramesWhenCongestionWindowPushbackSet) {
1363 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001364 DataRate::BitsPerSec(kTargetBitrateBps),
1365 DataRate::BitsPerSec(kTargetBitrateBps),
1366 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ying Wang9b881ab2020-02-07 14:29:32 +01001367 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1368 WaitForEncodedFrame(1);
1369
1370 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001371 DataRate::BitsPerSec(kTargetBitrateBps),
1372 DataRate::BitsPerSec(kTargetBitrateBps),
1373 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0.5);
Ying Wang9b881ab2020-02-07 14:29:32 +01001374 // The congestion window pushback is set to 0.5, which will drop 1/2 of
1375 // frames. Adding two frames means that the first frame will be dropped and
1376 // the second frame will be sent to the encoder.
1377 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1378 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1379 WaitForEncodedFrame(3);
1380 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1381 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1382 WaitForEncodedFrame(5);
1383 EXPECT_EQ(2u, stats_proxy_->GetStats().frames_dropped_by_congestion_window);
1384 video_stream_encoder_->Stop();
1385}
1386
mflodmancc3d4422017-08-03 08:27:51 -07001387TEST_F(VideoStreamEncoderTest,
1388 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001389 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001390 DataRate::BitsPerSec(kTargetBitrateBps),
1391 DataRate::BitsPerSec(kTargetBitrateBps),
1392 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Per21d45d22016-10-30 21:37:57 +01001393 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001394
1395 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001396 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001397 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001398 // The encoder will have been configured once when the first frame is
1399 // received.
1400 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001401
1402 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001403 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001404 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001405 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001406 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001407
1408 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001409 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001410 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001411 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001412 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001413
mflodmancc3d4422017-08-03 08:27:51 -07001414 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001415}
1416
mflodmancc3d4422017-08-03 08:27:51 -07001417TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001418 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001419 DataRate::BitsPerSec(kTargetBitrateBps),
1420 DataRate::BitsPerSec(kTargetBitrateBps),
1421 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001422
1423 // Capture a frame and wait for it to synchronize with the encoder thread.
1424 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001425 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001426 // The encoder will have been configured once.
1427 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001428 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1429 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1430
1431 codec_width_ *= 2;
1432 codec_height_ *= 2;
1433 // Capture a frame with a higher resolution and wait for it to synchronize
1434 // with the encoder thread.
1435 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001436 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001437 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1438 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001439 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001440
mflodmancc3d4422017-08-03 08:27:51 -07001441 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001442}
1443
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001444TEST_F(VideoStreamEncoderTest,
1445 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1446 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001447 DataRate::BitsPerSec(kTargetBitrateBps),
1448 DataRate::BitsPerSec(kTargetBitrateBps),
1449 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001450
1451 // Capture a frame and wait for it to synchronize with the encoder thread.
1452 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1453 WaitForEncodedFrame(1);
1454
1455 VideoEncoderConfig video_encoder_config;
1456 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1457 // Changing the max payload data length recreates encoder.
1458 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1459 kMaxPayloadLength / 2);
1460
1461 // Capture a frame and wait for it to synchronize with the encoder thread.
1462 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1463 WaitForEncodedFrame(2);
1464 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1465
1466 video_stream_encoder_->Stop();
1467}
1468
Sergey Silkin5ee69672019-07-02 14:18:34 +02001469TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1470 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001471 DataRate::BitsPerSec(kTargetBitrateBps),
1472 DataRate::BitsPerSec(kTargetBitrateBps),
1473 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001474
1475 VideoEncoderConfig video_encoder_config;
1476 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1477 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1478 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1479 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1480 kMaxPayloadLength);
1481
1482 // Capture a frame and wait for it to synchronize with the encoder thread.
1483 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1484 WaitForEncodedFrame(1);
1485 // The encoder will have been configured once when the first frame is
1486 // received.
1487 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1488 EXPECT_EQ(kTargetBitrateBps,
1489 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1490 EXPECT_EQ(kStartBitrateBps,
1491 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1492
Sergey Silkin6456e352019-07-08 17:56:40 +02001493 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1494 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001495 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1496 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1497 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1498 kMaxPayloadLength);
1499
1500 // Capture a frame and wait for it to synchronize with the encoder thread.
1501 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1502 WaitForEncodedFrame(2);
1503 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1504 // Bitrate limits have changed - rate allocator should be reconfigured,
1505 // encoder should not be reconfigured.
1506 EXPECT_EQ(kTargetBitrateBps * 2,
1507 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1508 EXPECT_EQ(kStartBitrateBps * 2,
1509 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1510 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1511
1512 video_stream_encoder_->Stop();
1513}
1514
Sergey Silkin6456e352019-07-08 17:56:40 +02001515TEST_F(VideoStreamEncoderTest,
Sergey Silkincd02eba2020-01-20 14:48:40 +01001516 IntersectionOfEncoderAndAppBitrateLimitsUsedWhenBothProvided) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001517 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001518 DataRate::BitsPerSec(kTargetBitrateBps),
1519 DataRate::BitsPerSec(kTargetBitrateBps),
1520 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001521
Sergey Silkincd02eba2020-01-20 14:48:40 +01001522 const uint32_t kMinEncBitrateKbps = 100;
1523 const uint32_t kMaxEncBitrateKbps = 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001524 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
Sergey Silkincd02eba2020-01-20 14:48:40 +01001525 /*frame_size_pixels=*/codec_width_ * codec_height_,
1526 /*min_start_bitrate_bps=*/0,
1527 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1528 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001529 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1530
Sergey Silkincd02eba2020-01-20 14:48:40 +01001531 VideoEncoderConfig video_encoder_config;
1532 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1533 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps + 1) * 1000;
1534 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1535 (kMinEncBitrateKbps + 1) * 1000;
1536 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1537 kMaxPayloadLength);
1538
1539 // When both encoder and app provide bitrate limits, the intersection of
1540 // provided sets should be used.
1541 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1542 WaitForEncodedFrame(1);
1543 EXPECT_EQ(kMaxEncBitrateKbps,
1544 bitrate_allocator_factory_.codec_config().maxBitrate);
1545 EXPECT_EQ(kMinEncBitrateKbps + 1,
1546 bitrate_allocator_factory_.codec_config().minBitrate);
1547
1548 video_encoder_config.max_bitrate_bps = (kMaxEncBitrateKbps - 1) * 1000;
1549 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1550 (kMinEncBitrateKbps - 1) * 1000;
1551 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1552 kMaxPayloadLength);
1553 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001554 WaitForEncodedFrame(2);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001555 EXPECT_EQ(kMaxEncBitrateKbps - 1,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001556 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001557 EXPECT_EQ(kMinEncBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001558 bitrate_allocator_factory_.codec_config().minBitrate);
1559
Sergey Silkincd02eba2020-01-20 14:48:40 +01001560 video_stream_encoder_->Stop();
1561}
1562
1563TEST_F(VideoStreamEncoderTest,
1564 EncoderAndAppLimitsDontIntersectEncoderLimitsIgnored) {
1565 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001566 DataRate::BitsPerSec(kTargetBitrateBps),
1567 DataRate::BitsPerSec(kTargetBitrateBps),
1568 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001569
1570 const uint32_t kMinAppBitrateKbps = 100;
1571 const uint32_t kMaxAppBitrateKbps = 200;
1572 const uint32_t kMinEncBitrateKbps = kMaxAppBitrateKbps + 1;
1573 const uint32_t kMaxEncBitrateKbps = kMaxAppBitrateKbps * 2;
1574 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1575 /*frame_size_pixels=*/codec_width_ * codec_height_,
1576 /*min_start_bitrate_bps=*/0,
1577 /*min_bitrate_bps=*/kMinEncBitrateKbps * 1000,
1578 /*max_bitrate_bps=*/kMaxEncBitrateKbps * 1000);
1579 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1580
1581 VideoEncoderConfig video_encoder_config;
1582 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1583 video_encoder_config.max_bitrate_bps = kMaxAppBitrateKbps * 1000;
1584 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1585 kMinAppBitrateKbps * 1000;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001586 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1587 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001588
Sergey Silkincd02eba2020-01-20 14:48:40 +01001589 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1590 WaitForEncodedFrame(1);
1591 EXPECT_EQ(kMaxAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001592 bitrate_allocator_factory_.codec_config().maxBitrate);
Sergey Silkincd02eba2020-01-20 14:48:40 +01001593 EXPECT_EQ(kMinAppBitrateKbps,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001594 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001595
1596 video_stream_encoder_->Stop();
1597}
1598
1599TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001600 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001601 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001602 DataRate::BitsPerSec(kTargetBitrateBps),
1603 DataRate::BitsPerSec(kTargetBitrateBps),
1604 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001605
1606 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001607 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001608 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001609 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001610 fake_encoder_.SetResolutionBitrateLimits(
1611 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1612
1613 VideoEncoderConfig video_encoder_config;
1614 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1615 video_encoder_config.max_bitrate_bps = 0;
1616 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1617 kMaxPayloadLength);
1618
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001619 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001620 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1621 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001622 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1623 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001624 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1625 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1626
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001627 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001628 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1629 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001630 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1631 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001632 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1633 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1634
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001635 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001636 // encoder for 360p should be used.
1637 video_source_.IncomingCapturedFrame(
1638 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1639 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001640 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1641 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001642 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1643 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1644
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001645 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001646 // ignored.
1647 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1648 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001649 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1650 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001651 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1652 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001653 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1654 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001655 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1656 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1657
1658 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1659 // for 270p should be used.
1660 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1661 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001662 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1663 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001664 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1665 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1666
1667 video_stream_encoder_->Stop();
1668}
1669
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001670TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1671 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001672 DataRate::BitsPerSec(kTargetBitrateBps),
1673 DataRate::BitsPerSec(kTargetBitrateBps),
1674 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001675
1676 VideoEncoderConfig video_encoder_config;
1677 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1678 video_encoder_config.max_bitrate_bps = 0;
1679 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1680 kMaxPayloadLength);
1681
1682 // Encode 720p frame to get the default encoder target bitrate.
1683 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1684 WaitForEncodedFrame(1);
1685 const uint32_t kDefaultTargetBitrateFor720pKbps =
1686 bitrate_allocator_factory_.codec_config()
1687 .simulcastStream[0]
1688 .targetBitrate;
1689
1690 // Set the max recommended encoder bitrate to something lower than the default
1691 // target bitrate.
1692 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1693 1280 * 720, 10 * 1000, 10 * 1000,
1694 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1695 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1696
1697 // Change resolution to trigger encoder reinitialization.
1698 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1699 WaitForEncodedFrame(2);
1700 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1701 WaitForEncodedFrame(3);
1702
1703 // Ensure the target bitrate is capped by the max bitrate.
1704 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1705 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1706 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1707 .simulcastStream[0]
1708 .targetBitrate *
1709 1000,
1710 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1711
1712 video_stream_encoder_->Stop();
1713}
1714
mflodmancc3d4422017-08-03 08:27:51 -07001715TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001716 EXPECT_TRUE(video_source_.has_sinks());
1717 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001718 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001719 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001720 EXPECT_FALSE(video_source_.has_sinks());
1721 EXPECT_TRUE(new_video_source.has_sinks());
1722
mflodmancc3d4422017-08-03 08:27:51 -07001723 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001724}
1725
mflodmancc3d4422017-08-03 08:27:51 -07001726TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001727 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001728 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001729 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001730 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001731}
1732
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001733TEST_F(VideoStreamEncoderTest, SinkWantsResolutionAlignment) {
1734 constexpr int kRequestedResolutionAlignment = 7;
1735 video_source_.set_adaptation_enabled(true);
1736 fake_encoder_.SetRequestedResolutionAlignment(kRequestedResolutionAlignment);
1737 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001738 DataRate::BitsPerSec(kTargetBitrateBps),
1739 DataRate::BitsPerSec(kTargetBitrateBps),
1740 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001741
1742 // On the 1st frame, we should have initialized the encoder and
1743 // asked for its resolution requirements.
1744 video_source_.IncomingCapturedFrame(
1745 CreateFrame(1, codec_width_, codec_height_));
1746 WaitForEncodedFrame(1);
1747 EXPECT_EQ(video_source_.sink_wants().resolution_alignment,
1748 kRequestedResolutionAlignment);
1749
1750 // On the 2nd frame, we should be receiving a correctly aligned resolution.
1751 // (It's up the to the encoder to potentially drop the previous frame,
1752 // to avoid coding back-to-back keyframes.)
1753 video_source_.IncomingCapturedFrame(
1754 CreateFrame(2, codec_width_, codec_height_));
1755 WaitForEncodedFrame(2);
1756 sink_.CheckLastFrameSizeIsMultipleOf(kRequestedResolutionAlignment);
1757
1758 video_stream_encoder_->Stop();
1759}
1760
Jonathan Yubc771b72017-12-08 17:04:29 -08001761TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1762 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001763 const int kWidth = 1280;
1764 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001765
1766 // We rely on the automatic resolution adaptation, but we handle framerate
1767 // adaptation manually by mocking the stats proxy.
1768 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001769
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001770 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001771 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001772 DataRate::BitsPerSec(kTargetBitrateBps),
1773 DataRate::BitsPerSec(kTargetBitrateBps),
1774 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001775 video_stream_encoder_->SetSource(&video_source_,
1776 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001777 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001778 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001779 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001780 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1781
Jonathan Yubc771b72017-12-08 17:04:29 -08001782 // Adapt down as far as possible.
1783 rtc::VideoSinkWants last_wants;
1784 int64_t t = 1;
1785 int loop_count = 0;
1786 do {
1787 ++loop_count;
1788 last_wants = video_source_.sink_wants();
1789
1790 // Simulate the framerate we've been asked to adapt to.
1791 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1792 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1793 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1794 mock_stats.input_frame_rate = fps;
1795 stats_proxy_->SetMockStats(mock_stats);
1796
1797 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1798 sink_.WaitForEncodedFrame(t);
1799 t += frame_interval_ms;
1800
mflodmancc3d4422017-08-03 08:27:51 -07001801 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001802 VerifyBalancedModeFpsRange(
1803 video_source_.sink_wants(),
1804 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1805 } while (video_source_.sink_wants().max_pixel_count <
1806 last_wants.max_pixel_count ||
1807 video_source_.sink_wants().max_framerate_fps <
1808 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001809
Jonathan Yubc771b72017-12-08 17:04:29 -08001810 // Verify that we've adapted all the way down.
1811 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001812 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001813 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1814 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001815 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001816 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1817 *video_source_.last_sent_height());
1818 EXPECT_EQ(kMinBalancedFramerateFps,
1819 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001820
Jonathan Yubc771b72017-12-08 17:04:29 -08001821 // Adapt back up the same number of times we adapted down.
1822 for (int i = 0; i < loop_count - 1; ++i) {
1823 last_wants = video_source_.sink_wants();
1824
1825 // Simulate the framerate we've been asked to adapt to.
1826 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1827 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1828 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1829 mock_stats.input_frame_rate = fps;
1830 stats_proxy_->SetMockStats(mock_stats);
1831
1832 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1833 sink_.WaitForEncodedFrame(t);
1834 t += frame_interval_ms;
1835
mflodmancc3d4422017-08-03 08:27:51 -07001836 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001837 VerifyBalancedModeFpsRange(
1838 video_source_.sink_wants(),
1839 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1840 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1841 last_wants.max_pixel_count ||
1842 video_source_.sink_wants().max_framerate_fps >
1843 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001844 }
1845
Åsa Persson8c1bf952018-09-13 10:42:19 +02001846 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001847 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001848 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001849 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1850 EXPECT_EQ((loop_count - 1) * 2,
1851 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001852
mflodmancc3d4422017-08-03 08:27:51 -07001853 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001854}
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01001855
mflodmancc3d4422017-08-03 08:27:51 -07001856TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001857 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001858 DataRate::BitsPerSec(kTargetBitrateBps),
1859 DataRate::BitsPerSec(kTargetBitrateBps),
1860 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001861 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001862
sprangc5d62e22017-04-02 23:53:04 -07001863 const int kFrameWidth = 1280;
1864 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001865
Åsa Persson8c1bf952018-09-13 10:42:19 +02001866 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001867
kthelgason5e13d412016-12-01 03:59:51 -08001868 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001869 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001870 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001871 frame_timestamp += kFrameIntervalMs;
1872
perkj803d97f2016-11-01 11:45:46 -07001873 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001874 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001875 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001876 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001877 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001878 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001879
asapersson0944a802017-04-07 00:57:58 -07001880 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001881 // wanted resolution.
1882 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1883 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1884 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001885 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001886
1887 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001888 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001889 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001890 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001891 // Give the encoder queue time to process the change in degradation preference
1892 // by waiting for an encoded frame.
1893 new_video_source.IncomingCapturedFrame(
1894 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1895 sink_.WaitForEncodedFrame(frame_timestamp);
1896 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001897 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001898 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001899
sprangc5d62e22017-04-02 23:53:04 -07001900 // Force an input frame rate to be available, or the adaptation call won't
1901 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001902 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001903 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001904 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001905 stats_proxy_->SetMockStats(stats);
1906
mflodmancc3d4422017-08-03 08:27:51 -07001907 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001908 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001909 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001910 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001911 frame_timestamp += kFrameIntervalMs;
1912
1913 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001914 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001915 EXPECT_EQ(std::numeric_limits<int>::max(),
1916 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001917 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001918
asapersson02465b82017-04-10 01:12:52 -07001919 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001920 video_stream_encoder_->SetSource(&new_video_source,
1921 webrtc::DegradationPreference::DISABLED);
Henrik Boström07b17df2020-01-15 11:42:12 +01001922 // Give the encoder queue time to process the change in degradation preference
1923 // by waiting for an encoded frame.
1924 new_video_source.IncomingCapturedFrame(
1925 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1926 sink_.WaitForEncodedFrame(frame_timestamp);
1927 frame_timestamp += kFrameIntervalMs;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001928 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001929
mflodmancc3d4422017-08-03 08:27:51 -07001930 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001931 new_video_source.IncomingCapturedFrame(
1932 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001933 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001934 frame_timestamp += kFrameIntervalMs;
1935
1936 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001937 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001938
1939 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001940 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001941 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
Henrik Boström07b17df2020-01-15 11:42:12 +01001942 // Give the encoder queue time to process the change in degradation preference
1943 // by waiting for an encoded frame.
1944 new_video_source.IncomingCapturedFrame(
1945 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1946 sink_.WaitForEncodedFrame(frame_timestamp);
1947 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001948 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1949 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001950 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001951 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001952
1953 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001954 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001955 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01001956 // Give the encoder queue time to process the change in degradation preference
1957 // by waiting for an encoded frame.
1958 new_video_source.IncomingCapturedFrame(
1959 CreateFrame(frame_timestamp, kFrameWidth, kFrameWidth));
1960 sink_.WaitForEncodedFrame(frame_timestamp);
1961 frame_timestamp += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07001962 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1963 EXPECT_EQ(std::numeric_limits<int>::max(),
1964 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001965 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001966
mflodmancc3d4422017-08-03 08:27:51 -07001967 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001968}
1969
mflodmancc3d4422017-08-03 08:27:51 -07001970TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001971 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001972 DataRate::BitsPerSec(kTargetBitrateBps),
1973 DataRate::BitsPerSec(kTargetBitrateBps),
1974 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001975
asaperssonfab67072017-04-04 05:51:49 -07001976 const int kWidth = 1280;
1977 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001978 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001979 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001980 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1981 EXPECT_FALSE(stats.bw_limited_resolution);
1982 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1983
1984 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001985 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001986 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001987 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001988
1989 stats = stats_proxy_->GetStats();
1990 EXPECT_TRUE(stats.bw_limited_resolution);
1991 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1992
1993 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001994 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001995 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001996 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001997
1998 stats = stats_proxy_->GetStats();
1999 EXPECT_FALSE(stats.bw_limited_resolution);
2000 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
2001 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2002
mflodmancc3d4422017-08-03 08:27:51 -07002003 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07002004}
2005
mflodmancc3d4422017-08-03 08:27:51 -07002006TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002007 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002008 DataRate::BitsPerSec(kTargetBitrateBps),
2009 DataRate::BitsPerSec(kTargetBitrateBps),
2010 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07002011
2012 const int kWidth = 1280;
2013 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07002014 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002015 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07002016 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2017 EXPECT_FALSE(stats.cpu_limited_resolution);
2018 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2019
2020 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002021 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002022 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002023 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07002024
2025 stats = stats_proxy_->GetStats();
2026 EXPECT_TRUE(stats.cpu_limited_resolution);
2027 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2028
2029 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002030 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002031 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002032 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07002033
2034 stats = stats_proxy_->GetStats();
2035 EXPECT_FALSE(stats.cpu_limited_resolution);
2036 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07002037 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002038
mflodmancc3d4422017-08-03 08:27:51 -07002039 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002040}
2041
mflodmancc3d4422017-08-03 08:27:51 -07002042TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002043 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002044 DataRate::BitsPerSec(kTargetBitrateBps),
2045 DataRate::BitsPerSec(kTargetBitrateBps),
2046 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002047
asaperssonfab67072017-04-04 05:51:49 -07002048 const int kWidth = 1280;
2049 const int kHeight = 720;
2050 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002051 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002052 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002053 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002054 EXPECT_FALSE(stats.cpu_limited_resolution);
2055 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2056
asaperssonfab67072017-04-04 05:51:49 -07002057 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002058 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002059 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002060 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002061 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002062 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002063 EXPECT_TRUE(stats.cpu_limited_resolution);
2064 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2065
2066 // Set new source with adaptation still enabled.
2067 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002068 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002069 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002070
asaperssonfab67072017-04-04 05:51:49 -07002071 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002072 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002073 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002074 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002075 EXPECT_TRUE(stats.cpu_limited_resolution);
2076 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2077
2078 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002079 video_stream_encoder_->SetSource(&new_video_source,
2080 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08002081
asaperssonfab67072017-04-04 05:51:49 -07002082 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002083 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002084 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002085 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002086 EXPECT_FALSE(stats.cpu_limited_resolution);
2087 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2088
2089 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002090 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002091 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08002092
asaperssonfab67072017-04-04 05:51:49 -07002093 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002094 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002095 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002096 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002097 EXPECT_TRUE(stats.cpu_limited_resolution);
2098 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2099
asaperssonfab67072017-04-04 05:51:49 -07002100 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07002101 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002102 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002103 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08002104 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002105 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08002106 EXPECT_FALSE(stats.cpu_limited_resolution);
2107 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002108 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002109
mflodmancc3d4422017-08-03 08:27:51 -07002110 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002111}
2112
mflodmancc3d4422017-08-03 08:27:51 -07002113TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002114 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002115 DataRate::BitsPerSec(kTargetBitrateBps),
2116 DataRate::BitsPerSec(kTargetBitrateBps),
2117 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002118
asaperssonfab67072017-04-04 05:51:49 -07002119 const int kWidth = 1280;
2120 const int kHeight = 720;
2121 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002122 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002123 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002124 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002125 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002126 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002127
2128 // Set new source with adaptation still enabled.
2129 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002130 video_stream_encoder_->SetSource(&new_video_source,
2131 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002132
asaperssonfab67072017-04-04 05:51:49 -07002133 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002134 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08002135 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002136 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002137 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002138 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002139
asaperssonfab67072017-04-04 05:51:49 -07002140 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002141 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002142 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002143 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002144 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002145 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002146 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002147 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002148
asaperssonfab67072017-04-04 05:51:49 -07002149 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002150 video_stream_encoder_->SetSource(&new_video_source,
2151 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08002152
asaperssonfab67072017-04-04 05:51:49 -07002153 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002154 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002155 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002156 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002157 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002158 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002159
asapersson02465b82017-04-10 01:12:52 -07002160 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07002161 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002162 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002163
asaperssonfab67072017-04-04 05:51:49 -07002164 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002165 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08002166 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08002167 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002168 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07002169 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
2170 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08002171
mflodmancc3d4422017-08-03 08:27:51 -07002172 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002173}
2174
mflodmancc3d4422017-08-03 08:27:51 -07002175TEST_F(VideoStreamEncoderTest,
2176 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002177 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002178 DataRate::BitsPerSec(kTargetBitrateBps),
2179 DataRate::BitsPerSec(kTargetBitrateBps),
2180 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002181
2182 const int kWidth = 1280;
2183 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002184 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002185 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002186 video_source_.IncomingCapturedFrame(
2187 CreateFrame(timestamp_ms, kWidth, kHeight));
2188 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002189 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2190 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2191 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2192
2193 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002194 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002195 timestamp_ms += kFrameIntervalMs;
2196 video_source_.IncomingCapturedFrame(
2197 CreateFrame(timestamp_ms, kWidth, kHeight));
2198 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002199 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2200 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2201 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2202
2203 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002204 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002205 timestamp_ms += kFrameIntervalMs;
2206 video_source_.IncomingCapturedFrame(
2207 CreateFrame(timestamp_ms, kWidth, kHeight));
2208 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002209 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2210 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2211 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2212
Niels Möller4db138e2018-04-19 09:04:13 +02002213 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002214 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002215
2216 VideoEncoderConfig video_encoder_config;
2217 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2218 // Make format different, to force recreation of encoder.
2219 video_encoder_config.video_format.parameters["foo"] = "foo";
2220 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002221 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002222 timestamp_ms += kFrameIntervalMs;
2223 video_source_.IncomingCapturedFrame(
2224 CreateFrame(timestamp_ms, kWidth, kHeight));
2225 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002226 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2227 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2228 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2229
mflodmancc3d4422017-08-03 08:27:51 -07002230 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002231}
2232
mflodmancc3d4422017-08-03 08:27:51 -07002233TEST_F(VideoStreamEncoderTest,
Evan Shrubsole33be9df2020-03-05 18:39:32 +01002234 StatsTracksCpuAdaptationStatsWhenSwitchingSource_Balanced) {
2235 video_stream_encoder_->OnBitrateUpdated(
2236 DataRate::BitsPerSec(kTargetBitrateBps),
2237 DataRate::BitsPerSec(kTargetBitrateBps),
2238 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
2239
2240 const int kWidth = 1280;
2241 const int kHeight = 720;
2242 int sequence = 1;
2243
2244 // Enable BALANCED preference, no initial limitation.
2245 test::FrameForwarder source;
2246 video_stream_encoder_->SetSource(&source,
2247 webrtc::DegradationPreference::BALANCED);
2248 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2249 WaitForEncodedFrame(sequence++);
2250 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2251 EXPECT_FALSE(stats.cpu_limited_resolution);
2252 EXPECT_FALSE(stats.cpu_limited_framerate);
2253 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2254
2255 // Trigger CPU overuse, should now adapt down.
2256 video_stream_encoder_->TriggerCpuOveruse();
2257 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2258 WaitForEncodedFrame(sequence++);
2259 stats = stats_proxy_->GetStats();
2260 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2261
2262 // Set new degradation preference should clear restrictions since we changed
2263 // from BALANCED.
2264 video_stream_encoder_->SetSource(
2265 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2266 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2267 WaitForEncodedFrame(sequence++);
2268 stats = stats_proxy_->GetStats();
2269 EXPECT_FALSE(stats.cpu_limited_resolution);
2270 EXPECT_FALSE(stats.cpu_limited_framerate);
2271 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2272
2273 // Force an input frame rate to be available, or the adaptation call won't
2274 // know what framerate to adapt from.
2275 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2276 mock_stats.input_frame_rate = 30;
2277 stats_proxy_->SetMockStats(mock_stats);
2278 video_stream_encoder_->TriggerCpuOveruse();
2279 stats_proxy_->ResetMockStats();
2280 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2281 WaitForEncodedFrame(sequence++);
2282
2283 // We have now adapted once.
2284 stats = stats_proxy_->GetStats();
2285 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2286
2287 // Back to BALANCED, should clear the restrictions again.
2288 video_stream_encoder_->SetSource(&source,
2289 webrtc::DegradationPreference::BALANCED);
2290 source.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
2291 WaitForEncodedFrame(sequence++);
2292 stats = stats_proxy_->GetStats();
2293 EXPECT_FALSE(stats.cpu_limited_resolution);
2294 EXPECT_FALSE(stats.cpu_limited_framerate);
2295 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2296
2297 video_stream_encoder_->Stop();
2298}
2299
2300TEST_F(VideoStreamEncoderTest,
mflodmancc3d4422017-08-03 08:27:51 -07002301 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002302 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002303 DataRate::BitsPerSec(kTargetBitrateBps),
2304 DataRate::BitsPerSec(kTargetBitrateBps),
2305 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002306
asapersson0944a802017-04-07 00:57:58 -07002307 const int kWidth = 1280;
2308 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002309 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002310
asaperssonfab67072017-04-04 05:51:49 -07002311 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002312 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002313 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002314 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002315 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002316 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2317
asapersson02465b82017-04-10 01:12:52 -07002318 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002319 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002320 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002321 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002322 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002323 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002324 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002325 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2326
2327 // Set new source with adaptation still enabled.
2328 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002329 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002330 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002331
2332 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002333 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002334 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002335 stats = stats_proxy_->GetStats();
2336 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002337 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002338 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2339
sprangc5d62e22017-04-02 23:53:04 -07002340 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002341 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002342 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002343 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002344 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002345 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002346 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002347 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002348 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002349 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002350 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2351
sprangc5d62e22017-04-02 23:53:04 -07002352 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002353 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002354 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2355 mock_stats.input_frame_rate = 30;
2356 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002357 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002358 stats_proxy_->ResetMockStats();
2359
2360 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002361 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002362 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002363
2364 // Framerate now adapted.
2365 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002366 EXPECT_FALSE(stats.cpu_limited_resolution);
2367 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002368 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2369
2370 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002371 video_stream_encoder_->SetSource(&new_video_source,
2372 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002373 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002374 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002375 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002376
2377 stats = stats_proxy_->GetStats();
2378 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002379 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002380 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2381
2382 // Try to trigger overuse. Should not succeed.
2383 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002384 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002385 stats_proxy_->ResetMockStats();
2386
2387 stats = stats_proxy_->GetStats();
2388 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002389 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002390 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2391
2392 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002393 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002394 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002395 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002396 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002397 stats = stats_proxy_->GetStats();
2398 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002399 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002400 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002401
2402 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002403 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002404 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002405 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002406 stats = stats_proxy_->GetStats();
2407 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002408 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002409 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2410
2411 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002412 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002413 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002414 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002415 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002416 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002417 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002418 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002419 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002420 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002421 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2422
2423 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002424 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002425 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002426 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002427 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002428 stats = stats_proxy_->GetStats();
2429 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002430 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002431 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002432 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002433
mflodmancc3d4422017-08-03 08:27:51 -07002434 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002435}
2436
mflodmancc3d4422017-08-03 08:27:51 -07002437TEST_F(VideoStreamEncoderTest,
2438 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002439 const int kWidth = 1280;
2440 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002441 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002442 DataRate::BitsPerSec(kTargetBitrateBps),
2443 DataRate::BitsPerSec(kTargetBitrateBps),
2444 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002445
asaperssonfab67072017-04-04 05:51:49 -07002446 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002447 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002448
asaperssonfab67072017-04-04 05:51:49 -07002449 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002450 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002451
asaperssonfab67072017-04-04 05:51:49 -07002452 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002453 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002454
asaperssonfab67072017-04-04 05:51:49 -07002455 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002456 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002457
kthelgason876222f2016-11-29 01:44:11 -08002458 // Expect a scale down.
2459 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002460 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002461
asapersson02465b82017-04-10 01:12:52 -07002462 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002463 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002464 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002465 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002466
asaperssonfab67072017-04-04 05:51:49 -07002467 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002468 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002469 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002470 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002471
asaperssonfab67072017-04-04 05:51:49 -07002472 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002473 EXPECT_EQ(std::numeric_limits<int>::max(),
2474 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002475
asaperssonfab67072017-04-04 05:51:49 -07002476 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002477 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002478 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002479 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002480
asapersson02465b82017-04-10 01:12:52 -07002481 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002482 EXPECT_EQ(std::numeric_limits<int>::max(),
2483 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002484
mflodmancc3d4422017-08-03 08:27:51 -07002485 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002486}
2487
mflodmancc3d4422017-08-03 08:27:51 -07002488TEST_F(VideoStreamEncoderTest,
2489 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002490 const int kWidth = 1280;
2491 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002492 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002493 DataRate::BitsPerSec(kTargetBitrateBps),
2494 DataRate::BitsPerSec(kTargetBitrateBps),
2495 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002496
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002497 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002498 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002499 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002500 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002501
2502 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002503 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002504 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002505 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2506 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2507
2508 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002509 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002510 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002511 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2512 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2513 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2514
2515 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002516 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002517 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2518 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2519 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2520
mflodmancc3d4422017-08-03 08:27:51 -07002521 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002522}
2523
mflodmancc3d4422017-08-03 08:27:51 -07002524TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002525 const int kWidth = 1280;
2526 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002527 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002528 DataRate::BitsPerSec(kTargetBitrateBps),
2529 DataRate::BitsPerSec(kTargetBitrateBps),
2530 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002531
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002532 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002533 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002534 video_stream_encoder_->SetSource(&source,
2535 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002536 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2537 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002538 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002539
2540 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002541 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002542 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2543 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2544 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2545 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2546
2547 // Trigger adapt down for same input resolution, expect no change.
2548 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2549 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002550 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002551 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2552 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2553 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2554
2555 // Trigger adapt down for larger input resolution, expect no change.
2556 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2557 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002558 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002559 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2560 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2561 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2562
mflodmancc3d4422017-08-03 08:27:51 -07002563 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002564}
2565
mflodmancc3d4422017-08-03 08:27:51 -07002566TEST_F(VideoStreamEncoderTest,
2567 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002568 const int kWidth = 1280;
2569 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002570 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002571 DataRate::BitsPerSec(kTargetBitrateBps),
2572 DataRate::BitsPerSec(kTargetBitrateBps),
2573 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002574
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002575 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002576 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002577 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002578 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002579
2580 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002581 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002582 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002583 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2584 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2585
2586 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002587 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002588 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002589 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2590 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2591
mflodmancc3d4422017-08-03 08:27:51 -07002592 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002593}
2594
mflodmancc3d4422017-08-03 08:27:51 -07002595TEST_F(VideoStreamEncoderTest,
2596 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002597 const int kWidth = 1280;
2598 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002599 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002600 DataRate::BitsPerSec(kTargetBitrateBps),
2601 DataRate::BitsPerSec(kTargetBitrateBps),
2602 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002603
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002604 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002605 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002606 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002607 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002608
2609 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002610 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002611 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002612 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002613 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2614
2615 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002616 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002617 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002618 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002619 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2620
mflodmancc3d4422017-08-03 08:27:51 -07002621 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002622}
2623
mflodmancc3d4422017-08-03 08:27:51 -07002624TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002625 const int kWidth = 1280;
2626 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002627 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002628 DataRate::BitsPerSec(kTargetBitrateBps),
2629 DataRate::BitsPerSec(kTargetBitrateBps),
2630 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002631
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002632 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002633 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002634 video_stream_encoder_->SetSource(&source,
2635 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002636
2637 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2638 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002639 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002640 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2641 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2642 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2643
2644 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002645 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002646 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002647 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2648 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2649 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2650
mflodmancc3d4422017-08-03 08:27:51 -07002651 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002652}
2653
mflodmancc3d4422017-08-03 08:27:51 -07002654TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002655 const int kWidth = 1280;
2656 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002657 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002658 DataRate::BitsPerSec(kTargetBitrateBps),
2659 DataRate::BitsPerSec(kTargetBitrateBps),
2660 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002661
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002662 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002663 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002664 video_stream_encoder_->SetSource(&source,
2665 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002666
2667 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2668 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002669 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002670 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2671 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2672 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2673
2674 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002675 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002676 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002677 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2678 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2679 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2680
mflodmancc3d4422017-08-03 08:27:51 -07002681 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002682}
2683
mflodmancc3d4422017-08-03 08:27:51 -07002684TEST_F(VideoStreamEncoderTest,
2685 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002686 const int kWidth = 1280;
2687 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002688 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002689 DataRate::BitsPerSec(kTargetBitrateBps),
2690 DataRate::BitsPerSec(kTargetBitrateBps),
2691 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002692
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002693 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002694 AdaptingFrameForwarder source;
2695 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002696 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002697 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002698
2699 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002700 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002701 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002702 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2703 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2704
2705 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002706 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002707 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002708 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002709 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002710 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2711 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2712
2713 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002714 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002715 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002716 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2717 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2718 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2719
mflodmancc3d4422017-08-03 08:27:51 -07002720 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002721}
2722
mflodmancc3d4422017-08-03 08:27:51 -07002723TEST_F(VideoStreamEncoderTest,
2724 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002725 const int kWidth = 1280;
2726 const int kHeight = 720;
2727 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002728 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002729 DataRate::BitsPerSec(kTargetBitrateBps),
2730 DataRate::BitsPerSec(kTargetBitrateBps),
2731 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002732
2733 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2734 stats.input_frame_rate = kInputFps;
2735 stats_proxy_->SetMockStats(stats);
2736
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002737 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002738 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2739 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002740 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002741
2742 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002743 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002744 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2745 sink_.WaitForEncodedFrame(2);
2746 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2747
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002748 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002749 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002750 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002751 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Henrik Boström07b17df2020-01-15 11:42:12 +01002752 // Give the encoder queue time to process the change in degradation preference
2753 // by waiting for an encoded frame.
2754 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2755 sink_.WaitForEncodedFrame(3);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002756 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002757
2758 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002759 video_stream_encoder_->TriggerQualityLow();
Henrik Boström07b17df2020-01-15 11:42:12 +01002760 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
2761 sink_.WaitForEncodedFrame(4);
asapersson09f05612017-05-15 23:40:18 -07002762 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2763
2764 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002765 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002766 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002767
mflodmancc3d4422017-08-03 08:27:51 -07002768 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002769}
2770
mflodmancc3d4422017-08-03 08:27:51 -07002771TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002772 const int kWidth = 1280;
2773 const int kHeight = 720;
2774 const size_t kNumFrames = 10;
2775
Erik Språng4c6ca302019-04-08 15:14:01 +02002776 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002777 DataRate::BitsPerSec(kTargetBitrateBps),
2778 DataRate::BitsPerSec(kTargetBitrateBps),
2779 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002780
asaperssond0de2952017-04-21 01:47:31 -07002781 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002782 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002783 video_source_.set_adaptation_enabled(true);
2784
2785 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2786 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2787
2788 int downscales = 0;
2789 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002790 video_source_.IncomingCapturedFrame(
2791 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2792 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002793
asaperssonfab67072017-04-04 05:51:49 -07002794 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002795 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002796 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002797 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002798
2799 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2800 ++downscales;
2801
2802 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2803 EXPECT_EQ(downscales,
2804 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2805 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002806 }
mflodmancc3d4422017-08-03 08:27:51 -07002807 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002808}
2809
mflodmancc3d4422017-08-03 08:27:51 -07002810TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002811 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2812 const int kWidth = 1280;
2813 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002814 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002815 DataRate::BitsPerSec(kTargetBitrateBps),
2816 DataRate::BitsPerSec(kTargetBitrateBps),
2817 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002818
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002819 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002820 AdaptingFrameForwarder source;
2821 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002822 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002823 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002824
Åsa Persson8c1bf952018-09-13 10:42:19 +02002825 int64_t timestamp_ms = kFrameIntervalMs;
2826 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002827 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002828 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002829 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2830 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2831
2832 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002833 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002834 timestamp_ms += kFrameIntervalMs;
2835 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2836 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002837 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002838 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2839 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2840
2841 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002842 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002843 timestamp_ms += kFrameIntervalMs;
2844 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002845 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002846 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002847 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2848 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2849
2850 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002851 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002852 timestamp_ms += kFrameIntervalMs;
2853 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2854 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002855 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002856 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2857 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2858
2859 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002860 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002861 timestamp_ms += kFrameIntervalMs;
2862 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002863 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002864 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002865 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2866 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2867
mflodmancc3d4422017-08-03 08:27:51 -07002868 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002869}
2870
mflodmancc3d4422017-08-03 08:27:51 -07002871TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002872 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2873 const int kWidth = 1280;
2874 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002875 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002876 DataRate::BitsPerSec(kTargetBitrateBps),
2877 DataRate::BitsPerSec(kTargetBitrateBps),
2878 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002879
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002880 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002881 AdaptingFrameForwarder source;
2882 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002883 video_stream_encoder_->SetSource(&source,
2884 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002885
Åsa Persson8c1bf952018-09-13 10:42:19 +02002886 int64_t timestamp_ms = kFrameIntervalMs;
2887 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002888 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002889 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002890 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2891 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2892
2893 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002894 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002895 timestamp_ms += kFrameIntervalMs;
2896 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2897 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002898 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2899 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2900 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2901
2902 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002903 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002904 timestamp_ms += kFrameIntervalMs;
2905 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002906 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002907 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002908 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2909 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2910
2911 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002912 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002913 timestamp_ms += kFrameIntervalMs;
2914 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2915 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002916 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2917 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2918 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2919
2920 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002921 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002922 timestamp_ms += kFrameIntervalMs;
2923 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002924 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002925 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002926 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2927 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2928
mflodmancc3d4422017-08-03 08:27:51 -07002929 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002930}
2931
Sergey Silkin41c650b2019-10-14 13:12:19 +02002932TEST_F(VideoStreamEncoderTest, AdaptUpIfBwEstimateIsHigherThanMinBitrate) {
2933 fake_encoder_.SetResolutionBitrateLimits(
2934 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2935
2936 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002937 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2938 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2939 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2940 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002941
2942 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2943 AdaptingFrameForwarder source;
2944 source.set_adaptation_enabled(true);
2945 video_stream_encoder_->SetSource(
2946 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
2947
2948 // Insert 720p frame.
2949 int64_t timestamp_ms = kFrameIntervalMs;
2950 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2951 WaitForEncodedFrame(1280, 720);
2952
2953 // Reduce bitrate and trigger adapt down.
2954 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002955 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2956 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2957 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2958 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002959 video_stream_encoder_->TriggerQualityLow();
2960
2961 // Insert 720p frame. It should be downscaled and encoded.
2962 timestamp_ms += kFrameIntervalMs;
2963 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
2964 WaitForEncodedFrame(960, 540);
2965
2966 // Trigger adapt up. Higher resolution should not be requested duo to lack
2967 // of bitrate.
2968 video_stream_encoder_->TriggerQualityHigh();
2969 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
2970
2971 // Increase bitrate.
2972 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002973 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2974 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps),
2975 DataRate::BitsPerSec(kEncoderBitrateLimits720p.min_start_bitrate_bps), 0,
2976 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002977
2978 // Trigger adapt up. Higher resolution should be requested.
2979 video_stream_encoder_->TriggerQualityHigh();
2980 VerifyFpsMaxResolutionMax(source.sink_wants());
2981
2982 video_stream_encoder_->Stop();
2983}
2984
2985TEST_F(VideoStreamEncoderTest, DropFirstFramesIfBwEstimateIsTooLow) {
2986 fake_encoder_.SetResolutionBitrateLimits(
2987 {kEncoderBitrateLimits540p, kEncoderBitrateLimits720p});
2988
2989 // Set bitrate equal to min bitrate of 540p.
2990 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01002991 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2992 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps),
2993 DataRate::BitsPerSec(kEncoderBitrateLimits540p.min_start_bitrate_bps), 0,
2994 0, 0);
Sergey Silkin41c650b2019-10-14 13:12:19 +02002995
2996 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
2997 AdaptingFrameForwarder source;
2998 source.set_adaptation_enabled(true);
2999 video_stream_encoder_->SetSource(
3000 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
3001
3002 // Insert 720p frame. It should be dropped and lower resolution should be
3003 // requested.
3004 int64_t timestamp_ms = kFrameIntervalMs;
3005 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
3006 ExpectDroppedFrame();
3007 VerifyFpsMaxResolutionLt(source.sink_wants(), 1280 * 720);
3008
3009 // Insert 720p frame. It should be downscaled and encoded.
3010 timestamp_ms += kFrameIntervalMs;
3011 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, 1280, 720));
3012 WaitForEncodedFrame(960, 540);
3013
3014 video_stream_encoder_->Stop();
3015}
3016
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003017class BalancedDegradationTest : public VideoStreamEncoderTest {
3018 protected:
3019 void SetupTest() {
3020 // Reset encoder for field trials to take effect.
3021 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02003022 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003023
3024 // Enable BALANCED preference.
3025 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02003026 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
3027 }
3028
3029 void OnBitrateUpdated(int bitrate_bps) {
Ying Wang9b881ab2020-02-07 14:29:32 +01003030 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003031 DataRate::BitsPerSec(bitrate_bps), DataRate::BitsPerSec(bitrate_bps),
3032 DataRate::BitsPerSec(bitrate_bps), 0, 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003033 }
3034
Åsa Persson45b176f2019-09-30 11:19:05 +02003035 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003036 timestamp_ms_ += kFrameIntervalMs;
3037 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02003038 }
3039
3040 void InsertFrameAndWaitForEncoded() {
3041 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003042 sink_.WaitForEncodedFrame(timestamp_ms_);
3043 }
3044
3045 const int kWidth = 640; // pixels:640x360=230400
3046 const int kHeight = 360;
3047 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
3048 int64_t timestamp_ms_ = 0;
3049 AdaptingFrameForwarder source_;
3050};
3051
3052TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
3053 test::ScopedFieldTrials field_trials(
3054 "WebRTC-Video-BalancedDegradationSettings/"
3055 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
3056 SetupTest();
3057
3058 // Force input frame rate.
3059 const int kInputFps = 24;
3060 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3061 stats.input_frame_rate = kInputFps;
3062 stats_proxy_->SetMockStats(stats);
3063
Åsa Persson45b176f2019-09-30 11:19:05 +02003064 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003065 VerifyFpsMaxResolutionMax(source_.sink_wants());
3066
3067 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3068 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
3069 video_stream_encoder_->TriggerQualityLowExpectFalse();
3070 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3071
3072 video_stream_encoder_->Stop();
3073}
3074
3075TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
3076 test::ScopedFieldTrials field_trials(
3077 "WebRTC-Video-BalancedDegradationSettings/"
3078 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
3079 SetupTest();
3080
3081 // Force input frame rate.
3082 const int kInputFps = 25;
3083 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3084 stats.input_frame_rate = kInputFps;
3085 stats_proxy_->SetMockStats(stats);
3086
Åsa Persson45b176f2019-09-30 11:19:05 +02003087 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003088 VerifyFpsMaxResolutionMax(source_.sink_wants());
3089
3090 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
3091 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
3092 video_stream_encoder_->TriggerQualityLow();
3093 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
3094
3095 video_stream_encoder_->Stop();
3096}
3097
3098TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
3099 test::ScopedFieldTrials field_trials(
3100 "WebRTC-Video-BalancedDegradationSettings/"
3101 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
3102 SetupTest();
3103
3104 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
3105
Åsa Persson45b176f2019-09-30 11:19:05 +02003106 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003107 VerifyFpsMaxResolutionMax(source_.sink_wants());
3108
3109 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
3110 video_stream_encoder_->TriggerQualityLow();
3111 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
3112
3113 video_stream_encoder_->Stop();
3114}
3115
Åsa Perssonccfb3402019-09-25 15:13:04 +02003116TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003117 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02003118 "WebRTC-Video-BalancedDegradationSettings/"
3119 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003120 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02003121
Åsa Persson1b247f12019-08-14 17:26:39 +02003122 const int kMinBitrateBps = 425000;
3123 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003124 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003125
Åsa Persson45b176f2019-09-30 11:19:05 +02003126 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003127 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003128 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3129
3130 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3131 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003132 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003133 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02003134 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3135
3136 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3137 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003138 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003139 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003140 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3141
Åsa Persson30ab0152019-08-27 12:22:33 +02003142 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3143 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003144 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003145 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
3146 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02003147 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3148
3149 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02003150 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003151 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003152 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02003153
Åsa Persson30ab0152019-08-27 12:22:33 +02003154 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003155 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02003156 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003157 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003158 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003159 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3160
3161 video_stream_encoder_->Stop();
3162}
3163
Åsa Perssonccfb3402019-09-25 15:13:04 +02003164TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02003165 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
3166 test::ScopedFieldTrials field_trials(
3167 "WebRTC-Video-BalancedDegradationSettings/"
3168 "pixels:57600|129600|230400,fps:7|24|24/");
3169 SetupTest();
3170 OnBitrateUpdated(kLowTargetBitrateBps);
3171
3172 VerifyNoLimitation(source_.sink_wants());
3173
3174 // Insert frame, expect scaled down:
3175 // framerate (640x360@24fps) -> resolution (480x270@24fps).
3176 InsertFrame();
3177 EXPECT_FALSE(WaitForFrame(1000));
3178 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
3179 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3180
3181 // Insert frame, expect scaled down:
3182 // resolution (320x180@24fps).
3183 InsertFrame();
3184 EXPECT_FALSE(WaitForFrame(1000));
3185 EXPECT_LT(source_.sink_wants().max_pixel_count,
3186 source_.last_wants().max_pixel_count);
3187 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
3188
3189 // Frame should not be dropped (min pixels per frame reached).
3190 InsertFrameAndWaitForEncoded();
3191
3192 video_stream_encoder_->Stop();
3193}
3194
3195TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003196 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003197 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003198 "WebRTC-Video-BalancedDegradationSettings/"
3199 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003200 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003201
Åsa Persson30ab0152019-08-27 12:22:33 +02003202 const int kResolutionMinBitrateBps = 435000;
3203 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003204 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003205
Åsa Persson45b176f2019-09-30 11:19:05 +02003206 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003207 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003208 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3209
3210 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3211 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003212 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003213 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003214 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3215
3216 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3217 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003218 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003219 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003220 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3221
3222 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3223 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003224 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003225 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02003226 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3227
Åsa Persson30ab0152019-08-27 12:22:33 +02003228 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
3229 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003230 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003231 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003232 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3233
3234 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
3235 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003236 InsertFrameAndWaitForEncoded();
Å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 upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003240 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003241 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003242 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003243 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003244 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3245
3246 video_stream_encoder_->Stop();
3247}
3248
Åsa Perssonccfb3402019-09-25 15:13:04 +02003249TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02003250 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02003251 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02003252 "WebRTC-Video-BalancedDegradationSettings/"
3253 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02003254 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02003255
Åsa Persson30ab0152019-08-27 12:22:33 +02003256 const int kMinBitrateBps = 425000;
3257 const int kTooLowMinBitrateBps = 424000;
3258 const int kResolutionMinBitrateBps = 435000;
3259 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02003260 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003261
Åsa Persson45b176f2019-09-30 11:19:05 +02003262 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003263 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003264 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3265
3266 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
3267 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003268 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003269 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02003270 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3271
3272 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
3273 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003274 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003275 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003276 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3277
3278 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
3279 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02003280 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003281 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003282 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3283
3284 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
3285 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003286 InsertFrameAndWaitForEncoded();
Å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 upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003290 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003291 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003292 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003293 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003294 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3295
3296 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003297 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003298 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003299 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02003300 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3301
3302 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02003303 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02003304 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02003305 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02003306 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02003307 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3308
Åsa Persson1b247f12019-08-14 17:26:39 +02003309 video_stream_encoder_->Stop();
3310}
3311
mflodmancc3d4422017-08-03 08:27:51 -07003312TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003313 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3314 const int kWidth = 1280;
3315 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003316 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003317 DataRate::BitsPerSec(kTargetBitrateBps),
3318 DataRate::BitsPerSec(kTargetBitrateBps),
3319 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003320
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003321 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003322 AdaptingFrameForwarder source;
3323 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003324 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003325 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003326
Åsa Persson8c1bf952018-09-13 10:42:19 +02003327 int64_t timestamp_ms = kFrameIntervalMs;
3328 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003329 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003330 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003331 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3332 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3333 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3334 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3335
3336 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003337 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003338 timestamp_ms += kFrameIntervalMs;
3339 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3340 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003341 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003342 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3343 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3344 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3345 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3346
3347 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003348 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003349 timestamp_ms += kFrameIntervalMs;
3350 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3351 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003352 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003353 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3354 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3355 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3356 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3357
Jonathan Yubc771b72017-12-08 17:04:29 -08003358 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003359 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003360 timestamp_ms += kFrameIntervalMs;
3361 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3362 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003363 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003364 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3365 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003366 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003367 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3368
Jonathan Yubc771b72017-12-08 17:04:29 -08003369 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003370 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003371 timestamp_ms += kFrameIntervalMs;
3372 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3373 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003374 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003375 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003376 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3377 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3378 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3379 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3380
Jonathan Yubc771b72017-12-08 17:04:29 -08003381 // Trigger quality adapt down, expect no change (min resolution reached).
3382 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003383 timestamp_ms += kFrameIntervalMs;
3384 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3385 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003386 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3387 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3388 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3389 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3390 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3391
3392 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003393 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003394 timestamp_ms += kFrameIntervalMs;
3395 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3396 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003397 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003398 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3399 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3400 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3401 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3402
3403 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3404 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003405 timestamp_ms += kFrameIntervalMs;
3406 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3407 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003408 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3409 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3410 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3411 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3412 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3413
3414 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3415 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003416 timestamp_ms += kFrameIntervalMs;
3417 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3418 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003419 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003420 last_wants = source.sink_wants();
3421 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3422 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003423 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003424 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3425
3426 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003427 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003428 timestamp_ms += kFrameIntervalMs;
3429 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3430 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003431 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003432 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3433 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003434 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003435 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3436
3437 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003438 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003439 timestamp_ms += kFrameIntervalMs;
3440 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003441 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003442 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003443 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003444 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3445 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003446 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003447 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003448
mflodmancc3d4422017-08-03 08:27:51 -07003449 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003450}
3451
mflodmancc3d4422017-08-03 08:27:51 -07003452TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003453 const int kWidth = 640;
3454 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003455
Erik Språng4c6ca302019-04-08 15:14:01 +02003456 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003457 DataRate::BitsPerSec(kTargetBitrateBps),
3458 DataRate::BitsPerSec(kTargetBitrateBps),
3459 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003460
perkj803d97f2016-11-01 11:45:46 -07003461 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003462 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003463 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003464 }
3465
mflodmancc3d4422017-08-03 08:27:51 -07003466 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003467 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003468 video_source_.IncomingCapturedFrame(CreateFrame(
3469 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003470 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003471 }
3472
mflodmancc3d4422017-08-03 08:27:51 -07003473 video_stream_encoder_->Stop();
3474 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003475 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003476
Ying Wangef3998f2019-12-09 13:06:53 +01003477 EXPECT_METRIC_EQ(
3478 1, metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3479 EXPECT_METRIC_EQ(
perkj803d97f2016-11-01 11:45:46 -07003480 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3481}
3482
mflodmancc3d4422017-08-03 08:27:51 -07003483TEST_F(VideoStreamEncoderTest,
3484 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003485 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003486 DataRate::BitsPerSec(kTargetBitrateBps),
3487 DataRate::BitsPerSec(kTargetBitrateBps),
3488 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003489 const int kWidth = 640;
3490 const int kHeight = 360;
3491
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003492 video_stream_encoder_->SetSource(&video_source_,
3493 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003494
3495 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3496 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003497 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003498 }
3499
mflodmancc3d4422017-08-03 08:27:51 -07003500 video_stream_encoder_->Stop();
3501 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003502 stats_proxy_.reset();
3503
3504 EXPECT_EQ(0,
3505 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3506}
3507
mflodmancc3d4422017-08-03 08:27:51 -07003508TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003509 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003510 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003511
3512 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003513 const VideoBitrateAllocation expected_bitrate =
Mirta Dvornicic6799d732020-02-12 15:36:49 +01003514 SimulcastRateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003515 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3516 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003517
sprang57c2fff2017-01-16 06:24:02 -08003518 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003519 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003520 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003521 DataRate::BitsPerSec(kLowTargetBitrateBps),
3522 DataRate::BitsPerSec(kLowTargetBitrateBps),
3523 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003524
sprang57c2fff2017-01-16 06:24:02 -08003525 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003526 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3527 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003528 VideoBitrateAllocation bitrate_allocation =
3529 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003530 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003531 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003532 // TODO(srte): The use of millisecs here looks like an error, but the tests
3533 // fails using seconds, this should be investigated.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003534 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003535
3536 // Not called on second frame.
3537 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3538 .Times(0);
3539 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003540 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3541 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003542 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003543
3544 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003545 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3546 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003547 const int64_t start_time_ms = rtc::TimeMillis();
3548 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3549 video_source_.IncomingCapturedFrame(
3550 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3551 WaitForEncodedFrame(rtc::TimeMillis());
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003552 fake_clock_.AdvanceTime(TimeDelta::Millis(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003553 }
3554
3555 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003556 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003557
mflodmancc3d4422017-08-03 08:27:51 -07003558 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003559}
3560
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003561TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3562 // 2 TLs configured, temporal layers supported by encoder.
3563 const int kNumTemporalLayers = 2;
3564 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3565 fake_encoder_.SetTemporalLayersSupported(0, true);
3566
3567 // Bitrate allocated across temporal layers.
3568 const int kTl0Bps = kTargetBitrateBps *
3569 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003570 kNumTemporalLayers, /*temporal_id*/ 0,
3571 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003572 const int kTl1Bps = kTargetBitrateBps *
3573 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003574 kNumTemporalLayers, /*temporal_id*/ 1,
3575 /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003576 VideoBitrateAllocation expected_bitrate;
3577 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3578 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3579
3580 VerifyAllocatedBitrate(expected_bitrate);
3581 video_stream_encoder_->Stop();
3582}
3583
3584TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3585 // 2 TLs configured, temporal layers not supported by encoder.
3586 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3587 fake_encoder_.SetTemporalLayersSupported(0, false);
3588
3589 // Temporal layers not supported by the encoder.
3590 // Total bitrate should be at ti:0.
3591 VideoBitrateAllocation expected_bitrate;
3592 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3593
3594 VerifyAllocatedBitrate(expected_bitrate);
3595 video_stream_encoder_->Stop();
3596}
3597
3598TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3599 // 2 TLs configured, temporal layers only supported for first stream.
3600 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3601 fake_encoder_.SetTemporalLayersSupported(0, true);
3602 fake_encoder_.SetTemporalLayersSupported(1, false);
3603
3604 const int kS0Bps = 150000;
3605 const int kS0Tl0Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003606 kS0Bps *
3607 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3608 /*num_layers*/ 2, /*temporal_id*/ 0, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003609 const int kS0Tl1Bps =
Rasmus Brandt2b9317a2019-10-30 13:01:46 +01003610 kS0Bps *
3611 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3612 /*num_layers*/ 2, /*temporal_id*/ 1, /*base_heavy_tl3_alloc*/ false);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003613 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3614 // Temporal layers not supported by si:1.
3615 VideoBitrateAllocation expected_bitrate;
3616 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3617 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3618 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3619
3620 VerifyAllocatedBitrate(expected_bitrate);
3621 video_stream_encoder_->Stop();
3622}
3623
Niels Möller7dc26b72017-12-06 10:27:48 +01003624TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3625 const int kFrameWidth = 1280;
3626 const int kFrameHeight = 720;
3627 const int kFramerate = 24;
3628
Erik Språng4c6ca302019-04-08 15:14:01 +02003629 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003630 DataRate::BitsPerSec(kTargetBitrateBps),
3631 DataRate::BitsPerSec(kTargetBitrateBps),
3632 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003633 test::FrameForwarder source;
3634 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003635 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003636
3637 // Insert a single frame, triggering initial configuration.
3638 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3639 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3640
3641 EXPECT_EQ(
3642 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3643 kDefaultFramerate);
3644
3645 // Trigger reconfigure encoder (without resetting the entire instance).
3646 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003647 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003648 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3649 video_encoder_config.number_of_streams = 1;
3650 video_encoder_config.video_stream_factory =
3651 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3652 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003653 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003654 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3655
3656 // Detector should be updated with fps limit from codec config.
3657 EXPECT_EQ(
3658 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3659 kFramerate);
3660
3661 // Trigger overuse, max framerate should be reduced.
3662 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3663 stats.input_frame_rate = kFramerate;
3664 stats_proxy_->SetMockStats(stats);
3665 video_stream_encoder_->TriggerCpuOveruse();
3666 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3667 int adapted_framerate =
3668 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3669 EXPECT_LT(adapted_framerate, kFramerate);
3670
3671 // Trigger underuse, max framerate should go back to codec configured fps.
3672 // Set extra low fps, to make sure it's actually reset, not just incremented.
3673 stats = stats_proxy_->GetStats();
3674 stats.input_frame_rate = adapted_framerate / 2;
3675 stats_proxy_->SetMockStats(stats);
3676 video_stream_encoder_->TriggerCpuNormalUsage();
3677 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3678 EXPECT_EQ(
3679 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3680 kFramerate);
3681
3682 video_stream_encoder_->Stop();
3683}
3684
3685TEST_F(VideoStreamEncoderTest,
3686 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3687 const int kFrameWidth = 1280;
3688 const int kFrameHeight = 720;
3689 const int kLowFramerate = 15;
3690 const int kHighFramerate = 25;
3691
Erik Språng4c6ca302019-04-08 15:14:01 +02003692 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003693 DataRate::BitsPerSec(kTargetBitrateBps),
3694 DataRate::BitsPerSec(kTargetBitrateBps),
3695 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003696 test::FrameForwarder source;
3697 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003698 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003699
3700 // Trigger initial configuration.
3701 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003702 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003703 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3704 video_encoder_config.number_of_streams = 1;
3705 video_encoder_config.video_stream_factory =
3706 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3707 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3708 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003709 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003710 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3711
3712 EXPECT_EQ(
3713 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3714 kLowFramerate);
3715
3716 // Trigger overuse, max framerate should be reduced.
3717 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3718 stats.input_frame_rate = kLowFramerate;
3719 stats_proxy_->SetMockStats(stats);
3720 video_stream_encoder_->TriggerCpuOveruse();
3721 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3722 int adapted_framerate =
3723 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3724 EXPECT_LT(adapted_framerate, kLowFramerate);
3725
3726 // Reconfigure the encoder with a new (higher max framerate), max fps should
3727 // still respect the adaptation.
3728 video_encoder_config.video_stream_factory =
3729 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3730 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3731 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003732 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003733 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3734
3735 EXPECT_EQ(
3736 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3737 adapted_framerate);
3738
3739 // Trigger underuse, max framerate should go back to codec configured fps.
3740 stats = stats_proxy_->GetStats();
3741 stats.input_frame_rate = adapted_framerate;
3742 stats_proxy_->SetMockStats(stats);
3743 video_stream_encoder_->TriggerCpuNormalUsage();
3744 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3745 EXPECT_EQ(
3746 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3747 kHighFramerate);
3748
3749 video_stream_encoder_->Stop();
3750}
3751
mflodmancc3d4422017-08-03 08:27:51 -07003752TEST_F(VideoStreamEncoderTest,
3753 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003754 const int kFrameWidth = 1280;
3755 const int kFrameHeight = 720;
3756 const int kFramerate = 24;
3757
Erik Språng4c6ca302019-04-08 15:14:01 +02003758 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003759 DataRate::BitsPerSec(kTargetBitrateBps),
3760 DataRate::BitsPerSec(kTargetBitrateBps),
3761 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003762 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003763 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003764 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003765
3766 // Trigger initial configuration.
3767 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003768 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003769 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3770 video_encoder_config.number_of_streams = 1;
3771 video_encoder_config.video_stream_factory =
3772 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3773 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003774 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003775 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003776 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003777
Niels Möller7dc26b72017-12-06 10:27:48 +01003778 EXPECT_EQ(
3779 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3780 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003781
3782 // Trigger overuse, max framerate should be reduced.
3783 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3784 stats.input_frame_rate = kFramerate;
3785 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003786 video_stream_encoder_->TriggerCpuOveruse();
3787 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003788 int adapted_framerate =
3789 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003790 EXPECT_LT(adapted_framerate, kFramerate);
3791
3792 // Change degradation preference to not enable framerate scaling. Target
3793 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003794 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003795 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003796 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003797 EXPECT_EQ(
3798 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3799 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003800
mflodmancc3d4422017-08-03 08:27:51 -07003801 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003802}
3803
mflodmancc3d4422017-08-03 08:27:51 -07003804TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003805 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003806 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003807 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3808 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3809 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003810 const int kWidth = 640;
3811 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003812
asaperssonfab67072017-04-04 05:51:49 -07003813 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003814
3815 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003816 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003817
3818 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003819 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003820
sprangc5d62e22017-04-02 23:53:04 -07003821 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003822
asaperssonfab67072017-04-04 05:51:49 -07003823 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003824 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003825 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003826
3827 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003828 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003829
sprangc5d62e22017-04-02 23:53:04 -07003830 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003831
mflodmancc3d4422017-08-03 08:27:51 -07003832 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003833}
3834
mflodmancc3d4422017-08-03 08:27:51 -07003835TEST_F(VideoStreamEncoderTest,
3836 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003837 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003838 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003839 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3840 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3841 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003842 const int kWidth = 640;
3843 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003844
3845 // We expect the n initial frames to get dropped.
3846 int i;
3847 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003848 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003849 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003850 }
3851 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003852 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003853 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003854
3855 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003856 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003857
mflodmancc3d4422017-08-03 08:27:51 -07003858 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003859}
3860
mflodmancc3d4422017-08-03 08:27:51 -07003861TEST_F(VideoStreamEncoderTest,
3862 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003863 const int kWidth = 640;
3864 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003865 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003866 DataRate::BitsPerSec(kLowTargetBitrateBps),
3867 DataRate::BitsPerSec(kLowTargetBitrateBps),
3868 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003869
3870 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003871 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003872 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003873
asaperssonfab67072017-04-04 05:51:49 -07003874 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003875 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003876 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003877
mflodmancc3d4422017-08-03 08:27:51 -07003878 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003879}
3880
mflodmancc3d4422017-08-03 08:27:51 -07003881TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003882 const int kWidth = 640;
3883 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003884 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003885
3886 VideoEncoderConfig video_encoder_config;
3887 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3888 // Make format different, to force recreation of encoder.
3889 video_encoder_config.video_format.parameters["foo"] = "foo";
3890 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003891 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003892 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003893 DataRate::BitsPerSec(kLowTargetBitrateBps),
3894 DataRate::BitsPerSec(kLowTargetBitrateBps),
3895 DataRate::BitsPerSec(kLowTargetBitrateBps), 0, 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003896
kthelgasonb83797b2017-02-14 11:57:25 -08003897 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003898 video_stream_encoder_->SetSource(&video_source_,
3899 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003900
asaperssonfab67072017-04-04 05:51:49 -07003901 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003902 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003903 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003904
mflodmancc3d4422017-08-03 08:27:51 -07003905 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003906 fake_encoder_.SetQualityScaling(true);
3907}
3908
Åsa Persson139f4dc2019-08-02 09:29:58 +02003909TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3910 webrtc::test::ScopedFieldTrials field_trials(
3911 "WebRTC-Video-QualityScalerSettings/"
3912 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3913 // Reset encoder for field trials to take effect.
3914 ConfigureEncoder(video_encoder_config_.Copy());
3915 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3916 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3917 const int kWidth = 640;
3918 const int kHeight = 360;
3919
3920 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003921 DataRate::BitsPerSec(kTargetBitrateBps),
3922 DataRate::BitsPerSec(kTargetBitrateBps),
3923 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003924 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3925 // Frame should not be dropped.
3926 WaitForEncodedFrame(1);
3927
3928 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003929 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3930 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps),
3931 DataRate::BitsPerSec(kNotTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003932 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3933 // Frame should not be dropped.
3934 WaitForEncodedFrame(2);
3935
3936 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003937 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3938 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps),
3939 DataRate::BitsPerSec(kTooLowBitrateForFrameSizeBps), 0, 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003940 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3941 // Expect to drop this frame, the wait should time out.
3942 ExpectDroppedFrame();
3943
3944 // Expect the sink_wants to specify a scaled frame.
3945 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3946 video_stream_encoder_->Stop();
3947}
3948
Åsa Perssone644a032019-11-08 15:56:00 +01003949TEST_F(VideoStreamEncoderTest, RampsUpInQualityWhenBwIsHigh) {
3950 webrtc::test::ScopedFieldTrials field_trials(
3951 "WebRTC-Video-QualityRampupSettings/min_pixels:1,min_duration_ms:2000/");
3952
3953 // Reset encoder for field trials to take effect.
3954 VideoEncoderConfig config = video_encoder_config_.Copy();
3955 config.max_bitrate_bps = kTargetBitrateBps;
Evan Shrubsoledff79252020-04-16 11:34:32 +02003956 DataRate max_bitrate = DataRate::BitsPerSec(config.max_bitrate_bps);
Åsa Perssone644a032019-11-08 15:56:00 +01003957 ConfigureEncoder(std::move(config));
3958 fake_encoder_.SetQp(kQpLow);
3959
3960 // Enable MAINTAIN_FRAMERATE preference.
3961 AdaptingFrameForwarder source;
3962 source.set_adaptation_enabled(true);
3963 video_stream_encoder_->SetSource(&source,
3964 DegradationPreference::MAINTAIN_FRAMERATE);
3965
3966 // Start at low bitrate.
3967 const int kLowBitrateBps = 200000;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01003968 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(kLowBitrateBps),
3969 DataRate::BitsPerSec(kLowBitrateBps),
3970 DataRate::BitsPerSec(kLowBitrateBps),
3971 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003972
3973 // Expect first frame to be dropped and resolution to be limited.
3974 const int kWidth = 1280;
3975 const int kHeight = 720;
3976 const int64_t kFrameIntervalMs = 100;
3977 int64_t timestamp_ms = kFrameIntervalMs;
3978 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3979 ExpectDroppedFrame();
3980 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3981
3982 // Increase bitrate to encoder max.
Evan Shrubsoledff79252020-04-16 11:34:32 +02003983 video_stream_encoder_->OnBitrateUpdated(max_bitrate, max_bitrate, max_bitrate,
3984 0, 0, 0);
Åsa Perssone644a032019-11-08 15:56:00 +01003985
3986 // Insert frames and advance |min_duration_ms|.
3987 for (size_t i = 1; i <= 10; i++) {
3988 timestamp_ms += kFrameIntervalMs;
3989 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3990 WaitForEncodedFrame(timestamp_ms);
3991 }
3992 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3993 EXPECT_LT(source.sink_wants().max_pixel_count, kWidth * kHeight);
3994
Danil Chapovalov0c626af2020-02-10 11:16:00 +01003995 fake_clock_.AdvanceTime(TimeDelta::Millis(2000));
Åsa Perssone644a032019-11-08 15:56:00 +01003996
3997 // Insert frame should trigger high BW and release quality limitation.
3998 timestamp_ms += kFrameIntervalMs;
3999 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
4000 WaitForEncodedFrame(timestamp_ms);
4001 VerifyFpsMaxResolutionMax(source.sink_wants());
4002
4003 // Frame should not be adapted.
4004 timestamp_ms += kFrameIntervalMs;
4005 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
4006 WaitForEncodedFrame(kWidth, kHeight);
4007 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4008
4009 video_stream_encoder_->Stop();
4010}
4011
mflodmancc3d4422017-08-03 08:27:51 -07004012TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07004013 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
4014 const int kTooSmallWidth = 10;
4015 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02004016 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004017 DataRate::BitsPerSec(kTargetBitrateBps),
4018 DataRate::BitsPerSec(kTargetBitrateBps),
4019 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07004020
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004021 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07004022 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07004023 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004024 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07004025 VerifyNoLimitation(source.sink_wants());
4026 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4027
4028 // Trigger adapt down, too small frame, expect no change.
4029 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004030 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07004031 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004032 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07004033 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4034 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4035
mflodmancc3d4422017-08-03 08:27:51 -07004036 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07004037}
4038
mflodmancc3d4422017-08-03 08:27:51 -07004039TEST_F(VideoStreamEncoderTest,
4040 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004041 const int kTooSmallWidth = 10;
4042 const int kTooSmallHeight = 10;
4043 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02004044 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004045 DataRate::BitsPerSec(kTargetBitrateBps),
4046 DataRate::BitsPerSec(kTargetBitrateBps),
4047 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004048
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004049 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004050 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004051 video_stream_encoder_->SetSource(&source,
4052 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004053 VerifyNoLimitation(source.sink_wants());
4054 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4055 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4056
4057 // Trigger adapt down, expect limited framerate.
4058 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004059 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07004060 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004061 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4062 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4063 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4064 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4065
4066 // Trigger adapt down, too small frame, expect no change.
4067 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07004068 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07004069 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004070 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4071 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4072 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4073 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4074
mflodmancc3d4422017-08-03 08:27:51 -07004075 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004076}
4077
mflodmancc3d4422017-08-03 08:27:51 -07004078TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07004079 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02004080 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004081 DataRate::BitsPerSec(kTargetBitrateBps),
4082 DataRate::BitsPerSec(kTargetBitrateBps),
4083 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02004084 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07004085 const int kFrameWidth = 1280;
4086 const int kFrameHeight = 720;
4087 video_source_.IncomingCapturedFrame(
4088 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004089 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07004090 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07004091}
4092
sprangb1ca0732017-02-01 08:38:12 -08004093// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07004094TEST_F(VideoStreamEncoderTest,
4095 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004096 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004097 DataRate::BitsPerSec(kTargetBitrateBps),
4098 DataRate::BitsPerSec(kTargetBitrateBps),
4099 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08004100
4101 const int kFrameWidth = 1280;
4102 const int kFrameHeight = 720;
4103 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07004104 // requested by
4105 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08004106 video_source_.set_adaptation_enabled(true);
4107
4108 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004109 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004110 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004111
4112 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004113 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08004114 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004115 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004116 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08004117
asaperssonfab67072017-04-04 05:51:49 -07004118 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07004119 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08004120 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02004121 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004122 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08004123
mflodmancc3d4422017-08-03 08:27:51 -07004124 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08004125}
sprangfe627f32017-03-29 08:24:59 -07004126
mflodmancc3d4422017-08-03 08:27:51 -07004127TEST_F(VideoStreamEncoderTest,
4128 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07004129 const int kFrameWidth = 1280;
4130 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07004131
Erik Språng4c6ca302019-04-08 15:14:01 +02004132 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004133 DataRate::BitsPerSec(kTargetBitrateBps),
4134 DataRate::BitsPerSec(kTargetBitrateBps),
4135 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004136 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004137 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004138 video_source_.set_adaptation_enabled(true);
4139
sprang4847ae62017-06-27 07:06:52 -07004140 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004141
4142 video_source_.IncomingCapturedFrame(
4143 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004144 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004145
4146 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07004147 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004148
4149 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07004150 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004151 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004152 video_source_.IncomingCapturedFrame(
4153 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004154 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07004155 }
4156
4157 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07004158 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004159 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004160 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004161 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004162 video_source_.IncomingCapturedFrame(
4163 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004164 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004165 ++num_frames_dropped;
4166 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004167 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004168 }
4169 }
4170
sprang4847ae62017-06-27 07:06:52 -07004171 // Add some slack to account for frames dropped by the frame dropper.
4172 const int kErrorMargin = 1;
4173 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004174 kErrorMargin);
4175
4176 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07004177 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07004178 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02004179 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004180 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004181 video_source_.IncomingCapturedFrame(
4182 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004183 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004184 ++num_frames_dropped;
4185 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004186 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004187 }
4188 }
sprang4847ae62017-06-27 07:06:52 -07004189 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07004190 kErrorMargin);
4191
4192 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07004193 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004194 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004195 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004196 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004197 video_source_.IncomingCapturedFrame(
4198 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004199 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004200 ++num_frames_dropped;
4201 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004202 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004203 }
4204 }
sprang4847ae62017-06-27 07:06:52 -07004205 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07004206 kErrorMargin);
4207
4208 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07004209 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07004210 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07004211 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07004212 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07004213 video_source_.IncomingCapturedFrame(
4214 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004215 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07004216 ++num_frames_dropped;
4217 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01004218 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07004219 }
4220 }
4221 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
4222
mflodmancc3d4422017-08-03 08:27:51 -07004223 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004224}
4225
mflodmancc3d4422017-08-03 08:27:51 -07004226TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07004227 const int kFramerateFps = 5;
4228 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07004229 const int kFrameWidth = 1280;
4230 const int kFrameHeight = 720;
4231
sprang4847ae62017-06-27 07:06:52 -07004232 // Reconfigure encoder with two temporal layers and screensharing, which will
4233 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02004234 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07004235
Erik Språng4c6ca302019-04-08 15:14:01 +02004236 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004237 DataRate::BitsPerSec(kTargetBitrateBps),
4238 DataRate::BitsPerSec(kTargetBitrateBps),
4239 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004240 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004241 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07004242 video_source_.set_adaptation_enabled(true);
4243
sprang4847ae62017-06-27 07:06:52 -07004244 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07004245
4246 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08004247 rtc::VideoSinkWants last_wants;
4248 do {
4249 last_wants = video_source_.sink_wants();
4250
sprangc5d62e22017-04-02 23:53:04 -07004251 // Insert frames to get a new fps estimate...
4252 for (int j = 0; j < kFramerateFps; ++j) {
4253 video_source_.IncomingCapturedFrame(
4254 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08004255 if (video_source_.last_sent_width()) {
4256 sink_.WaitForEncodedFrame(timestamp_ms);
4257 }
sprangc5d62e22017-04-02 23:53:04 -07004258 timestamp_ms += kFrameIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004259 fake_clock_.AdvanceTime(TimeDelta::Millis(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07004260 }
4261 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07004262 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08004263 } while (video_source_.sink_wants().max_framerate_fps <
4264 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07004265
Jonathan Yubc771b72017-12-08 17:04:29 -08004266 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07004267
mflodmancc3d4422017-08-03 08:27:51 -07004268 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07004269}
asaperssonf7e294d2017-06-13 23:25:22 -07004270
mflodmancc3d4422017-08-03 08:27:51 -07004271TEST_F(VideoStreamEncoderTest,
4272 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07004273 const int kWidth = 1280;
4274 const int kHeight = 720;
4275 const int64_t kFrameIntervalMs = 150;
4276 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004277 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004278 DataRate::BitsPerSec(kTargetBitrateBps),
4279 DataRate::BitsPerSec(kTargetBitrateBps),
4280 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004281
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004282 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004283 AdaptingFrameForwarder source;
4284 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004285 video_stream_encoder_->SetSource(&source,
4286 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004287 timestamp_ms += kFrameIntervalMs;
4288 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004289 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004290 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004291 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4292 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4293 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4294
4295 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004296 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004297 timestamp_ms += kFrameIntervalMs;
4298 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004299 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004300 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4301 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4302 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4303 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4304
4305 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004306 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004307 timestamp_ms += kFrameIntervalMs;
4308 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004309 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004310 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4311 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4312 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4313 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4314
4315 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004316 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004317 timestamp_ms += kFrameIntervalMs;
4318 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004319 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004320 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4321 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4322 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4323 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4324
4325 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004326 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004327 timestamp_ms += kFrameIntervalMs;
4328 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004329 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004330 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4331 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4332 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4333 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4334
4335 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004336 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004337 timestamp_ms += kFrameIntervalMs;
4338 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004339 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004340 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4341 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4342 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4343 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4344
4345 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004346 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004347 timestamp_ms += kFrameIntervalMs;
4348 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004349 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004350 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4351 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4352 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4353 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4354
4355 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004356 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004357 timestamp_ms += kFrameIntervalMs;
4358 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004359 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004360 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4361 rtc::VideoSinkWants last_wants = source.sink_wants();
4362 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4363 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4364 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4365
4366 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004367 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004368 timestamp_ms += kFrameIntervalMs;
4369 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004370 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004371 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4372 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4373 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4374 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4375
4376 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004377 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004378 timestamp_ms += kFrameIntervalMs;
4379 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004380 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004381 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4382 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4383 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4384 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4385
4386 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004387 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004388 timestamp_ms += kFrameIntervalMs;
4389 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004390 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004391 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4392 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4393 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4394 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4395
4396 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004397 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004398 timestamp_ms += kFrameIntervalMs;
4399 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004400 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004401 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4402 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4403 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4404 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4405
4406 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004407 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004408 timestamp_ms += kFrameIntervalMs;
4409 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004410 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004411 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4412 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4413 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4414 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4415
4416 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004417 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004418 timestamp_ms += kFrameIntervalMs;
4419 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004420 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004421 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4422 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4423 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4424 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4425
4426 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004427 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004428 timestamp_ms += kFrameIntervalMs;
4429 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004430 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004431 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4432 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4433 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4434 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4435
Åsa Persson30ab0152019-08-27 12:22:33 +02004436 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004437 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004438 timestamp_ms += kFrameIntervalMs;
4439 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004440 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004441 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004442 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004443 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4444 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4445 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4446
4447 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004448 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004449 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004450 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4451
mflodmancc3d4422017-08-03 08:27:51 -07004452 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004453}
4454
mflodmancc3d4422017-08-03 08:27:51 -07004455TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004456 const int kWidth = 1280;
4457 const int kHeight = 720;
4458 const int64_t kFrameIntervalMs = 150;
4459 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004460 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004461 DataRate::BitsPerSec(kTargetBitrateBps),
4462 DataRate::BitsPerSec(kTargetBitrateBps),
4463 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004464
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004465 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004466 AdaptingFrameForwarder source;
4467 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004468 video_stream_encoder_->SetSource(&source,
4469 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004470 timestamp_ms += kFrameIntervalMs;
4471 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004472 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004473 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004474 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4475 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4476 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4477 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4478 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4479 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4480
4481 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004482 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004483 timestamp_ms += kFrameIntervalMs;
4484 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004485 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004486 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4487 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4488 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4489 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4490 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4491 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4492 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4493
4494 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004495 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004496 timestamp_ms += kFrameIntervalMs;
4497 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004498 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004499 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4500 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4501 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4502 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4503 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4504 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4505 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4506
4507 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004508 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004509 timestamp_ms += kFrameIntervalMs;
4510 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004511 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004512 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4513 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4514 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4515 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4516 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4517 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4518 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4519
4520 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004521 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004522 timestamp_ms += kFrameIntervalMs;
4523 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004524 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004525 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4526 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4527 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4528 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4529 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4530 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4531 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4532
4533 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004534 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004535 timestamp_ms += kFrameIntervalMs;
4536 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004537 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004538 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4539 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4540 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4541 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4542 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4543 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4544 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4545
4546 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004547 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004548 timestamp_ms += kFrameIntervalMs;
4549 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004550 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004551 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004552 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004553 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4554 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4555 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4556 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4557 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4558 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4559
4560 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004561 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004562 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004563 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4564 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4565
mflodmancc3d4422017-08-03 08:27:51 -07004566 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004567}
4568
mflodmancc3d4422017-08-03 08:27:51 -07004569TEST_F(VideoStreamEncoderTest,
4570 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004571 const int kWidth = 640;
4572 const int kHeight = 360;
4573 const int kFpsLimit = 15;
4574 const int64_t kFrameIntervalMs = 150;
4575 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004576 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004577 DataRate::BitsPerSec(kTargetBitrateBps),
4578 DataRate::BitsPerSec(kTargetBitrateBps),
4579 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004580
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004581 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004582 AdaptingFrameForwarder source;
4583 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004584 video_stream_encoder_->SetSource(&source,
4585 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004586 timestamp_ms += kFrameIntervalMs;
4587 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004588 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004589 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004590 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4591 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4592 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4593 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4594 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4595 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4596
4597 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004598 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004599 timestamp_ms += kFrameIntervalMs;
4600 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004601 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004602 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4603 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4604 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4605 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4606 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4607 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4608 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4609
4610 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004611 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004612 timestamp_ms += kFrameIntervalMs;
4613 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004614 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004615 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4616 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4617 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4618 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4619 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4620 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4621 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4622
4623 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004624 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004625 timestamp_ms += kFrameIntervalMs;
4626 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004627 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004628 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4629 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4630 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4631 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4632 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4633 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4634 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4635
4636 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004637 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004638 timestamp_ms += kFrameIntervalMs;
4639 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004640 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004641 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004642 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4643 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4644 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4645 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4646 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4647 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4648
4649 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004650 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004651 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004652 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4653 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4654
mflodmancc3d4422017-08-03 08:27:51 -07004655 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004656}
4657
mflodmancc3d4422017-08-03 08:27:51 -07004658TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004659 const int kFrameWidth = 1920;
4660 const int kFrameHeight = 1080;
4661 // 3/4 of 1920.
4662 const int kAdaptedFrameWidth = 1440;
4663 // 3/4 of 1080 rounded down to multiple of 4.
4664 const int kAdaptedFrameHeight = 808;
4665 const int kFramerate = 24;
4666
Erik Språng4c6ca302019-04-08 15:14:01 +02004667 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004668 DataRate::BitsPerSec(kTargetBitrateBps),
4669 DataRate::BitsPerSec(kTargetBitrateBps),
4670 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004671 // Trigger reconfigure encoder (without resetting the entire instance).
4672 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004673 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004674 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4675 video_encoder_config.number_of_streams = 1;
4676 video_encoder_config.video_stream_factory =
4677 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004678 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004679 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004680 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004681
4682 video_source_.set_adaptation_enabled(true);
4683
4684 video_source_.IncomingCapturedFrame(
4685 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004686 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004687
4688 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004689 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004690 video_source_.IncomingCapturedFrame(
4691 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004692 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004693
mflodmancc3d4422017-08-03 08:27:51 -07004694 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004695}
4696
mflodmancc3d4422017-08-03 08:27:51 -07004697TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004698 const int kFrameWidth = 1280;
4699 const int kFrameHeight = 720;
4700 const int kLowFps = 2;
4701 const int kHighFps = 30;
4702
Erik Språng4c6ca302019-04-08 15:14:01 +02004703 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004704 DataRate::BitsPerSec(kTargetBitrateBps),
4705 DataRate::BitsPerSec(kTargetBitrateBps),
4706 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004707
4708 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4709 max_framerate_ = kLowFps;
4710
4711 // Insert 2 seconds of 2fps video.
4712 for (int i = 0; i < kLowFps * 2; ++i) {
4713 video_source_.IncomingCapturedFrame(
4714 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4715 WaitForEncodedFrame(timestamp_ms);
4716 timestamp_ms += 1000 / kLowFps;
4717 }
4718
4719 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004720 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004721 DataRate::BitsPerSec(kTargetBitrateBps),
4722 DataRate::BitsPerSec(kTargetBitrateBps),
4723 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004724 video_source_.IncomingCapturedFrame(
4725 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4726 WaitForEncodedFrame(timestamp_ms);
4727 timestamp_ms += 1000 / kLowFps;
4728
4729 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4730
4731 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004732 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004733 const int kFrameIntervalMs = 1000 / kHighFps;
4734 max_framerate_ = kHighFps;
4735 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4736 video_source_.IncomingCapturedFrame(
4737 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4738 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4739 // be dropped if the encoder hans't been updated with the new higher target
4740 // framerate yet, causing it to overshoot the target bitrate and then
4741 // suffering the wrath of the media optimizer.
4742 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4743 timestamp_ms += kFrameIntervalMs;
4744 }
4745
4746 // Don expect correct measurement just yet, but it should be higher than
4747 // before.
4748 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4749
mflodmancc3d4422017-08-03 08:27:51 -07004750 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004751}
4752
mflodmancc3d4422017-08-03 08:27:51 -07004753TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004754 const int kFrameWidth = 1280;
4755 const int kFrameHeight = 720;
4756 const int kTargetBitrateBps = 1000000;
4757
4758 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004759 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004760 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004761 DataRate::BitsPerSec(kTargetBitrateBps),
4762 DataRate::BitsPerSec(kTargetBitrateBps),
4763 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004764 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004765
4766 // Insert a first video frame, causes another bitrate update.
4767 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4768 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4769 video_source_.IncomingCapturedFrame(
4770 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4771 WaitForEncodedFrame(timestamp_ms);
4772
4773 // Next, simulate video suspension due to pacer queue overrun.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004774 video_stream_encoder_->OnBitrateUpdated(DataRate::BitsPerSec(0),
4775 DataRate::BitsPerSec(0),
4776 DataRate::BitsPerSec(0), 0, 1, 0);
sprang4847ae62017-06-27 07:06:52 -07004777
4778 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004779 timestamp_ms += kProcessIntervalMs;
Danil Chapovalov0c626af2020-02-10 11:16:00 +01004780 fake_clock_.AdvanceTime(TimeDelta::Millis(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004781
4782 // Bitrate observer should not be called.
4783 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4784 video_source_.IncomingCapturedFrame(
4785 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4786 ExpectDroppedFrame();
4787
mflodmancc3d4422017-08-03 08:27:51 -07004788 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004789}
ilnik6b826ef2017-06-16 06:53:48 -07004790
Niels Möller4db138e2018-04-19 09:04:13 +02004791TEST_F(VideoStreamEncoderTest,
4792 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4793 const int kFrameWidth = 1280;
4794 const int kFrameHeight = 720;
4795 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004796 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004797 DataRate::BitsPerSec(kTargetBitrateBps),
4798 DataRate::BitsPerSec(kTargetBitrateBps),
4799 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004800 video_source_.IncomingCapturedFrame(
4801 CreateFrame(1, kFrameWidth, kFrameHeight));
4802 WaitForEncodedFrame(1);
4803 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4804 .low_encode_usage_threshold_percent,
4805 default_options.low_encode_usage_threshold_percent);
4806 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4807 .high_encode_usage_threshold_percent,
4808 default_options.high_encode_usage_threshold_percent);
4809 video_stream_encoder_->Stop();
4810}
4811
4812TEST_F(VideoStreamEncoderTest,
4813 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4814 const int kFrameWidth = 1280;
4815 const int kFrameHeight = 720;
4816 CpuOveruseOptions hardware_options;
4817 hardware_options.low_encode_usage_threshold_percent = 150;
4818 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004819 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004820
Erik Språng4c6ca302019-04-08 15:14:01 +02004821 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004822 DataRate::BitsPerSec(kTargetBitrateBps),
4823 DataRate::BitsPerSec(kTargetBitrateBps),
4824 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004825 video_source_.IncomingCapturedFrame(
4826 CreateFrame(1, kFrameWidth, kFrameHeight));
4827 WaitForEncodedFrame(1);
4828 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4829 .low_encode_usage_threshold_percent,
4830 hardware_options.low_encode_usage_threshold_percent);
4831 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4832 .high_encode_usage_threshold_percent,
4833 hardware_options.high_encode_usage_threshold_percent);
4834 video_stream_encoder_->Stop();
4835}
4836
Niels Möller6bb5ab92019-01-11 11:11:10 +01004837TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4838 const int kFrameWidth = 320;
4839 const int kFrameHeight = 240;
4840 const int kFps = 30;
4841 const int kTargetBitrateBps = 120000;
4842 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4843
Erik Språng4c6ca302019-04-08 15:14:01 +02004844 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004845 DataRate::BitsPerSec(kTargetBitrateBps),
4846 DataRate::BitsPerSec(kTargetBitrateBps),
4847 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004848
4849 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4850 max_framerate_ = kFps;
4851
4852 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4853 fake_encoder_.SimulateOvershoot(1.0);
4854 int num_dropped = 0;
4855 for (int i = 0; i < kNumFramesInRun; ++i) {
4856 video_source_.IncomingCapturedFrame(
4857 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4858 // Wait up to two frame durations for a frame to arrive.
4859 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4860 ++num_dropped;
4861 }
4862 timestamp_ms += 1000 / kFps;
4863 }
4864
Erik Språnga8d48ab2019-02-08 14:17:40 +01004865 // Framerate should be measured to be near the expected target rate.
4866 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4867
4868 // Frame drops should be within 5% of expected 0%.
4869 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004870
4871 // Make encoder produce frames at double the expected bitrate during 3 seconds
4872 // of video, verify number of drops. Rate needs to be slightly changed in
4873 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004874 double overshoot_factor = 2.0;
4875 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4876 // With bitrate adjuster, when need to overshoot even more to trigger
4877 // frame dropping.
4878 overshoot_factor *= 2;
4879 }
4880 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004881 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004882 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4883 DataRate::BitsPerSec(kTargetBitrateBps + 1000),
4884 DataRate::BitsPerSec(kTargetBitrateBps + 1000), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004885 num_dropped = 0;
4886 for (int i = 0; i < kNumFramesInRun; ++i) {
4887 video_source_.IncomingCapturedFrame(
4888 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4889 // Wait up to two frame durations for a frame to arrive.
4890 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4891 ++num_dropped;
4892 }
4893 timestamp_ms += 1000 / kFps;
4894 }
4895
Erik Språng4c6ca302019-04-08 15:14:01 +02004896 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004897 DataRate::BitsPerSec(kTargetBitrateBps),
4898 DataRate::BitsPerSec(kTargetBitrateBps),
4899 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004900
4901 // Target framerate should be still be near the expected target, despite
4902 // the frame drops.
4903 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4904
4905 // Frame drops should be within 5% of expected 50%.
4906 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004907
4908 video_stream_encoder_->Stop();
4909}
4910
4911TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4912 const int kFrameWidth = 320;
4913 const int kFrameHeight = 240;
4914 const int kActualInputFps = 24;
4915 const int kTargetBitrateBps = 120000;
4916
4917 ASSERT_GT(max_framerate_, kActualInputFps);
4918
4919 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4920 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004921 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004922 DataRate::BitsPerSec(kTargetBitrateBps),
4923 DataRate::BitsPerSec(kTargetBitrateBps),
4924 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004925
4926 // Insert 3 seconds of video, with an input fps lower than configured max.
4927 for (int i = 0; i < kActualInputFps * 3; ++i) {
4928 video_source_.IncomingCapturedFrame(
4929 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4930 // Wait up to two frame durations for a frame to arrive.
4931 WaitForEncodedFrame(timestamp_ms);
4932 timestamp_ms += 1000 / kActualInputFps;
4933 }
4934
4935 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4936
4937 video_stream_encoder_->Stop();
4938}
4939
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004940TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4941 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004942 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004943 DataRate::BitsPerSec(kTargetBitrateBps),
4944 DataRate::BitsPerSec(kTargetBitrateBps),
4945 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004946
4947 fake_encoder_.BlockNextEncode();
4948 video_source_.IncomingCapturedFrame(
4949 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4950 WaitForEncodedFrame(1);
4951 // On the very first frame full update should be forced.
4952 rect = fake_encoder_.GetLastUpdateRect();
4953 EXPECT_EQ(rect.offset_x, 0);
4954 EXPECT_EQ(rect.offset_y, 0);
4955 EXPECT_EQ(rect.height, codec_height_);
4956 EXPECT_EQ(rect.width, codec_width_);
4957 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4958 // call to ContinueEncode.
4959 video_source_.IncomingCapturedFrame(
4960 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4961 ExpectDroppedFrame();
4962 video_source_.IncomingCapturedFrame(
4963 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4964 ExpectDroppedFrame();
4965 fake_encoder_.ContinueEncode();
4966 WaitForEncodedFrame(3);
4967 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4968 rect = fake_encoder_.GetLastUpdateRect();
4969 EXPECT_EQ(rect.offset_x, 1);
4970 EXPECT_EQ(rect.offset_y, 0);
4971 EXPECT_EQ(rect.width, 10);
4972 EXPECT_EQ(rect.height, 1);
4973
4974 video_source_.IncomingCapturedFrame(
4975 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4976 WaitForEncodedFrame(4);
4977 // Previous frame was encoded, so no accumulation should happen.
4978 rect = fake_encoder_.GetLastUpdateRect();
4979 EXPECT_EQ(rect.offset_x, 0);
4980 EXPECT_EQ(rect.offset_y, 0);
4981 EXPECT_EQ(rect.width, 1);
4982 EXPECT_EQ(rect.height, 1);
4983
4984 video_stream_encoder_->Stop();
4985}
4986
Erik Språngd7329ca2019-02-21 21:19:53 +01004987TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004988 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01004989 DataRate::BitsPerSec(kTargetBitrateBps),
4990 DataRate::BitsPerSec(kTargetBitrateBps),
4991 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004992
4993 // First frame is always keyframe.
4994 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4995 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004996 EXPECT_THAT(
4997 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004998 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004999
5000 // Insert delta frame.
5001 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
5002 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01005003 EXPECT_THAT(
5004 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005005 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005006
5007 // Request next frame be a key-frame.
5008 video_stream_encoder_->SendKeyFrame();
5009 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
5010 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01005011 EXPECT_THAT(
5012 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005013 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005014
5015 video_stream_encoder_->Stop();
5016}
5017
5018TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
5019 // Setup simulcast with three streams.
5020 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01005021 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005022 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
5023 DataRate::BitsPerSec(kSimulcastTargetBitrateBps),
5024 DataRate::BitsPerSec(kSimulcastTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01005025 // Wait for all three layers before triggering event.
5026 sink_.SetNumExpectedLayers(3);
5027
5028 // First frame is always keyframe.
5029 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
5030 WaitForEncodedFrame(1);
5031 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005032 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
5033 VideoFrameType::kVideoFrameKey,
5034 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005035
5036 // Insert delta frame.
5037 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
5038 WaitForEncodedFrame(2);
5039 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005040 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
5041 VideoFrameType::kVideoFrameDelta,
5042 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005043
5044 // Request next frame be a key-frame.
5045 // Only first stream is configured to produce key-frame.
5046 video_stream_encoder_->SendKeyFrame();
5047 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
5048 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02005049
5050 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
5051 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01005052 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005053 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02005054 VideoFrameType::kVideoFrameKey,
5055 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005056
5057 video_stream_encoder_->Stop();
5058}
5059
5060TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
5061 // Configure internal source factory and setup test again.
5062 encoder_factory_.SetHasInternalSource(true);
5063 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005064 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005065 DataRate::BitsPerSec(kTargetBitrateBps),
5066 DataRate::BitsPerSec(kTargetBitrateBps),
5067 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01005068
5069 // Call encoder directly, simulating internal source where encoded frame
5070 // callback in VideoStreamEncoder is called despite no OnFrame().
5071 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
5072 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005073 EXPECT_THAT(
5074 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005075 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005076
Niels Möller8f7ce222019-03-21 15:43:58 +01005077 const std::vector<VideoFrameType> kDeltaFrame = {
5078 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01005079 // Need to set timestamp manually since manually for injected frame.
5080 VideoFrame frame = CreateFrame(101, nullptr);
5081 frame.set_timestamp(101);
5082 fake_encoder_.InjectFrame(frame, false);
5083 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005084 EXPECT_THAT(
5085 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005086 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005087
5088 // Request key-frame. The forces a dummy frame down into the encoder.
5089 fake_encoder_.ExpectNullFrame();
5090 video_stream_encoder_->SendKeyFrame();
5091 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01005092 EXPECT_THAT(
5093 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005094 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01005095
5096 video_stream_encoder_->Stop();
5097}
Erik Språngb7cb7b52019-02-26 15:52:33 +01005098
5099TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
5100 // Configure internal source factory and setup test again.
5101 encoder_factory_.SetHasInternalSource(true);
5102 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02005103 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005104 DataRate::BitsPerSec(kTargetBitrateBps),
5105 DataRate::BitsPerSec(kTargetBitrateBps),
5106 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01005107
5108 int64_t timestamp = 1;
5109 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02005110 image.SetEncodedData(
5111 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01005112 image.capture_time_ms_ = ++timestamp;
5113 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
5114 const int64_t kEncodeFinishDelayMs = 10;
5115 image.timing_.encode_start_ms = timestamp;
5116 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
5117 fake_encoder_.InjectEncodedImage(image);
5118 // Wait for frame without incrementing clock.
5119 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5120 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
5121 // capture timestamp should be kEncodeFinishDelayMs in the past.
5122 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
5123 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
5124 kEncodeFinishDelayMs);
5125
5126 video_stream_encoder_->Stop();
5127}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02005128
5129TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
5130 // Configure internal source factory and setup test again.
5131 encoder_factory_.SetHasInternalSource(true);
5132 ResetEncoder("H264", 1, 1, 1, false);
5133
5134 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
5135 image._frameType = VideoFrameType::kVideoFrameKey;
5136
5137 CodecSpecificInfo codec_specific_info;
5138 codec_specific_info.codecType = kVideoCodecH264;
5139
5140 RTPFragmentationHeader fragmentation;
5141 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5142 fragmentation.fragmentationOffset[0] = 4;
5143 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
5144
5145 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5146 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5147
5148 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5149 testing::ElementsAreArray(optimal_sps));
5150 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5151 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5152 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5153 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5154
5155 video_stream_encoder_->Stop();
5156}
5157
5158TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
5159 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
5160 0x00, 0x00, 0x03, 0x03, 0xF4,
5161 0x05, 0x03, 0xC7, 0xC0};
5162
5163 // Configure internal source factory and setup test again.
5164 encoder_factory_.SetHasInternalSource(true);
5165 ResetEncoder("H264", 1, 1, 1, false);
5166
5167 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
5168 image._frameType = VideoFrameType::kVideoFrameKey;
5169
5170 CodecSpecificInfo codec_specific_info;
5171 codec_specific_info.codecType = kVideoCodecH264;
5172
5173 RTPFragmentationHeader fragmentation;
5174 fragmentation.VerifyAndAllocateFragmentationHeader(1);
5175 fragmentation.fragmentationOffset[0] = 4;
5176 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
5177
5178 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
5179 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
5180
5181 EXPECT_THAT(sink_.GetLastEncodedImageData(),
5182 testing::ElementsAreArray(optimal_sps));
5183 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
5184 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
5185 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
5186 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
5187
5188 video_stream_encoder_->Stop();
5189}
5190
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005191TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
5192 const int kFrameWidth = 1280;
5193 const int kFrameHeight = 720;
5194 const int kTargetBitrateBps = 300000; // To low for HD resolution.
5195
5196 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005197 DataRate::BitsPerSec(kTargetBitrateBps),
5198 DataRate::BitsPerSec(kTargetBitrateBps),
5199 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02005200 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5201
5202 // Insert a first video frame. It should be dropped because of downscale in
5203 // resolution.
5204 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5205 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5206 frame.set_rotation(kVideoRotation_270);
5207 video_source_.IncomingCapturedFrame(frame);
5208
5209 ExpectDroppedFrame();
5210
5211 // Second frame is downscaled.
5212 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5213 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5214 frame.set_rotation(kVideoRotation_90);
5215 video_source_.IncomingCapturedFrame(frame);
5216
5217 WaitForEncodedFrame(timestamp_ms);
5218 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
5219
5220 // Insert another frame, also downscaled.
5221 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5222 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
5223 frame.set_rotation(kVideoRotation_180);
5224 video_source_.IncomingCapturedFrame(frame);
5225
5226 WaitForEncodedFrame(timestamp_ms);
5227 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
5228
5229 video_stream_encoder_->Stop();
5230}
5231
Erik Språng5056af02019-09-02 15:53:11 +02005232TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
5233 const int kFrameWidth = 320;
5234 const int kFrameHeight = 180;
5235
5236 // Initial rate.
5237 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005238 /*target_bitrate=*/DataRate::KilobitsPerSec(300),
5239 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(300),
5240 /*link_allocation=*/DataRate::KilobitsPerSec(300),
Erik Språng5056af02019-09-02 15:53:11 +02005241 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005242 /*rtt_ms=*/0,
5243 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005244
5245 // Insert a first video frame so that encoder gets configured.
5246 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5247 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5248 frame.set_rotation(kVideoRotation_270);
5249 video_source_.IncomingCapturedFrame(frame);
5250 WaitForEncodedFrame(timestamp_ms);
5251
5252 // Set a target rate below the minimum allowed by the codec settings.
5253 VideoCodec codec_config = fake_encoder_.codec_config();
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005254 DataRate min_rate = DataRate::KilobitsPerSec(codec_config.minBitrate);
5255 DataRate target_rate = min_rate - DataRate::KilobitsPerSec(1);
Erik Språng5056af02019-09-02 15:53:11 +02005256 video_stream_encoder_->OnBitrateUpdated(
5257 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02005258 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02005259 /*link_allocation=*/target_rate,
5260 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005261 /*rtt_ms=*/0,
5262 /*cwnd_reduce_ratio=*/0);
Erik Språng5056af02019-09-02 15:53:11 +02005263 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5264
5265 // Target bitrate and bandwidth allocation should both be capped at min_rate.
5266 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5267 ASSERT_TRUE(rate_settings.has_value());
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005268 DataRate allocation_sum =
5269 DataRate::BitsPerSec(rate_settings->bitrate.get_sum_bps());
Erik Språng5056af02019-09-02 15:53:11 +02005270 EXPECT_EQ(min_rate, allocation_sum);
5271 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
5272
5273 video_stream_encoder_->Stop();
5274}
5275
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005276TEST_F(VideoStreamEncoderTest, EncoderRatesPropagatedOnReconfigure) {
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005277 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005278 DataRate::BitsPerSec(kTargetBitrateBps),
5279 DataRate::BitsPerSec(kTargetBitrateBps),
5280 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005281 // Capture a frame and wait for it to synchronize with the encoder thread.
5282 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5283 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5284 WaitForEncodedFrame(1);
5285
5286 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
5287 ASSERT_TRUE(prev_rate_settings.has_value());
5288 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
5289 kDefaultFramerate);
5290
5291 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
5292 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
5293 timestamp_ms += 1000 / kDefaultFramerate;
5294 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5295 WaitForEncodedFrame(timestamp_ms);
5296 }
5297 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
5298 kDefaultFramerate);
5299 // Capture larger frame to trigger a reconfigure.
5300 codec_height_ *= 2;
5301 codec_width_ *= 2;
5302 timestamp_ms += 1000 / kDefaultFramerate;
5303 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
5304 WaitForEncodedFrame(timestamp_ms);
5305
5306 EXPECT_EQ(2, sink_.number_of_reconfigurations());
5307 auto current_rate_settings =
5308 fake_encoder_.GetAndResetLastRateControlSettings();
5309 // Ensure we have actually reconfigured twice
5310 // The rate settings should have been set again even though
5311 // they haven't changed.
5312 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005313 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02005314
5315 video_stream_encoder_->Stop();
5316}
5317
philipeld9cc8c02019-09-16 14:53:40 +02005318struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
5319 MOCK_METHOD0(RequestEncoderFallback, void());
5320 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
philipel9b058032020-02-10 11:30:00 +01005321 MOCK_METHOD1(RequestEncoderSwitch,
5322 void(const webrtc::SdpVideoFormat& format));
philipeld9cc8c02019-09-16 14:53:40 +02005323};
5324
5325TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
5326 constexpr int kDontCare = 100;
5327
5328 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5329 video_send_config_.encoder_settings.encoder_switch_request_callback =
5330 &switch_callback;
5331 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5332 encoder_config.codec_type = kVideoCodecVP8;
5333 webrtc::test::ScopedFieldTrials field_trial(
5334 "WebRTC-NetworkCondition-EncoderSwitch/"
5335 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5336 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5337
5338 // Reset encoder for new configuration to take effect.
5339 ConfigureEncoder(std::move(encoder_config));
5340
5341 // Send one frame to trigger ReconfigureEncoder.
5342 video_source_.IncomingCapturedFrame(
5343 CreateFrame(kDontCare, kDontCare, kDontCare));
5344
5345 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005346 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5347 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005348 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005349 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005350
5351 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005352 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5353 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5354 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipeld9cc8c02019-09-16 14:53:40 +02005355 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005356 /*rtt_ms=*/0,
5357 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005358
5359 video_stream_encoder_->Stop();
5360}
5361
Mirta Dvornicic5ed40cf2020-02-21 16:35:51 +01005362TEST_F(VideoStreamEncoderTest, VideoSuspendedNoEncoderSwitch) {
5363 constexpr int kDontCare = 100;
5364
5365 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5366 video_send_config_.encoder_settings.encoder_switch_request_callback =
5367 &switch_callback;
5368 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5369 encoder_config.codec_type = kVideoCodecVP8;
5370 webrtc::test::ScopedFieldTrials field_trial(
5371 "WebRTC-NetworkCondition-EncoderSwitch/"
5372 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
5373 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5374
5375 // Reset encoder for new configuration to take effect.
5376 ConfigureEncoder(std::move(encoder_config));
5377
5378 // Send one frame to trigger ReconfigureEncoder.
5379 video_source_.IncomingCapturedFrame(
5380 CreateFrame(kDontCare, kDontCare, kDontCare));
5381
5382 using Config = EncoderSwitchRequestCallback::Config;
5383 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(_)))
5384 .Times(0);
5385
5386 video_stream_encoder_->OnBitrateUpdated(
5387 /*target_bitrate=*/DataRate::KilobitsPerSec(0),
5388 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(0),
5389 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
5390 /*fraction_lost=*/0,
5391 /*rtt_ms=*/0,
5392 /*cwnd_reduce_ratio=*/0);
5393
5394 video_stream_encoder_->Stop();
5395}
5396
philipeld9cc8c02019-09-16 14:53:40 +02005397TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
5398 constexpr int kSufficientBitrateToNotDrop = 1000;
5399 constexpr int kHighRes = 500;
5400 constexpr int kLowRes = 100;
5401
5402 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5403 video_send_config_.encoder_settings.encoder_switch_request_callback =
5404 &switch_callback;
5405 webrtc::test::ScopedFieldTrials field_trial(
5406 "WebRTC-NetworkCondition-EncoderSwitch/"
5407 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
5408 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
5409 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
5410 encoder_config.codec_type = kVideoCodecH264;
5411
5412 // Reset encoder for new configuration to take effect.
5413 ConfigureEncoder(std::move(encoder_config));
5414
5415 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5416 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5417 // not fail.
5418 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005419 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5420 /*stable_target_bitrate=*/
5421 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5422 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipeld9cc8c02019-09-16 14:53:40 +02005423 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005424 /*rtt_ms=*/0,
5425 /*cwnd_reduce_ratio=*/0);
philipeld9cc8c02019-09-16 14:53:40 +02005426
5427 // Send one frame to trigger ReconfigureEncoder.
5428 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
5429 WaitForEncodedFrame(1);
5430
5431 using Config = EncoderSwitchRequestCallback::Config;
philipel9b058032020-02-10 11:30:00 +01005432 EXPECT_CALL(switch_callback, RequestEncoderSwitch(Matcher<const Config&>(
5433 AllOf(Field(&Config::codec_name, "AV1"),
philipeld9cc8c02019-09-16 14:53:40 +02005434 Field(&Config::param, "ping"),
philipel9b058032020-02-10 11:30:00 +01005435 Field(&Config::value, "pong")))));
philipeld9cc8c02019-09-16 14:53:40 +02005436
5437 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
5438 WaitForEncodedFrame(2);
5439
5440 video_stream_encoder_->Stop();
5441}
5442
philipel9b058032020-02-10 11:30:00 +01005443TEST_F(VideoStreamEncoderTest, EncoderSelectorCurrentEncoderIsSignaled) {
5444 constexpr int kDontCare = 100;
5445 StrictMock<MockEncoderSelector> encoder_selector;
5446 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5447 &fake_encoder_, &encoder_selector);
5448 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5449
5450 // Reset encoder for new configuration to take effect.
5451 ConfigureEncoder(video_encoder_config_.Copy());
5452
5453 EXPECT_CALL(encoder_selector, OnCurrentEncoder(_));
5454
5455 video_source_.IncomingCapturedFrame(
5456 CreateFrame(kDontCare, kDontCare, kDontCare));
5457 video_stream_encoder_->Stop();
5458
5459 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5460 // to it's factory, so in order for the encoder instance in the
5461 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5462 // reset the |video_stream_encoder_| here.
5463 video_stream_encoder_.reset();
5464}
5465
5466TEST_F(VideoStreamEncoderTest, EncoderSelectorBitrateSwitch) {
5467 constexpr int kDontCare = 100;
5468
5469 NiceMock<MockEncoderSelector> encoder_selector;
5470 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5471 video_send_config_.encoder_settings.encoder_switch_request_callback =
5472 &switch_callback;
5473 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5474 &fake_encoder_, &encoder_selector);
5475 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5476
5477 // Reset encoder for new configuration to take effect.
5478 ConfigureEncoder(video_encoder_config_.Copy());
5479
Mirta Dvornicic4f34d782020-02-26 13:01:19 +01005480 ON_CALL(encoder_selector, OnAvailableBitrate(_))
philipel9b058032020-02-10 11:30:00 +01005481 .WillByDefault(Return(SdpVideoFormat("AV1")));
5482 EXPECT_CALL(switch_callback,
5483 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(
5484 Field(&SdpVideoFormat::name, "AV1"))));
5485
5486 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005487 /*target_bitrate=*/DataRate::KilobitsPerSec(50),
5488 /*stable_target_bitrate=*/DataRate::KilobitsPerSec(kDontCare),
5489 /*link_allocation=*/DataRate::KilobitsPerSec(kDontCare),
philipel9b058032020-02-10 11:30:00 +01005490 /*fraction_lost=*/0,
5491 /*rtt_ms=*/0,
5492 /*cwnd_reduce_ratio=*/0);
5493
5494 video_stream_encoder_->Stop();
5495}
5496
5497TEST_F(VideoStreamEncoderTest, EncoderSelectorBrokenEncoderSwitch) {
5498 constexpr int kSufficientBitrateToNotDrop = 1000;
5499 constexpr int kDontCare = 100;
5500
5501 NiceMock<MockVideoEncoder> video_encoder;
5502 NiceMock<MockEncoderSelector> encoder_selector;
5503 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
5504 video_send_config_.encoder_settings.encoder_switch_request_callback =
5505 &switch_callback;
5506 auto encoder_factory = std::make_unique<test::VideoEncoderProxyFactory>(
5507 &video_encoder, &encoder_selector);
5508 video_send_config_.encoder_settings.encoder_factory = encoder_factory.get();
5509
5510 // Reset encoder for new configuration to take effect.
5511 ConfigureEncoder(video_encoder_config_.Copy());
5512
5513 // The VideoStreamEncoder needs some bitrate before it can start encoding,
5514 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
5515 // not fail.
5516 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005517 /*target_bitrate=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5518 /*stable_target_bitrate=*/
5519 DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
5520 /*link_allocation=*/DataRate::KilobitsPerSec(kSufficientBitrateToNotDrop),
philipel9b058032020-02-10 11:30:00 +01005521 /*fraction_lost=*/0,
5522 /*rtt_ms=*/0,
5523 /*cwnd_reduce_ratio=*/0);
5524
5525 ON_CALL(video_encoder, Encode(_, _))
5526 .WillByDefault(Return(WEBRTC_VIDEO_CODEC_ENCODER_FAILURE));
5527 ON_CALL(encoder_selector, OnEncoderBroken())
5528 .WillByDefault(Return(SdpVideoFormat("AV2")));
5529
5530 rtc::Event encode_attempted;
5531 EXPECT_CALL(switch_callback,
5532 RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(_)))
5533 .WillOnce([&encode_attempted](const SdpVideoFormat& format) {
5534 EXPECT_EQ(format.name, "AV2");
5535 encode_attempted.Set();
5536 });
5537
5538 video_source_.IncomingCapturedFrame(CreateFrame(1, kDontCare, kDontCare));
5539 encode_attempted.Wait(3000);
5540
5541 video_stream_encoder_->Stop();
5542
5543 // The encoders produces by the VideoEncoderProxyFactory have a pointer back
5544 // to it's factory, so in order for the encoder instance in the
5545 // |video_stream_encoder_| to be destroyed before the |encoder_factory| we
5546 // reset the |video_stream_encoder_| here.
5547 video_stream_encoder_.reset();
5548}
5549
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005550TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005551 AllocationPropagatedToEncoderWhenTargetRateChanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005552 const int kFrameWidth = 320;
5553 const int kFrameHeight = 180;
5554
5555 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005556 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005557 video_stream_encoder_->OnBitrateUpdated(
5558 /*target_bitrate=*/rate,
5559 /*stable_target_bitrate=*/rate,
5560 /*link_allocation=*/rate,
5561 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005562 /*rtt_ms=*/0,
5563 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005564
5565 // Insert a first video frame so that encoder gets configured.
5566 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5567 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5568 frame.set_rotation(kVideoRotation_270);
5569 video_source_.IncomingCapturedFrame(frame);
5570 WaitForEncodedFrame(timestamp_ms);
5571 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5572
5573 // Change of target bitrate propagates to the encoder.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005574 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005575 video_stream_encoder_->OnBitrateUpdated(
5576 /*target_bitrate=*/new_stable_rate,
5577 /*stable_target_bitrate=*/new_stable_rate,
5578 /*link_allocation=*/rate,
5579 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005580 /*rtt_ms=*/0,
5581 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005582 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5583 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5584 video_stream_encoder_->Stop();
5585}
5586
5587TEST_F(VideoStreamEncoderTest,
Rasmus Brandt5cad55b2019-12-19 09:47:11 +01005588 AllocationNotPropagatedToEncoderWhenTargetRateUnchanged) {
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005589 const int kFrameWidth = 320;
5590 const int kFrameHeight = 180;
5591
5592 // Set initial rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005593 auto rate = DataRate::KilobitsPerSec(100);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005594 video_stream_encoder_->OnBitrateUpdated(
5595 /*target_bitrate=*/rate,
5596 /*stable_target_bitrate=*/rate,
5597 /*link_allocation=*/rate,
5598 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005599 /*rtt_ms=*/0,
5600 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005601
5602 // Insert a first video frame so that encoder gets configured.
5603 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5604 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5605 frame.set_rotation(kVideoRotation_270);
5606 video_source_.IncomingCapturedFrame(frame);
5607 WaitForEncodedFrame(timestamp_ms);
5608 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5609
5610 // Set a higher target rate without changing the link_allocation. Should not
5611 // reset encoder's rate.
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005612 auto new_stable_rate = rate - DataRate::KilobitsPerSec(5);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005613 video_stream_encoder_->OnBitrateUpdated(
5614 /*target_bitrate=*/rate,
5615 /*stable_target_bitrate=*/new_stable_rate,
5616 /*link_allocation=*/rate,
5617 /*fraction_lost=*/0,
Ying Wang9b881ab2020-02-07 14:29:32 +01005618 /*rtt_ms=*/0,
5619 /*cwnd_reduce_ratio=*/0);
Evan Shrubsole7c079f62019-09-26 09:55:03 +02005620 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5621 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5622 video_stream_encoder_->Stop();
5623}
5624
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005625TEST_F(VideoStreamEncoderTest, AutomaticAnimationDetection) {
5626 test::ScopedFieldTrials field_trials(
5627 "WebRTC-AutomaticAnimationDetectionScreenshare/"
5628 "enabled:true,min_fps:20,min_duration_ms:1000,min_area_ratio:0.8/");
5629 const int kFramerateFps = 30;
5630 const int kWidth = 1920;
5631 const int kHeight = 1080;
5632 const int kNumFrames = 2 * kFramerateFps; // >1 seconds of frames.
5633 // Works on screenshare mode.
5634 ResetEncoder("VP8", 1, 1, 1, /*screenshare*/ true);
5635 // We rely on the automatic resolution adaptation, but we handle framerate
5636 // adaptation manually by mocking the stats proxy.
5637 video_source_.set_adaptation_enabled(true);
5638
5639 // BALANCED degradation preference is required for this feature.
5640 video_stream_encoder_->OnBitrateUpdated(
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01005641 DataRate::BitsPerSec(kTargetBitrateBps),
5642 DataRate::BitsPerSec(kTargetBitrateBps),
5643 DataRate::BitsPerSec(kTargetBitrateBps), 0, 0, 0);
Ilya Nikolaevskiy648b9d72019-12-03 16:54:17 +01005644 video_stream_encoder_->SetSource(&video_source_,
5645 webrtc::DegradationPreference::BALANCED);
5646 VerifyNoLimitation(video_source_.sink_wants());
5647
5648 VideoFrame frame = CreateFrame(1, kWidth, kHeight);
5649 frame.set_update_rect(VideoFrame::UpdateRect{0, 0, kWidth, kHeight});
5650
5651 // Pass enough frames with the full update to trigger animation detection.
5652 for (int i = 0; i < kNumFrames; ++i) {
5653 int64_t timestamp_ms =
5654 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5655 frame.set_ntp_time_ms(timestamp_ms);
5656 frame.set_timestamp_us(timestamp_ms * 1000);
5657 video_source_.IncomingCapturedFrame(frame);
5658 WaitForEncodedFrame(timestamp_ms);
5659 }
5660
5661 // Resolution should be limited.
5662 rtc::VideoSinkWants expected;
5663 expected.max_framerate_fps = kFramerateFps;
5664 expected.max_pixel_count = 1280 * 720 + 1;
5665 VerifyFpsEqResolutionLt(video_source_.sink_wants(), expected);
5666
5667 // Pass one frame with no known update.
5668 // Resolution cap should be removed immediately.
5669 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5670 frame.set_ntp_time_ms(timestamp_ms);
5671 frame.set_timestamp_us(timestamp_ms * 1000);
5672 frame.clear_update_rect();
5673
5674 video_source_.IncomingCapturedFrame(frame);
5675 WaitForEncodedFrame(timestamp_ms);
5676
5677 // Resolution should be unlimited now.
5678 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kFramerateFps);
5679
5680 video_stream_encoder_->Stop();
5681}
5682
perkj26091b12016-09-01 01:17:40 -07005683} // namespace webrtc