blob: e037bf1d3de651354ab67255505bcfc9ce5fe0b9 [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::_;
kthelgason876222f2016-11-29 01:44:11 -080052
perkj803d97f2016-11-01 11:45:46 -070053namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020054const int kMinPixelsPerFrame = 320 * 180;
55const int kMinFramerateFps = 2;
56const int kMinBalancedFramerateFps = 7;
57const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080058const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010059const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020060const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010061const uint32_t kSimulcastTargetBitrateBps = 3150000;
62const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080063const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070064const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020065const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020066const int64_t kProcessIntervalMs = 1000;
asapersson5f7226f2016-11-25 04:37:00 -080067
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020068uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
69 0x00, 0x00, 0x03, 0x03, 0xF4,
70 0x05, 0x03, 0xC7, 0xE0, 0x1B,
71 0x41, 0x10, 0x8D, 0x00};
72
perkj803d97f2016-11-01 11:45:46 -070073class TestBuffer : public webrtc::I420Buffer {
74 public:
75 TestBuffer(rtc::Event* event, int width, int height)
76 : I420Buffer(width, height), event_(event) {}
77
78 private:
79 friend class rtc::RefCountedObject<TestBuffer>;
80 ~TestBuffer() override {
81 if (event_)
82 event_->Set();
83 }
84 rtc::Event* const event_;
85};
86
Noah Richards51db4212019-06-12 06:59:12 -070087// A fake native buffer that can't be converted to I420.
88class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
89 public:
90 FakeNativeBuffer(rtc::Event* event, int width, int height)
91 : event_(event), width_(width), height_(height) {}
92 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
93 int width() const override { return width_; }
94 int height() const override { return height_; }
95 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
96 return nullptr;
97 }
98
99 private:
100 friend class rtc::RefCountedObject<FakeNativeBuffer>;
101 ~FakeNativeBuffer() override {
102 if (event_)
103 event_->Set();
104 }
105 rtc::Event* const event_;
106 const int width_;
107 const int height_;
108};
109
Niels Möller7dc26b72017-12-06 10:27:48 +0100110class CpuOveruseDetectorProxy : public OveruseFrameDetector {
111 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200112 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
113 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100114 last_target_framerate_fps_(-1) {}
115 virtual ~CpuOveruseDetectorProxy() {}
116
117 void OnTargetFramerateUpdated(int framerate_fps) override {
118 rtc::CritScope cs(&lock_);
119 last_target_framerate_fps_ = framerate_fps;
120 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
121 }
122
123 int GetLastTargetFramerate() {
124 rtc::CritScope cs(&lock_);
125 return last_target_framerate_fps_;
126 }
127
Niels Möller4db138e2018-04-19 09:04:13 +0200128 CpuOveruseOptions GetOptions() { return options_; }
129
Niels Möller7dc26b72017-12-06 10:27:48 +0100130 private:
131 rtc::CriticalSection lock_;
132 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
133};
134
mflodmancc3d4422017-08-03 08:27:51 -0700135class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700136 public:
Niels Möller213618e2018-07-24 09:29:58 +0200137 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200138 const VideoStreamEncoderSettings& settings,
139 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100140 : VideoStreamEncoder(Clock::GetRealTimeClock(),
141 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200142 stats_proxy,
143 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200144 std::unique_ptr<OveruseFrameDetector>(
145 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100146 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200147 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700148
sprangb1ca0732017-02-01 08:38:12 -0800149 void PostTaskAndWait(bool down, AdaptReason reason) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100150 rtc::Event event;
kthelgason876222f2016-11-29 01:44:11 -0800151 encoder_queue()->PostTask([this, &event, reason, down] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200152 if (down)
153 AdaptDown(reason);
154 else
155 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700156 event.Set();
157 });
perkj070ba852017-02-16 15:46:27 -0800158 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700159 }
160
kthelgason2fc52542017-03-03 00:24:41 -0800161 // This is used as a synchronisation mechanism, to make sure that the
162 // encoder queue is not blocked before we start sending it frames.
163 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100164 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200165 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800166 ASSERT_TRUE(event.Wait(5000));
167 }
168
sprangb1ca0732017-02-01 08:38:12 -0800169 void TriggerCpuOveruse() { PostTaskAndWait(true, AdaptReason::kCpu); }
kthelgason876222f2016-11-29 01:44:11 -0800170
sprangb1ca0732017-02-01 08:38:12 -0800171 void TriggerCpuNormalUsage() { PostTaskAndWait(false, AdaptReason::kCpu); }
kthelgason876222f2016-11-29 01:44:11 -0800172
sprangb1ca0732017-02-01 08:38:12 -0800173 void TriggerQualityLow() { PostTaskAndWait(true, AdaptReason::kQuality); }
kthelgason876222f2016-11-29 01:44:11 -0800174
sprangb1ca0732017-02-01 08:38:12 -0800175 void TriggerQualityHigh() { PostTaskAndWait(false, AdaptReason::kQuality); }
sprangfda496a2017-06-15 04:21:07 -0700176
Niels Möller7dc26b72017-12-06 10:27:48 +0100177 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700178};
179
asapersson5f7226f2016-11-25 04:37:00 -0800180class VideoStreamFactory
181 : public VideoEncoderConfig::VideoStreamFactoryInterface {
182 public:
sprangfda496a2017-06-15 04:21:07 -0700183 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
184 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800185 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700186 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800187 }
188
189 private:
190 std::vector<VideoStream> CreateEncoderStreams(
191 int width,
192 int height,
193 const VideoEncoderConfig& encoder_config) override {
194 std::vector<VideoStream> streams =
195 test::CreateVideoStreams(width, height, encoder_config);
196 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100197 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700198 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800199 }
200 return streams;
201 }
sprangfda496a2017-06-15 04:21:07 -0700202
asapersson5f7226f2016-11-25 04:37:00 -0800203 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700204 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800205};
206
Noah Richards51db4212019-06-12 06:59:12 -0700207// Simulates simulcast behavior and makes highest stream resolutions divisible
208// by 4.
209class CroppingVideoStreamFactory
210 : public VideoEncoderConfig::VideoStreamFactoryInterface {
211 public:
212 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
213 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
214 EXPECT_GT(num_temporal_layers, 0u);
215 EXPECT_GT(framerate, 0);
216 }
217
218 private:
219 std::vector<VideoStream> CreateEncoderStreams(
220 int width,
221 int height,
222 const VideoEncoderConfig& encoder_config) override {
223 std::vector<VideoStream> streams = test::CreateVideoStreams(
224 width - width % 4, height - height % 4, encoder_config);
225 for (VideoStream& stream : streams) {
226 stream.num_temporal_layers = num_temporal_layers_;
227 stream.max_framerate = framerate_;
228 }
229 return streams;
230 }
231
232 const size_t num_temporal_layers_;
233 const int framerate_;
234};
235
sprangb1ca0732017-02-01 08:38:12 -0800236class AdaptingFrameForwarder : public test::FrameForwarder {
237 public:
238 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700239 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800240
241 void set_adaptation_enabled(bool enabled) {
242 rtc::CritScope cs(&crit_);
243 adaptation_enabled_ = enabled;
244 }
245
asaperssonfab67072017-04-04 05:51:49 -0700246 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800247 rtc::CritScope cs(&crit_);
248 return adaptation_enabled_;
249 }
250
asapersson09f05612017-05-15 23:40:18 -0700251 rtc::VideoSinkWants last_wants() const {
252 rtc::CritScope cs(&crit_);
253 return last_wants_;
254 }
255
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200256 absl::optional<int> last_sent_width() const { return last_width_; }
257 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800258
sprangb1ca0732017-02-01 08:38:12 -0800259 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
260 int cropped_width = 0;
261 int cropped_height = 0;
262 int out_width = 0;
263 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700264 if (adaption_enabled()) {
265 if (adapter_.AdaptFrameResolution(
266 video_frame.width(), video_frame.height(),
267 video_frame.timestamp_us() * 1000, &cropped_width,
268 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100269 VideoFrame adapted_frame =
270 VideoFrame::Builder()
271 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
272 nullptr, out_width, out_height))
273 .set_timestamp_rtp(99)
274 .set_timestamp_ms(99)
275 .set_rotation(kVideoRotation_0)
276 .build();
sprangc5d62e22017-04-02 23:53:04 -0700277 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
278 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800279 last_width_.emplace(adapted_frame.width());
280 last_height_.emplace(adapted_frame.height());
281 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200282 last_width_ = absl::nullopt;
283 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700284 }
sprangb1ca0732017-02-01 08:38:12 -0800285 } else {
286 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800287 last_width_.emplace(video_frame.width());
288 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800289 }
290 }
291
292 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
293 const rtc::VideoSinkWants& wants) override {
294 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700295 last_wants_ = sink_wants();
sprangc5d62e22017-04-02 23:53:04 -0700296 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
297 wants.max_pixel_count,
298 wants.max_framerate_fps);
sprangb1ca0732017-02-01 08:38:12 -0800299 test::FrameForwarder::AddOrUpdateSink(sink, wants);
300 }
sprangb1ca0732017-02-01 08:38:12 -0800301 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700302 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
303 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200304 absl::optional<int> last_width_;
305 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800306};
sprangc5d62e22017-04-02 23:53:04 -0700307
Niels Möller213618e2018-07-24 09:29:58 +0200308// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700309class MockableSendStatisticsProxy : public SendStatisticsProxy {
310 public:
311 MockableSendStatisticsProxy(Clock* clock,
312 const VideoSendStream::Config& config,
313 VideoEncoderConfig::ContentType content_type)
314 : SendStatisticsProxy(clock, config, content_type) {}
315
316 VideoSendStream::Stats GetStats() override {
317 rtc::CritScope cs(&lock_);
318 if (mock_stats_)
319 return *mock_stats_;
320 return SendStatisticsProxy::GetStats();
321 }
322
Niels Möller213618e2018-07-24 09:29:58 +0200323 int GetInputFrameRate() const override {
324 rtc::CritScope cs(&lock_);
325 if (mock_stats_)
326 return mock_stats_->input_frame_rate;
327 return SendStatisticsProxy::GetInputFrameRate();
328 }
sprangc5d62e22017-04-02 23:53:04 -0700329 void SetMockStats(const VideoSendStream::Stats& stats) {
330 rtc::CritScope cs(&lock_);
331 mock_stats_.emplace(stats);
332 }
333
334 void ResetMockStats() {
335 rtc::CritScope cs(&lock_);
336 mock_stats_.reset();
337 }
338
339 private:
340 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200341 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700342};
343
sprang4847ae62017-06-27 07:06:52 -0700344class MockBitrateObserver : public VideoBitrateAllocationObserver {
345 public:
Erik Språng566124a2018-04-23 12:32:22 +0200346 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700347};
348
perkj803d97f2016-11-01 11:45:46 -0700349} // namespace
350
mflodmancc3d4422017-08-03 08:27:51 -0700351class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700352 public:
353 static const int kDefaultTimeoutMs = 30 * 1000;
354
mflodmancc3d4422017-08-03 08:27:51 -0700355 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700356 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700357 codec_width_(320),
358 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200359 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200360 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700361 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200362 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700363 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700364 Clock::GetRealTimeClock(),
365 video_send_config_,
366 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700367 sink_(&fake_encoder_) {}
368
369 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700370 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700371 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200372 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800373 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200374 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200375 video_send_config_.rtp.payload_name = "FAKE";
376 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700377
Per512ecb32016-09-23 15:52:06 +0200378 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200379 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700380 video_encoder_config.video_stream_factory =
381 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100382 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700383
384 // Framerate limit is specified by the VideoStreamFactory.
385 std::vector<VideoStream> streams =
386 video_encoder_config.video_stream_factory->CreateEncoderStreams(
387 codec_width_, codec_height_, video_encoder_config);
388 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200389 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700390
Niels Möllerf1338562018-04-26 09:51:47 +0200391 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800392 }
393
Niels Möllerf1338562018-04-26 09:51:47 +0200394 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700395 if (video_stream_encoder_)
396 video_stream_encoder_->Stop();
397 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200398 stats_proxy_.get(), video_send_config_.encoder_settings,
399 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700400 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
401 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700402 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700403 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
404 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200405 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700406 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800407 }
408
409 void ResetEncoder(const std::string& payload_name,
410 size_t num_streams,
411 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700412 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700413 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200414 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800415
416 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200417 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800418 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100419 video_encoder_config.max_bitrate_bps =
420 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800421 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700422 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
423 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700424 video_encoder_config.content_type =
425 screenshare ? VideoEncoderConfig::ContentType::kScreen
426 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700427 if (payload_name == "VP9") {
428 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
429 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
430 video_encoder_config.encoder_specific_settings =
431 new rtc::RefCountedObject<
432 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
433 }
Niels Möllerf1338562018-04-26 09:51:47 +0200434 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700435 }
436
sprang57c2fff2017-01-16 06:24:02 -0800437 VideoFrame CreateFrame(int64_t ntp_time_ms,
438 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100439 VideoFrame frame =
440 VideoFrame::Builder()
441 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
442 destruction_event, codec_width_, codec_height_))
443 .set_timestamp_rtp(99)
444 .set_timestamp_ms(99)
445 .set_rotation(kVideoRotation_0)
446 .build();
sprang57c2fff2017-01-16 06:24:02 -0800447 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700448 return frame;
449 }
450
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100451 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
452 rtc::Event* destruction_event,
453 int offset_x) const {
454 VideoFrame frame =
455 VideoFrame::Builder()
456 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
457 destruction_event, codec_width_, codec_height_))
458 .set_timestamp_rtp(99)
459 .set_timestamp_ms(99)
460 .set_rotation(kVideoRotation_0)
461 .set_update_rect({offset_x, 0, 1, 1})
462 .build();
463 frame.set_ntp_time_ms(ntp_time_ms);
464 return frame;
465 }
466
sprang57c2fff2017-01-16 06:24:02 -0800467 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100468 VideoFrame frame =
469 VideoFrame::Builder()
470 .set_video_frame_buffer(
471 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
472 .set_timestamp_rtp(99)
473 .set_timestamp_ms(99)
474 .set_rotation(kVideoRotation_0)
475 .build();
sprang57c2fff2017-01-16 06:24:02 -0800476 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700477 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700478 return frame;
479 }
480
Noah Richards51db4212019-06-12 06:59:12 -0700481 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
482 rtc::Event* destruction_event,
483 int width,
484 int height) const {
485 VideoFrame frame =
486 VideoFrame::Builder()
487 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
488 destruction_event, width, height))
489 .set_timestamp_rtp(99)
490 .set_timestamp_ms(99)
491 .set_rotation(kVideoRotation_0)
492 .build();
493 frame.set_ntp_time_ms(ntp_time_ms);
494 return frame;
495 }
496
497 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
498 rtc::Event* destruction_event) const {
499 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
500 codec_height_);
501 }
502
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100503 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
504 MockBitrateObserver bitrate_observer;
505 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
506
507 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
508 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200509 video_stream_encoder_->OnBitrateUpdated(
510 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
511 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100512
513 video_source_.IncomingCapturedFrame(
514 CreateFrame(1, codec_width_, codec_height_));
515 WaitForEncodedFrame(1);
516 }
517
asapersson02465b82017-04-10 01:12:52 -0700518 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700519 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700520 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
521 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700522 }
523
asapersson09f05612017-05-15 23:40:18 -0700524 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
525 const rtc::VideoSinkWants& wants2) {
526 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
527 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
528 }
529
Åsa Persson8c1bf952018-09-13 10:42:19 +0200530 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
531 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
532 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
533 EXPECT_FALSE(wants.target_pixel_count);
534 }
535
asapersson09f05612017-05-15 23:40:18 -0700536 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
537 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200538 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700539 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
540 EXPECT_GT(wants1.max_pixel_count, 0);
541 }
542
543 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
544 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200545 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700546 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
547 }
548
asaperssonf7e294d2017-06-13 23:25:22 -0700549 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
550 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200551 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700552 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
553 }
554
555 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
556 const rtc::VideoSinkWants& wants2) {
557 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
558 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
559 }
560
561 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
562 const rtc::VideoSinkWants& wants2) {
563 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
564 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
565 }
566
567 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
568 const rtc::VideoSinkWants& wants2) {
569 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
570 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
571 EXPECT_GT(wants1.max_pixel_count, 0);
572 }
573
574 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
575 const rtc::VideoSinkWants& wants2) {
576 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
577 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
578 }
579
asapersson09f05612017-05-15 23:40:18 -0700580 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
581 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200582 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700583 EXPECT_LT(wants.max_pixel_count, pixel_count);
584 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700585 }
586
587 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
588 EXPECT_LT(wants.max_framerate_fps, fps);
589 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
590 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700591 }
592
asaperssonf7e294d2017-06-13 23:25:22 -0700593 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
594 int expected_fps) {
595 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
596 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
597 EXPECT_FALSE(wants.target_pixel_count);
598 }
599
Jonathan Yubc771b72017-12-08 17:04:29 -0800600 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
601 int last_frame_pixels) {
602 // Balanced mode should always scale FPS to the desired range before
603 // attempting to scale resolution.
604 int fps_limit = wants.max_framerate_fps;
605 if (last_frame_pixels <= 320 * 240) {
606 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
607 } else if (last_frame_pixels <= 480 * 270) {
608 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
609 } else if (last_frame_pixels <= 640 * 480) {
610 EXPECT_LE(15, fps_limit);
611 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200612 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800613 }
614 }
615
sprang4847ae62017-06-27 07:06:52 -0700616 void WaitForEncodedFrame(int64_t expected_ntp_time) {
617 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200618 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700619 }
620
621 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
622 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200623 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700624 return ok;
625 }
626
627 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
628 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200629 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700630 }
631
632 void ExpectDroppedFrame() {
633 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200634 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700635 }
636
637 bool WaitForFrame(int64_t timeout_ms) {
638 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200639 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700640 return ok;
641 }
642
perkj26091b12016-09-01 01:17:40 -0700643 class TestEncoder : public test::FakeEncoder {
644 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100645 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700646
asaperssonfab67072017-04-04 05:51:49 -0700647 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800648 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700649 return config_;
650 }
651
652 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800653 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700654 block_next_encode_ = true;
655 }
656
Erik Språngaed30702018-11-05 12:57:17 +0100657 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800658 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100659 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100660 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100661 if (quality_scaling_) {
662 info.scaling_settings =
663 VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame);
664 }
665 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100666 for (int i = 0; i < kMaxSpatialLayers; ++i) {
667 if (temporal_layers_supported_[i]) {
668 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
669 info.fps_allocation[i].resize(num_layers);
670 }
671 }
Erik Språngaed30702018-11-05 12:57:17 +0100672 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200673
674 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Erik Språngaed30702018-11-05 12:57:17 +0100675 return info;
kthelgason876222f2016-11-29 01:44:11 -0800676 }
677
Erik Språngb7cb7b52019-02-26 15:52:33 +0100678 int32_t RegisterEncodeCompleteCallback(
679 EncodedImageCallback* callback) override {
680 rtc::CritScope lock(&local_crit_sect_);
681 encoded_image_callback_ = callback;
682 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
683 }
684
perkjfa10b552016-10-02 23:45:26 -0700685 void ContinueEncode() { continue_encode_event_.Set(); }
686
687 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
688 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800689 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700690 EXPECT_EQ(timestamp_, timestamp);
691 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
692 }
693
kthelgason2fc52542017-03-03 00:24:41 -0800694 void SetQualityScaling(bool b) {
695 rtc::CritScope lock(&local_crit_sect_);
696 quality_scaling_ = b;
697 }
kthelgasonad9010c2017-02-14 00:46:51 -0800698
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100699 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
700 rtc::CritScope lock(&local_crit_sect_);
701 is_hardware_accelerated_ = is_hardware_accelerated;
702 }
703
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100704 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
705 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
706 rtc::CritScope lock(&local_crit_sect_);
707 temporal_layers_supported_[spatial_idx] = supported;
708 }
709
Sergey Silkin6456e352019-07-08 17:56:40 +0200710 void SetResolutionBitrateLimits(
711 std::vector<ResolutionBitrateLimits> thresholds) {
712 rtc::CritScope cs(&local_crit_sect_);
713 resolution_bitrate_limits_ = thresholds;
714 }
715
sprangfe627f32017-03-29 08:24:59 -0700716 void ForceInitEncodeFailure(bool force_failure) {
717 rtc::CritScope lock(&local_crit_sect_);
718 force_init_encode_failed_ = force_failure;
719 }
720
Niels Möller6bb5ab92019-01-11 11:11:10 +0100721 void SimulateOvershoot(double rate_factor) {
722 rtc::CritScope lock(&local_crit_sect_);
723 rate_factor_ = rate_factor;
724 }
725
Erik Språngd7329ca2019-02-21 21:19:53 +0100726 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100727 rtc::CritScope lock(&local_crit_sect_);
728 return last_framerate_;
729 }
730
Erik Språngd7329ca2019-02-21 21:19:53 +0100731 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100732 rtc::CritScope lock(&local_crit_sect_);
733 return last_update_rect_;
734 }
735
Niels Möller87e2d782019-03-07 10:18:23 +0100736 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100737 rtc::CritScope lock(&local_crit_sect_);
738 return last_frame_types_;
739 }
740
741 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100742 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100743 keyframe ? VideoFrameType::kVideoFrameKey
744 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100745 {
746 rtc::CritScope lock(&local_crit_sect_);
747 last_frame_types_ = frame_type;
748 }
Niels Möllerb859b322019-03-07 12:40:01 +0100749 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100750 }
751
Erik Språngb7cb7b52019-02-26 15:52:33 +0100752 void InjectEncodedImage(const EncodedImage& image) {
753 rtc::CritScope lock(&local_crit_sect_);
754 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
755 }
756
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200757 void InjectEncodedImage(const EncodedImage& image,
758 const CodecSpecificInfo* codec_specific_info,
759 const RTPFragmentationHeader* fragmentation) {
760 rtc::CritScope lock(&local_crit_sect_);
761 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
762 fragmentation);
763 }
764
Erik Språngd7329ca2019-02-21 21:19:53 +0100765 void ExpectNullFrame() {
766 rtc::CritScope lock(&local_crit_sect_);
767 expect_null_frame_ = true;
768 }
769
Erik Språng5056af02019-09-02 15:53:11 +0200770 absl::optional<VideoEncoder::RateControlParameters>
771 GetAndResetLastRateControlSettings() {
772 auto settings = last_rate_control_settings_;
773 last_rate_control_settings_.reset();
774 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100775 }
776
Sergey Silkin5ee69672019-07-02 14:18:34 +0200777 int GetNumEncoderInitializations() const {
778 rtc::CritScope lock(&local_crit_sect_);
779 return num_encoder_initializations_;
780 }
781
perkjfa10b552016-10-02 23:45:26 -0700782 private:
perkj26091b12016-09-01 01:17:40 -0700783 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100784 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700785 bool block_encode;
786 {
brandtre78d2662017-01-16 05:57:16 -0800787 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100788 if (expect_null_frame_) {
789 EXPECT_EQ(input_image.timestamp(), 0u);
790 EXPECT_EQ(input_image.width(), 1);
791 last_frame_types_ = *frame_types;
792 expect_null_frame_ = false;
793 } else {
794 EXPECT_GT(input_image.timestamp(), timestamp_);
795 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
796 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
797 }
perkj26091b12016-09-01 01:17:40 -0700798
799 timestamp_ = input_image.timestamp();
800 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700801 last_input_width_ = input_image.width();
802 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700803 block_encode = block_next_encode_;
804 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100805 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100806 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700807 }
Niels Möllerb859b322019-03-07 12:40:01 +0100808 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700809 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700810 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700811 return result;
812 }
813
sprangfe627f32017-03-29 08:24:59 -0700814 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200815 const Settings& settings) override {
816 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200817
sprangfe627f32017-03-29 08:24:59 -0700818 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100819 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200820
821 ++num_encoder_initializations_;
822
Erik Språng82fad3d2018-03-21 09:57:23 +0100823 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700824 // Simulate setting up temporal layers, in order to validate the life
825 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100826 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200827 frame_buffer_controller_ =
828 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700829 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100830 if (force_init_encode_failed_) {
831 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700832 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100833 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100834
Erik Språngb7cb7b52019-02-26 15:52:33 +0100835 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700836 return res;
837 }
838
Erik Språngb7cb7b52019-02-26 15:52:33 +0100839 int32_t Release() override {
840 rtc::CritScope lock(&local_crit_sect_);
841 EXPECT_NE(initialized_, EncoderState::kUninitialized);
842 initialized_ = EncoderState::kUninitialized;
843 return FakeEncoder::Release();
844 }
845
Erik Språng16cb8f52019-04-12 13:59:09 +0200846 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100847 rtc::CritScope lock(&local_crit_sect_);
848 VideoBitrateAllocation adjusted_rate_allocation;
849 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
850 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200851 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100852 adjusted_rate_allocation.SetBitrate(
853 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200854 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100855 rate_factor_));
856 }
857 }
858 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200859 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200860 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200861 RateControlParameters adjusted_paramters = parameters;
862 adjusted_paramters.bitrate = adjusted_rate_allocation;
863 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100864 }
865
brandtre78d2662017-01-16 05:57:16 -0800866 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100867 enum class EncoderState {
868 kUninitialized,
869 kInitializationFailed,
870 kInitialized
871 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
872 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700873 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700874 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700875 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
876 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
877 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
878 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
879 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100880 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100881 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700882 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100883 absl::optional<bool>
884 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
885 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700886 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100887 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
888 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200889 absl::optional<VideoEncoder::RateControlParameters>
890 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100891 VideoFrame::UpdateRect last_update_rect_
892 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100893 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100894 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100895 EncodedImageCallback* encoded_image_callback_
896 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200897 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200898 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200899 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
900 RTC_GUARDED_BY(local_crit_sect_);
perkj26091b12016-09-01 01:17:40 -0700901 };
902
mflodmancc3d4422017-08-03 08:27:51 -0700903 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700904 public:
905 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100906 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700907
perkj26091b12016-09-01 01:17:40 -0700908 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700909 EXPECT_TRUE(
910 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
911 }
912
913 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
914 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700915 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700916 if (!encoded_frame_event_.Wait(timeout_ms))
917 return false;
perkj26091b12016-09-01 01:17:40 -0700918 {
919 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800920 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700921 }
922 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700923 return true;
perkj26091b12016-09-01 01:17:40 -0700924 }
925
sprangb1ca0732017-02-01 08:38:12 -0800926 void WaitForEncodedFrame(uint32_t expected_width,
927 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700928 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100929 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700930 }
931
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100932 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700933 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800934 uint32_t width = 0;
935 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800936 {
937 rtc::CritScope lock(&crit_);
938 width = last_width_;
939 height = last_height_;
940 }
941 EXPECT_EQ(expected_height, height);
942 EXPECT_EQ(expected_width, width);
943 }
944
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +0200945 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
946 VideoRotation rotation;
947 {
948 rtc::CritScope lock(&crit_);
949 rotation = last_rotation_;
950 }
951 EXPECT_EQ(expected_rotation, rotation);
952 }
953
kthelgason2fc52542017-03-03 00:24:41 -0800954 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -0800955
sprangc5d62e22017-04-02 23:53:04 -0700956 bool WaitForFrame(int64_t timeout_ms) {
957 return encoded_frame_event_.Wait(timeout_ms);
958 }
959
perkj26091b12016-09-01 01:17:40 -0700960 void SetExpectNoFrames() {
961 rtc::CritScope lock(&crit_);
962 expect_frames_ = false;
963 }
964
asaperssonfab67072017-04-04 05:51:49 -0700965 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +0200966 rtc::CritScope lock(&crit_);
967 return number_of_reconfigurations_;
968 }
969
asaperssonfab67072017-04-04 05:51:49 -0700970 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +0200971 rtc::CritScope lock(&crit_);
972 return min_transmit_bitrate_bps_;
973 }
974
Erik Språngd7329ca2019-02-21 21:19:53 +0100975 void SetNumExpectedLayers(size_t num_layers) {
976 rtc::CritScope lock(&crit_);
977 num_expected_layers_ = num_layers;
978 }
979
Erik Språngb7cb7b52019-02-26 15:52:33 +0100980 int64_t GetLastCaptureTimeMs() const {
981 rtc::CritScope lock(&crit_);
982 return last_capture_time_ms_;
983 }
984
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200985 std::vector<uint8_t> GetLastEncodedImageData() {
986 rtc::CritScope lock(&crit_);
987 return std::move(last_encoded_image_data_);
988 }
989
990 RTPFragmentationHeader GetLastFragmentation() {
991 rtc::CritScope lock(&crit_);
992 return std::move(last_fragmentation_);
993 }
994
perkj26091b12016-09-01 01:17:40 -0700995 private:
sergeyu2cb155a2016-11-04 11:39:29 -0700996 Result OnEncodedImage(
997 const EncodedImage& encoded_image,
998 const CodecSpecificInfo* codec_specific_info,
999 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001000 rtc::CritScope lock(&crit_);
1001 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001002 last_encoded_image_data_ = std::vector<uint8_t>(
1003 encoded_image.data(), encoded_image.data() + encoded_image.size());
1004 if (fragmentation) {
1005 last_fragmentation_.CopyFrom(*fragmentation);
1006 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001007 uint32_t timestamp = encoded_image.Timestamp();
1008 if (last_timestamp_ != timestamp) {
1009 num_received_layers_ = 1;
1010 } else {
1011 ++num_received_layers_;
1012 }
1013 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001014 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001015 last_width_ = encoded_image._encodedWidth;
1016 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001017 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001018 if (num_received_layers_ == num_expected_layers_) {
1019 encoded_frame_event_.Set();
1020 }
sprangb1ca0732017-02-01 08:38:12 -08001021 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001022 }
1023
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001024 void OnEncoderConfigurationChanged(
1025 std::vector<VideoStream> streams,
1026 VideoEncoderConfig::ContentType content_type,
1027 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001028 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001029 ++number_of_reconfigurations_;
1030 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1031 }
1032
perkj26091b12016-09-01 01:17:40 -07001033 rtc::CriticalSection crit_;
1034 TestEncoder* test_encoder_;
1035 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001036 std::vector<uint8_t> last_encoded_image_data_;
1037 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001038 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001039 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001040 uint32_t last_height_ = 0;
1041 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001042 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001043 size_t num_expected_layers_ = 1;
1044 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001045 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001046 int number_of_reconfigurations_ = 0;
1047 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001048 };
1049
Sergey Silkin5ee69672019-07-02 14:18:34 +02001050 class VideoBitrateAllocatorProxyFactory
1051 : public VideoBitrateAllocatorFactory {
1052 public:
1053 VideoBitrateAllocatorProxyFactory()
1054 : bitrate_allocator_factory_(
1055 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1056
1057 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1058 const VideoCodec& codec) override {
1059 rtc::CritScope lock(&crit_);
1060 codec_config_ = codec;
1061 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1062 }
1063
1064 VideoCodec codec_config() const {
1065 rtc::CritScope lock(&crit_);
1066 return codec_config_;
1067 }
1068
1069 private:
1070 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1071
1072 rtc::CriticalSection crit_;
1073 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1074 };
1075
perkj26091b12016-09-01 01:17:40 -07001076 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001077 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001078 int codec_width_;
1079 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001080 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001081 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001082 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001083 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001084 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001085 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001086 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001087 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001088 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001089 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001090};
1091
mflodmancc3d4422017-08-03 08:27:51 -07001092TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001093 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001094 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1095 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001096 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001097 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001098 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001099 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001100 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001101}
1102
mflodmancc3d4422017-08-03 08:27:51 -07001103TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001104 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001105 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001106 // The encoder will cache up to one frame for a short duration. Adding two
1107 // frames means that the first frame will be dropped and the second frame will
1108 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001109 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001110 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001111 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001112
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);
perkj26091b12016-09-01 01:17:40 -07001116
Sebastian Janssona3177052018-04-10 13:05:49 +02001117 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001118 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001119 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1120
1121 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001122 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001123}
1124
mflodmancc3d4422017-08-03 08:27:51 -07001125TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001126 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001127 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1128 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001129 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001130 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001131
Florent Castellia8336d32019-09-09 13:36:55 +02001132 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1133 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001134 // The encoder will cache up to one frame for a short duration. Adding two
1135 // frames means that the first frame will be dropped and the second frame will
1136 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001137 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001138 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001139
Erik Språng4c6ca302019-04-08 15:14:01 +02001140 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001141 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1142 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001143 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001144 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1145 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001146 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001147}
1148
mflodmancc3d4422017-08-03 08:27:51 -07001149TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001150 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001151 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1152 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001153 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001154 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001155
1156 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001157 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001158
perkja49cbd32016-09-16 07:53:41 -07001159 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001160 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001161 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001162}
1163
mflodmancc3d4422017-08-03 08:27:51 -07001164TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001165 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001166 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1167 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001168
perkja49cbd32016-09-16 07:53:41 -07001169 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001170 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001171
mflodmancc3d4422017-08-03 08:27:51 -07001172 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001173 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001174 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001175 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1176 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001177}
1178
mflodmancc3d4422017-08-03 08:27:51 -07001179TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001180 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001181 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1182 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001183
1184 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001185 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001186 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001187 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1188 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001189 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1190 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001191 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001192 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001193
mflodmancc3d4422017-08-03 08:27:51 -07001194 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001195}
1196
Noah Richards51db4212019-06-12 06:59:12 -07001197TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1198 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001199 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1200 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001201
1202 rtc::Event frame_destroyed_event;
1203 video_source_.IncomingCapturedFrame(
1204 CreateFakeNativeFrame(1, &frame_destroyed_event));
1205 ExpectDroppedFrame();
1206 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1207 video_stream_encoder_->Stop();
1208}
1209
1210TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1211 // Use the cropping factory.
1212 video_encoder_config_.video_stream_factory =
1213 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1214 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1215 kMaxPayloadLength);
1216 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1217
1218 // Capture a frame at codec_width_/codec_height_.
1219 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001220 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1221 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001222 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1223 WaitForEncodedFrame(1);
1224 // The encoder will have been configured once.
1225 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1226 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1227 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1228
1229 // Now send in a fake frame that needs to be cropped as the width/height
1230 // aren't divisible by 4 (see CreateEncoderStreams above).
1231 rtc::Event frame_destroyed_event;
1232 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1233 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1234 ExpectDroppedFrame();
1235 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1236 video_stream_encoder_->Stop();
1237}
1238
mflodmancc3d4422017-08-03 08:27:51 -07001239TEST_F(VideoStreamEncoderTest,
1240 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001241 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001242 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1243 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001244 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001245
1246 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001247 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001248 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001249 // The encoder will have been configured once when the first frame is
1250 // received.
1251 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001252
1253 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001254 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001255 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001256 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001257 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001258
1259 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001260 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001261 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001262 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001263 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001264
mflodmancc3d4422017-08-03 08:27:51 -07001265 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001266}
1267
mflodmancc3d4422017-08-03 08:27:51 -07001268TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001269 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001270 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1271 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001272
1273 // Capture a frame and wait for it to synchronize with the encoder thread.
1274 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001275 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001276 // The encoder will have been configured once.
1277 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001278 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1279 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1280
1281 codec_width_ *= 2;
1282 codec_height_ *= 2;
1283 // Capture a frame with a higher resolution and wait for it to synchronize
1284 // with the encoder thread.
1285 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001286 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001287 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1288 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001289 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001290
mflodmancc3d4422017-08-03 08:27:51 -07001291 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001292}
1293
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001294TEST_F(VideoStreamEncoderTest,
1295 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1296 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001297 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1298 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001299
1300 // Capture a frame and wait for it to synchronize with the encoder thread.
1301 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1302 WaitForEncodedFrame(1);
1303
1304 VideoEncoderConfig video_encoder_config;
1305 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1306 // Changing the max payload data length recreates encoder.
1307 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1308 kMaxPayloadLength / 2);
1309
1310 // Capture a frame and wait for it to synchronize with the encoder thread.
1311 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1312 WaitForEncodedFrame(2);
1313 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1314
1315 video_stream_encoder_->Stop();
1316}
1317
Sergey Silkin5ee69672019-07-02 14:18:34 +02001318TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1319 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001320 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1321 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001322
1323 VideoEncoderConfig video_encoder_config;
1324 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1325 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1326 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1327 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1328 kMaxPayloadLength);
1329
1330 // Capture a frame and wait for it to synchronize with the encoder thread.
1331 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1332 WaitForEncodedFrame(1);
1333 // The encoder will have been configured once when the first frame is
1334 // received.
1335 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1336 EXPECT_EQ(kTargetBitrateBps,
1337 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1338 EXPECT_EQ(kStartBitrateBps,
1339 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1340
Sergey Silkin6456e352019-07-08 17:56:40 +02001341 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1342 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001343 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1344 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1345 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1346 kMaxPayloadLength);
1347
1348 // Capture a frame and wait for it to synchronize with the encoder thread.
1349 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1350 WaitForEncodedFrame(2);
1351 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1352 // Bitrate limits have changed - rate allocator should be reconfigured,
1353 // encoder should not be reconfigured.
1354 EXPECT_EQ(kTargetBitrateBps * 2,
1355 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1356 EXPECT_EQ(kStartBitrateBps * 2,
1357 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1358 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1359
1360 video_stream_encoder_->Stop();
1361}
1362
Sergey Silkin6456e352019-07-08 17:56:40 +02001363TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001364 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001365 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001366 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1367 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001368
Sergey Silkin6456e352019-07-08 17:56:40 +02001369 VideoEncoderConfig video_encoder_config;
1370 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1371 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001372 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001373 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1374 kMaxPayloadLength);
1375
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001376 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001377 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001378
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001379 // Get the default bitrate limits and use them as baseline for custom
1380 // application and encoder recommended limits.
1381 const uint32_t kDefaultMinBitrateKbps =
1382 bitrate_allocator_factory_.codec_config().minBitrate;
1383 const uint32_t kDefaultMaxBitrateKbps =
1384 bitrate_allocator_factory_.codec_config().maxBitrate;
1385 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1386 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1387 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1388 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1389
1390 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1391 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1392 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1393 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1394
1395 // Change resolution. This will trigger encoder re-configuration and video
1396 // stream encoder will pick up the bitrate limits recommended by encoder.
1397 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1398 WaitForEncodedFrame(2);
1399 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1400 WaitForEncodedFrame(3);
1401
1402 // App bitrate limits are not set - bitrate limits recommended by encoder
1403 // should be used.
1404 EXPECT_EQ(kEncMaxBitrateKbps,
1405 bitrate_allocator_factory_.codec_config().maxBitrate);
1406 EXPECT_EQ(kEncMinBitrateKbps,
1407 bitrate_allocator_factory_.codec_config().minBitrate);
1408
1409 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1410 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1411 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1412 kMaxPayloadLength);
1413 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1414 WaitForEncodedFrame(4);
1415
1416 // App limited the max bitrate - bitrate limits recommended by encoder should
1417 // not be applied.
1418 EXPECT_EQ(kAppMaxBitrateKbps,
1419 bitrate_allocator_factory_.codec_config().maxBitrate);
1420 EXPECT_EQ(kDefaultMinBitrateKbps,
1421 bitrate_allocator_factory_.codec_config().minBitrate);
1422
1423 video_encoder_config.max_bitrate_bps = 0;
1424 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1425 kAppMinBitrateKbps * 1000;
1426 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1427 kMaxPayloadLength);
1428 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1429 WaitForEncodedFrame(5);
1430
1431 // App limited the min bitrate - bitrate limits recommended by encoder should
1432 // not be applied.
1433 EXPECT_EQ(kDefaultMaxBitrateKbps,
1434 bitrate_allocator_factory_.codec_config().maxBitrate);
1435 EXPECT_EQ(kAppMinBitrateKbps,
1436 bitrate_allocator_factory_.codec_config().minBitrate);
1437
1438 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1439 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1440 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001441 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1442 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001443 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1444 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001445
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001446 // App limited both min and max bitrates - bitrate limits recommended by
1447 // encoder should not be applied.
1448 EXPECT_EQ(kAppMaxBitrateKbps,
1449 bitrate_allocator_factory_.codec_config().maxBitrate);
1450 EXPECT_EQ(kAppMinBitrateKbps,
1451 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001452
1453 video_stream_encoder_->Stop();
1454}
1455
1456TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001457 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001458 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001459 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1460 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001461
1462 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001463 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001464 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001465 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001466 fake_encoder_.SetResolutionBitrateLimits(
1467 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1468
1469 VideoEncoderConfig video_encoder_config;
1470 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1471 video_encoder_config.max_bitrate_bps = 0;
1472 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1473 kMaxPayloadLength);
1474
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001475 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001476 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1477 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001478 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1479 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001480 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1481 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1482
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001483 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001484 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1485 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001486 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1487 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001488 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1489 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1490
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001491 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001492 // encoder for 360p should be used.
1493 video_source_.IncomingCapturedFrame(
1494 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1495 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001496 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1497 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001498 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1499 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1500
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001501 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001502 // ignored.
1503 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1504 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001505 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1506 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001507 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1508 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001509 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1510 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001511 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1512 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1513
1514 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1515 // for 270p should be used.
1516 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1517 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001518 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1519 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001520 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1521 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1522
1523 video_stream_encoder_->Stop();
1524}
1525
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001526TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1527 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001528 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1529 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001530
1531 VideoEncoderConfig video_encoder_config;
1532 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1533 video_encoder_config.max_bitrate_bps = 0;
1534 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1535 kMaxPayloadLength);
1536
1537 // Encode 720p frame to get the default encoder target bitrate.
1538 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1539 WaitForEncodedFrame(1);
1540 const uint32_t kDefaultTargetBitrateFor720pKbps =
1541 bitrate_allocator_factory_.codec_config()
1542 .simulcastStream[0]
1543 .targetBitrate;
1544
1545 // Set the max recommended encoder bitrate to something lower than the default
1546 // target bitrate.
1547 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1548 1280 * 720, 10 * 1000, 10 * 1000,
1549 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1550 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1551
1552 // Change resolution to trigger encoder reinitialization.
1553 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1554 WaitForEncodedFrame(2);
1555 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1556 WaitForEncodedFrame(3);
1557
1558 // Ensure the target bitrate is capped by the max bitrate.
1559 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1560 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1561 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1562 .simulcastStream[0]
1563 .targetBitrate *
1564 1000,
1565 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1566
1567 video_stream_encoder_->Stop();
1568}
1569
mflodmancc3d4422017-08-03 08:27:51 -07001570TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001571 EXPECT_TRUE(video_source_.has_sinks());
1572 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001573 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001574 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001575 EXPECT_FALSE(video_source_.has_sinks());
1576 EXPECT_TRUE(new_video_source.has_sinks());
1577
mflodmancc3d4422017-08-03 08:27:51 -07001578 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001579}
1580
mflodmancc3d4422017-08-03 08:27:51 -07001581TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001582 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001583 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001584 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001585 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001586}
1587
Jonathan Yubc771b72017-12-08 17:04:29 -08001588TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1589 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001590 const int kWidth = 1280;
1591 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001592
1593 // We rely on the automatic resolution adaptation, but we handle framerate
1594 // adaptation manually by mocking the stats proxy.
1595 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001596
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001597 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001598 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001599 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1600 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001601 video_stream_encoder_->SetSource(&video_source_,
1602 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001603 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001604 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001605 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001606 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1607
Jonathan Yubc771b72017-12-08 17:04:29 -08001608 // Adapt down as far as possible.
1609 rtc::VideoSinkWants last_wants;
1610 int64_t t = 1;
1611 int loop_count = 0;
1612 do {
1613 ++loop_count;
1614 last_wants = video_source_.sink_wants();
1615
1616 // Simulate the framerate we've been asked to adapt to.
1617 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1618 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1619 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1620 mock_stats.input_frame_rate = fps;
1621 stats_proxy_->SetMockStats(mock_stats);
1622
1623 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1624 sink_.WaitForEncodedFrame(t);
1625 t += frame_interval_ms;
1626
mflodmancc3d4422017-08-03 08:27:51 -07001627 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001628 VerifyBalancedModeFpsRange(
1629 video_source_.sink_wants(),
1630 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1631 } while (video_source_.sink_wants().max_pixel_count <
1632 last_wants.max_pixel_count ||
1633 video_source_.sink_wants().max_framerate_fps <
1634 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001635
Jonathan Yubc771b72017-12-08 17:04:29 -08001636 // Verify that we've adapted all the way down.
1637 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001638 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001639 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1640 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001641 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001642 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1643 *video_source_.last_sent_height());
1644 EXPECT_EQ(kMinBalancedFramerateFps,
1645 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001646
Jonathan Yubc771b72017-12-08 17:04:29 -08001647 // Adapt back up the same number of times we adapted down.
1648 for (int i = 0; i < loop_count - 1; ++i) {
1649 last_wants = video_source_.sink_wants();
1650
1651 // Simulate the framerate we've been asked to adapt to.
1652 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1653 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1654 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1655 mock_stats.input_frame_rate = fps;
1656 stats_proxy_->SetMockStats(mock_stats);
1657
1658 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1659 sink_.WaitForEncodedFrame(t);
1660 t += frame_interval_ms;
1661
mflodmancc3d4422017-08-03 08:27:51 -07001662 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001663 VerifyBalancedModeFpsRange(
1664 video_source_.sink_wants(),
1665 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1666 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1667 last_wants.max_pixel_count ||
1668 video_source_.sink_wants().max_framerate_fps >
1669 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001670 }
1671
Åsa Persson8c1bf952018-09-13 10:42:19 +02001672 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001673 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001674 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001675 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1676 EXPECT_EQ((loop_count - 1) * 2,
1677 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001678
mflodmancc3d4422017-08-03 08:27:51 -07001679 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001680}
mflodmancc3d4422017-08-03 08:27:51 -07001681TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001682 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001683 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1684 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001685 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001686
sprangc5d62e22017-04-02 23:53:04 -07001687 const int kFrameWidth = 1280;
1688 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001689
Åsa Persson8c1bf952018-09-13 10:42:19 +02001690 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001691
kthelgason5e13d412016-12-01 03:59:51 -08001692 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001693 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001694 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001695 frame_timestamp += kFrameIntervalMs;
1696
perkj803d97f2016-11-01 11:45:46 -07001697 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001698 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001699 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001700 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001701 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001702 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001703
asapersson0944a802017-04-07 00:57:58 -07001704 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001705 // wanted resolution.
1706 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1707 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1708 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001709 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001710
1711 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001712 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001713 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001714 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001715
sprangc5d62e22017-04-02 23:53:04 -07001716 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001717 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001718
sprangc5d62e22017-04-02 23:53:04 -07001719 // Force an input frame rate to be available, or the adaptation call won't
1720 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001721 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001722 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001723 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001724 stats_proxy_->SetMockStats(stats);
1725
mflodmancc3d4422017-08-03 08:27:51 -07001726 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001727 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001728 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001729 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001730 frame_timestamp += kFrameIntervalMs;
1731
1732 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001733 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001734 EXPECT_EQ(std::numeric_limits<int>::max(),
1735 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001736 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001737
asapersson02465b82017-04-10 01:12:52 -07001738 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001739 video_stream_encoder_->SetSource(&new_video_source,
1740 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001741 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001742
mflodmancc3d4422017-08-03 08:27:51 -07001743 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001744 new_video_source.IncomingCapturedFrame(
1745 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001746 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001747 frame_timestamp += kFrameIntervalMs;
1748
1749 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001750 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001751
1752 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001753 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001754 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001755 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1756 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001757 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001758 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001759
1760 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001761 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001762 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001763 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1764 EXPECT_EQ(std::numeric_limits<int>::max(),
1765 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001766 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001767
mflodmancc3d4422017-08-03 08:27:51 -07001768 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001769}
1770
mflodmancc3d4422017-08-03 08:27:51 -07001771TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001772 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001773 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1774 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001775
asaperssonfab67072017-04-04 05:51:49 -07001776 const int kWidth = 1280;
1777 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001778 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001779 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001780 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1781 EXPECT_FALSE(stats.bw_limited_resolution);
1782 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1783
1784 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001785 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001786 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001787 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001788
1789 stats = stats_proxy_->GetStats();
1790 EXPECT_TRUE(stats.bw_limited_resolution);
1791 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1792
1793 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001794 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001795 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001796 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001797
1798 stats = stats_proxy_->GetStats();
1799 EXPECT_FALSE(stats.bw_limited_resolution);
1800 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1801 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1802
mflodmancc3d4422017-08-03 08:27:51 -07001803 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001804}
1805
mflodmancc3d4422017-08-03 08:27:51 -07001806TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001807 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001808 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1809 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001810
1811 const int kWidth = 1280;
1812 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001813 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001814 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001815 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1816 EXPECT_FALSE(stats.cpu_limited_resolution);
1817 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1818
1819 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001820 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001821 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001822 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001823
1824 stats = stats_proxy_->GetStats();
1825 EXPECT_TRUE(stats.cpu_limited_resolution);
1826 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1827
1828 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001829 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001830 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001831 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001832
1833 stats = stats_proxy_->GetStats();
1834 EXPECT_FALSE(stats.cpu_limited_resolution);
1835 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001836 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001837
mflodmancc3d4422017-08-03 08:27:51 -07001838 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001839}
1840
mflodmancc3d4422017-08-03 08:27:51 -07001841TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001842 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001843 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1844 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001845
asaperssonfab67072017-04-04 05:51:49 -07001846 const int kWidth = 1280;
1847 const int kHeight = 720;
1848 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001849 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001850 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001851 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001852 EXPECT_FALSE(stats.cpu_limited_resolution);
1853 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1854
asaperssonfab67072017-04-04 05:51:49 -07001855 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001856 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001857 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001858 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001859 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001860 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001861 EXPECT_TRUE(stats.cpu_limited_resolution);
1862 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1863
1864 // Set new source with adaptation still enabled.
1865 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001866 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001867 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001868
asaperssonfab67072017-04-04 05:51:49 -07001869 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001870 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001871 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001872 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001873 EXPECT_TRUE(stats.cpu_limited_resolution);
1874 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1875
1876 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001877 video_stream_encoder_->SetSource(&new_video_source,
1878 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001879
asaperssonfab67072017-04-04 05:51:49 -07001880 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001881 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001882 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001883 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001884 EXPECT_FALSE(stats.cpu_limited_resolution);
1885 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1886
1887 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001888 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001889 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001890
asaperssonfab67072017-04-04 05:51:49 -07001891 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001892 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001893 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001894 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001895 EXPECT_TRUE(stats.cpu_limited_resolution);
1896 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1897
asaperssonfab67072017-04-04 05:51:49 -07001898 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001899 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001900 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001901 WaitForEncodedFrame(6);
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(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001906 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001907
mflodmancc3d4422017-08-03 08:27:51 -07001908 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001909}
1910
mflodmancc3d4422017-08-03 08:27:51 -07001911TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001912 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001913 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1914 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001915
asaperssonfab67072017-04-04 05:51:49 -07001916 const int kWidth = 1280;
1917 const int kHeight = 720;
1918 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001919 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001920 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001921 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001922 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001923 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001924
1925 // Set new source with adaptation still enabled.
1926 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001927 video_stream_encoder_->SetSource(&new_video_source,
1928 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001929
asaperssonfab67072017-04-04 05:51:49 -07001930 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001931 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001932 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001933 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001934 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001935 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001936
asaperssonfab67072017-04-04 05:51:49 -07001937 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001938 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001939 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001940 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001941 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001942 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001943 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001944 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001945
asaperssonfab67072017-04-04 05:51:49 -07001946 // Set new source with adaptation still enabled.
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(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001951 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001952 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001953 EXPECT_TRUE(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(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001956
asapersson02465b82017-04-10 01:12:52 -07001957 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07001958 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001959 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08001960
asaperssonfab67072017-04-04 05:51:49 -07001961 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001962 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001963 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001964 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001965 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001966 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1967 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001968
mflodmancc3d4422017-08-03 08:27:51 -07001969 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001970}
1971
mflodmancc3d4422017-08-03 08:27:51 -07001972TEST_F(VideoStreamEncoderTest,
1973 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001974 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001975 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1976 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07001977
1978 const int kWidth = 1280;
1979 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02001980 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07001981 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001982 video_source_.IncomingCapturedFrame(
1983 CreateFrame(timestamp_ms, kWidth, kHeight));
1984 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07001985 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1986 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1987 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1988
1989 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001990 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02001991 timestamp_ms += kFrameIntervalMs;
1992 video_source_.IncomingCapturedFrame(
1993 CreateFrame(timestamp_ms, kWidth, kHeight));
1994 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07001995 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1996 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1997 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1998
1999 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002000 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002001 timestamp_ms += kFrameIntervalMs;
2002 video_source_.IncomingCapturedFrame(
2003 CreateFrame(timestamp_ms, kWidth, kHeight));
2004 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002005 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2006 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2007 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2008
Niels Möller4db138e2018-04-19 09:04:13 +02002009 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002010 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002011
2012 VideoEncoderConfig video_encoder_config;
2013 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2014 // Make format different, to force recreation of encoder.
2015 video_encoder_config.video_format.parameters["foo"] = "foo";
2016 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002017 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002018 timestamp_ms += kFrameIntervalMs;
2019 video_source_.IncomingCapturedFrame(
2020 CreateFrame(timestamp_ms, kWidth, kHeight));
2021 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002022 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2023 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2024 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2025
mflodmancc3d4422017-08-03 08:27:51 -07002026 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002027}
2028
mflodmancc3d4422017-08-03 08:27:51 -07002029TEST_F(VideoStreamEncoderTest,
2030 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002031 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002032 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2033 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002034
asapersson0944a802017-04-07 00:57:58 -07002035 const int kWidth = 1280;
2036 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002037 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002038
asaperssonfab67072017-04-04 05:51:49 -07002039 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002040 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002041 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002042 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002043 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002044 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2045
asapersson02465b82017-04-10 01:12:52 -07002046 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002047 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002048 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002049 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002050 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002051 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002052 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002053 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2054
2055 // Set new source with adaptation still enabled.
2056 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002057 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002058 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002059
2060 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002061 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002062 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002063 stats = stats_proxy_->GetStats();
2064 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002065 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002066 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2067
sprangc5d62e22017-04-02 23:53:04 -07002068 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002069 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002070 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002071 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002072 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002073 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002074 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002075 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002076 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002077 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002078 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2079
sprangc5d62e22017-04-02 23:53:04 -07002080 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002081 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002082 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2083 mock_stats.input_frame_rate = 30;
2084 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002085 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002086 stats_proxy_->ResetMockStats();
2087
2088 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002089 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002090 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002091
2092 // Framerate now adapted.
2093 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002094 EXPECT_FALSE(stats.cpu_limited_resolution);
2095 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002096 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2097
2098 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002099 video_stream_encoder_->SetSource(&new_video_source,
2100 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002101 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002102 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002103 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002104
2105 stats = stats_proxy_->GetStats();
2106 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002107 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002108 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2109
2110 // Try to trigger overuse. Should not succeed.
2111 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002112 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002113 stats_proxy_->ResetMockStats();
2114
2115 stats = stats_proxy_->GetStats();
2116 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002117 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002118 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2119
2120 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002121 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002122 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002123 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002124 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002125 stats = stats_proxy_->GetStats();
2126 EXPECT_TRUE(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);
perkj803d97f2016-11-01 11:45:46 -07002129
2130 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002131 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002132 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002133 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002134 stats = stats_proxy_->GetStats();
2135 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002136 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002137 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2138
2139 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002140 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002141 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002142 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002143 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002144 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002145 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002146 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002147 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002148 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002149 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2150
2151 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002152 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002153 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002154 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002155 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002156 stats = stats_proxy_->GetStats();
2157 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002158 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002159 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002160 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002161
mflodmancc3d4422017-08-03 08:27:51 -07002162 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002163}
2164
mflodmancc3d4422017-08-03 08:27:51 -07002165TEST_F(VideoStreamEncoderTest,
2166 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002167 const int kWidth = 1280;
2168 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002169 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002170 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2171 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002172
asaperssonfab67072017-04-04 05:51:49 -07002173 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002174 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002175
asaperssonfab67072017-04-04 05:51:49 -07002176 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002177 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002178
asaperssonfab67072017-04-04 05:51:49 -07002179 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002180 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002181
asaperssonfab67072017-04-04 05:51:49 -07002182 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002183 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002184
kthelgason876222f2016-11-29 01:44:11 -08002185 // Expect a scale down.
2186 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002187 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002188
asapersson02465b82017-04-10 01:12:52 -07002189 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002190 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002191 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002192 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002193
asaperssonfab67072017-04-04 05:51:49 -07002194 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002195 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002196 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002197 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002198
asaperssonfab67072017-04-04 05:51:49 -07002199 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002200 EXPECT_EQ(std::numeric_limits<int>::max(),
2201 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002202
asaperssonfab67072017-04-04 05:51:49 -07002203 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002204 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002205 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002206 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002207
asapersson02465b82017-04-10 01:12:52 -07002208 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002209 EXPECT_EQ(std::numeric_limits<int>::max(),
2210 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002211
mflodmancc3d4422017-08-03 08:27:51 -07002212 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002213}
2214
mflodmancc3d4422017-08-03 08:27:51 -07002215TEST_F(VideoStreamEncoderTest,
2216 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002217 const int kWidth = 1280;
2218 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002219 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002220 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2221 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002222
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002223 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002224 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002225 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002226 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002227
2228 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002229 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002230 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002231 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2232 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2233
2234 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002235 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002236 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002237 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2238 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2239 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2240
2241 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002242 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002243 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2244 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2245 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2246
mflodmancc3d4422017-08-03 08:27:51 -07002247 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002248}
2249
mflodmancc3d4422017-08-03 08:27:51 -07002250TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002251 const int kWidth = 1280;
2252 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002253 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002254 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2255 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002256
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002257 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002258 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002259 video_stream_encoder_->SetSource(&source,
2260 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002261 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2262 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002263 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002264
2265 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002266 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002267 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2268 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2269 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2270 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2271
2272 // Trigger adapt down for same input resolution, expect no change.
2273 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2274 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002275 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002276 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2277 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2278 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2279
2280 // Trigger adapt down for larger input resolution, expect no change.
2281 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2282 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002283 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002284 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2285 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2286 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2287
mflodmancc3d4422017-08-03 08:27:51 -07002288 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002289}
2290
mflodmancc3d4422017-08-03 08:27:51 -07002291TEST_F(VideoStreamEncoderTest,
2292 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002293 const int kWidth = 1280;
2294 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002295 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002296 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2297 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002298
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002299 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002300 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002301 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002302 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002303
2304 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002305 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002306 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002307 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2308 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2309
2310 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002311 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002312 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002313 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2314 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2315
mflodmancc3d4422017-08-03 08:27:51 -07002316 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002317}
2318
mflodmancc3d4422017-08-03 08:27:51 -07002319TEST_F(VideoStreamEncoderTest,
2320 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -07002321 const int kWidth = 1280;
2322 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002323 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002324 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2325 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002326
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002327 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002328 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002329 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002330 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002331
2332 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002333 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002334 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002335 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002336 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2337
2338 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002339 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002340 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002341 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002342 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2343
mflodmancc3d4422017-08-03 08:27:51 -07002344 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002345}
2346
mflodmancc3d4422017-08-03 08:27:51 -07002347TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002348 const int kWidth = 1280;
2349 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002350 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002351 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2352 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002353
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002354 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002355 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002356 video_stream_encoder_->SetSource(&source,
2357 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002358
2359 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2360 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002361 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002362 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2363 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2364 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2365
2366 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002367 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002368 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002369 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2370 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2371 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2372
mflodmancc3d4422017-08-03 08:27:51 -07002373 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002374}
2375
mflodmancc3d4422017-08-03 08:27:51 -07002376TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002377 const int kWidth = 1280;
2378 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002379 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002380 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2381 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002382
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002383 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002384 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002385 video_stream_encoder_->SetSource(&source,
2386 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002387
2388 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2389 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002390 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002391 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2392 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2393 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2394
2395 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002396 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002397 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002398 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2399 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2400 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2401
mflodmancc3d4422017-08-03 08:27:51 -07002402 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002403}
2404
mflodmancc3d4422017-08-03 08:27:51 -07002405TEST_F(VideoStreamEncoderTest,
2406 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002407 const int kWidth = 1280;
2408 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002409 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002410 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2411 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002412
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002413 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002414 AdaptingFrameForwarder source;
2415 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002416 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002417 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002418
2419 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002420 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002421 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002422 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2423 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2424
2425 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002426 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002427 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002428 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002429 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002430 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2431 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2432
2433 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002434 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002435 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002436 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2437 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2438 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2439
mflodmancc3d4422017-08-03 08:27:51 -07002440 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002441}
2442
mflodmancc3d4422017-08-03 08:27:51 -07002443TEST_F(VideoStreamEncoderTest,
2444 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002445 const int kWidth = 1280;
2446 const int kHeight = 720;
2447 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002448 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002449 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2450 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002451
2452 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2453 stats.input_frame_rate = kInputFps;
2454 stats_proxy_->SetMockStats(stats);
2455
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002456 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002457 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2458 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002459 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002460
2461 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002462 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002463 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2464 sink_.WaitForEncodedFrame(2);
2465 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2466
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002467 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002468 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002469 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002470 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002471 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002472
2473 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002474 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002475 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2476 sink_.WaitForEncodedFrame(3);
2477 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2478
2479 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002480 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002481 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002482
mflodmancc3d4422017-08-03 08:27:51 -07002483 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002484}
2485
mflodmancc3d4422017-08-03 08:27:51 -07002486TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002487 const int kWidth = 1280;
2488 const int kHeight = 720;
2489 const size_t kNumFrames = 10;
2490
Erik Språng4c6ca302019-04-08 15:14:01 +02002491 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002492 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2493 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002494
asaperssond0de2952017-04-21 01:47:31 -07002495 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002496 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002497 video_source_.set_adaptation_enabled(true);
2498
2499 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2500 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2501
2502 int downscales = 0;
2503 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002504 video_source_.IncomingCapturedFrame(
2505 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2506 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002507
asaperssonfab67072017-04-04 05:51:49 -07002508 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002509 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002510 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002511 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002512
2513 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2514 ++downscales;
2515
2516 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2517 EXPECT_EQ(downscales,
2518 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2519 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002520 }
mflodmancc3d4422017-08-03 08:27:51 -07002521 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002522}
2523
mflodmancc3d4422017-08-03 08:27:51 -07002524TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002525 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2526 const int kWidth = 1280;
2527 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002528 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002529 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2530 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002531
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002532 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002533 AdaptingFrameForwarder source;
2534 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002535 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002536 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002537
Åsa Persson8c1bf952018-09-13 10:42:19 +02002538 int64_t timestamp_ms = kFrameIntervalMs;
2539 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002540 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002541 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002542 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2543 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2544
2545 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002546 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002547 timestamp_ms += kFrameIntervalMs;
2548 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2549 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002550 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002551 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2552 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2553
2554 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002555 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002556 timestamp_ms += kFrameIntervalMs;
2557 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002558 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002559 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002560 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2561 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2562
2563 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002564 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002565 timestamp_ms += kFrameIntervalMs;
2566 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2567 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002568 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002569 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2570 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2571
2572 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002573 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002574 timestamp_ms += kFrameIntervalMs;
2575 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002576 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002577 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002578 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2579 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2580
mflodmancc3d4422017-08-03 08:27:51 -07002581 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002582}
2583
mflodmancc3d4422017-08-03 08:27:51 -07002584TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002585 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2586 const int kWidth = 1280;
2587 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002588 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002589 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2590 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002591
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002592 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002593 AdaptingFrameForwarder source;
2594 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002595 video_stream_encoder_->SetSource(&source,
2596 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002597
Åsa Persson8c1bf952018-09-13 10:42:19 +02002598 int64_t timestamp_ms = kFrameIntervalMs;
2599 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002600 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002601 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002602 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2603 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2604
2605 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002606 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002607 timestamp_ms += kFrameIntervalMs;
2608 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2609 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002610 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2611 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2612 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2613
2614 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002615 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002616 timestamp_ms += kFrameIntervalMs;
2617 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002618 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002619 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002620 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2621 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2622
2623 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002624 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002625 timestamp_ms += kFrameIntervalMs;
2626 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2627 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002628 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2629 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2630 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2631
2632 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002633 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002634 timestamp_ms += kFrameIntervalMs;
2635 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002636 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002637 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002638 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2639 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2640
mflodmancc3d4422017-08-03 08:27:51 -07002641 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002642}
2643
Åsa Persson1b247f12019-08-14 17:26:39 +02002644TEST_F(VideoStreamEncoderTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
2645 webrtc::test::ScopedFieldTrials field_trials(
2646 "WebRTC-Video-BalancedDegradationSettings/"
2647 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
2648 // Reset encoder for field trials to take effect.
2649 ConfigureEncoder(video_encoder_config_.Copy());
2650
2651 const int kWidth = 640; // pixels:640x360=230400
2652 const int kHeight = 360;
2653 const int64_t kFrameIntervalMs = 150;
2654 const int kMinBitrateBps = 425000;
2655 const int kTooLowMinBitrateBps = 424000;
Florent Castellia8336d32019-09-09 13:36:55 +02002656 video_stream_encoder_->OnBitrateUpdated(
2657 DataRate::bps(kTooLowMinBitrateBps), DataRate::bps(kTooLowMinBitrateBps),
2658 DataRate::bps(kTooLowMinBitrateBps), 0, 0);
Åsa Persson1b247f12019-08-14 17:26:39 +02002659
2660 // Enable BALANCED preference, no initial limitation.
2661 AdaptingFrameForwarder source;
2662 source.set_adaptation_enabled(true);
2663 video_stream_encoder_->SetSource(&source,
2664 webrtc::DegradationPreference::BALANCED);
2665
2666 int64_t timestamp_ms = kFrameIntervalMs;
2667 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2668 sink_.WaitForEncodedFrame(kWidth, kHeight);
2669 VerifyFpsMaxResolutionMax(source.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002670 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2671
2672 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2673 video_stream_encoder_->TriggerQualityLow();
2674 timestamp_ms += kFrameIntervalMs;
2675 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2676 sink_.WaitForEncodedFrame(timestamp_ms);
2677 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002678 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2679
2680 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2681 video_stream_encoder_->TriggerQualityLow();
2682 timestamp_ms += kFrameIntervalMs;
2683 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2684 sink_.WaitForEncodedFrame(timestamp_ms);
2685 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002686 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2687
Åsa Persson30ab0152019-08-27 12:22:33 +02002688 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2689 video_stream_encoder_->TriggerQualityLow();
2690 timestamp_ms += kFrameIntervalMs;
2691 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2692 sink_.WaitForEncodedFrame(timestamp_ms);
2693 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
2694 EXPECT_EQ(source.sink_wants().max_framerate_fps, 10);
2695 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2696
2697 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002698 video_stream_encoder_->TriggerQualityHigh();
2699 timestamp_ms += kFrameIntervalMs;
2700 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2701 sink_.WaitForEncodedFrame(timestamp_ms);
Åsa Persson30ab0152019-08-27 12:22:33 +02002702 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002703
Åsa Persson30ab0152019-08-27 12:22:33 +02002704 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002705 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002706 DataRate::bps(kMinBitrateBps),
Åsa Persson1b247f12019-08-14 17:26:39 +02002707 DataRate::bps(kMinBitrateBps), 0, 0);
2708 video_stream_encoder_->TriggerQualityHigh();
2709 timestamp_ms += kFrameIntervalMs;
2710 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2711 sink_.WaitForEncodedFrame(timestamp_ms);
Åsa Persson30ab0152019-08-27 12:22:33 +02002712 EXPECT_EQ(source.sink_wants().max_framerate_fps, 14);
2713 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2714
2715 video_stream_encoder_->Stop();
2716}
2717
2718TEST_F(VideoStreamEncoderTest,
2719 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
2720 webrtc::test::ScopedFieldTrials field_trials(
2721 "WebRTC-Video-BalancedDegradationSettings/"
2722 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
2723 // Reset encoder for field trials to take effect.
2724 ConfigureEncoder(video_encoder_config_.Copy());
2725
2726 const int kWidth = 640; // pixels:640x360=230400
2727 const int kHeight = 360;
2728 const int64_t kFrameIntervalMs = 150;
2729 const int kResolutionMinBitrateBps = 435000;
2730 const int kTooLowMinResolutionBitrateBps = 434000;
2731 video_stream_encoder_->OnBitrateUpdated(
2732 DataRate::bps(kTooLowMinResolutionBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002733 DataRate::bps(kTooLowMinResolutionBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002734 DataRate::bps(kTooLowMinResolutionBitrateBps), 0, 0);
2735
2736 // Enable BALANCED preference, no initial limitation.
2737 AdaptingFrameForwarder source;
2738 source.set_adaptation_enabled(true);
2739 video_stream_encoder_->SetSource(&source,
2740 webrtc::DegradationPreference::BALANCED);
2741
2742 int64_t timestamp_ms = kFrameIntervalMs;
2743 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2744 sink_.WaitForEncodedFrame(kWidth, kHeight);
2745 VerifyFpsMaxResolutionMax(source.sink_wants());
2746 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2747
2748 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2749 video_stream_encoder_->TriggerQualityLow();
2750 timestamp_ms += kFrameIntervalMs;
2751 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2752 sink_.WaitForEncodedFrame(timestamp_ms);
2753 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
2754 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2755
2756 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2757 video_stream_encoder_->TriggerQualityLow();
2758 timestamp_ms += kFrameIntervalMs;
2759 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2760 sink_.WaitForEncodedFrame(timestamp_ms);
2761 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
2762 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2763
2764 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2765 video_stream_encoder_->TriggerQualityLow();
2766 timestamp_ms += kFrameIntervalMs;
2767 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2768 sink_.WaitForEncodedFrame(timestamp_ms);
2769 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002770 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2771
Åsa Persson30ab0152019-08-27 12:22:33 +02002772 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2773 video_stream_encoder_->TriggerQualityHigh();
2774 timestamp_ms += kFrameIntervalMs;
2775 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2776 sink_.WaitForEncodedFrame(timestamp_ms);
2777 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
2778 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2779
2780 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2781 video_stream_encoder_->TriggerQualityHigh();
2782 timestamp_ms += kFrameIntervalMs;
2783 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2784 sink_.WaitForEncodedFrame(timestamp_ms);
2785 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2786
2787 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
2788 video_stream_encoder_->OnBitrateUpdated(
2789 DataRate::bps(kResolutionMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002790 DataRate::bps(kResolutionMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002791 DataRate::bps(kResolutionMinBitrateBps), 0, 0);
2792 video_stream_encoder_->TriggerQualityHigh();
2793 timestamp_ms += kFrameIntervalMs;
2794 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2795 sink_.WaitForEncodedFrame(timestamp_ms);
2796 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
2797 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2798
2799 video_stream_encoder_->Stop();
2800}
2801
2802TEST_F(VideoStreamEncoderTest,
2803 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
2804 webrtc::test::ScopedFieldTrials field_trials(
2805 "WebRTC-Video-BalancedDegradationSettings/"
2806 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
2807 // Reset encoder for field trials to take effect.
2808 ConfigureEncoder(video_encoder_config_.Copy());
2809
2810 const int kWidth = 640; // pixels:640x360=230400
2811 const int kHeight = 360;
2812 const int64_t kFrameIntervalMs = 150;
2813 const int kMinBitrateBps = 425000;
2814 const int kTooLowMinBitrateBps = 424000;
2815 const int kResolutionMinBitrateBps = 435000;
2816 const int kTooLowMinResolutionBitrateBps = 434000;
Florent Castellia8336d32019-09-09 13:36:55 +02002817 video_stream_encoder_->OnBitrateUpdated(
2818 DataRate::bps(kTooLowMinBitrateBps), DataRate::bps(kTooLowMinBitrateBps),
2819 DataRate::bps(kTooLowMinBitrateBps), 0, 0);
Åsa Persson30ab0152019-08-27 12:22:33 +02002820
2821 // Enable BALANCED preference, no initial limitation.
2822 AdaptingFrameForwarder source;
2823 source.set_adaptation_enabled(true);
2824 video_stream_encoder_->SetSource(&source,
2825 webrtc::DegradationPreference::BALANCED);
2826
2827 int64_t timestamp_ms = kFrameIntervalMs;
2828 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2829 sink_.WaitForEncodedFrame(kWidth, kHeight);
2830 VerifyFpsMaxResolutionMax(source.sink_wants());
2831 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2832
2833 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2834 video_stream_encoder_->TriggerQualityLow();
2835 timestamp_ms += kFrameIntervalMs;
2836 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2837 sink_.WaitForEncodedFrame(timestamp_ms);
2838 VerifyFpsEqResolutionMax(source.sink_wants(), 14);
2839 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2840
2841 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2842 video_stream_encoder_->TriggerQualityLow();
2843 timestamp_ms += kFrameIntervalMs;
2844 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2845 sink_.WaitForEncodedFrame(timestamp_ms);
2846 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
2847 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2848
2849 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2850 video_stream_encoder_->TriggerQualityLow();
2851 timestamp_ms += kFrameIntervalMs;
2852 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2853 sink_.WaitForEncodedFrame(timestamp_ms);
2854 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
2855 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2856
2857 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
2858 video_stream_encoder_->TriggerQualityHigh();
2859 timestamp_ms += kFrameIntervalMs;
2860 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2861 sink_.WaitForEncodedFrame(timestamp_ms);
2862 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2863
2864 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
2865 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002866 DataRate::bps(kMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002867 DataRate::bps(kMinBitrateBps), 0, 0);
2868 video_stream_encoder_->TriggerQualityHigh();
2869 timestamp_ms += kFrameIntervalMs;
2870 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2871 sink_.WaitForEncodedFrame(timestamp_ms);
2872 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
2873 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2874
2875 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2876 video_stream_encoder_->OnBitrateUpdated(
2877 DataRate::bps(kTooLowMinResolutionBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002878 DataRate::bps(kTooLowMinResolutionBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002879 DataRate::bps(kTooLowMinResolutionBitrateBps), 0, 0);
2880 video_stream_encoder_->TriggerQualityHigh();
2881 timestamp_ms += kFrameIntervalMs;
2882 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2883 sink_.WaitForEncodedFrame(timestamp_ms);
2884 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2885
2886 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
2887 video_stream_encoder_->OnBitrateUpdated(
2888 DataRate::bps(kResolutionMinBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02002889 DataRate::bps(kResolutionMinBitrateBps),
Åsa Persson30ab0152019-08-27 12:22:33 +02002890 DataRate::bps(kResolutionMinBitrateBps), 0, 0);
2891 video_stream_encoder_->TriggerQualityHigh();
2892 timestamp_ms += kFrameIntervalMs;
2893 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2894 sink_.WaitForEncodedFrame(timestamp_ms);
2895 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
2896 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2897
Åsa Persson1b247f12019-08-14 17:26:39 +02002898 video_stream_encoder_->Stop();
2899}
2900
mflodmancc3d4422017-08-03 08:27:51 -07002901TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002902 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
2903 const int kWidth = 1280;
2904 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002905 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002906 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2907 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002908
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002909 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002910 AdaptingFrameForwarder source;
2911 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002912 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002913 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002914
Åsa Persson8c1bf952018-09-13 10:42:19 +02002915 int64_t timestamp_ms = kFrameIntervalMs;
2916 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002917 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002918 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002919 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2920 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2921 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2922 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2923
2924 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07002925 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002926 timestamp_ms += kFrameIntervalMs;
2927 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2928 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002929 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002930 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2931 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2932 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2933 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2934
2935 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07002936 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002937 timestamp_ms += kFrameIntervalMs;
2938 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2939 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002940 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07002941 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2942 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2943 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2944 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2945
Jonathan Yubc771b72017-12-08 17:04:29 -08002946 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07002947 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002948 timestamp_ms += kFrameIntervalMs;
2949 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2950 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08002951 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07002952 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2953 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08002954 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07002955 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2956
Jonathan Yubc771b72017-12-08 17:04:29 -08002957 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07002958 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002959 timestamp_ms += kFrameIntervalMs;
2960 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2961 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002962 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08002963 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07002964 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2965 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2966 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2967 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2968
Jonathan Yubc771b72017-12-08 17:04:29 -08002969 // Trigger quality adapt down, expect no change (min resolution reached).
2970 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002971 timestamp_ms += kFrameIntervalMs;
2972 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2973 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08002974 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
2975 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2976 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2977 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2978 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2979
2980 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07002981 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002982 timestamp_ms += kFrameIntervalMs;
2983 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2984 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002985 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08002986 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2987 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2988 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2989 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2990
2991 // Trigger cpu adapt up, expect upscaled resolution (640x360).
2992 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002993 timestamp_ms += kFrameIntervalMs;
2994 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2995 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08002996 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
2997 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2998 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2999 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3000 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3001
3002 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3003 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003004 timestamp_ms += kFrameIntervalMs;
3005 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3006 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003007 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003008 last_wants = source.sink_wants();
3009 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3010 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003011 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003012 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3013
3014 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003015 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003016 timestamp_ms += kFrameIntervalMs;
3017 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3018 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003019 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003020 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3021 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003022 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003023 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3024
3025 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003026 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003027 timestamp_ms += kFrameIntervalMs;
3028 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003029 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003030 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Å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);
Jonathan Yubc771b72017-12-08 17:04:29 -08003034 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003035 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003036
mflodmancc3d4422017-08-03 08:27:51 -07003037 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003038}
3039
mflodmancc3d4422017-08-03 08:27:51 -07003040TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003041 const int kWidth = 640;
3042 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003043
Erik Språng4c6ca302019-04-08 15:14:01 +02003044 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003045 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3046 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003047
perkj803d97f2016-11-01 11:45:46 -07003048 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003049 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003050 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003051 }
3052
mflodmancc3d4422017-08-03 08:27:51 -07003053 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003054 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003055 video_source_.IncomingCapturedFrame(CreateFrame(
3056 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003057 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003058 }
3059
mflodmancc3d4422017-08-03 08:27:51 -07003060 video_stream_encoder_->Stop();
3061 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003062 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003063
perkj803d97f2016-11-01 11:45:46 -07003064 EXPECT_EQ(1,
3065 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3066 EXPECT_EQ(
3067 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3068}
3069
mflodmancc3d4422017-08-03 08:27:51 -07003070TEST_F(VideoStreamEncoderTest,
3071 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003072 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003073 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3074 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003075 const int kWidth = 640;
3076 const int kHeight = 360;
3077
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003078 video_stream_encoder_->SetSource(&video_source_,
3079 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003080
3081 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3082 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003083 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003084 }
3085
mflodmancc3d4422017-08-03 08:27:51 -07003086 video_stream_encoder_->Stop();
3087 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003088 stats_proxy_.reset();
3089
3090 EXPECT_EQ(0,
3091 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3092}
3093
mflodmancc3d4422017-08-03 08:27:51 -07003094TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003095 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003096 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003097
3098 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003099 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003100 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003101 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3102 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003103
sprang57c2fff2017-01-16 06:24:02 -08003104 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003105 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003106 video_stream_encoder_->OnBitrateUpdated(
3107 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3108 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003109
sprang57c2fff2017-01-16 06:24:02 -08003110 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003111 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3112 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003113 VideoBitrateAllocation bitrate_allocation =
3114 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003115 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003116 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003117 // TODO(srte): The use of millisecs here looks like an error, but the tests
3118 // fails using seconds, this should be investigated.
3119 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003120
3121 // Not called on second frame.
3122 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3123 .Times(0);
3124 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003125 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3126 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003127 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003128
3129 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003130 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3131 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003132 const int64_t start_time_ms = rtc::TimeMillis();
3133 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3134 video_source_.IncomingCapturedFrame(
3135 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3136 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003137 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003138 }
3139
3140 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003141 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003142
mflodmancc3d4422017-08-03 08:27:51 -07003143 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003144}
3145
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003146TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3147 // 2 TLs configured, temporal layers supported by encoder.
3148 const int kNumTemporalLayers = 2;
3149 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3150 fake_encoder_.SetTemporalLayersSupported(0, true);
3151
3152 // Bitrate allocated across temporal layers.
3153 const int kTl0Bps = kTargetBitrateBps *
3154 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3155 kNumTemporalLayers, /*temporal_id*/ 0);
3156 const int kTl1Bps = kTargetBitrateBps *
3157 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3158 kNumTemporalLayers, /*temporal_id*/ 1);
3159 VideoBitrateAllocation expected_bitrate;
3160 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3161 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3162
3163 VerifyAllocatedBitrate(expected_bitrate);
3164 video_stream_encoder_->Stop();
3165}
3166
3167TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3168 // 2 TLs configured, temporal layers not supported by encoder.
3169 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3170 fake_encoder_.SetTemporalLayersSupported(0, false);
3171
3172 // Temporal layers not supported by the encoder.
3173 // Total bitrate should be at ti:0.
3174 VideoBitrateAllocation expected_bitrate;
3175 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3176
3177 VerifyAllocatedBitrate(expected_bitrate);
3178 video_stream_encoder_->Stop();
3179}
3180
3181TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3182 // 2 TLs configured, temporal layers only supported for first stream.
3183 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3184 fake_encoder_.SetTemporalLayersSupported(0, true);
3185 fake_encoder_.SetTemporalLayersSupported(1, false);
3186
3187 const int kS0Bps = 150000;
3188 const int kS0Tl0Bps =
3189 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3190 /*num_layers*/ 2, /*temporal_id*/ 0);
3191 const int kS0Tl1Bps =
3192 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3193 /*num_layers*/ 2, /*temporal_id*/ 1);
3194 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3195 // Temporal layers not supported by si:1.
3196 VideoBitrateAllocation expected_bitrate;
3197 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3198 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3199 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3200
3201 VerifyAllocatedBitrate(expected_bitrate);
3202 video_stream_encoder_->Stop();
3203}
3204
Niels Möller7dc26b72017-12-06 10:27:48 +01003205TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3206 const int kFrameWidth = 1280;
3207 const int kFrameHeight = 720;
3208 const int kFramerate = 24;
3209
Erik Språng4c6ca302019-04-08 15:14:01 +02003210 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003211 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3212 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003213 test::FrameForwarder source;
3214 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003215 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003216
3217 // Insert a single frame, triggering initial configuration.
3218 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3219 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3220
3221 EXPECT_EQ(
3222 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3223 kDefaultFramerate);
3224
3225 // Trigger reconfigure encoder (without resetting the entire instance).
3226 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003227 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003228 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3229 video_encoder_config.number_of_streams = 1;
3230 video_encoder_config.video_stream_factory =
3231 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3232 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003233 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003234 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3235
3236 // Detector should be updated with fps limit from codec config.
3237 EXPECT_EQ(
3238 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3239 kFramerate);
3240
3241 // Trigger overuse, max framerate should be reduced.
3242 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3243 stats.input_frame_rate = kFramerate;
3244 stats_proxy_->SetMockStats(stats);
3245 video_stream_encoder_->TriggerCpuOveruse();
3246 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3247 int adapted_framerate =
3248 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3249 EXPECT_LT(adapted_framerate, kFramerate);
3250
3251 // Trigger underuse, max framerate should go back to codec configured fps.
3252 // Set extra low fps, to make sure it's actually reset, not just incremented.
3253 stats = stats_proxy_->GetStats();
3254 stats.input_frame_rate = adapted_framerate / 2;
3255 stats_proxy_->SetMockStats(stats);
3256 video_stream_encoder_->TriggerCpuNormalUsage();
3257 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3258 EXPECT_EQ(
3259 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3260 kFramerate);
3261
3262 video_stream_encoder_->Stop();
3263}
3264
3265TEST_F(VideoStreamEncoderTest,
3266 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3267 const int kFrameWidth = 1280;
3268 const int kFrameHeight = 720;
3269 const int kLowFramerate = 15;
3270 const int kHighFramerate = 25;
3271
Erik Språng4c6ca302019-04-08 15:14:01 +02003272 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003273 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3274 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003275 test::FrameForwarder source;
3276 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003277 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003278
3279 // Trigger initial configuration.
3280 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003281 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003282 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3283 video_encoder_config.number_of_streams = 1;
3284 video_encoder_config.video_stream_factory =
3285 new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate);
3286 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3287 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003288 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003289 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3290
3291 EXPECT_EQ(
3292 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3293 kLowFramerate);
3294
3295 // Trigger overuse, max framerate should be reduced.
3296 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3297 stats.input_frame_rate = kLowFramerate;
3298 stats_proxy_->SetMockStats(stats);
3299 video_stream_encoder_->TriggerCpuOveruse();
3300 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3301 int adapted_framerate =
3302 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3303 EXPECT_LT(adapted_framerate, kLowFramerate);
3304
3305 // Reconfigure the encoder with a new (higher max framerate), max fps should
3306 // still respect the adaptation.
3307 video_encoder_config.video_stream_factory =
3308 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3309 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3310 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003311 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003312 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3313
3314 EXPECT_EQ(
3315 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3316 adapted_framerate);
3317
3318 // Trigger underuse, max framerate should go back to codec configured fps.
3319 stats = stats_proxy_->GetStats();
3320 stats.input_frame_rate = adapted_framerate;
3321 stats_proxy_->SetMockStats(stats);
3322 video_stream_encoder_->TriggerCpuNormalUsage();
3323 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3324 EXPECT_EQ(
3325 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3326 kHighFramerate);
3327
3328 video_stream_encoder_->Stop();
3329}
3330
mflodmancc3d4422017-08-03 08:27:51 -07003331TEST_F(VideoStreamEncoderTest,
3332 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003333 const int kFrameWidth = 1280;
3334 const int kFrameHeight = 720;
3335 const int kFramerate = 24;
3336
Erik Språng4c6ca302019-04-08 15:14:01 +02003337 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003338 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3339 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003340 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003341 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003342 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003343
3344 // Trigger initial configuration.
3345 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003346 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003347 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3348 video_encoder_config.number_of_streams = 1;
3349 video_encoder_config.video_stream_factory =
3350 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3351 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003352 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003353 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003354 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003355
Niels Möller7dc26b72017-12-06 10:27:48 +01003356 EXPECT_EQ(
3357 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3358 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003359
3360 // Trigger overuse, max framerate should be reduced.
3361 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3362 stats.input_frame_rate = kFramerate;
3363 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003364 video_stream_encoder_->TriggerCpuOveruse();
3365 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003366 int adapted_framerate =
3367 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003368 EXPECT_LT(adapted_framerate, kFramerate);
3369
3370 // Change degradation preference to not enable framerate scaling. Target
3371 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003372 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003373 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003374 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003375 EXPECT_EQ(
3376 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3377 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003378
mflodmancc3d4422017-08-03 08:27:51 -07003379 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003380}
3381
mflodmancc3d4422017-08-03 08:27:51 -07003382TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003383 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003384 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003385 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003386 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003387 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003388 const int kWidth = 640;
3389 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003390
asaperssonfab67072017-04-04 05:51:49 -07003391 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003392
3393 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003394 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003395
3396 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003397 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003398
sprangc5d62e22017-04-02 23:53:04 -07003399 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003400
asaperssonfab67072017-04-04 05:51:49 -07003401 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003402 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003403 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003404
3405 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003406 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003407
sprangc5d62e22017-04-02 23:53:04 -07003408 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003409
mflodmancc3d4422017-08-03 08:27:51 -07003410 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003411}
3412
mflodmancc3d4422017-08-03 08:27:51 -07003413TEST_F(VideoStreamEncoderTest,
3414 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003415 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003416 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003417 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003418 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003419 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003420 const int kWidth = 640;
3421 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003422
3423 // We expect the n initial frames to get dropped.
3424 int i;
3425 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003426 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003427 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003428 }
3429 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003430 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003431 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003432
3433 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003434 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003435
mflodmancc3d4422017-08-03 08:27:51 -07003436 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003437}
3438
mflodmancc3d4422017-08-03 08:27:51 -07003439TEST_F(VideoStreamEncoderTest,
3440 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003441 const int kWidth = 640;
3442 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003443 video_stream_encoder_->OnBitrateUpdated(
3444 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3445 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003446
3447 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003448 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003449 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003450
asaperssonfab67072017-04-04 05:51:49 -07003451 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003452 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003453 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003454
mflodmancc3d4422017-08-03 08:27:51 -07003455 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003456}
3457
mflodmancc3d4422017-08-03 08:27:51 -07003458TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003459 const int kWidth = 640;
3460 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003461 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003462
3463 VideoEncoderConfig video_encoder_config;
3464 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3465 // Make format different, to force recreation of encoder.
3466 video_encoder_config.video_format.parameters["foo"] = "foo";
3467 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003468 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003469 video_stream_encoder_->OnBitrateUpdated(
3470 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3471 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003472
kthelgasonb83797b2017-02-14 11:57:25 -08003473 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003474 video_stream_encoder_->SetSource(&video_source_,
3475 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003476
asaperssonfab67072017-04-04 05:51:49 -07003477 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003478 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003479 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003480
mflodmancc3d4422017-08-03 08:27:51 -07003481 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003482 fake_encoder_.SetQualityScaling(true);
3483}
3484
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003485TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3486 webrtc::test::ScopedFieldTrials field_trials(
3487 "WebRTC-InitialFramedrop/Enabled/");
3488 // Reset encoder for field trials to take effect.
3489 ConfigureEncoder(video_encoder_config_.Copy());
3490 const int kTooLowBitrateForFrameSizeBps = 10000;
3491 const int kWidth = 640;
3492 const int kHeight = 360;
3493
Erik Språng4c6ca302019-04-08 15:14:01 +02003494 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003495 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3496 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003497 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3498 // Frame should not be dropped.
3499 WaitForEncodedFrame(1);
3500
Erik Språng610c7632019-03-06 15:37:33 +01003501 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003502 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003503 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003504 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003505 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3506 // Expect to drop this frame, the wait should time out.
3507 ExpectDroppedFrame();
3508
3509 // Expect the sink_wants to specify a scaled frame.
3510 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3511 video_stream_encoder_->Stop();
3512}
3513
Åsa Persson139f4dc2019-08-02 09:29:58 +02003514TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3515 webrtc::test::ScopedFieldTrials field_trials(
3516 "WebRTC-Video-QualityScalerSettings/"
3517 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3518 // Reset encoder for field trials to take effect.
3519 ConfigureEncoder(video_encoder_config_.Copy());
3520 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3521 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3522 const int kWidth = 640;
3523 const int kHeight = 360;
3524
3525 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003526 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3527 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003528 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3529 // Frame should not be dropped.
3530 WaitForEncodedFrame(1);
3531
3532 video_stream_encoder_->OnBitrateUpdated(
3533 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003534 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003535 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3536 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3537 // Frame should not be dropped.
3538 WaitForEncodedFrame(2);
3539
3540 video_stream_encoder_->OnBitrateUpdated(
3541 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003542 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003543 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3544 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3545 // Expect to drop this frame, the wait should time out.
3546 ExpectDroppedFrame();
3547
3548 // Expect the sink_wants to specify a scaled frame.
3549 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3550 video_stream_encoder_->Stop();
3551}
3552
mflodmancc3d4422017-08-03 08:27:51 -07003553TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003554 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3555 const int kTooSmallWidth = 10;
3556 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003557 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003558 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3559 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003560
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003561 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003562 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003563 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003564 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003565 VerifyNoLimitation(source.sink_wants());
3566 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3567
3568 // Trigger adapt down, too small frame, expect no change.
3569 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003570 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003571 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003572 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003573 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3574 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3575
mflodmancc3d4422017-08-03 08:27:51 -07003576 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003577}
3578
mflodmancc3d4422017-08-03 08:27:51 -07003579TEST_F(VideoStreamEncoderTest,
3580 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003581 const int kTooSmallWidth = 10;
3582 const int kTooSmallHeight = 10;
3583 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003584 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003585 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3586 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003587
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003588 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003589 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003590 video_stream_encoder_->SetSource(&source,
3591 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003592 VerifyNoLimitation(source.sink_wants());
3593 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3594 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3595
3596 // Trigger adapt down, expect limited framerate.
3597 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003598 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003599 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003600 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3601 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3602 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3603 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3604
3605 // Trigger adapt down, too small frame, expect no change.
3606 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003607 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003608 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003609 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3610 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3611 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3612 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3613
mflodmancc3d4422017-08-03 08:27:51 -07003614 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003615}
3616
mflodmancc3d4422017-08-03 08:27:51 -07003617TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003618 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003619 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003620 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3621 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003622 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003623 const int kFrameWidth = 1280;
3624 const int kFrameHeight = 720;
3625 video_source_.IncomingCapturedFrame(
3626 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003627 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003628 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003629}
3630
sprangb1ca0732017-02-01 08:38:12 -08003631// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003632TEST_F(VideoStreamEncoderTest,
3633 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003634 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003635 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3636 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003637
3638 const int kFrameWidth = 1280;
3639 const int kFrameHeight = 720;
3640 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003641 // requested by
3642 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003643 video_source_.set_adaptation_enabled(true);
3644
3645 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003646 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003647 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003648
3649 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003650 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003651 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003652 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003653 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003654
asaperssonfab67072017-04-04 05:51:49 -07003655 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003656 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003657 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003658 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003659 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003660
mflodmancc3d4422017-08-03 08:27:51 -07003661 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003662}
sprangfe627f32017-03-29 08:24:59 -07003663
mflodmancc3d4422017-08-03 08:27:51 -07003664TEST_F(VideoStreamEncoderTest,
3665 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003666 const int kFrameWidth = 1280;
3667 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003668
Erik Språng4c6ca302019-04-08 15:14:01 +02003669 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003670 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3671 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003672 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003673 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003674 video_source_.set_adaptation_enabled(true);
3675
sprang4847ae62017-06-27 07:06:52 -07003676 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003677
3678 video_source_.IncomingCapturedFrame(
3679 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003680 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003681
3682 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003683 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003684
3685 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003686 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003687 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003688 video_source_.IncomingCapturedFrame(
3689 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003690 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003691 }
3692
3693 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003694 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003695 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003696 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003697 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003698 video_source_.IncomingCapturedFrame(
3699 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003700 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003701 ++num_frames_dropped;
3702 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003703 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003704 }
3705 }
3706
sprang4847ae62017-06-27 07:06:52 -07003707 // Add some slack to account for frames dropped by the frame dropper.
3708 const int kErrorMargin = 1;
3709 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003710 kErrorMargin);
3711
3712 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003713 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003714 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003715 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003716 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003717 video_source_.IncomingCapturedFrame(
3718 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003719 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003720 ++num_frames_dropped;
3721 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003722 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003723 }
3724 }
sprang4847ae62017-06-27 07:06:52 -07003725 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003726 kErrorMargin);
3727
3728 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003729 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003730 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003731 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003732 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003733 video_source_.IncomingCapturedFrame(
3734 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003735 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003736 ++num_frames_dropped;
3737 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003738 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003739 }
3740 }
sprang4847ae62017-06-27 07:06:52 -07003741 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003742 kErrorMargin);
3743
3744 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003745 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003746 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003747 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003748 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003749 video_source_.IncomingCapturedFrame(
3750 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003751 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003752 ++num_frames_dropped;
3753 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003754 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003755 }
3756 }
3757 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3758
mflodmancc3d4422017-08-03 08:27:51 -07003759 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003760}
3761
mflodmancc3d4422017-08-03 08:27:51 -07003762TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003763 const int kFramerateFps = 5;
3764 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003765 const int kFrameWidth = 1280;
3766 const int kFrameHeight = 720;
3767
sprang4847ae62017-06-27 07:06:52 -07003768 // Reconfigure encoder with two temporal layers and screensharing, which will
3769 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003770 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003771
Erik Språng4c6ca302019-04-08 15:14:01 +02003772 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003773 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3774 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003775 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003776 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003777 video_source_.set_adaptation_enabled(true);
3778
sprang4847ae62017-06-27 07:06:52 -07003779 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003780
3781 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003782 rtc::VideoSinkWants last_wants;
3783 do {
3784 last_wants = video_source_.sink_wants();
3785
sprangc5d62e22017-04-02 23:53:04 -07003786 // Insert frames to get a new fps estimate...
3787 for (int j = 0; j < kFramerateFps; ++j) {
3788 video_source_.IncomingCapturedFrame(
3789 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08003790 if (video_source_.last_sent_width()) {
3791 sink_.WaitForEncodedFrame(timestamp_ms);
3792 }
sprangc5d62e22017-04-02 23:53:04 -07003793 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02003794 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07003795 }
3796 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07003797 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08003798 } while (video_source_.sink_wants().max_framerate_fps <
3799 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07003800
Jonathan Yubc771b72017-12-08 17:04:29 -08003801 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07003802
mflodmancc3d4422017-08-03 08:27:51 -07003803 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003804}
asaperssonf7e294d2017-06-13 23:25:22 -07003805
mflodmancc3d4422017-08-03 08:27:51 -07003806TEST_F(VideoStreamEncoderTest,
3807 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003808 const int kWidth = 1280;
3809 const int kHeight = 720;
3810 const int64_t kFrameIntervalMs = 150;
3811 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003812 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003813 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3814 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003815
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003816 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003817 AdaptingFrameForwarder source;
3818 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003819 video_stream_encoder_->SetSource(&source,
3820 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003821 timestamp_ms += kFrameIntervalMs;
3822 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003823 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003824 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003825 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3826 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3827 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3828
3829 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003830 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003831 timestamp_ms += kFrameIntervalMs;
3832 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003833 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003834 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
3835 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3836 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3837 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3838
3839 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003840 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003841 timestamp_ms += kFrameIntervalMs;
3842 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003843 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003844 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
3845 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3846 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3847 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3848
3849 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003850 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003851 timestamp_ms += kFrameIntervalMs;
3852 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003853 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003854 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3855 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3856 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3857 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3858
3859 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003860 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003861 timestamp_ms += kFrameIntervalMs;
3862 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003863 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003864 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3865 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3866 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3867 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3868
3869 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003870 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003871 timestamp_ms += kFrameIntervalMs;
3872 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003873 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003874 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3875 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3876 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3877 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3878
3879 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003880 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003881 timestamp_ms += kFrameIntervalMs;
3882 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003883 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003884 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3885 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3886 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3887 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3888
3889 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07003890 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003891 timestamp_ms += kFrameIntervalMs;
3892 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003893 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003894 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3895 rtc::VideoSinkWants last_wants = source.sink_wants();
3896 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3897 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3898 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3899
3900 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07003901 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003902 timestamp_ms += kFrameIntervalMs;
3903 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003904 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003905 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
3906 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3907 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3908 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3909
3910 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003911 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003912 timestamp_ms += kFrameIntervalMs;
3913 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003914 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003915 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
3916 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3917 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3918 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3919
3920 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003921 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003922 timestamp_ms += kFrameIntervalMs;
3923 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003924 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003925 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
3926 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3927 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3928 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3929
3930 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003931 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003932 timestamp_ms += kFrameIntervalMs;
3933 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003934 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003935 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
3936 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3937 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3938 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3939
3940 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003941 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003942 timestamp_ms += kFrameIntervalMs;
3943 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003944 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003945 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
3946 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3947 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3948 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3949
3950 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003951 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003952 timestamp_ms += kFrameIntervalMs;
3953 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003954 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003955 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
3956 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3957 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3958 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3959
3960 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003961 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003962 timestamp_ms += kFrameIntervalMs;
3963 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003964 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003965 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3966 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3967 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3968 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3969
Åsa Persson30ab0152019-08-27 12:22:33 +02003970 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003971 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003972 timestamp_ms += kFrameIntervalMs;
3973 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003974 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07003975 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003976 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003977 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3978 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3979 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3980
3981 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07003982 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003983 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003984 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3985
mflodmancc3d4422017-08-03 08:27:51 -07003986 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003987}
3988
mflodmancc3d4422017-08-03 08:27:51 -07003989TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07003990 const int kWidth = 1280;
3991 const int kHeight = 720;
3992 const int64_t kFrameIntervalMs = 150;
3993 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003994 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003995 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3996 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003997
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003998 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003999 AdaptingFrameForwarder source;
4000 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004001 video_stream_encoder_->SetSource(&source,
4002 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004003 timestamp_ms += kFrameIntervalMs;
4004 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004005 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004006 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004007 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4008 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4009 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4010 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4011 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4012 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4013
4014 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004015 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004016 timestamp_ms += kFrameIntervalMs;
4017 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004018 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004019 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4020 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4021 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4022 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4023 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4024 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4025 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4026
4027 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004028 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004029 timestamp_ms += kFrameIntervalMs;
4030 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004031 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004032 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4033 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4034 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4035 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4036 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4037 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4038 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4039
4040 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004041 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004042 timestamp_ms += kFrameIntervalMs;
4043 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004044 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004045 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4046 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4047 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4048 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4049 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4050 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4051 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4052
4053 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004054 video_stream_encoder_->TriggerCpuNormalUsage();
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 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4059 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4060 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4061 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4062 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4063 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4064 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4065
4066 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004067 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004068 timestamp_ms += kFrameIntervalMs;
4069 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004070 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004071 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4072 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4073 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4074 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4075 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4076 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4077 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4078
4079 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004080 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004081 timestamp_ms += kFrameIntervalMs;
4082 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004083 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004084 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004085 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004086 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4087 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4088 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4089 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4090 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4091 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4092
4093 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004094 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004095 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004096 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4097 EXPECT_EQ(2, 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,
4103 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004104 const int kWidth = 640;
4105 const int kHeight = 360;
4106 const int kFpsLimit = 15;
4107 const int64_t kFrameIntervalMs = 150;
4108 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004109 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004110 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4111 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004112
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004113 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004114 AdaptingFrameForwarder source;
4115 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004116 video_stream_encoder_->SetSource(&source,
4117 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004118 timestamp_ms += kFrameIntervalMs;
4119 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004120 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004121 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004122 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4123 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4124 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4125 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4126 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4127 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4128
4129 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004130 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004131 timestamp_ms += kFrameIntervalMs;
4132 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004133 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004134 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4135 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4136 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4137 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4138 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4139 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4140 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4141
4142 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004143 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004144 timestamp_ms += kFrameIntervalMs;
4145 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004146 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004147 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4148 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4149 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4150 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4151 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4152 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4153 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4154
4155 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004156 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004157 timestamp_ms += kFrameIntervalMs;
4158 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004159 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004160 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4161 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4162 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4163 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4164 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4165 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4166 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4167
4168 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004169 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004170 timestamp_ms += kFrameIntervalMs;
4171 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004172 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004173 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004174 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4175 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4176 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4177 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4178 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4179 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4180
4181 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004182 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004183 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004184 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4185 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4186
mflodmancc3d4422017-08-03 08:27:51 -07004187 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004188}
4189
mflodmancc3d4422017-08-03 08:27:51 -07004190TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004191 const int kFrameWidth = 1920;
4192 const int kFrameHeight = 1080;
4193 // 3/4 of 1920.
4194 const int kAdaptedFrameWidth = 1440;
4195 // 3/4 of 1080 rounded down to multiple of 4.
4196 const int kAdaptedFrameHeight = 808;
4197 const int kFramerate = 24;
4198
Erik Språng4c6ca302019-04-08 15:14:01 +02004199 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004200 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4201 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004202 // Trigger reconfigure encoder (without resetting the entire instance).
4203 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004204 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004205 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4206 video_encoder_config.number_of_streams = 1;
4207 video_encoder_config.video_stream_factory =
4208 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004209 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004210 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004211 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004212
4213 video_source_.set_adaptation_enabled(true);
4214
4215 video_source_.IncomingCapturedFrame(
4216 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004217 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004218
4219 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004220 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004221 video_source_.IncomingCapturedFrame(
4222 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004223 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004224
mflodmancc3d4422017-08-03 08:27:51 -07004225 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004226}
4227
mflodmancc3d4422017-08-03 08:27:51 -07004228TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004229 const int kFrameWidth = 1280;
4230 const int kFrameHeight = 720;
4231 const int kLowFps = 2;
4232 const int kHighFps = 30;
4233
Erik Språng4c6ca302019-04-08 15:14:01 +02004234 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004235 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4236 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004237
4238 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4239 max_framerate_ = kLowFps;
4240
4241 // Insert 2 seconds of 2fps video.
4242 for (int i = 0; i < kLowFps * 2; ++i) {
4243 video_source_.IncomingCapturedFrame(
4244 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4245 WaitForEncodedFrame(timestamp_ms);
4246 timestamp_ms += 1000 / kLowFps;
4247 }
4248
4249 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004250 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004251 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4252 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004253 video_source_.IncomingCapturedFrame(
4254 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4255 WaitForEncodedFrame(timestamp_ms);
4256 timestamp_ms += 1000 / kLowFps;
4257
4258 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4259
4260 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004261 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004262 const int kFrameIntervalMs = 1000 / kHighFps;
4263 max_framerate_ = kHighFps;
4264 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4265 video_source_.IncomingCapturedFrame(
4266 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4267 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4268 // be dropped if the encoder hans't been updated with the new higher target
4269 // framerate yet, causing it to overshoot the target bitrate and then
4270 // suffering the wrath of the media optimizer.
4271 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4272 timestamp_ms += kFrameIntervalMs;
4273 }
4274
4275 // Don expect correct measurement just yet, but it should be higher than
4276 // before.
4277 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4278
mflodmancc3d4422017-08-03 08:27:51 -07004279 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004280}
4281
mflodmancc3d4422017-08-03 08:27:51 -07004282TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004283 const int kFrameWidth = 1280;
4284 const int kFrameHeight = 720;
4285 const int kTargetBitrateBps = 1000000;
4286
4287 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004288 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004289 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004290 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4291 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004292 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004293
4294 // Insert a first video frame, causes another bitrate update.
4295 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4296 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4297 video_source_.IncomingCapturedFrame(
4298 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4299 WaitForEncodedFrame(timestamp_ms);
4300
4301 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004302 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4303 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004304
4305 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004306 timestamp_ms += kProcessIntervalMs;
4307 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004308
4309 // Bitrate observer should not be called.
4310 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4311 video_source_.IncomingCapturedFrame(
4312 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4313 ExpectDroppedFrame();
4314
mflodmancc3d4422017-08-03 08:27:51 -07004315 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004316}
ilnik6b826ef2017-06-16 06:53:48 -07004317
Niels Möller4db138e2018-04-19 09:04:13 +02004318TEST_F(VideoStreamEncoderTest,
4319 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4320 const int kFrameWidth = 1280;
4321 const int kFrameHeight = 720;
4322 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004323 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004324 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4325 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004326 video_source_.IncomingCapturedFrame(
4327 CreateFrame(1, kFrameWidth, kFrameHeight));
4328 WaitForEncodedFrame(1);
4329 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4330 .low_encode_usage_threshold_percent,
4331 default_options.low_encode_usage_threshold_percent);
4332 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4333 .high_encode_usage_threshold_percent,
4334 default_options.high_encode_usage_threshold_percent);
4335 video_stream_encoder_->Stop();
4336}
4337
4338TEST_F(VideoStreamEncoderTest,
4339 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4340 const int kFrameWidth = 1280;
4341 const int kFrameHeight = 720;
4342 CpuOveruseOptions hardware_options;
4343 hardware_options.low_encode_usage_threshold_percent = 150;
4344 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004345 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004346
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);
Niels Möller4db138e2018-04-19 09:04:13 +02004350 video_source_.IncomingCapturedFrame(
4351 CreateFrame(1, kFrameWidth, kFrameHeight));
4352 WaitForEncodedFrame(1);
4353 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4354 .low_encode_usage_threshold_percent,
4355 hardware_options.low_encode_usage_threshold_percent);
4356 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4357 .high_encode_usage_threshold_percent,
4358 hardware_options.high_encode_usage_threshold_percent);
4359 video_stream_encoder_->Stop();
4360}
4361
Niels Möller6bb5ab92019-01-11 11:11:10 +01004362TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4363 const int kFrameWidth = 320;
4364 const int kFrameHeight = 240;
4365 const int kFps = 30;
4366 const int kTargetBitrateBps = 120000;
4367 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4368
Erik Språng4c6ca302019-04-08 15:14:01 +02004369 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004370 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4371 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004372
4373 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4374 max_framerate_ = kFps;
4375
4376 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4377 fake_encoder_.SimulateOvershoot(1.0);
4378 int num_dropped = 0;
4379 for (int i = 0; i < kNumFramesInRun; ++i) {
4380 video_source_.IncomingCapturedFrame(
4381 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4382 // Wait up to two frame durations for a frame to arrive.
4383 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4384 ++num_dropped;
4385 }
4386 timestamp_ms += 1000 / kFps;
4387 }
4388
Erik Språnga8d48ab2019-02-08 14:17:40 +01004389 // Framerate should be measured to be near the expected target rate.
4390 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4391
4392 // Frame drops should be within 5% of expected 0%.
4393 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004394
4395 // Make encoder produce frames at double the expected bitrate during 3 seconds
4396 // of video, verify number of drops. Rate needs to be slightly changed in
4397 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004398 double overshoot_factor = 2.0;
4399 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4400 // With bitrate adjuster, when need to overshoot even more to trigger
4401 // frame dropping.
4402 overshoot_factor *= 2;
4403 }
4404 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004405 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004406 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004407 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004408 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004409 num_dropped = 0;
4410 for (int i = 0; i < kNumFramesInRun; ++i) {
4411 video_source_.IncomingCapturedFrame(
4412 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4413 // Wait up to two frame durations for a frame to arrive.
4414 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4415 ++num_dropped;
4416 }
4417 timestamp_ms += 1000 / kFps;
4418 }
4419
Erik Språng4c6ca302019-04-08 15:14:01 +02004420 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004421 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4422 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004423
4424 // Target framerate should be still be near the expected target, despite
4425 // the frame drops.
4426 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4427
4428 // Frame drops should be within 5% of expected 50%.
4429 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004430
4431 video_stream_encoder_->Stop();
4432}
4433
4434TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4435 const int kFrameWidth = 320;
4436 const int kFrameHeight = 240;
4437 const int kActualInputFps = 24;
4438 const int kTargetBitrateBps = 120000;
4439
4440 ASSERT_GT(max_framerate_, kActualInputFps);
4441
4442 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4443 max_framerate_ = kActualInputFps;
Erik Språng4c6ca302019-04-08 15:14:01 +02004444 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004445 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4446 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004447
4448 // Insert 3 seconds of video, with an input fps lower than configured max.
4449 for (int i = 0; i < kActualInputFps * 3; ++i) {
4450 video_source_.IncomingCapturedFrame(
4451 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4452 // Wait up to two frame durations for a frame to arrive.
4453 WaitForEncodedFrame(timestamp_ms);
4454 timestamp_ms += 1000 / kActualInputFps;
4455 }
4456
4457 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4458
4459 video_stream_encoder_->Stop();
4460}
4461
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004462TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4463 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004464 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004465 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4466 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004467
4468 fake_encoder_.BlockNextEncode();
4469 video_source_.IncomingCapturedFrame(
4470 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4471 WaitForEncodedFrame(1);
4472 // On the very first frame full update should be forced.
4473 rect = fake_encoder_.GetLastUpdateRect();
4474 EXPECT_EQ(rect.offset_x, 0);
4475 EXPECT_EQ(rect.offset_y, 0);
4476 EXPECT_EQ(rect.height, codec_height_);
4477 EXPECT_EQ(rect.width, codec_width_);
4478 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4479 // call to ContinueEncode.
4480 video_source_.IncomingCapturedFrame(
4481 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4482 ExpectDroppedFrame();
4483 video_source_.IncomingCapturedFrame(
4484 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4485 ExpectDroppedFrame();
4486 fake_encoder_.ContinueEncode();
4487 WaitForEncodedFrame(3);
4488 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4489 rect = fake_encoder_.GetLastUpdateRect();
4490 EXPECT_EQ(rect.offset_x, 1);
4491 EXPECT_EQ(rect.offset_y, 0);
4492 EXPECT_EQ(rect.width, 10);
4493 EXPECT_EQ(rect.height, 1);
4494
4495 video_source_.IncomingCapturedFrame(
4496 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4497 WaitForEncodedFrame(4);
4498 // Previous frame was encoded, so no accumulation should happen.
4499 rect = fake_encoder_.GetLastUpdateRect();
4500 EXPECT_EQ(rect.offset_x, 0);
4501 EXPECT_EQ(rect.offset_y, 0);
4502 EXPECT_EQ(rect.width, 1);
4503 EXPECT_EQ(rect.height, 1);
4504
4505 video_stream_encoder_->Stop();
4506}
4507
Erik Språngd7329ca2019-02-21 21:19:53 +01004508TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004509 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004510 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4511 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004512
4513 // First frame is always keyframe.
4514 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4515 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004516 EXPECT_THAT(
4517 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004518 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004519
4520 // Insert delta frame.
4521 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4522 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004523 EXPECT_THAT(
4524 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004525 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004526
4527 // Request next frame be a key-frame.
4528 video_stream_encoder_->SendKeyFrame();
4529 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4530 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004531 EXPECT_THAT(
4532 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004533 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004534
4535 video_stream_encoder_->Stop();
4536}
4537
4538TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4539 // Setup simulcast with three streams.
4540 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004541 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004542 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004543 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004544 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004545 // Wait for all three layers before triggering event.
4546 sink_.SetNumExpectedLayers(3);
4547
4548 // First frame is always keyframe.
4549 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4550 WaitForEncodedFrame(1);
4551 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004552 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4553 VideoFrameType::kVideoFrameKey,
4554 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004555
4556 // Insert delta frame.
4557 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4558 WaitForEncodedFrame(2);
4559 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004560 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4561 VideoFrameType::kVideoFrameDelta,
4562 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004563
4564 // Request next frame be a key-frame.
4565 // Only first stream is configured to produce key-frame.
4566 video_stream_encoder_->SendKeyFrame();
4567 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4568 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004569
4570 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4571 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004572 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004573 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004574 VideoFrameType::kVideoFrameKey,
4575 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004576
4577 video_stream_encoder_->Stop();
4578}
4579
4580TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4581 // Configure internal source factory and setup test again.
4582 encoder_factory_.SetHasInternalSource(true);
4583 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004584 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004585 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4586 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004587
4588 // Call encoder directly, simulating internal source where encoded frame
4589 // callback in VideoStreamEncoder is called despite no OnFrame().
4590 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4591 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004592 EXPECT_THAT(
4593 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004594 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004595
Niels Möller8f7ce222019-03-21 15:43:58 +01004596 const std::vector<VideoFrameType> kDeltaFrame = {
4597 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004598 // Need to set timestamp manually since manually for injected frame.
4599 VideoFrame frame = CreateFrame(101, nullptr);
4600 frame.set_timestamp(101);
4601 fake_encoder_.InjectFrame(frame, false);
4602 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004603 EXPECT_THAT(
4604 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004605 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004606
4607 // Request key-frame. The forces a dummy frame down into the encoder.
4608 fake_encoder_.ExpectNullFrame();
4609 video_stream_encoder_->SendKeyFrame();
4610 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004611 EXPECT_THAT(
4612 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004613 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004614
4615 video_stream_encoder_->Stop();
4616}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004617
4618TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4619 // Configure internal source factory and setup test again.
4620 encoder_factory_.SetHasInternalSource(true);
4621 ResetEncoder("VP8", 1, 1, 1, false);
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ångb7cb7b52019-02-26 15:52:33 +01004625
4626 int64_t timestamp = 1;
4627 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004628 image.SetEncodedData(
4629 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004630 image.capture_time_ms_ = ++timestamp;
4631 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4632 const int64_t kEncodeFinishDelayMs = 10;
4633 image.timing_.encode_start_ms = timestamp;
4634 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4635 fake_encoder_.InjectEncodedImage(image);
4636 // Wait for frame without incrementing clock.
4637 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4638 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4639 // capture timestamp should be kEncodeFinishDelayMs in the past.
4640 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4641 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4642 kEncodeFinishDelayMs);
4643
4644 video_stream_encoder_->Stop();
4645}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004646
4647TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4648 // Configure internal source factory and setup test again.
4649 encoder_factory_.SetHasInternalSource(true);
4650 ResetEncoder("H264", 1, 1, 1, false);
4651
4652 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4653 image._frameType = VideoFrameType::kVideoFrameKey;
4654
4655 CodecSpecificInfo codec_specific_info;
4656 codec_specific_info.codecType = kVideoCodecH264;
4657
4658 RTPFragmentationHeader fragmentation;
4659 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4660 fragmentation.fragmentationOffset[0] = 4;
4661 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4662
4663 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4664 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4665
4666 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4667 testing::ElementsAreArray(optimal_sps));
4668 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4669 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4670 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4671 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4672
4673 video_stream_encoder_->Stop();
4674}
4675
4676TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4677 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4678 0x00, 0x00, 0x03, 0x03, 0xF4,
4679 0x05, 0x03, 0xC7, 0xC0};
4680
4681 // Configure internal source factory and setup test again.
4682 encoder_factory_.SetHasInternalSource(true);
4683 ResetEncoder("H264", 1, 1, 1, false);
4684
4685 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4686 image._frameType = VideoFrameType::kVideoFrameKey;
4687
4688 CodecSpecificInfo codec_specific_info;
4689 codec_specific_info.codecType = kVideoCodecH264;
4690
4691 RTPFragmentationHeader fragmentation;
4692 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4693 fragmentation.fragmentationOffset[0] = 4;
4694 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4695
4696 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4697 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4698
4699 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4700 testing::ElementsAreArray(optimal_sps));
4701 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4702 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4703 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4704 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4705
4706 video_stream_encoder_->Stop();
4707}
4708
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004709TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4710 const int kFrameWidth = 1280;
4711 const int kFrameHeight = 720;
4712 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4713
4714 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004715 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4716 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004717 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4718
4719 // Insert a first video frame. It should be dropped because of downscale in
4720 // resolution.
4721 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4722 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4723 frame.set_rotation(kVideoRotation_270);
4724 video_source_.IncomingCapturedFrame(frame);
4725
4726 ExpectDroppedFrame();
4727
4728 // Second frame is downscaled.
4729 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4730 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4731 frame.set_rotation(kVideoRotation_90);
4732 video_source_.IncomingCapturedFrame(frame);
4733
4734 WaitForEncodedFrame(timestamp_ms);
4735 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4736
4737 // Insert another frame, also downscaled.
4738 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4739 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4740 frame.set_rotation(kVideoRotation_180);
4741 video_source_.IncomingCapturedFrame(frame);
4742
4743 WaitForEncodedFrame(timestamp_ms);
4744 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4745
4746 video_stream_encoder_->Stop();
4747}
4748
Erik Språng5056af02019-09-02 15:53:11 +02004749TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4750 const int kFrameWidth = 320;
4751 const int kFrameHeight = 180;
4752
4753 // Initial rate.
4754 video_stream_encoder_->OnBitrateUpdated(
4755 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004756 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004757 /*link_allocation=*/DataRate::kbps(300),
4758 /*fraction_lost=*/0,
4759 /*rtt_ms=*/0);
4760
4761 // Insert a first video frame so that encoder gets configured.
4762 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4763 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4764 frame.set_rotation(kVideoRotation_270);
4765 video_source_.IncomingCapturedFrame(frame);
4766 WaitForEncodedFrame(timestamp_ms);
4767
4768 // Set a target rate below the minimum allowed by the codec settings.
4769 VideoCodec codec_config = fake_encoder_.codec_config();
4770 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4771 DataRate target_rate = min_rate - DataRate::kbps(1);
4772 video_stream_encoder_->OnBitrateUpdated(
4773 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004774 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004775 /*link_allocation=*/target_rate,
4776 /*fraction_lost=*/0,
4777 /*rtt_ms=*/0);
4778 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4779
4780 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4781 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4782 ASSERT_TRUE(rate_settings.has_value());
4783 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
4784 EXPECT_EQ(min_rate, allocation_sum);
4785 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
4786
4787 video_stream_encoder_->Stop();
4788}
4789
perkj26091b12016-09-01 01:17:40 -07004790} // namespace webrtc