blob: a572506875c6bf224a4547dd6d5a046fe4a3baab [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());
2669
2670 video_stream_encoder_->OnBitrateUpdated(
2671 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2672 DataRate::bps(kTargetBitrateBps), 0, 0);
2673
2674 // Enable BALANCED preference.
2675 source_.set_adaptation_enabled(true);
2676 video_stream_encoder_->SetSource(&source_,
2677 webrtc::DegradationPreference::BALANCED);
2678 }
2679
2680 void IncomingCapturedFrame() {
2681 timestamp_ms_ += kFrameIntervalMs;
2682 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
2683 sink_.WaitForEncodedFrame(timestamp_ms_);
2684 }
2685
2686 const int kWidth = 640; // pixels:640x360=230400
2687 const int kHeight = 360;
2688 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2689 int64_t timestamp_ms_ = 0;
2690 AdaptingFrameForwarder source_;
2691};
2692
2693TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2694 test::ScopedFieldTrials field_trials(
2695 "WebRTC-Video-BalancedDegradationSettings/"
2696 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2697 SetupTest();
2698
2699 // Force input frame rate.
2700 const int kInputFps = 24;
2701 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2702 stats.input_frame_rate = kInputFps;
2703 stats_proxy_->SetMockStats(stats);
2704
2705 IncomingCapturedFrame();
2706 VerifyFpsMaxResolutionMax(source_.sink_wants());
2707
2708 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2709 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2710 video_stream_encoder_->TriggerQualityLowExpectFalse();
2711 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2712
2713 video_stream_encoder_->Stop();
2714}
2715
2716TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2717 test::ScopedFieldTrials field_trials(
2718 "WebRTC-Video-BalancedDegradationSettings/"
2719 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2720 SetupTest();
2721
2722 // Force input frame rate.
2723 const int kInputFps = 25;
2724 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2725 stats.input_frame_rate = kInputFps;
2726 stats_proxy_->SetMockStats(stats);
2727
2728 IncomingCapturedFrame();
2729 VerifyFpsMaxResolutionMax(source_.sink_wants());
2730
2731 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2732 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2733 video_stream_encoder_->TriggerQualityLow();
2734 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2735
2736 video_stream_encoder_->Stop();
2737}
2738
2739TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2740 test::ScopedFieldTrials field_trials(
2741 "WebRTC-Video-BalancedDegradationSettings/"
2742 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2743 SetupTest();
2744
2745 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2746
2747 IncomingCapturedFrame();
2748 VerifyFpsMaxResolutionMax(source_.sink_wants());
2749
2750 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2751 video_stream_encoder_->TriggerQualityLow();
2752 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2753
2754 video_stream_encoder_->Stop();
2755}
2756
Åsa Persson1b247f12019-08-14 17:26:39 +02002757TEST_F(VideoStreamEncoderTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002758 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002759 "WebRTC-Video-BalancedDegradationSettings/"
2760 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
2761 // Reset encoder for field trials to take effect.
2762 ConfigureEncoder(video_encoder_config_.Copy());
2763
2764 const int kWidth = 640; // pixels:640x360=230400
2765 const int kHeight = 360;
2766 const int64_t kFrameIntervalMs = 150;
2767 const int kMinBitrateBps = 425000;
2768 const int kTooLowMinBitrateBps = 424000;
Florent Castellia8336d32019-09-09 13:36:55 +02002769 video_stream_encoder_->OnBitrateUpdated(
2770 DataRate::bps(kTooLowMinBitrateBps), DataRate::bps(kTooLowMinBitrateBps),
2771 DataRate::bps(kTooLowMinBitrateBps), 0, 0);
Åsa Persson1b247f12019-08-14 17:26:39 +02002772
2773 // Enable BALANCED preference, no initial limitation.
2774 AdaptingFrameForwarder source;
2775 source.set_adaptation_enabled(true);
2776 video_stream_encoder_->SetSource(&source,
2777 webrtc::DegradationPreference::BALANCED);
2778
2779 int64_t timestamp_ms = kFrameIntervalMs;
2780 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2781 sink_.WaitForEncodedFrame(kWidth, kHeight);
2782 VerifyFpsMaxResolutionMax(source.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002783 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2784
2785 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2786 video_stream_encoder_->TriggerQualityLow();
2787 timestamp_ms += kFrameIntervalMs;
2788 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2789 sink_.WaitForEncodedFrame(timestamp_ms);
2790 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002791 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2792
2793 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2794 video_stream_encoder_->TriggerQualityLow();
2795 timestamp_ms += kFrameIntervalMs;
2796 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2797 sink_.WaitForEncodedFrame(timestamp_ms);
2798 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002799 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2800
Åsa Persson30ab0152019-08-27 12:22:33 +02002801 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2802 video_stream_encoder_->TriggerQualityLow();
2803 timestamp_ms += kFrameIntervalMs;
2804 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2805 sink_.WaitForEncodedFrame(timestamp_ms);
2806 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
2807 EXPECT_EQ(source.sink_wants().max_framerate_fps, 10);
2808 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2809
2810 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002811 video_stream_encoder_->TriggerQualityHigh();
2812 timestamp_ms += kFrameIntervalMs;
2813 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2814 sink_.WaitForEncodedFrame(timestamp_ms);
Åsa Persson30ab0152019-08-27 12:22:33 +02002815 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002816
Åsa Persson30ab0152019-08-27 12:22:33 +02002817 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002818 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002819 DataRate::bps(kMinBitrateBps),
Åsa Persson1b247f12019-08-14 17:26:39 +02002820 DataRate::bps(kMinBitrateBps), 0, 0);
2821 video_stream_encoder_->TriggerQualityHigh();
2822 timestamp_ms += kFrameIntervalMs;
2823 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2824 sink_.WaitForEncodedFrame(timestamp_ms);
Åsa Persson30ab0152019-08-27 12:22:33 +02002825 EXPECT_EQ(source.sink_wants().max_framerate_fps, 14);
2826 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2827
2828 video_stream_encoder_->Stop();
2829}
2830
2831TEST_F(VideoStreamEncoderTest,
2832 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002833 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002834 "WebRTC-Video-BalancedDegradationSettings/"
2835 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
2836 // Reset encoder for field trials to take effect.
2837 ConfigureEncoder(video_encoder_config_.Copy());
2838
2839 const int kWidth = 640; // pixels:640x360=230400
2840 const int kHeight = 360;
2841 const int64_t kFrameIntervalMs = 150;
2842 const int kResolutionMinBitrateBps = 435000;
2843 const int kTooLowMinResolutionBitrateBps = 434000;
2844 video_stream_encoder_->OnBitrateUpdated(
2845 DataRate::bps(kTooLowMinResolutionBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002846 DataRate::bps(kTooLowMinResolutionBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002847 DataRate::bps(kTooLowMinResolutionBitrateBps), 0, 0);
2848
2849 // Enable BALANCED preference, no initial limitation.
2850 AdaptingFrameForwarder source;
2851 source.set_adaptation_enabled(true);
2852 video_stream_encoder_->SetSource(&source,
2853 webrtc::DegradationPreference::BALANCED);
2854
2855 int64_t timestamp_ms = kFrameIntervalMs;
2856 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2857 sink_.WaitForEncodedFrame(kWidth, kHeight);
2858 VerifyFpsMaxResolutionMax(source.sink_wants());
2859 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2860
2861 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2862 video_stream_encoder_->TriggerQualityLow();
2863 timestamp_ms += kFrameIntervalMs;
2864 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2865 sink_.WaitForEncodedFrame(timestamp_ms);
2866 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
2867 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2868
2869 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2870 video_stream_encoder_->TriggerQualityLow();
2871 timestamp_ms += kFrameIntervalMs;
2872 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2873 sink_.WaitForEncodedFrame(timestamp_ms);
2874 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
2875 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2876
2877 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2878 video_stream_encoder_->TriggerQualityLow();
2879 timestamp_ms += kFrameIntervalMs;
2880 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2881 sink_.WaitForEncodedFrame(timestamp_ms);
2882 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002883 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2884
Åsa Persson30ab0152019-08-27 12:22:33 +02002885 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2886 video_stream_encoder_->TriggerQualityHigh();
2887 timestamp_ms += kFrameIntervalMs;
2888 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2889 sink_.WaitForEncodedFrame(timestamp_ms);
2890 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
2891 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2892
2893 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2894 video_stream_encoder_->TriggerQualityHigh();
2895 timestamp_ms += kFrameIntervalMs;
2896 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2897 sink_.WaitForEncodedFrame(timestamp_ms);
2898 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2899
2900 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
2901 video_stream_encoder_->OnBitrateUpdated(
2902 DataRate::bps(kResolutionMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002903 DataRate::bps(kResolutionMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002904 DataRate::bps(kResolutionMinBitrateBps), 0, 0);
2905 video_stream_encoder_->TriggerQualityHigh();
2906 timestamp_ms += kFrameIntervalMs;
2907 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2908 sink_.WaitForEncodedFrame(timestamp_ms);
2909 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
2910 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2911
2912 video_stream_encoder_->Stop();
2913}
2914
2915TEST_F(VideoStreamEncoderTest,
2916 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002917 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002918 "WebRTC-Video-BalancedDegradationSettings/"
2919 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
2920 // Reset encoder for field trials to take effect.
2921 ConfigureEncoder(video_encoder_config_.Copy());
2922
2923 const int kWidth = 640; // pixels:640x360=230400
2924 const int kHeight = 360;
2925 const int64_t kFrameIntervalMs = 150;
2926 const int kMinBitrateBps = 425000;
2927 const int kTooLowMinBitrateBps = 424000;
2928 const int kResolutionMinBitrateBps = 435000;
2929 const int kTooLowMinResolutionBitrateBps = 434000;
Florent Castellia8336d32019-09-09 13:36:55 +02002930 video_stream_encoder_->OnBitrateUpdated(
2931 DataRate::bps(kTooLowMinBitrateBps), DataRate::bps(kTooLowMinBitrateBps),
2932 DataRate::bps(kTooLowMinBitrateBps), 0, 0);
Åsa Persson30ab0152019-08-27 12:22:33 +02002933
2934 // Enable BALANCED preference, no initial limitation.
2935 AdaptingFrameForwarder source;
2936 source.set_adaptation_enabled(true);
2937 video_stream_encoder_->SetSource(&source,
2938 webrtc::DegradationPreference::BALANCED);
2939
2940 int64_t timestamp_ms = kFrameIntervalMs;
2941 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2942 sink_.WaitForEncodedFrame(kWidth, kHeight);
2943 VerifyFpsMaxResolutionMax(source.sink_wants());
2944 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2945
2946 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2947 video_stream_encoder_->TriggerQualityLow();
2948 timestamp_ms += kFrameIntervalMs;
2949 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2950 sink_.WaitForEncodedFrame(timestamp_ms);
2951 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
2952 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2953
2954 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2955 video_stream_encoder_->TriggerQualityLow();
2956 timestamp_ms += kFrameIntervalMs;
2957 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2958 sink_.WaitForEncodedFrame(timestamp_ms);
2959 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
2960 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2961
2962 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2963 video_stream_encoder_->TriggerQualityLow();
2964 timestamp_ms += kFrameIntervalMs;
2965 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2966 sink_.WaitForEncodedFrame(timestamp_ms);
2967 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
2968 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2969
2970 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
2971 video_stream_encoder_->TriggerQualityHigh();
2972 timestamp_ms += kFrameIntervalMs;
2973 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2974 sink_.WaitForEncodedFrame(timestamp_ms);
2975 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2976
2977 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
2978 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002979 DataRate::bps(kMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002980 DataRate::bps(kMinBitrateBps), 0, 0);
2981 video_stream_encoder_->TriggerQualityHigh();
2982 timestamp_ms += kFrameIntervalMs;
2983 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2984 sink_.WaitForEncodedFrame(timestamp_ms);
2985 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
2986 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2987
2988 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2989 video_stream_encoder_->OnBitrateUpdated(
2990 DataRate::bps(kTooLowMinResolutionBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002991 DataRate::bps(kTooLowMinResolutionBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002992 DataRate::bps(kTooLowMinResolutionBitrateBps), 0, 0);
2993 video_stream_encoder_->TriggerQualityHigh();
2994 timestamp_ms += kFrameIntervalMs;
2995 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2996 sink_.WaitForEncodedFrame(timestamp_ms);
2997 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2998
2999 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
3000 video_stream_encoder_->OnBitrateUpdated(
3001 DataRate::bps(kResolutionMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003002 DataRate::bps(kResolutionMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02003003 DataRate::bps(kResolutionMinBitrateBps), 0, 0);
3004 video_stream_encoder_->TriggerQualityHigh();
3005 timestamp_ms += kFrameIntervalMs;
3006 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3007 sink_.WaitForEncodedFrame(timestamp_ms);
3008 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
3009 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3010
Åsa Persson1b247f12019-08-14 17:26:39 +02003011 video_stream_encoder_->Stop();
3012}
3013
mflodmancc3d4422017-08-03 08:27:51 -07003014TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003015 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
3016 const int kWidth = 1280;
3017 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02003018 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003019 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3020 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003021
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003022 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003023 AdaptingFrameForwarder source;
3024 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07003025 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003026 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003027
Åsa Persson8c1bf952018-09-13 10:42:19 +02003028 int64_t timestamp_ms = kFrameIntervalMs;
3029 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003030 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003031 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003032 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3033 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3034 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3035 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3036
3037 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003038 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003039 timestamp_ms += kFrameIntervalMs;
3040 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3041 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003042 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07003043 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3044 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3045 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3046 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3047
3048 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003049 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003050 timestamp_ms += kFrameIntervalMs;
3051 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3052 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003053 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003054 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3055 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3056 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3057 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3058
Jonathan Yubc771b72017-12-08 17:04:29 -08003059 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003060 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003061 timestamp_ms += kFrameIntervalMs;
3062 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3063 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003064 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003065 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3066 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003067 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003068 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3069
Jonathan Yubc771b72017-12-08 17:04:29 -08003070 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003071 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003072 timestamp_ms += kFrameIntervalMs;
3073 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3074 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003075 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003076 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003077 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3078 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3079 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3080 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3081
Jonathan Yubc771b72017-12-08 17:04:29 -08003082 // Trigger quality adapt down, expect no change (min resolution reached).
3083 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003084 timestamp_ms += kFrameIntervalMs;
3085 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3086 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003087 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3088 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3089 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3090 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3091 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3092
3093 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003094 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003095 timestamp_ms += kFrameIntervalMs;
3096 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3097 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003098 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003099 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3100 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3101 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3102 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3103
3104 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3105 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003106 timestamp_ms += kFrameIntervalMs;
3107 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3108 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003109 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3110 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3111 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3112 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3113 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3114
3115 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3116 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003117 timestamp_ms += kFrameIntervalMs;
3118 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3119 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003120 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003121 last_wants = source.sink_wants();
3122 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3123 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003124 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003125 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3126
3127 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003128 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003129 timestamp_ms += kFrameIntervalMs;
3130 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3131 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003132 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003133 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3134 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003135 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003136 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3137
3138 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003139 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003140 timestamp_ms += kFrameIntervalMs;
3141 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003142 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003143 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003144 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003145 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3146 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003147 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003148 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003149
mflodmancc3d4422017-08-03 08:27:51 -07003150 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003151}
3152
mflodmancc3d4422017-08-03 08:27:51 -07003153TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003154 const int kWidth = 640;
3155 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003156
Erik Språng4c6ca302019-04-08 15:14:01 +02003157 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003158 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3159 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003160
perkj803d97f2016-11-01 11:45:46 -07003161 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003162 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003163 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003164 }
3165
mflodmancc3d4422017-08-03 08:27:51 -07003166 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003167 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003168 video_source_.IncomingCapturedFrame(CreateFrame(
3169 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003170 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003171 }
3172
mflodmancc3d4422017-08-03 08:27:51 -07003173 video_stream_encoder_->Stop();
3174 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003175 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003176
perkj803d97f2016-11-01 11:45:46 -07003177 EXPECT_EQ(1,
3178 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3179 EXPECT_EQ(
3180 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3181}
3182
mflodmancc3d4422017-08-03 08:27:51 -07003183TEST_F(VideoStreamEncoderTest,
3184 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003185 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003186 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3187 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003188 const int kWidth = 640;
3189 const int kHeight = 360;
3190
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003191 video_stream_encoder_->SetSource(&video_source_,
3192 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003193
3194 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3195 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003196 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003197 }
3198
mflodmancc3d4422017-08-03 08:27:51 -07003199 video_stream_encoder_->Stop();
3200 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003201 stats_proxy_.reset();
3202
3203 EXPECT_EQ(0,
3204 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3205}
3206
mflodmancc3d4422017-08-03 08:27:51 -07003207TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003208 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003209 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003210
3211 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003212 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003213 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003214 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3215 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003216
sprang57c2fff2017-01-16 06:24:02 -08003217 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003218 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003219 video_stream_encoder_->OnBitrateUpdated(
3220 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3221 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003222
sprang57c2fff2017-01-16 06:24:02 -08003223 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003224 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3225 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003226 VideoBitrateAllocation bitrate_allocation =
3227 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003228 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003229 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003230 // TODO(srte): The use of millisecs here looks like an error, but the tests
3231 // fails using seconds, this should be investigated.
3232 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003233
3234 // Not called on second frame.
3235 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3236 .Times(0);
3237 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003238 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3239 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003240 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003241
3242 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003243 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3244 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003245 const int64_t start_time_ms = rtc::TimeMillis();
3246 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3247 video_source_.IncomingCapturedFrame(
3248 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3249 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003250 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003251 }
3252
3253 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003254 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003255
mflodmancc3d4422017-08-03 08:27:51 -07003256 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003257}
3258
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003259TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3260 // 2 TLs configured, temporal layers supported by encoder.
3261 const int kNumTemporalLayers = 2;
3262 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3263 fake_encoder_.SetTemporalLayersSupported(0, true);
3264
3265 // Bitrate allocated across temporal layers.
3266 const int kTl0Bps = kTargetBitrateBps *
3267 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3268 kNumTemporalLayers, /*temporal_id*/ 0);
3269 const int kTl1Bps = kTargetBitrateBps *
3270 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3271 kNumTemporalLayers, /*temporal_id*/ 1);
3272 VideoBitrateAllocation expected_bitrate;
3273 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3274 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3275
3276 VerifyAllocatedBitrate(expected_bitrate);
3277 video_stream_encoder_->Stop();
3278}
3279
3280TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3281 // 2 TLs configured, temporal layers not supported by encoder.
3282 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3283 fake_encoder_.SetTemporalLayersSupported(0, false);
3284
3285 // Temporal layers not supported by the encoder.
3286 // Total bitrate should be at ti:0.
3287 VideoBitrateAllocation expected_bitrate;
3288 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3289
3290 VerifyAllocatedBitrate(expected_bitrate);
3291 video_stream_encoder_->Stop();
3292}
3293
3294TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3295 // 2 TLs configured, temporal layers only supported for first stream.
3296 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3297 fake_encoder_.SetTemporalLayersSupported(0, true);
3298 fake_encoder_.SetTemporalLayersSupported(1, false);
3299
3300 const int kS0Bps = 150000;
3301 const int kS0Tl0Bps =
3302 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3303 /*num_layers*/ 2, /*temporal_id*/ 0);
3304 const int kS0Tl1Bps =
3305 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3306 /*num_layers*/ 2, /*temporal_id*/ 1);
3307 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3308 // Temporal layers not supported by si:1.
3309 VideoBitrateAllocation expected_bitrate;
3310 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3311 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3312 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3313
3314 VerifyAllocatedBitrate(expected_bitrate);
3315 video_stream_encoder_->Stop();
3316}
3317
Niels Möller7dc26b72017-12-06 10:27:48 +01003318TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3319 const int kFrameWidth = 1280;
3320 const int kFrameHeight = 720;
3321 const int kFramerate = 24;
3322
Erik Språng4c6ca302019-04-08 15:14:01 +02003323 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003324 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3325 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003326 test::FrameForwarder source;
3327 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003328 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003329
3330 // Insert a single frame, triggering initial configuration.
3331 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3332 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3333
3334 EXPECT_EQ(
3335 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3336 kDefaultFramerate);
3337
3338 // Trigger reconfigure encoder (without resetting the entire instance).
3339 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003340 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003341 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3342 video_encoder_config.number_of_streams = 1;
3343 video_encoder_config.video_stream_factory =
3344 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
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 // Detector should be updated with fps limit from codec config.
3350 EXPECT_EQ(
3351 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3352 kFramerate);
3353
3354 // Trigger overuse, max framerate should be reduced.
3355 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3356 stats.input_frame_rate = kFramerate;
3357 stats_proxy_->SetMockStats(stats);
3358 video_stream_encoder_->TriggerCpuOveruse();
3359 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3360 int adapted_framerate =
3361 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3362 EXPECT_LT(adapted_framerate, kFramerate);
3363
3364 // Trigger underuse, max framerate should go back to codec configured fps.
3365 // Set extra low fps, to make sure it's actually reset, not just incremented.
3366 stats = stats_proxy_->GetStats();
3367 stats.input_frame_rate = adapted_framerate / 2;
3368 stats_proxy_->SetMockStats(stats);
3369 video_stream_encoder_->TriggerCpuNormalUsage();
3370 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3371 EXPECT_EQ(
3372 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3373 kFramerate);
3374
3375 video_stream_encoder_->Stop();
3376}
3377
3378TEST_F(VideoStreamEncoderTest,
3379 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3380 const int kFrameWidth = 1280;
3381 const int kFrameHeight = 720;
3382 const int kLowFramerate = 15;
3383 const int kHighFramerate = 25;
3384
Erik Språng4c6ca302019-04-08 15:14:01 +02003385 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003386 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3387 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003388 test::FrameForwarder source;
3389 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003390 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003391
3392 // Trigger initial configuration.
3393 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003394 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003395 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3396 video_encoder_config.number_of_streams = 1;
3397 video_encoder_config.video_stream_factory =
3398 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3399 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3400 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003401 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003402 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3403
3404 EXPECT_EQ(
3405 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3406 kLowFramerate);
3407
3408 // Trigger overuse, max framerate should be reduced.
3409 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3410 stats.input_frame_rate = kLowFramerate;
3411 stats_proxy_->SetMockStats(stats);
3412 video_stream_encoder_->TriggerCpuOveruse();
3413 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3414 int adapted_framerate =
3415 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3416 EXPECT_LT(adapted_framerate, kLowFramerate);
3417
3418 // Reconfigure the encoder with a new (higher max framerate), max fps should
3419 // still respect the adaptation.
3420 video_encoder_config.video_stream_factory =
3421 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3422 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3423 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003424 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003425 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3426
3427 EXPECT_EQ(
3428 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3429 adapted_framerate);
3430
3431 // Trigger underuse, max framerate should go back to codec configured fps.
3432 stats = stats_proxy_->GetStats();
3433 stats.input_frame_rate = adapted_framerate;
3434 stats_proxy_->SetMockStats(stats);
3435 video_stream_encoder_->TriggerCpuNormalUsage();
3436 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3437 EXPECT_EQ(
3438 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3439 kHighFramerate);
3440
3441 video_stream_encoder_->Stop();
3442}
3443
mflodmancc3d4422017-08-03 08:27:51 -07003444TEST_F(VideoStreamEncoderTest,
3445 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003446 const int kFrameWidth = 1280;
3447 const int kFrameHeight = 720;
3448 const int kFramerate = 24;
3449
Erik Språng4c6ca302019-04-08 15:14:01 +02003450 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003451 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3452 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003453 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003454 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003455 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003456
3457 // Trigger initial configuration.
3458 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003459 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003460 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3461 video_encoder_config.number_of_streams = 1;
3462 video_encoder_config.video_stream_factory =
3463 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3464 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003465 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003466 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003467 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003468
Niels Möller7dc26b72017-12-06 10:27:48 +01003469 EXPECT_EQ(
3470 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3471 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003472
3473 // Trigger overuse, max framerate should be reduced.
3474 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3475 stats.input_frame_rate = kFramerate;
3476 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003477 video_stream_encoder_->TriggerCpuOveruse();
3478 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003479 int adapted_framerate =
3480 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003481 EXPECT_LT(adapted_framerate, kFramerate);
3482
3483 // Change degradation preference to not enable framerate scaling. Target
3484 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003485 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003486 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003487 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003488 EXPECT_EQ(
3489 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3490 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003491
mflodmancc3d4422017-08-03 08:27:51 -07003492 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003493}
3494
mflodmancc3d4422017-08-03 08:27:51 -07003495TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003496 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003497 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003498 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003499 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003500 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003501 const int kWidth = 640;
3502 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003503
asaperssonfab67072017-04-04 05:51:49 -07003504 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003505
3506 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003507 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003508
3509 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003510 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003511
sprangc5d62e22017-04-02 23:53:04 -07003512 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003513
asaperssonfab67072017-04-04 05:51:49 -07003514 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003515 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003516 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003517
3518 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003519 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003520
sprangc5d62e22017-04-02 23:53:04 -07003521 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003522
mflodmancc3d4422017-08-03 08:27:51 -07003523 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003524}
3525
mflodmancc3d4422017-08-03 08:27:51 -07003526TEST_F(VideoStreamEncoderTest,
3527 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003528 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003529 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003530 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003531 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003532 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003533 const int kWidth = 640;
3534 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003535
3536 // We expect the n initial frames to get dropped.
3537 int i;
3538 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003539 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003540 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003541 }
3542 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003543 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003544 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003545
3546 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003547 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003548
mflodmancc3d4422017-08-03 08:27:51 -07003549 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003550}
3551
mflodmancc3d4422017-08-03 08:27:51 -07003552TEST_F(VideoStreamEncoderTest,
3553 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003554 const int kWidth = 640;
3555 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003556 video_stream_encoder_->OnBitrateUpdated(
3557 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3558 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003559
3560 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003561 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003562 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003563
asaperssonfab67072017-04-04 05:51:49 -07003564 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003565 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003566 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003567
mflodmancc3d4422017-08-03 08:27:51 -07003568 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003569}
3570
mflodmancc3d4422017-08-03 08:27:51 -07003571TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003572 const int kWidth = 640;
3573 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003574 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003575
3576 VideoEncoderConfig video_encoder_config;
3577 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3578 // Make format different, to force recreation of encoder.
3579 video_encoder_config.video_format.parameters["foo"] = "foo";
3580 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003581 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003582 video_stream_encoder_->OnBitrateUpdated(
3583 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3584 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003585
kthelgasonb83797b2017-02-14 11:57:25 -08003586 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003587 video_stream_encoder_->SetSource(&video_source_,
3588 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003589
asaperssonfab67072017-04-04 05:51:49 -07003590 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003591 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003592 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003593
mflodmancc3d4422017-08-03 08:27:51 -07003594 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003595 fake_encoder_.SetQualityScaling(true);
3596}
3597
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003598TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3599 webrtc::test::ScopedFieldTrials field_trials(
3600 "WebRTC-InitialFramedrop/Enabled/");
3601 // Reset encoder for field trials to take effect.
3602 ConfigureEncoder(video_encoder_config_.Copy());
3603 const int kTooLowBitrateForFrameSizeBps = 10000;
3604 const int kWidth = 640;
3605 const int kHeight = 360;
3606
Erik Språng4c6ca302019-04-08 15:14:01 +02003607 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003608 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3609 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003610 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3611 // Frame should not be dropped.
3612 WaitForEncodedFrame(1);
3613
Erik Språng610c7632019-03-06 15:37:33 +01003614 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003615 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003616 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003617 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003618 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3619 // Expect to drop this frame, the wait should time out.
3620 ExpectDroppedFrame();
3621
3622 // Expect the sink_wants to specify a scaled frame.
3623 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3624 video_stream_encoder_->Stop();
3625}
3626
Åsa Persson139f4dc2019-08-02 09:29:58 +02003627TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3628 webrtc::test::ScopedFieldTrials field_trials(
3629 "WebRTC-Video-QualityScalerSettings/"
3630 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3631 // Reset encoder for field trials to take effect.
3632 ConfigureEncoder(video_encoder_config_.Copy());
3633 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3634 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3635 const int kWidth = 640;
3636 const int kHeight = 360;
3637
3638 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003639 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3640 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003641 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3642 // Frame should not be dropped.
3643 WaitForEncodedFrame(1);
3644
3645 video_stream_encoder_->OnBitrateUpdated(
3646 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003647 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003648 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3649 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3650 // Frame should not be dropped.
3651 WaitForEncodedFrame(2);
3652
3653 video_stream_encoder_->OnBitrateUpdated(
3654 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003655 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003656 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3657 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3658 // Expect to drop this frame, the wait should time out.
3659 ExpectDroppedFrame();
3660
3661 // Expect the sink_wants to specify a scaled frame.
3662 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3663 video_stream_encoder_->Stop();
3664}
3665
mflodmancc3d4422017-08-03 08:27:51 -07003666TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003667 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3668 const int kTooSmallWidth = 10;
3669 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003670 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003671 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3672 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003673
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003674 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003675 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003676 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003677 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003678 VerifyNoLimitation(source.sink_wants());
3679 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3680
3681 // Trigger adapt down, too small frame, expect no change.
3682 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003683 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003684 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003685 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003686 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3687 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3688
mflodmancc3d4422017-08-03 08:27:51 -07003689 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003690}
3691
mflodmancc3d4422017-08-03 08:27:51 -07003692TEST_F(VideoStreamEncoderTest,
3693 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003694 const int kTooSmallWidth = 10;
3695 const int kTooSmallHeight = 10;
3696 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003697 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003698 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3699 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003700
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003701 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003702 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003703 video_stream_encoder_->SetSource(&source,
3704 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003705 VerifyNoLimitation(source.sink_wants());
3706 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3707 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3708
3709 // Trigger adapt down, expect limited framerate.
3710 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003711 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003712 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003713 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3714 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3715 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3716 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3717
3718 // Trigger adapt down, too small frame, expect no change.
3719 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003720 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003721 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003722 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3723 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3724 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3725 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3726
mflodmancc3d4422017-08-03 08:27:51 -07003727 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003728}
3729
mflodmancc3d4422017-08-03 08:27:51 -07003730TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003731 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003732 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003733 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3734 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003735 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003736 const int kFrameWidth = 1280;
3737 const int kFrameHeight = 720;
3738 video_source_.IncomingCapturedFrame(
3739 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003740 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003741 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003742}
3743
sprangb1ca0732017-02-01 08:38:12 -08003744// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003745TEST_F(VideoStreamEncoderTest,
3746 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003747 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003748 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3749 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003750
3751 const int kFrameWidth = 1280;
3752 const int kFrameHeight = 720;
3753 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003754 // requested by
3755 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003756 video_source_.set_adaptation_enabled(true);
3757
3758 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003759 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003760 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003761
3762 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003763 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003764 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003765 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003766 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003767
asaperssonfab67072017-04-04 05:51:49 -07003768 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003769 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003770 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003771 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003772 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003773
mflodmancc3d4422017-08-03 08:27:51 -07003774 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003775}
sprangfe627f32017-03-29 08:24:59 -07003776
mflodmancc3d4422017-08-03 08:27:51 -07003777TEST_F(VideoStreamEncoderTest,
3778 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003779 const int kFrameWidth = 1280;
3780 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003781
Erik Språng4c6ca302019-04-08 15:14:01 +02003782 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003783 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3784 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003785 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003786 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003787 video_source_.set_adaptation_enabled(true);
3788
sprang4847ae62017-06-27 07:06:52 -07003789 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003790
3791 video_source_.IncomingCapturedFrame(
3792 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003793 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003794
3795 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003796 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003797
3798 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003799 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003800 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003801 video_source_.IncomingCapturedFrame(
3802 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003803 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003804 }
3805
3806 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003807 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003808 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003809 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003810 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003811 video_source_.IncomingCapturedFrame(
3812 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003813 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003814 ++num_frames_dropped;
3815 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003816 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003817 }
3818 }
3819
sprang4847ae62017-06-27 07:06:52 -07003820 // Add some slack to account for frames dropped by the frame dropper.
3821 const int kErrorMargin = 1;
3822 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003823 kErrorMargin);
3824
3825 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003826 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003827 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003828 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003829 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003830 video_source_.IncomingCapturedFrame(
3831 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003832 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003833 ++num_frames_dropped;
3834 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003835 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003836 }
3837 }
sprang4847ae62017-06-27 07:06:52 -07003838 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003839 kErrorMargin);
3840
3841 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003842 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003843 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003844 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003845 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003846 video_source_.IncomingCapturedFrame(
3847 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003848 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003849 ++num_frames_dropped;
3850 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003851 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003852 }
3853 }
sprang4847ae62017-06-27 07:06:52 -07003854 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003855 kErrorMargin);
3856
3857 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003858 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003859 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003860 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003861 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003862 video_source_.IncomingCapturedFrame(
3863 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003864 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003865 ++num_frames_dropped;
3866 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003867 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003868 }
3869 }
3870 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3871
mflodmancc3d4422017-08-03 08:27:51 -07003872 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003873}
3874
mflodmancc3d4422017-08-03 08:27:51 -07003875TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003876 const int kFramerateFps = 5;
3877 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003878 const int kFrameWidth = 1280;
3879 const int kFrameHeight = 720;
3880
sprang4847ae62017-06-27 07:06:52 -07003881 // Reconfigure encoder with two temporal layers and screensharing, which will
3882 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003883 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003884
Erik Språng4c6ca302019-04-08 15:14:01 +02003885 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003886 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3887 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003888 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003889 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003890 video_source_.set_adaptation_enabled(true);
3891
sprang4847ae62017-06-27 07:06:52 -07003892 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003893
3894 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003895 rtc::VideoSinkWants last_wants;
3896 do {
3897 last_wants = video_source_.sink_wants();
3898
sprangc5d62e22017-04-02 23:53:04 -07003899 // Insert frames to get a new fps estimate...
3900 for (int j = 0; j < kFramerateFps; ++j) {
3901 video_source_.IncomingCapturedFrame(
3902 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08003903 if (video_source_.last_sent_width()) {
3904 sink_.WaitForEncodedFrame(timestamp_ms);
3905 }
sprangc5d62e22017-04-02 23:53:04 -07003906 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02003907 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07003908 }
3909 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07003910 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08003911 } while (video_source_.sink_wants().max_framerate_fps <
3912 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07003913
Jonathan Yubc771b72017-12-08 17:04:29 -08003914 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07003915
mflodmancc3d4422017-08-03 08:27:51 -07003916 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003917}
asaperssonf7e294d2017-06-13 23:25:22 -07003918
mflodmancc3d4422017-08-03 08:27:51 -07003919TEST_F(VideoStreamEncoderTest,
3920 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003921 const int kWidth = 1280;
3922 const int kHeight = 720;
3923 const int64_t kFrameIntervalMs = 150;
3924 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003925 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003926 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3927 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003928
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003929 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003930 AdaptingFrameForwarder source;
3931 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003932 video_stream_encoder_->SetSource(&source,
3933 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003934 timestamp_ms += kFrameIntervalMs;
3935 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003936 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003937 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003938 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3939 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3940 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3941
3942 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003943 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003944 timestamp_ms += kFrameIntervalMs;
3945 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003946 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003947 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
3948 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3949 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3950 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3951
3952 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003953 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003954 timestamp_ms += kFrameIntervalMs;
3955 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003956 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003957 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
3958 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3959 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3960 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3961
3962 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003963 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003964 timestamp_ms += kFrameIntervalMs;
3965 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003966 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003967 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3968 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3969 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3970 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3971
3972 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003973 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003974 timestamp_ms += kFrameIntervalMs;
3975 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003976 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003977 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3978 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3979 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3980 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3981
3982 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003983 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003984 timestamp_ms += kFrameIntervalMs;
3985 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003986 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003987 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3988 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3989 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3990 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3991
3992 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003993 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003994 timestamp_ms += kFrameIntervalMs;
3995 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003996 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003997 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3998 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3999 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4000 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4001
4002 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07004003 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004004 timestamp_ms += kFrameIntervalMs;
4005 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004006 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004007 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4008 rtc::VideoSinkWants last_wants = source.sink_wants();
4009 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4010 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4011 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4012
4013 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004014 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004015 timestamp_ms += kFrameIntervalMs;
4016 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004017 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004018 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
4019 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4020 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4021 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4022
4023 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004024 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004025 timestamp_ms += kFrameIntervalMs;
4026 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004027 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004028 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4029 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4030 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4031 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4032
4033 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07004034 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004035 timestamp_ms += kFrameIntervalMs;
4036 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004037 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004038 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4039 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4040 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4041 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4042
4043 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004044 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004045 timestamp_ms += kFrameIntervalMs;
4046 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004047 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004048 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4049 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4050 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4051 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4052
4053 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004054 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004055 timestamp_ms += kFrameIntervalMs;
4056 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004057 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004058 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4059 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4060 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4061 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4062
4063 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004064 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004065 timestamp_ms += kFrameIntervalMs;
4066 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004067 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004068 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4069 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4070 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4071 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4072
4073 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004074 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004075 timestamp_ms += kFrameIntervalMs;
4076 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004077 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004078 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4079 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4080 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4081 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4082
Åsa Persson30ab0152019-08-27 12:22:33 +02004083 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004084 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004085 timestamp_ms += kFrameIntervalMs;
4086 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004087 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004088 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004089 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004090 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4091 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4092 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4093
4094 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004095 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004096 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004097 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4098
mflodmancc3d4422017-08-03 08:27:51 -07004099 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004100}
4101
mflodmancc3d4422017-08-03 08:27:51 -07004102TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004103 const int kWidth = 1280;
4104 const int kHeight = 720;
4105 const int64_t kFrameIntervalMs = 150;
4106 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004107 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004108 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4109 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004110
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004111 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004112 AdaptingFrameForwarder source;
4113 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004114 video_stream_encoder_->SetSource(&source,
4115 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004116 timestamp_ms += kFrameIntervalMs;
4117 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004118 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004119 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004120 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4121 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4122 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4123 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4124 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4125 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4126
4127 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004128 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004129 timestamp_ms += kFrameIntervalMs;
4130 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004131 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004132 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4133 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4134 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4135 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4136 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4137 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4138 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4139
4140 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004141 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004142 timestamp_ms += kFrameIntervalMs;
4143 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004144 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004145 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4146 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4147 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4148 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4149 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4150 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4151 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4152
4153 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004154 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004155 timestamp_ms += kFrameIntervalMs;
4156 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004157 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004158 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4159 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4160 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4161 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4162 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4163 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4164 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4165
4166 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004167 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004168 timestamp_ms += kFrameIntervalMs;
4169 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004170 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004171 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4172 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4173 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4174 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4175 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4176 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4177 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4178
4179 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004180 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004181 timestamp_ms += kFrameIntervalMs;
4182 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004183 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004184 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4185 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4186 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4187 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4188 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4189 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4190 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4191
4192 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004193 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004194 timestamp_ms += kFrameIntervalMs;
4195 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004196 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004197 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004198 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004199 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4200 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4201 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4202 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4203 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4204 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4205
4206 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004207 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004208 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004209 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4210 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4211
mflodmancc3d4422017-08-03 08:27:51 -07004212 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004213}
4214
mflodmancc3d4422017-08-03 08:27:51 -07004215TEST_F(VideoStreamEncoderTest,
4216 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004217 const int kWidth = 640;
4218 const int kHeight = 360;
4219 const int kFpsLimit = 15;
4220 const int64_t kFrameIntervalMs = 150;
4221 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004222 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004223 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4224 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004225
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004226 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004227 AdaptingFrameForwarder source;
4228 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004229 video_stream_encoder_->SetSource(&source,
4230 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004231 timestamp_ms += kFrameIntervalMs;
4232 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004233 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004234 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004235 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4236 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4237 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4238 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4239 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4240 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4241
4242 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004243 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004244 timestamp_ms += kFrameIntervalMs;
4245 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004246 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004247 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4248 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4249 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4250 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4251 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4252 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4253 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4254
4255 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004256 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004257 timestamp_ms += kFrameIntervalMs;
4258 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004259 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004260 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4261 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4262 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4263 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4264 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4265 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4266 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4267
4268 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004269 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004270 timestamp_ms += kFrameIntervalMs;
4271 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004272 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004273 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4274 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4275 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4276 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4277 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4278 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4279 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4280
4281 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004282 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004283 timestamp_ms += kFrameIntervalMs;
4284 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004285 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004286 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004287 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4288 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4289 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4290 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4291 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4292 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4293
4294 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004295 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004296 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004297 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4298 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4299
mflodmancc3d4422017-08-03 08:27:51 -07004300 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004301}
4302
mflodmancc3d4422017-08-03 08:27:51 -07004303TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004304 const int kFrameWidth = 1920;
4305 const int kFrameHeight = 1080;
4306 // 3/4 of 1920.
4307 const int kAdaptedFrameWidth = 1440;
4308 // 3/4 of 1080 rounded down to multiple of 4.
4309 const int kAdaptedFrameHeight = 808;
4310 const int kFramerate = 24;
4311
Erik Språng4c6ca302019-04-08 15:14:01 +02004312 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004313 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4314 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004315 // Trigger reconfigure encoder (without resetting the entire instance).
4316 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004317 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004318 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4319 video_encoder_config.number_of_streams = 1;
4320 video_encoder_config.video_stream_factory =
4321 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004322 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004323 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004324 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004325
4326 video_source_.set_adaptation_enabled(true);
4327
4328 video_source_.IncomingCapturedFrame(
4329 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004330 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004331
4332 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004333 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004334 video_source_.IncomingCapturedFrame(
4335 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004336 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004337
mflodmancc3d4422017-08-03 08:27:51 -07004338 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004339}
4340
mflodmancc3d4422017-08-03 08:27:51 -07004341TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004342 const int kFrameWidth = 1280;
4343 const int kFrameHeight = 720;
4344 const int kLowFps = 2;
4345 const int kHighFps = 30;
4346
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);
sprang4847ae62017-06-27 07:06:52 -07004350
4351 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4352 max_framerate_ = kLowFps;
4353
4354 // Insert 2 seconds of 2fps video.
4355 for (int i = 0; i < kLowFps * 2; ++i) {
4356 video_source_.IncomingCapturedFrame(
4357 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4358 WaitForEncodedFrame(timestamp_ms);
4359 timestamp_ms += 1000 / kLowFps;
4360 }
4361
4362 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004363 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004364 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4365 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004366 video_source_.IncomingCapturedFrame(
4367 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4368 WaitForEncodedFrame(timestamp_ms);
4369 timestamp_ms += 1000 / kLowFps;
4370
4371 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4372
4373 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004374 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004375 const int kFrameIntervalMs = 1000 / kHighFps;
4376 max_framerate_ = kHighFps;
4377 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4378 video_source_.IncomingCapturedFrame(
4379 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4380 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4381 // be dropped if the encoder hans't been updated with the new higher target
4382 // framerate yet, causing it to overshoot the target bitrate and then
4383 // suffering the wrath of the media optimizer.
4384 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4385 timestamp_ms += kFrameIntervalMs;
4386 }
4387
4388 // Don expect correct measurement just yet, but it should be higher than
4389 // before.
4390 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4391
mflodmancc3d4422017-08-03 08:27:51 -07004392 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004393}
4394
mflodmancc3d4422017-08-03 08:27:51 -07004395TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004396 const int kFrameWidth = 1280;
4397 const int kFrameHeight = 720;
4398 const int kTargetBitrateBps = 1000000;
4399
4400 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004401 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004402 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004403 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4404 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004405 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004406
4407 // Insert a first video frame, causes another bitrate update.
4408 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4409 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4410 video_source_.IncomingCapturedFrame(
4411 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4412 WaitForEncodedFrame(timestamp_ms);
4413
4414 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004415 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4416 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004417
4418 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004419 timestamp_ms += kProcessIntervalMs;
4420 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004421
4422 // Bitrate observer should not be called.
4423 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4424 video_source_.IncomingCapturedFrame(
4425 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4426 ExpectDroppedFrame();
4427
mflodmancc3d4422017-08-03 08:27:51 -07004428 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004429}
ilnik6b826ef2017-06-16 06:53:48 -07004430
Niels Möller4db138e2018-04-19 09:04:13 +02004431TEST_F(VideoStreamEncoderTest,
4432 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4433 const int kFrameWidth = 1280;
4434 const int kFrameHeight = 720;
4435 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004436 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004437 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4438 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004439 video_source_.IncomingCapturedFrame(
4440 CreateFrame(1, kFrameWidth, kFrameHeight));
4441 WaitForEncodedFrame(1);
4442 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4443 .low_encode_usage_threshold_percent,
4444 default_options.low_encode_usage_threshold_percent);
4445 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4446 .high_encode_usage_threshold_percent,
4447 default_options.high_encode_usage_threshold_percent);
4448 video_stream_encoder_->Stop();
4449}
4450
4451TEST_F(VideoStreamEncoderTest,
4452 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4453 const int kFrameWidth = 1280;
4454 const int kFrameHeight = 720;
4455 CpuOveruseOptions hardware_options;
4456 hardware_options.low_encode_usage_threshold_percent = 150;
4457 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004458 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004459
Erik Språng4c6ca302019-04-08 15:14:01 +02004460 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004461 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4462 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004463 video_source_.IncomingCapturedFrame(
4464 CreateFrame(1, kFrameWidth, kFrameHeight));
4465 WaitForEncodedFrame(1);
4466 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4467 .low_encode_usage_threshold_percent,
4468 hardware_options.low_encode_usage_threshold_percent);
4469 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4470 .high_encode_usage_threshold_percent,
4471 hardware_options.high_encode_usage_threshold_percent);
4472 video_stream_encoder_->Stop();
4473}
4474
Niels Möller6bb5ab92019-01-11 11:11:10 +01004475TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4476 const int kFrameWidth = 320;
4477 const int kFrameHeight = 240;
4478 const int kFps = 30;
4479 const int kTargetBitrateBps = 120000;
4480 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4481
Erik Språng4c6ca302019-04-08 15:14:01 +02004482 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004483 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4484 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004485
4486 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4487 max_framerate_ = kFps;
4488
4489 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4490 fake_encoder_.SimulateOvershoot(1.0);
4491 int num_dropped = 0;
4492 for (int i = 0; i < kNumFramesInRun; ++i) {
4493 video_source_.IncomingCapturedFrame(
4494 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4495 // Wait up to two frame durations for a frame to arrive.
4496 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4497 ++num_dropped;
4498 }
4499 timestamp_ms += 1000 / kFps;
4500 }
4501
Erik Språnga8d48ab2019-02-08 14:17:40 +01004502 // Framerate should be measured to be near the expected target rate.
4503 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4504
4505 // Frame drops should be within 5% of expected 0%.
4506 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004507
4508 // Make encoder produce frames at double the expected bitrate during 3 seconds
4509 // of video, verify number of drops. Rate needs to be slightly changed in
4510 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004511 double overshoot_factor = 2.0;
4512 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4513 // With bitrate adjuster, when need to overshoot even more to trigger
4514 // frame dropping.
4515 overshoot_factor *= 2;
4516 }
4517 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004518 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004519 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004520 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004521 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004522 num_dropped = 0;
4523 for (int i = 0; i < kNumFramesInRun; ++i) {
4524 video_source_.IncomingCapturedFrame(
4525 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4526 // Wait up to two frame durations for a frame to arrive.
4527 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4528 ++num_dropped;
4529 }
4530 timestamp_ms += 1000 / kFps;
4531 }
4532
Erik Språng4c6ca302019-04-08 15:14:01 +02004533 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004534 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4535 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004536
4537 // Target framerate should be still be near the expected target, despite
4538 // the frame drops.
4539 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4540
4541 // Frame drops should be within 5% of expected 50%.
4542 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004543
4544 video_stream_encoder_->Stop();
4545}
4546
4547TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4548 const int kFrameWidth = 320;
4549 const int kFrameHeight = 240;
4550 const int kActualInputFps = 24;
4551 const int kTargetBitrateBps = 120000;
4552
4553 ASSERT_GT(max_framerate_, kActualInputFps);
4554
4555 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4556 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004557 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004558 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4559 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004560
4561 // Insert 3 seconds of video, with an input fps lower than configured max.
4562 for (int i = 0; i < kActualInputFps * 3; ++i) {
4563 video_source_.IncomingCapturedFrame(
4564 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4565 // Wait up to two frame durations for a frame to arrive.
4566 WaitForEncodedFrame(timestamp_ms);
4567 timestamp_ms += 1000 / kActualInputFps;
4568 }
4569
4570 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4571
4572 video_stream_encoder_->Stop();
4573}
4574
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004575TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4576 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004577 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004578 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4579 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004580
4581 fake_encoder_.BlockNextEncode();
4582 video_source_.IncomingCapturedFrame(
4583 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4584 WaitForEncodedFrame(1);
4585 // On the very first frame full update should be forced.
4586 rect = fake_encoder_.GetLastUpdateRect();
4587 EXPECT_EQ(rect.offset_x, 0);
4588 EXPECT_EQ(rect.offset_y, 0);
4589 EXPECT_EQ(rect.height, codec_height_);
4590 EXPECT_EQ(rect.width, codec_width_);
4591 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4592 // call to ContinueEncode.
4593 video_source_.IncomingCapturedFrame(
4594 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4595 ExpectDroppedFrame();
4596 video_source_.IncomingCapturedFrame(
4597 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4598 ExpectDroppedFrame();
4599 fake_encoder_.ContinueEncode();
4600 WaitForEncodedFrame(3);
4601 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4602 rect = fake_encoder_.GetLastUpdateRect();
4603 EXPECT_EQ(rect.offset_x, 1);
4604 EXPECT_EQ(rect.offset_y, 0);
4605 EXPECT_EQ(rect.width, 10);
4606 EXPECT_EQ(rect.height, 1);
4607
4608 video_source_.IncomingCapturedFrame(
4609 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4610 WaitForEncodedFrame(4);
4611 // Previous frame was encoded, so no accumulation should happen.
4612 rect = fake_encoder_.GetLastUpdateRect();
4613 EXPECT_EQ(rect.offset_x, 0);
4614 EXPECT_EQ(rect.offset_y, 0);
4615 EXPECT_EQ(rect.width, 1);
4616 EXPECT_EQ(rect.height, 1);
4617
4618 video_stream_encoder_->Stop();
4619}
4620
Erik Språngd7329ca2019-02-21 21:19:53 +01004621TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004622 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004623 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4624 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004625
4626 // First frame is always keyframe.
4627 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4628 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004629 EXPECT_THAT(
4630 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004631 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004632
4633 // Insert delta frame.
4634 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4635 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004636 EXPECT_THAT(
4637 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004638 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004639
4640 // Request next frame be a key-frame.
4641 video_stream_encoder_->SendKeyFrame();
4642 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4643 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004644 EXPECT_THAT(
4645 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004646 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004647
4648 video_stream_encoder_->Stop();
4649}
4650
4651TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4652 // Setup simulcast with three streams.
4653 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004654 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004655 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004656 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004657 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004658 // Wait for all three layers before triggering event.
4659 sink_.SetNumExpectedLayers(3);
4660
4661 // First frame is always keyframe.
4662 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4663 WaitForEncodedFrame(1);
4664 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004665 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4666 VideoFrameType::kVideoFrameKey,
4667 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004668
4669 // Insert delta frame.
4670 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4671 WaitForEncodedFrame(2);
4672 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004673 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4674 VideoFrameType::kVideoFrameDelta,
4675 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004676
4677 // Request next frame be a key-frame.
4678 // Only first stream is configured to produce key-frame.
4679 video_stream_encoder_->SendKeyFrame();
4680 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4681 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004682
4683 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4684 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004685 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004686 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004687 VideoFrameType::kVideoFrameKey,
4688 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004689
4690 video_stream_encoder_->Stop();
4691}
4692
4693TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4694 // Configure internal source factory and setup test again.
4695 encoder_factory_.SetHasInternalSource(true);
4696 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004697 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004698 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4699 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004700
4701 // Call encoder directly, simulating internal source where encoded frame
4702 // callback in VideoStreamEncoder is called despite no OnFrame().
4703 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4704 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004705 EXPECT_THAT(
4706 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004707 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004708
Niels Möller8f7ce222019-03-21 15:43:58 +01004709 const std::vector<VideoFrameType> kDeltaFrame = {
4710 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004711 // Need to set timestamp manually since manually for injected frame.
4712 VideoFrame frame = CreateFrame(101, nullptr);
4713 frame.set_timestamp(101);
4714 fake_encoder_.InjectFrame(frame, false);
4715 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004716 EXPECT_THAT(
4717 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004718 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004719
4720 // Request key-frame. The forces a dummy frame down into the encoder.
4721 fake_encoder_.ExpectNullFrame();
4722 video_stream_encoder_->SendKeyFrame();
4723 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004724 EXPECT_THAT(
4725 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004726 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004727
4728 video_stream_encoder_->Stop();
4729}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004730
4731TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4732 // Configure internal source factory and setup test again.
4733 encoder_factory_.SetHasInternalSource(true);
4734 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004735 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004736 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4737 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004738
4739 int64_t timestamp = 1;
4740 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004741 image.SetEncodedData(
4742 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004743 image.capture_time_ms_ = ++timestamp;
4744 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4745 const int64_t kEncodeFinishDelayMs = 10;
4746 image.timing_.encode_start_ms = timestamp;
4747 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4748 fake_encoder_.InjectEncodedImage(image);
4749 // Wait for frame without incrementing clock.
4750 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4751 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4752 // capture timestamp should be kEncodeFinishDelayMs in the past.
4753 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4754 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4755 kEncodeFinishDelayMs);
4756
4757 video_stream_encoder_->Stop();
4758}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004759
4760TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4761 // Configure internal source factory and setup test again.
4762 encoder_factory_.SetHasInternalSource(true);
4763 ResetEncoder("H264", 1, 1, 1, false);
4764
4765 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4766 image._frameType = VideoFrameType::kVideoFrameKey;
4767
4768 CodecSpecificInfo codec_specific_info;
4769 codec_specific_info.codecType = kVideoCodecH264;
4770
4771 RTPFragmentationHeader fragmentation;
4772 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4773 fragmentation.fragmentationOffset[0] = 4;
4774 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4775
4776 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4777 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4778
4779 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4780 testing::ElementsAreArray(optimal_sps));
4781 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4782 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4783 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4784 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4785
4786 video_stream_encoder_->Stop();
4787}
4788
4789TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4790 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4791 0x00, 0x00, 0x03, 0x03, 0xF4,
4792 0x05, 0x03, 0xC7, 0xC0};
4793
4794 // Configure internal source factory and setup test again.
4795 encoder_factory_.SetHasInternalSource(true);
4796 ResetEncoder("H264", 1, 1, 1, false);
4797
4798 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4799 image._frameType = VideoFrameType::kVideoFrameKey;
4800
4801 CodecSpecificInfo codec_specific_info;
4802 codec_specific_info.codecType = kVideoCodecH264;
4803
4804 RTPFragmentationHeader fragmentation;
4805 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4806 fragmentation.fragmentationOffset[0] = 4;
4807 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4808
4809 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4810 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4811
4812 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4813 testing::ElementsAreArray(optimal_sps));
4814 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4815 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4816 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4817 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4818
4819 video_stream_encoder_->Stop();
4820}
4821
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004822TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4823 const int kFrameWidth = 1280;
4824 const int kFrameHeight = 720;
4825 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4826
4827 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004828 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4829 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004830 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4831
4832 // Insert a first video frame. It should be dropped because of downscale in
4833 // resolution.
4834 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4835 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4836 frame.set_rotation(kVideoRotation_270);
4837 video_source_.IncomingCapturedFrame(frame);
4838
4839 ExpectDroppedFrame();
4840
4841 // Second frame is downscaled.
4842 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4843 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4844 frame.set_rotation(kVideoRotation_90);
4845 video_source_.IncomingCapturedFrame(frame);
4846
4847 WaitForEncodedFrame(timestamp_ms);
4848 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4849
4850 // Insert another frame, also downscaled.
4851 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4852 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4853 frame.set_rotation(kVideoRotation_180);
4854 video_source_.IncomingCapturedFrame(frame);
4855
4856 WaitForEncodedFrame(timestamp_ms);
4857 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4858
4859 video_stream_encoder_->Stop();
4860}
4861
Erik Språng5056af02019-09-02 15:53:11 +02004862TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4863 const int kFrameWidth = 320;
4864 const int kFrameHeight = 180;
4865
4866 // Initial rate.
4867 video_stream_encoder_->OnBitrateUpdated(
4868 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004869 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004870 /*link_allocation=*/DataRate::kbps(300),
4871 /*fraction_lost=*/0,
4872 /*rtt_ms=*/0);
4873
4874 // Insert a first video frame so that encoder gets configured.
4875 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4876 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4877 frame.set_rotation(kVideoRotation_270);
4878 video_source_.IncomingCapturedFrame(frame);
4879 WaitForEncodedFrame(timestamp_ms);
4880
4881 // Set a target rate below the minimum allowed by the codec settings.
4882 VideoCodec codec_config = fake_encoder_.codec_config();
4883 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4884 DataRate target_rate = min_rate - DataRate::kbps(1);
4885 video_stream_encoder_->OnBitrateUpdated(
4886 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004887 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004888 /*link_allocation=*/target_rate,
4889 /*fraction_lost=*/0,
4890 /*rtt_ms=*/0);
4891 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4892
4893 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4894 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4895 ASSERT_TRUE(rate_settings.has_value());
4896 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
4897 EXPECT_EQ(min_rate, allocation_sum);
4898 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
4899
4900 video_stream_encoder_->Stop();
4901}
4902
philipeld9cc8c02019-09-16 14:53:40 +02004903struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
4904 MOCK_METHOD0(RequestEncoderFallback, void());
4905 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
4906};
4907
4908TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
4909 constexpr int kDontCare = 100;
4910
4911 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4912 video_send_config_.encoder_settings.encoder_switch_request_callback =
4913 &switch_callback;
4914 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4915 encoder_config.codec_type = kVideoCodecVP8;
4916 webrtc::test::ScopedFieldTrials field_trial(
4917 "WebRTC-NetworkCondition-EncoderSwitch/"
4918 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
4919 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4920
4921 // Reset encoder for new configuration to take effect.
4922 ConfigureEncoder(std::move(encoder_config));
4923
4924 // Send one frame to trigger ReconfigureEncoder.
4925 video_source_.IncomingCapturedFrame(
4926 CreateFrame(kDontCare, kDontCare, kDontCare));
4927
4928 using Config = EncoderSwitchRequestCallback::Config;
4929 EXPECT_CALL(switch_callback,
4930 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4931 Field(&Config::param, "ping"),
4932 Field(&Config::value, "pong"))));
4933
4934 video_stream_encoder_->OnBitrateUpdated(
4935 /*target_bitrate=*/DataRate::kbps(50),
4936 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
4937 /*link_allocation=*/DataRate::kbps(kDontCare),
4938 /*fraction_lost=*/0,
4939 /*rtt_ms=*/0);
4940
4941 video_stream_encoder_->Stop();
4942}
4943
4944TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
4945 constexpr int kSufficientBitrateToNotDrop = 1000;
4946 constexpr int kHighRes = 500;
4947 constexpr int kLowRes = 100;
4948
4949 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4950 video_send_config_.encoder_settings.encoder_switch_request_callback =
4951 &switch_callback;
4952 webrtc::test::ScopedFieldTrials field_trial(
4953 "WebRTC-NetworkCondition-EncoderSwitch/"
4954 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
4955 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4956 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4957 encoder_config.codec_type = kVideoCodecH264;
4958
4959 // Reset encoder for new configuration to take effect.
4960 ConfigureEncoder(std::move(encoder_config));
4961
4962 // The VideoStreamEncoder needs some bitrate before it can start encoding,
4963 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
4964 // not fail.
4965 video_stream_encoder_->OnBitrateUpdated(
4966 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4967 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4968 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4969 /*fraction_lost=*/0,
4970 /*rtt_ms=*/0);
4971
4972 // Send one frame to trigger ReconfigureEncoder.
4973 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
4974 WaitForEncodedFrame(1);
4975
4976 using Config = EncoderSwitchRequestCallback::Config;
4977 EXPECT_CALL(switch_callback,
4978 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4979 Field(&Config::param, "ping"),
4980 Field(&Config::value, "pong"))));
4981
4982 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
4983 WaitForEncodedFrame(2);
4984
4985 video_stream_encoder_->Stop();
4986}
4987
perkj26091b12016-09-01 01:17:40 -07004988} // namespace webrtc