blob: 31d3aa15eb805f1e4d7bf1f9016d913bcf33349a [file] [log] [blame]
perkj26091b12016-09-01 01:17:40 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Erik Språng4529fbc2018-10-12 10:30:31 +020011#include "video/video_stream_encoder.h"
12
sprangfe627f32017-03-29 08:24:59 -070013#include <algorithm>
perkj803d97f2016-11-01 11:45:46 -070014#include <limits>
Danil Chapovalovd3ba2362019-04-10 17:01:23 +020015#include <memory>
Per512ecb32016-09-23 15:52:06 +020016#include <utility>
17
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020018#include "absl/memory/memory.h"
Danil Chapovalovd3ba2362019-04-10 17:01:23 +020019#include "api/task_queue/default_task_queue_factory.h"
Elad Alon45befc52019-07-02 11:20:09 +020020#include "api/test/mock_fec_controller_override.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080021#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/i420_buffer.h"
Erik Språngf93eda12019-01-16 17:10:57 +010023#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020024#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020025#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010026#include "api/video_codecs/vp8_temporal_layers_factory.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020027#include "common_video/h264/h264_common.h"
Noah Richards51db4212019-06-12 06:59:12 -070028#include "common_video/include/video_frame_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "media/base/video_adapter.h"
Sergey Silkin86684962018-03-28 19:32:37 +020030#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Åsa Perssonc29cb2c2019-03-25 12:06:59 +010032#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/ref_counted_object.h"
Erik Språng7ca375c2019-02-06 16:20:17 +010036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020037#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "system_wrappers/include/sleep.h"
39#include "test/encoder_settings.h"
40#include "test/fake_encoder.h"
Kári Tristan Helgason639602a2018-08-02 10:51:40 +020041#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/frame_generator.h"
43#include "test/gmock.h"
44#include "test/gtest.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020045#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "video/send_statistics_proxy.h"
perkj26091b12016-09-01 01:17:40 -070047
48namespace webrtc {
49
sprangb1ca0732017-02-01 08:38:12 -080050using ScaleReason = AdaptationObserverInterface::AdaptReason;
sprang57c2fff2017-01-16 06:24:02 -080051using ::testing::_;
philipeld9cc8c02019-09-16 14:53:40 +020052using ::testing::AllOf;
53using ::testing::Field;
54using ::testing::StrictMock;
kthelgason876222f2016-11-29 01:44:11 -080055
perkj803d97f2016-11-01 11:45:46 -070056namespace {
Åsa Persson8c1bf952018-09-13 10:42:19 +020057const int kMinPixelsPerFrame = 320 * 180;
58const int kMinFramerateFps = 2;
59const int kMinBalancedFramerateFps = 7;
60const int64_t kFrameTimeoutMs = 100;
asapersson5f7226f2016-11-25 04:37:00 -080061const size_t kMaxPayloadLength = 1440;
Erik Språngd7329ca2019-02-21 21:19:53 +010062const uint32_t kTargetBitrateBps = 1000000;
Sergey Silkin5ee69672019-07-02 14:18:34 +020063const uint32_t kStartBitrateBps = 600000;
Erik Språngd7329ca2019-02-21 21:19:53 +010064const uint32_t kSimulcastTargetBitrateBps = 3150000;
65const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10;
kthelgason2bc68642017-02-07 07:02:22 -080066const int kMaxInitialFramedrop = 4;
sprangfda496a2017-06-15 04:21:07 -070067const int kDefaultFramerate = 30;
Åsa Persson8c1bf952018-09-13 10:42:19 +020068const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate;
Niels Möllerfe407b72019-09-10 10:48:48 +020069const int64_t kProcessIntervalMs = 1000;
asapersson5f7226f2016-11-25 04:37:00 -080070
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020071uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
72 0x00, 0x00, 0x03, 0x03, 0xF4,
73 0x05, 0x03, 0xC7, 0xE0, 0x1B,
74 0x41, 0x10, 0x8D, 0x00};
75
perkj803d97f2016-11-01 11:45:46 -070076class TestBuffer : public webrtc::I420Buffer {
77 public:
78 TestBuffer(rtc::Event* event, int width, int height)
79 : I420Buffer(width, height), event_(event) {}
80
81 private:
82 friend class rtc::RefCountedObject<TestBuffer>;
83 ~TestBuffer() override {
84 if (event_)
85 event_->Set();
86 }
87 rtc::Event* const event_;
88};
89
Noah Richards51db4212019-06-12 06:59:12 -070090// A fake native buffer that can't be converted to I420.
91class FakeNativeBuffer : public webrtc::VideoFrameBuffer {
92 public:
93 FakeNativeBuffer(rtc::Event* event, int width, int height)
94 : event_(event), width_(width), height_(height) {}
95 webrtc::VideoFrameBuffer::Type type() const override { return Type::kNative; }
96 int width() const override { return width_; }
97 int height() const override { return height_; }
98 rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
99 return nullptr;
100 }
101
102 private:
103 friend class rtc::RefCountedObject<FakeNativeBuffer>;
104 ~FakeNativeBuffer() override {
105 if (event_)
106 event_->Set();
107 }
108 rtc::Event* const event_;
109 const int width_;
110 const int height_;
111};
112
Niels Möller7dc26b72017-12-06 10:27:48 +0100113class CpuOveruseDetectorProxy : public OveruseFrameDetector {
114 public:
Niels Möllerd1f7eb62018-03-28 16:40:58 +0200115 explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer)
116 : OveruseFrameDetector(metrics_observer),
Niels Möller7dc26b72017-12-06 10:27:48 +0100117 last_target_framerate_fps_(-1) {}
118 virtual ~CpuOveruseDetectorProxy() {}
119
120 void OnTargetFramerateUpdated(int framerate_fps) override {
121 rtc::CritScope cs(&lock_);
122 last_target_framerate_fps_ = framerate_fps;
123 OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps);
124 }
125
126 int GetLastTargetFramerate() {
127 rtc::CritScope cs(&lock_);
128 return last_target_framerate_fps_;
129 }
130
Niels Möller4db138e2018-04-19 09:04:13 +0200131 CpuOveruseOptions GetOptions() { return options_; }
132
Niels Möller7dc26b72017-12-06 10:27:48 +0100133 private:
134 rtc::CriticalSection lock_;
135 int last_target_framerate_fps_ RTC_GUARDED_BY(lock_);
136};
137
mflodmancc3d4422017-08-03 08:27:51 -0700138class VideoStreamEncoderUnderTest : public VideoStreamEncoder {
perkj803d97f2016-11-01 11:45:46 -0700139 public:
Niels Möller213618e2018-07-24 09:29:58 +0200140 VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy,
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200141 const VideoStreamEncoderSettings& settings,
142 TaskQueueFactory* task_queue_factory)
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100143 : VideoStreamEncoder(Clock::GetRealTimeClock(),
144 1 /* number_of_cores */,
Yves Gerey665174f2018-06-19 15:03:05 +0200145 stats_proxy,
146 settings,
Yves Gerey665174f2018-06-19 15:03:05 +0200147 std::unique_ptr<OveruseFrameDetector>(
148 overuse_detector_proxy_ =
Sebastian Jansson74682c12019-03-01 11:50:20 +0100149 new CpuOveruseDetectorProxy(stats_proxy)),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200150 task_queue_factory) {}
perkj803d97f2016-11-01 11:45:46 -0700151
sprangb1ca0732017-02-01 08:38:12 -0800152 void PostTaskAndWait(bool down, AdaptReason reason) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200153 PostTaskAndWait(down, reason, /*expected_results=*/true);
154 }
155
156 void PostTaskAndWait(bool down, AdaptReason reason, bool expected_results) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100157 rtc::Event event;
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200158 encoder_queue()->PostTask([this, &event, reason, down, expected_results] {
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200159 if (down)
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200160 EXPECT_EQ(expected_results, AdaptDown(reason));
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200161 else
162 AdaptUp(reason);
perkj803d97f2016-11-01 11:45:46 -0700163 event.Set();
164 });
perkj070ba852017-02-16 15:46:27 -0800165 ASSERT_TRUE(event.Wait(5000));
perkj803d97f2016-11-01 11:45:46 -0700166 }
167
kthelgason2fc52542017-03-03 00:24:41 -0800168 // This is used as a synchronisation mechanism, to make sure that the
169 // encoder queue is not blocked before we start sending it frames.
170 void WaitUntilTaskQueueIsIdle() {
Niels Möllerc572ff32018-11-07 08:43:50 +0100171 rtc::Event event;
Yves Gerey665174f2018-06-19 15:03:05 +0200172 encoder_queue()->PostTask([&event] { event.Set(); });
kthelgason2fc52542017-03-03 00:24:41 -0800173 ASSERT_TRUE(event.Wait(5000));
174 }
175
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200176 void TriggerCpuOveruse() {
177 PostTaskAndWait(/*down=*/true, AdaptReason::kCpu);
178 }
kthelgason876222f2016-11-29 01:44:11 -0800179
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200180 void TriggerCpuNormalUsage() {
181 PostTaskAndWait(/*down=*/false, AdaptReason::kCpu);
182 }
kthelgason876222f2016-11-29 01:44:11 -0800183
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200184 void TriggerQualityLow() {
185 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality);
186 }
kthelgason876222f2016-11-29 01:44:11 -0800187
Åsa Perssonb67c44c2019-09-24 15:25:32 +0200188 void TriggerQualityLowExpectFalse() {
189 PostTaskAndWait(/*down=*/true, AdaptReason::kQuality,
190 /*expected_results=*/false);
191 }
192
193 void TriggerQualityHigh() {
194 PostTaskAndWait(/*down=*/false, AdaptReason::kQuality);
195 }
sprangfda496a2017-06-15 04:21:07 -0700196
Niels Möller7dc26b72017-12-06 10:27:48 +0100197 CpuOveruseDetectorProxy* overuse_detector_proxy_;
perkj803d97f2016-11-01 11:45:46 -0700198};
199
asapersson5f7226f2016-11-25 04:37:00 -0800200class VideoStreamFactory
201 : public VideoEncoderConfig::VideoStreamFactoryInterface {
202 public:
sprangfda496a2017-06-15 04:21:07 -0700203 explicit VideoStreamFactory(size_t num_temporal_layers, int framerate)
204 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
asapersson5f7226f2016-11-25 04:37:00 -0800205 EXPECT_GT(num_temporal_layers, 0u);
sprangfda496a2017-06-15 04:21:07 -0700206 EXPECT_GT(framerate, 0);
asapersson5f7226f2016-11-25 04:37:00 -0800207 }
208
209 private:
210 std::vector<VideoStream> CreateEncoderStreams(
211 int width,
212 int height,
213 const VideoEncoderConfig& encoder_config) override {
214 std::vector<VideoStream> streams =
215 test::CreateVideoStreams(width, height, encoder_config);
216 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +0100217 stream.num_temporal_layers = num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700218 stream.max_framerate = framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800219 }
220 return streams;
221 }
sprangfda496a2017-06-15 04:21:07 -0700222
asapersson5f7226f2016-11-25 04:37:00 -0800223 const size_t num_temporal_layers_;
sprangfda496a2017-06-15 04:21:07 -0700224 const int framerate_;
asapersson5f7226f2016-11-25 04:37:00 -0800225};
226
Noah Richards51db4212019-06-12 06:59:12 -0700227// Simulates simulcast behavior and makes highest stream resolutions divisible
228// by 4.
229class CroppingVideoStreamFactory
230 : public VideoEncoderConfig::VideoStreamFactoryInterface {
231 public:
232 explicit CroppingVideoStreamFactory(size_t num_temporal_layers, int framerate)
233 : num_temporal_layers_(num_temporal_layers), framerate_(framerate) {
234 EXPECT_GT(num_temporal_layers, 0u);
235 EXPECT_GT(framerate, 0);
236 }
237
238 private:
239 std::vector<VideoStream> CreateEncoderStreams(
240 int width,
241 int height,
242 const VideoEncoderConfig& encoder_config) override {
243 std::vector<VideoStream> streams = test::CreateVideoStreams(
244 width - width % 4, height - height % 4, encoder_config);
245 for (VideoStream& stream : streams) {
246 stream.num_temporal_layers = num_temporal_layers_;
247 stream.max_framerate = framerate_;
248 }
249 return streams;
250 }
251
252 const size_t num_temporal_layers_;
253 const int framerate_;
254};
255
sprangb1ca0732017-02-01 08:38:12 -0800256class AdaptingFrameForwarder : public test::FrameForwarder {
257 public:
258 AdaptingFrameForwarder() : adaptation_enabled_(false) {}
asaperssonfab67072017-04-04 05:51:49 -0700259 ~AdaptingFrameForwarder() override {}
sprangb1ca0732017-02-01 08:38:12 -0800260
261 void set_adaptation_enabled(bool enabled) {
262 rtc::CritScope cs(&crit_);
263 adaptation_enabled_ = enabled;
264 }
265
asaperssonfab67072017-04-04 05:51:49 -0700266 bool adaption_enabled() const {
sprangb1ca0732017-02-01 08:38:12 -0800267 rtc::CritScope cs(&crit_);
268 return adaptation_enabled_;
269 }
270
asapersson09f05612017-05-15 23:40:18 -0700271 rtc::VideoSinkWants last_wants() const {
272 rtc::CritScope cs(&crit_);
273 return last_wants_;
274 }
275
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200276 absl::optional<int> last_sent_width() const { return last_width_; }
277 absl::optional<int> last_sent_height() const { return last_height_; }
Jonathan Yubc771b72017-12-08 17:04:29 -0800278
sprangb1ca0732017-02-01 08:38:12 -0800279 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
280 int cropped_width = 0;
281 int cropped_height = 0;
282 int out_width = 0;
283 int out_height = 0;
sprangc5d62e22017-04-02 23:53:04 -0700284 if (adaption_enabled()) {
285 if (adapter_.AdaptFrameResolution(
286 video_frame.width(), video_frame.height(),
287 video_frame.timestamp_us() * 1000, &cropped_width,
288 &cropped_height, &out_width, &out_height)) {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100289 VideoFrame adapted_frame =
290 VideoFrame::Builder()
291 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
292 nullptr, out_width, out_height))
293 .set_timestamp_rtp(99)
294 .set_timestamp_ms(99)
295 .set_rotation(kVideoRotation_0)
296 .build();
sprangc5d62e22017-04-02 23:53:04 -0700297 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
298 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800299 last_width_.emplace(adapted_frame.width());
300 last_height_.emplace(adapted_frame.height());
301 } else {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200302 last_width_ = absl::nullopt;
303 last_height_ = absl::nullopt;
sprangc5d62e22017-04-02 23:53:04 -0700304 }
sprangb1ca0732017-02-01 08:38:12 -0800305 } else {
306 test::FrameForwarder::IncomingCapturedFrame(video_frame);
Jonathan Yubc771b72017-12-08 17:04:29 -0800307 last_width_.emplace(video_frame.width());
308 last_height_.emplace(video_frame.height());
sprangb1ca0732017-02-01 08:38:12 -0800309 }
310 }
311
312 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
313 const rtc::VideoSinkWants& wants) override {
314 rtc::CritScope cs(&crit_);
asapersson09f05612017-05-15 23:40:18 -0700315 last_wants_ = sink_wants();
sprangc5d62e22017-04-02 23:53:04 -0700316 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
317 wants.max_pixel_count,
318 wants.max_framerate_fps);
sprangb1ca0732017-02-01 08:38:12 -0800319 test::FrameForwarder::AddOrUpdateSink(sink, wants);
320 }
sprangb1ca0732017-02-01 08:38:12 -0800321 cricket::VideoAdapter adapter_;
danilchapa37de392017-09-09 04:17:22 -0700322 bool adaptation_enabled_ RTC_GUARDED_BY(crit_);
323 rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200324 absl::optional<int> last_width_;
325 absl::optional<int> last_height_;
sprangb1ca0732017-02-01 08:38:12 -0800326};
sprangc5d62e22017-04-02 23:53:04 -0700327
Niels Möller213618e2018-07-24 09:29:58 +0200328// TODO(nisse): Mock only VideoStreamEncoderObserver.
sprangc5d62e22017-04-02 23:53:04 -0700329class MockableSendStatisticsProxy : public SendStatisticsProxy {
330 public:
331 MockableSendStatisticsProxy(Clock* clock,
332 const VideoSendStream::Config& config,
333 VideoEncoderConfig::ContentType content_type)
334 : SendStatisticsProxy(clock, config, content_type) {}
335
336 VideoSendStream::Stats GetStats() override {
337 rtc::CritScope cs(&lock_);
338 if (mock_stats_)
339 return *mock_stats_;
340 return SendStatisticsProxy::GetStats();
341 }
342
Niels Möller213618e2018-07-24 09:29:58 +0200343 int GetInputFrameRate() const override {
344 rtc::CritScope cs(&lock_);
345 if (mock_stats_)
346 return mock_stats_->input_frame_rate;
347 return SendStatisticsProxy::GetInputFrameRate();
348 }
sprangc5d62e22017-04-02 23:53:04 -0700349 void SetMockStats(const VideoSendStream::Stats& stats) {
350 rtc::CritScope cs(&lock_);
351 mock_stats_.emplace(stats);
352 }
353
354 void ResetMockStats() {
355 rtc::CritScope cs(&lock_);
356 mock_stats_.reset();
357 }
358
359 private:
360 rtc::CriticalSection lock_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200361 absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_);
sprangc5d62e22017-04-02 23:53:04 -0700362};
363
sprang4847ae62017-06-27 07:06:52 -0700364class MockBitrateObserver : public VideoBitrateAllocationObserver {
365 public:
Erik Språng566124a2018-04-23 12:32:22 +0200366 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
sprang4847ae62017-06-27 07:06:52 -0700367};
368
perkj803d97f2016-11-01 11:45:46 -0700369} // namespace
370
mflodmancc3d4422017-08-03 08:27:51 -0700371class VideoStreamEncoderTest : public ::testing::Test {
perkj26091b12016-09-01 01:17:40 -0700372 public:
373 static const int kDefaultTimeoutMs = 30 * 1000;
374
mflodmancc3d4422017-08-03 08:27:51 -0700375 VideoStreamEncoderTest()
perkj26091b12016-09-01 01:17:40 -0700376 : video_send_config_(VideoSendStream::Config(nullptr)),
perkjfa10b552016-10-02 23:45:26 -0700377 codec_width_(320),
378 codec_height_(240),
Åsa Persson8c1bf952018-09-13 10:42:19 +0200379 max_framerate_(kDefaultFramerate),
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200380 task_queue_factory_(CreateDefaultTaskQueueFactory()),
perkj26091b12016-09-01 01:17:40 -0700381 fake_encoder_(),
Niels Möller4db138e2018-04-19 09:04:13 +0200382 encoder_factory_(&fake_encoder_),
sprangc5d62e22017-04-02 23:53:04 -0700383 stats_proxy_(new MockableSendStatisticsProxy(
perkj803d97f2016-11-01 11:45:46 -0700384 Clock::GetRealTimeClock(),
385 video_send_config_,
386 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
perkj26091b12016-09-01 01:17:40 -0700387 sink_(&fake_encoder_) {}
388
389 void SetUp() override {
perkj803d97f2016-11-01 11:45:46 -0700390 metrics::Reset();
perkj26091b12016-09-01 01:17:40 -0700391 video_send_config_ = VideoSendStream::Config(nullptr);
Niels Möller4db138e2018-04-19 09:04:13 +0200392 video_send_config_.encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800393 video_send_config_.encoder_settings.bitrate_allocator_factory =
Sergey Silkin5ee69672019-07-02 14:18:34 +0200394 &bitrate_allocator_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200395 video_send_config_.rtp.payload_name = "FAKE";
396 video_send_config_.rtp.payload_type = 125;
perkj26091b12016-09-01 01:17:40 -0700397
Per512ecb32016-09-23 15:52:06 +0200398 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200399 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
sprang4847ae62017-06-27 07:06:52 -0700400 video_encoder_config.video_stream_factory =
401 new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_);
Erik Språng08127a92016-11-16 16:41:30 +0100402 video_encoder_config_ = video_encoder_config.Copy();
sprang4847ae62017-06-27 07:06:52 -0700403
404 // Framerate limit is specified by the VideoStreamFactory.
405 std::vector<VideoStream> streams =
406 video_encoder_config.video_stream_factory->CreateEncoderStreams(
407 codec_width_, codec_height_, video_encoder_config);
408 max_framerate_ = streams[0].max_framerate;
Sebastian Jansson40889f32019-04-17 12:11:20 +0200409 fake_clock_.SetTime(Timestamp::us(1234));
sprang4847ae62017-06-27 07:06:52 -0700410
Niels Möllerf1338562018-04-26 09:51:47 +0200411 ConfigureEncoder(std::move(video_encoder_config));
asapersson5f7226f2016-11-25 04:37:00 -0800412 }
413
Niels Möllerf1338562018-04-26 09:51:47 +0200414 void ConfigureEncoder(VideoEncoderConfig video_encoder_config) {
mflodmancc3d4422017-08-03 08:27:51 -0700415 if (video_stream_encoder_)
416 video_stream_encoder_->Stop();
417 video_stream_encoder_.reset(new VideoStreamEncoderUnderTest(
Danil Chapovalovd3ba2362019-04-10 17:01:23 +0200418 stats_proxy_.get(), video_send_config_.encoder_settings,
419 task_queue_factory_.get()));
mflodmancc3d4422017-08-03 08:27:51 -0700420 video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */);
421 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700422 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -0700423 video_stream_encoder_->SetStartBitrate(kTargetBitrateBps);
424 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +0200425 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -0700426 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
asapersson5f7226f2016-11-25 04:37:00 -0800427 }
428
429 void ResetEncoder(const std::string& payload_name,
430 size_t num_streams,
431 size_t num_temporal_layers,
emircanbbcc3562017-08-18 00:28:40 -0700432 unsigned char num_spatial_layers,
sprang4847ae62017-06-27 07:06:52 -0700433 bool screenshare) {
Niels Möller259a4972018-04-05 15:36:51 +0200434 video_send_config_.rtp.payload_name = payload_name;
asapersson5f7226f2016-11-25 04:37:00 -0800435
436 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +0200437 video_encoder_config.codec_type = PayloadStringToCodecType(payload_name);
asapersson5f7226f2016-11-25 04:37:00 -0800438 video_encoder_config.number_of_streams = num_streams;
Erik Språngd7329ca2019-02-21 21:19:53 +0100439 video_encoder_config.max_bitrate_bps =
440 num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps;
asapersson5f7226f2016-11-25 04:37:00 -0800441 video_encoder_config.video_stream_factory =
sprangfda496a2017-06-15 04:21:07 -0700442 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers,
443 kDefaultFramerate);
sprang4847ae62017-06-27 07:06:52 -0700444 video_encoder_config.content_type =
445 screenshare ? VideoEncoderConfig::ContentType::kScreen
446 : VideoEncoderConfig::ContentType::kRealtimeVideo;
emircanbbcc3562017-08-18 00:28:40 -0700447 if (payload_name == "VP9") {
448 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
449 vp9_settings.numberOfSpatialLayers = num_spatial_layers;
450 video_encoder_config.encoder_specific_settings =
451 new rtc::RefCountedObject<
452 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
453 }
Niels Möllerf1338562018-04-26 09:51:47 +0200454 ConfigureEncoder(std::move(video_encoder_config));
perkj26091b12016-09-01 01:17:40 -0700455 }
456
sprang57c2fff2017-01-16 06:24:02 -0800457 VideoFrame CreateFrame(int64_t ntp_time_ms,
458 rtc::Event* destruction_event) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100459 VideoFrame frame =
460 VideoFrame::Builder()
461 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
462 destruction_event, codec_width_, codec_height_))
463 .set_timestamp_rtp(99)
464 .set_timestamp_ms(99)
465 .set_rotation(kVideoRotation_0)
466 .build();
sprang57c2fff2017-01-16 06:24:02 -0800467 frame.set_ntp_time_ms(ntp_time_ms);
perkj26091b12016-09-01 01:17:40 -0700468 return frame;
469 }
470
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100471 VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms,
472 rtc::Event* destruction_event,
473 int offset_x) const {
474 VideoFrame frame =
475 VideoFrame::Builder()
476 .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>(
477 destruction_event, codec_width_, codec_height_))
478 .set_timestamp_rtp(99)
479 .set_timestamp_ms(99)
480 .set_rotation(kVideoRotation_0)
481 .set_update_rect({offset_x, 0, 1, 1})
482 .build();
483 frame.set_ntp_time_ms(ntp_time_ms);
484 return frame;
485 }
486
sprang57c2fff2017-01-16 06:24:02 -0800487 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100488 VideoFrame frame =
489 VideoFrame::Builder()
490 .set_video_frame_buffer(
491 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height))
492 .set_timestamp_rtp(99)
493 .set_timestamp_ms(99)
494 .set_rotation(kVideoRotation_0)
495 .build();
sprang57c2fff2017-01-16 06:24:02 -0800496 frame.set_ntp_time_ms(ntp_time_ms);
sprangc5d62e22017-04-02 23:53:04 -0700497 frame.set_timestamp_us(ntp_time_ms * 1000);
perkj803d97f2016-11-01 11:45:46 -0700498 return frame;
499 }
500
Noah Richards51db4212019-06-12 06:59:12 -0700501 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
502 rtc::Event* destruction_event,
503 int width,
504 int height) const {
505 VideoFrame frame =
506 VideoFrame::Builder()
507 .set_video_frame_buffer(new rtc::RefCountedObject<FakeNativeBuffer>(
508 destruction_event, width, height))
509 .set_timestamp_rtp(99)
510 .set_timestamp_ms(99)
511 .set_rotation(kVideoRotation_0)
512 .build();
513 frame.set_ntp_time_ms(ntp_time_ms);
514 return frame;
515 }
516
517 VideoFrame CreateFakeNativeFrame(int64_t ntp_time_ms,
518 rtc::Event* destruction_event) const {
519 return CreateFakeNativeFrame(ntp_time_ms, destruction_event, codec_width_,
520 codec_height_);
521 }
522
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100523 void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) {
524 MockBitrateObserver bitrate_observer;
525 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
526
527 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
528 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +0200529 video_stream_encoder_->OnBitrateUpdated(
530 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
531 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100532
533 video_source_.IncomingCapturedFrame(
534 CreateFrame(1, codec_width_, codec_height_));
535 WaitForEncodedFrame(1);
536 }
537
asapersson02465b82017-04-10 01:12:52 -0700538 void VerifyNoLimitation(const rtc::VideoSinkWants& wants) {
asapersson02465b82017-04-10 01:12:52 -0700539 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700540 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
541 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700542 }
543
asapersson09f05612017-05-15 23:40:18 -0700544 void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1,
545 const rtc::VideoSinkWants& wants2) {
546 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
547 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
548 }
549
Åsa Persson8c1bf952018-09-13 10:42:19 +0200550 void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) {
551 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
552 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
553 EXPECT_FALSE(wants.target_pixel_count);
554 }
555
asapersson09f05612017-05-15 23:40:18 -0700556 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1,
557 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200558 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700559 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
560 EXPECT_GT(wants1.max_pixel_count, 0);
561 }
562
563 void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1,
564 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200565 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asapersson09f05612017-05-15 23:40:18 -0700566 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
567 }
568
asaperssonf7e294d2017-06-13 23:25:22 -0700569 void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1,
570 const rtc::VideoSinkWants& wants2) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200571 EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -0700572 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
573 }
574
575 void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1,
576 const rtc::VideoSinkWants& wants2) {
577 EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps);
578 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
579 }
580
581 void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1,
582 const rtc::VideoSinkWants& wants2) {
583 EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps);
584 EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count);
585 }
586
587 void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1,
588 const rtc::VideoSinkWants& wants2) {
589 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
590 EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count);
591 EXPECT_GT(wants1.max_pixel_count, 0);
592 }
593
594 void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1,
595 const rtc::VideoSinkWants& wants2) {
596 EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps);
597 EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count);
598 }
599
asapersson09f05612017-05-15 23:40:18 -0700600 void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants,
601 int pixel_count) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200602 EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps);
asapersson02465b82017-04-10 01:12:52 -0700603 EXPECT_LT(wants.max_pixel_count, pixel_count);
604 EXPECT_GT(wants.max_pixel_count, 0);
asapersson09f05612017-05-15 23:40:18 -0700605 }
606
607 void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) {
608 EXPECT_LT(wants.max_framerate_fps, fps);
609 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
610 EXPECT_FALSE(wants.target_pixel_count);
asapersson02465b82017-04-10 01:12:52 -0700611 }
612
asaperssonf7e294d2017-06-13 23:25:22 -0700613 void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants,
614 int expected_fps) {
615 EXPECT_EQ(expected_fps, wants.max_framerate_fps);
616 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count);
617 EXPECT_FALSE(wants.target_pixel_count);
618 }
619
Jonathan Yubc771b72017-12-08 17:04:29 -0800620 void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants,
621 int last_frame_pixels) {
622 // Balanced mode should always scale FPS to the desired range before
623 // attempting to scale resolution.
624 int fps_limit = wants.max_framerate_fps;
625 if (last_frame_pixels <= 320 * 240) {
626 EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10);
627 } else if (last_frame_pixels <= 480 * 270) {
628 EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15);
629 } else if (last_frame_pixels <= 640 * 480) {
630 EXPECT_LE(15, fps_limit);
631 } else {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200632 EXPECT_EQ(kDefaultFramerate, fps_limit);
Jonathan Yubc771b72017-12-08 17:04:29 -0800633 }
634 }
635
sprang4847ae62017-06-27 07:06:52 -0700636 void WaitForEncodedFrame(int64_t expected_ntp_time) {
637 sink_.WaitForEncodedFrame(expected_ntp_time);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200638 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700639 }
640
641 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
642 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200643 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700644 return ok;
645 }
646
647 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
648 sink_.WaitForEncodedFrame(expected_width, expected_height);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200649 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700650 }
651
652 void ExpectDroppedFrame() {
653 sink_.ExpectDroppedFrame();
Sebastian Jansson40889f32019-04-17 12:11:20 +0200654 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700655 }
656
657 bool WaitForFrame(int64_t timeout_ms) {
658 bool ok = sink_.WaitForFrame(timeout_ms);
Sebastian Jansson40889f32019-04-17 12:11:20 +0200659 fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_);
sprang4847ae62017-06-27 07:06:52 -0700660 return ok;
661 }
662
perkj26091b12016-09-01 01:17:40 -0700663 class TestEncoder : public test::FakeEncoder {
664 public:
Niels Möllerc572ff32018-11-07 08:43:50 +0100665 TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
perkj26091b12016-09-01 01:17:40 -0700666
asaperssonfab67072017-04-04 05:51:49 -0700667 VideoCodec codec_config() const {
brandtre78d2662017-01-16 05:57:16 -0800668 rtc::CritScope lock(&crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700669 return config_;
670 }
671
672 void BlockNextEncode() {
brandtre78d2662017-01-16 05:57:16 -0800673 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700674 block_next_encode_ = true;
675 }
676
Erik Språngaed30702018-11-05 12:57:17 +0100677 VideoEncoder::EncoderInfo GetEncoderInfo() const override {
kthelgason2fc52542017-03-03 00:24:41 -0800678 rtc::CritScope lock(&local_crit_sect_);
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100679 EncoderInfo info;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100680 if (initialized_ == EncoderState::kInitialized) {
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100681 if (quality_scaling_) {
682 info.scaling_settings =
683 VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame);
684 }
685 info.is_hardware_accelerated = is_hardware_accelerated_;
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100686 for (int i = 0; i < kMaxSpatialLayers; ++i) {
687 if (temporal_layers_supported_[i]) {
688 int num_layers = temporal_layers_supported_[i].value() ? 2 : 1;
689 info.fps_allocation[i].resize(num_layers);
690 }
691 }
Erik Språngaed30702018-11-05 12:57:17 +0100692 }
Sergey Silkin6456e352019-07-08 17:56:40 +0200693
694 info.resolution_bitrate_limits = resolution_bitrate_limits_;
Erik Språngaed30702018-11-05 12:57:17 +0100695 return info;
kthelgason876222f2016-11-29 01:44:11 -0800696 }
697
Erik Språngb7cb7b52019-02-26 15:52:33 +0100698 int32_t RegisterEncodeCompleteCallback(
699 EncodedImageCallback* callback) override {
700 rtc::CritScope lock(&local_crit_sect_);
701 encoded_image_callback_ = callback;
702 return FakeEncoder::RegisterEncodeCompleteCallback(callback);
703 }
704
perkjfa10b552016-10-02 23:45:26 -0700705 void ContinueEncode() { continue_encode_event_.Set(); }
706
707 void CheckLastTimeStampsMatch(int64_t ntp_time_ms,
708 uint32_t timestamp) const {
brandtre78d2662017-01-16 05:57:16 -0800709 rtc::CritScope lock(&local_crit_sect_);
perkjfa10b552016-10-02 23:45:26 -0700710 EXPECT_EQ(timestamp_, timestamp);
711 EXPECT_EQ(ntp_time_ms_, ntp_time_ms);
712 }
713
kthelgason2fc52542017-03-03 00:24:41 -0800714 void SetQualityScaling(bool b) {
715 rtc::CritScope lock(&local_crit_sect_);
716 quality_scaling_ = b;
717 }
kthelgasonad9010c2017-02-14 00:46:51 -0800718
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100719 void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
720 rtc::CritScope lock(&local_crit_sect_);
721 is_hardware_accelerated_ = is_hardware_accelerated;
722 }
723
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100724 void SetTemporalLayersSupported(size_t spatial_idx, bool supported) {
725 RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers);
726 rtc::CritScope lock(&local_crit_sect_);
727 temporal_layers_supported_[spatial_idx] = supported;
728 }
729
Sergey Silkin6456e352019-07-08 17:56:40 +0200730 void SetResolutionBitrateLimits(
731 std::vector<ResolutionBitrateLimits> thresholds) {
732 rtc::CritScope cs(&local_crit_sect_);
733 resolution_bitrate_limits_ = thresholds;
734 }
735
sprangfe627f32017-03-29 08:24:59 -0700736 void ForceInitEncodeFailure(bool force_failure) {
737 rtc::CritScope lock(&local_crit_sect_);
738 force_init_encode_failed_ = force_failure;
739 }
740
Niels Möller6bb5ab92019-01-11 11:11:10 +0100741 void SimulateOvershoot(double rate_factor) {
742 rtc::CritScope lock(&local_crit_sect_);
743 rate_factor_ = rate_factor;
744 }
745
Erik Språngd7329ca2019-02-21 21:19:53 +0100746 uint32_t GetLastFramerate() const {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100747 rtc::CritScope lock(&local_crit_sect_);
748 return last_framerate_;
749 }
750
Erik Språngd7329ca2019-02-21 21:19:53 +0100751 VideoFrame::UpdateRect GetLastUpdateRect() const {
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100752 rtc::CritScope lock(&local_crit_sect_);
753 return last_update_rect_;
754 }
755
Niels Möller87e2d782019-03-07 10:18:23 +0100756 const std::vector<VideoFrameType>& LastFrameTypes() const {
Erik Språngd7329ca2019-02-21 21:19:53 +0100757 rtc::CritScope lock(&local_crit_sect_);
758 return last_frame_types_;
759 }
760
761 void InjectFrame(const VideoFrame& input_image, bool keyframe) {
Niels Möller87e2d782019-03-07 10:18:23 +0100762 const std::vector<VideoFrameType> frame_type = {
Niels Möller8f7ce222019-03-21 15:43:58 +0100763 keyframe ? VideoFrameType::kVideoFrameKey
764 : VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +0100765 {
766 rtc::CritScope lock(&local_crit_sect_);
767 last_frame_types_ = frame_type;
768 }
Niels Möllerb859b322019-03-07 12:40:01 +0100769 FakeEncoder::Encode(input_image, &frame_type);
Erik Språngd7329ca2019-02-21 21:19:53 +0100770 }
771
Erik Språngb7cb7b52019-02-26 15:52:33 +0100772 void InjectEncodedImage(const EncodedImage& image) {
773 rtc::CritScope lock(&local_crit_sect_);
774 encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr);
775 }
776
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +0200777 void InjectEncodedImage(const EncodedImage& image,
778 const CodecSpecificInfo* codec_specific_info,
779 const RTPFragmentationHeader* fragmentation) {
780 rtc::CritScope lock(&local_crit_sect_);
781 encoded_image_callback_->OnEncodedImage(image, codec_specific_info,
782 fragmentation);
783 }
784
Erik Språngd7329ca2019-02-21 21:19:53 +0100785 void ExpectNullFrame() {
786 rtc::CritScope lock(&local_crit_sect_);
787 expect_null_frame_ = true;
788 }
789
Erik Språng5056af02019-09-02 15:53:11 +0200790 absl::optional<VideoEncoder::RateControlParameters>
791 GetAndResetLastRateControlSettings() {
792 auto settings = last_rate_control_settings_;
793 last_rate_control_settings_.reset();
794 return settings;
Erik Språngd7329ca2019-02-21 21:19:53 +0100795 }
796
Sergey Silkin5ee69672019-07-02 14:18:34 +0200797 int GetNumEncoderInitializations() const {
798 rtc::CritScope lock(&local_crit_sect_);
799 return num_encoder_initializations_;
800 }
801
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200802 int GetNumSetRates() const {
803 rtc::CritScope lock(&local_crit_sect_);
804 return num_set_rates_;
805 }
806
perkjfa10b552016-10-02 23:45:26 -0700807 private:
perkj26091b12016-09-01 01:17:40 -0700808 int32_t Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100809 const std::vector<VideoFrameType>* frame_types) override {
perkj26091b12016-09-01 01:17:40 -0700810 bool block_encode;
811 {
brandtre78d2662017-01-16 05:57:16 -0800812 rtc::CritScope lock(&local_crit_sect_);
Erik Språngd7329ca2019-02-21 21:19:53 +0100813 if (expect_null_frame_) {
814 EXPECT_EQ(input_image.timestamp(), 0u);
815 EXPECT_EQ(input_image.width(), 1);
816 last_frame_types_ = *frame_types;
817 expect_null_frame_ = false;
818 } else {
819 EXPECT_GT(input_image.timestamp(), timestamp_);
820 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_);
821 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90);
822 }
perkj26091b12016-09-01 01:17:40 -0700823
824 timestamp_ = input_image.timestamp();
825 ntp_time_ms_ = input_image.ntp_time_ms();
perkj803d97f2016-11-01 11:45:46 -0700826 last_input_width_ = input_image.width();
827 last_input_height_ = input_image.height();
perkj26091b12016-09-01 01:17:40 -0700828 block_encode = block_next_encode_;
829 block_next_encode_ = false;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100830 last_update_rect_ = input_image.update_rect();
Erik Språngd7329ca2019-02-21 21:19:53 +0100831 last_frame_types_ = *frame_types;
perkj26091b12016-09-01 01:17:40 -0700832 }
Niels Möllerb859b322019-03-07 12:40:01 +0100833 int32_t result = FakeEncoder::Encode(input_image, frame_types);
perkj26091b12016-09-01 01:17:40 -0700834 if (block_encode)
perkja49cbd32016-09-16 07:53:41 -0700835 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -0700836 return result;
837 }
838
sprangfe627f32017-03-29 08:24:59 -0700839 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200840 const Settings& settings) override {
841 int res = FakeEncoder::InitEncode(config, settings);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200842
sprangfe627f32017-03-29 08:24:59 -0700843 rtc::CritScope lock(&local_crit_sect_);
Erik Språngb7cb7b52019-02-26 15:52:33 +0100844 EXPECT_EQ(initialized_, EncoderState::kUninitialized);
Sergey Silkin5ee69672019-07-02 14:18:34 +0200845
846 ++num_encoder_initializations_;
847
Erik Språng82fad3d2018-03-21 09:57:23 +0100848 if (config->codecType == kVideoCodecVP8) {
sprangfe627f32017-03-29 08:24:59 -0700849 // Simulate setting up temporal layers, in order to validate the life
850 // cycle of these objects.
Elad Aloncde8ab22019-03-20 11:56:20 +0100851 Vp8TemporalLayersFactory factory;
Elad Alon45befc52019-07-02 11:20:09 +0200852 frame_buffer_controller_ =
853 factory.Create(*config, settings, &fec_controller_override_);
sprangfe627f32017-03-29 08:24:59 -0700854 }
Erik Språngb7cb7b52019-02-26 15:52:33 +0100855 if (force_init_encode_failed_) {
856 initialized_ = EncoderState::kInitializationFailed;
sprangfe627f32017-03-29 08:24:59 -0700857 return -1;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100858 }
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100859
Erik Språngb7cb7b52019-02-26 15:52:33 +0100860 initialized_ = EncoderState::kInitialized;
sprangfe627f32017-03-29 08:24:59 -0700861 return res;
862 }
863
Erik Språngb7cb7b52019-02-26 15:52:33 +0100864 int32_t Release() override {
865 rtc::CritScope lock(&local_crit_sect_);
866 EXPECT_NE(initialized_, EncoderState::kUninitialized);
867 initialized_ = EncoderState::kUninitialized;
868 return FakeEncoder::Release();
869 }
870
Erik Språng16cb8f52019-04-12 13:59:09 +0200871 void SetRates(const RateControlParameters& parameters) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100872 rtc::CritScope lock(&local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200873 num_set_rates_++;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100874 VideoBitrateAllocation adjusted_rate_allocation;
875 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
876 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200877 if (parameters.bitrate.HasBitrate(si, ti)) {
Niels Möller6bb5ab92019-01-11 11:11:10 +0100878 adjusted_rate_allocation.SetBitrate(
879 si, ti,
Erik Språng16cb8f52019-04-12 13:59:09 +0200880 static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) *
Niels Möller6bb5ab92019-01-11 11:11:10 +0100881 rate_factor_));
882 }
883 }
884 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200885 last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng5056af02019-09-02 15:53:11 +0200886 last_rate_control_settings_ = parameters;
Erik Språng16cb8f52019-04-12 13:59:09 +0200887 RateControlParameters adjusted_paramters = parameters;
888 adjusted_paramters.bitrate = adjusted_rate_allocation;
889 FakeEncoder::SetRates(adjusted_paramters);
Niels Möller6bb5ab92019-01-11 11:11:10 +0100890 }
891
brandtre78d2662017-01-16 05:57:16 -0800892 rtc::CriticalSection local_crit_sect_;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100893 enum class EncoderState {
894 kUninitialized,
895 kInitializationFailed,
896 kInitialized
897 } initialized_ RTC_GUARDED_BY(local_crit_sect_) =
898 EncoderState::kUninitialized;
danilchapa37de392017-09-09 04:17:22 -0700899 bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false;
perkj26091b12016-09-01 01:17:40 -0700900 rtc::Event continue_encode_event_;
danilchapa37de392017-09-09 04:17:22 -0700901 uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0;
902 int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0;
903 int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0;
904 int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0;
905 bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +0100906 bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false;
Elad Aloncde8ab22019-03-20 11:56:20 +0100907 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_
danilchapa37de392017-09-09 04:17:22 -0700908 RTC_GUARDED_BY(local_crit_sect_);
Åsa Perssonc29cb2c2019-03-25 12:06:59 +0100909 absl::optional<bool>
910 temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY(
911 local_crit_sect_);
danilchapa37de392017-09-09 04:17:22 -0700912 bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100913 double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0;
914 uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Erik Språng5056af02019-09-02 15:53:11 +0200915 absl::optional<VideoEncoder::RateControlParameters>
916 last_rate_control_settings_;
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +0100917 VideoFrame::UpdateRect last_update_rect_
918 RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0};
Niels Möller87e2d782019-03-07 10:18:23 +0100919 std::vector<VideoFrameType> last_frame_types_;
Erik Språngd7329ca2019-02-21 21:19:53 +0100920 bool expect_null_frame_ = false;
Erik Språngb7cb7b52019-02-26 15:52:33 +0100921 EncodedImageCallback* encoded_image_callback_
922 RTC_GUARDED_BY(local_crit_sect_) = nullptr;
Elad Alon45befc52019-07-02 11:20:09 +0200923 MockFecControllerOverride fec_controller_override_;
Sergey Silkin5ee69672019-07-02 14:18:34 +0200924 int num_encoder_initializations_ RTC_GUARDED_BY(local_crit_sect_) = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +0200925 std::vector<ResolutionBitrateLimits> resolution_bitrate_limits_
926 RTC_GUARDED_BY(local_crit_sect_);
Evan Shrubsole7c079f62019-09-26 09:55:03 +0200927 int num_set_rates_ RTC_GUARDED_BY(local_crit_sect_) = 0;
perkj26091b12016-09-01 01:17:40 -0700928 };
929
mflodmancc3d4422017-08-03 08:27:51 -0700930 class TestSink : public VideoStreamEncoder::EncoderSink {
perkj26091b12016-09-01 01:17:40 -0700931 public:
932 explicit TestSink(TestEncoder* test_encoder)
Niels Möllerc572ff32018-11-07 08:43:50 +0100933 : test_encoder_(test_encoder) {}
perkj26091b12016-09-01 01:17:40 -0700934
perkj26091b12016-09-01 01:17:40 -0700935 void WaitForEncodedFrame(int64_t expected_ntp_time) {
sprang4847ae62017-06-27 07:06:52 -0700936 EXPECT_TRUE(
937 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
938 }
939
940 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
941 int64_t timeout_ms) {
perkj26091b12016-09-01 01:17:40 -0700942 uint32_t timestamp = 0;
sprang4847ae62017-06-27 07:06:52 -0700943 if (!encoded_frame_event_.Wait(timeout_ms))
944 return false;
perkj26091b12016-09-01 01:17:40 -0700945 {
946 rtc::CritScope lock(&crit_);
sprangb1ca0732017-02-01 08:38:12 -0800947 timestamp = last_timestamp_;
perkj26091b12016-09-01 01:17:40 -0700948 }
949 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
sprang4847ae62017-06-27 07:06:52 -0700950 return true;
perkj26091b12016-09-01 01:17:40 -0700951 }
952
sprangb1ca0732017-02-01 08:38:12 -0800953 void WaitForEncodedFrame(uint32_t expected_width,
954 uint32_t expected_height) {
sprangc5d62e22017-04-02 23:53:04 -0700955 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100956 CheckLastFrameSizeMatches(expected_width, expected_height);
sprangc5d62e22017-04-02 23:53:04 -0700957 }
958
Åsa Perssonc74d8da2017-12-04 14:13:56 +0100959 void CheckLastFrameSizeMatches(uint32_t expected_width,
sprangc5d62e22017-04-02 23:53:04 -0700960 uint32_t expected_height) {
sprangb1ca0732017-02-01 08:38:12 -0800961 uint32_t width = 0;
962 uint32_t height = 0;
sprangb1ca0732017-02-01 08:38:12 -0800963 {
964 rtc::CritScope lock(&crit_);
965 width = last_width_;
966 height = last_height_;
967 }
968 EXPECT_EQ(expected_height, height);
969 EXPECT_EQ(expected_width, width);
970 }
971
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +0200972 void CheckLastFrameRotationMatches(VideoRotation expected_rotation) {
973 VideoRotation rotation;
974 {
975 rtc::CritScope lock(&crit_);
976 rotation = last_rotation_;
977 }
978 EXPECT_EQ(expected_rotation, rotation);
979 }
980
kthelgason2fc52542017-03-03 00:24:41 -0800981 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
kthelgason2bc68642017-02-07 07:02:22 -0800982
sprangc5d62e22017-04-02 23:53:04 -0700983 bool WaitForFrame(int64_t timeout_ms) {
984 return encoded_frame_event_.Wait(timeout_ms);
985 }
986
perkj26091b12016-09-01 01:17:40 -0700987 void SetExpectNoFrames() {
988 rtc::CritScope lock(&crit_);
989 expect_frames_ = false;
990 }
991
asaperssonfab67072017-04-04 05:51:49 -0700992 int number_of_reconfigurations() const {
Per512ecb32016-09-23 15:52:06 +0200993 rtc::CritScope lock(&crit_);
994 return number_of_reconfigurations_;
995 }
996
asaperssonfab67072017-04-04 05:51:49 -0700997 int last_min_transmit_bitrate() const {
Per512ecb32016-09-23 15:52:06 +0200998 rtc::CritScope lock(&crit_);
999 return min_transmit_bitrate_bps_;
1000 }
1001
Erik Språngd7329ca2019-02-21 21:19:53 +01001002 void SetNumExpectedLayers(size_t num_layers) {
1003 rtc::CritScope lock(&crit_);
1004 num_expected_layers_ = num_layers;
1005 }
1006
Erik Språngb7cb7b52019-02-26 15:52:33 +01001007 int64_t GetLastCaptureTimeMs() const {
1008 rtc::CritScope lock(&crit_);
1009 return last_capture_time_ms_;
1010 }
1011
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001012 std::vector<uint8_t> GetLastEncodedImageData() {
1013 rtc::CritScope lock(&crit_);
1014 return std::move(last_encoded_image_data_);
1015 }
1016
1017 RTPFragmentationHeader GetLastFragmentation() {
1018 rtc::CritScope lock(&crit_);
1019 return std::move(last_fragmentation_);
1020 }
1021
perkj26091b12016-09-01 01:17:40 -07001022 private:
sergeyu2cb155a2016-11-04 11:39:29 -07001023 Result OnEncodedImage(
1024 const EncodedImage& encoded_image,
1025 const CodecSpecificInfo* codec_specific_info,
1026 const RTPFragmentationHeader* fragmentation) override {
Per512ecb32016-09-23 15:52:06 +02001027 rtc::CritScope lock(&crit_);
1028 EXPECT_TRUE(expect_frames_);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001029 last_encoded_image_data_ = std::vector<uint8_t>(
1030 encoded_image.data(), encoded_image.data() + encoded_image.size());
1031 if (fragmentation) {
1032 last_fragmentation_.CopyFrom(*fragmentation);
1033 }
Erik Språngd7329ca2019-02-21 21:19:53 +01001034 uint32_t timestamp = encoded_image.Timestamp();
1035 if (last_timestamp_ != timestamp) {
1036 num_received_layers_ = 1;
1037 } else {
1038 ++num_received_layers_;
1039 }
1040 last_timestamp_ = timestamp;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001041 last_capture_time_ms_ = encoded_image.capture_time_ms_;
sprangb1ca0732017-02-01 08:38:12 -08001042 last_width_ = encoded_image._encodedWidth;
1043 last_height_ = encoded_image._encodedHeight;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001044 last_rotation_ = encoded_image.rotation_;
Erik Språngd7329ca2019-02-21 21:19:53 +01001045 if (num_received_layers_ == num_expected_layers_) {
1046 encoded_frame_event_.Set();
1047 }
sprangb1ca0732017-02-01 08:38:12 -08001048 return Result(Result::OK, last_timestamp_);
Per512ecb32016-09-23 15:52:06 +02001049 }
1050
Rasmus Brandtc402dbe2019-02-04 11:09:46 +01001051 void OnEncoderConfigurationChanged(
1052 std::vector<VideoStream> streams,
1053 VideoEncoderConfig::ContentType content_type,
1054 int min_transmit_bitrate_bps) override {
Sergey Silkin5ee69672019-07-02 14:18:34 +02001055 rtc::CritScope lock(&crit_);
Per512ecb32016-09-23 15:52:06 +02001056 ++number_of_reconfigurations_;
1057 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
1058 }
1059
perkj26091b12016-09-01 01:17:40 -07001060 rtc::CriticalSection crit_;
1061 TestEncoder* test_encoder_;
1062 rtc::Event encoded_frame_event_;
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02001063 std::vector<uint8_t> last_encoded_image_data_;
1064 RTPFragmentationHeader last_fragmentation_;
sprangb1ca0732017-02-01 08:38:12 -08001065 uint32_t last_timestamp_ = 0;
Erik Språngb7cb7b52019-02-26 15:52:33 +01001066 int64_t last_capture_time_ms_ = 0;
sprangb1ca0732017-02-01 08:38:12 -08001067 uint32_t last_height_ = 0;
1068 uint32_t last_width_ = 0;
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02001069 VideoRotation last_rotation_ = kVideoRotation_0;
Erik Språngd7329ca2019-02-21 21:19:53 +01001070 size_t num_expected_layers_ = 1;
1071 size_t num_received_layers_ = 0;
perkj26091b12016-09-01 01:17:40 -07001072 bool expect_frames_ = true;
Per512ecb32016-09-23 15:52:06 +02001073 int number_of_reconfigurations_ = 0;
1074 int min_transmit_bitrate_bps_ = 0;
perkj26091b12016-09-01 01:17:40 -07001075 };
1076
Sergey Silkin5ee69672019-07-02 14:18:34 +02001077 class VideoBitrateAllocatorProxyFactory
1078 : public VideoBitrateAllocatorFactory {
1079 public:
1080 VideoBitrateAllocatorProxyFactory()
1081 : bitrate_allocator_factory_(
1082 CreateBuiltinVideoBitrateAllocatorFactory()) {}
1083
1084 std::unique_ptr<VideoBitrateAllocator> CreateVideoBitrateAllocator(
1085 const VideoCodec& codec) override {
1086 rtc::CritScope lock(&crit_);
1087 codec_config_ = codec;
1088 return bitrate_allocator_factory_->CreateVideoBitrateAllocator(codec);
1089 }
1090
1091 VideoCodec codec_config() const {
1092 rtc::CritScope lock(&crit_);
1093 return codec_config_;
1094 }
1095
1096 private:
1097 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
1098
1099 rtc::CriticalSection crit_;
1100 VideoCodec codec_config_ RTC_GUARDED_BY(crit_);
1101 };
1102
perkj26091b12016-09-01 01:17:40 -07001103 VideoSendStream::Config video_send_config_;
Erik Språng08127a92016-11-16 16:41:30 +01001104 VideoEncoderConfig video_encoder_config_;
Per512ecb32016-09-23 15:52:06 +02001105 int codec_width_;
1106 int codec_height_;
sprang4847ae62017-06-27 07:06:52 -07001107 int max_framerate_;
Erik Språng82268752019-08-29 15:07:47 +02001108 rtc::ScopedFakeClock fake_clock_;
Danil Chapovalovd3ba2362019-04-10 17:01:23 +02001109 const std::unique_ptr<TaskQueueFactory> task_queue_factory_;
perkj26091b12016-09-01 01:17:40 -07001110 TestEncoder fake_encoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +02001111 test::VideoEncoderProxyFactory encoder_factory_;
Sergey Silkin5ee69672019-07-02 14:18:34 +02001112 VideoBitrateAllocatorProxyFactory bitrate_allocator_factory_;
sprangc5d62e22017-04-02 23:53:04 -07001113 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
perkj26091b12016-09-01 01:17:40 -07001114 TestSink sink_;
sprangb1ca0732017-02-01 08:38:12 -08001115 AdaptingFrameForwarder video_source_;
mflodmancc3d4422017-08-03 08:27:51 -07001116 std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_;
perkj26091b12016-09-01 01:17:40 -07001117};
1118
mflodmancc3d4422017-08-03 08:27:51 -07001119TEST_F(VideoStreamEncoderTest, EncodeOneFrame) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001120 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001121 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1122 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerc572ff32018-11-07 08:43:50 +01001123 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001124 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
sprang4847ae62017-06-27 07:06:52 -07001125 WaitForEncodedFrame(1);
perkja49cbd32016-09-16 07:53:41 -07001126 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
mflodmancc3d4422017-08-03 08:27:51 -07001127 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001128}
1129
mflodmancc3d4422017-08-03 08:27:51 -07001130TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
perkj26091b12016-09-01 01:17:40 -07001131 // Dropped since no target bitrate has been set.
Niels Möllerc572ff32018-11-07 08:43:50 +01001132 rtc::Event frame_destroyed_event;
Sebastian Janssona3177052018-04-10 13:05:49 +02001133 // The encoder will cache up to one frame for a short duration. Adding two
1134 // frames means that the first frame will be dropped and the second frame will
1135 // be sent when the encoder is enabled.
perkja49cbd32016-09-16 07:53:41 -07001136 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
Sebastian Janssona3177052018-04-10 13:05:49 +02001137 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
perkja49cbd32016-09-16 07:53:41 -07001138 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
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);
perkj26091b12016-09-01 01:17:40 -07001143
Sebastian Janssona3177052018-04-10 13:05:49 +02001144 // The pending frame should be received.
sprang4847ae62017-06-27 07:06:52 -07001145 WaitForEncodedFrame(2);
Sebastian Janssona3177052018-04-10 13:05:49 +02001146 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
1147
1148 WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07001149 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001150}
1151
mflodmancc3d4422017-08-03 08:27:51 -07001152TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001153 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001154 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1155 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001156 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001157 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001158
Florent Castellia8336d32019-09-09 13:36:55 +02001159 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
1160 DataRate::bps(0), 0, 0);
Sebastian Janssona3177052018-04-10 13:05:49 +02001161 // The encoder will cache up to one frame for a short duration. Adding two
1162 // frames means that the first frame will be dropped and the second frame will
1163 // be sent when the encoder is resumed.
perkja49cbd32016-09-16 07:53:41 -07001164 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
Sebastian Janssona3177052018-04-10 13:05:49 +02001165 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001166
Erik Språng4c6ca302019-04-08 15:14:01 +02001167 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001168 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1169 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07001170 WaitForEncodedFrame(3);
Sebastian Janssona3177052018-04-10 13:05:49 +02001171 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1172 WaitForEncodedFrame(4);
mflodmancc3d4422017-08-03 08:27:51 -07001173 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001174}
1175
mflodmancc3d4422017-08-03 08:27:51 -07001176TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001177 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001178 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1179 DataRate::bps(kTargetBitrateBps), 0, 0);
perkja49cbd32016-09-16 07:53:41 -07001180 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001181 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001182
1183 // This frame will be dropped since it has the same ntp timestamp.
perkja49cbd32016-09-16 07:53:41 -07001184 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
perkj26091b12016-09-01 01:17:40 -07001185
perkja49cbd32016-09-16 07:53:41 -07001186 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001187 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07001188 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001189}
1190
mflodmancc3d4422017-08-03 08:27:51 -07001191TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001192 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001193 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1194 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001195
perkja49cbd32016-09-16 07:53:41 -07001196 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001197 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001198
mflodmancc3d4422017-08-03 08:27:51 -07001199 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001200 sink_.SetExpectNoFrames();
Niels Möllerc572ff32018-11-07 08:43:50 +01001201 rtc::Event frame_destroyed_event;
perkja49cbd32016-09-16 07:53:41 -07001202 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
1203 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
perkj26091b12016-09-01 01:17:40 -07001204}
1205
mflodmancc3d4422017-08-03 08:27:51 -07001206TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001207 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001208 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1209 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj26091b12016-09-01 01:17:40 -07001210
1211 fake_encoder_.BlockNextEncode();
perkja49cbd32016-09-16 07:53:41 -07001212 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001213 WaitForEncodedFrame(1);
perkj26091b12016-09-01 01:17:40 -07001214 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
1215 // call to ContinueEncode.
perkja49cbd32016-09-16 07:53:41 -07001216 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1217 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
perkj26091b12016-09-01 01:17:40 -07001218 fake_encoder_.ContinueEncode();
sprang4847ae62017-06-27 07:06:52 -07001219 WaitForEncodedFrame(3);
perkj26091b12016-09-01 01:17:40 -07001220
mflodmancc3d4422017-08-03 08:27:51 -07001221 video_stream_encoder_->Stop();
perkj26091b12016-09-01 01:17:40 -07001222}
1223
Noah Richards51db4212019-06-12 06:59:12 -07001224TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420Conversion) {
1225 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001226 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1227 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001228
1229 rtc::Event frame_destroyed_event;
1230 video_source_.IncomingCapturedFrame(
1231 CreateFakeNativeFrame(1, &frame_destroyed_event));
1232 ExpectDroppedFrame();
1233 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1234 video_stream_encoder_->Stop();
1235}
1236
1237TEST_F(VideoStreamEncoderTest, DropFrameWithFailedI420ConversionWithCrop) {
1238 // Use the cropping factory.
1239 video_encoder_config_.video_stream_factory =
1240 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, 30);
1241 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config_),
1242 kMaxPayloadLength);
1243 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
1244
1245 // Capture a frame at codec_width_/codec_height_.
1246 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001247 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1248 DataRate::bps(kTargetBitrateBps), 0, 0);
Noah Richards51db4212019-06-12 06:59:12 -07001249 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1250 WaitForEncodedFrame(1);
1251 // The encoder will have been configured once.
1252 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1253 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1254 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1255
1256 // Now send in a fake frame that needs to be cropped as the width/height
1257 // aren't divisible by 4 (see CreateEncoderStreams above).
1258 rtc::Event frame_destroyed_event;
1259 video_source_.IncomingCapturedFrame(CreateFakeNativeFrame(
1260 2, &frame_destroyed_event, codec_width_ + 1, codec_height_ + 1));
1261 ExpectDroppedFrame();
1262 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
1263 video_stream_encoder_->Stop();
1264}
1265
mflodmancc3d4422017-08-03 08:27:51 -07001266TEST_F(VideoStreamEncoderTest,
1267 ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001268 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001269 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1270 DataRate::bps(kTargetBitrateBps), 0, 0);
Per21d45d22016-10-30 21:37:57 +01001271 EXPECT_EQ(0, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001272
1273 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001274 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 when the first frame is
1277 // received.
1278 EXPECT_EQ(1, sink_.number_of_reconfigurations());
Per512ecb32016-09-23 15:52:06 +02001279
1280 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02001281 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
Per512ecb32016-09-23 15:52:06 +02001282 video_encoder_config.min_transmit_bitrate_bps = 9999;
mflodmancc3d4422017-08-03 08:27:51 -07001283 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02001284 kMaxPayloadLength);
Per512ecb32016-09-23 15:52:06 +02001285
1286 // Capture a frame and wait for it to synchronize with the encoder thread.
Perf8c5f2b2016-09-23 16:24:55 +02001287 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001288 WaitForEncodedFrame(2);
Per21d45d22016-10-30 21:37:57 +01001289 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkj3b703ed2016-09-29 23:25:40 -07001290 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
perkj26105b42016-09-29 22:39:10 -07001291
mflodmancc3d4422017-08-03 08:27:51 -07001292 video_stream_encoder_->Stop();
perkj26105b42016-09-29 22:39:10 -07001293}
1294
mflodmancc3d4422017-08-03 08:27:51 -07001295TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001296 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001297 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1298 DataRate::bps(kTargetBitrateBps), 0, 0);
perkjfa10b552016-10-02 23:45:26 -07001299
1300 // Capture a frame and wait for it to synchronize with the encoder thread.
1301 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001302 WaitForEncodedFrame(1);
Per21d45d22016-10-30 21:37:57 +01001303 // The encoder will have been configured once.
1304 EXPECT_EQ(1, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001305 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1306 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
1307
1308 codec_width_ *= 2;
1309 codec_height_ *= 2;
1310 // Capture a frame with a higher resolution and wait for it to synchronize
1311 // with the encoder thread.
1312 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
sprang4847ae62017-06-27 07:06:52 -07001313 WaitForEncodedFrame(2);
perkjfa10b552016-10-02 23:45:26 -07001314 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
1315 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
Per21d45d22016-10-30 21:37:57 +01001316 EXPECT_EQ(2, sink_.number_of_reconfigurations());
perkjfa10b552016-10-02 23:45:26 -07001317
mflodmancc3d4422017-08-03 08:27:51 -07001318 video_stream_encoder_->Stop();
perkjfa10b552016-10-02 23:45:26 -07001319}
1320
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001321TEST_F(VideoStreamEncoderTest,
1322 EncoderInstanceDestroyedBeforeAnotherInstanceCreated) {
1323 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001324 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1325 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin443b7ee2019-06-28 12:53:07 +02001326
1327 // Capture a frame and wait for it to synchronize with the encoder thread.
1328 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1329 WaitForEncodedFrame(1);
1330
1331 VideoEncoderConfig video_encoder_config;
1332 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1333 // Changing the max payload data length recreates encoder.
1334 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1335 kMaxPayloadLength / 2);
1336
1337 // Capture a frame and wait for it to synchronize with the encoder thread.
1338 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1339 WaitForEncodedFrame(2);
1340 EXPECT_EQ(1, encoder_factory_.GetMaxNumberOfSimultaneousEncoderInstances());
1341
1342 video_stream_encoder_->Stop();
1343}
1344
Sergey Silkin5ee69672019-07-02 14:18:34 +02001345TEST_F(VideoStreamEncoderTest, BitrateLimitsChangeReconfigureRateAllocator) {
1346 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001347 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1348 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin5ee69672019-07-02 14:18:34 +02001349
1350 VideoEncoderConfig video_encoder_config;
1351 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1352 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
1353 video_stream_encoder_->SetStartBitrate(kStartBitrateBps);
1354 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1355 kMaxPayloadLength);
1356
1357 // Capture a frame and wait for it to synchronize with the encoder thread.
1358 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
1359 WaitForEncodedFrame(1);
1360 // The encoder will have been configured once when the first frame is
1361 // received.
1362 EXPECT_EQ(1, sink_.number_of_reconfigurations());
1363 EXPECT_EQ(kTargetBitrateBps,
1364 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1365 EXPECT_EQ(kStartBitrateBps,
1366 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1367
Sergey Silkin6456e352019-07-08 17:56:40 +02001368 test::FillEncoderConfiguration(kVideoCodecVP8, 1,
1369 &video_encoder_config); //???
Sergey Silkin5ee69672019-07-02 14:18:34 +02001370 video_encoder_config.max_bitrate_bps = kTargetBitrateBps * 2;
1371 video_stream_encoder_->SetStartBitrate(kStartBitrateBps * 2);
1372 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1373 kMaxPayloadLength);
1374
1375 // Capture a frame and wait for it to synchronize with the encoder thread.
1376 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
1377 WaitForEncodedFrame(2);
1378 EXPECT_EQ(2, sink_.number_of_reconfigurations());
1379 // Bitrate limits have changed - rate allocator should be reconfigured,
1380 // encoder should not be reconfigured.
1381 EXPECT_EQ(kTargetBitrateBps * 2,
1382 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1383 EXPECT_EQ(kStartBitrateBps * 2,
1384 bitrate_allocator_factory_.codec_config().startBitrate * 1000);
1385 EXPECT_EQ(1, fake_encoder_.GetNumEncoderInitializations());
1386
1387 video_stream_encoder_->Stop();
1388}
1389
Sergey Silkin6456e352019-07-08 17:56:40 +02001390TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001391 EncoderRecommendedBitrateLimitsDoNotOverrideAppBitrateLimits) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001392 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001393 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1394 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001395
Sergey Silkin6456e352019-07-08 17:56:40 +02001396 VideoEncoderConfig video_encoder_config;
1397 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1398 video_encoder_config.max_bitrate_bps = 0;
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001399 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
Sergey Silkin6456e352019-07-08 17:56:40 +02001400 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1401 kMaxPayloadLength);
1402
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001403 video_source_.IncomingCapturedFrame(CreateFrame(1, 360, 180));
Sergey Silkin6456e352019-07-08 17:56:40 +02001404 WaitForEncodedFrame(1);
Sergey Silkin6456e352019-07-08 17:56:40 +02001405
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001406 // Get the default bitrate limits and use them as baseline for custom
1407 // application and encoder recommended limits.
1408 const uint32_t kDefaultMinBitrateKbps =
1409 bitrate_allocator_factory_.codec_config().minBitrate;
1410 const uint32_t kDefaultMaxBitrateKbps =
1411 bitrate_allocator_factory_.codec_config().maxBitrate;
1412 const uint32_t kEncMinBitrateKbps = kDefaultMinBitrateKbps * 2;
1413 const uint32_t kEncMaxBitrateKbps = kDefaultMaxBitrateKbps * 2;
1414 const uint32_t kAppMinBitrateKbps = kDefaultMinBitrateKbps * 3;
1415 const uint32_t kAppMaxBitrateKbps = kDefaultMaxBitrateKbps * 3;
1416
1417 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1418 codec_width_ * codec_height_, kEncMinBitrateKbps * 1000,
1419 kEncMinBitrateKbps * 1000, kEncMaxBitrateKbps * 1000);
1420 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1421
1422 // Change resolution. This will trigger encoder re-configuration and video
1423 // stream encoder will pick up the bitrate limits recommended by encoder.
1424 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1425 WaitForEncodedFrame(2);
1426 video_source_.IncomingCapturedFrame(CreateFrame(3, 360, 180));
1427 WaitForEncodedFrame(3);
1428
1429 // App bitrate limits are not set - bitrate limits recommended by encoder
1430 // should be used.
1431 EXPECT_EQ(kEncMaxBitrateKbps,
1432 bitrate_allocator_factory_.codec_config().maxBitrate);
1433 EXPECT_EQ(kEncMinBitrateKbps,
1434 bitrate_allocator_factory_.codec_config().minBitrate);
1435
1436 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1437 video_encoder_config.simulcast_layers[0].min_bitrate_bps = 0;
1438 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1439 kMaxPayloadLength);
1440 video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr));
1441 WaitForEncodedFrame(4);
1442
1443 // App limited the max bitrate - bitrate limits recommended by encoder should
1444 // not be applied.
1445 EXPECT_EQ(kAppMaxBitrateKbps,
1446 bitrate_allocator_factory_.codec_config().maxBitrate);
1447 EXPECT_EQ(kDefaultMinBitrateKbps,
1448 bitrate_allocator_factory_.codec_config().minBitrate);
1449
1450 video_encoder_config.max_bitrate_bps = 0;
1451 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1452 kAppMinBitrateKbps * 1000;
1453 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1454 kMaxPayloadLength);
1455 video_source_.IncomingCapturedFrame(CreateFrame(5, nullptr));
1456 WaitForEncodedFrame(5);
1457
1458 // App limited the min bitrate - bitrate limits recommended by encoder should
1459 // not be applied.
1460 EXPECT_EQ(kDefaultMaxBitrateKbps,
1461 bitrate_allocator_factory_.codec_config().maxBitrate);
1462 EXPECT_EQ(kAppMinBitrateKbps,
1463 bitrate_allocator_factory_.codec_config().minBitrate);
1464
1465 video_encoder_config.max_bitrate_bps = kAppMaxBitrateKbps * 1000;
1466 video_encoder_config.simulcast_layers[0].min_bitrate_bps =
1467 kAppMinBitrateKbps * 1000;
Sergey Silkin6456e352019-07-08 17:56:40 +02001468 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
1469 kMaxPayloadLength);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001470 video_source_.IncomingCapturedFrame(CreateFrame(6, nullptr));
1471 WaitForEncodedFrame(6);
Sergey Silkin6456e352019-07-08 17:56:40 +02001472
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001473 // App limited both min and max bitrates - bitrate limits recommended by
1474 // encoder should not be applied.
1475 EXPECT_EQ(kAppMaxBitrateKbps,
1476 bitrate_allocator_factory_.codec_config().maxBitrate);
1477 EXPECT_EQ(kAppMinBitrateKbps,
1478 bitrate_allocator_factory_.codec_config().minBitrate);
Sergey Silkin6456e352019-07-08 17:56:40 +02001479
1480 video_stream_encoder_->Stop();
1481}
1482
1483TEST_F(VideoStreamEncoderTest,
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001484 EncoderRecommendedMaxAndMinBitratesUsedForGivenResolution) {
Sergey Silkin6456e352019-07-08 17:56:40 +02001485 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001486 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1487 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6456e352019-07-08 17:56:40 +02001488
1489 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_270p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001490 480 * 270, 34 * 1000, 12 * 1000, 1234 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001491 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits_360p(
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001492 640 * 360, 43 * 1000, 21 * 1000, 2345 * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001493 fake_encoder_.SetResolutionBitrateLimits(
1494 {encoder_bitrate_limits_270p, encoder_bitrate_limits_360p});
1495
1496 VideoEncoderConfig video_encoder_config;
1497 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1498 video_encoder_config.max_bitrate_bps = 0;
1499 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1500 kMaxPayloadLength);
1501
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001502 // 270p. The bitrate limits recommended by encoder for 270p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001503 video_source_.IncomingCapturedFrame(CreateFrame(1, 480, 270));
1504 WaitForEncodedFrame(1);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001505 EXPECT_EQ(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_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1508 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1509
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001510 // 360p. The bitrate limits recommended by encoder for 360p should be used.
Sergey Silkin6456e352019-07-08 17:56:40 +02001511 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1512 WaitForEncodedFrame(2);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001513 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1514 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001515 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1516 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1517
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001518 // Resolution between 270p and 360p. The bitrate limits recommended by
Sergey Silkin6456e352019-07-08 17:56:40 +02001519 // encoder for 360p should be used.
1520 video_source_.IncomingCapturedFrame(
1521 CreateFrame(3, (640 + 480) / 2, (360 + 270) / 2));
1522 WaitForEncodedFrame(3);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001523 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1524 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001525 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1526 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1527
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001528 // Resolution higher than 360p. The caps recommended by encoder should be
Sergey Silkin6456e352019-07-08 17:56:40 +02001529 // ignored.
1530 video_source_.IncomingCapturedFrame(CreateFrame(4, 960, 540));
1531 WaitForEncodedFrame(4);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001532 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1533 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001534 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1535 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001536 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.min_bitrate_bps),
1537 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001538 EXPECT_NE(static_cast<uint32_t>(encoder_bitrate_limits_360p.max_bitrate_bps),
1539 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1540
1541 // Resolution lower than 270p. The max bitrate limit recommended by encoder
1542 // for 270p should be used.
1543 video_source_.IncomingCapturedFrame(CreateFrame(5, 320, 180));
1544 WaitForEncodedFrame(5);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001545 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.min_bitrate_bps),
1546 bitrate_allocator_factory_.codec_config().minBitrate * 1000);
Sergey Silkin6456e352019-07-08 17:56:40 +02001547 EXPECT_EQ(static_cast<uint32_t>(encoder_bitrate_limits_270p.max_bitrate_bps),
1548 bitrate_allocator_factory_.codec_config().maxBitrate * 1000);
1549
1550 video_stream_encoder_->Stop();
1551}
1552
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001553TEST_F(VideoStreamEncoderTest, EncoderRecommendedMaxBitrateCapsTargetBitrate) {
1554 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001555 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1556 DataRate::bps(kTargetBitrateBps), 0, 0);
Sergey Silkin6b2cec12019-08-09 16:04:05 +02001557
1558 VideoEncoderConfig video_encoder_config;
1559 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
1560 video_encoder_config.max_bitrate_bps = 0;
1561 video_stream_encoder_->ConfigureEncoder(video_encoder_config.Copy(),
1562 kMaxPayloadLength);
1563
1564 // Encode 720p frame to get the default encoder target bitrate.
1565 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1566 WaitForEncodedFrame(1);
1567 const uint32_t kDefaultTargetBitrateFor720pKbps =
1568 bitrate_allocator_factory_.codec_config()
1569 .simulcastStream[0]
1570 .targetBitrate;
1571
1572 // Set the max recommended encoder bitrate to something lower than the default
1573 // target bitrate.
1574 const VideoEncoder::ResolutionBitrateLimits encoder_bitrate_limits(
1575 1280 * 720, 10 * 1000, 10 * 1000,
1576 kDefaultTargetBitrateFor720pKbps / 2 * 1000);
1577 fake_encoder_.SetResolutionBitrateLimits({encoder_bitrate_limits});
1578
1579 // Change resolution to trigger encoder reinitialization.
1580 video_source_.IncomingCapturedFrame(CreateFrame(2, 640, 360));
1581 WaitForEncodedFrame(2);
1582 video_source_.IncomingCapturedFrame(CreateFrame(3, 1280, 720));
1583 WaitForEncodedFrame(3);
1584
1585 // Ensure the target bitrate is capped by the max bitrate.
1586 EXPECT_EQ(bitrate_allocator_factory_.codec_config().maxBitrate * 1000,
1587 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1588 EXPECT_EQ(bitrate_allocator_factory_.codec_config()
1589 .simulcastStream[0]
1590 .targetBitrate *
1591 1000,
1592 static_cast<uint32_t>(encoder_bitrate_limits.max_bitrate_bps));
1593
1594 video_stream_encoder_->Stop();
1595}
1596
mflodmancc3d4422017-08-03 08:27:51 -07001597TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
perkj803d97f2016-11-01 11:45:46 -07001598 EXPECT_TRUE(video_source_.has_sinks());
1599 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001600 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001601 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07001602 EXPECT_FALSE(video_source_.has_sinks());
1603 EXPECT_TRUE(new_video_source.has_sinks());
1604
mflodmancc3d4422017-08-03 08:27:51 -07001605 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001606}
1607
mflodmancc3d4422017-08-03 08:27:51 -07001608TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) {
perkj803d97f2016-11-01 11:45:46 -07001609 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001610 video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/);
perkj803d97f2016-11-01 11:45:46 -07001611 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
mflodmancc3d4422017-08-03 08:27:51 -07001612 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001613}
1614
Jonathan Yubc771b72017-12-08 17:04:29 -08001615TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) {
1616 const int kFramerateFps = 30;
asaperssonf7e294d2017-06-13 23:25:22 -07001617 const int kWidth = 1280;
1618 const int kHeight = 720;
Jonathan Yubc771b72017-12-08 17:04:29 -08001619
1620 // We rely on the automatic resolution adaptation, but we handle framerate
1621 // adaptation manually by mocking the stats proxy.
1622 video_source_.set_adaptation_enabled(true);
asaperssonf7e294d2017-06-13 23:25:22 -07001623
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001624 // Enable BALANCED preference, no initial limitation.
Erik Språng4c6ca302019-04-08 15:14:01 +02001625 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001626 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1627 DataRate::bps(kTargetBitrateBps), 0, 0);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001628 video_stream_encoder_->SetSource(&video_source_,
1629 webrtc::DegradationPreference::BALANCED);
Jonathan Yubc771b72017-12-08 17:04:29 -08001630 VerifyNoLimitation(video_source_.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07001631 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001632 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asaperssonf7e294d2017-06-13 23:25:22 -07001633 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1634
Jonathan Yubc771b72017-12-08 17:04:29 -08001635 // Adapt down as far as possible.
1636 rtc::VideoSinkWants last_wants;
1637 int64_t t = 1;
1638 int loop_count = 0;
1639 do {
1640 ++loop_count;
1641 last_wants = video_source_.sink_wants();
1642
1643 // Simulate the framerate we've been asked to adapt to.
1644 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1645 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1646 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1647 mock_stats.input_frame_rate = fps;
1648 stats_proxy_->SetMockStats(mock_stats);
1649
1650 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1651 sink_.WaitForEncodedFrame(t);
1652 t += frame_interval_ms;
1653
mflodmancc3d4422017-08-03 08:27:51 -07001654 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08001655 VerifyBalancedModeFpsRange(
1656 video_source_.sink_wants(),
1657 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1658 } while (video_source_.sink_wants().max_pixel_count <
1659 last_wants.max_pixel_count ||
1660 video_source_.sink_wants().max_framerate_fps <
1661 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001662
Jonathan Yubc771b72017-12-08 17:04:29 -08001663 // Verify that we've adapted all the way down.
1664 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001665 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001666 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
1667 EXPECT_EQ(loop_count - 1,
asaperssonf7e294d2017-06-13 23:25:22 -07001668 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
Jonathan Yubc771b72017-12-08 17:04:29 -08001669 EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() *
1670 *video_source_.last_sent_height());
1671 EXPECT_EQ(kMinBalancedFramerateFps,
1672 video_source_.sink_wants().max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001673
Jonathan Yubc771b72017-12-08 17:04:29 -08001674 // Adapt back up the same number of times we adapted down.
1675 for (int i = 0; i < loop_count - 1; ++i) {
1676 last_wants = video_source_.sink_wants();
1677
1678 // Simulate the framerate we've been asked to adapt to.
1679 const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps);
1680 const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps;
1681 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1682 mock_stats.input_frame_rate = fps;
1683 stats_proxy_->SetMockStats(mock_stats);
1684
1685 video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight));
1686 sink_.WaitForEncodedFrame(t);
1687 t += frame_interval_ms;
1688
mflodmancc3d4422017-08-03 08:27:51 -07001689 video_stream_encoder_->TriggerCpuNormalUsage();
Jonathan Yubc771b72017-12-08 17:04:29 -08001690 VerifyBalancedModeFpsRange(
1691 video_source_.sink_wants(),
1692 *video_source_.last_sent_width() * *video_source_.last_sent_height());
1693 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count >
1694 last_wants.max_pixel_count ||
1695 video_source_.sink_wants().max_framerate_fps >
1696 last_wants.max_framerate_fps);
asaperssonf7e294d2017-06-13 23:25:22 -07001697 }
1698
Åsa Persson8c1bf952018-09-13 10:42:19 +02001699 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08001700 stats_proxy_->ResetMockStats();
asaperssonf7e294d2017-06-13 23:25:22 -07001701 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08001702 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
1703 EXPECT_EQ((loop_count - 1) * 2,
1704 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssonf7e294d2017-06-13 23:25:22 -07001705
mflodmancc3d4422017-08-03 08:27:51 -07001706 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07001707}
mflodmancc3d4422017-08-03 08:27:51 -07001708TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001709 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001710 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1711 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07001712 VerifyNoLimitation(video_source_.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001713
sprangc5d62e22017-04-02 23:53:04 -07001714 const int kFrameWidth = 1280;
1715 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07001716
Åsa Persson8c1bf952018-09-13 10:42:19 +02001717 int64_t frame_timestamp = 1;
perkj803d97f2016-11-01 11:45:46 -07001718
kthelgason5e13d412016-12-01 03:59:51 -08001719 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001720 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001721 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001722 frame_timestamp += kFrameIntervalMs;
1723
perkj803d97f2016-11-01 11:45:46 -07001724 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001725 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001726 video_source_.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001727 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001728 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001729 frame_timestamp += kFrameIntervalMs;
sprang3ea3c772017-03-30 07:23:48 -07001730
asapersson0944a802017-04-07 00:57:58 -07001731 // Default degradation preference is maintain-framerate, so will lower max
sprangc5d62e22017-04-02 23:53:04 -07001732 // wanted resolution.
1733 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1734 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1735 kFrameWidth * kFrameHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001736 EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001737
1738 // Set new source, switch to maintain-resolution.
perkj803d97f2016-11-01 11:45:46 -07001739 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001740 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001741 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07001742
sprangc5d62e22017-04-02 23:53:04 -07001743 // Initially no degradation registered.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001744 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001745
sprangc5d62e22017-04-02 23:53:04 -07001746 // Force an input frame rate to be available, or the adaptation call won't
1747 // know what framerate to adapt form.
asapersson02465b82017-04-10 01:12:52 -07001748 const int kInputFps = 30;
sprangc5d62e22017-04-02 23:53:04 -07001749 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson02465b82017-04-10 01:12:52 -07001750 stats.input_frame_rate = kInputFps;
sprangc5d62e22017-04-02 23:53:04 -07001751 stats_proxy_->SetMockStats(stats);
1752
mflodmancc3d4422017-08-03 08:27:51 -07001753 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07001754 new_video_source.IncomingCapturedFrame(
sprangc5d62e22017-04-02 23:53:04 -07001755 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001756 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001757 frame_timestamp += kFrameIntervalMs;
1758
1759 // Some framerate constraint should be set.
sprang84a37592017-02-10 07:04:27 -08001760 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
sprangc5d62e22017-04-02 23:53:04 -07001761 EXPECT_EQ(std::numeric_limits<int>::max(),
1762 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001763 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
sprangc5d62e22017-04-02 23:53:04 -07001764
asapersson02465b82017-04-10 01:12:52 -07001765 // Turn off degradation completely.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001766 video_stream_encoder_->SetSource(&new_video_source,
1767 webrtc::DegradationPreference::DISABLED);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001768 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
sprangc5d62e22017-04-02 23:53:04 -07001769
mflodmancc3d4422017-08-03 08:27:51 -07001770 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07001771 new_video_source.IncomingCapturedFrame(
1772 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07001773 WaitForEncodedFrame(frame_timestamp);
sprangc5d62e22017-04-02 23:53:04 -07001774 frame_timestamp += kFrameIntervalMs;
1775
1776 // Still no degradation.
Åsa Persson8c1bf952018-09-13 10:42:19 +02001777 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
perkj803d97f2016-11-01 11:45:46 -07001778
1779 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001780 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001781 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
sprangc5d62e22017-04-02 23:53:04 -07001782 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
1783 kFrameWidth * kFrameHeight);
sprang84a37592017-02-10 07:04:27 -08001784 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
Åsa Persson8c1bf952018-09-13 10:42:19 +02001785 EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07001786
1787 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
mflodmancc3d4422017-08-03 08:27:51 -07001788 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001789 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07001790 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
1791 EXPECT_EQ(std::numeric_limits<int>::max(),
1792 new_video_source.sink_wants().max_pixel_count);
asapersson02465b82017-04-10 01:12:52 -07001793 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
perkj803d97f2016-11-01 11:45:46 -07001794
mflodmancc3d4422017-08-03 08:27:51 -07001795 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001796}
1797
mflodmancc3d4422017-08-03 08:27:51 -07001798TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001799 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001800 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1801 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07001802
asaperssonfab67072017-04-04 05:51:49 -07001803 const int kWidth = 1280;
1804 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001805 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001806 WaitForEncodedFrame(1);
asaperssonfab67072017-04-04 05:51:49 -07001807 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1808 EXPECT_FALSE(stats.bw_limited_resolution);
1809 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1810
1811 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001812 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001813 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001814 WaitForEncodedFrame(2);
asaperssonfab67072017-04-04 05:51:49 -07001815
1816 stats = stats_proxy_->GetStats();
1817 EXPECT_TRUE(stats.bw_limited_resolution);
1818 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1819
1820 // Trigger adapt up.
mflodmancc3d4422017-08-03 08:27:51 -07001821 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07001822 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001823 WaitForEncodedFrame(3);
asaperssonfab67072017-04-04 05:51:49 -07001824
1825 stats = stats_proxy_->GetStats();
1826 EXPECT_FALSE(stats.bw_limited_resolution);
1827 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
1828 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1829
mflodmancc3d4422017-08-03 08:27:51 -07001830 video_stream_encoder_->Stop();
asaperssonfab67072017-04-04 05:51:49 -07001831}
1832
mflodmancc3d4422017-08-03 08:27:51 -07001833TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001834 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001835 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1836 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07001837
1838 const int kWidth = 1280;
1839 const int kHeight = 720;
asaperssonfab67072017-04-04 05:51:49 -07001840 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001841 WaitForEncodedFrame(1);
perkj803d97f2016-11-01 11:45:46 -07001842 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1843 EXPECT_FALSE(stats.cpu_limited_resolution);
1844 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1845
1846 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001847 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001848 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001849 WaitForEncodedFrame(2);
perkj803d97f2016-11-01 11:45:46 -07001850
1851 stats = stats_proxy_->GetStats();
1852 EXPECT_TRUE(stats.cpu_limited_resolution);
1853 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1854
1855 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001856 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001857 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001858 WaitForEncodedFrame(3);
perkj803d97f2016-11-01 11:45:46 -07001859
1860 stats = stats_proxy_->GetStats();
1861 EXPECT_FALSE(stats.cpu_limited_resolution);
1862 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asaperssonfab67072017-04-04 05:51:49 -07001863 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07001864
mflodmancc3d4422017-08-03 08:27:51 -07001865 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07001866}
1867
mflodmancc3d4422017-08-03 08:27:51 -07001868TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001869 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001870 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1871 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001872
asaperssonfab67072017-04-04 05:51:49 -07001873 const int kWidth = 1280;
1874 const int kHeight = 720;
1875 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001876 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001877 VideoSendStream::Stats stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001878 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001879 EXPECT_FALSE(stats.cpu_limited_resolution);
1880 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1881
asaperssonfab67072017-04-04 05:51:49 -07001882 // Trigger CPU overuse.
mflodmancc3d4422017-08-03 08:27:51 -07001883 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07001884 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001885 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001886 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001887 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001888 EXPECT_TRUE(stats.cpu_limited_resolution);
1889 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1890
1891 // Set new source with adaptation still enabled.
1892 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07001893 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001894 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001895
asaperssonfab67072017-04-04 05:51:49 -07001896 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001897 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001898 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001899 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001900 EXPECT_TRUE(stats.cpu_limited_resolution);
1901 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1902
1903 // Set adaptation disabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001904 video_stream_encoder_->SetSource(&new_video_source,
1905 webrtc::DegradationPreference::DISABLED);
kthelgason876222f2016-11-29 01:44:11 -08001906
asaperssonfab67072017-04-04 05:51:49 -07001907 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001908 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001909 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001910 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001911 EXPECT_FALSE(stats.cpu_limited_resolution);
1912 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1913
1914 // Set adaptation back to enabled.
mflodmancc3d4422017-08-03 08:27:51 -07001915 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001916 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
kthelgason876222f2016-11-29 01:44:11 -08001917
asaperssonfab67072017-04-04 05:51:49 -07001918 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001919 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001920 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001921 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001922 EXPECT_TRUE(stats.cpu_limited_resolution);
1923 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1924
asaperssonfab67072017-04-04 05:51:49 -07001925 // Trigger CPU normal use.
mflodmancc3d4422017-08-03 08:27:51 -07001926 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07001927 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001928 WaitForEncodedFrame(6);
kthelgason876222f2016-11-29 01:44:11 -08001929 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07001930 EXPECT_FALSE(stats.bw_limited_resolution);
kthelgason876222f2016-11-29 01:44:11 -08001931 EXPECT_FALSE(stats.cpu_limited_resolution);
1932 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07001933 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001934
mflodmancc3d4422017-08-03 08:27:51 -07001935 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001936}
1937
mflodmancc3d4422017-08-03 08:27:51 -07001938TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02001939 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02001940 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
1941 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08001942
asaperssonfab67072017-04-04 05:51:49 -07001943 const int kWidth = 1280;
1944 const int kHeight = 720;
1945 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001946 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08001947 VideoSendStream::Stats stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001948 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001949 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001950 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001951
1952 // Set new source with adaptation still enabled.
1953 test::FrameForwarder new_video_source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001954 video_stream_encoder_->SetSource(&new_video_source,
1955 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001956
asaperssonfab67072017-04-04 05:51:49 -07001957 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001958 WaitForEncodedFrame(2);
kthelgason876222f2016-11-29 01:44:11 -08001959 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001960 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001961 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001962 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001963
asaperssonfab67072017-04-04 05:51:49 -07001964 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07001965 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07001966 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001967 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08001968 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001969 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001970 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001971 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001972
asaperssonfab67072017-04-04 05:51:49 -07001973 // Set new source with adaptation still enabled.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001974 video_stream_encoder_->SetSource(&new_video_source,
1975 webrtc::DegradationPreference::BALANCED);
kthelgason876222f2016-11-29 01:44:11 -08001976
asaperssonfab67072017-04-04 05:51:49 -07001977 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001978 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08001979 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001980 EXPECT_TRUE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001981 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001982 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001983
asapersson02465b82017-04-10 01:12:52 -07001984 // Disable resolution scaling.
mflodmancc3d4422017-08-03 08:27:51 -07001985 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001986 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08001987
asaperssonfab67072017-04-04 05:51:49 -07001988 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07001989 WaitForEncodedFrame(5);
kthelgason876222f2016-11-29 01:44:11 -08001990 stats = stats_proxy_->GetStats();
kthelgason876222f2016-11-29 01:44:11 -08001991 EXPECT_FALSE(stats.bw_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07001992 EXPECT_FALSE(stats.bw_limited_framerate);
asaperssonfab67072017-04-04 05:51:49 -07001993 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1994 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
kthelgason876222f2016-11-29 01:44:11 -08001995
mflodmancc3d4422017-08-03 08:27:51 -07001996 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08001997}
1998
mflodmancc3d4422017-08-03 08:27:51 -07001999TEST_F(VideoStreamEncoderTest,
2000 QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002001 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002002 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2003 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson36e9eb42017-03-31 05:29:12 -07002004
2005 const int kWidth = 1280;
2006 const int kHeight = 720;
Åsa Persson8c1bf952018-09-13 10:42:19 +02002007 int64_t timestamp_ms = kFrameIntervalMs;
asapersson36e9eb42017-03-31 05:29:12 -07002008 video_source_.set_adaptation_enabled(true);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002009 video_source_.IncomingCapturedFrame(
2010 CreateFrame(timestamp_ms, kWidth, kHeight));
2011 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002012 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2013 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2014 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2015
2016 // Trigger adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002017 video_stream_encoder_->TriggerQualityLow();
Å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_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2023 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2024 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2025
2026 // Trigger overuse.
mflodmancc3d4422017-08-03 08:27:51 -07002027 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002028 timestamp_ms += kFrameIntervalMs;
2029 video_source_.IncomingCapturedFrame(
2030 CreateFrame(timestamp_ms, kWidth, kHeight));
2031 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002032 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2033 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2034 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2035
Niels Möller4db138e2018-04-19 09:04:13 +02002036 // Leave source unchanged, but disable quality scaler.
asapersson36e9eb42017-03-31 05:29:12 -07002037 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02002038
2039 VideoEncoderConfig video_encoder_config;
2040 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
2041 // Make format different, to force recreation of encoder.
2042 video_encoder_config.video_format.parameters["foo"] = "foo";
2043 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02002044 kMaxPayloadLength);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002045 timestamp_ms += kFrameIntervalMs;
2046 video_source_.IncomingCapturedFrame(
2047 CreateFrame(timestamp_ms, kWidth, kHeight));
2048 WaitForEncodedFrame(timestamp_ms);
asapersson36e9eb42017-03-31 05:29:12 -07002049 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2050 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2051 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2052
mflodmancc3d4422017-08-03 08:27:51 -07002053 video_stream_encoder_->Stop();
asapersson36e9eb42017-03-31 05:29:12 -07002054}
2055
mflodmancc3d4422017-08-03 08:27:51 -07002056TEST_F(VideoStreamEncoderTest,
2057 StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
Erik Språng4c6ca302019-04-08 15:14:01 +02002058 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002059 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2060 DataRate::bps(kTargetBitrateBps), 0, 0);
perkj803d97f2016-11-01 11:45:46 -07002061
asapersson0944a802017-04-07 00:57:58 -07002062 const int kWidth = 1280;
2063 const int kHeight = 720;
sprang84a37592017-02-10 07:04:27 -08002064 int sequence = 1;
perkj803d97f2016-11-01 11:45:46 -07002065
asaperssonfab67072017-04-04 05:51:49 -07002066 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002067 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002068 VideoSendStream::Stats stats = stats_proxy_->GetStats();
sprang84a37592017-02-10 07:04:27 -08002069 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002070 EXPECT_FALSE(stats.cpu_limited_framerate);
sprang84a37592017-02-10 07:04:27 -08002071 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
2072
asapersson02465b82017-04-10 01:12:52 -07002073 // Trigger CPU overuse, should now adapt down.
mflodmancc3d4422017-08-03 08:27:51 -07002074 video_stream_encoder_->TriggerCpuOveruse();
asaperssonfab67072017-04-04 05:51:49 -07002075 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002076 WaitForEncodedFrame(sequence++);
sprang84a37592017-02-10 07:04:27 -08002077 stats = stats_proxy_->GetStats();
perkj803d97f2016-11-01 11:45:46 -07002078 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002079 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002080 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2081
2082 // Set new source with adaptation still enabled.
2083 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002084 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002085 &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07002086
2087 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002088 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002089 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002090 stats = stats_proxy_->GetStats();
2091 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002092 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002093 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2094
sprangc5d62e22017-04-02 23:53:04 -07002095 // Set cpu adaptation by frame dropping.
mflodmancc3d4422017-08-03 08:27:51 -07002096 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002097 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
perkj803d97f2016-11-01 11:45:46 -07002098 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002099 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002100 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002101 stats = stats_proxy_->GetStats();
sprangc5d62e22017-04-02 23:53:04 -07002102 // Not adapted at first.
perkj803d97f2016-11-01 11:45:46 -07002103 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson09f05612017-05-15 23:40:18 -07002104 EXPECT_FALSE(stats.cpu_limited_framerate);
perkj803d97f2016-11-01 11:45:46 -07002105 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
2106
sprangc5d62e22017-04-02 23:53:04 -07002107 // Force an input frame rate to be available, or the adaptation call won't
asapersson09f05612017-05-15 23:40:18 -07002108 // know what framerate to adapt from.
sprangc5d62e22017-04-02 23:53:04 -07002109 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
2110 mock_stats.input_frame_rate = 30;
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 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002116 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002117 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002118
2119 // Framerate now adapted.
2120 stats = stats_proxy_->GetStats();
asapersson09f05612017-05-15 23:40:18 -07002121 EXPECT_FALSE(stats.cpu_limited_resolution);
2122 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002123 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2124
2125 // Disable CPU adaptation.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002126 video_stream_encoder_->SetSource(&new_video_source,
2127 webrtc::DegradationPreference::DISABLED);
sprangc5d62e22017-04-02 23:53:04 -07002128 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002129 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002130 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002131
2132 stats = stats_proxy_->GetStats();
2133 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002134 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002135 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2136
2137 // Try to trigger overuse. Should not succeed.
2138 stats_proxy_->SetMockStats(mock_stats);
mflodmancc3d4422017-08-03 08:27:51 -07002139 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07002140 stats_proxy_->ResetMockStats();
2141
2142 stats = stats_proxy_->GetStats();
2143 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002144 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002145 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
2146
2147 // Switch back the source with resolution adaptation enabled.
mflodmancc3d4422017-08-03 08:27:51 -07002148 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002149 &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssonfab67072017-04-04 05:51:49 -07002150 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002151 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002152 stats = stats_proxy_->GetStats();
2153 EXPECT_TRUE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002154 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002155 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002156
2157 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002158 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonfab67072017-04-04 05:51:49 -07002159 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002160 WaitForEncodedFrame(sequence++);
perkj803d97f2016-11-01 11:45:46 -07002161 stats = stats_proxy_->GetStats();
2162 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002163 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002164 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2165
2166 // Back to the source with adaptation off, set it back to maintain-resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002167 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002168 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07002169 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002170 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002171 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002172 stats = stats_proxy_->GetStats();
asapersson13874762017-06-07 00:01:02 -07002173 // Disabled, since we previously switched the source to disabled.
sprangc5d62e22017-04-02 23:53:04 -07002174 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002175 EXPECT_TRUE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002176 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
2177
2178 // Trigger CPU normal usage.
mflodmancc3d4422017-08-03 08:27:51 -07002179 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07002180 new_video_source.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07002181 CreateFrame(sequence, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002182 WaitForEncodedFrame(sequence++);
sprangc5d62e22017-04-02 23:53:04 -07002183 stats = stats_proxy_->GetStats();
2184 EXPECT_FALSE(stats.cpu_limited_resolution);
asapersson13874762017-06-07 00:01:02 -07002185 EXPECT_FALSE(stats.cpu_limited_framerate);
sprangc5d62e22017-04-02 23:53:04 -07002186 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
asapersson02465b82017-04-10 01:12:52 -07002187 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
perkj803d97f2016-11-01 11:45:46 -07002188
mflodmancc3d4422017-08-03 08:27:51 -07002189 video_stream_encoder_->Stop();
perkj803d97f2016-11-01 11:45:46 -07002190}
2191
mflodmancc3d4422017-08-03 08:27:51 -07002192TEST_F(VideoStreamEncoderTest,
2193 ScalingUpAndDownDoesNothingWithMaintainResolution) {
asaperssonfab67072017-04-04 05:51:49 -07002194 const int kWidth = 1280;
2195 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002196 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002197 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2198 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason876222f2016-11-29 01:44:11 -08002199
asaperssonfab67072017-04-04 05:51:49 -07002200 // Expect no scaling to begin with.
asapersson02465b82017-04-10 01:12:52 -07002201 VerifyNoLimitation(video_source_.sink_wants());
kthelgason876222f2016-11-29 01:44:11 -08002202
asaperssonfab67072017-04-04 05:51:49 -07002203 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002204 WaitForEncodedFrame(1);
kthelgason876222f2016-11-29 01:44:11 -08002205
asaperssonfab67072017-04-04 05:51:49 -07002206 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002207 video_stream_encoder_->TriggerQualityLow();
kthelgason5e13d412016-12-01 03:59:51 -08002208
asaperssonfab67072017-04-04 05:51:49 -07002209 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002210 WaitForEncodedFrame(2);
kthelgason5e13d412016-12-01 03:59:51 -08002211
kthelgason876222f2016-11-29 01:44:11 -08002212 // Expect a scale down.
2213 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
asaperssonfab67072017-04-04 05:51:49 -07002214 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason876222f2016-11-29 01:44:11 -08002215
asapersson02465b82017-04-10 01:12:52 -07002216 // Set resolution scaling disabled.
kthelgason876222f2016-11-29 01:44:11 -08002217 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002218 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002219 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason876222f2016-11-29 01:44:11 -08002220
asaperssonfab67072017-04-04 05:51:49 -07002221 // Trigger scale down.
mflodmancc3d4422017-08-03 08:27:51 -07002222 video_stream_encoder_->TriggerQualityLow();
asaperssonfab67072017-04-04 05:51:49 -07002223 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002224 WaitForEncodedFrame(3);
kthelgason876222f2016-11-29 01:44:11 -08002225
asaperssonfab67072017-04-04 05:51:49 -07002226 // Expect no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002227 EXPECT_EQ(std::numeric_limits<int>::max(),
2228 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002229
asaperssonfab67072017-04-04 05:51:49 -07002230 // Trigger scale up.
mflodmancc3d4422017-08-03 08:27:51 -07002231 video_stream_encoder_->TriggerQualityHigh();
asaperssonfab67072017-04-04 05:51:49 -07002232 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002233 WaitForEncodedFrame(4);
kthelgason876222f2016-11-29 01:44:11 -08002234
asapersson02465b82017-04-10 01:12:52 -07002235 // Expect nothing to change, still no scaling.
sprangc5d62e22017-04-02 23:53:04 -07002236 EXPECT_EQ(std::numeric_limits<int>::max(),
2237 new_video_source.sink_wants().max_pixel_count);
kthelgason876222f2016-11-29 01:44:11 -08002238
mflodmancc3d4422017-08-03 08:27:51 -07002239 video_stream_encoder_->Stop();
kthelgason876222f2016-11-29 01:44:11 -08002240}
2241
mflodmancc3d4422017-08-03 08:27:51 -07002242TEST_F(VideoStreamEncoderTest,
2243 SkipsSameAdaptDownRequest_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002244 const int kWidth = 1280;
2245 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002246 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002247 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2248 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002249
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002250 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002251 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002252 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002253 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002254
2255 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002256 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002257 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002258 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2259 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2260
2261 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002262 video_stream_encoder_->TriggerCpuOveruse();
asapersson09f05612017-05-15 23:40:18 -07002263 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002264 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2265 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2266 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2267
2268 // Trigger adapt down for same input resolution, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002269 video_stream_encoder_->TriggerCpuOveruse();
asapersson02465b82017-04-10 01:12:52 -07002270 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2271 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2272 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2273
mflodmancc3d4422017-08-03 08:27:51 -07002274 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002275}
2276
mflodmancc3d4422017-08-03 08:27:51 -07002277TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002278 const int kWidth = 1280;
2279 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002280 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002281 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2282 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002283
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002284 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002285 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002286 video_stream_encoder_->SetSource(&source,
2287 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002288 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2289 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002290 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002291
2292 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002293 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002294 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2295 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2296 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2297 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
2298
2299 // Trigger adapt down for same input resolution, expect no change.
2300 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2301 sink_.WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07002302 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002303 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2304 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2305 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2306
2307 // Trigger adapt down for larger input resolution, expect no change.
2308 source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1));
2309 sink_.WaitForEncodedFrame(3);
mflodmancc3d4422017-08-03 08:27:51 -07002310 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07002311 EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count);
2312 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2313 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2314
mflodmancc3d4422017-08-03 08:27:51 -07002315 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002316}
2317
mflodmancc3d4422017-08-03 08:27:51 -07002318TEST_F(VideoStreamEncoderTest,
2319 NoChangeForInitialNormalUsage_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002320 const int kWidth = 1280;
2321 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002322 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002323 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2324 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002325
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002326 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002327 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002328 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002329 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002330
2331 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002332 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002333 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002334 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2335 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2336
2337 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002338 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002339 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002340 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2341 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2342
mflodmancc3d4422017-08-03 08:27:51 -07002343 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002344}
2345
mflodmancc3d4422017-08-03 08:27:51 -07002346TEST_F(VideoStreamEncoderTest,
2347 NoChangeForInitialNormalUsage_MaintainResolutionMode) {
asapersson02465b82017-04-10 01:12:52 -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);
asapersson02465b82017-04-10 01:12:52 -07002353
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002354 // Enable MAINTAIN_RESOLUTION preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002355 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07002356 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002357 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
asapersson02465b82017-04-10 01:12:52 -07002358
2359 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002360 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002361 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002362 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002363 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2364
2365 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002366 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002367 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002368 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
asapersson02465b82017-04-10 01:12:52 -07002369 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2370
mflodmancc3d4422017-08-03 08:27:51 -07002371 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002372}
2373
mflodmancc3d4422017-08-03 08:27:51 -07002374TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07002375 const int kWidth = 1280;
2376 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002377 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002378 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2379 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002380
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002381 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002382 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002383 video_stream_encoder_->SetSource(&source,
2384 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002385
2386 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2387 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002388 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002389 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2390 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2391 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2392
2393 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002394 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002395 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002396 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2397 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2398 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2399
mflodmancc3d4422017-08-03 08:27:51 -07002400 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002401}
2402
mflodmancc3d4422017-08-03 08:27:51 -07002403TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) {
asapersson09f05612017-05-15 23:40:18 -07002404 const int kWidth = 1280;
2405 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002406 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002407 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2408 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002409
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002410 // Enable DISABLED preference, no initial limitation.
asapersson09f05612017-05-15 23:40:18 -07002411 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002412 video_stream_encoder_->SetSource(&source,
2413 webrtc::DegradationPreference::DISABLED);
asapersson09f05612017-05-15 23:40:18 -07002414
2415 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2416 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002417 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002418 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2419 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2420 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2421
2422 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07002423 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002424 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002425 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2426 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
2427 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2428
mflodmancc3d4422017-08-03 08:27:51 -07002429 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002430}
2431
mflodmancc3d4422017-08-03 08:27:51 -07002432TEST_F(VideoStreamEncoderTest,
2433 AdaptsResolutionForLowQuality_MaintainFramerateMode) {
asapersson02465b82017-04-10 01:12:52 -07002434 const int kWidth = 1280;
2435 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002436 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002437 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2438 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson02465b82017-04-10 01:12:52 -07002439
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002440 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asapersson02465b82017-04-10 01:12:52 -07002441 AdaptingFrameForwarder source;
2442 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002443 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002444 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asapersson02465b82017-04-10 01:12:52 -07002445
2446 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002447 WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002448 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002449 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2450 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2451
2452 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002453 video_stream_encoder_->TriggerQualityLow();
asapersson02465b82017-04-10 01:12:52 -07002454 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002455 WaitForEncodedFrame(2);
asapersson09f05612017-05-15 23:40:18 -07002456 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asapersson02465b82017-04-10 01:12:52 -07002457 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2458 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2459
2460 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002461 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002462 VerifyFpsMaxResolutionMax(source.sink_wants());
asapersson02465b82017-04-10 01:12:52 -07002463 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2464 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2465 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2466
mflodmancc3d4422017-08-03 08:27:51 -07002467 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07002468}
2469
mflodmancc3d4422017-08-03 08:27:51 -07002470TEST_F(VideoStreamEncoderTest,
2471 AdaptsFramerateForLowQuality_MaintainResolutionMode) {
asapersson09f05612017-05-15 23:40:18 -07002472 const int kWidth = 1280;
2473 const int kHeight = 720;
2474 const int kInputFps = 30;
Erik Språng4c6ca302019-04-08 15:14:01 +02002475 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002476 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2477 DataRate::bps(kTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07002478
2479 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2480 stats.input_frame_rate = kInputFps;
2481 stats_proxy_->SetMockStats(stats);
2482
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002483 // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE).
asapersson09f05612017-05-15 23:40:18 -07002484 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
2485 sink_.WaitForEncodedFrame(1);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002486 VerifyFpsMaxResolutionMax(video_source_.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002487
2488 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002489 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002490 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
2491 sink_.WaitForEncodedFrame(2);
2492 VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight);
2493
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002494 // Enable MAINTAIN_RESOLUTION preference.
asapersson09f05612017-05-15 23:40:18 -07002495 test::FrameForwarder new_video_source;
mflodmancc3d4422017-08-03 08:27:51 -07002496 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002497 &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002498 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002499
2500 // Trigger adapt down, expect reduced framerate.
mflodmancc3d4422017-08-03 08:27:51 -07002501 video_stream_encoder_->TriggerQualityLow();
asapersson09f05612017-05-15 23:40:18 -07002502 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
2503 sink_.WaitForEncodedFrame(3);
2504 VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps);
2505
2506 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002507 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002508 VerifyFpsMaxResolutionMax(new_video_source.sink_wants());
asapersson09f05612017-05-15 23:40:18 -07002509
mflodmancc3d4422017-08-03 08:27:51 -07002510 video_stream_encoder_->Stop();
asapersson09f05612017-05-15 23:40:18 -07002511}
2512
mflodmancc3d4422017-08-03 08:27:51 -07002513TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) {
asaperssond0de2952017-04-21 01:47:31 -07002514 const int kWidth = 1280;
2515 const int kHeight = 720;
2516 const size_t kNumFrames = 10;
2517
Erik Språng4c6ca302019-04-08 15:14:01 +02002518 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002519 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2520 DataRate::bps(kTargetBitrateBps), 0, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002521
asaperssond0de2952017-04-21 01:47:31 -07002522 // Enable adapter, expected input resolutions when downscaling:
asapersson142fcc92017-08-17 08:58:54 -07002523 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame)
asaperssond0de2952017-04-21 01:47:31 -07002524 video_source_.set_adaptation_enabled(true);
2525
2526 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2527 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2528
2529 int downscales = 0;
2530 for (size_t i = 1; i <= kNumFrames; i++) {
Åsa Persson8c1bf952018-09-13 10:42:19 +02002531 video_source_.IncomingCapturedFrame(
2532 CreateFrame(i * kFrameIntervalMs, kWidth, kHeight));
2533 WaitForEncodedFrame(i * kFrameIntervalMs);
asaperssond0de2952017-04-21 01:47:31 -07002534
asaperssonfab67072017-04-04 05:51:49 -07002535 // Trigger scale down.
asaperssond0de2952017-04-21 01:47:31 -07002536 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
mflodmancc3d4422017-08-03 08:27:51 -07002537 video_stream_encoder_->TriggerQualityLow();
sprangc5d62e22017-04-02 23:53:04 -07002538 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
asaperssond0de2952017-04-21 01:47:31 -07002539
2540 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
2541 ++downscales;
2542
2543 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2544 EXPECT_EQ(downscales,
2545 stats_proxy_->GetStats().number_of_quality_adapt_changes);
2546 EXPECT_GT(downscales, 0);
kthelgason5e13d412016-12-01 03:59:51 -08002547 }
mflodmancc3d4422017-08-03 08:27:51 -07002548 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002549}
2550
mflodmancc3d4422017-08-03 08:27:51 -07002551TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002552 AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) {
2553 const int kWidth = 1280;
2554 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002555 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002556 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2557 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002558
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002559 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002560 AdaptingFrameForwarder source;
2561 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002562 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002563 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002564
Åsa Persson8c1bf952018-09-13 10:42:19 +02002565 int64_t timestamp_ms = kFrameIntervalMs;
2566 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002567 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002568 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002569 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2570 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2571
2572 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002573 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002574 timestamp_ms += kFrameIntervalMs;
2575 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2576 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002577 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002578 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2579 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2580
2581 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002582 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002583 timestamp_ms += kFrameIntervalMs;
2584 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002585 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002586 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002587 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2588 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2589
2590 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002591 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002592 timestamp_ms += kFrameIntervalMs;
2593 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2594 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002595 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002596 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2597 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2598
2599 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002600 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002601 timestamp_ms += kFrameIntervalMs;
2602 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asapersson09f05612017-05-15 23:40:18 -07002603 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002604 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002605 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2606 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2607
mflodmancc3d4422017-08-03 08:27:51 -07002608 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07002609}
2610
mflodmancc3d4422017-08-03 08:27:51 -07002611TEST_F(VideoStreamEncoderTest,
asaperssonf7e294d2017-06-13 23:25:22 -07002612 AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) {
2613 const int kWidth = 1280;
2614 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002615 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002616 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2617 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07002618
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002619 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07002620 AdaptingFrameForwarder source;
2621 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002622 video_stream_encoder_->SetSource(&source,
2623 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07002624
Åsa Persson8c1bf952018-09-13 10:42:19 +02002625 int64_t timestamp_ms = kFrameIntervalMs;
2626 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002627 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002628 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002629 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2630 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2631
2632 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002633 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002634 timestamp_ms += kFrameIntervalMs;
2635 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2636 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002637 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2638 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2639 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2640
2641 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002642 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002643 timestamp_ms += kFrameIntervalMs;
2644 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002645 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002646 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002647 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2648 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2649
2650 // Trigger adapt down, expect scaled down resolution.
mflodmancc3d4422017-08-03 08:27:51 -07002651 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002652 timestamp_ms += kFrameIntervalMs;
2653 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2654 sink_.WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07002655 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
2656 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
2657 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2658
2659 // Trigger adapt up, expect no restriction.
mflodmancc3d4422017-08-03 08:27:51 -07002660 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002661 timestamp_ms += kFrameIntervalMs;
2662 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
asaperssonf7e294d2017-06-13 23:25:22 -07002663 sink_.WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002664 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07002665 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2666 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2667
mflodmancc3d4422017-08-03 08:27:51 -07002668 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07002669}
2670
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002671class BalancedDegradationTest : public VideoStreamEncoderTest {
2672 protected:
2673 void SetupTest() {
2674 // Reset encoder for field trials to take effect.
2675 ConfigureEncoder(video_encoder_config_.Copy());
Åsa Perssonccfb3402019-09-25 15:13:04 +02002676 OnBitrateUpdated(kTargetBitrateBps);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002677
2678 // Enable BALANCED preference.
2679 source_.set_adaptation_enabled(true);
Åsa Perssonccfb3402019-09-25 15:13:04 +02002680 video_stream_encoder_->SetSource(&source_, DegradationPreference::BALANCED);
2681 }
2682
2683 void OnBitrateUpdated(int bitrate_bps) {
2684 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(bitrate_bps),
2685 DataRate::bps(bitrate_bps),
2686 DataRate::bps(bitrate_bps), 0, 0);
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002687 }
2688
Åsa Persson45b176f2019-09-30 11:19:05 +02002689 void InsertFrame() {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002690 timestamp_ms_ += kFrameIntervalMs;
2691 source_.IncomingCapturedFrame(CreateFrame(timestamp_ms_, kWidth, kHeight));
Åsa Persson45b176f2019-09-30 11:19:05 +02002692 }
2693
2694 void InsertFrameAndWaitForEncoded() {
2695 InsertFrame();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002696 sink_.WaitForEncodedFrame(timestamp_ms_);
2697 }
2698
2699 const int kWidth = 640; // pixels:640x360=230400
2700 const int kHeight = 360;
2701 const int64_t kFrameIntervalMs = 150; // Use low fps to not drop any frame.
2702 int64_t timestamp_ms_ = 0;
2703 AdaptingFrameForwarder source_;
2704};
2705
2706TEST_F(BalancedDegradationTest, AdaptDownReturnsFalseIfFpsDiffLtThreshold) {
2707 test::ScopedFieldTrials field_trials(
2708 "WebRTC-Video-BalancedDegradationSettings/"
2709 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2710 SetupTest();
2711
2712 // Force input frame rate.
2713 const int kInputFps = 24;
2714 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2715 stats.input_frame_rate = kInputFps;
2716 stats_proxy_->SetMockStats(stats);
2717
Åsa Persson45b176f2019-09-30 11:19:05 +02002718 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002719 VerifyFpsMaxResolutionMax(source_.sink_wants());
2720
2721 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2722 // Fps diff (input-requested:0) < threshold, expect AdaptDown to return false.
2723 video_stream_encoder_->TriggerQualityLowExpectFalse();
2724 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2725
2726 video_stream_encoder_->Stop();
2727}
2728
2729TEST_F(BalancedDegradationTest, AdaptDownReturnsTrueIfFpsDiffGeThreshold) {
2730 test::ScopedFieldTrials field_trials(
2731 "WebRTC-Video-BalancedDegradationSettings/"
2732 "pixels:57600|129600|230400,fps:7|10|24,fps_diff:1|1|1/");
2733 SetupTest();
2734
2735 // Force input frame rate.
2736 const int kInputFps = 25;
2737 VideoSendStream::Stats stats = stats_proxy_->GetStats();
2738 stats.input_frame_rate = kInputFps;
2739 stats_proxy_->SetMockStats(stats);
2740
Åsa Persson45b176f2019-09-30 11:19:05 +02002741 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002742 VerifyFpsMaxResolutionMax(source_.sink_wants());
2743
2744 // Trigger adapt down, expect scaled down framerate (640x360@24fps).
2745 // Fps diff (input-requested:1) == threshold, expect AdaptDown to return true.
2746 video_stream_encoder_->TriggerQualityLow();
2747 VerifyFpsEqResolutionMax(source_.sink_wants(), 24);
2748
2749 video_stream_encoder_->Stop();
2750}
2751
2752TEST_F(BalancedDegradationTest, AdaptDownUsesCodecSpecificFps) {
2753 test::ScopedFieldTrials field_trials(
2754 "WebRTC-Video-BalancedDegradationSettings/"
2755 "pixels:57600|129600|230400,fps:7|10|24,vp8_fps:8|11|22/");
2756 SetupTest();
2757
2758 EXPECT_EQ(kVideoCodecVP8, video_encoder_config_.codec_type);
2759
Åsa Persson45b176f2019-09-30 11:19:05 +02002760 InsertFrameAndWaitForEncoded();
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002761 VerifyFpsMaxResolutionMax(source_.sink_wants());
2762
2763 // Trigger adapt down, expect scaled down framerate (640x360@22fps).
2764 video_stream_encoder_->TriggerQualityLow();
2765 VerifyFpsEqResolutionMax(source_.sink_wants(), 22);
2766
2767 video_stream_encoder_->Stop();
2768}
2769
Åsa Perssonccfb3402019-09-25 15:13:04 +02002770TEST_F(BalancedDegradationTest, NoAdaptUpIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002771 test::ScopedFieldTrials field_trials(
Åsa Persson1b247f12019-08-14 17:26:39 +02002772 "WebRTC-Video-BalancedDegradationSettings/"
2773 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002774 SetupTest();
Åsa Persson1b247f12019-08-14 17:26:39 +02002775
Åsa Persson1b247f12019-08-14 17:26:39 +02002776 const int kMinBitrateBps = 425000;
2777 const int kTooLowMinBitrateBps = 424000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002778 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002779
Åsa Persson45b176f2019-09-30 11:19:05 +02002780 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002781 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002782 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2783
2784 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2785 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002786 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002787 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson1b247f12019-08-14 17:26:39 +02002788 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2789
2790 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2791 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002792 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002793 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002794 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2795
Åsa Persson30ab0152019-08-27 12:22:33 +02002796 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2797 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002798 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002799 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
2800 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 10);
Åsa Persson30ab0152019-08-27 12:22:33 +02002801 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2802
2803 // Trigger adapt up, expect no upscale in fps (target bitrate < min bitrate).
Åsa Persson1b247f12019-08-14 17:26:39 +02002804 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002805 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002806 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
Åsa Persson1b247f12019-08-14 17:26:39 +02002807
Åsa Persson30ab0152019-08-27 12:22:33 +02002808 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002809 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson1b247f12019-08-14 17:26:39 +02002810 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002811 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002812 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002813 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2814
2815 video_stream_encoder_->Stop();
2816}
2817
Åsa Perssonccfb3402019-09-25 15:13:04 +02002818TEST_F(BalancedDegradationTest,
Åsa Persson45b176f2019-09-30 11:19:05 +02002819 InitialFrameDropAdaptsFpsAndResolutionInOneStep) {
2820 test::ScopedFieldTrials field_trials(
2821 "WebRTC-Video-BalancedDegradationSettings/"
2822 "pixels:57600|129600|230400,fps:7|24|24/");
2823 SetupTest();
2824 OnBitrateUpdated(kLowTargetBitrateBps);
2825
2826 VerifyNoLimitation(source_.sink_wants());
2827
2828 // Insert frame, expect scaled down:
2829 // framerate (640x360@24fps) -> resolution (480x270@24fps).
2830 InsertFrame();
2831 EXPECT_FALSE(WaitForFrame(1000));
2832 EXPECT_LT(source_.sink_wants().max_pixel_count, kWidth * kHeight);
2833 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2834
2835 // Insert frame, expect scaled down:
2836 // resolution (320x180@24fps).
2837 InsertFrame();
2838 EXPECT_FALSE(WaitForFrame(1000));
2839 EXPECT_LT(source_.sink_wants().max_pixel_count,
2840 source_.last_wants().max_pixel_count);
2841 EXPECT_EQ(source_.sink_wants().max_framerate_fps, 24);
2842
2843 // Frame should not be dropped (min pixels per frame reached).
2844 InsertFrameAndWaitForEncoded();
2845
2846 video_stream_encoder_->Stop();
2847}
2848
2849TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002850 NoAdaptUpInResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002851 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002852 "WebRTC-Video-BalancedDegradationSettings/"
2853 "pixels:57600|129600|230400,fps:7|10|14,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002854 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002855
Åsa Persson30ab0152019-08-27 12:22:33 +02002856 const int kResolutionMinBitrateBps = 435000;
2857 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002858 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002859
Åsa Persson45b176f2019-09-30 11:19:05 +02002860 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002861 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002862 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2863
2864 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2865 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002866 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002867 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002868 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2869
2870 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2871 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002872 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002873 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002874 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2875
2876 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2877 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002878 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002879 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson1b247f12019-08-14 17:26:39 +02002880 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2881
Åsa Persson30ab0152019-08-27 12:22:33 +02002882 // Trigger adapt up, expect upscaled fps (no bitrate limit) (480x270@14fps).
2883 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002884 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002885 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002886 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2887
2888 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
2889 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002890 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002891 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2892
2893 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002894 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002895 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002896 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002897 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002898 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2899
2900 video_stream_encoder_->Stop();
2901}
2902
Åsa Perssonccfb3402019-09-25 15:13:04 +02002903TEST_F(BalancedDegradationTest,
Åsa Persson30ab0152019-08-27 12:22:33 +02002904 NoAdaptUpInFpsAndResolutionIfBwEstimateIsLessThanMinBitrate) {
Åsa Perssonb67c44c2019-09-24 15:25:32 +02002905 test::ScopedFieldTrials field_trials(
Åsa Persson30ab0152019-08-27 12:22:33 +02002906 "WebRTC-Video-BalancedDegradationSettings/"
2907 "pixels:57600|129600|230400,fps:7|10|14,kbps:0|0|425,kbps_res:0|0|435/");
Åsa Perssonccfb3402019-09-25 15:13:04 +02002908 SetupTest();
Åsa Persson30ab0152019-08-27 12:22:33 +02002909
Åsa Persson30ab0152019-08-27 12:22:33 +02002910 const int kMinBitrateBps = 425000;
2911 const int kTooLowMinBitrateBps = 424000;
2912 const int kResolutionMinBitrateBps = 435000;
2913 const int kTooLowMinResolutionBitrateBps = 434000;
Åsa Perssonccfb3402019-09-25 15:13:04 +02002914 OnBitrateUpdated(kTooLowMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002915
Åsa Persson45b176f2019-09-30 11:19:05 +02002916 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002917 VerifyFpsMaxResolutionMax(source_.sink_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002918 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2919
2920 // Trigger adapt down, expect scaled down framerate (640x360@14fps).
2921 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002922 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002923 VerifyFpsEqResolutionMax(source_.sink_wants(), 14);
Åsa Persson30ab0152019-08-27 12:22:33 +02002924 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2925
2926 // Trigger adapt down, expect scaled down resolution (480x270@14fps).
2927 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002928 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002929 VerifyFpsEqResolutionLt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002930 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2931
2932 // Trigger adapt down, expect scaled down framerate (480x270@10fps).
2933 video_stream_encoder_->TriggerQualityLow();
Åsa Persson45b176f2019-09-30 11:19:05 +02002934 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002935 VerifyFpsLtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002936 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2937
2938 // Trigger adapt up, expect no upscale (target bitrate < min bitrate).
2939 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002940 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002941 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2942
2943 // Trigger adapt up, expect upscaled fps (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002944 OnBitrateUpdated(kMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002945 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002946 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002947 VerifyFpsGtResolutionEq(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002948 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2949
2950 // Trigger adapt up, expect no upscale in res (target bitrate < min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002951 OnBitrateUpdated(kTooLowMinResolutionBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002952 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002953 InsertFrameAndWaitForEncoded();
Åsa Persson30ab0152019-08-27 12:22:33 +02002954 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2955
2956 // Trigger adapt up, expect upscaled res (target bitrate == min bitrate).
Åsa Perssonccfb3402019-09-25 15:13:04 +02002957 OnBitrateUpdated(kResolutionMinBitrateBps);
Åsa Persson30ab0152019-08-27 12:22:33 +02002958 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson45b176f2019-09-30 11:19:05 +02002959 InsertFrameAndWaitForEncoded();
Åsa Perssonccfb3402019-09-25 15:13:04 +02002960 VerifyFpsEqResolutionGt(source_.sink_wants(), source_.last_wants());
Åsa Persson30ab0152019-08-27 12:22:33 +02002961 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2962
Åsa Persson1b247f12019-08-14 17:26:39 +02002963 video_stream_encoder_->Stop();
2964}
2965
mflodmancc3d4422017-08-03 08:27:51 -07002966TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07002967 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
2968 const int kWidth = 1280;
2969 const int kHeight = 720;
Erik Språng4c6ca302019-04-08 15:14:01 +02002970 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02002971 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
2972 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07002973
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002974 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07002975 AdaptingFrameForwarder source;
2976 source.set_adaptation_enabled(true);
mflodmancc3d4422017-08-03 08:27:51 -07002977 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002978 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07002979
Åsa Persson8c1bf952018-09-13 10:42:19 +02002980 int64_t timestamp_ms = kFrameIntervalMs;
2981 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07002982 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02002983 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07002984 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
2985 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2986 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2987 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2988
2989 // Trigger cpu adapt down, expect scaled down resolution (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07002990 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02002991 timestamp_ms += kFrameIntervalMs;
2992 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
2993 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07002994 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
asaperssond0de2952017-04-21 01:47:31 -07002995 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
2996 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
2997 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
2998 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
2999
3000 // Trigger cpu adapt down, expect scaled down resolution (640x360).
mflodmancc3d4422017-08-03 08:27:51 -07003001 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003002 timestamp_ms += kFrameIntervalMs;
3003 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3004 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003005 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003006 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3007 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3008 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3009 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3010
Jonathan Yubc771b72017-12-08 17:04:29 -08003011 // Trigger cpu adapt down, expect scaled down resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003012 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003013 timestamp_ms += kFrameIntervalMs;
3014 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3015 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003016 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003017 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3018 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003019 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003020 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3021
Jonathan Yubc771b72017-12-08 17:04:29 -08003022 // Trigger quality adapt down, expect scaled down resolution (320x180).
mflodmancc3d4422017-08-03 08:27:51 -07003023 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003024 timestamp_ms += kFrameIntervalMs;
3025 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3026 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003027 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003028 rtc::VideoSinkWants last_wants = source.sink_wants();
asaperssond0de2952017-04-21 01:47:31 -07003029 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3030 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3031 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3032 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3033
Jonathan Yubc771b72017-12-08 17:04:29 -08003034 // Trigger quality adapt down, expect no change (min resolution reached).
3035 video_stream_encoder_->TriggerQualityLow();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003036 timestamp_ms += kFrameIntervalMs;
3037 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3038 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003039 VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants);
3040 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3041 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3042 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3043 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3044
3045 // Trigger cpu adapt up, expect upscaled resolution (480x270).
mflodmancc3d4422017-08-03 08:27:51 -07003046 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003047 timestamp_ms += kFrameIntervalMs;
3048 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3049 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003050 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Jonathan Yubc771b72017-12-08 17:04:29 -08003051 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3052 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3053 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3054 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3055
3056 // Trigger cpu adapt up, expect upscaled resolution (640x360).
3057 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003058 timestamp_ms += kFrameIntervalMs;
3059 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3060 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003061 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
3062 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
3063 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3064 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3065 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3066
3067 // Trigger cpu adapt up, expect upscaled resolution (960x540).
3068 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003069 timestamp_ms += kFrameIntervalMs;
3070 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3071 WaitForEncodedFrame(timestamp_ms);
Jonathan Yubc771b72017-12-08 17:04:29 -08003072 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
asaperssond0de2952017-04-21 01:47:31 -07003073 last_wants = source.sink_wants();
3074 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3075 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003076 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003077 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3078
3079 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
mflodmancc3d4422017-08-03 08:27:51 -07003080 video_stream_encoder_->TriggerCpuNormalUsage();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003081 timestamp_ms += kFrameIntervalMs;
3082 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
3083 WaitForEncodedFrame(timestamp_ms);
asapersson09f05612017-05-15 23:40:18 -07003084 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
asaperssond0de2952017-04-21 01:47:31 -07003085 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3086 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003087 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003088 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3089
3090 // Trigger quality adapt up, expect no restriction (1280x720).
mflodmancc3d4422017-08-03 08:27:51 -07003091 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003092 timestamp_ms += kFrameIntervalMs;
3093 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003094 WaitForEncodedFrame(kWidth, kHeight);
asapersson09f05612017-05-15 23:40:18 -07003095 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02003096 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003097 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3098 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
Jonathan Yubc771b72017-12-08 17:04:29 -08003099 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
asaperssond0de2952017-04-21 01:47:31 -07003100 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
kthelgason5e13d412016-12-01 03:59:51 -08003101
mflodmancc3d4422017-08-03 08:27:51 -07003102 video_stream_encoder_->Stop();
kthelgason5e13d412016-12-01 03:59:51 -08003103}
3104
mflodmancc3d4422017-08-03 08:27:51 -07003105TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) {
asaperssonfab67072017-04-04 05:51:49 -07003106 const int kWidth = 640;
3107 const int kHeight = 360;
perkj803d97f2016-11-01 11:45:46 -07003108
Erik Språng4c6ca302019-04-08 15:14:01 +02003109 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003110 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3111 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07003112
perkj803d97f2016-11-01 11:45:46 -07003113 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003114 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003115 WaitForEncodedFrame(i);
perkj803d97f2016-11-01 11:45:46 -07003116 }
3117
mflodmancc3d4422017-08-03 08:27:51 -07003118 video_stream_encoder_->TriggerCpuOveruse();
perkj803d97f2016-11-01 11:45:46 -07003119 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003120 video_source_.IncomingCapturedFrame(CreateFrame(
3121 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003122 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
perkj803d97f2016-11-01 11:45:46 -07003123 }
3124
mflodmancc3d4422017-08-03 08:27:51 -07003125 video_stream_encoder_->Stop();
3126 video_stream_encoder_.reset();
perkj803d97f2016-11-01 11:45:46 -07003127 stats_proxy_.reset();
sprangf8ee65e2017-02-28 08:49:33 -08003128
perkj803d97f2016-11-01 11:45:46 -07003129 EXPECT_EQ(1,
3130 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3131 EXPECT_EQ(
3132 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
3133}
3134
mflodmancc3d4422017-08-03 08:27:51 -07003135TEST_F(VideoStreamEncoderTest,
3136 CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003137 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003138 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3139 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf4e44af2017-04-19 02:01:06 -07003140 const int kWidth = 640;
3141 const int kHeight = 360;
3142
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003143 video_stream_encoder_->SetSource(&video_source_,
3144 webrtc::DegradationPreference::DISABLED);
asaperssonf4e44af2017-04-19 02:01:06 -07003145
3146 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
3147 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003148 WaitForEncodedFrame(i);
asaperssonf4e44af2017-04-19 02:01:06 -07003149 }
3150
mflodmancc3d4422017-08-03 08:27:51 -07003151 video_stream_encoder_->Stop();
3152 video_stream_encoder_.reset();
asaperssonf4e44af2017-04-19 02:01:06 -07003153 stats_proxy_.reset();
3154
3155 EXPECT_EQ(0,
3156 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
3157}
3158
mflodmancc3d4422017-08-03 08:27:51 -07003159TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) {
sprang4847ae62017-06-27 07:06:52 -07003160 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02003161 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
sprang57c2fff2017-01-16 06:24:02 -08003162
3163 const int kDefaultFps = 30;
Erik Språng566124a2018-04-23 12:32:22 +02003164 const VideoBitrateAllocation expected_bitrate =
sprang57c2fff2017-01-16 06:24:02 -08003165 DefaultVideoBitrateAllocator(fake_encoder_.codec_config())
Florent Castelli8bbdb5b2019-08-02 15:16:28 +02003166 .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrateBps,
3167 kDefaultFps));
sprang57c2fff2017-01-16 06:24:02 -08003168
sprang57c2fff2017-01-16 06:24:02 -08003169 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
Niels Möller6bb5ab92019-01-11 11:11:10 +01003170 .Times(1);
Florent Castellia8336d32019-09-09 13:36:55 +02003171 video_stream_encoder_->OnBitrateUpdated(
3172 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3173 DataRate::bps(kLowTargetBitrateBps), 0, 0);
sprang57c2fff2017-01-16 06:24:02 -08003174
sprang57c2fff2017-01-16 06:24:02 -08003175 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003176 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3177 WaitForEncodedFrame(rtc::TimeMillis());
Erik Språng5056af02019-09-02 15:53:11 +02003178 VideoBitrateAllocation bitrate_allocation =
3179 fake_encoder_.GetAndResetLastRateControlSettings()->bitrate;
Erik Språngd7329ca2019-02-21 21:19:53 +01003180 // Check that encoder has been updated too, not just allocation observer.
Erik Språng5056af02019-09-02 15:53:11 +02003181 EXPECT_EQ(bitrate_allocation.get_sum_bps(), kLowTargetBitrateBps);
Sebastian Jansson40889f32019-04-17 12:11:20 +02003182 // TODO(srte): The use of millisecs here looks like an error, but the tests
3183 // fails using seconds, this should be investigated.
3184 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003185
3186 // Not called on second frame.
3187 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3188 .Times(0);
3189 video_source_.IncomingCapturedFrame(
Erik Språngd7329ca2019-02-21 21:19:53 +01003190 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3191 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003192 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
sprang57c2fff2017-01-16 06:24:02 -08003193
3194 // Called after a process interval.
sprang57c2fff2017-01-16 06:24:02 -08003195 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
3196 .Times(1);
Erik Språngd7329ca2019-02-21 21:19:53 +01003197 const int64_t start_time_ms = rtc::TimeMillis();
3198 while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) {
3199 video_source_.IncomingCapturedFrame(
3200 CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_));
3201 WaitForEncodedFrame(rtc::TimeMillis());
Sebastian Jansson40889f32019-04-17 12:11:20 +02003202 fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps);
Erik Språngd7329ca2019-02-21 21:19:53 +01003203 }
3204
3205 // Since rates are unchanged, encoder should not be reconfigured.
Erik Språng5056af02019-09-02 15:53:11 +02003206 EXPECT_FALSE(fake_encoder_.GetAndResetLastRateControlSettings().has_value());
sprang57c2fff2017-01-16 06:24:02 -08003207
mflodmancc3d4422017-08-03 08:27:51 -07003208 video_stream_encoder_->Stop();
sprang57c2fff2017-01-16 06:24:02 -08003209}
3210
Åsa Perssonc29cb2c2019-03-25 12:06:59 +01003211TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) {
3212 // 2 TLs configured, temporal layers supported by encoder.
3213 const int kNumTemporalLayers = 2;
3214 ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false);
3215 fake_encoder_.SetTemporalLayersSupported(0, true);
3216
3217 // Bitrate allocated across temporal layers.
3218 const int kTl0Bps = kTargetBitrateBps *
3219 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3220 kNumTemporalLayers, /*temporal_id*/ 0);
3221 const int kTl1Bps = kTargetBitrateBps *
3222 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3223 kNumTemporalLayers, /*temporal_id*/ 1);
3224 VideoBitrateAllocation expected_bitrate;
3225 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps);
3226 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps);
3227
3228 VerifyAllocatedBitrate(expected_bitrate);
3229 video_stream_encoder_->Stop();
3230}
3231
3232TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) {
3233 // 2 TLs configured, temporal layers not supported by encoder.
3234 ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3235 fake_encoder_.SetTemporalLayersSupported(0, false);
3236
3237 // Temporal layers not supported by the encoder.
3238 // Total bitrate should be at ti:0.
3239 VideoBitrateAllocation expected_bitrate;
3240 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps);
3241
3242 VerifyAllocatedBitrate(expected_bitrate);
3243 video_stream_encoder_->Stop();
3244}
3245
3246TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) {
3247 // 2 TLs configured, temporal layers only supported for first stream.
3248 ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false);
3249 fake_encoder_.SetTemporalLayersSupported(0, true);
3250 fake_encoder_.SetTemporalLayersSupported(1, false);
3251
3252 const int kS0Bps = 150000;
3253 const int kS0Tl0Bps =
3254 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3255 /*num_layers*/ 2, /*temporal_id*/ 0);
3256 const int kS0Tl1Bps =
3257 kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
3258 /*num_layers*/ 2, /*temporal_id*/ 1);
3259 const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps;
3260 // Temporal layers not supported by si:1.
3261 VideoBitrateAllocation expected_bitrate;
3262 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps);
3263 expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps);
3264 expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps);
3265
3266 VerifyAllocatedBitrate(expected_bitrate);
3267 video_stream_encoder_->Stop();
3268}
3269
Niels Möller7dc26b72017-12-06 10:27:48 +01003270TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) {
3271 const int kFrameWidth = 1280;
3272 const int kFrameHeight = 720;
3273 const int kFramerate = 24;
3274
Erik Språng4c6ca302019-04-08 15:14:01 +02003275 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003276 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3277 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller7dc26b72017-12-06 10:27:48 +01003278 test::FrameForwarder source;
3279 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003280 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003281
3282 // Insert a single frame, triggering initial configuration.
3283 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3284 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3285
3286 EXPECT_EQ(
3287 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3288 kDefaultFramerate);
3289
3290 // Trigger reconfigure encoder (without resetting the entire instance).
3291 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003292 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003293 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3294 video_encoder_config.number_of_streams = 1;
3295 video_encoder_config.video_stream_factory =
3296 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3297 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003298 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003299 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3300
3301 // Detector should be updated with fps limit from codec config.
3302 EXPECT_EQ(
3303 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3304 kFramerate);
3305
3306 // Trigger overuse, max framerate should be reduced.
3307 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3308 stats.input_frame_rate = kFramerate;
3309 stats_proxy_->SetMockStats(stats);
3310 video_stream_encoder_->TriggerCpuOveruse();
3311 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3312 int adapted_framerate =
3313 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3314 EXPECT_LT(adapted_framerate, kFramerate);
3315
3316 // Trigger underuse, max framerate should go back to codec configured fps.
3317 // Set extra low fps, to make sure it's actually reset, not just incremented.
3318 stats = stats_proxy_->GetStats();
3319 stats.input_frame_rate = adapted_framerate / 2;
3320 stats_proxy_->SetMockStats(stats);
3321 video_stream_encoder_->TriggerCpuNormalUsage();
3322 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3323 EXPECT_EQ(
3324 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3325 kFramerate);
3326
3327 video_stream_encoder_->Stop();
3328}
3329
3330TEST_F(VideoStreamEncoderTest,
3331 OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) {
3332 const int kFrameWidth = 1280;
3333 const int kFrameHeight = 720;
3334 const int kLowFramerate = 15;
3335 const int kHighFramerate = 25;
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);
Niels Möller7dc26b72017-12-06 10:27:48 +01003340 test::FrameForwarder source;
3341 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003342 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
Niels Möller7dc26b72017-12-06 10:27:48 +01003343
3344 // Trigger initial configuration.
3345 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003346 video_encoder_config.codec_type = kVideoCodecVP8;
Niels Möller7dc26b72017-12-06 10:27:48 +01003347 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, kLowFramerate);
3351 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3352 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003353 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003354 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3355
3356 EXPECT_EQ(
3357 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3358 kLowFramerate);
3359
3360 // Trigger overuse, max framerate should be reduced.
3361 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3362 stats.input_frame_rate = kLowFramerate;
3363 stats_proxy_->SetMockStats(stats);
3364 video_stream_encoder_->TriggerCpuOveruse();
3365 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3366 int adapted_framerate =
3367 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
3368 EXPECT_LT(adapted_framerate, kLowFramerate);
3369
3370 // Reconfigure the encoder with a new (higher max framerate), max fps should
3371 // still respect the adaptation.
3372 video_encoder_config.video_stream_factory =
3373 new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate);
3374 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
3375 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003376 kMaxPayloadLength);
Niels Möller7dc26b72017-12-06 10:27:48 +01003377 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3378
3379 EXPECT_EQ(
3380 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3381 adapted_framerate);
3382
3383 // Trigger underuse, max framerate should go back to codec configured fps.
3384 stats = stats_proxy_->GetStats();
3385 stats.input_frame_rate = adapted_framerate;
3386 stats_proxy_->SetMockStats(stats);
3387 video_stream_encoder_->TriggerCpuNormalUsage();
3388 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
3389 EXPECT_EQ(
3390 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3391 kHighFramerate);
3392
3393 video_stream_encoder_->Stop();
3394}
3395
mflodmancc3d4422017-08-03 08:27:51 -07003396TEST_F(VideoStreamEncoderTest,
3397 OveruseDetectorUpdatedOnDegradationPreferenceChange) {
sprangfda496a2017-06-15 04:21:07 -07003398 const int kFrameWidth = 1280;
3399 const int kFrameHeight = 720;
3400 const int kFramerate = 24;
3401
Erik Språng4c6ca302019-04-08 15:14:01 +02003402 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003403 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3404 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangfda496a2017-06-15 04:21:07 -07003405 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003406 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003407 &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangfda496a2017-06-15 04:21:07 -07003408
3409 // Trigger initial configuration.
3410 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02003411 video_encoder_config.codec_type = kVideoCodecVP8;
sprangfda496a2017-06-15 04:21:07 -07003412 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
3413 video_encoder_config.number_of_streams = 1;
3414 video_encoder_config.video_stream_factory =
3415 new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate);
3416 source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight));
mflodmancc3d4422017-08-03 08:27:51 -07003417 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003418 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07003419 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprangfda496a2017-06-15 04:21:07 -07003420
Niels Möller7dc26b72017-12-06 10:27:48 +01003421 EXPECT_EQ(
3422 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3423 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003424
3425 // Trigger overuse, max framerate should be reduced.
3426 VideoSendStream::Stats stats = stats_proxy_->GetStats();
3427 stats.input_frame_rate = kFramerate;
3428 stats_proxy_->SetMockStats(stats);
mflodmancc3d4422017-08-03 08:27:51 -07003429 video_stream_encoder_->TriggerCpuOveruse();
3430 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003431 int adapted_framerate =
3432 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate();
sprangfda496a2017-06-15 04:21:07 -07003433 EXPECT_LT(adapted_framerate, kFramerate);
3434
3435 // Change degradation preference to not enable framerate scaling. Target
3436 // framerate should be changed to codec defined limit.
mflodmancc3d4422017-08-03 08:27:51 -07003437 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003438 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
mflodmancc3d4422017-08-03 08:27:51 -07003439 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
Niels Möller7dc26b72017-12-06 10:27:48 +01003440 EXPECT_EQ(
3441 video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(),
3442 kFramerate);
sprangfda496a2017-06-15 04:21:07 -07003443
mflodmancc3d4422017-08-03 08:27:51 -07003444 video_stream_encoder_->Stop();
sprangfda496a2017-06-15 04:21:07 -07003445}
3446
mflodmancc3d4422017-08-03 08:27:51 -07003447TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003448 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003449 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003450 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003451 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003452 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003453 const int kWidth = 640;
3454 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003455
asaperssonfab67072017-04-04 05:51:49 -07003456 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003457
3458 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003459 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003460
3461 // Expect the sink_wants to specify a scaled frame.
asapersson0944a802017-04-07 00:57:58 -07003462 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003463
sprangc5d62e22017-04-02 23:53:04 -07003464 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
kthelgason2bc68642017-02-07 07:02:22 -08003465
asaperssonfab67072017-04-04 05:51:49 -07003466 // Next frame is scaled.
kthelgason2bc68642017-02-07 07:02:22 -08003467 video_source_.IncomingCapturedFrame(
asaperssonfab67072017-04-04 05:51:49 -07003468 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
kthelgason2bc68642017-02-07 07:02:22 -08003469
3470 // Expect to drop this frame, the wait should time out.
sprang4847ae62017-06-27 07:06:52 -07003471 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003472
sprangc5d62e22017-04-02 23:53:04 -07003473 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
kthelgason2bc68642017-02-07 07:02:22 -08003474
mflodmancc3d4422017-08-03 08:27:51 -07003475 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003476}
3477
mflodmancc3d4422017-08-03 08:27:51 -07003478TEST_F(VideoStreamEncoderTest,
3479 NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) {
asaperssonfab67072017-04-04 05:51:49 -07003480 const int kTooLowBitrateForFrameSizeBps = 10000;
Erik Språng610c7632019-03-06 15:37:33 +01003481 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003482 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003483 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003484 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
asaperssonfab67072017-04-04 05:51:49 -07003485 const int kWidth = 640;
3486 const int kHeight = 360;
kthelgason2bc68642017-02-07 07:02:22 -08003487
3488 // We expect the n initial frames to get dropped.
3489 int i;
3490 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
asaperssonfab67072017-04-04 05:51:49 -07003491 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003492 ExpectDroppedFrame();
kthelgason2bc68642017-02-07 07:02:22 -08003493 }
3494 // The n+1th frame should not be dropped, even though it's size is too large.
asaperssonfab67072017-04-04 05:51:49 -07003495 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003496 WaitForEncodedFrame(i);
kthelgason2bc68642017-02-07 07:02:22 -08003497
3498 // Expect the sink_wants to specify a scaled frame.
asaperssonfab67072017-04-04 05:51:49 -07003499 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
kthelgason2bc68642017-02-07 07:02:22 -08003500
mflodmancc3d4422017-08-03 08:27:51 -07003501 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003502}
3503
mflodmancc3d4422017-08-03 08:27:51 -07003504TEST_F(VideoStreamEncoderTest,
3505 InitialFrameDropOffWithMaintainResolutionPreference) {
asaperssonfab67072017-04-04 05:51:49 -07003506 const int kWidth = 640;
3507 const int kHeight = 360;
Florent Castellia8336d32019-09-09 13:36:55 +02003508 video_stream_encoder_->OnBitrateUpdated(
3509 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3510 DataRate::bps(kLowTargetBitrateBps), 0, 0);
kthelgason2bc68642017-02-07 07:02:22 -08003511
3512 // Set degradation preference.
mflodmancc3d4422017-08-03 08:27:51 -07003513 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003514 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
kthelgason2bc68642017-02-07 07:02:22 -08003515
asaperssonfab67072017-04-04 05:51:49 -07003516 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgason2bc68642017-02-07 07:02:22 -08003517 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003518 WaitForEncodedFrame(1);
kthelgason2bc68642017-02-07 07:02:22 -08003519
mflodmancc3d4422017-08-03 08:27:51 -07003520 video_stream_encoder_->Stop();
kthelgason2bc68642017-02-07 07:02:22 -08003521}
3522
mflodmancc3d4422017-08-03 08:27:51 -07003523TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
asaperssonfab67072017-04-04 05:51:49 -07003524 const int kWidth = 640;
3525 const int kHeight = 360;
kthelgasonad9010c2017-02-14 00:46:51 -08003526 fake_encoder_.SetQualityScaling(false);
Niels Möller4db138e2018-04-19 09:04:13 +02003527
3528 VideoEncoderConfig video_encoder_config;
3529 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
3530 // Make format different, to force recreation of encoder.
3531 video_encoder_config.video_format.parameters["foo"] = "foo";
3532 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02003533 kMaxPayloadLength);
Florent Castellia8336d32019-09-09 13:36:55 +02003534 video_stream_encoder_->OnBitrateUpdated(
3535 DataRate::bps(kLowTargetBitrateBps), DataRate::bps(kLowTargetBitrateBps),
3536 DataRate::bps(kLowTargetBitrateBps), 0, 0);
asapersson09f05612017-05-15 23:40:18 -07003537
kthelgasonb83797b2017-02-14 11:57:25 -08003538 // Force quality scaler reconfiguration by resetting the source.
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003539 video_stream_encoder_->SetSource(&video_source_,
3540 webrtc::DegradationPreference::BALANCED);
kthelgasonad9010c2017-02-14 00:46:51 -08003541
asaperssonfab67072017-04-04 05:51:49 -07003542 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
kthelgasonad9010c2017-02-14 00:46:51 -08003543 // Frame should not be dropped, even if it's too large.
sprang4847ae62017-06-27 07:06:52 -07003544 WaitForEncodedFrame(1);
kthelgasonad9010c2017-02-14 00:46:51 -08003545
mflodmancc3d4422017-08-03 08:27:51 -07003546 video_stream_encoder_->Stop();
kthelgasonad9010c2017-02-14 00:46:51 -08003547 fake_encoder_.SetQualityScaling(true);
3548}
3549
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003550TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) {
3551 webrtc::test::ScopedFieldTrials field_trials(
3552 "WebRTC-InitialFramedrop/Enabled/");
3553 // Reset encoder for field trials to take effect.
3554 ConfigureEncoder(video_encoder_config_.Copy());
3555 const int kTooLowBitrateForFrameSizeBps = 10000;
3556 const int kWidth = 640;
3557 const int kHeight = 360;
3558
Erik Språng4c6ca302019-04-08 15:14:01 +02003559 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003560 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3561 DataRate::bps(kTargetBitrateBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003562 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3563 // Frame should not be dropped.
3564 WaitForEncodedFrame(1);
3565
Erik Språng610c7632019-03-06 15:37:33 +01003566 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02003567 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003568 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02003569 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
Kári Tristan Helgason639602a2018-08-02 10:51:40 +02003570 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3571 // Expect to drop this frame, the wait should time out.
3572 ExpectDroppedFrame();
3573
3574 // Expect the sink_wants to specify a scaled frame.
3575 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3576 video_stream_encoder_->Stop();
3577}
3578
Åsa Persson139f4dc2019-08-02 09:29:58 +02003579TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBweDrops) {
3580 webrtc::test::ScopedFieldTrials field_trials(
3581 "WebRTC-Video-QualityScalerSettings/"
3582 "initial_bitrate_interval_ms:1000,initial_bitrate_factor:0.2/");
3583 // Reset encoder for field trials to take effect.
3584 ConfigureEncoder(video_encoder_config_.Copy());
3585 const int kNotTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.2;
3586 const int kTooLowBitrateForFrameSizeBps = kTargetBitrateBps * 0.19;
3587 const int kWidth = 640;
3588 const int kHeight = 360;
3589
3590 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003591 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3592 DataRate::bps(kTargetBitrateBps), 0, 0);
Åsa Persson139f4dc2019-08-02 09:29:58 +02003593 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
3594 // Frame should not be dropped.
3595 WaitForEncodedFrame(1);
3596
3597 video_stream_encoder_->OnBitrateUpdated(
3598 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003599 DataRate::bps(kNotTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003600 DataRate::bps(kNotTooLowBitrateForFrameSizeBps), 0, 0);
3601 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
3602 // Frame should not be dropped.
3603 WaitForEncodedFrame(2);
3604
3605 video_stream_encoder_->OnBitrateUpdated(
3606 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Florent Castellia8336d32019-09-09 13:36:55 +02003607 DataRate::bps(kTooLowBitrateForFrameSizeBps),
Åsa Persson139f4dc2019-08-02 09:29:58 +02003608 DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0);
3609 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
3610 // Expect to drop this frame, the wait should time out.
3611 ExpectDroppedFrame();
3612
3613 // Expect the sink_wants to specify a scaled frame.
3614 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
3615 video_stream_encoder_->Stop();
3616}
3617
mflodmancc3d4422017-08-03 08:27:51 -07003618TEST_F(VideoStreamEncoderTest,
asaperssond0de2952017-04-21 01:47:31 -07003619 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
3620 const int kTooSmallWidth = 10;
3621 const int kTooSmallHeight = 10;
Erik Språng4c6ca302019-04-08 15:14:01 +02003622 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003623 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3624 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssond0de2952017-04-21 01:47:31 -07003625
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003626 // Enable MAINTAIN_FRAMERATE preference, no initial limitation.
asaperssond0de2952017-04-21 01:47:31 -07003627 test::FrameForwarder source;
mflodmancc3d4422017-08-03 08:27:51 -07003628 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003629 &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE);
asaperssond0de2952017-04-21 01:47:31 -07003630 VerifyNoLimitation(source.sink_wants());
3631 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3632
3633 // Trigger adapt down, too small frame, expect no change.
3634 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003635 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003636 video_stream_encoder_->TriggerCpuOveruse();
Åsa Persson8c1bf952018-09-13 10:42:19 +02003637 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssond0de2952017-04-21 01:47:31 -07003638 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
3639 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
3640
mflodmancc3d4422017-08-03 08:27:51 -07003641 video_stream_encoder_->Stop();
asaperssond0de2952017-04-21 01:47:31 -07003642}
3643
mflodmancc3d4422017-08-03 08:27:51 -07003644TEST_F(VideoStreamEncoderTest,
3645 ResolutionNotAdaptedForTooSmallFrame_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003646 const int kTooSmallWidth = 10;
3647 const int kTooSmallHeight = 10;
3648 const int kFpsLimit = 7;
Erik Språng4c6ca302019-04-08 15:14:01 +02003649 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003650 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3651 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003652
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003653 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003654 test::FrameForwarder source;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003655 video_stream_encoder_->SetSource(&source,
3656 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003657 VerifyNoLimitation(source.sink_wants());
3658 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3659 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3660
3661 // Trigger adapt down, expect limited framerate.
3662 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003663 WaitForEncodedFrame(1);
mflodmancc3d4422017-08-03 08:27:51 -07003664 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003665 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3666 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3667 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3668 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3669
3670 // Trigger adapt down, too small frame, expect no change.
3671 source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight));
sprang4847ae62017-06-27 07:06:52 -07003672 WaitForEncodedFrame(2);
mflodmancc3d4422017-08-03 08:27:51 -07003673 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003674 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
3675 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3676 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3677 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3678
mflodmancc3d4422017-08-03 08:27:51 -07003679 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07003680}
3681
mflodmancc3d4422017-08-03 08:27:51 -07003682TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) {
asapersson02465b82017-04-10 01:12:52 -07003683 fake_encoder_.ForceInitEncodeFailure(true);
Erik Språng4c6ca302019-04-08 15:14:01 +02003684 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003685 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3686 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möllerf1338562018-04-26 09:51:47 +02003687 ResetEncoder("VP8", 2, 1, 1, false);
asapersson02465b82017-04-10 01:12:52 -07003688 const int kFrameWidth = 1280;
3689 const int kFrameHeight = 720;
3690 video_source_.IncomingCapturedFrame(
3691 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003692 ExpectDroppedFrame();
mflodmancc3d4422017-08-03 08:27:51 -07003693 video_stream_encoder_->Stop();
asapersson02465b82017-04-10 01:12:52 -07003694}
3695
sprangb1ca0732017-02-01 08:38:12 -08003696// TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
mflodmancc3d4422017-08-03 08:27:51 -07003697TEST_F(VideoStreamEncoderTest,
3698 AdaptsResolutionOnOveruse_MaintainFramerateMode) {
Erik Språng4c6ca302019-04-08 15:14:01 +02003699 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003700 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3701 DataRate::bps(kTargetBitrateBps), 0, 0);
sprangb1ca0732017-02-01 08:38:12 -08003702
3703 const int kFrameWidth = 1280;
3704 const int kFrameHeight = 720;
3705 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
mflodmancc3d4422017-08-03 08:27:51 -07003706 // requested by
3707 // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan().
sprangb1ca0732017-02-01 08:38:12 -08003708 video_source_.set_adaptation_enabled(true);
3709
3710 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003711 CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003712 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003713
3714 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07003715 video_stream_encoder_->TriggerCpuOveruse();
sprangb1ca0732017-02-01 08:38:12 -08003716 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003717 CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003718 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
sprangb1ca0732017-02-01 08:38:12 -08003719
asaperssonfab67072017-04-04 05:51:49 -07003720 // Trigger CPU normal use, return to original resolution.
mflodmancc3d4422017-08-03 08:27:51 -07003721 video_stream_encoder_->TriggerCpuNormalUsage();
sprangb1ca0732017-02-01 08:38:12 -08003722 video_source_.IncomingCapturedFrame(
Åsa Persson8c1bf952018-09-13 10:42:19 +02003723 CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003724 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
sprangb1ca0732017-02-01 08:38:12 -08003725
mflodmancc3d4422017-08-03 08:27:51 -07003726 video_stream_encoder_->Stop();
sprangb1ca0732017-02-01 08:38:12 -08003727}
sprangfe627f32017-03-29 08:24:59 -07003728
mflodmancc3d4422017-08-03 08:27:51 -07003729TEST_F(VideoStreamEncoderTest,
3730 AdaptsFramerateOnOveruse_MaintainResolutionMode) {
sprangc5d62e22017-04-02 23:53:04 -07003731 const int kFrameWidth = 1280;
3732 const int kFrameHeight = 720;
sprangc5d62e22017-04-02 23:53:04 -07003733
Erik Språng4c6ca302019-04-08 15:14:01 +02003734 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003735 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3736 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003737 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003738 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003739 video_source_.set_adaptation_enabled(true);
3740
sprang4847ae62017-06-27 07:06:52 -07003741 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003742
3743 video_source_.IncomingCapturedFrame(
3744 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003745 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003746
3747 // Try to trigger overuse. No fps estimate available => no effect.
mflodmancc3d4422017-08-03 08:27:51 -07003748 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003749
3750 // Insert frames for one second to get a stable estimate.
sprang4847ae62017-06-27 07:06:52 -07003751 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003752 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003753 video_source_.IncomingCapturedFrame(
3754 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003755 WaitForEncodedFrame(timestamp_ms);
sprangc5d62e22017-04-02 23:53:04 -07003756 }
3757
3758 // Trigger CPU overuse, reduce framerate by 2/3.
mflodmancc3d4422017-08-03 08:27:51 -07003759 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003760 int num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003761 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003762 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003763 video_source_.IncomingCapturedFrame(
3764 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003765 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003766 ++num_frames_dropped;
3767 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003768 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003769 }
3770 }
3771
sprang4847ae62017-06-27 07:06:52 -07003772 // Add some slack to account for frames dropped by the frame dropper.
3773 const int kErrorMargin = 1;
3774 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003775 kErrorMargin);
3776
3777 // Trigger CPU overuse, reduce framerate by 2/3 again.
mflodmancc3d4422017-08-03 08:27:51 -07003778 video_stream_encoder_->TriggerCpuOveruse();
sprangc5d62e22017-04-02 23:53:04 -07003779 num_frames_dropped = 0;
Åsa Persson8c1bf952018-09-13 10:42:19 +02003780 for (int i = 0; i <= max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003781 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003782 video_source_.IncomingCapturedFrame(
3783 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003784 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003785 ++num_frames_dropped;
3786 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003787 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003788 }
3789 }
sprang4847ae62017-06-27 07:06:52 -07003790 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
sprangc5d62e22017-04-02 23:53:04 -07003791 kErrorMargin);
3792
3793 // Go back up one step.
mflodmancc3d4422017-08-03 08:27:51 -07003794 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003795 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003796 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003797 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003798 video_source_.IncomingCapturedFrame(
3799 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003800 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003801 ++num_frames_dropped;
3802 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003803 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003804 }
3805 }
sprang4847ae62017-06-27 07:06:52 -07003806 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
sprangc5d62e22017-04-02 23:53:04 -07003807 kErrorMargin);
3808
3809 // Go back up to original mode.
mflodmancc3d4422017-08-03 08:27:51 -07003810 video_stream_encoder_->TriggerCpuNormalUsage();
sprangc5d62e22017-04-02 23:53:04 -07003811 num_frames_dropped = 0;
sprang4847ae62017-06-27 07:06:52 -07003812 for (int i = 0; i < max_framerate_; ++i) {
sprangc5d62e22017-04-02 23:53:04 -07003813 timestamp_ms += kFrameIntervalMs;
sprangc5d62e22017-04-02 23:53:04 -07003814 video_source_.IncomingCapturedFrame(
3815 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07003816 if (!WaitForFrame(kFrameTimeoutMs)) {
sprangc5d62e22017-04-02 23:53:04 -07003817 ++num_frames_dropped;
3818 } else {
Åsa Perssonc74d8da2017-12-04 14:13:56 +01003819 sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight);
sprangc5d62e22017-04-02 23:53:04 -07003820 }
3821 }
3822 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
3823
mflodmancc3d4422017-08-03 08:27:51 -07003824 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003825}
3826
mflodmancc3d4422017-08-03 08:27:51 -07003827TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) {
sprangc5d62e22017-04-02 23:53:04 -07003828 const int kFramerateFps = 5;
3829 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
sprangc5d62e22017-04-02 23:53:04 -07003830 const int kFrameWidth = 1280;
3831 const int kFrameHeight = 720;
3832
sprang4847ae62017-06-27 07:06:52 -07003833 // Reconfigure encoder with two temporal layers and screensharing, which will
3834 // disable frame dropping and make testing easier.
Niels Möllerf1338562018-04-26 09:51:47 +02003835 ResetEncoder("VP8", 1, 2, 1, true);
sprang4847ae62017-06-27 07:06:52 -07003836
Erik Språng4c6ca302019-04-08 15:14:01 +02003837 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003838 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3839 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07003840 video_stream_encoder_->SetSource(
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003841 &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION);
sprangc5d62e22017-04-02 23:53:04 -07003842 video_source_.set_adaptation_enabled(true);
3843
sprang4847ae62017-06-27 07:06:52 -07003844 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
sprangc5d62e22017-04-02 23:53:04 -07003845
3846 // Trigger overuse as much as we can.
Jonathan Yubc771b72017-12-08 17:04:29 -08003847 rtc::VideoSinkWants last_wants;
3848 do {
3849 last_wants = video_source_.sink_wants();
3850
sprangc5d62e22017-04-02 23:53:04 -07003851 // Insert frames to get a new fps estimate...
3852 for (int j = 0; j < kFramerateFps; ++j) {
3853 video_source_.IncomingCapturedFrame(
3854 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
Jonathan Yubc771b72017-12-08 17:04:29 -08003855 if (video_source_.last_sent_width()) {
3856 sink_.WaitForEncodedFrame(timestamp_ms);
3857 }
sprangc5d62e22017-04-02 23:53:04 -07003858 timestamp_ms += kFrameIntervalMs;
Sebastian Jansson40889f32019-04-17 12:11:20 +02003859 fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs));
sprangc5d62e22017-04-02 23:53:04 -07003860 }
3861 // ...and then try to adapt again.
mflodmancc3d4422017-08-03 08:27:51 -07003862 video_stream_encoder_->TriggerCpuOveruse();
Jonathan Yubc771b72017-12-08 17:04:29 -08003863 } while (video_source_.sink_wants().max_framerate_fps <
3864 last_wants.max_framerate_fps);
sprangc5d62e22017-04-02 23:53:04 -07003865
Jonathan Yubc771b72017-12-08 17:04:29 -08003866 VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps);
asaperssonf7e294d2017-06-13 23:25:22 -07003867
mflodmancc3d4422017-08-03 08:27:51 -07003868 video_stream_encoder_->Stop();
sprangc5d62e22017-04-02 23:53:04 -07003869}
asaperssonf7e294d2017-06-13 23:25:22 -07003870
mflodmancc3d4422017-08-03 08:27:51 -07003871TEST_F(VideoStreamEncoderTest,
3872 AdaptsResolutionAndFramerateForLowQuality_BalancedMode) {
asaperssonf7e294d2017-06-13 23:25:22 -07003873 const int kWidth = 1280;
3874 const int kHeight = 720;
3875 const int64_t kFrameIntervalMs = 150;
3876 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02003877 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02003878 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
3879 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07003880
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003881 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07003882 AdaptingFrameForwarder source;
3883 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003884 video_stream_encoder_->SetSource(&source,
3885 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07003886 timestamp_ms += kFrameIntervalMs;
3887 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003888 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02003889 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07003890 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
3891 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3892 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3893
3894 // Trigger adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003895 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003896 timestamp_ms += kFrameIntervalMs;
3897 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003898 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003899 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
3900 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3901 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3902 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3903
3904 // Trigger adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07003905 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003906 timestamp_ms += kFrameIntervalMs;
3907 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003908 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003909 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
3910 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3911 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
3912 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3913
3914 // Trigger adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003915 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003916 timestamp_ms += kFrameIntervalMs;
3917 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003918 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003919 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3920 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3921 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3922 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3923
3924 // Trigger adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003925 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003926 timestamp_ms += kFrameIntervalMs;
3927 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003928 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003929 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3930 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3931 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3932 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3933
3934 // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003935 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003936 timestamp_ms += kFrameIntervalMs;
3937 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003938 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003939 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3940 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3941 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3942 EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3943
3944 // Trigger adapt down, expect scaled down resolution (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003945 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003946 timestamp_ms += kFrameIntervalMs;
3947 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003948 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003949 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
3950 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3951 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3952 EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3953
3954 // Trigger adapt down, expect reduced fps (320x180@7fps).
mflodmancc3d4422017-08-03 08:27:51 -07003955 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003956 timestamp_ms += kFrameIntervalMs;
3957 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003958 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003959 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
3960 rtc::VideoSinkWants last_wants = source.sink_wants();
3961 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3962 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3963 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3964
3965 // Trigger adapt down, min resolution reached, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07003966 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07003967 timestamp_ms += kFrameIntervalMs;
3968 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003969 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003970 VerifyFpsEqResolutionEq(source.sink_wants(), last_wants);
3971 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3972 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3973 EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3974
3975 // Trigger adapt down, expect expect increased fps (320x180@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003976 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003977 timestamp_ms += kFrameIntervalMs;
3978 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003979 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003980 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
3981 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3982 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3983 EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3984
3985 // Trigger adapt up, expect upscaled resolution (480x270@10fps).
mflodmancc3d4422017-08-03 08:27:51 -07003986 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003987 timestamp_ms += kFrameIntervalMs;
3988 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003989 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07003990 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
3991 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
3992 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
3993 EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes);
3994
3995 // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07003996 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07003997 timestamp_ms += kFrameIntervalMs;
3998 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07003999 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004000 VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants());
4001 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4002 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4003 EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4004
4005 // Trigger adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004006 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004007 timestamp_ms += kFrameIntervalMs;
4008 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004009 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004010 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4011 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4012 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4013 EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4014
4015 // Trigger adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004016 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004017 timestamp_ms += kFrameIntervalMs;
4018 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004019 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004020 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4021 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4022 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4023 EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4024
4025 // Trigger adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004026 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004027 timestamp_ms += kFrameIntervalMs;
4028 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004029 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004030 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4031 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4032 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4033 EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4034
Åsa Persson30ab0152019-08-27 12:22:33 +02004035 // Trigger adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004036 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004037 timestamp_ms += kFrameIntervalMs;
4038 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004039 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004040 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004041 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004042 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4043 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4044 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4045
4046 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004047 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004048 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004049 EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4050
mflodmancc3d4422017-08-03 08:27:51 -07004051 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004052}
4053
mflodmancc3d4422017-08-03 08:27:51 -07004054TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) {
asaperssonf7e294d2017-06-13 23:25:22 -07004055 const int kWidth = 1280;
4056 const int kHeight = 720;
4057 const int64_t kFrameIntervalMs = 150;
4058 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004059 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004060 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4061 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004062
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004063 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004064 AdaptingFrameForwarder source;
4065 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004066 video_stream_encoder_->SetSource(&source,
4067 webrtc::DegradationPreference::BALANCED);
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(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004071 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004072 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4073 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4074 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4075 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4076 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4077 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4078
4079 // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004080 video_stream_encoder_->TriggerCpuOveruse();
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(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004084 VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight);
4085 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4086 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4087 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4088 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4089 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4090 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4091
4092 // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004093 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004094 timestamp_ms += kFrameIntervalMs;
4095 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004096 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004097 VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants());
4098 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4099 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4100 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4101 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4102 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4103 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4104
4105 // Trigger quality adapt down, expect reduced fps (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004106 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004107 timestamp_ms += kFrameIntervalMs;
4108 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004109 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004110 VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants());
4111 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4112 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4113 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4114 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4115 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4116 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4117
4118 // Trigger cpu adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004119 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004120 timestamp_ms += kFrameIntervalMs;
4121 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004122 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004123 VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants());
4124 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4125 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4126 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4127 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4128 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4129 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4130
4131 // Trigger quality adapt up, expect upscaled resolution (960x540@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004132 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004133 timestamp_ms += kFrameIntervalMs;
4134 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004135 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004136 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
4137 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4138 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4139 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
4140 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4141 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4142 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4143
4144 // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004145 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004146 timestamp_ms += kFrameIntervalMs;
4147 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004148 WaitForEncodedFrame(kWidth, kHeight);
asaperssonf7e294d2017-06-13 23:25:22 -07004149 VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants());
Åsa Persson8c1bf952018-09-13 10:42:19 +02004150 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004151 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4152 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4153 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4154 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4155 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4156 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4157
4158 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004159 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004160 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004161 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4162 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4163
mflodmancc3d4422017-08-03 08:27:51 -07004164 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004165}
4166
mflodmancc3d4422017-08-03 08:27:51 -07004167TEST_F(VideoStreamEncoderTest,
4168 AdaptWithTwoReasonsAndDifferentOrder_Resolution) {
asaperssonf7e294d2017-06-13 23:25:22 -07004169 const int kWidth = 640;
4170 const int kHeight = 360;
4171 const int kFpsLimit = 15;
4172 const int64_t kFrameIntervalMs = 150;
4173 int64_t timestamp_ms = kFrameIntervalMs;
Erik Språng4c6ca302019-04-08 15:14:01 +02004174 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004175 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4176 DataRate::bps(kTargetBitrateBps), 0, 0);
asaperssonf7e294d2017-06-13 23:25:22 -07004177
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004178 // Enable BALANCED preference, no initial limitation.
asaperssonf7e294d2017-06-13 23:25:22 -07004179 AdaptingFrameForwarder source;
4180 source.set_adaptation_enabled(true);
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07004181 video_stream_encoder_->SetSource(&source,
4182 webrtc::DegradationPreference::BALANCED);
asaperssonf7e294d2017-06-13 23:25:22 -07004183 timestamp_ms += kFrameIntervalMs;
4184 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004185 WaitForEncodedFrame(kWidth, kHeight);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004186 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004187 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4188 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4189 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4190 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4191 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4192 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4193
4194 // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004195 video_stream_encoder_->TriggerCpuOveruse();
asaperssonf7e294d2017-06-13 23:25:22 -07004196 timestamp_ms += kFrameIntervalMs;
4197 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004198 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004199 VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit);
4200 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4201 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4202 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4203 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4204 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4205 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4206
4207 // Trigger quality adapt down, expect scaled down resolution (480x270@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004208 video_stream_encoder_->TriggerQualityLow();
asaperssonf7e294d2017-06-13 23:25:22 -07004209 timestamp_ms += kFrameIntervalMs;
4210 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004211 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004212 VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants());
4213 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
4214 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4215 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4216 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate);
4217 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4218 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4219
4220 // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps).
mflodmancc3d4422017-08-03 08:27:51 -07004221 video_stream_encoder_->TriggerCpuNormalUsage();
asaperssonf7e294d2017-06-13 23:25:22 -07004222 timestamp_ms += kFrameIntervalMs;
4223 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004224 WaitForEncodedFrame(timestamp_ms);
asaperssonf7e294d2017-06-13 23:25:22 -07004225 VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants());
4226 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4227 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate);
4228 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4229 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4230 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4231 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4232
4233 // Trigger quality adapt up, expect increased fps (640x360@30fps).
mflodmancc3d4422017-08-03 08:27:51 -07004234 video_stream_encoder_->TriggerQualityHigh();
asaperssonf7e294d2017-06-13 23:25:22 -07004235 timestamp_ms += kFrameIntervalMs;
4236 source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight));
sprang4847ae62017-06-27 07:06:52 -07004237 WaitForEncodedFrame(timestamp_ms);
Åsa Persson8c1bf952018-09-13 10:42:19 +02004238 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004239 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
4240 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate);
4241 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
4242 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate);
4243 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4244 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4245
4246 // Trigger adapt up, expect no change.
mflodmancc3d4422017-08-03 08:27:51 -07004247 video_stream_encoder_->TriggerQualityHigh();
Åsa Persson8c1bf952018-09-13 10:42:19 +02004248 VerifyFpsMaxResolutionMax(source.sink_wants());
asaperssonf7e294d2017-06-13 23:25:22 -07004249 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
4250 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
4251
mflodmancc3d4422017-08-03 08:27:51 -07004252 video_stream_encoder_->Stop();
asaperssonf7e294d2017-06-13 23:25:22 -07004253}
4254
mflodmancc3d4422017-08-03 08:27:51 -07004255TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) {
ilnik6b826ef2017-06-16 06:53:48 -07004256 const int kFrameWidth = 1920;
4257 const int kFrameHeight = 1080;
4258 // 3/4 of 1920.
4259 const int kAdaptedFrameWidth = 1440;
4260 // 3/4 of 1080 rounded down to multiple of 4.
4261 const int kAdaptedFrameHeight = 808;
4262 const int kFramerate = 24;
4263
Erik Språng4c6ca302019-04-08 15:14:01 +02004264 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004265 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4266 DataRate::bps(kTargetBitrateBps), 0, 0);
ilnik6b826ef2017-06-16 06:53:48 -07004267 // Trigger reconfigure encoder (without resetting the entire instance).
4268 VideoEncoderConfig video_encoder_config;
Niels Möller259a4972018-04-05 15:36:51 +02004269 video_encoder_config.codec_type = kVideoCodecVP8;
ilnik6b826ef2017-06-16 06:53:48 -07004270 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
4271 video_encoder_config.number_of_streams = 1;
4272 video_encoder_config.video_stream_factory =
4273 new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate);
mflodmancc3d4422017-08-03 08:27:51 -07004274 video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config),
Niels Möllerf1338562018-04-26 09:51:47 +02004275 kMaxPayloadLength);
mflodmancc3d4422017-08-03 08:27:51 -07004276 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
ilnik6b826ef2017-06-16 06:53:48 -07004277
4278 video_source_.set_adaptation_enabled(true);
4279
4280 video_source_.IncomingCapturedFrame(
4281 CreateFrame(1, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004282 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004283
4284 // Trigger CPU overuse, downscale by 3/4.
mflodmancc3d4422017-08-03 08:27:51 -07004285 video_stream_encoder_->TriggerCpuOveruse();
ilnik6b826ef2017-06-16 06:53:48 -07004286 video_source_.IncomingCapturedFrame(
4287 CreateFrame(2, kFrameWidth, kFrameHeight));
sprang4847ae62017-06-27 07:06:52 -07004288 WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight);
ilnik6b826ef2017-06-16 06:53:48 -07004289
mflodmancc3d4422017-08-03 08:27:51 -07004290 video_stream_encoder_->Stop();
ilnik6b826ef2017-06-16 06:53:48 -07004291}
4292
mflodmancc3d4422017-08-03 08:27:51 -07004293TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) {
sprang4847ae62017-06-27 07:06:52 -07004294 const int kFrameWidth = 1280;
4295 const int kFrameHeight = 720;
4296 const int kLowFps = 2;
4297 const int kHighFps = 30;
4298
Erik Språng4c6ca302019-04-08 15:14:01 +02004299 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004300 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4301 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004302
4303 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4304 max_framerate_ = kLowFps;
4305
4306 // Insert 2 seconds of 2fps video.
4307 for (int i = 0; i < kLowFps * 2; ++i) {
4308 video_source_.IncomingCapturedFrame(
4309 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4310 WaitForEncodedFrame(timestamp_ms);
4311 timestamp_ms += 1000 / kLowFps;
4312 }
4313
4314 // Make sure encoder is updated with new target.
Erik Språng4c6ca302019-04-08 15:14:01 +02004315 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004316 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4317 DataRate::bps(kTargetBitrateBps), 0, 0);
sprang4847ae62017-06-27 07:06:52 -07004318 video_source_.IncomingCapturedFrame(
4319 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4320 WaitForEncodedFrame(timestamp_ms);
4321 timestamp_ms += 1000 / kLowFps;
4322
4323 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
4324
4325 // Insert 30fps frames for just a little more than the forced update period.
Niels Möllerfe407b72019-09-10 10:48:48 +02004326 const int kVcmTimerIntervalFrames = (kProcessIntervalMs * kHighFps) / 1000;
sprang4847ae62017-06-27 07:06:52 -07004327 const int kFrameIntervalMs = 1000 / kHighFps;
4328 max_framerate_ = kHighFps;
4329 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
4330 video_source_.IncomingCapturedFrame(
4331 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4332 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
4333 // be dropped if the encoder hans't been updated with the new higher target
4334 // framerate yet, causing it to overshoot the target bitrate and then
4335 // suffering the wrath of the media optimizer.
4336 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
4337 timestamp_ms += kFrameIntervalMs;
4338 }
4339
4340 // Don expect correct measurement just yet, but it should be higher than
4341 // before.
4342 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
4343
mflodmancc3d4422017-08-03 08:27:51 -07004344 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004345}
4346
mflodmancc3d4422017-08-03 08:27:51 -07004347TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) {
sprang4847ae62017-06-27 07:06:52 -07004348 const int kFrameWidth = 1280;
4349 const int kFrameHeight = 720;
4350 const int kTargetBitrateBps = 1000000;
4351
4352 MockBitrateObserver bitrate_observer;
Niels Möller0327c2d2018-05-21 14:09:31 +02004353 video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer);
Erik Språng4c6ca302019-04-08 15:14:01 +02004354 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004355 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4356 DataRate::bps(kTargetBitrateBps), 0, 0);
mflodmancc3d4422017-08-03 08:27:51 -07004357 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
sprang4847ae62017-06-27 07:06:52 -07004358
4359 // Insert a first video frame, causes another bitrate update.
4360 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4361 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1);
4362 video_source_.IncomingCapturedFrame(
4363 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4364 WaitForEncodedFrame(timestamp_ms);
4365
4366 // Next, simulate video suspension due to pacer queue overrun.
Florent Castellia8336d32019-09-09 13:36:55 +02004367 video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0),
4368 DataRate::bps(0), 0, 1);
sprang4847ae62017-06-27 07:06:52 -07004369
4370 // Skip ahead until a new periodic parameter update should have occured.
Niels Möllerfe407b72019-09-10 10:48:48 +02004371 timestamp_ms += kProcessIntervalMs;
4372 fake_clock_.AdvanceTime(TimeDelta::ms(kProcessIntervalMs));
sprang4847ae62017-06-27 07:06:52 -07004373
4374 // Bitrate observer should not be called.
4375 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0);
4376 video_source_.IncomingCapturedFrame(
4377 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4378 ExpectDroppedFrame();
4379
mflodmancc3d4422017-08-03 08:27:51 -07004380 video_stream_encoder_->Stop();
sprang4847ae62017-06-27 07:06:52 -07004381}
ilnik6b826ef2017-06-16 06:53:48 -07004382
Niels Möller4db138e2018-04-19 09:04:13 +02004383TEST_F(VideoStreamEncoderTest,
4384 DefaultCpuAdaptationThresholdsForSoftwareEncoder) {
4385 const int kFrameWidth = 1280;
4386 const int kFrameHeight = 720;
4387 const CpuOveruseOptions default_options;
Erik Språng4c6ca302019-04-08 15:14:01 +02004388 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004389 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4390 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004391 video_source_.IncomingCapturedFrame(
4392 CreateFrame(1, kFrameWidth, kFrameHeight));
4393 WaitForEncodedFrame(1);
4394 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4395 .low_encode_usage_threshold_percent,
4396 default_options.low_encode_usage_threshold_percent);
4397 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4398 .high_encode_usage_threshold_percent,
4399 default_options.high_encode_usage_threshold_percent);
4400 video_stream_encoder_->Stop();
4401}
4402
4403TEST_F(VideoStreamEncoderTest,
4404 HigherCpuAdaptationThresholdsForHardwareEncoder) {
4405 const int kFrameWidth = 1280;
4406 const int kFrameHeight = 720;
4407 CpuOveruseOptions hardware_options;
4408 hardware_options.low_encode_usage_threshold_percent = 150;
4409 hardware_options.high_encode_usage_threshold_percent = 200;
Mirta Dvornicicccc1b572019-01-15 12:42:18 +01004410 fake_encoder_.SetIsHardwareAccelerated(true);
Niels Möller4db138e2018-04-19 09:04:13 +02004411
Erik Språng4c6ca302019-04-08 15:14:01 +02004412 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004413 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4414 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller4db138e2018-04-19 09:04:13 +02004415 video_source_.IncomingCapturedFrame(
4416 CreateFrame(1, kFrameWidth, kFrameHeight));
4417 WaitForEncodedFrame(1);
4418 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4419 .low_encode_usage_threshold_percent,
4420 hardware_options.low_encode_usage_threshold_percent);
4421 EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions()
4422 .high_encode_usage_threshold_percent,
4423 hardware_options.high_encode_usage_threshold_percent);
4424 video_stream_encoder_->Stop();
4425}
4426
Niels Möller6bb5ab92019-01-11 11:11:10 +01004427TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) {
4428 const int kFrameWidth = 320;
4429 const int kFrameHeight = 240;
4430 const int kFps = 30;
4431 const int kTargetBitrateBps = 120000;
4432 const int kNumFramesInRun = kFps * 5; // Runs of five seconds.
4433
Erik Språng4c6ca302019-04-08 15:14:01 +02004434 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004435 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4436 DataRate::bps(kTargetBitrateBps), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004437
4438 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4439 max_framerate_ = kFps;
4440
4441 // Insert 3 seconds of video, verify number of drops with normal bitrate.
4442 fake_encoder_.SimulateOvershoot(1.0);
4443 int num_dropped = 0;
4444 for (int i = 0; i < kNumFramesInRun; ++i) {
4445 video_source_.IncomingCapturedFrame(
4446 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4447 // Wait up to two frame durations for a frame to arrive.
4448 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4449 ++num_dropped;
4450 }
4451 timestamp_ms += 1000 / kFps;
4452 }
4453
Erik Språnga8d48ab2019-02-08 14:17:40 +01004454 // Framerate should be measured to be near the expected target rate.
4455 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4456
4457 // Frame drops should be within 5% of expected 0%.
4458 EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004459
4460 // Make encoder produce frames at double the expected bitrate during 3 seconds
4461 // of video, verify number of drops. Rate needs to be slightly changed in
4462 // order to force the rate to be reconfigured.
Erik Språng7ca375c2019-02-06 16:20:17 +01004463 double overshoot_factor = 2.0;
4464 if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) {
4465 // With bitrate adjuster, when need to overshoot even more to trigger
4466 // frame dropping.
4467 overshoot_factor *= 2;
4468 }
4469 fake_encoder_.SimulateOvershoot(overshoot_factor);
Erik Språng610c7632019-03-06 15:37:33 +01004470 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004471 DataRate::bps(kTargetBitrateBps + 1000),
Florent Castellia8336d32019-09-09 13:36:55 +02004472 DataRate::bps(kTargetBitrateBps + 1000),
Erik Språng4c6ca302019-04-08 15:14:01 +02004473 DataRate::bps(kTargetBitrateBps + 1000), 0, 0);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004474 num_dropped = 0;
4475 for (int i = 0; i < kNumFramesInRun; ++i) {
4476 video_source_.IncomingCapturedFrame(
4477 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4478 // Wait up to two frame durations for a frame to arrive.
4479 if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) {
4480 ++num_dropped;
4481 }
4482 timestamp_ms += 1000 / kFps;
4483 }
4484
Erik Språng4c6ca302019-04-08 15:14:01 +02004485 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004486 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4487 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språnga8d48ab2019-02-08 14:17:40 +01004488
4489 // Target framerate should be still be near the expected target, despite
4490 // the frame drops.
4491 EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
4492
4493 // Frame drops should be within 5% of expected 50%.
4494 EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004495
4496 video_stream_encoder_->Stop();
4497}
4498
4499TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) {
4500 const int kFrameWidth = 320;
4501 const int kFrameHeight = 240;
4502 const int kActualInputFps = 24;
4503 const int kTargetBitrateBps = 120000;
4504
4505 ASSERT_GT(max_framerate_, kActualInputFps);
4506
4507 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4508 max_framerate_ = kActualInputFps;
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);
Niels Möller6bb5ab92019-01-11 11:11:10 +01004512
4513 // Insert 3 seconds of video, with an input fps lower than configured max.
4514 for (int i = 0; i < kActualInputFps * 3; ++i) {
4515 video_source_.IncomingCapturedFrame(
4516 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
4517 // Wait up to two frame durations for a frame to arrive.
4518 WaitForEncodedFrame(timestamp_ms);
4519 timestamp_ms += 1000 / kActualInputFps;
4520 }
4521
4522 EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1);
4523
4524 video_stream_encoder_->Stop();
4525}
4526
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004527TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) {
4528 VideoFrame::UpdateRect rect;
Erik Språng4c6ca302019-04-08 15:14:01 +02004529 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004530 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4531 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiy71aee3a2019-02-18 13:01:26 +01004532
4533 fake_encoder_.BlockNextEncode();
4534 video_source_.IncomingCapturedFrame(
4535 CreateFrameWithUpdatedPixel(1, nullptr, 0));
4536 WaitForEncodedFrame(1);
4537 // On the very first frame full update should be forced.
4538 rect = fake_encoder_.GetLastUpdateRect();
4539 EXPECT_EQ(rect.offset_x, 0);
4540 EXPECT_EQ(rect.offset_y, 0);
4541 EXPECT_EQ(rect.height, codec_height_);
4542 EXPECT_EQ(rect.width, codec_width_);
4543 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
4544 // call to ContinueEncode.
4545 video_source_.IncomingCapturedFrame(
4546 CreateFrameWithUpdatedPixel(2, nullptr, 1));
4547 ExpectDroppedFrame();
4548 video_source_.IncomingCapturedFrame(
4549 CreateFrameWithUpdatedPixel(3, nullptr, 10));
4550 ExpectDroppedFrame();
4551 fake_encoder_.ContinueEncode();
4552 WaitForEncodedFrame(3);
4553 // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect.
4554 rect = fake_encoder_.GetLastUpdateRect();
4555 EXPECT_EQ(rect.offset_x, 1);
4556 EXPECT_EQ(rect.offset_y, 0);
4557 EXPECT_EQ(rect.width, 10);
4558 EXPECT_EQ(rect.height, 1);
4559
4560 video_source_.IncomingCapturedFrame(
4561 CreateFrameWithUpdatedPixel(4, nullptr, 0));
4562 WaitForEncodedFrame(4);
4563 // Previous frame was encoded, so no accumulation should happen.
4564 rect = fake_encoder_.GetLastUpdateRect();
4565 EXPECT_EQ(rect.offset_x, 0);
4566 EXPECT_EQ(rect.offset_y, 0);
4567 EXPECT_EQ(rect.width, 1);
4568 EXPECT_EQ(rect.height, 1);
4569
4570 video_stream_encoder_->Stop();
4571}
4572
Erik Språngd7329ca2019-02-21 21:19:53 +01004573TEST_F(VideoStreamEncoderTest, SetsFrameTypes) {
Erik Språng4c6ca302019-04-08 15:14:01 +02004574 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004575 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4576 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004577
4578 // First frame is always keyframe.
4579 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4580 WaitForEncodedFrame(1);
Niels Möller8f7ce222019-03-21 15:43:58 +01004581 EXPECT_THAT(
4582 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004583 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004584
4585 // Insert delta frame.
4586 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4587 WaitForEncodedFrame(2);
Niels Möller8f7ce222019-03-21 15:43:58 +01004588 EXPECT_THAT(
4589 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004590 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004591
4592 // Request next frame be a key-frame.
4593 video_stream_encoder_->SendKeyFrame();
4594 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4595 WaitForEncodedFrame(3);
Niels Möller8f7ce222019-03-21 15:43:58 +01004596 EXPECT_THAT(
4597 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004598 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004599
4600 video_stream_encoder_->Stop();
4601}
4602
4603TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) {
4604 // Setup simulcast with three streams.
4605 ResetEncoder("VP8", 3, 1, 1, false);
Erik Språng610c7632019-03-06 15:37:33 +01004606 video_stream_encoder_->OnBitrateUpdated(
Erik Språng4c6ca302019-04-08 15:14:01 +02004607 DataRate::bps(kSimulcastTargetBitrateBps),
Florent Castellia8336d32019-09-09 13:36:55 +02004608 DataRate::bps(kSimulcastTargetBitrateBps),
Erik Språng4c6ca302019-04-08 15:14:01 +02004609 DataRate::bps(kSimulcastTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004610 // Wait for all three layers before triggering event.
4611 sink_.SetNumExpectedLayers(3);
4612
4613 // First frame is always keyframe.
4614 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
4615 WaitForEncodedFrame(1);
4616 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004617 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
4618 VideoFrameType::kVideoFrameKey,
4619 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004620
4621 // Insert delta frame.
4622 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
4623 WaitForEncodedFrame(2);
4624 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004625 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta,
4626 VideoFrameType::kVideoFrameDelta,
4627 VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004628
4629 // Request next frame be a key-frame.
4630 // Only first stream is configured to produce key-frame.
4631 video_stream_encoder_->SendKeyFrame();
4632 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
4633 WaitForEncodedFrame(3);
Sergey Silkine62a08a2019-05-13 13:45:39 +02004634
4635 // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently
4636 // keyframe request on any layer triggers keyframe on all layers.
Erik Språngd7329ca2019-02-21 21:19:53 +01004637 EXPECT_THAT(fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004638 ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey,
Sergey Silkine62a08a2019-05-13 13:45:39 +02004639 VideoFrameType::kVideoFrameKey,
4640 VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004641
4642 video_stream_encoder_->Stop();
4643}
4644
4645TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) {
4646 // Configure internal source factory and setup test again.
4647 encoder_factory_.SetHasInternalSource(true);
4648 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004649 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004650 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4651 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngd7329ca2019-02-21 21:19:53 +01004652
4653 // Call encoder directly, simulating internal source where encoded frame
4654 // callback in VideoStreamEncoder is called despite no OnFrame().
4655 fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true);
4656 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004657 EXPECT_THAT(
4658 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004659 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004660
Niels Möller8f7ce222019-03-21 15:43:58 +01004661 const std::vector<VideoFrameType> kDeltaFrame = {
4662 VideoFrameType::kVideoFrameDelta};
Erik Språngd7329ca2019-02-21 21:19:53 +01004663 // Need to set timestamp manually since manually for injected frame.
4664 VideoFrame frame = CreateFrame(101, nullptr);
4665 frame.set_timestamp(101);
4666 fake_encoder_.InjectFrame(frame, false);
4667 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004668 EXPECT_THAT(
4669 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004670 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004671
4672 // Request key-frame. The forces a dummy frame down into the encoder.
4673 fake_encoder_.ExpectNullFrame();
4674 video_stream_encoder_->SendKeyFrame();
4675 EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs));
Niels Möller8f7ce222019-03-21 15:43:58 +01004676 EXPECT_THAT(
4677 fake_encoder_.LastFrameTypes(),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004678 ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey}));
Erik Språngd7329ca2019-02-21 21:19:53 +01004679
4680 video_stream_encoder_->Stop();
4681}
Erik Språngb7cb7b52019-02-26 15:52:33 +01004682
4683TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) {
4684 // Configure internal source factory and setup test again.
4685 encoder_factory_.SetHasInternalSource(true);
4686 ResetEncoder("VP8", 1, 1, 1, false);
Erik Språng4c6ca302019-04-08 15:14:01 +02004687 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004688 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4689 DataRate::bps(kTargetBitrateBps), 0, 0);
Erik Språngb7cb7b52019-02-26 15:52:33 +01004690
4691 int64_t timestamp = 1;
4692 EncodedImage image;
Niels Möller4d504c72019-06-18 15:56:56 +02004693 image.SetEncodedData(
4694 EncodedImageBuffer::Create(kTargetBitrateBps / kDefaultFramerate / 8));
Erik Språngb7cb7b52019-02-26 15:52:33 +01004695 image.capture_time_ms_ = ++timestamp;
4696 image.SetTimestamp(static_cast<uint32_t>(timestamp * 90));
4697 const int64_t kEncodeFinishDelayMs = 10;
4698 image.timing_.encode_start_ms = timestamp;
4699 image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs;
4700 fake_encoder_.InjectEncodedImage(image);
4701 // Wait for frame without incrementing clock.
4702 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4703 // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored
4704 // capture timestamp should be kEncodeFinishDelayMs in the past.
4705 EXPECT_EQ(sink_.GetLastCaptureTimeMs(),
4706 fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec -
4707 kEncodeFinishDelayMs);
4708
4709 video_stream_encoder_->Stop();
4710}
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +02004711
4712TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) {
4713 // Configure internal source factory and setup test again.
4714 encoder_factory_.SetHasInternalSource(true);
4715 ResetEncoder("H264", 1, 1, 1, false);
4716
4717 EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps));
4718 image._frameType = VideoFrameType::kVideoFrameKey;
4719
4720 CodecSpecificInfo codec_specific_info;
4721 codec_specific_info.codecType = kVideoCodecH264;
4722
4723 RTPFragmentationHeader fragmentation;
4724 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4725 fragmentation.fragmentationOffset[0] = 4;
4726 fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4;
4727
4728 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4729 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4730
4731 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4732 testing::ElementsAreArray(optimal_sps));
4733 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4734 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4735 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4736 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4737
4738 video_stream_encoder_->Stop();
4739}
4740
4741TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) {
4742 uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps,
4743 0x00, 0x00, 0x03, 0x03, 0xF4,
4744 0x05, 0x03, 0xC7, 0xC0};
4745
4746 // Configure internal source factory and setup test again.
4747 encoder_factory_.SetHasInternalSource(true);
4748 ResetEncoder("H264", 1, 1, 1, false);
4749
4750 EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps));
4751 image._frameType = VideoFrameType::kVideoFrameKey;
4752
4753 CodecSpecificInfo codec_specific_info;
4754 codec_specific_info.codecType = kVideoCodecH264;
4755
4756 RTPFragmentationHeader fragmentation;
4757 fragmentation.VerifyAndAllocateFragmentationHeader(1);
4758 fragmentation.fragmentationOffset[0] = 4;
4759 fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4;
4760
4761 fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation);
4762 EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs));
4763
4764 EXPECT_THAT(sink_.GetLastEncodedImageData(),
4765 testing::ElementsAreArray(optimal_sps));
4766 RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation();
4767 ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U);
4768 EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U);
4769 EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4);
4770
4771 video_stream_encoder_->Stop();
4772}
4773
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004774TEST_F(VideoStreamEncoderTest, CopiesVideoFrameMetadataAfterDownscale) {
4775 const int kFrameWidth = 1280;
4776 const int kFrameHeight = 720;
4777 const int kTargetBitrateBps = 300000; // To low for HD resolution.
4778
4779 video_stream_encoder_->OnBitrateUpdated(
Florent Castellia8336d32019-09-09 13:36:55 +02004780 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4781 DataRate::bps(kTargetBitrateBps), 0, 0);
Ilya Nikolaevskiyba96e2f2019-06-04 15:15:14 +02004782 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4783
4784 // Insert a first video frame. It should be dropped because of downscale in
4785 // resolution.
4786 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4787 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4788 frame.set_rotation(kVideoRotation_270);
4789 video_source_.IncomingCapturedFrame(frame);
4790
4791 ExpectDroppedFrame();
4792
4793 // Second frame is downscaled.
4794 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4795 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4796 frame.set_rotation(kVideoRotation_90);
4797 video_source_.IncomingCapturedFrame(frame);
4798
4799 WaitForEncodedFrame(timestamp_ms);
4800 sink_.CheckLastFrameRotationMatches(kVideoRotation_90);
4801
4802 // Insert another frame, also downscaled.
4803 timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4804 frame = CreateFrame(timestamp_ms, kFrameWidth / 2, kFrameHeight / 2);
4805 frame.set_rotation(kVideoRotation_180);
4806 video_source_.IncomingCapturedFrame(frame);
4807
4808 WaitForEncodedFrame(timestamp_ms);
4809 sink_.CheckLastFrameRotationMatches(kVideoRotation_180);
4810
4811 video_stream_encoder_->Stop();
4812}
4813
Erik Språng5056af02019-09-02 15:53:11 +02004814TEST_F(VideoStreamEncoderTest, BandwidthAllocationLowerBound) {
4815 const int kFrameWidth = 320;
4816 const int kFrameHeight = 180;
4817
4818 // Initial rate.
4819 video_stream_encoder_->OnBitrateUpdated(
4820 /*target_bitrate=*/DataRate::kbps(300),
Florent Castellia8336d32019-09-09 13:36:55 +02004821 /*stable_target_bitrate=*/DataRate::kbps(300),
Erik Språng5056af02019-09-02 15:53:11 +02004822 /*link_allocation=*/DataRate::kbps(300),
4823 /*fraction_lost=*/0,
4824 /*rtt_ms=*/0);
4825
4826 // Insert a first video frame so that encoder gets configured.
4827 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4828 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4829 frame.set_rotation(kVideoRotation_270);
4830 video_source_.IncomingCapturedFrame(frame);
4831 WaitForEncodedFrame(timestamp_ms);
4832
4833 // Set a target rate below the minimum allowed by the codec settings.
4834 VideoCodec codec_config = fake_encoder_.codec_config();
4835 DataRate min_rate = DataRate::kbps(codec_config.minBitrate);
4836 DataRate target_rate = min_rate - DataRate::kbps(1);
4837 video_stream_encoder_->OnBitrateUpdated(
4838 /*target_bitrate=*/target_rate,
Florent Castellia8336d32019-09-09 13:36:55 +02004839 /*stable_target_bitrate=*/target_rate,
Erik Språng5056af02019-09-02 15:53:11 +02004840 /*link_allocation=*/target_rate,
4841 /*fraction_lost=*/0,
4842 /*rtt_ms=*/0);
4843 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
4844
4845 // Target bitrate and bandwidth allocation should both be capped at min_rate.
4846 auto rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4847 ASSERT_TRUE(rate_settings.has_value());
4848 DataRate allocation_sum = DataRate::bps(rate_settings->bitrate.get_sum_bps());
4849 EXPECT_EQ(min_rate, allocation_sum);
4850 EXPECT_EQ(rate_settings->bandwidth_allocation, min_rate);
4851
4852 video_stream_encoder_->Stop();
4853}
4854
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02004855TEST_F(VideoStreamEncoderTest, EncoderRatesPropegatedOnReconfigure) {
4856 video_stream_encoder_->OnBitrateUpdated(
4857 DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps),
4858 DataRate::bps(kTargetBitrateBps), 0, 0);
4859 // Capture a frame and wait for it to synchronize with the encoder thread.
4860 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4861 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4862 WaitForEncodedFrame(1);
4863
4864 auto prev_rate_settings = fake_encoder_.GetAndResetLastRateControlSettings();
4865 ASSERT_TRUE(prev_rate_settings.has_value());
4866 EXPECT_EQ(static_cast<int>(prev_rate_settings->framerate_fps),
4867 kDefaultFramerate);
4868
4869 // Send 1s of video to ensure the framerate is stable at kDefaultFramerate.
4870 for (int i = 0; i < 2 * kDefaultFramerate; i++) {
4871 timestamp_ms += 1000 / kDefaultFramerate;
4872 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4873 WaitForEncodedFrame(timestamp_ms);
4874 }
4875 EXPECT_EQ(static_cast<int>(fake_encoder_.GetLastFramerate()),
4876 kDefaultFramerate);
4877 // Capture larger frame to trigger a reconfigure.
4878 codec_height_ *= 2;
4879 codec_width_ *= 2;
4880 timestamp_ms += 1000 / kDefaultFramerate;
4881 video_source_.IncomingCapturedFrame(CreateFrame(timestamp_ms, nullptr));
4882 WaitForEncodedFrame(timestamp_ms);
4883
4884 EXPECT_EQ(2, sink_.number_of_reconfigurations());
4885 auto current_rate_settings =
4886 fake_encoder_.GetAndResetLastRateControlSettings();
4887 // Ensure we have actually reconfigured twice
4888 // The rate settings should have been set again even though
4889 // they haven't changed.
4890 ASSERT_TRUE(current_rate_settings.has_value());
Evan Shrubsole7c079f62019-09-26 09:55:03 +02004891 EXPECT_EQ(prev_rate_settings, current_rate_settings);
Evan Shrubsolee32ae4f2019-09-25 12:50:23 +02004892
4893 video_stream_encoder_->Stop();
4894}
4895
philipeld9cc8c02019-09-16 14:53:40 +02004896struct MockEncoderSwitchRequestCallback : public EncoderSwitchRequestCallback {
4897 MOCK_METHOD0(RequestEncoderFallback, void());
4898 MOCK_METHOD1(RequestEncoderSwitch, void(const Config& conf));
4899};
4900
4901TEST_F(VideoStreamEncoderTest, BitrateEncoderSwitch) {
4902 constexpr int kDontCare = 100;
4903
4904 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4905 video_send_config_.encoder_settings.encoder_switch_request_callback =
4906 &switch_callback;
4907 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4908 encoder_config.codec_type = kVideoCodecVP8;
4909 webrtc::test::ScopedFieldTrials field_trial(
4910 "WebRTC-NetworkCondition-EncoderSwitch/"
4911 "codec_thresholds:VP8;100;-1|H264;-1;30000,"
4912 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4913
4914 // Reset encoder for new configuration to take effect.
4915 ConfigureEncoder(std::move(encoder_config));
4916
4917 // Send one frame to trigger ReconfigureEncoder.
4918 video_source_.IncomingCapturedFrame(
4919 CreateFrame(kDontCare, kDontCare, kDontCare));
4920
4921 using Config = EncoderSwitchRequestCallback::Config;
4922 EXPECT_CALL(switch_callback,
4923 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4924 Field(&Config::param, "ping"),
4925 Field(&Config::value, "pong"))));
4926
4927 video_stream_encoder_->OnBitrateUpdated(
4928 /*target_bitrate=*/DataRate::kbps(50),
4929 /*stable_target_bitrate=*/DataRate::kbps(kDontCare),
4930 /*link_allocation=*/DataRate::kbps(kDontCare),
4931 /*fraction_lost=*/0,
4932 /*rtt_ms=*/0);
4933
4934 video_stream_encoder_->Stop();
4935}
4936
4937TEST_F(VideoStreamEncoderTest, ResolutionEncoderSwitch) {
4938 constexpr int kSufficientBitrateToNotDrop = 1000;
4939 constexpr int kHighRes = 500;
4940 constexpr int kLowRes = 100;
4941
4942 StrictMock<MockEncoderSwitchRequestCallback> switch_callback;
4943 video_send_config_.encoder_settings.encoder_switch_request_callback =
4944 &switch_callback;
4945 webrtc::test::ScopedFieldTrials field_trial(
4946 "WebRTC-NetworkCondition-EncoderSwitch/"
4947 "codec_thresholds:VP8;120;-1|H264;-1;30000,"
4948 "to_codec:AV1,to_param:ping,to_value:pong,window:2.0/");
4949 VideoEncoderConfig encoder_config = video_encoder_config_.Copy();
4950 encoder_config.codec_type = kVideoCodecH264;
4951
4952 // Reset encoder for new configuration to take effect.
4953 ConfigureEncoder(std::move(encoder_config));
4954
4955 // The VideoStreamEncoder needs some bitrate before it can start encoding,
4956 // setting some bitrate so that subsequent calls to WaitForEncodedFrame does
4957 // not fail.
4958 video_stream_encoder_->OnBitrateUpdated(
4959 /*target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4960 /*stable_target_bitrate=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4961 /*link_allocation=*/DataRate::kbps(kSufficientBitrateToNotDrop),
4962 /*fraction_lost=*/0,
4963 /*rtt_ms=*/0);
4964
4965 // Send one frame to trigger ReconfigureEncoder.
4966 video_source_.IncomingCapturedFrame(CreateFrame(1, kHighRes, kHighRes));
4967 WaitForEncodedFrame(1);
4968
4969 using Config = EncoderSwitchRequestCallback::Config;
4970 EXPECT_CALL(switch_callback,
4971 RequestEncoderSwitch(AllOf(Field(&Config::codec_name, "AV1"),
4972 Field(&Config::param, "ping"),
4973 Field(&Config::value, "pong"))));
4974
4975 video_source_.IncomingCapturedFrame(CreateFrame(2, kLowRes, kLowRes));
4976 WaitForEncodedFrame(2);
4977
4978 video_stream_encoder_->Stop();
4979}
4980
Evan Shrubsole7c079f62019-09-26 09:55:03 +02004981TEST_F(VideoStreamEncoderTest,
4982 AllocationPropegratedToEncoderWhenTargetRateChanged) {
4983 const int kFrameWidth = 320;
4984 const int kFrameHeight = 180;
4985
4986 // Set initial rate.
4987 auto rate = DataRate::kbps(100);
4988 video_stream_encoder_->OnBitrateUpdated(
4989 /*target_bitrate=*/rate,
4990 /*stable_target_bitrate=*/rate,
4991 /*link_allocation=*/rate,
4992 /*fraction_lost=*/0,
4993 /*rtt_ms=*/0);
4994
4995 // Insert a first video frame so that encoder gets configured.
4996 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
4997 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
4998 frame.set_rotation(kVideoRotation_270);
4999 video_source_.IncomingCapturedFrame(frame);
5000 WaitForEncodedFrame(timestamp_ms);
5001 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5002
5003 // Change of target bitrate propagates to the encoder.
5004 auto new_stable_rate = rate - DataRate::kbps(5);
5005 video_stream_encoder_->OnBitrateUpdated(
5006 /*target_bitrate=*/new_stable_rate,
5007 /*stable_target_bitrate=*/new_stable_rate,
5008 /*link_allocation=*/rate,
5009 /*fraction_lost=*/0,
5010 /*rtt_ms=*/0);
5011 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5012 EXPECT_EQ(2, fake_encoder_.GetNumSetRates());
5013 video_stream_encoder_->Stop();
5014}
5015
5016TEST_F(VideoStreamEncoderTest,
5017 AllocationNotPropegratedToEncoderWhenTargetRateUnchanged) {
5018 const int kFrameWidth = 320;
5019 const int kFrameHeight = 180;
5020
5021 // Set initial rate.
5022 auto rate = DataRate::kbps(100);
5023 video_stream_encoder_->OnBitrateUpdated(
5024 /*target_bitrate=*/rate,
5025 /*stable_target_bitrate=*/rate,
5026 /*link_allocation=*/rate,
5027 /*fraction_lost=*/0,
5028 /*rtt_ms=*/0);
5029
5030 // Insert a first video frame so that encoder gets configured.
5031 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
5032 VideoFrame frame = CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight);
5033 frame.set_rotation(kVideoRotation_270);
5034 video_source_.IncomingCapturedFrame(frame);
5035 WaitForEncodedFrame(timestamp_ms);
5036 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5037
5038 // Set a higher target rate without changing the link_allocation. Should not
5039 // reset encoder's rate.
5040 auto new_stable_rate = rate - DataRate::kbps(5);
5041 video_stream_encoder_->OnBitrateUpdated(
5042 /*target_bitrate=*/rate,
5043 /*stable_target_bitrate=*/new_stable_rate,
5044 /*link_allocation=*/rate,
5045 /*fraction_lost=*/0,
5046 /*rtt_ms=*/0);
5047 video_stream_encoder_->WaitUntilTaskQueueIsIdle();
5048 EXPECT_EQ(1, fake_encoder_.GetNumSetRates());
5049 video_stream_encoder_->Stop();
5050}
5051
perkj26091b12016-09-01 01:17:40 -07005052} // namespace webrtc