blob: 99a4b2b4df4ff2f522b46521cb4a01759cf74dd9 [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"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080021#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/i420_buffer.h"
Erik Språngf93eda12019-01-16 17:10:57 +010023#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020024#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020025#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010026#include "api/video_codecs/vp8_temporal_layers_factory.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020027#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070028#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020030#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010032#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020037#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "system_wrappers/include/sleep.h"
39#include "test/encoder_settings.h"
40#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020041#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/frame_generator.h"
43#include "test/gmock.h"
44#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020045#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070047
48namespace webrtc {
49
sprangb1ca0732017-02-01 08:38:12 -080050using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080051using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020052using ::testing::AllOf;
53using ::testing::Field;
54using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080055
perkj803d97f2016-11-01 11:45:46 -070056namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020057const int kMinPixelsPerFrame = 320 * 180;
58const int kMinFramerateFps = 2;
59const int kMinBalancedFramerateFps = 7;
60const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080061const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010062const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020063const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010064const uint32_t kSimulcastTargetBitrateBps = 3150000;
65const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080066const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070067const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020068const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020069const int64_t kProcessIntervalMs = 1000;
asapersson5f7226f2016-11-25 04:37:00 -080070
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020071uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
72 0x00, 0x00, 0x03, 0x03, 0xF4,
73 0x05, 0x03, 0xC7, 0xE0, 0x1B,
74 0x41, 0x10, 0x8D, 0x00};
75
perkj803d97f2016-11-01 11:45:46 -070076class TestBuffer : public webrtc::I420Buffer {
77 public:
78 TestBuffer(rtc::Event* event, int width, int height)
79 : I420Buffer(width, height), event_(event) {}
80
81 private:
82 friend class rtc::RefCountedObject<TestBuffer>;
83 ~TestBuffer() override {
84 if (event_)
85 event_->Set();
86 }
87 rtc::Event* const event_;
88};
89
Noah Richards51db4212019-06-12 06:59:12 -070090// A fake native buffer that can't be converted to I420.
91class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
92 public:
93 FakeNativeBuffer(rtc::Event* event, int width, int height)
94 : event_(event), width_(width), height_(height) {}
95 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
96 int width() const override { return width_; }
97 int height() const override { return height_; }
98 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
99 return nullptr;
100 }
101
102 private:
103 friend class rtc::RefCountedObject<FakeNativeBuffer>;
104 ~FakeNativeBuffer() override {
105 if (event_)
106 event_->Set();
107 }
108 rtc::Event* const event_;
109 const int width_;
110 const int height_;
111};
112
Niels Möller7dc26b72017-12-06 10:27:48 +0100113class CpuOveruseDetectorProxy : public OveruseFrameDetector {
114 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200115 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
116 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100117 last_target_framerate_fps_(-1) {}
118 virtual ~CpuOveruseDetectorProxy() {}
119
120 void OnTargetFramerateUpdated(int framerate_fps) override {
121 rtc::CritScope cs(&lock_);
122 last_target_framerate_fps_ = framerate_fps;
123 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
124 }
125
126 int GetLastTargetFramerate() {
127 rtc::CritScope cs(&lock_);
128 return last_target_framerate_fps_;
129 }
130
Niels Möller4db138e2018-04-19 09:04:13 +0200131 CpuOveruseOptions GetOptions() { return options_; }
132
Niels Möller7dc26b72017-12-06 10:27:48 +0100133 private:
134 rtc::CriticalSection lock_;
135 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
136};
137
mflodmancc3d4422017-08-03 08:27:51 -0700138class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700139 public:
Niels Möller213618e2018-07-24 09:29:58 +0200140 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200141 const VideoStreamEncoderSettings& settings,
142 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100143 : VideoStreamEncoder(Clock::GetRealTimeClock(),
144 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200145 stats_proxy,
146 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200147 std::unique_ptr<OveruseFrameDetector>(
148 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100149 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200150 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700151
sprangb1ca0732017-02-01 08:38:12 -0800152 void PostTaskAndWait(bool down, AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200153 PostTaskAndWait(down, reason, /*expected_results=*/true);
154 }
155
156 void PostTaskAndWait(bool down, AdaptReason reason, bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100157 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200158 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200159 if (down)
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200160 EXPECT_EQ(expected_results, AdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200161 else
162 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700163 event.Set();
164 });
perkj070ba852017-02-16 15:46:27 -0800165 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700166 }
167
kthelgason2fc52542017-03-03 00:24:41 -0800168 // This is used as a synchronisation mechanism, to make sure that the
169 // encoder queue is not blocked before we start sending it frames.
170 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100171 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200172 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800173 ASSERT_TRUE(event.Wait(5000));
174 }
175
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200176 void TriggerCpuOveruse() {
177 PostTaskAndWait(/*down=*/true, AdaptReason::kCpu);
178 }
kthelgason876222f2016-11-29 01:44:11 -0800179
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200180 void TriggerCpuNormalUsage() {
181 PostTaskAndWait(/*down=*/false, AdaptReason::kCpu);
182 }
kthelgason876222f2016-11-29 01:44:11 -0800183
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200184 void TriggerQualityLow() {
185 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality);
186 }
kthelgason876222f2016-11-29 01:44:11 -0800187
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200188 void TriggerQualityLowExpectFalse() {
189 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality,
190 /*expected_results=*/false);
191 }
192
193 void TriggerQualityHigh() {
194 PostTaskAndWait(/*down=*/false, AdaptReason::kQuality);
195 }
sprangfda496a2017-06-15 04:21:07 -0700196
Niels Möller7dc26b72017-12-06 10:27:48 +0100197 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700198};
199
asapersson5f7226f2016-11-25 04:37:00 -0800200class VideoStreamFactory
201 : public VideoEncoderConfig::VideoStreamFactoryInterface {
202 public:
sprangfda496a2017-06-15 04:21:07 -0700203 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
204 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800205 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700206 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800207 }
208
209 private:
210 std::vector<VideoStream> CreateEncoderStreams(
211 int width,
212 int height,
213 const VideoEncoderConfig& encoder_config) override {
214 std::vector<VideoStream> streams =
215 test::CreateVideoStreams(width, height, encoder_config);
216 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100217 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700218 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800219 }
220 return streams;
221 }
sprangfda496a2017-06-15 04:21:07 -0700222
asapersson5f7226f2016-11-25 04:37:00 -0800223 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700224 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800225};
226
Noah Richards51db4212019-06-12 06:59:12 -0700227// Simulates simulcast behavior and makes highest stream resolutions divisible
228// by 4.
229class CroppingVideoStreamFactory
230 : public VideoEncoderConfig::VideoStreamFactoryInterface {
231 public:
232 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
233 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
234 EXPECT_GT(num_temporal_layers, 0u);
235 EXPECT_GT(framerate, 0);
236 }
237
238 private:
239 std::vector<VideoStream> CreateEncoderStreams(
240 int width,
241 int height,
242 const VideoEncoderConfig& encoder_config) override {
243 std::vector<VideoStream> streams = test::CreateVideoStreams(
244 width - width % 4, height - height % 4, encoder_config);
245 for (VideoStream& stream : streams) {
246 stream.num_temporal_layers = num_temporal_layers_;
247 stream.max_framerate = framerate_;
248 }
249 return streams;
250 }
251
252 const size_t num_temporal_layers_;
253 const int framerate_;
254};
255
sprangb1ca0732017-02-01 08:38:12 -0800256class AdaptingFrameForwarder : public test::FrameForwarder {
257 public:
258 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700259 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800260
261 void set_adaptation_enabled(bool enabled) {
262 rtc::CritScope cs(&crit_);
263 adaptation_enabled_ = enabled;
264 }
265
asaperssonfab67072017-04-04 05:51:49 -0700266 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800267 rtc::CritScope cs(&crit_);
268 return adaptation_enabled_;
269 }
270
asapersson09f05612017-05-15 23:40:18 -0700271 rtc::VideoSinkWants last_wants() const {
272 rtc::CritScope cs(&crit_);
273 return last_wants_;
274 }
275
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200276 absl::optional<int> last_sent_width() const { return last_width_; }
277 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800278
sprangb1ca0732017-02-01 08:38:12 -0800279 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
280 int cropped_width = 0;
281 int cropped_height = 0;
282 int out_width = 0;
283 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700284 if (adaption_enabled()) {
285 if (adapter_.AdaptFrameResolution(
286 video_frame.width(), video_frame.height(),
287 video_frame.timestamp_us() * 1000, &cropped_width,
288 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100289 VideoFrame adapted_frame =
290 VideoFrame::Builder()
291 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
292 nullptr, out_width, out_height))
293 .set_timestamp_rtp(99)
294 .set_timestamp_ms(99)
295 .set_rotation(kVideoRotation_0)
296 .build();
sprangc5d62e22017-04-02 23:53:04 -0700297 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
298 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800299 last_width_.emplace(adapted_frame.width());
300 last_height_.emplace(adapted_frame.height());
301 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200302 last_width_ = absl::nullopt;
303 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700304 }
sprangb1ca0732017-02-01 08:38:12 -0800305 } else {
306 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800307 last_width_.emplace(video_frame.width());
308 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800309 }
310 }
311
312 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
313 const rtc::VideoSinkWants& wants) override {
314 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700315 last_wants_ = sink_wants();
sprangc5d62e22017-04-02 23:53:04 -0700316 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
317 wants.max_pixel_count,
318 wants.max_framerate_fps);
sprangb1ca0732017-02-01 08:38:12 -0800319 test::FrameForwarder::AddOrUpdateSink(sink, wants);
320 }
sprangb1ca0732017-02-01 08:38:12 -0800321 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700322 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
323 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200324 absl::optional<int> last_width_;
325 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800326};
sprangc5d62e22017-04-02 23:53:04 -0700327
Niels Möller213618e2018-07-24 09:29:58 +0200328// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700329class MockableSendStatisticsProxy : public SendStatisticsProxy {
330 public:
331 MockableSendStatisticsProxy(Clock* clock,
332 const VideoSendStream::Config& config,
333 VideoEncoderConfig::ContentType content_type)
334 : SendStatisticsProxy(clock, config, content_type) {}
335
336 VideoSendStream::Stats GetStats() override {
337 rtc::CritScope cs(&lock_);
338 if (mock_stats_)
339 return *mock_stats_;
340 return SendStatisticsProxy::GetStats();
341 }
342
Niels Möller213618e2018-07-24 09:29:58 +0200343 int GetInputFrameRate() const override {
344 rtc::CritScope cs(&lock_);
345 if (mock_stats_)
346 return mock_stats_->input_frame_rate;
347 return SendStatisticsProxy::GetInputFrameRate();
348 }
sprangc5d62e22017-04-02 23:53:04 -0700349 void SetMockStats(const VideoSendStream::Stats& stats) {
350 rtc::CritScope cs(&lock_);
351 mock_stats_.emplace(stats);
352 }
353
354 void ResetMockStats() {
355 rtc::CritScope cs(&lock_);
356 mock_stats_.reset();
357 }
358
359 private:
360 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200361 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700362};
363
sprang4847ae62017-06-27 07:06:52 -0700364class MockBitrateObserver : public VideoBitrateAllocationObserver {
365 public:
Erik Språng566124a2018-04-23 12:32:22 +0200366 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700367};
368
perkj803d97f2016-11-01 11:45:46 -0700369} // namespace
370
mflodmancc3d4422017-08-03 08:27:51 -0700371class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700372 public:
373 static const int kDefaultTimeoutMs = 30 * 1000;
374
mflodmancc3d4422017-08-03 08:27:51 -0700375 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700376 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700377 codec_width_(320),
378 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200379 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200380 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700381 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200382 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700383 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700384 Clock::GetRealTimeClock(),
385 video_send_config_,
386 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700387 sink_(&fake_encoder_) {}
388
389 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700390 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700391 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200392 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800393 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200394 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200395 video_send_config_.rtp.payload_name = "FAKE";
396 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700397
Per512ecb32016-09-23 15:52:06 +0200398 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200399 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700400 video_encoder_config.video_stream_factory =
401 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100402 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700403
404 // Framerate limit is specified by the VideoStreamFactory.
405 std::vector<VideoStream> streams =
406 video_encoder_config.video_stream_factory->CreateEncoderStreams(
407 codec_width_, codec_height_, video_encoder_config);
408 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200409 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700410
Niels Möllerf1338562018-04-26 09:51:47 +0200411 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800412 }
413
Niels Möllerf1338562018-04-26 09:51:47 +0200414 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700415 if (video_stream_encoder_)
416 video_stream_encoder_->Stop();
417 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200418 stats_proxy_.get(), video_send_config_.encoder_settings,
419 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700420 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
421 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700422 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700423 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
424 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200425 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700426 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800427 }
428
429 void ResetEncoder(const std::string& payload_name,
430 size_t num_streams,
431 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700432 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700433 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200434 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800435
436 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200437 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800438 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100439 video_encoder_config.max_bitrate_bps =
440 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800441 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700442 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
443 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700444 video_encoder_config.content_type =
445 screenshare ? VideoEncoderConfig::ContentType::kScreen
446 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700447 if (payload_name == "VP9") {
448 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
449 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
450 video_encoder_config.encoder_specific_settings =
451 new rtc::RefCountedObject<
452 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
453 }
Niels Möllerf1338562018-04-26 09:51:47 +0200454 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700455 }
456
sprang57c2fff2017-01-16 06:24:02 -0800457 VideoFrame CreateFrame(int64_t ntp_time_ms,
458 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100459 VideoFrame frame =
460 VideoFrame::Builder()
461 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
462 destruction_event, codec_width_, codec_height_))
463 .set_timestamp_rtp(99)
464 .set_timestamp_ms(99)
465 .set_rotation(kVideoRotation_0)
466 .build();
sprang57c2fff2017-01-16 06:24:02 -0800467 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700468 return frame;
469 }
470
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100471 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
472 rtc::Event* destruction_event,
473 int offset_x) const {
474 VideoFrame frame =
475 VideoFrame::Builder()
476 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
477 destruction_event, codec_width_, codec_height_))
478 .set_timestamp_rtp(99)
479 .set_timestamp_ms(99)
480 .set_rotation(kVideoRotation_0)
481 .set_update_rect({offset_x, 0, 1, 1})
482 .build();
483 frame.set_ntp_time_ms(ntp_time_ms);
484 return frame;
485 }
486
sprang57c2fff2017-01-16 06:24:02 -0800487 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100488 VideoFrame frame =
489 VideoFrame::Builder()
490 .set_video_frame_buffer(
491 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
492 .set_timestamp_rtp(99)
493 .set_timestamp_ms(99)
494 .set_rotation(kVideoRotation_0)
495 .build();
sprang57c2fff2017-01-16 06:24:02 -0800496 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700497 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700498 return frame;
499 }
500
Noah Richards51db4212019-06-12 06:59:12 -0700501 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
502 rtc::Event* destruction_event,
503 int width,
504 int height) const {
505 VideoFrame frame =
506 VideoFrame::Builder()
507 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
508 destruction_event, width, height))
509 .set_timestamp_rtp(99)
510 .set_timestamp_ms(99)
511 .set_rotation(kVideoRotation_0)
512 .build();
513 frame.set_ntp_time_ms(ntp_time_ms);
514 return frame;
515 }
516
517 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
518 rtc::Event* destruction_event) const {
519 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
520 codec_height_);
521 }
522
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100523 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
524 MockBitrateObserver bitrate_observer;
525 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
526
527 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
528 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200529 video_stream_encoder_->OnBitrateUpdated(
530 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
531 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100532
533 video_source_.IncomingCapturedFrame(
534 CreateFrame(1, codec_width_, codec_height_));
535 WaitForEncodedFrame(1);
536 }
537
asapersson02465b82017-04-10 01:12:52 -0700538 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700539 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700540 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
541 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700542 }
543
asapersson09f05612017-05-15 23:40:18 -0700544 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
545 const rtc::VideoSinkWants& wants2) {
546 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
547 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
548 }
549
Åsa Persson8c1bf952018-09-13 10:42:19 +0200550 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
551 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
552 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
553 EXPECT_FALSE(wants.target_pixel_count);
554 }
555
asapersson09f05612017-05-15 23:40:18 -0700556 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
557 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200558 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700559 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
560 EXPECT_GT(wants1.max_pixel_count, 0);
561 }
562
563 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
564 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200565 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700566 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
567 }
568
asaperssonf7e294d2017-06-13 23:25:22 -0700569 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
570 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200571 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700572 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
573 }
574
575 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
576 const rtc::VideoSinkWants& wants2) {
577 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
578 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
579 }
580
581 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
582 const rtc::VideoSinkWants& wants2) {
583 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
584 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
585 }
586
587 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
588 const rtc::VideoSinkWants& wants2) {
589 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
590 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
591 EXPECT_GT(wants1.max_pixel_count, 0);
592 }
593
594 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
595 const rtc::VideoSinkWants& wants2) {
596 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
597 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
598 }
599
asapersson09f05612017-05-15 23:40:18 -0700600 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
601 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200602 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700603 EXPECT_LT(wants.max_pixel_count, pixel_count);
604 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700605 }
606
607 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
608 EXPECT_LT(wants.max_framerate_fps, fps);
609 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
610 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700611 }
612
asaperssonf7e294d2017-06-13 23:25:22 -0700613 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
614 int expected_fps) {
615 EXPECT_EQ(expected_fps, 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
Jonathan Yubc771b72017-12-08 17:04:29 -0800620 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
621 int last_frame_pixels) {
622 // Balanced mode should always scale FPS to the desired range before
623 // attempting to scale resolution.
624 int fps_limit = wants.max_framerate_fps;
625 if (last_frame_pixels <= 320 * 240) {
626 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
627 } else if (last_frame_pixels <= 480 * 270) {
628 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
629 } else if (last_frame_pixels <= 640 * 480) {
630 EXPECT_LE(15, fps_limit);
631 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200632 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800633 }
634 }
635
sprang4847ae62017-06-27 07:06:52 -0700636 void WaitForEncodedFrame(int64_t expected_ntp_time) {
637 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200638 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700639 }
640
641 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
642 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200643 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700644 return ok;
645 }
646
647 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
648 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200649 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700650 }
651
652 void ExpectDroppedFrame() {
653 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200654 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700655 }
656
657 bool WaitForFrame(int64_t timeout_ms) {
658 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200659 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700660 return ok;
661 }
662
perkj26091b12016-09-01 01:17:40 -0700663 class TestEncoder : public test::FakeEncoder {
664 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100665 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700666
asaperssonfab67072017-04-04 05:51:49 -0700667 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800668 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700669 return config_;
670 }
671
672 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800673 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700674 block_next_encode_ = true;
675 }
676
Erik Språngaed30702018-11-05 12:57:17 +0100677 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800678 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100679 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100680 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100681 if (quality_scaling_) {
682 info.scaling_settings =
683 VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame);
684 }
685 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100686 for (int i = 0; i < kMaxSpatialLayers; ++i) {
687 if (temporal_layers_supported_[i]) {
688 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
689 info.fps_allocation[i].resize(num_layers);
690 }
691 }
Erik Språngaed30702018-11-05 12:57:17 +0100692 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200693
694 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Erik Språngaed30702018-11-05 12:57:17 +0100695 return info;
kthelgason876222f2016-11-29 01:44:11 -0800696 }
697
Erik Språngb7cb7b52019-02-26 15:52:33 +0100698 int32_t RegisterEncodeCompleteCallback(
699 EncodedImageCallback* callback) override {
700 rtc::CritScope lock(&local_crit_sect_);
701 encoded_image_callback_ = callback;
702 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
703 }
704
perkjfa10b552016-10-02 23:45:26 -0700705 void ContinueEncode() { continue_encode_event_.Set(); }
706
707 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
708 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800709 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700710 EXPECT_EQ(timestamp_, timestamp);
711 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
712 }
713
kthelgason2fc52542017-03-03 00:24:41 -0800714 void SetQualityScaling(bool b) {
715 rtc::CritScope lock(&local_crit_sect_);
716 quality_scaling_ = b;
717 }
kthelgasonad9010c2017-02-14 00:46:51 -0800718
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100719 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
720 rtc::CritScope lock(&local_crit_sect_);
721 is_hardware_accelerated_ = is_hardware_accelerated;
722 }
723
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100724 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
725 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
726 rtc::CritScope lock(&local_crit_sect_);
727 temporal_layers_supported_[spatial_idx] = supported;
728 }
729
Sergey Silkin6456e352019-07-08 17:56:40 +0200730 void SetResolutionBitrateLimits(
731 std::vector<ResolutionBitrateLimits> thresholds) {
732 rtc::CritScope cs(&local_crit_sect_);
733 resolution_bitrate_limits_ = thresholds;
734 }
735
sprangfe627f32017-03-29 08:24:59 -0700736 void ForceInitEncodeFailure(bool force_failure) {
737 rtc::CritScope lock(&local_crit_sect_);
738 force_init_encode_failed_ = force_failure;
739 }
740
Niels Möller6bb5ab92019-01-11 11:11:10 +0100741 void SimulateOvershoot(double rate_factor) {
742 rtc::CritScope lock(&local_crit_sect_);
743 rate_factor_ = rate_factor;
744 }
745
Erik Språngd7329ca2019-02-21 21:19:53 +0100746 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100747 rtc::CritScope lock(&local_crit_sect_);
748 return last_framerate_;
749 }
750
Erik Språngd7329ca2019-02-21 21:19:53 +0100751 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100752 rtc::CritScope lock(&local_crit_sect_);
753 return last_update_rect_;
754 }
755
Niels Möller87e2d782019-03-07 10:18:23 +0100756 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100757 rtc::CritScope lock(&local_crit_sect_);
758 return last_frame_types_;
759 }
760
761 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100762 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100763 keyframe ? VideoFrameType::kVideoFrameKey
764 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100765 {
766 rtc::CritScope lock(&local_crit_sect_);
767 last_frame_types_ = frame_type;
768 }
Niels Möllerb859b322019-03-07 12:40:01 +0100769 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100770 }
771
Erik Språngb7cb7b52019-02-26 15:52:33 +0100772 void InjectEncodedImage(const EncodedImage& image) {
773 rtc::CritScope lock(&local_crit_sect_);
774 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
775 }
776
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200777 void InjectEncodedImage(const EncodedImage& image,
778 const CodecSpecificInfo* codec_specific_info,
779 const RTPFragmentationHeader* fragmentation) {
780 rtc::CritScope lock(&local_crit_sect_);
781 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
782 fragmentation);
783 }
784
Erik Språngd7329ca2019-02-21 21:19:53 +0100785 void ExpectNullFrame() {
786 rtc::CritScope lock(&local_crit_sect_);
787 expect_null_frame_ = true;
788 }
789
Erik Språng5056af02019-09-02 15:53:11 +0200790 absl::optional<VideoEncoder::RateControlParameters>
791 GetAndResetLastRateControlSettings() {
792 auto settings = last_rate_control_settings_;
793 last_rate_control_settings_.reset();
794 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100795 }
796
Sergey Silkin5ee69672019-07-02 14:18:34 +0200797 int GetNumEncoderInitializations() const {
798 rtc::CritScope lock(&local_crit_sect_);
799 return num_encoder_initializations_;
800 }
801
perkjfa10b552016-10-02 23:45:26 -0700802 private:
perkj26091b12016-09-01 01:17:40 -0700803 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100804 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700805 bool block_encode;
806 {
brandtre78d2662017-01-16 05:57:16 -0800807 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100808 if (expect_null_frame_) {
809 EXPECT_EQ(input_image.timestamp(), 0u);
810 EXPECT_EQ(input_image.width(), 1);
811 last_frame_types_ = *frame_types;
812 expect_null_frame_ = false;
813 } else {
814 EXPECT_GT(input_image.timestamp(), timestamp_);
815 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
816 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
817 }
perkj26091b12016-09-01 01:17:40 -0700818
819 timestamp_ = input_image.timestamp();
820 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700821 last_input_width_ = input_image.width();
822 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700823 block_encode = block_next_encode_;
824 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100825 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100826 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700827 }
Niels Möllerb859b322019-03-07 12:40:01 +0100828 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700829 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700830 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700831 return result;
832 }
833
sprangfe627f32017-03-29 08:24:59 -0700834 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200835 const Settings& settings) override {
836 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200837
sprangfe627f32017-03-29 08:24:59 -0700838 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100839 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200840
841 ++num_encoder_initializations_;
842
Erik Språng82fad3d2018-03-21 09:57:23 +0100843 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700844 // Simulate setting up temporal layers, in order to validate the life
845 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100846 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200847 frame_buffer_controller_ =
848 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700849 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100850 if (force_init_encode_failed_) {
851 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700852 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100853 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100854
Erik Språngb7cb7b52019-02-26 15:52:33 +0100855 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700856 return res;
857 }
858
Erik Språngb7cb7b52019-02-26 15:52:33 +0100859 int32_t Release() override {
860 rtc::CritScope lock(&local_crit_sect_);
861 EXPECT_NE(initialized_, EncoderState::kUninitialized);
862 initialized_ = EncoderState::kUninitialized;
863 return FakeEncoder::Release();
864 }
865
Erik Språng16cb8f52019-04-12 13:59:09 +0200866 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100867 rtc::CritScope lock(&local_crit_sect_);
868 VideoBitrateAllocation adjusted_rate_allocation;
869 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
870 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200871 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100872 adjusted_rate_allocation.SetBitrate(
873 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200874 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100875 rate_factor_));
876 }
877 }
878 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200879 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200880 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200881 RateControlParameters adjusted_paramters = parameters;
882 adjusted_paramters.bitrate = adjusted_rate_allocation;
883 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100884 }
885
brandtre78d2662017-01-16 05:57:16 -0800886 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100887 enum class EncoderState {
888 kUninitialized,
889 kInitializationFailed,
890 kInitialized
891 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
892 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700893 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700894 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700895 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
896 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
897 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
898 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
899 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100900 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100901 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700902 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100903 absl::optional<bool>
904 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
905 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700906 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100907 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
908 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200909 absl::optional<VideoEncoder::RateControlParameters>
910 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100911 VideoFrame::UpdateRect last_update_rect_
912 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100913 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100914 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100915 EncodedImageCallback* encoded_image_callback_
916 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200917 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200918 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200919 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
920 RTC_GUARDED_BY(local_crit_sect_);
perkj26091b12016-09-01 01:17:40 -0700921 };
922
mflodmancc3d4422017-08-03 08:27:51 -0700923 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700924 public:
925 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100926 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700927
perkj26091b12016-09-01 01:17:40 -0700928 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700929 EXPECT_TRUE(
930 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
931 }
932
933 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
934 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700935 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700936 if (!encoded_frame_event_.Wait(timeout_ms))
937 return false;
perkj26091b12016-09-01 01:17:40 -0700938 {
939 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800940 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700941 }
942 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700943 return true;
perkj26091b12016-09-01 01:17:40 -0700944 }
945
sprangb1ca0732017-02-01 08:38:12 -0800946 void WaitForEncodedFrame(uint32_t expected_width,
947 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700948 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100949 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700950 }
951
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100952 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700953 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800954 uint32_t width = 0;
955 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800956 {
957 rtc::CritScope lock(&crit_);
958 width = last_width_;
959 height = last_height_;
960 }
961 EXPECT_EQ(expected_height, height);
962 EXPECT_EQ(expected_width, width);
963 }
964
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +0200965 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
966 VideoRotation rotation;
967 {
968 rtc::CritScope lock(&crit_);
969 rotation = last_rotation_;
970 }
971 EXPECT_EQ(expected_rotation, rotation);
972 }
973
kthelgason2fc52542017-03-03 00:24:41 -0800974 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -0800975
sprangc5d62e22017-04-02 23:53:04 -0700976 bool WaitForFrame(int64_t timeout_ms) {
977 return encoded_frame_event_.Wait(timeout_ms);
978 }
979
perkj26091b12016-09-01 01:17:40 -0700980 void SetExpectNoFrames() {
981 rtc::CritScope lock(&crit_);
982 expect_frames_ = false;
983 }
984
asaperssonfab67072017-04-04 05:51:49 -0700985 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +0200986 rtc::CritScope lock(&crit_);
987 return number_of_reconfigurations_;
988 }
989
asaperssonfab67072017-04-04 05:51:49 -0700990 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +0200991 rtc::CritScope lock(&crit_);
992 return min_transmit_bitrate_bps_;
993 }
994
Erik Språngd7329ca2019-02-21 21:19:53 +0100995 void SetNumExpectedLayers(size_t num_layers) {
996 rtc::CritScope lock(&crit_);
997 num_expected_layers_ = num_layers;
998 }
999
Erik Språngb7cb7b52019-02-26 15:52:33 +01001000 int64_t GetLastCaptureTimeMs() const {
1001 rtc::CritScope lock(&crit_);
1002 return last_capture_time_ms_;
1003 }
1004
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001005 std::vector<uint8_t> GetLastEncodedImageData() {
1006 rtc::CritScope lock(&crit_);
1007 return std::move(last_encoded_image_data_);
1008 }
1009
1010 RTPFragmentationHeader GetLastFragmentation() {
1011 rtc::CritScope lock(&crit_);
1012 return std::move(last_fragmentation_);
1013 }
1014
perkj26091b12016-09-01 01:17:40 -07001015 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001016 Result OnEncodedImage(
1017 const EncodedImage& encoded_image,
1018 const CodecSpecificInfo* codec_specific_info,
1019 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001020 rtc::CritScope lock(&crit_);
1021 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001022 last_encoded_image_data_ = std::vector<uint8_t>(
1023 encoded_image.data(), encoded_image.data() + encoded_image.size());
1024 if (fragmentation) {
1025 last_fragmentation_.CopyFrom(*fragmentation);
1026 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001027 uint32_t timestamp = encoded_image.Timestamp();
1028 if (last_timestamp_ != timestamp) {
1029 num_received_layers_ = 1;
1030 } else {
1031 ++num_received_layers_;
1032 }
1033 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001034 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001035 last_width_ = encoded_image._encodedWidth;
1036 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001037 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001038 if (num_received_layers_ == num_expected_layers_) {
1039 encoded_frame_event_.Set();
1040 }
sprangb1ca0732017-02-01 08:38:12 -08001041 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001042 }
1043
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001044 void OnEncoderConfigurationChanged(
1045 std::vector<VideoStream> streams,
1046 VideoEncoderConfig::ContentType content_type,
1047 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001048 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001049 ++number_of_reconfigurations_;
1050 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1051 }
1052
perkj26091b12016-09-01 01:17:40 -07001053 rtc::CriticalSection crit_;
1054 TestEncoder* test_encoder_;
1055 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001056 std::vector<uint8_t> last_encoded_image_data_;
1057 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001058 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001059 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001060 uint32_t last_height_ = 0;
1061 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001062 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001063 size_t num_expected_layers_ = 1;
1064 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001065 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001066 int number_of_reconfigurations_ = 0;
1067 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001068 };
1069
Sergey Silkin5ee69672019-07-02 14:18:34 +02001070 class VideoBitrateAllocatorProxyFactory
1071 : public VideoBitrateAllocatorFactory {
1072 public:
1073 VideoBitrateAllocatorProxyFactory()
1074 : bitrate_allocator_factory_(
1075 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1076
1077 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1078 const VideoCodec& codec) override {
1079 rtc::CritScope lock(&crit_);
1080 codec_config_ = codec;
1081 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1082 }
1083
1084 VideoCodec codec_config() const {
1085 rtc::CritScope lock(&crit_);
1086 return codec_config_;
1087 }
1088
1089 private:
1090 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1091
1092 rtc::CriticalSection crit_;
1093 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1094 };
1095
perkj26091b12016-09-01 01:17:40 -07001096 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001097 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001098 int codec_width_;
1099 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001100 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001101 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001102 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001103 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001104 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001105 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001106 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001107 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001108 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001109 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001110};
1111
mflodmancc3d4422017-08-03 08:27:51 -07001112TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001113 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001114 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1115 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001116 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001117 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001118 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001119 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001120 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001121}
1122
mflodmancc3d4422017-08-03 08:27:51 -07001123TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001124 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001125 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001126 // The encoder will cache up to one frame for a short duration. Adding two
1127 // frames means that the first frame will be dropped and the second frame will
1128 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001129 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001130 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001131 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001132
Erik Språng4c6ca302019-04-08 15:14:01 +02001133 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001134 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1135 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001136
Sebastian Janssona3177052018-04-10 13:05:49 +02001137 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001138 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001139 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1140
1141 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001142 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001143}
1144
mflodmancc3d4422017-08-03 08:27:51 -07001145TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001146 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001147 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1148 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001149 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001150 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001151
Florent Castellia8336d32019-09-09 13:36:55 +02001152 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1153 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001154 // The encoder will cache up to one frame for a short duration. Adding two
1155 // frames means that the first frame will be dropped and the second frame will
1156 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001157 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001158 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001159
Erik Språng4c6ca302019-04-08 15:14:01 +02001160 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001161 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1162 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001163 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001164 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1165 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001166 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001167}
1168
mflodmancc3d4422017-08-03 08:27:51 -07001169TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001170 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001171 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1172 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001173 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001174 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001175
1176 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001177 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001178
perkja49cbd32016-09-16 07:53:41 -07001179 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001180 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001181 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001182}
1183
mflodmancc3d4422017-08-03 08:27:51 -07001184TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001185 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001186 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1187 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001188
perkja49cbd32016-09-16 07:53:41 -07001189 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001190 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001191
mflodmancc3d4422017-08-03 08:27:51 -07001192 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001193 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001194 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001195 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1196 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001197}
1198
mflodmancc3d4422017-08-03 08:27:51 -07001199TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001200 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001201 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1202 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001203
1204 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001205 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001206 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001207 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1208 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001209 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1210 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001211 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001212 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001213
mflodmancc3d4422017-08-03 08:27:51 -07001214 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001215}
1216
Noah Richards51db4212019-06-12 06:59:12 -07001217TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1218 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001219 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1220 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001221
1222 rtc::Event frame_destroyed_event;
1223 video_source_.IncomingCapturedFrame(
1224 CreateFakeNativeFrame(1, &frame_destroyed_event));
1225 ExpectDroppedFrame();
1226 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1227 video_stream_encoder_->Stop();
1228}
1229
1230TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1231 // Use the cropping factory.
1232 video_encoder_config_.video_stream_factory =
1233 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1234 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1235 kMaxPayloadLength);
1236 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1237
1238 // Capture a frame at codec_width_/codec_height_.
1239 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001240 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1241 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001242 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1243 WaitForEncodedFrame(1);
1244 // The encoder will have been configured once.
1245 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1246 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1247 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1248
1249 // Now send in a fake frame that needs to be cropped as the width/height
1250 // aren't divisible by 4 (see CreateEncoderStreams above).
1251 rtc::Event frame_destroyed_event;
1252 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1253 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1254 ExpectDroppedFrame();
1255 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1256 video_stream_encoder_->Stop();
1257}
1258
mflodmancc3d4422017-08-03 08:27:51 -07001259TEST_F(VideoStreamEncoderTest,
1260 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001261 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001262 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1263 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001264 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001265
1266 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001267 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001268 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001269 // The encoder will have been configured once when the first frame is
1270 // received.
1271 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001272
1273 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001274 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001275 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001276 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001277 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001278
1279 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001280 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001281 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001282 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001283 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001284
mflodmancc3d4422017-08-03 08:27:51 -07001285 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001286}
1287
mflodmancc3d4422017-08-03 08:27:51 -07001288TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001289 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001290 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1291 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001292
1293 // Capture a frame and wait for it to synchronize with the encoder thread.
1294 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001295 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001296 // The encoder will have been configured once.
1297 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001298 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1299 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1300
1301 codec_width_ *= 2;
1302 codec_height_ *= 2;
1303 // Capture a frame with a higher resolution and wait for it to synchronize
1304 // with the encoder thread.
1305 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001306 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001307 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1308 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001309 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001310
mflodmancc3d4422017-08-03 08:27:51 -07001311 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001312}
1313
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001314TEST_F(VideoStreamEncoderTest,
1315 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1316 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001317 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1318 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001319
1320 // Capture a frame and wait for it to synchronize with the encoder thread.
1321 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1322 WaitForEncodedFrame(1);
1323
1324 VideoEncoderConfig video_encoder_config;
1325 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1326 // Changing the max payload data length recreates encoder.
1327 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1328 kMaxPayloadLength / 2);
1329
1330 // Capture a frame and wait for it to synchronize with the encoder thread.
1331 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1332 WaitForEncodedFrame(2);
1333 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1334
1335 video_stream_encoder_->Stop();
1336}
1337
Sergey Silkin5ee69672019-07-02 14:18:34 +02001338TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1339 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001340 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1341 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001342
1343 VideoEncoderConfig video_encoder_config;
1344 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1345 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1346 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1347 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1348 kMaxPayloadLength);
1349
1350 // Capture a frame and wait for it to synchronize with the encoder thread.
1351 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1352 WaitForEncodedFrame(1);
1353 // The encoder will have been configured once when the first frame is
1354 // received.
1355 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1356 EXPECT_EQ(kTargetBitrateBps,
1357 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1358 EXPECT_EQ(kStartBitrateBps,
1359 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1360
Sergey Silkin6456e352019-07-08 17:56:40 +02001361 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1362 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001363 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1364 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1365 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1366 kMaxPayloadLength);
1367
1368 // Capture a frame and wait for it to synchronize with the encoder thread.
1369 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1370 WaitForEncodedFrame(2);
1371 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1372 // Bitrate limits have changed - rate allocator should be reconfigured,
1373 // encoder should not be reconfigured.
1374 EXPECT_EQ(kTargetBitrateBps * 2,
1375 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1376 EXPECT_EQ(kStartBitrateBps * 2,
1377 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1378 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1379
1380 video_stream_encoder_->Stop();
1381}
1382
Sergey Silkin6456e352019-07-08 17:56:40 +02001383TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001384 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001385 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001386 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1387 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001388
Sergey Silkin6456e352019-07-08 17:56:40 +02001389 VideoEncoderConfig video_encoder_config;
1390 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1391 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001392 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001393 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1394 kMaxPayloadLength);
1395
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001396 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001397 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001398
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001399 // Get the default bitrate limits and use them as baseline for custom
1400 // application and encoder recommended limits.
1401 const uint32_t kDefaultMinBitrateKbps =
1402 bitrate_allocator_factory_.codec_config().minBitrate;
1403 const uint32_t kDefaultMaxBitrateKbps =
1404 bitrate_allocator_factory_.codec_config().maxBitrate;
1405 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1406 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1407 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1408 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1409
1410 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1411 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1412 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1413 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1414
1415 // Change resolution. This will trigger encoder re-configuration and video
1416 // stream encoder will pick up the bitrate limits recommended by encoder.
1417 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1418 WaitForEncodedFrame(2);
1419 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1420 WaitForEncodedFrame(3);
1421
1422 // App bitrate limits are not set - bitrate limits recommended by encoder
1423 // should be used.
1424 EXPECT_EQ(kEncMaxBitrateKbps,
1425 bitrate_allocator_factory_.codec_config().maxBitrate);
1426 EXPECT_EQ(kEncMinBitrateKbps,
1427 bitrate_allocator_factory_.codec_config().minBitrate);
1428
1429 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1430 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1431 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1432 kMaxPayloadLength);
1433 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1434 WaitForEncodedFrame(4);
1435
1436 // App limited the max bitrate - bitrate limits recommended by encoder should
1437 // not be applied.
1438 EXPECT_EQ(kAppMaxBitrateKbps,
1439 bitrate_allocator_factory_.codec_config().maxBitrate);
1440 EXPECT_EQ(kDefaultMinBitrateKbps,
1441 bitrate_allocator_factory_.codec_config().minBitrate);
1442
1443 video_encoder_config.max_bitrate_bps = 0;
1444 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1445 kAppMinBitrateKbps * 1000;
1446 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1447 kMaxPayloadLength);
1448 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1449 WaitForEncodedFrame(5);
1450
1451 // App limited the min bitrate - bitrate limits recommended by encoder should
1452 // not be applied.
1453 EXPECT_EQ(kDefaultMaxBitrateKbps,
1454 bitrate_allocator_factory_.codec_config().maxBitrate);
1455 EXPECT_EQ(kAppMinBitrateKbps,
1456 bitrate_allocator_factory_.codec_config().minBitrate);
1457
1458 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1459 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1460 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001461 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1462 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001463 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1464 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001465
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001466 // App limited both min and max bitrates - bitrate limits recommended by
1467 // encoder should not be applied.
1468 EXPECT_EQ(kAppMaxBitrateKbps,
1469 bitrate_allocator_factory_.codec_config().maxBitrate);
1470 EXPECT_EQ(kAppMinBitrateKbps,
1471 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001472
1473 video_stream_encoder_->Stop();
1474}
1475
1476TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001477 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001478 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001479 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1480 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001481
1482 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001483 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001484 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001485 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001486 fake_encoder_.SetResolutionBitrateLimits(
1487 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1488
1489 VideoEncoderConfig video_encoder_config;
1490 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1491 video_encoder_config.max_bitrate_bps = 0;
1492 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1493 kMaxPayloadLength);
1494
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001495 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001496 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1497 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001498 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1499 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001500 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1501 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1502
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001503 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001504 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1505 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001506 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1507 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001508 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1509 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1510
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001511 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001512 // encoder for 360p should be used.
1513 video_source_.IncomingCapturedFrame(
1514 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1515 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001516 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1517 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001518 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1519 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1520
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001521 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001522 // ignored.
1523 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1524 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001525 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1526 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001527 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1528 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001529 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1530 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001531 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1532 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1533
1534 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1535 // for 270p should be used.
1536 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1537 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001538 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1539 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001540 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1541 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1542
1543 video_stream_encoder_->Stop();
1544}
1545
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001546TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1547 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001548 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1549 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001550
1551 VideoEncoderConfig video_encoder_config;
1552 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1553 video_encoder_config.max_bitrate_bps = 0;
1554 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1555 kMaxPayloadLength);
1556
1557 // Encode 720p frame to get the default encoder target bitrate.
1558 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1559 WaitForEncodedFrame(1);
1560 const uint32_t kDefaultTargetBitrateFor720pKbps =
1561 bitrate_allocator_factory_.codec_config()
1562 .simulcastStream[0]
1563 .targetBitrate;
1564
1565 // Set the max recommended encoder bitrate to something lower than the default
1566 // target bitrate.
1567 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1568 1280 * 720, 10 * 1000, 10 * 1000,
1569 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1570 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1571
1572 // Change resolution to trigger encoder reinitialization.
1573 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1574 WaitForEncodedFrame(2);
1575 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1576 WaitForEncodedFrame(3);
1577
1578 // Ensure the target bitrate is capped by the max bitrate.
1579 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1580 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1581 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1582 .simulcastStream[0]
1583 .targetBitrate *
1584 1000,
1585 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1586
1587 video_stream_encoder_->Stop();
1588}
1589
mflodmancc3d4422017-08-03 08:27:51 -07001590TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001591 EXPECT_TRUE(video_source_.has_sinks());
1592 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001593 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001594 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001595 EXPECT_FALSE(video_source_.has_sinks());
1596 EXPECT_TRUE(new_video_source.has_sinks());
1597
mflodmancc3d4422017-08-03 08:27:51 -07001598 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001599}
1600
mflodmancc3d4422017-08-03 08:27:51 -07001601TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001602 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001603 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001604 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001605 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001606}
1607
Jonathan Yubc771b72017-12-08 17:04:29 -08001608TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1609 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001610 const int kWidth = 1280;
1611 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001612
1613 // We rely on the automatic resolution adaptation, but we handle framerate
1614 // adaptation manually by mocking the stats proxy.
1615 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001616
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001617 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001618 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001619 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1620 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001621 video_stream_encoder_->SetSource(&video_source_,
1622 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001623 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001624 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001625 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001626 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1627
Jonathan Yubc771b72017-12-08 17:04:29 -08001628 // Adapt down as far as possible.
1629 rtc::VideoSinkWants last_wants;
1630 int64_t t = 1;
1631 int loop_count = 0;
1632 do {
1633 ++loop_count;
1634 last_wants = video_source_.sink_wants();
1635
1636 // Simulate the framerate we've been asked to adapt to.
1637 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1638 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1639 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1640 mock_stats.input_frame_rate = fps;
1641 stats_proxy_->SetMockStats(mock_stats);
1642
1643 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1644 sink_.WaitForEncodedFrame(t);
1645 t += frame_interval_ms;
1646
mflodmancc3d4422017-08-03 08:27:51 -07001647 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001648 VerifyBalancedModeFpsRange(
1649 video_source_.sink_wants(),
1650 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1651 } while (video_source_.sink_wants().max_pixel_count <
1652 last_wants.max_pixel_count ||
1653 video_source_.sink_wants().max_framerate_fps <
1654 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001655
Jonathan Yubc771b72017-12-08 17:04:29 -08001656 // Verify that we've adapted all the way down.
1657 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001658 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001659 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1660 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001661 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001662 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1663 *video_source_.last_sent_height());
1664 EXPECT_EQ(kMinBalancedFramerateFps,
1665 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001666
Jonathan Yubc771b72017-12-08 17:04:29 -08001667 // Adapt back up the same number of times we adapted down.
1668 for (int i = 0; i < loop_count - 1; ++i) {
1669 last_wants = video_source_.sink_wants();
1670
1671 // Simulate the framerate we've been asked to adapt to.
1672 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1673 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1674 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1675 mock_stats.input_frame_rate = fps;
1676 stats_proxy_->SetMockStats(mock_stats);
1677
1678 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1679 sink_.WaitForEncodedFrame(t);
1680 t += frame_interval_ms;
1681
mflodmancc3d4422017-08-03 08:27:51 -07001682 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001683 VerifyBalancedModeFpsRange(
1684 video_source_.sink_wants(),
1685 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1686 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1687 last_wants.max_pixel_count ||
1688 video_source_.sink_wants().max_framerate_fps >
1689 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001690 }
1691
Åsa Persson8c1bf952018-09-13 10:42:19 +02001692 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001693 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001694 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001695 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1696 EXPECT_EQ((loop_count - 1) * 2,
1697 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001698
mflodmancc3d4422017-08-03 08:27:51 -07001699 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001700}
mflodmancc3d4422017-08-03 08:27:51 -07001701TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001702 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001703 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1704 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001705 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001706
sprangc5d62e22017-04-02 23:53:04 -07001707 const int kFrameWidth = 1280;
1708 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001709
Åsa Persson8c1bf952018-09-13 10:42:19 +02001710 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001711
kthelgason5e13d412016-12-01 03:59:51 -08001712 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001713 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001714 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001715 frame_timestamp += kFrameIntervalMs;
1716
perkj803d97f2016-11-01 11:45:46 -07001717 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001718 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001719 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001720 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001721 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001722 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001723
asapersson0944a802017-04-07 00:57:58 -07001724 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001725 // wanted resolution.
1726 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1727 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1728 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001729 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001730
1731 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001732 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001733 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001734 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001735
sprangc5d62e22017-04-02 23:53:04 -07001736 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001737 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001738
sprangc5d62e22017-04-02 23:53:04 -07001739 // Force an input frame rate to be available, or the adaptation call won't
1740 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001741 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001742 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001743 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001744 stats_proxy_->SetMockStats(stats);
1745
mflodmancc3d4422017-08-03 08:27:51 -07001746 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001747 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001748 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001749 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001750 frame_timestamp += kFrameIntervalMs;
1751
1752 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001753 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001754 EXPECT_EQ(std::numeric_limits<int>::max(),
1755 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001756 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001757
asapersson02465b82017-04-10 01:12:52 -07001758 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001759 video_stream_encoder_->SetSource(&new_video_source,
1760 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001761 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001762
mflodmancc3d4422017-08-03 08:27:51 -07001763 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001764 new_video_source.IncomingCapturedFrame(
1765 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001766 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001767 frame_timestamp += kFrameIntervalMs;
1768
1769 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001770 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001771
1772 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001773 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001774 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001775 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1776 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001777 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001778 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001779
1780 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001781 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001782 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001783 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1784 EXPECT_EQ(std::numeric_limits<int>::max(),
1785 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001786 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001787
mflodmancc3d4422017-08-03 08:27:51 -07001788 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001789}
1790
mflodmancc3d4422017-08-03 08:27:51 -07001791TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001792 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001793 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1794 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001795
asaperssonfab67072017-04-04 05:51:49 -07001796 const int kWidth = 1280;
1797 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001798 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001799 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001800 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1801 EXPECT_FALSE(stats.bw_limited_resolution);
1802 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1803
1804 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001805 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001806 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001807 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001808
1809 stats = stats_proxy_->GetStats();
1810 EXPECT_TRUE(stats.bw_limited_resolution);
1811 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1812
1813 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001814 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001815 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001816 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001817
1818 stats = stats_proxy_->GetStats();
1819 EXPECT_FALSE(stats.bw_limited_resolution);
1820 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1821 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1822
mflodmancc3d4422017-08-03 08:27:51 -07001823 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001824}
1825
mflodmancc3d4422017-08-03 08:27:51 -07001826TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001827 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001828 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1829 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001830
1831 const int kWidth = 1280;
1832 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001833 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001834 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001835 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1836 EXPECT_FALSE(stats.cpu_limited_resolution);
1837 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1838
1839 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001840 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001841 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001842 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001843
1844 stats = stats_proxy_->GetStats();
1845 EXPECT_TRUE(stats.cpu_limited_resolution);
1846 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1847
1848 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001849 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001850 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001851 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001852
1853 stats = stats_proxy_->GetStats();
1854 EXPECT_FALSE(stats.cpu_limited_resolution);
1855 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001856 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001857
mflodmancc3d4422017-08-03 08:27:51 -07001858 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001859}
1860
mflodmancc3d4422017-08-03 08:27:51 -07001861TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001862 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001863 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1864 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001865
asaperssonfab67072017-04-04 05:51:49 -07001866 const int kWidth = 1280;
1867 const int kHeight = 720;
1868 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001869 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001870 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001871 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001872 EXPECT_FALSE(stats.cpu_limited_resolution);
1873 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1874
asaperssonfab67072017-04-04 05:51:49 -07001875 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001876 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001877 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001878 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001879 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001880 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001881 EXPECT_TRUE(stats.cpu_limited_resolution);
1882 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1883
1884 // Set new source with adaptation still enabled.
1885 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001886 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001887 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001888
asaperssonfab67072017-04-04 05:51:49 -07001889 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001890 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001891 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001892 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001893 EXPECT_TRUE(stats.cpu_limited_resolution);
1894 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1895
1896 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001897 video_stream_encoder_->SetSource(&new_video_source,
1898 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001899
asaperssonfab67072017-04-04 05:51:49 -07001900 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001901 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001902 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001903 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001904 EXPECT_FALSE(stats.cpu_limited_resolution);
1905 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1906
1907 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001908 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001909 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001910
asaperssonfab67072017-04-04 05:51:49 -07001911 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001912 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001913 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001914 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001915 EXPECT_TRUE(stats.cpu_limited_resolution);
1916 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1917
asaperssonfab67072017-04-04 05:51:49 -07001918 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001919 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001920 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001921 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001922 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001923 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001924 EXPECT_FALSE(stats.cpu_limited_resolution);
1925 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001926 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001927
mflodmancc3d4422017-08-03 08:27:51 -07001928 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001929}
1930
mflodmancc3d4422017-08-03 08:27:51 -07001931TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001932 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001933 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1934 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001935
asaperssonfab67072017-04-04 05:51:49 -07001936 const int kWidth = 1280;
1937 const int kHeight = 720;
1938 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001939 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001940 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001941 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001942 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001943 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001944
1945 // Set new source with adaptation still enabled.
1946 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001947 video_stream_encoder_->SetSource(&new_video_source,
1948 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001949
asaperssonfab67072017-04-04 05:51:49 -07001950 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001951 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001952 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001953 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001954 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001955 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001956
asaperssonfab67072017-04-04 05:51:49 -07001957 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001958 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001959 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001960 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001961 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001962 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001963 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001964 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001965
asaperssonfab67072017-04-04 05:51:49 -07001966 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001967 video_stream_encoder_->SetSource(&new_video_source,
1968 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001969
asaperssonfab67072017-04-04 05:51:49 -07001970 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001971 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001972 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001973 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001974 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001975 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001976
asapersson02465b82017-04-10 01:12:52 -07001977 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07001978 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001979 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08001980
asaperssonfab67072017-04-04 05:51:49 -07001981 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001982 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001983 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001984 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001985 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001986 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1987 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001988
mflodmancc3d4422017-08-03 08:27:51 -07001989 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001990}
1991
mflodmancc3d4422017-08-03 08:27:51 -07001992TEST_F(VideoStreamEncoderTest,
1993 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001994 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001995 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1996 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07001997
1998 const int kWidth = 1280;
1999 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002000 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002001 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002002 video_source_.IncomingCapturedFrame(
2003 CreateFrame(timestamp_ms, kWidth, kHeight));
2004 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002005 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2006 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2007 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2008
2009 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002010 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002011 timestamp_ms += kFrameIntervalMs;
2012 video_source_.IncomingCapturedFrame(
2013 CreateFrame(timestamp_ms, kWidth, kHeight));
2014 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002015 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2016 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2017 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2018
2019 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002020 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002021 timestamp_ms += kFrameIntervalMs;
2022 video_source_.IncomingCapturedFrame(
2023 CreateFrame(timestamp_ms, kWidth, kHeight));
2024 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002025 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2026 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2027 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2028
Niels Möller4db138e2018-04-19 09:04:13 +02002029 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002030 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002031
2032 VideoEncoderConfig video_encoder_config;
2033 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2034 // Make format different, to force recreation of encoder.
2035 video_encoder_config.video_format.parameters["foo"] = "foo";
2036 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002037 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002038 timestamp_ms += kFrameIntervalMs;
2039 video_source_.IncomingCapturedFrame(
2040 CreateFrame(timestamp_ms, kWidth, kHeight));
2041 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002042 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2043 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2044 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2045
mflodmancc3d4422017-08-03 08:27:51 -07002046 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002047}
2048
mflodmancc3d4422017-08-03 08:27:51 -07002049TEST_F(VideoStreamEncoderTest,
2050 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002051 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002052 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2053 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002054
asapersson0944a802017-04-07 00:57:58 -07002055 const int kWidth = 1280;
2056 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002057 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002058
asaperssonfab67072017-04-04 05:51:49 -07002059 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002060 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002061 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002062 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002063 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002064 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2065
asapersson02465b82017-04-10 01:12:52 -07002066 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002067 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002068 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002069 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002070 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002071 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002072 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002073 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2074
2075 // Set new source with adaptation still enabled.
2076 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002077 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002078 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002079
2080 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002081 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002082 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002083 stats = stats_proxy_->GetStats();
2084 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002085 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002086 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2087
sprangc5d62e22017-04-02 23:53:04 -07002088 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002089 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002090 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002091 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002092 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002093 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002094 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002095 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002096 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002097 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002098 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2099
sprangc5d62e22017-04-02 23:53:04 -07002100 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002101 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002102 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2103 mock_stats.input_frame_rate = 30;
2104 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002105 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002106 stats_proxy_->ResetMockStats();
2107
2108 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002109 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002110 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002111
2112 // Framerate now adapted.
2113 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002114 EXPECT_FALSE(stats.cpu_limited_resolution);
2115 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002116 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2117
2118 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002119 video_stream_encoder_->SetSource(&new_video_source,
2120 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002121 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002122 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002123 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002124
2125 stats = stats_proxy_->GetStats();
2126 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002127 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002128 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2129
2130 // Try to trigger overuse. Should not succeed.
2131 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002132 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002133 stats_proxy_->ResetMockStats();
2134
2135 stats = stats_proxy_->GetStats();
2136 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002137 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002138 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2139
2140 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002141 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002142 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002143 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002144 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002145 stats = stats_proxy_->GetStats();
2146 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002147 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002148 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002149
2150 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002151 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002152 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002153 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002154 stats = stats_proxy_->GetStats();
2155 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002156 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002157 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2158
2159 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002160 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002161 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002162 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002163 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002164 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002165 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002166 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002167 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002168 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002169 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2170
2171 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002172 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002173 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002174 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002175 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002176 stats = stats_proxy_->GetStats();
2177 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002178 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002179 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002180 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002181
mflodmancc3d4422017-08-03 08:27:51 -07002182 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002183}
2184
mflodmancc3d4422017-08-03 08:27:51 -07002185TEST_F(VideoStreamEncoderTest,
2186 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002187 const int kWidth = 1280;
2188 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002189 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002190 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2191 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002192
asaperssonfab67072017-04-04 05:51:49 -07002193 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002194 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002195
asaperssonfab67072017-04-04 05:51:49 -07002196 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002197 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002198
asaperssonfab67072017-04-04 05:51:49 -07002199 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002200 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002201
asaperssonfab67072017-04-04 05:51:49 -07002202 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002203 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002204
kthelgason876222f2016-11-29 01:44:11 -08002205 // Expect a scale down.
2206 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002207 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002208
asapersson02465b82017-04-10 01:12:52 -07002209 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002210 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002211 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002212 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002213
asaperssonfab67072017-04-04 05:51:49 -07002214 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002215 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002216 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002217 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002218
asaperssonfab67072017-04-04 05:51:49 -07002219 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002220 EXPECT_EQ(std::numeric_limits<int>::max(),
2221 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002222
asaperssonfab67072017-04-04 05:51:49 -07002223 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002224 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002225 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002226 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002227
asapersson02465b82017-04-10 01:12:52 -07002228 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002229 EXPECT_EQ(std::numeric_limits<int>::max(),
2230 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002231
mflodmancc3d4422017-08-03 08:27:51 -07002232 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002233}
2234
mflodmancc3d4422017-08-03 08:27:51 -07002235TEST_F(VideoStreamEncoderTest,
2236 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002237 const int kWidth = 1280;
2238 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002239 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002240 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2241 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002242
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002243 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002244 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002245 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002246 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002247
2248 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002249 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002250 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002251 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2252 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2253
2254 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002255 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002256 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002257 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2258 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2259 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2260
2261 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002262 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002263 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2264 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2265 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2266
mflodmancc3d4422017-08-03 08:27:51 -07002267 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002268}
2269
mflodmancc3d4422017-08-03 08:27:51 -07002270TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002271 const int kWidth = 1280;
2272 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002273 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002274 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2275 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002276
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002277 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002278 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002279 video_stream_encoder_->SetSource(&source,
2280 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002281 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2282 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002283 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002284
2285 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002286 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002287 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2288 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2289 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2290 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2291
2292 // Trigger adapt down for same input resolution, expect no change.
2293 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2294 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002295 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002296 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2297 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2298 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2299
2300 // Trigger adapt down for larger input resolution, expect no change.
2301 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2302 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002303 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002304 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2305 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2306 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2307
mflodmancc3d4422017-08-03 08:27:51 -07002308 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002309}
2310
mflodmancc3d4422017-08-03 08:27:51 -07002311TEST_F(VideoStreamEncoderTest,
2312 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002313 const int kWidth = 1280;
2314 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002315 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002316 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2317 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002318
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002319 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002320 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002321 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002322 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002323
2324 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002325 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002326 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002327 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2328 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2329
2330 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002331 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002332 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002333 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2334 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2335
mflodmancc3d4422017-08-03 08:27:51 -07002336 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002337}
2338
mflodmancc3d4422017-08-03 08:27:51 -07002339TEST_F(VideoStreamEncoderTest,
2340 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002341 const int kWidth = 1280;
2342 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002343 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002344 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2345 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002346
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002347 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002348 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002349 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002350 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002351
2352 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002353 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002354 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002355 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002356 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2357
2358 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002359 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002360 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002361 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002362 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2363
mflodmancc3d4422017-08-03 08:27:51 -07002364 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002365}
2366
mflodmancc3d4422017-08-03 08:27:51 -07002367TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002368 const int kWidth = 1280;
2369 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002370 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002371 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2372 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002373
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002374 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002375 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002376 video_stream_encoder_->SetSource(&source,
2377 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002378
2379 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2380 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002381 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002382 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2383 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2384 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2385
2386 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002387 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002388 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002389 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2390 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2391 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2392
mflodmancc3d4422017-08-03 08:27:51 -07002393 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002394}
2395
mflodmancc3d4422017-08-03 08:27:51 -07002396TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002397 const int kWidth = 1280;
2398 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002399 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002400 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2401 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002402
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002403 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002404 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002405 video_stream_encoder_->SetSource(&source,
2406 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002407
2408 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2409 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002410 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002411 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2412 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2413 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2414
2415 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002416 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002417 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002418 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2419 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2420 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2421
mflodmancc3d4422017-08-03 08:27:51 -07002422 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002423}
2424
mflodmancc3d4422017-08-03 08:27:51 -07002425TEST_F(VideoStreamEncoderTest,
2426 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002427 const int kWidth = 1280;
2428 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002429 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002430 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2431 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002432
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002433 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002434 AdaptingFrameForwarder source;
2435 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002436 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002437 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002438
2439 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002440 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002441 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002442 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2443 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2444
2445 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002446 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002447 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002448 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002449 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002450 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2451 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2452
2453 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002454 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002455 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002456 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2457 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2458 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2459
mflodmancc3d4422017-08-03 08:27:51 -07002460 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002461}
2462
mflodmancc3d4422017-08-03 08:27:51 -07002463TEST_F(VideoStreamEncoderTest,
2464 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002465 const int kWidth = 1280;
2466 const int kHeight = 720;
2467 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002468 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002469 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2470 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002471
2472 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2473 stats.input_frame_rate = kInputFps;
2474 stats_proxy_->SetMockStats(stats);
2475
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002476 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002477 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2478 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002479 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002480
2481 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002482 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002483 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2484 sink_.WaitForEncodedFrame(2);
2485 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2486
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002487 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002488 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002489 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002490 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002491 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002492
2493 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002494 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002495 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2496 sink_.WaitForEncodedFrame(3);
2497 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2498
2499 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002500 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002501 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002502
mflodmancc3d4422017-08-03 08:27:51 -07002503 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002504}
2505
mflodmancc3d4422017-08-03 08:27:51 -07002506TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002507 const int kWidth = 1280;
2508 const int kHeight = 720;
2509 const size_t kNumFrames = 10;
2510
Erik Språng4c6ca302019-04-08 15:14:01 +02002511 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002512 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2513 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002514
asaperssond0de2952017-04-21 01:47:31 -07002515 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002516 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002517 video_source_.set_adaptation_enabled(true);
2518
2519 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2520 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2521
2522 int downscales = 0;
2523 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002524 video_source_.IncomingCapturedFrame(
2525 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2526 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002527
asaperssonfab67072017-04-04 05:51:49 -07002528 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002529 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002530 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002531 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002532
2533 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2534 ++downscales;
2535
2536 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2537 EXPECT_EQ(downscales,
2538 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2539 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002540 }
mflodmancc3d4422017-08-03 08:27:51 -07002541 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002542}
2543
mflodmancc3d4422017-08-03 08:27:51 -07002544TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002545 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2546 const int kWidth = 1280;
2547 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002548 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002549 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2550 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002551
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002552 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002553 AdaptingFrameForwarder source;
2554 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002555 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002556 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002557
Åsa Persson8c1bf952018-09-13 10:42:19 +02002558 int64_t timestamp_ms = kFrameIntervalMs;
2559 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002560 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002561 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002562 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2563 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2564
2565 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002566 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002567 timestamp_ms += kFrameIntervalMs;
2568 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2569 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002570 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002571 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2572 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2573
2574 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002575 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002576 timestamp_ms += kFrameIntervalMs;
2577 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002578 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002579 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002580 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2581 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2582
2583 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002584 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002585 timestamp_ms += kFrameIntervalMs;
2586 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2587 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002588 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002589 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2590 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2591
2592 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002593 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002594 timestamp_ms += kFrameIntervalMs;
2595 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002596 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002597 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002598 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2599 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2600
mflodmancc3d4422017-08-03 08:27:51 -07002601 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002602}
2603
mflodmancc3d4422017-08-03 08:27:51 -07002604TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002605 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2606 const int kWidth = 1280;
2607 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002608 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002609 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2610 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002611
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002612 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002613 AdaptingFrameForwarder source;
2614 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002615 video_stream_encoder_->SetSource(&source,
2616 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002617
Åsa Persson8c1bf952018-09-13 10:42:19 +02002618 int64_t timestamp_ms = kFrameIntervalMs;
2619 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002620 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002621 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002622 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2623 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2624
2625 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002626 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002627 timestamp_ms += kFrameIntervalMs;
2628 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2629 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002630 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2631 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2632 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2633
2634 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002635 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002636 timestamp_ms += kFrameIntervalMs;
2637 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002638 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_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2642
2643 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002644 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002645 timestamp_ms += kFrameIntervalMs;
2646 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2647 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002648 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2649 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2650 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2651
2652 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002653 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002654 timestamp_ms += kFrameIntervalMs;
2655 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002656 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002657 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002658 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2659 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2660
mflodmancc3d4422017-08-03 08:27:51 -07002661 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002662}
2663
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002664class BalancedDegradationTest : public VideoStreamEncoderTest {
2665 protected:
2666 void SetupTest() {
2667 // Reset encoder for field trials to take effect.
2668 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002669 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002670
2671 // Enable BALANCED preference.
2672 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002673 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2674 }
2675
2676 void OnBitrateUpdated(int bitrate_bps) {
2677 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2678 DataRate::bps(bitrate_bps),
2679 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002680 }
2681
Åsa Persson45b176f2019-09-30 11:19:05 +02002682 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002683 timestamp_ms_ += kFrameIntervalMs;
2684 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002685 }
2686
2687 void InsertFrameAndWaitForEncoded() {
2688 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002689 sink_.WaitForEncodedFrame(timestamp_ms_);
2690 }
2691
2692 const int kWidth = 640; // pixels:640x360=230400
2693 const int kHeight = 360;
2694 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2695 int64_t timestamp_ms_ = 0;
2696 AdaptingFrameForwarder source_;
2697};
2698
2699TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2700 test::ScopedFieldTrials field_trials(
2701 "WebRTC-Video-BalancedDegradationSettings/"
2702 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2703 SetupTest();
2704
2705 // Force input frame rate.
2706 const int kInputFps = 24;
2707 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2708 stats.input_frame_rate = kInputFps;
2709 stats_proxy_->SetMockStats(stats);
2710
Åsa Persson45b176f2019-09-30 11:19:05 +02002711 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002712 VerifyFpsMaxResolutionMax(source_.sink_wants());
2713
2714 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2715 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2716 video_stream_encoder_->TriggerQualityLowExpectFalse();
2717 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2718
2719 video_stream_encoder_->Stop();
2720}
2721
2722TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2723 test::ScopedFieldTrials field_trials(
2724 "WebRTC-Video-BalancedDegradationSettings/"
2725 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2726 SetupTest();
2727
2728 // Force input frame rate.
2729 const int kInputFps = 25;
2730 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2731 stats.input_frame_rate = kInputFps;
2732 stats_proxy_->SetMockStats(stats);
2733
Åsa Persson45b176f2019-09-30 11:19:05 +02002734 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002735 VerifyFpsMaxResolutionMax(source_.sink_wants());
2736
2737 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2738 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2739 video_stream_encoder_->TriggerQualityLow();
2740 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2741
2742 video_stream_encoder_->Stop();
2743}
2744
2745TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2746 test::ScopedFieldTrials field_trials(
2747 "WebRTC-Video-BalancedDegradationSettings/"
2748 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2749 SetupTest();
2750
2751 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2752
Åsa Persson45b176f2019-09-30 11:19:05 +02002753 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002754 VerifyFpsMaxResolutionMax(source_.sink_wants());
2755
2756 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2757 video_stream_encoder_->TriggerQualityLow();
2758 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2759
2760 video_stream_encoder_->Stop();
2761}
2762
Åsa Perssonccfb3402019-09-25 15:13:04 +02002763TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002764 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002765 "WebRTC-Video-BalancedDegradationSettings/"
2766 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002767 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002768
Åsa Persson1b247f12019-08-14 17:26:39 +02002769 const int kMinBitrateBps = 425000;
2770 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002771 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002772
Åsa Persson45b176f2019-09-30 11:19:05 +02002773 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002774 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002775 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2776
2777 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2778 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002779 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002780 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002781 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2782
2783 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2784 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002785 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002786 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002787 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2788
Åsa Persson30ab0152019-08-27 12:22:33 +02002789 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2790 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002791 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002792 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2793 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002794 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2795
2796 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002797 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002798 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002799 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002800
Åsa Persson30ab0152019-08-27 12:22:33 +02002801 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002802 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002803 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002804 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002805 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002806 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2807
2808 video_stream_encoder_->Stop();
2809}
2810
Åsa Perssonccfb3402019-09-25 15:13:04 +02002811TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002812 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2813 test::ScopedFieldTrials field_trials(
2814 "WebRTC-Video-BalancedDegradationSettings/"
2815 "pixels:57600|129600|230400,fps:7|24|24/");
2816 SetupTest();
2817 OnBitrateUpdated(kLowTargetBitrateBps);
2818
2819 VerifyNoLimitation(source_.sink_wants());
2820
2821 // Insert frame, expect scaled down:
2822 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2823 InsertFrame();
2824 EXPECT_FALSE(WaitForFrame(1000));
2825 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2826 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2827
2828 // Insert frame, expect scaled down:
2829 // resolution (320x180@24fps).
2830 InsertFrame();
2831 EXPECT_FALSE(WaitForFrame(1000));
2832 EXPECT_LT(source_.sink_wants().max_pixel_count,
2833 source_.last_wants().max_pixel_count);
2834 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2835
2836 // Frame should not be dropped (min pixels per frame reached).
2837 InsertFrameAndWaitForEncoded();
2838
2839 video_stream_encoder_->Stop();
2840}
2841
2842TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002843 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002844 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002845 "WebRTC-Video-BalancedDegradationSettings/"
2846 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002847 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002848
Åsa Persson30ab0152019-08-27 12:22:33 +02002849 const int kResolutionMinBitrateBps = 435000;
2850 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002851 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002852
Åsa Persson45b176f2019-09-30 11:19:05 +02002853 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002854 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002855 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2856
2857 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2858 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002859 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002860 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002861 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2862
2863 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2864 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002865 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002866 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002867 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2868
2869 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2870 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002871 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002872 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002873 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2874
Åsa Persson30ab0152019-08-27 12:22:33 +02002875 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2876 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002877 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002878 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002879 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2880
2881 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2882 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002883 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002884 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2885
2886 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002887 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002888 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002889 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002890 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002891 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2892
2893 video_stream_encoder_->Stop();
2894}
2895
Åsa Perssonccfb3402019-09-25 15:13:04 +02002896TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002897 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002898 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002899 "WebRTC-Video-BalancedDegradationSettings/"
2900 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002901 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002902
Åsa Persson30ab0152019-08-27 12:22:33 +02002903 const int kMinBitrateBps = 425000;
2904 const int kTooLowMinBitrateBps = 424000;
2905 const int kResolutionMinBitrateBps = 435000;
2906 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002907 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002908
Åsa Persson45b176f2019-09-30 11:19:05 +02002909 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002910 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002911 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2912
2913 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2914 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002915 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002916 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002917 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2918
2919 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2920 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002921 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002922 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002923 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2924
2925 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2926 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002927 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002928 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002929 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2930
2931 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
2932 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002933 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002934 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2935
2936 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002937 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002938 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002939 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002940 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002941 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2942
2943 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002944 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002945 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002946 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002947 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2948
2949 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002950 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002951 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002952 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002953 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002954 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2955
Åsa Persson1b247f12019-08-14 17:26:39 +02002956 video_stream_encoder_->Stop();
2957}
2958
mflodmancc3d4422017-08-03 08:27:51 -07002959TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002960 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
2961 const int kWidth = 1280;
2962 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002963 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002964 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2965 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002966
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002967 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002968 AdaptingFrameForwarder source;
2969 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002970 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002971 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002972
Åsa Persson8c1bf952018-09-13 10:42:19 +02002973 int64_t timestamp_ms = kFrameIntervalMs;
2974 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002975 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002976 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002977 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2978 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2979 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2980 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2981
2982 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07002983 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002984 timestamp_ms += kFrameIntervalMs;
2985 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2986 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002987 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002988 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2989 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2990 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2991 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2992
2993 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07002994 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002995 timestamp_ms += kFrameIntervalMs;
2996 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2997 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002998 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07002999 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3000 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3001 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3002 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3003
Jonathan Yubc771b72017-12-08 17:04:29 -08003004 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003005 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003006 timestamp_ms += kFrameIntervalMs;
3007 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3008 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003009 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003010 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3011 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003012 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003013 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3014
Jonathan Yubc771b72017-12-08 17:04:29 -08003015 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003016 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003017 timestamp_ms += kFrameIntervalMs;
3018 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3019 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003020 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003021 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003022 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3023 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3024 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3025 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3026
Jonathan Yubc771b72017-12-08 17:04:29 -08003027 // Trigger quality adapt down, expect no change (min resolution reached).
3028 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003029 timestamp_ms += kFrameIntervalMs;
3030 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3031 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003032 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3033 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3034 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3035 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3036 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3037
3038 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003039 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003040 timestamp_ms += kFrameIntervalMs;
3041 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3042 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003043 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003044 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3045 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3046 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3047 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3048
3049 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3050 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003051 timestamp_ms += kFrameIntervalMs;
3052 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3053 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003054 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3055 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3056 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3057 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3058 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3059
3060 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3061 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003062 timestamp_ms += kFrameIntervalMs;
3063 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3064 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003065 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003066 last_wants = source.sink_wants();
3067 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3068 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003069 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003070 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3071
3072 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003073 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003074 timestamp_ms += kFrameIntervalMs;
3075 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3076 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003077 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003078 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3079 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003080 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003081 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3082
3083 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003084 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003085 timestamp_ms += kFrameIntervalMs;
3086 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003087 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003088 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003089 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003090 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3091 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003092 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003093 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003094
mflodmancc3d4422017-08-03 08:27:51 -07003095 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003096}
3097
mflodmancc3d4422017-08-03 08:27:51 -07003098TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003099 const int kWidth = 640;
3100 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003101
Erik Språng4c6ca302019-04-08 15:14:01 +02003102 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003103 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3104 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003105
perkj803d97f2016-11-01 11:45:46 -07003106 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003107 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003108 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003109 }
3110
mflodmancc3d4422017-08-03 08:27:51 -07003111 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003112 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003113 video_source_.IncomingCapturedFrame(CreateFrame(
3114 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003115 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003116 }
3117
mflodmancc3d4422017-08-03 08:27:51 -07003118 video_stream_encoder_->Stop();
3119 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003120 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003121
perkj803d97f2016-11-01 11:45:46 -07003122 EXPECT_EQ(1,
3123 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3124 EXPECT_EQ(
3125 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3126}
3127
mflodmancc3d4422017-08-03 08:27:51 -07003128TEST_F(VideoStreamEncoderTest,
3129 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003130 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003131 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3132 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003133 const int kWidth = 640;
3134 const int kHeight = 360;
3135
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003136 video_stream_encoder_->SetSource(&video_source_,
3137 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003138
3139 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3140 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003141 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003142 }
3143
mflodmancc3d4422017-08-03 08:27:51 -07003144 video_stream_encoder_->Stop();
3145 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003146 stats_proxy_.reset();
3147
3148 EXPECT_EQ(0,
3149 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3150}
3151
mflodmancc3d4422017-08-03 08:27:51 -07003152TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003153 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003154 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003155
3156 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003157 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003158 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003159 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3160 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003161
sprang57c2fff2017-01-16 06:24:02 -08003162 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003163 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003164 video_stream_encoder_->OnBitrateUpdated(
3165 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3166 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003167
sprang57c2fff2017-01-16 06:24:02 -08003168 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003169 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3170 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003171 VideoBitrateAllocation bitrate_allocation =
3172 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003173 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003174 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003175 // TODO(srte): The use of millisecs here looks like an error, but the tests
3176 // fails using seconds, this should be investigated.
3177 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003178
3179 // Not called on second frame.
3180 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3181 .Times(0);
3182 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003183 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3184 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003185 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003186
3187 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003188 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3189 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003190 const int64_t start_time_ms = rtc::TimeMillis();
3191 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3192 video_source_.IncomingCapturedFrame(
3193 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3194 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003195 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003196 }
3197
3198 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003199 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003200
mflodmancc3d4422017-08-03 08:27:51 -07003201 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003202}
3203
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003204TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3205 // 2 TLs configured, temporal layers supported by encoder.
3206 const int kNumTemporalLayers = 2;
3207 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3208 fake_encoder_.SetTemporalLayersSupported(0, true);
3209
3210 // Bitrate allocated across temporal layers.
3211 const int kTl0Bps = kTargetBitrateBps *
3212 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3213 kNumTemporalLayers, /*temporal_id*/ 0);
3214 const int kTl1Bps = kTargetBitrateBps *
3215 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3216 kNumTemporalLayers, /*temporal_id*/ 1);
3217 VideoBitrateAllocation expected_bitrate;
3218 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3219 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3220
3221 VerifyAllocatedBitrate(expected_bitrate);
3222 video_stream_encoder_->Stop();
3223}
3224
3225TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3226 // 2 TLs configured, temporal layers not supported by encoder.
3227 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3228 fake_encoder_.SetTemporalLayersSupported(0, false);
3229
3230 // Temporal layers not supported by the encoder.
3231 // Total bitrate should be at ti:0.
3232 VideoBitrateAllocation expected_bitrate;
3233 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3234
3235 VerifyAllocatedBitrate(expected_bitrate);
3236 video_stream_encoder_->Stop();
3237}
3238
3239TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3240 // 2 TLs configured, temporal layers only supported for first stream.
3241 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3242 fake_encoder_.SetTemporalLayersSupported(0, true);
3243 fake_encoder_.SetTemporalLayersSupported(1, false);
3244
3245 const int kS0Bps = 150000;
3246 const int kS0Tl0Bps =
3247 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3248 /*num_layers*/ 2, /*temporal_id*/ 0);
3249 const int kS0Tl1Bps =
3250 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3251 /*num_layers*/ 2, /*temporal_id*/ 1);
3252 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3253 // Temporal layers not supported by si:1.
3254 VideoBitrateAllocation expected_bitrate;
3255 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3256 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3257 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3258
3259 VerifyAllocatedBitrate(expected_bitrate);
3260 video_stream_encoder_->Stop();
3261}
3262
Niels Möller7dc26b72017-12-06 10:27:48 +01003263TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3264 const int kFrameWidth = 1280;
3265 const int kFrameHeight = 720;
3266 const int kFramerate = 24;
3267
Erik Språng4c6ca302019-04-08 15:14:01 +02003268 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003269 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3270 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003271 test::FrameForwarder source;
3272 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003273 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003274
3275 // Insert a single frame, triggering initial configuration.
3276 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3277 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3278
3279 EXPECT_EQ(
3280 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3281 kDefaultFramerate);
3282
3283 // Trigger reconfigure encoder (without resetting the entire instance).
3284 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003285 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003286 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3287 video_encoder_config.number_of_streams = 1;
3288 video_encoder_config.video_stream_factory =
3289 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3290 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003291 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003292 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3293
3294 // Detector should be updated with fps limit from codec config.
3295 EXPECT_EQ(
3296 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3297 kFramerate);
3298
3299 // Trigger overuse, max framerate should be reduced.
3300 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3301 stats.input_frame_rate = kFramerate;
3302 stats_proxy_->SetMockStats(stats);
3303 video_stream_encoder_->TriggerCpuOveruse();
3304 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3305 int adapted_framerate =
3306 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3307 EXPECT_LT(adapted_framerate, kFramerate);
3308
3309 // Trigger underuse, max framerate should go back to codec configured fps.
3310 // Set extra low fps, to make sure it's actually reset, not just incremented.
3311 stats = stats_proxy_->GetStats();
3312 stats.input_frame_rate = adapted_framerate / 2;
3313 stats_proxy_->SetMockStats(stats);
3314 video_stream_encoder_->TriggerCpuNormalUsage();
3315 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3316 EXPECT_EQ(
3317 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3318 kFramerate);
3319
3320 video_stream_encoder_->Stop();
3321}
3322
3323TEST_F(VideoStreamEncoderTest,
3324 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3325 const int kFrameWidth = 1280;
3326 const int kFrameHeight = 720;
3327 const int kLowFramerate = 15;
3328 const int kHighFramerate = 25;
3329
Erik Språng4c6ca302019-04-08 15:14:01 +02003330 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003331 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3332 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003333 test::FrameForwarder source;
3334 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003335 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003336
3337 // Trigger initial configuration.
3338 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003339 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003340 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3341 video_encoder_config.number_of_streams = 1;
3342 video_encoder_config.video_stream_factory =
3343 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3344 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3345 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003346 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003347 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3348
3349 EXPECT_EQ(
3350 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3351 kLowFramerate);
3352
3353 // Trigger overuse, max framerate should be reduced.
3354 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3355 stats.input_frame_rate = kLowFramerate;
3356 stats_proxy_->SetMockStats(stats);
3357 video_stream_encoder_->TriggerCpuOveruse();
3358 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3359 int adapted_framerate =
3360 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3361 EXPECT_LT(adapted_framerate, kLowFramerate);
3362
3363 // Reconfigure the encoder with a new (higher max framerate), max fps should
3364 // still respect the adaptation.
3365 video_encoder_config.video_stream_factory =
3366 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3367 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3368 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003369 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003370 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3371
3372 EXPECT_EQ(
3373 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3374 adapted_framerate);
3375
3376 // Trigger underuse, max framerate should go back to codec configured fps.
3377 stats = stats_proxy_->GetStats();
3378 stats.input_frame_rate = adapted_framerate;
3379 stats_proxy_->SetMockStats(stats);
3380 video_stream_encoder_->TriggerCpuNormalUsage();
3381 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3382 EXPECT_EQ(
3383 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3384 kHighFramerate);
3385
3386 video_stream_encoder_->Stop();
3387}
3388
mflodmancc3d4422017-08-03 08:27:51 -07003389TEST_F(VideoStreamEncoderTest,
3390 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003391 const int kFrameWidth = 1280;
3392 const int kFrameHeight = 720;
3393 const int kFramerate = 24;
3394
Erik Språng4c6ca302019-04-08 15:14:01 +02003395 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003396 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3397 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003398 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003399 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003400 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003401
3402 // Trigger initial configuration.
3403 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003404 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003405 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3406 video_encoder_config.number_of_streams = 1;
3407 video_encoder_config.video_stream_factory =
3408 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3409 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003410 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003411 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003412 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003413
Niels Möller7dc26b72017-12-06 10:27:48 +01003414 EXPECT_EQ(
3415 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3416 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003417
3418 // Trigger overuse, max framerate should be reduced.
3419 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3420 stats.input_frame_rate = kFramerate;
3421 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003422 video_stream_encoder_->TriggerCpuOveruse();
3423 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003424 int adapted_framerate =
3425 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003426 EXPECT_LT(adapted_framerate, kFramerate);
3427
3428 // Change degradation preference to not enable framerate scaling. Target
3429 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003430 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003431 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003432 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003433 EXPECT_EQ(
3434 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3435 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003436
mflodmancc3d4422017-08-03 08:27:51 -07003437 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003438}
3439
mflodmancc3d4422017-08-03 08:27:51 -07003440TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003441 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003442 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003443 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003444 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003445 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003446 const int kWidth = 640;
3447 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003448
asaperssonfab67072017-04-04 05:51:49 -07003449 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003450
3451 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003452 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003453
3454 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003455 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003456
sprangc5d62e22017-04-02 23:53:04 -07003457 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003458
asaperssonfab67072017-04-04 05:51:49 -07003459 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003460 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003461 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003462
3463 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003464 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003465
sprangc5d62e22017-04-02 23:53:04 -07003466 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003467
mflodmancc3d4422017-08-03 08:27:51 -07003468 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003469}
3470
mflodmancc3d4422017-08-03 08:27:51 -07003471TEST_F(VideoStreamEncoderTest,
3472 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003473 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003474 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003475 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003476 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003477 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003478 const int kWidth = 640;
3479 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003480
3481 // We expect the n initial frames to get dropped.
3482 int i;
3483 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003484 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003485 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003486 }
3487 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003488 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003489 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003490
3491 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003492 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003493
mflodmancc3d4422017-08-03 08:27:51 -07003494 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003495}
3496
mflodmancc3d4422017-08-03 08:27:51 -07003497TEST_F(VideoStreamEncoderTest,
3498 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003499 const int kWidth = 640;
3500 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003501 video_stream_encoder_->OnBitrateUpdated(
3502 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3503 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003504
3505 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003506 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003507 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003508
asaperssonfab67072017-04-04 05:51:49 -07003509 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003510 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003511 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003512
mflodmancc3d4422017-08-03 08:27:51 -07003513 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003514}
3515
mflodmancc3d4422017-08-03 08:27:51 -07003516TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003517 const int kWidth = 640;
3518 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003519 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003520
3521 VideoEncoderConfig video_encoder_config;
3522 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3523 // Make format different, to force recreation of encoder.
3524 video_encoder_config.video_format.parameters["foo"] = "foo";
3525 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003526 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003527 video_stream_encoder_->OnBitrateUpdated(
3528 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3529 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003530
kthelgasonb83797b2017-02-14 11:57:25 -08003531 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003532 video_stream_encoder_->SetSource(&video_source_,
3533 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003534
asaperssonfab67072017-04-04 05:51:49 -07003535 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003536 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003537 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003538
mflodmancc3d4422017-08-03 08:27:51 -07003539 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003540 fake_encoder_.SetQualityScaling(true);
3541}
3542
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003543TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3544 webrtc::test::ScopedFieldTrials field_trials(
3545 "WebRTC-InitialFramedrop/Enabled/");
3546 // Reset encoder for field trials to take effect.
3547 ConfigureEncoder(video_encoder_config_.Copy());
3548 const int kTooLowBitrateForFrameSizeBps = 10000;
3549 const int kWidth = 640;
3550 const int kHeight = 360;
3551
Erik Språng4c6ca302019-04-08 15:14:01 +02003552 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003553 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3554 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003555 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3556 // Frame should not be dropped.
3557 WaitForEncodedFrame(1);
3558
Erik Språng610c7632019-03-06 15:37:33 +01003559 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003560 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003561 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003562 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003563 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3564 // Expect to drop this frame, the wait should time out.
3565 ExpectDroppedFrame();
3566
3567 // Expect the sink_wants to specify a scaled frame.
3568 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3569 video_stream_encoder_->Stop();
3570}
3571
Åsa Persson139f4dc2019-08-02 09:29:58 +02003572TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3573 webrtc::test::ScopedFieldTrials field_trials(
3574 "WebRTC-Video-QualityScalerSettings/"
3575 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3576 // Reset encoder for field trials to take effect.
3577 ConfigureEncoder(video_encoder_config_.Copy());
3578 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3579 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3580 const int kWidth = 640;
3581 const int kHeight = 360;
3582
3583 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003584 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3585 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003586 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3587 // Frame should not be dropped.
3588 WaitForEncodedFrame(1);
3589
3590 video_stream_encoder_->OnBitrateUpdated(
3591 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003592 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003593 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3594 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3595 // Frame should not be dropped.
3596 WaitForEncodedFrame(2);
3597
3598 video_stream_encoder_->OnBitrateUpdated(
3599 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003600 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003601 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3602 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3603 // Expect to drop this frame, the wait should time out.
3604 ExpectDroppedFrame();
3605
3606 // Expect the sink_wants to specify a scaled frame.
3607 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3608 video_stream_encoder_->Stop();
3609}
3610
mflodmancc3d4422017-08-03 08:27:51 -07003611TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003612 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3613 const int kTooSmallWidth = 10;
3614 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003615 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003616 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3617 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003618
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003619 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003620 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003621 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003622 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003623 VerifyNoLimitation(source.sink_wants());
3624 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3625
3626 // Trigger adapt down, too small frame, expect no change.
3627 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003628 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003629 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003630 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003631 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3632 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3633
mflodmancc3d4422017-08-03 08:27:51 -07003634 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003635}
3636
mflodmancc3d4422017-08-03 08:27:51 -07003637TEST_F(VideoStreamEncoderTest,
3638 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003639 const int kTooSmallWidth = 10;
3640 const int kTooSmallHeight = 10;
3641 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003642 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003643 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3644 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003645
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003646 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003647 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003648 video_stream_encoder_->SetSource(&source,
3649 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003650 VerifyNoLimitation(source.sink_wants());
3651 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3652 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3653
3654 // Trigger adapt down, expect limited framerate.
3655 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003656 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003657 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003658 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3659 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3660 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3661 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3662
3663 // Trigger adapt down, too small frame, expect no change.
3664 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003665 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003666 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003667 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3668 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3669 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3670 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3671
mflodmancc3d4422017-08-03 08:27:51 -07003672 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003673}
3674
mflodmancc3d4422017-08-03 08:27:51 -07003675TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003676 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003677 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003678 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3679 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003680 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003681 const int kFrameWidth = 1280;
3682 const int kFrameHeight = 720;
3683 video_source_.IncomingCapturedFrame(
3684 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003685 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003686 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003687}
3688
sprangb1ca0732017-02-01 08:38:12 -08003689// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003690TEST_F(VideoStreamEncoderTest,
3691 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003692 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003693 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3694 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003695
3696 const int kFrameWidth = 1280;
3697 const int kFrameHeight = 720;
3698 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003699 // requested by
3700 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003701 video_source_.set_adaptation_enabled(true);
3702
3703 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003704 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003705 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003706
3707 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003708 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003709 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003710 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003711 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003712
asaperssonfab67072017-04-04 05:51:49 -07003713 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003714 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003715 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003716 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003717 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003718
mflodmancc3d4422017-08-03 08:27:51 -07003719 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003720}
sprangfe627f32017-03-29 08:24:59 -07003721
mflodmancc3d4422017-08-03 08:27:51 -07003722TEST_F(VideoStreamEncoderTest,
3723 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003724 const int kFrameWidth = 1280;
3725 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003726
Erik Språng4c6ca302019-04-08 15:14:01 +02003727 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003728 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3729 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003730 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003731 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003732 video_source_.set_adaptation_enabled(true);
3733
sprang4847ae62017-06-27 07:06:52 -07003734 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003735
3736 video_source_.IncomingCapturedFrame(
3737 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003738 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003739
3740 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003741 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003742
3743 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003744 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003745 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003746 video_source_.IncomingCapturedFrame(
3747 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003748 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003749 }
3750
3751 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003752 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003753 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003754 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003755 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003756 video_source_.IncomingCapturedFrame(
3757 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003758 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003759 ++num_frames_dropped;
3760 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003761 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003762 }
3763 }
3764
sprang4847ae62017-06-27 07:06:52 -07003765 // Add some slack to account for frames dropped by the frame dropper.
3766 const int kErrorMargin = 1;
3767 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003768 kErrorMargin);
3769
3770 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003771 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003772 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003773 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003774 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003775 video_source_.IncomingCapturedFrame(
3776 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003777 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003778 ++num_frames_dropped;
3779 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003780 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003781 }
3782 }
sprang4847ae62017-06-27 07:06:52 -07003783 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003784 kErrorMargin);
3785
3786 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003787 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003788 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003789 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003790 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003791 video_source_.IncomingCapturedFrame(
3792 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003793 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003794 ++num_frames_dropped;
3795 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003796 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003797 }
3798 }
sprang4847ae62017-06-27 07:06:52 -07003799 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003800 kErrorMargin);
3801
3802 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003803 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003804 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003805 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003806 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003807 video_source_.IncomingCapturedFrame(
3808 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003809 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003810 ++num_frames_dropped;
3811 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003812 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003813 }
3814 }
3815 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3816
mflodmancc3d4422017-08-03 08:27:51 -07003817 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003818}
3819
mflodmancc3d4422017-08-03 08:27:51 -07003820TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003821 const int kFramerateFps = 5;
3822 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003823 const int kFrameWidth = 1280;
3824 const int kFrameHeight = 720;
3825
sprang4847ae62017-06-27 07:06:52 -07003826 // Reconfigure encoder with two temporal layers and screensharing, which will
3827 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003828 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003829
Erik Språng4c6ca302019-04-08 15:14:01 +02003830 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003831 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3832 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003833 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003834 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003835 video_source_.set_adaptation_enabled(true);
3836
sprang4847ae62017-06-27 07:06:52 -07003837 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003838
3839 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003840 rtc::VideoSinkWants last_wants;
3841 do {
3842 last_wants = video_source_.sink_wants();
3843
sprangc5d62e22017-04-02 23:53:04 -07003844 // Insert frames to get a new fps estimate...
3845 for (int j = 0; j < kFramerateFps; ++j) {
3846 video_source_.IncomingCapturedFrame(
3847 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08003848 if (video_source_.last_sent_width()) {
3849 sink_.WaitForEncodedFrame(timestamp_ms);
3850 }
sprangc5d62e22017-04-02 23:53:04 -07003851 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02003852 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07003853 }
3854 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07003855 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08003856 } while (video_source_.sink_wants().max_framerate_fps <
3857 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07003858
Jonathan Yubc771b72017-12-08 17:04:29 -08003859 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07003860
mflodmancc3d4422017-08-03 08:27:51 -07003861 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003862}
asaperssonf7e294d2017-06-13 23:25:22 -07003863
mflodmancc3d4422017-08-03 08:27:51 -07003864TEST_F(VideoStreamEncoderTest,
3865 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003866 const int kWidth = 1280;
3867 const int kHeight = 720;
3868 const int64_t kFrameIntervalMs = 150;
3869 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003870 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003871 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3872 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003873
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003874 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003875 AdaptingFrameForwarder source;
3876 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003877 video_stream_encoder_->SetSource(&source,
3878 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003879 timestamp_ms += kFrameIntervalMs;
3880 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003881 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003882 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003883 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3884 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3885 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3886
3887 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003888 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003889 timestamp_ms += kFrameIntervalMs;
3890 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003891 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003892 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
3893 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3894 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3895 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3896
3897 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003898 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003899 timestamp_ms += kFrameIntervalMs;
3900 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003901 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003902 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
3903 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3904 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3905 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3906
3907 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003908 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003909 timestamp_ms += kFrameIntervalMs;
3910 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003911 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003912 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3913 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3914 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3915 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3916
3917 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003918 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003919 timestamp_ms += kFrameIntervalMs;
3920 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003921 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003922 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3923 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3924 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3925 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3926
3927 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003928 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003929 timestamp_ms += kFrameIntervalMs;
3930 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003931 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003932 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3933 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3934 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3935 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3936
3937 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003938 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003939 timestamp_ms += kFrameIntervalMs;
3940 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003941 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003942 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3943 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3944 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3945 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3946
3947 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07003948 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003949 timestamp_ms += kFrameIntervalMs;
3950 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003951 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003952 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3953 rtc::VideoSinkWants last_wants = source.sink_wants();
3954 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3955 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3956 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3957
3958 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07003959 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003960 timestamp_ms += kFrameIntervalMs;
3961 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003962 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003963 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
3964 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3965 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3966 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3967
3968 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003969 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003970 timestamp_ms += kFrameIntervalMs;
3971 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003972 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003973 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
3974 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3975 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3976 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3977
3978 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003979 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003980 timestamp_ms += kFrameIntervalMs;
3981 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003982 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003983 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
3984 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3985 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3986 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3987
3988 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003989 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003990 timestamp_ms += kFrameIntervalMs;
3991 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003992 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003993 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
3994 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3995 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3996 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3997
3998 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003999 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004000 timestamp_ms += kFrameIntervalMs;
4001 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004002 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004003 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4004 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4005 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4006 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4007
4008 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004009 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004010 timestamp_ms += kFrameIntervalMs;
4011 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004012 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004013 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4014 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4015 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4016 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4017
4018 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004019 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004020 timestamp_ms += kFrameIntervalMs;
4021 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004022 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004023 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4024 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4025 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4026 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4027
Åsa Persson30ab0152019-08-27 12:22:33 +02004028 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004029 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004030 timestamp_ms += kFrameIntervalMs;
4031 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004032 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004033 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004034 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004035 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4036 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4037 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4038
4039 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004040 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004041 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004042 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4043
mflodmancc3d4422017-08-03 08:27:51 -07004044 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004045}
4046
mflodmancc3d4422017-08-03 08:27:51 -07004047TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004048 const int kWidth = 1280;
4049 const int kHeight = 720;
4050 const int64_t kFrameIntervalMs = 150;
4051 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004052 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004053 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4054 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004055
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004056 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004057 AdaptingFrameForwarder source;
4058 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004059 video_stream_encoder_->SetSource(&source,
4060 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004061 timestamp_ms += kFrameIntervalMs;
4062 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004063 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004064 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004065 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4066 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4067 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4068 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4069 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4070 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4071
4072 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004073 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004074 timestamp_ms += kFrameIntervalMs;
4075 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004076 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004077 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4078 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4079 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4080 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4081 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4082 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4083 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4084
4085 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004086 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004087 timestamp_ms += kFrameIntervalMs;
4088 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004089 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004090 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4091 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4092 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4093 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4094 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4095 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4096 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4097
4098 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004099 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004100 timestamp_ms += kFrameIntervalMs;
4101 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004102 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004103 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4104 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4105 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4106 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4107 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4108 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4109 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4110
4111 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004112 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004113 timestamp_ms += kFrameIntervalMs;
4114 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004115 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004116 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4117 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4118 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4119 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4120 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4121 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4122 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4123
4124 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004125 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004126 timestamp_ms += kFrameIntervalMs;
4127 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004128 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004129 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4130 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4131 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4132 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4133 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4134 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4135 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4136
4137 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004138 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004139 timestamp_ms += kFrameIntervalMs;
4140 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004141 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004142 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004143 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004144 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4145 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4146 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4147 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4148 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4149 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4150
4151 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004152 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004153 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004154 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4155 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4156
mflodmancc3d4422017-08-03 08:27:51 -07004157 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004158}
4159
mflodmancc3d4422017-08-03 08:27:51 -07004160TEST_F(VideoStreamEncoderTest,
4161 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004162 const int kWidth = 640;
4163 const int kHeight = 360;
4164 const int kFpsLimit = 15;
4165 const int64_t kFrameIntervalMs = 150;
4166 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004167 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004168 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4169 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004170
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004171 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004172 AdaptingFrameForwarder source;
4173 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004174 video_stream_encoder_->SetSource(&source,
4175 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004176 timestamp_ms += kFrameIntervalMs;
4177 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004178 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004179 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004180 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4181 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4182 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4183 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4184 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4185 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4186
4187 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004188 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004189 timestamp_ms += kFrameIntervalMs;
4190 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004191 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004192 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4193 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4194 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4195 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4196 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4197 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4198 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4199
4200 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004201 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004202 timestamp_ms += kFrameIntervalMs;
4203 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004204 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004205 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4206 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4207 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4208 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4209 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4210 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4211 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4212
4213 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004214 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004215 timestamp_ms += kFrameIntervalMs;
4216 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004217 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004218 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4219 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4220 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4221 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4222 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4223 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4224 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4225
4226 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004227 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004228 timestamp_ms += kFrameIntervalMs;
4229 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004230 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004231 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004232 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4233 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4234 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4235 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4236 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4237 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4238
4239 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004240 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004241 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004242 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4243 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4244
mflodmancc3d4422017-08-03 08:27:51 -07004245 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004246}
4247
mflodmancc3d4422017-08-03 08:27:51 -07004248TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004249 const int kFrameWidth = 1920;
4250 const int kFrameHeight = 1080;
4251 // 3/4 of 1920.
4252 const int kAdaptedFrameWidth = 1440;
4253 // 3/4 of 1080 rounded down to multiple of 4.
4254 const int kAdaptedFrameHeight = 808;
4255 const int kFramerate = 24;
4256
Erik Språng4c6ca302019-04-08 15:14:01 +02004257 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004258 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4259 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004260 // Trigger reconfigure encoder (without resetting the entire instance).
4261 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004262 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004263 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4264 video_encoder_config.number_of_streams = 1;
4265 video_encoder_config.video_stream_factory =
4266 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004267 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004268 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004269 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004270
4271 video_source_.set_adaptation_enabled(true);
4272
4273 video_source_.IncomingCapturedFrame(
4274 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004275 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004276
4277 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004278 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004279 video_source_.IncomingCapturedFrame(
4280 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004281 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004282
mflodmancc3d4422017-08-03 08:27:51 -07004283 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004284}
4285
mflodmancc3d4422017-08-03 08:27:51 -07004286TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004287 const int kFrameWidth = 1280;
4288 const int kFrameHeight = 720;
4289 const int kLowFps = 2;
4290 const int kHighFps = 30;
4291
Erik Språng4c6ca302019-04-08 15:14:01 +02004292 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004293 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4294 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004295
4296 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4297 max_framerate_ = kLowFps;
4298
4299 // Insert 2 seconds of 2fps video.
4300 for (int i = 0; i < kLowFps * 2; ++i) {
4301 video_source_.IncomingCapturedFrame(
4302 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4303 WaitForEncodedFrame(timestamp_ms);
4304 timestamp_ms += 1000 / kLowFps;
4305 }
4306
4307 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004308 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004309 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4310 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004311 video_source_.IncomingCapturedFrame(
4312 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4313 WaitForEncodedFrame(timestamp_ms);
4314 timestamp_ms += 1000 / kLowFps;
4315
4316 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4317
4318 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004319 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004320 const int kFrameIntervalMs = 1000 / kHighFps;
4321 max_framerate_ = kHighFps;
4322 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4323 video_source_.IncomingCapturedFrame(
4324 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4325 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4326 // be dropped if the encoder hans't been updated with the new higher target
4327 // framerate yet, causing it to overshoot the target bitrate and then
4328 // suffering the wrath of the media optimizer.
4329 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4330 timestamp_ms += kFrameIntervalMs;
4331 }
4332
4333 // Don expect correct measurement just yet, but it should be higher than
4334 // before.
4335 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4336
mflodmancc3d4422017-08-03 08:27:51 -07004337 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004338}
4339
mflodmancc3d4422017-08-03 08:27:51 -07004340TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004341 const int kFrameWidth = 1280;
4342 const int kFrameHeight = 720;
4343 const int kTargetBitrateBps = 1000000;
4344
4345 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004346 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004347 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004348 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4349 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004350 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004351
4352 // Insert a first video frame, causes another bitrate update.
4353 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4354 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4355 video_source_.IncomingCapturedFrame(
4356 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4357 WaitForEncodedFrame(timestamp_ms);
4358
4359 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004360 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4361 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004362
4363 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004364 timestamp_ms += kProcessIntervalMs;
4365 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004366
4367 // Bitrate observer should not be called.
4368 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4369 video_source_.IncomingCapturedFrame(
4370 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4371 ExpectDroppedFrame();
4372
mflodmancc3d4422017-08-03 08:27:51 -07004373 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004374}
ilnik6b826ef2017-06-16 06:53:48 -07004375
Niels Möller4db138e2018-04-19 09:04:13 +02004376TEST_F(VideoStreamEncoderTest,
4377 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4378 const int kFrameWidth = 1280;
4379 const int kFrameHeight = 720;
4380 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004381 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004382 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4383 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004384 video_source_.IncomingCapturedFrame(
4385 CreateFrame(1, kFrameWidth, kFrameHeight));
4386 WaitForEncodedFrame(1);
4387 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4388 .low_encode_usage_threshold_percent,
4389 default_options.low_encode_usage_threshold_percent);
4390 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4391 .high_encode_usage_threshold_percent,
4392 default_options.high_encode_usage_threshold_percent);
4393 video_stream_encoder_->Stop();
4394}
4395
4396TEST_F(VideoStreamEncoderTest,
4397 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4398 const int kFrameWidth = 1280;
4399 const int kFrameHeight = 720;
4400 CpuOveruseOptions hardware_options;
4401 hardware_options.low_encode_usage_threshold_percent = 150;
4402 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004403 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004404
Erik Språng4c6ca302019-04-08 15:14:01 +02004405 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004406 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4407 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004408 video_source_.IncomingCapturedFrame(
4409 CreateFrame(1, kFrameWidth, kFrameHeight));
4410 WaitForEncodedFrame(1);
4411 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4412 .low_encode_usage_threshold_percent,
4413 hardware_options.low_encode_usage_threshold_percent);
4414 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4415 .high_encode_usage_threshold_percent,
4416 hardware_options.high_encode_usage_threshold_percent);
4417 video_stream_encoder_->Stop();
4418}
4419
Niels Möller6bb5ab92019-01-11 11:11:10 +01004420TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4421 const int kFrameWidth = 320;
4422 const int kFrameHeight = 240;
4423 const int kFps = 30;
4424 const int kTargetBitrateBps = 120000;
4425 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4426
Erik Språng4c6ca302019-04-08 15:14:01 +02004427 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004428 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4429 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004430
4431 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4432 max_framerate_ = kFps;
4433
4434 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4435 fake_encoder_.SimulateOvershoot(1.0);
4436 int num_dropped = 0;
4437 for (int i = 0; i < kNumFramesInRun; ++i) {
4438 video_source_.IncomingCapturedFrame(
4439 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4440 // Wait up to two frame durations for a frame to arrive.
4441 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4442 ++num_dropped;
4443 }
4444 timestamp_ms += 1000 / kFps;
4445 }
4446
Erik Språnga8d48ab2019-02-08 14:17:40 +01004447 // Framerate should be measured to be near the expected target rate.
4448 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4449
4450 // Frame drops should be within 5% of expected 0%.
4451 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004452
4453 // Make encoder produce frames at double the expected bitrate during 3 seconds
4454 // of video, verify number of drops. Rate needs to be slightly changed in
4455 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004456 double overshoot_factor = 2.0;
4457 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4458 // With bitrate adjuster, when need to overshoot even more to trigger
4459 // frame dropping.
4460 overshoot_factor *= 2;
4461 }
4462 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004463 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004464 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004465 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004466 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004467 num_dropped = 0;
4468 for (int i = 0; i < kNumFramesInRun; ++i) {
4469 video_source_.IncomingCapturedFrame(
4470 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4471 // Wait up to two frame durations for a frame to arrive.
4472 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4473 ++num_dropped;
4474 }
4475 timestamp_ms += 1000 / kFps;
4476 }
4477
Erik Språng4c6ca302019-04-08 15:14:01 +02004478 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004479 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4480 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004481
4482 // Target framerate should be still be near the expected target, despite
4483 // the frame drops.
4484 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4485
4486 // Frame drops should be within 5% of expected 50%.
4487 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004488
4489 video_stream_encoder_->Stop();
4490}
4491
4492TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4493 const int kFrameWidth = 320;
4494 const int kFrameHeight = 240;
4495 const int kActualInputFps = 24;
4496 const int kTargetBitrateBps = 120000;
4497
4498 ASSERT_GT(max_framerate_, kActualInputFps);
4499
4500 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4501 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004502 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004503 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4504 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004505
4506 // Insert 3 seconds of video, with an input fps lower than configured max.
4507 for (int i = 0; i < kActualInputFps * 3; ++i) {
4508 video_source_.IncomingCapturedFrame(
4509 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4510 // Wait up to two frame durations for a frame to arrive.
4511 WaitForEncodedFrame(timestamp_ms);
4512 timestamp_ms += 1000 / kActualInputFps;
4513 }
4514
4515 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4516
4517 video_stream_encoder_->Stop();
4518}
4519
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004520TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4521 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004522 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004523 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4524 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004525
4526 fake_encoder_.BlockNextEncode();
4527 video_source_.IncomingCapturedFrame(
4528 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4529 WaitForEncodedFrame(1);
4530 // On the very first frame full update should be forced.
4531 rect = fake_encoder_.GetLastUpdateRect();
4532 EXPECT_EQ(rect.offset_x, 0);
4533 EXPECT_EQ(rect.offset_y, 0);
4534 EXPECT_EQ(rect.height, codec_height_);
4535 EXPECT_EQ(rect.width, codec_width_);
4536 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4537 // call to ContinueEncode.
4538 video_source_.IncomingCapturedFrame(
4539 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4540 ExpectDroppedFrame();
4541 video_source_.IncomingCapturedFrame(
4542 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4543 ExpectDroppedFrame();
4544 fake_encoder_.ContinueEncode();
4545 WaitForEncodedFrame(3);
4546 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4547 rect = fake_encoder_.GetLastUpdateRect();
4548 EXPECT_EQ(rect.offset_x, 1);
4549 EXPECT_EQ(rect.offset_y, 0);
4550 EXPECT_EQ(rect.width, 10);
4551 EXPECT_EQ(rect.height, 1);
4552
4553 video_source_.IncomingCapturedFrame(
4554 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4555 WaitForEncodedFrame(4);
4556 // Previous frame was encoded, so no accumulation should happen.
4557 rect = fake_encoder_.GetLastUpdateRect();
4558 EXPECT_EQ(rect.offset_x, 0);
4559 EXPECT_EQ(rect.offset_y, 0);
4560 EXPECT_EQ(rect.width, 1);
4561 EXPECT_EQ(rect.height, 1);
4562
4563 video_stream_encoder_->Stop();
4564}
4565
Erik Språngd7329ca2019-02-21 21:19:53 +01004566TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004567 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004568 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4569 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004570
4571 // First frame is always keyframe.
4572 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4573 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004574 EXPECT_THAT(
4575 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004576 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004577
4578 // Insert delta frame.
4579 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4580 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004581 EXPECT_THAT(
4582 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004583 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004584
4585 // Request next frame be a key-frame.
4586 video_stream_encoder_->SendKeyFrame();
4587 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4588 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004589 EXPECT_THAT(
4590 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004591 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004592
4593 video_stream_encoder_->Stop();
4594}
4595
4596TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4597 // Setup simulcast with three streams.
4598 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004599 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004600 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004601 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004602 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004603 // Wait for all three layers before triggering event.
4604 sink_.SetNumExpectedLayers(3);
4605
4606 // First frame is always keyframe.
4607 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4608 WaitForEncodedFrame(1);
4609 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004610 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4611 VideoFrameType::kVideoFrameKey,
4612 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004613
4614 // Insert delta frame.
4615 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4616 WaitForEncodedFrame(2);
4617 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004618 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4619 VideoFrameType::kVideoFrameDelta,
4620 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004621
4622 // Request next frame be a key-frame.
4623 // Only first stream is configured to produce key-frame.
4624 video_stream_encoder_->SendKeyFrame();
4625 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4626 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004627
4628 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4629 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004630 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004631 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004632 VideoFrameType::kVideoFrameKey,
4633 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004634
4635 video_stream_encoder_->Stop();
4636}
4637
4638TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4639 // Configure internal source factory and setup test again.
4640 encoder_factory_.SetHasInternalSource(true);
4641 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004642 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004643 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4644 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004645
4646 // Call encoder directly, simulating internal source where encoded frame
4647 // callback in VideoStreamEncoder is called despite no OnFrame().
4648 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4649 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004650 EXPECT_THAT(
4651 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004652 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004653
Niels Möller8f7ce222019-03-21 15:43:58 +01004654 const std::vector<VideoFrameType> kDeltaFrame = {
4655 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004656 // Need to set timestamp manually since manually for injected frame.
4657 VideoFrame frame = CreateFrame(101, nullptr);
4658 frame.set_timestamp(101);
4659 fake_encoder_.InjectFrame(frame, false);
4660 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004661 EXPECT_THAT(
4662 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004663 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004664
4665 // Request key-frame. The forces a dummy frame down into the encoder.
4666 fake_encoder_.ExpectNullFrame();
4667 video_stream_encoder_->SendKeyFrame();
4668 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004669 EXPECT_THAT(
4670 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004671 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004672
4673 video_stream_encoder_->Stop();
4674}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004675
4676TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4677 // Configure internal source factory and setup test again.
4678 encoder_factory_.SetHasInternalSource(true);
4679 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004680 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004681 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4682 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004683
4684 int64_t timestamp = 1;
4685 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004686 image.SetEncodedData(
4687 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004688 image.capture_time_ms_ = ++timestamp;
4689 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4690 const int64_t kEncodeFinishDelayMs = 10;
4691 image.timing_.encode_start_ms = timestamp;
4692 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4693 fake_encoder_.InjectEncodedImage(image);
4694 // Wait for frame without incrementing clock.
4695 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4696 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4697 // capture timestamp should be kEncodeFinishDelayMs in the past.
4698 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4699 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4700 kEncodeFinishDelayMs);
4701
4702 video_stream_encoder_->Stop();
4703}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004704
4705TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4706 // Configure internal source factory and setup test again.
4707 encoder_factory_.SetHasInternalSource(true);
4708 ResetEncoder("H264", 1, 1, 1, false);
4709
4710 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4711 image._frameType = VideoFrameType::kVideoFrameKey;
4712
4713 CodecSpecificInfo codec_specific_info;
4714 codec_specific_info.codecType = kVideoCodecH264;
4715
4716 RTPFragmentationHeader fragmentation;
4717 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4718 fragmentation.fragmentationOffset[0] = 4;
4719 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4720
4721 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4722 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4723
4724 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4725 testing::ElementsAreArray(optimal_sps));
4726 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4727 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4728 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4729 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4730
4731 video_stream_encoder_->Stop();
4732}
4733
4734TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4735 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4736 0x00, 0x00, 0x03, 0x03, 0xF4,
4737 0x05, 0x03, 0xC7, 0xC0};
4738
4739 // Configure internal source factory and setup test again.
4740 encoder_factory_.SetHasInternalSource(true);
4741 ResetEncoder("H264", 1, 1, 1, false);
4742
4743 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4744 image._frameType = VideoFrameType::kVideoFrameKey;
4745
4746 CodecSpecificInfo codec_specific_info;
4747 codec_specific_info.codecType = kVideoCodecH264;
4748
4749 RTPFragmentationHeader fragmentation;
4750 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4751 fragmentation.fragmentationOffset[0] = 4;
4752 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4753
4754 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4755 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4756
4757 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4758 testing::ElementsAreArray(optimal_sps));
4759 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4760 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4761 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4762 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4763
4764 video_stream_encoder_->Stop();
4765}
4766
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004767TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4768 const int kFrameWidth = 1280;
4769 const int kFrameHeight = 720;
4770 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4771
4772 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004773 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4774 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004775 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4776
4777 // Insert a first video frame. It should be dropped because of downscale in
4778 // resolution.
4779 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4780 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4781 frame.set_rotation(kVideoRotation_270);
4782 video_source_.IncomingCapturedFrame(frame);
4783
4784 ExpectDroppedFrame();
4785
4786 // Second frame is downscaled.
4787 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4788 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4789 frame.set_rotation(kVideoRotation_90);
4790 video_source_.IncomingCapturedFrame(frame);
4791
4792 WaitForEncodedFrame(timestamp_ms);
4793 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4794
4795 // Insert another frame, also downscaled.
4796 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4797 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4798 frame.set_rotation(kVideoRotation_180);
4799 video_source_.IncomingCapturedFrame(frame);
4800
4801 WaitForEncodedFrame(timestamp_ms);
4802 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4803
4804 video_stream_encoder_->Stop();
4805}
4806
Erik Språng5056af02019-09-02 15:53:11 +02004807TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4808 const int kFrameWidth = 320;
4809 const int kFrameHeight = 180;
4810
4811 // Initial rate.
4812 video_stream_encoder_->OnBitrateUpdated(
4813 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004814 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004815 /*link_allocation=*/DataRate::kbps(300),
4816 /*fraction_lost=*/0,
4817 /*rtt_ms=*/0);
4818
4819 // Insert a first video frame so that encoder gets configured.
4820 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4821 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4822 frame.set_rotation(kVideoRotation_270);
4823 video_source_.IncomingCapturedFrame(frame);
4824 WaitForEncodedFrame(timestamp_ms);
4825
4826 // Set a target rate below the minimum allowed by the codec settings.
4827 VideoCodec codec_config = fake_encoder_.codec_config();
4828 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4829 DataRate target_rate = min_rate - DataRate::kbps(1);
4830 video_stream_encoder_->OnBitrateUpdated(
4831 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004832 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004833 /*link_allocation=*/target_rate,
4834 /*fraction_lost=*/0,
4835 /*rtt_ms=*/0);
4836 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4837
4838 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4839 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4840 ASSERT_TRUE(rate_settings.has_value());
4841 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
4842 EXPECT_EQ(min_rate, allocation_sum);
4843 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
4844
4845 video_stream_encoder_->Stop();
4846}
4847
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02004848TEST_F(VideoStreamEncoderTest, EncoderRatesPropegatedOnReconfigure) {
4849 video_stream_encoder_->OnBitrateUpdated(
4850 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4851 DataRate::bps(kTargetBitrateBps), 0, 0);
4852 // Capture a frame and wait for it to synchronize with the encoder thread.
4853 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4854 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4855 WaitForEncodedFrame(1);
4856
4857 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4858 ASSERT_TRUE(prev_rate_settings.has_value());
4859 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
4860 kDefaultFramerate);
4861
4862 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
4863 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
4864 timestamp_ms += 1000 / kDefaultFramerate;
4865 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4866 WaitForEncodedFrame(timestamp_ms);
4867 }
4868 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
4869 kDefaultFramerate);
4870 // Capture larger frame to trigger a reconfigure.
4871 codec_height_ *= 2;
4872 codec_width_ *= 2;
4873 timestamp_ms += 1000 / kDefaultFramerate;
4874 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4875 WaitForEncodedFrame(timestamp_ms);
4876
4877 EXPECT_EQ(2, sink_.number_of_reconfigurations());
4878 auto current_rate_settings =
4879 fake_encoder_.GetAndResetLastRateControlSettings();
4880 // Ensure we have actually reconfigured twice
4881 // The rate settings should have been set again even though
4882 // they haven't changed.
4883 ASSERT_TRUE(current_rate_settings.has_value());
4884 EXPECT_EQ(prev_rate_settings->bitrate, current_rate_settings->bitrate);
4885 EXPECT_EQ(prev_rate_settings->framerate_fps,
4886 current_rate_settings->framerate_fps);
4887 EXPECT_EQ(prev_rate_settings->bandwidth_allocation,
4888 current_rate_settings->bandwidth_allocation);
4889
4890 video_stream_encoder_->Stop();
4891}
4892
philipeld9cc8c02019-09-16 14:53:40 +02004893struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
4894 MOCK_METHOD0(RequestEncoderFallback, void());
4895 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
4896};
4897
4898TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
4899 constexpr int kDontCare = 100;
4900
4901 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4902 video_send_config_.encoder_settings.encoder_switch_request_callback =
4903 &switch_callback;
4904 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4905 encoder_config.codec_type = kVideoCodecVP8;
4906 webrtc::test::ScopedFieldTrials field_trial(
4907 "WebRTC-NetworkCondition-EncoderSwitch/"
4908 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
4909 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4910
4911 // Reset encoder for new configuration to take effect.
4912 ConfigureEncoder(std::move(encoder_config));
4913
4914 // Send one frame to trigger ReconfigureEncoder.
4915 video_source_.IncomingCapturedFrame(
4916 CreateFrame(kDontCare, kDontCare, kDontCare));
4917
4918 using Config = EncoderSwitchRequestCallback::Config;
4919 EXPECT_CALL(switch_callback,
4920 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4921 Field(&Config::param, "ping"),
4922 Field(&Config::value, "pong"))));
4923
4924 video_stream_encoder_->OnBitrateUpdated(
4925 /*target_bitrate=*/DataRate::kbps(50),
4926 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
4927 /*link_allocation=*/DataRate::kbps(kDontCare),
4928 /*fraction_lost=*/0,
4929 /*rtt_ms=*/0);
4930
4931 video_stream_encoder_->Stop();
4932}
4933
4934TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
4935 constexpr int kSufficientBitrateToNotDrop = 1000;
4936 constexpr int kHighRes = 500;
4937 constexpr int kLowRes = 100;
4938
4939 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4940 video_send_config_.encoder_settings.encoder_switch_request_callback =
4941 &switch_callback;
4942 webrtc::test::ScopedFieldTrials field_trial(
4943 "WebRTC-NetworkCondition-EncoderSwitch/"
4944 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
4945 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4946 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4947 encoder_config.codec_type = kVideoCodecH264;
4948
4949 // Reset encoder for new configuration to take effect.
4950 ConfigureEncoder(std::move(encoder_config));
4951
4952 // The VideoStreamEncoder needs some bitrate before it can start encoding,
4953 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
4954 // not fail.
4955 video_stream_encoder_->OnBitrateUpdated(
4956 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4957 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4958 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4959 /*fraction_lost=*/0,
4960 /*rtt_ms=*/0);
4961
4962 // Send one frame to trigger ReconfigureEncoder.
4963 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
4964 WaitForEncodedFrame(1);
4965
4966 using Config = EncoderSwitchRequestCallback::Config;
4967 EXPECT_CALL(switch_callback,
4968 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4969 Field(&Config::param, "ping"),
4970 Field(&Config::value, "pong"))));
4971
4972 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
4973 WaitForEncodedFrame(2);
4974
4975 video_stream_encoder_->Stop();
4976}
4977
perkj26091b12016-09-01 01:17:40 -07004978} // namespace webrtc