Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: webrtc/video/vie_encoder_unittest.cc

Issue 2716643002: Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: Comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/video/vie_encoder.cc ('k') | webrtc/video_send_stream.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <limits> 11 #include <limits>
12 #include <utility> 12 #include <utility>
13 13
14 #include "webrtc/api/video/i420_buffer.h" 14 #include "webrtc/api/video/i420_buffer.h"
15 #include "webrtc/base/fakeclock.h"
15 #include "webrtc/base/logging.h" 16 #include "webrtc/base/logging.h"
16 #include "webrtc/media/base/videoadapter.h" 17 #include "webrtc/media/base/videoadapter.h"
17 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" 18 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
18 #include "webrtc/system_wrappers/include/metrics_default.h" 19 #include "webrtc/system_wrappers/include/metrics_default.h"
19 #include "webrtc/system_wrappers/include/sleep.h" 20 #include "webrtc/system_wrappers/include/sleep.h"
20 #include "webrtc/test/encoder_settings.h" 21 #include "webrtc/test/encoder_settings.h"
21 #include "webrtc/test/fake_encoder.h" 22 #include "webrtc/test/fake_encoder.h"
22 #include "webrtc/test/frame_generator.h" 23 #include "webrtc/test/frame_generator.h"
23 #include "webrtc/test/gmock.h" 24 #include "webrtc/test/gmock.h"
24 #include "webrtc/test/gtest.h" 25 #include "webrtc/test/gtest.h"
25 #include "webrtc/video/send_statistics_proxy.h" 26 #include "webrtc/video/send_statistics_proxy.h"
26 #include "webrtc/video/vie_encoder.h" 27 #include "webrtc/video/vie_encoder.h"
27 28
28 namespace { 29 namespace {
29 #if defined(WEBRTC_ANDROID) 30 #if defined(WEBRTC_ANDROID)
30 // TODO(kthelgason): Lower this limit when better testing 31 // TODO(kthelgason): Lower this limit when better testing
31 // on MediaCodec and fallback implementations are in place. 32 // on MediaCodec and fallback implementations are in place.
32 const int kMinPixelsPerFrame = 320 * 180; 33 const int kMinPixelsPerFrame = 320 * 180;
33 #else 34 #else
34 const int kMinPixelsPerFrame = 120 * 90; 35 const int kMinPixelsPerFrame = 120 * 90;
35 #endif 36 #endif
36 } 37 const int kMinFramerateFps = 2;
38 const int64_t kFrameTimeoutMs = 100;
39 } // namespace
37 40
38 namespace webrtc { 41 namespace webrtc {
39 42
40 using DegredationPreference = VideoSendStream::DegradationPreference; 43 using DegredationPreference = VideoSendStream::DegradationPreference;
41 using ScaleReason = AdaptationObserverInterface::AdaptReason; 44 using ScaleReason = AdaptationObserverInterface::AdaptReason;
42 using ::testing::_; 45 using ::testing::_;
43 using ::testing::Return; 46 using ::testing::Return;
44 47
45 namespace { 48 namespace {
46 const size_t kMaxPayloadLength = 1440; 49 const size_t kMaxPayloadLength = 1440;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 bool adaption_enabled() { 139 bool adaption_enabled() {
137 rtc::CritScope cs(&crit_); 140 rtc::CritScope cs(&crit_);
138 return adaptation_enabled_; 141 return adaptation_enabled_;
139 } 142 }
140 143
141 void IncomingCapturedFrame(const VideoFrame& video_frame) override { 144 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
142 int cropped_width = 0; 145 int cropped_width = 0;
143 int cropped_height = 0; 146 int cropped_height = 0;
144 int out_width = 0; 147 int out_width = 0;
145 int out_height = 0; 148 int out_height = 0;
146 if (adaption_enabled() && 149 if (adaption_enabled()) {
147 adapter_.AdaptFrameResolution(video_frame.width(), video_frame.height(), 150 if (adapter_.AdaptFrameResolution(
148 video_frame.timestamp_us() * 1000, 151 video_frame.width(), video_frame.height(),
149 &cropped_width, &cropped_height, 152 video_frame.timestamp_us() * 1000, &cropped_width,
150 &out_width, &out_height)) { 153 &cropped_height, &out_width, &out_height)) {
151 VideoFrame adapted_frame( 154 VideoFrame adapted_frame(new rtc::RefCountedObject<TestBuffer>(
152 new rtc::RefCountedObject<TestBuffer>(nullptr, out_width, out_height), 155 nullptr, out_width, out_height),
153 99, 99, kVideoRotation_0); 156 99, 99, kVideoRotation_0);
154 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); 157 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
155 test::FrameForwarder::IncomingCapturedFrame(adapted_frame); 158 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
159 }
156 } else { 160 } else {
157 test::FrameForwarder::IncomingCapturedFrame(video_frame); 161 test::FrameForwarder::IncomingCapturedFrame(video_frame);
158 } 162 }
159 } 163 }
160 164
161 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, 165 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
162 const rtc::VideoSinkWants& wants) override { 166 const rtc::VideoSinkWants& wants) override {
163 rtc::CritScope cs(&crit_); 167 rtc::CritScope cs(&crit_);
164 adapter_.OnResolutionRequest(wants.target_pixel_count, 168 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
165 wants.max_pixel_count); 169 wants.max_pixel_count,
170 wants.max_framerate_fps);
166 test::FrameForwarder::AddOrUpdateSink(sink, wants); 171 test::FrameForwarder::AddOrUpdateSink(sink, wants);
167 } 172 }
168 173
169 cricket::VideoAdapter adapter_; 174 cricket::VideoAdapter adapter_;
170 bool adaptation_enabled_ GUARDED_BY(crit_); 175 bool adaptation_enabled_ GUARDED_BY(crit_);
171 }; 176 };
177
178 class MockableSendStatisticsProxy : public SendStatisticsProxy {
179 public:
180 MockableSendStatisticsProxy(Clock* clock,
181 const VideoSendStream::Config& config,
182 VideoEncoderConfig::ContentType content_type)
183 : SendStatisticsProxy(clock, config, content_type) {}
184
185 VideoSendStream::Stats GetStats() override {
186 rtc::CritScope cs(&lock_);
187 if (mock_stats_)
188 return *mock_stats_;
189 return SendStatisticsProxy::GetStats();
190 }
191
192 void SetMockStats(const VideoSendStream::Stats& stats) {
193 rtc::CritScope cs(&lock_);
194 mock_stats_.emplace(stats);
195 }
196
197 void ResetMockStats() {
198 rtc::CritScope cs(&lock_);
199 mock_stats_.reset();
200 }
201
202 private:
203 rtc::CriticalSection lock_;
204 rtc::Optional<VideoSendStream::Stats> mock_stats_ GUARDED_BY(lock_);
205 };
206
172 } // namespace 207 } // namespace
173 208
174 class ViEEncoderTest : public ::testing::Test { 209 class ViEEncoderTest : public ::testing::Test {
175 public: 210 public:
176 static const int kDefaultTimeoutMs = 30 * 1000; 211 static const int kDefaultTimeoutMs = 30 * 1000;
177 212
178 ViEEncoderTest() 213 ViEEncoderTest()
179 : video_send_config_(VideoSendStream::Config(nullptr)), 214 : video_send_config_(VideoSendStream::Config(nullptr)),
180 codec_width_(320), 215 codec_width_(320),
181 codec_height_(240), 216 codec_height_(240),
182 fake_encoder_(), 217 fake_encoder_(),
183 stats_proxy_(new SendStatisticsProxy( 218 stats_proxy_(new MockableSendStatisticsProxy(
184 Clock::GetRealTimeClock(), 219 Clock::GetRealTimeClock(),
185 video_send_config_, 220 video_send_config_,
186 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), 221 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
187 sink_(&fake_encoder_) {} 222 sink_(&fake_encoder_) {}
188 223
189 void SetUp() override { 224 void SetUp() override {
190 metrics::Reset(); 225 metrics::Reset();
191 video_send_config_ = VideoSendStream::Config(nullptr); 226 video_send_config_ = VideoSendStream::Config(nullptr);
192 video_send_config_.encoder_settings.encoder = &fake_encoder_; 227 video_send_config_.encoder_settings.encoder = &fake_encoder_;
193 video_send_config_.encoder_settings.payload_name = "FAKE"; 228 video_send_config_.encoder_settings.payload_name = "FAKE";
194 video_send_config_.encoder_settings.payload_type = 125; 229 video_send_config_.encoder_settings.payload_type = 125;
195 230
196 VideoEncoderConfig video_encoder_config; 231 VideoEncoderConfig video_encoder_config;
197 test::FillEncoderConfiguration(1, &video_encoder_config); 232 test::FillEncoderConfiguration(1, &video_encoder_config);
198 video_encoder_config_ = video_encoder_config.Copy(); 233 video_encoder_config_ = video_encoder_config.Copy();
199 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */); 234 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */);
200 } 235 }
201 236
202 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, 237 void ConfigureEncoder(VideoEncoderConfig video_encoder_config,
203 bool nack_enabled) { 238 bool nack_enabled) {
204 if (vie_encoder_) 239 if (vie_encoder_)
205 vie_encoder_->Stop(); 240 vie_encoder_->Stop();
206 vie_encoder_.reset(new ViEEncoderUnderTest( 241 vie_encoder_.reset(new ViEEncoderUnderTest(
207 stats_proxy_.get(), video_send_config_.encoder_settings)); 242 stats_proxy_.get(), video_send_config_.encoder_settings));
208 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); 243 vie_encoder_->SetSink(&sink_, false /* rotation_applied */);
209 vie_encoder_->SetSource(&video_source_, 244 vie_encoder_->SetSource(
210 VideoSendStream::DegradationPreference::kBalanced); 245 &video_source_,
246 VideoSendStream::DegradationPreference::kMaintainFramerate);
211 vie_encoder_->SetStartBitrate(kTargetBitrateBps); 247 vie_encoder_->SetStartBitrate(kTargetBitrateBps);
212 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 248 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config),
213 kMaxPayloadLength, nack_enabled); 249 kMaxPayloadLength, nack_enabled);
214 vie_encoder_->WaitUntilTaskQueueIsIdle(); 250 vie_encoder_->WaitUntilTaskQueueIsIdle();
215 } 251 }
216 252
217 void ResetEncoder(const std::string& payload_name, 253 void ResetEncoder(const std::string& payload_name,
218 size_t num_streams, 254 size_t num_streams,
219 size_t num_temporal_layers, 255 size_t num_temporal_layers,
220 bool nack_enabled) { 256 bool nack_enabled) {
(...skipping 14 matching lines...) Expand all
235 99, 99, kVideoRotation_0); 271 99, 99, kVideoRotation_0);
236 frame.set_ntp_time_ms(ntp_time_ms); 272 frame.set_ntp_time_ms(ntp_time_ms);
237 return frame; 273 return frame;
238 } 274 }
239 275
240 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const { 276 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
241 VideoFrame frame( 277 VideoFrame frame(
242 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99, 278 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99,
243 kVideoRotation_0); 279 kVideoRotation_0);
244 frame.set_ntp_time_ms(ntp_time_ms); 280 frame.set_ntp_time_ms(ntp_time_ms);
281 frame.set_timestamp_us(ntp_time_ms * 1000);
245 return frame; 282 return frame;
246 } 283 }
247 284
248 class TestEncoder : public test::FakeEncoder { 285 class TestEncoder : public test::FakeEncoder {
249 public: 286 public:
250 TestEncoder() 287 TestEncoder()
251 : FakeEncoder(Clock::GetRealTimeClock()), 288 : FakeEncoder(Clock::GetRealTimeClock()),
252 continue_encode_event_(false, false) {} 289 continue_encode_event_(false, false) {}
253 290
254 VideoCodec codec_config() { 291 VideoCodec codec_config() {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 364 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
328 { 365 {
329 rtc::CritScope lock(&crit_); 366 rtc::CritScope lock(&crit_);
330 timestamp = last_timestamp_; 367 timestamp = last_timestamp_;
331 } 368 }
332 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); 369 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
333 } 370 }
334 371
335 void WaitForEncodedFrame(uint32_t expected_width, 372 void WaitForEncodedFrame(uint32_t expected_width,
336 uint32_t expected_height) { 373 uint32_t expected_height) {
374 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
375 CheckLastFrameSizeMathces(expected_width, expected_height);
376 }
377
378 void CheckLastFrameSizeMathces(uint32_t expected_width,
379 uint32_t expected_height) {
337 uint32_t width = 0; 380 uint32_t width = 0;
338 uint32_t height = 0; 381 uint32_t height = 0;
339 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
340 { 382 {
341 rtc::CritScope lock(&crit_); 383 rtc::CritScope lock(&crit_);
342 width = last_width_; 384 width = last_width_;
343 height = last_height_; 385 height = last_height_;
344 } 386 }
345 EXPECT_EQ(expected_height, height); 387 EXPECT_EQ(expected_height, height);
346 EXPECT_EQ(expected_width, width); 388 EXPECT_EQ(expected_width, width);
347 } 389 }
348 390
349 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); } 391 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
350 392
393 bool WaitForFrame(int64_t timeout_ms) {
394 return encoded_frame_event_.Wait(timeout_ms);
395 }
396
351 void SetExpectNoFrames() { 397 void SetExpectNoFrames() {
352 rtc::CritScope lock(&crit_); 398 rtc::CritScope lock(&crit_);
353 expect_frames_ = false; 399 expect_frames_ = false;
354 } 400 }
355 401
356 int number_of_reconfigurations() { 402 int number_of_reconfigurations() {
357 rtc::CritScope lock(&crit_); 403 rtc::CritScope lock(&crit_);
358 return number_of_reconfigurations_; 404 return number_of_reconfigurations_;
359 } 405 }
360 406
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 bool expect_frames_ = true; 439 bool expect_frames_ = true;
394 int number_of_reconfigurations_ = 0; 440 int number_of_reconfigurations_ = 0;
395 int min_transmit_bitrate_bps_ = 0; 441 int min_transmit_bitrate_bps_ = 0;
396 }; 442 };
397 443
398 VideoSendStream::Config video_send_config_; 444 VideoSendStream::Config video_send_config_;
399 VideoEncoderConfig video_encoder_config_; 445 VideoEncoderConfig video_encoder_config_;
400 int codec_width_; 446 int codec_width_;
401 int codec_height_; 447 int codec_height_;
402 TestEncoder fake_encoder_; 448 TestEncoder fake_encoder_;
403 std::unique_ptr<SendStatisticsProxy> stats_proxy_; 449 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
404 TestSink sink_; 450 TestSink sink_;
405 AdaptingFrameForwarder video_source_; 451 AdaptingFrameForwarder video_source_;
406 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_; 452 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_;
407 }; 453 };
408 454
409 TEST_F(ViEEncoderTest, EncodeOneFrame) { 455 TEST_F(ViEEncoderTest, EncodeOneFrame) {
410 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 456 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
411 rtc::Event frame_destroyed_event(false, false); 457 rtc::Event frame_destroyed_event(false, false);
412 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); 458 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
413 sink_.WaitForEncodedFrame(1); 459 sink_.WaitForEncodedFrame(1);
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 657 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
612 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 658 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
613 // Resilience is on for temporal layers. 659 // Resilience is on for temporal layers.
614 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience); 660 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience);
615 vie_encoder_->Stop(); 661 vie_encoder_->Stop();
616 } 662 }
617 663
618 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) { 664 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
619 EXPECT_TRUE(video_source_.has_sinks()); 665 EXPECT_TRUE(video_source_.has_sinks());
620 test::FrameForwarder new_video_source; 666 test::FrameForwarder new_video_source;
621 vie_encoder_->SetSource(&new_video_source, 667 vie_encoder_->SetSource(
622 VideoSendStream::DegradationPreference::kBalanced); 668 &new_video_source,
669 VideoSendStream::DegradationPreference::kMaintainFramerate);
623 EXPECT_FALSE(video_source_.has_sinks()); 670 EXPECT_FALSE(video_source_.has_sinks());
624 EXPECT_TRUE(new_video_source.has_sinks()); 671 EXPECT_TRUE(new_video_source.has_sinks());
625 672
626 vie_encoder_->Stop(); 673 vie_encoder_->Stop();
627 } 674 }
628 675
629 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) { 676 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) {
630 EXPECT_FALSE(video_source_.sink_wants().rotation_applied); 677 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
631 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/); 678 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/);
632 EXPECT_TRUE(video_source_.sink_wants().rotation_applied); 679 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
633 vie_encoder_->Stop(); 680 vie_encoder_->Stop();
634 } 681 }
635 682
636 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) { 683 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) {
637 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 684 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
638 685
639 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 686 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
640 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 687 EXPECT_EQ(std::numeric_limits<int>::max(),
688 video_source_.sink_wants().max_pixel_count);
641 689
642 int frame_width = 1280; 690 int frame_width = 1280;
643 int frame_height = 720; 691 int frame_height = 720;
644 692
645 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should 693 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should
646 // request lower resolution. 694 // request lower resolution.
647 for (int i = 1; i <= ViEEncoder::kMaxCpuDowngrades; ++i) { 695 for (int i = 1; i <= ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
648 video_source_.IncomingCapturedFrame( 696 video_source_.IncomingCapturedFrame(
649 CreateFrame(i, frame_width, frame_height)); 697 CreateFrame(i, frame_width, frame_height));
650 sink_.WaitForEncodedFrame(i); 698 sink_.WaitForEncodedFrame(i);
651 699
652 vie_encoder_->TriggerCpuOveruse(); 700 vie_encoder_->TriggerCpuOveruse();
653 701
654 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 702 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
655 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 703 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
656 std::numeric_limits<int>::max()),
657 frame_width * frame_height); 704 frame_width * frame_height);
658 705
659 frame_width /= 2; 706 frame_width /= 2;
660 frame_height /= 2; 707 frame_height /= 2;
661 } 708 }
662 709
663 // Trigger CPU overuse one more time. This should not trigger a request for 710 // Trigger CPU overuse one more time. This should not trigger a request for
664 // lower resolution. 711 // lower resolution.
665 rtc::VideoSinkWants current_wants = video_source_.sink_wants(); 712 rtc::VideoSinkWants current_wants = video_source_.sink_wants();
666 video_source_.IncomingCapturedFrame(CreateFrame( 713 video_source_.IncomingCapturedFrame(CreateFrame(
667 ViEEncoder::kMaxCpuDowngrades + 1, frame_width, frame_height)); 714 ViEEncoder::kMaxCpuResolutionDowngrades + 1, frame_width, frame_height));
668 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuDowngrades + 1); 715 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuResolutionDowngrades + 1);
669 vie_encoder_->TriggerCpuOveruse(); 716 vie_encoder_->TriggerCpuOveruse();
670 EXPECT_EQ(video_source_.sink_wants().target_pixel_count, 717 EXPECT_EQ(video_source_.sink_wants().target_pixel_count,
671 current_wants.target_pixel_count); 718 current_wants.target_pixel_count);
672 EXPECT_EQ(video_source_.sink_wants().max_pixel_count, 719 EXPECT_EQ(video_source_.sink_wants().max_pixel_count,
673 current_wants.max_pixel_count); 720 current_wants.max_pixel_count);
674 721
675 // Trigger CPU normal use. 722 // Trigger CPU normal use.
676 vie_encoder_->TriggerCpuNormalUsage(); 723 vie_encoder_->TriggerCpuNormalUsage();
677 EXPECT_EQ(frame_width * frame_height * 5 / 3, 724 EXPECT_EQ(frame_width * frame_height * 5 / 3,
678 video_source_.sink_wants().target_pixel_count.value_or(0)); 725 video_source_.sink_wants().target_pixel_count.value_or(0));
679 EXPECT_EQ(frame_width * frame_height * 4, 726 EXPECT_EQ(frame_width * frame_height * 4,
680 video_source_.sink_wants().max_pixel_count.value_or(0)); 727 video_source_.sink_wants().max_pixel_count);
681 728
682 vie_encoder_->Stop(); 729 vie_encoder_->Stop();
683 } 730 }
684 731
685 TEST_F(ViEEncoderTest, 732 TEST_F(ViEEncoderTest, SinkWantsStoredByDegradationPreference) {
686 ResolutionSinkWantsResetOnSetSourceWithDisabledResolutionScaling) {
687 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 733 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
688 734
689 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 735 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
690 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 736 EXPECT_EQ(std::numeric_limits<int>::max(),
737 video_source_.sink_wants().max_pixel_count);
738 EXPECT_EQ(std::numeric_limits<int>::max(),
739 video_source_.sink_wants().max_framerate_fps);
691 740
692 int frame_width = 1280; 741 const int kFrameWidth = 1280;
693 int frame_height = 720; 742 const int kFrameHeight = 720;
743 const int kFrameIntervalMs = 1000 / 30;
744
745 int frame_timestamp = 1;
694 746
695 video_source_.IncomingCapturedFrame( 747 video_source_.IncomingCapturedFrame(
696 CreateFrame(1, frame_width, frame_height)); 748 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
697 sink_.WaitForEncodedFrame(1); 749 sink_.WaitForEncodedFrame(frame_timestamp);
750 frame_timestamp += kFrameIntervalMs;
751
698 // Trigger CPU overuse. 752 // Trigger CPU overuse.
699 vie_encoder_->TriggerCpuOveruse(); 753 vie_encoder_->TriggerCpuOveruse();
754 video_source_.IncomingCapturedFrame(
755 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
756 sink_.WaitForEncodedFrame(frame_timestamp);
757 frame_timestamp += kFrameIntervalMs;
700 758
701 video_source_.IncomingCapturedFrame( 759 // Default degradation preference in maintain-framerate, so will lower max
702 CreateFrame(2, frame_width, frame_height)); 760 // wanted resolution.
703 sink_.WaitForEncodedFrame(2);
704 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 761 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
705 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 762 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
706 std::numeric_limits<int>::max()), 763 kFrameWidth * kFrameHeight);
707 frame_width * frame_height); 764 EXPECT_EQ(std::numeric_limits<int>::max(),
765 video_source_.sink_wants().max_framerate_fps);
708 766
709 // Set new source. 767 // Set new source, switch to maintain-resolution.
710 test::FrameForwarder new_video_source; 768 test::FrameForwarder new_video_source;
711 vie_encoder_->SetSource( 769 vie_encoder_->SetSource(
712 &new_video_source, 770 &new_video_source,
713 VideoSendStream::DegradationPreference::kMaintainResolution); 771 VideoSendStream::DegradationPreference::kMaintainResolution);
714 772
773 // Initially no degradation registered.
715 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 774 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
716 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 775 EXPECT_EQ(std::numeric_limits<int>::max(),
776 new_video_source.sink_wants().max_pixel_count);
777 EXPECT_EQ(std::numeric_limits<int>::max(),
778 new_video_source.sink_wants().max_framerate_fps);
717 779
780 // Force an input frame rate to be available, or the adaptation call won't
781 // know what framerate to adapt form.
782 VideoSendStream::Stats stats = stats_proxy_->GetStats();
783 stats.input_frame_rate = 30;
784 stats_proxy_->SetMockStats(stats);
785
786 vie_encoder_->TriggerCpuOveruse();
718 new_video_source.IncomingCapturedFrame( 787 new_video_source.IncomingCapturedFrame(
719 CreateFrame(3, frame_width, frame_height)); 788 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
720 sink_.WaitForEncodedFrame(3); 789 sink_.WaitForEncodedFrame(frame_timestamp);
790 frame_timestamp += kFrameIntervalMs;
791
792 // Some framerate constraint should be set.
721 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 793 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
722 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 794 EXPECT_EQ(std::numeric_limits<int>::max(),
795 new_video_source.sink_wants().max_pixel_count);
796 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
797
798 // Turn of degradation completely.
799 vie_encoder_->SetSource(
800 &new_video_source,
801 VideoSendStream::DegradationPreference::kDegradationDisabled);
802
803 // Initially no degradation registered.
804 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
805 EXPECT_EQ(std::numeric_limits<int>::max(),
806 new_video_source.sink_wants().max_pixel_count);
807 EXPECT_EQ(std::numeric_limits<int>::max(),
808 new_video_source.sink_wants().max_framerate_fps);
809
810 vie_encoder_->TriggerCpuOveruse();
811 new_video_source.IncomingCapturedFrame(
812 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
813 sink_.WaitForEncodedFrame(frame_timestamp);
814 frame_timestamp += kFrameIntervalMs;
815
816 // Still no degradation.
817 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
818 EXPECT_EQ(std::numeric_limits<int>::max(),
819 new_video_source.sink_wants().max_pixel_count);
820 EXPECT_EQ(std::numeric_limits<int>::max(),
821 new_video_source.sink_wants().max_framerate_fps);
723 822
724 // Calling SetSource with resolution scaling enabled apply the old SinkWants. 823 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
725 vie_encoder_->SetSource(&new_video_source, 824 vie_encoder_->SetSource(
726 VideoSendStream::DegradationPreference::kBalanced); 825 &new_video_source,
727 EXPECT_LT(new_video_source.sink_wants().max_pixel_count.value_or( 826 VideoSendStream::DegradationPreference::kMaintainFramerate);
728 std::numeric_limits<int>::max()), 827 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
729 frame_width * frame_height); 828 kFrameWidth * kFrameHeight);
730 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 829 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
830 EXPECT_EQ(std::numeric_limits<int>::max(),
831 new_video_source.sink_wants().max_framerate_fps);
832
833 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
834 vie_encoder_->SetSource(
835 &new_video_source,
836 VideoSendStream::DegradationPreference::kMaintainResolution);
837 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
838 EXPECT_EQ(std::numeric_limits<int>::max(),
839 new_video_source.sink_wants().max_pixel_count);
840 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
731 841
732 vie_encoder_->Stop(); 842 vie_encoder_->Stop();
733 } 843 }
734 844
735 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) { 845 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) {
736 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 846 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
737 847
738 int frame_width = 1280; 848 int frame_width = 1280;
739 int frame_height = 720; 849 int frame_height = 720;
740 850
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 895
786 video_source_.IncomingCapturedFrame( 896 video_source_.IncomingCapturedFrame(
787 CreateFrame(2, frame_width, frame_height)); 897 CreateFrame(2, frame_width, frame_height));
788 sink_.WaitForEncodedFrame(2); 898 sink_.WaitForEncodedFrame(2);
789 stats = stats_proxy_->GetStats(); 899 stats = stats_proxy_->GetStats();
790 EXPECT_TRUE(stats.cpu_limited_resolution); 900 EXPECT_TRUE(stats.cpu_limited_resolution);
791 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 901 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
792 902
793 // Set new source with adaptation still enabled. 903 // Set new source with adaptation still enabled.
794 test::FrameForwarder new_video_source; 904 test::FrameForwarder new_video_source;
795 vie_encoder_->SetSource(&new_video_source, 905 vie_encoder_->SetSource(
796 VideoSendStream::DegradationPreference::kBalanced); 906 &new_video_source,
907 VideoSendStream::DegradationPreference::kMaintainFramerate);
797 908
798 new_video_source.IncomingCapturedFrame( 909 new_video_source.IncomingCapturedFrame(
799 CreateFrame(3, frame_width, frame_height)); 910 CreateFrame(3, frame_width, frame_height));
800 sink_.WaitForEncodedFrame(3); 911 sink_.WaitForEncodedFrame(3);
801 stats = stats_proxy_->GetStats(); 912 stats = stats_proxy_->GetStats();
802 EXPECT_TRUE(stats.cpu_limited_resolution); 913 EXPECT_TRUE(stats.cpu_limited_resolution);
803 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 914 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
804 915
805 // Set adaptation disabled. 916 // Set adaptation disabled.
806 vie_encoder_->SetSource( 917 vie_encoder_->SetSource(
807 &new_video_source, 918 &new_video_source,
808 VideoSendStream::DegradationPreference::kMaintainResolution); 919 VideoSendStream::DegradationPreference::kDegradationDisabled);
809 920
810 new_video_source.IncomingCapturedFrame( 921 new_video_source.IncomingCapturedFrame(
811 CreateFrame(4, frame_width, frame_height)); 922 CreateFrame(4, frame_width, frame_height));
812 sink_.WaitForEncodedFrame(4); 923 sink_.WaitForEncodedFrame(4);
813 stats = stats_proxy_->GetStats(); 924 stats = stats_proxy_->GetStats();
814 EXPECT_FALSE(stats.cpu_limited_resolution); 925 EXPECT_FALSE(stats.cpu_limited_resolution);
815 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 926 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
816 927
817 // Set adaptation back to enabled. 928 // Set adaptation back to enabled.
818 vie_encoder_->SetSource(&new_video_source, 929 vie_encoder_->SetSource(
819 VideoSendStream::DegradationPreference::kBalanced); 930 &new_video_source,
931 VideoSendStream::DegradationPreference::kMaintainFramerate);
820 932
821 new_video_source.IncomingCapturedFrame( 933 new_video_source.IncomingCapturedFrame(
822 CreateFrame(5, frame_width, frame_height)); 934 CreateFrame(5, frame_width, frame_height));
823 sink_.WaitForEncodedFrame(5); 935 sink_.WaitForEncodedFrame(5);
824 stats = stats_proxy_->GetStats(); 936 stats = stats_proxy_->GetStats();
825 EXPECT_TRUE(stats.cpu_limited_resolution); 937 EXPECT_TRUE(stats.cpu_limited_resolution);
826 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 938 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
827 939
828 vie_encoder_->TriggerCpuNormalUsage(); 940 vie_encoder_->TriggerCpuNormalUsage();
829 941
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 video_source_.IncomingCapturedFrame( 1030 video_source_.IncomingCapturedFrame(
919 CreateFrame(sequence, frame_width, frame_height)); 1031 CreateFrame(sequence, frame_width, frame_height));
920 sink_.WaitForEncodedFrame(sequence++); 1032 sink_.WaitForEncodedFrame(sequence++);
921 1033
922 stats = stats_proxy_->GetStats(); 1034 stats = stats_proxy_->GetStats();
923 EXPECT_TRUE(stats.cpu_limited_resolution); 1035 EXPECT_TRUE(stats.cpu_limited_resolution);
924 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1036 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
925 1037
926 // Set new source with adaptation still enabled. 1038 // Set new source with adaptation still enabled.
927 test::FrameForwarder new_video_source; 1039 test::FrameForwarder new_video_source;
928 vie_encoder_->SetSource(&new_video_source, 1040 vie_encoder_->SetSource(
929 VideoSendStream::DegradationPreference::kBalanced); 1041 &new_video_source,
1042 VideoSendStream::DegradationPreference::kMaintainFramerate);
930 1043
931 new_video_source.IncomingCapturedFrame( 1044 new_video_source.IncomingCapturedFrame(
932 CreateFrame(sequence, frame_width, frame_height)); 1045 CreateFrame(sequence, frame_width, frame_height));
933 sink_.WaitForEncodedFrame(sequence++); 1046 sink_.WaitForEncodedFrame(sequence++);
934 stats = stats_proxy_->GetStats(); 1047 stats = stats_proxy_->GetStats();
935 EXPECT_TRUE(stats.cpu_limited_resolution); 1048 EXPECT_TRUE(stats.cpu_limited_resolution);
936 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1049 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
937 1050
938 // Set adaptation disabled. 1051 // Set cpu adaptation by frame dropping.
939 vie_encoder_->SetSource( 1052 vie_encoder_->SetSource(
940 &new_video_source, 1053 &new_video_source,
941 VideoSendStream::DegradationPreference::kMaintainResolution); 1054 VideoSendStream::DegradationPreference::kMaintainResolution);
942 new_video_source.IncomingCapturedFrame( 1055 new_video_source.IncomingCapturedFrame(
943 CreateFrame(sequence, frame_width, frame_height)); 1056 CreateFrame(sequence, frame_width, frame_height));
944 sink_.WaitForEncodedFrame(sequence++); 1057 sink_.WaitForEncodedFrame(sequence++);
945 stats = stats_proxy_->GetStats(); 1058 stats = stats_proxy_->GetStats();
1059 // Not adapted at first.
946 EXPECT_FALSE(stats.cpu_limited_resolution); 1060 EXPECT_FALSE(stats.cpu_limited_resolution);
947 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1061 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
948 1062
949 // Switch back the source with adaptation enabled. 1063 // Force an input frame rate to be available, or the adaptation call won't
950 vie_encoder_->SetSource(&video_source_, 1064 // know what framerate to adapt form.
951 VideoSendStream::DegradationPreference::kBalanced); 1065 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1066 mock_stats.input_frame_rate = 30;
1067 stats_proxy_->SetMockStats(mock_stats);
1068 vie_encoder_->TriggerCpuOveruse();
1069 stats_proxy_->ResetMockStats();
1070
1071 new_video_source.IncomingCapturedFrame(
1072 CreateFrame(sequence, frame_width, frame_height));
1073 sink_.WaitForEncodedFrame(sequence++);
1074
1075 // Framerate now adapted.
1076 stats = stats_proxy_->GetStats();
1077 EXPECT_TRUE(stats.cpu_limited_resolution);
1078 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1079
1080 // Disable CPU adaptation.
1081 vie_encoder_->SetSource(
1082 &new_video_source,
1083 VideoSendStream::DegradationPreference::kDegradationDisabled);
1084 new_video_source.IncomingCapturedFrame(
1085 CreateFrame(sequence, frame_width, frame_height));
1086 sink_.WaitForEncodedFrame(sequence++);
1087
1088 stats = stats_proxy_->GetStats();
1089 EXPECT_FALSE(stats.cpu_limited_resolution);
1090 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1091
1092 // Try to trigger overuse. Should not succeed.
1093 stats_proxy_->SetMockStats(mock_stats);
1094 vie_encoder_->TriggerCpuOveruse();
1095 stats_proxy_->ResetMockStats();
1096
1097 stats = stats_proxy_->GetStats();
1098 EXPECT_FALSE(stats.cpu_limited_resolution);
1099 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1100
1101 // Switch back the source with resolution adaptation enabled.
1102 vie_encoder_->SetSource(
1103 &video_source_,
1104 VideoSendStream::DegradationPreference::kMaintainFramerate);
952 video_source_.IncomingCapturedFrame( 1105 video_source_.IncomingCapturedFrame(
953 CreateFrame(sequence, frame_width, frame_height)); 1106 CreateFrame(sequence, frame_width, frame_height));
954 sink_.WaitForEncodedFrame(sequence++); 1107 sink_.WaitForEncodedFrame(sequence++);
955 stats = stats_proxy_->GetStats(); 1108 stats = stats_proxy_->GetStats();
956 EXPECT_TRUE(stats.cpu_limited_resolution); 1109 EXPECT_TRUE(stats.cpu_limited_resolution);
957 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1110 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
958 1111
959 // Trigger CPU normal usage. 1112 // Trigger CPU normal usage.
960 vie_encoder_->TriggerCpuNormalUsage(); 1113 vie_encoder_->TriggerCpuNormalUsage();
961 video_source_.IncomingCapturedFrame( 1114 video_source_.IncomingCapturedFrame(
962 CreateFrame(sequence, frame_width, frame_height)); 1115 CreateFrame(sequence, frame_width, frame_height));
963 sink_.WaitForEncodedFrame(sequence++); 1116 sink_.WaitForEncodedFrame(sequence++);
964 stats = stats_proxy_->GetStats(); 1117 stats = stats_proxy_->GetStats();
965 EXPECT_FALSE(stats.cpu_limited_resolution); 1118 EXPECT_FALSE(stats.cpu_limited_resolution);
966 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1119 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1120
1121 // Back to the source with adaptation off, set it back to maintain-resolution.
1122 vie_encoder_->SetSource(
1123 &new_video_source,
1124 VideoSendStream::DegradationPreference::kMaintainResolution);
1125 new_video_source.IncomingCapturedFrame(
1126 CreateFrame(sequence, frame_width, frame_height));
1127 sink_.WaitForEncodedFrame(sequence++);
1128 stats = stats_proxy_->GetStats();
1129 // Disabled, since we previously switched the source too disabled.
1130 EXPECT_FALSE(stats.cpu_limited_resolution);
1131 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1132
1133 // Trigger CPU normal usage.
1134 vie_encoder_->TriggerCpuNormalUsage();
1135 new_video_source.IncomingCapturedFrame(
1136 CreateFrame(sequence, frame_width, frame_height));
1137 sink_.WaitForEncodedFrame(sequence++);
1138 stats = stats_proxy_->GetStats();
1139 EXPECT_FALSE(stats.cpu_limited_resolution);
1140 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
967 1141
968 vie_encoder_->Stop(); 1142 vie_encoder_->Stop();
969 } 1143 }
970 1144
971 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) { 1145 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) {
972 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1146 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
973 1147
974 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720)); 1148 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
975 sink_.WaitForEncodedFrame(1); 1149 sink_.WaitForEncodedFrame(1);
976 1150
977 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1151 VideoSendStream::Stats stats = stats_proxy_->GetStats();
978 EXPECT_EQ(video_encoder_config_.max_bitrate_bps, 1152 EXPECT_EQ(video_encoder_config_.max_bitrate_bps,
979 stats.preferred_media_bitrate_bps); 1153 stats.preferred_media_bitrate_bps);
980 1154
981 vie_encoder_->Stop(); 1155 vie_encoder_->Stop();
982 } 1156 }
983 1157
984 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) { 1158 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) {
985 int frame_width = 1280; 1159 int frame_width = 1280;
986 int frame_height = 720; 1160 int frame_height = 720;
987 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1161 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
988 1162
989 // Expect no scaling to begin with 1163 // Expect no scaling to begin with
990 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 1164 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
991 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 1165 EXPECT_EQ(std::numeric_limits<int>::max(),
1166 video_source_.sink_wants().max_pixel_count);
992 1167
993 video_source_.IncomingCapturedFrame( 1168 video_source_.IncomingCapturedFrame(
994 CreateFrame(1, frame_width, frame_height)); 1169 CreateFrame(1, frame_width, frame_height));
995 sink_.WaitForEncodedFrame(1); 1170 sink_.WaitForEncodedFrame(1);
996 1171
997 // Trigger scale down 1172 // Trigger scale down
998 vie_encoder_->TriggerQualityLow(); 1173 vie_encoder_->TriggerQualityLow();
999 1174
1000 video_source_.IncomingCapturedFrame( 1175 video_source_.IncomingCapturedFrame(
1001 CreateFrame(2, frame_width, frame_height)); 1176 CreateFrame(2, frame_width, frame_height));
1002 sink_.WaitForEncodedFrame(2); 1177 sink_.WaitForEncodedFrame(2);
1003 1178
1004 // Expect a scale down. 1179 // Expect a scale down.
1005 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1180 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
1006 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1181 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1007 frame_width * frame_height); 1182 frame_width * frame_height);
1008 1183
1009 // Set adaptation disabled. 1184 // Set adaptation disabled.
1010 test::FrameForwarder new_video_source; 1185 test::FrameForwarder new_video_source;
1011 vie_encoder_->SetSource( 1186 vie_encoder_->SetSource(
1012 &new_video_source, 1187 &new_video_source,
1013 VideoSendStream::DegradationPreference::kMaintainResolution); 1188 VideoSendStream::DegradationPreference::kMaintainResolution);
1014 1189
1015 // Trigger scale down 1190 // Trigger scale down
1016 vie_encoder_->TriggerQualityLow(); 1191 vie_encoder_->TriggerQualityLow();
1017 new_video_source.IncomingCapturedFrame( 1192 new_video_source.IncomingCapturedFrame(
1018 CreateFrame(3, frame_width, frame_height)); 1193 CreateFrame(3, frame_width, frame_height));
1019 sink_.WaitForEncodedFrame(3); 1194 sink_.WaitForEncodedFrame(3);
1020 1195
1021 // Expect no scaling 1196 // Expect no scaling
1022 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 1197 EXPECT_EQ(std::numeric_limits<int>::max(),
1198 new_video_source.sink_wants().max_pixel_count);
1023 1199
1024 // Trigger scale up 1200 // Trigger scale up
1025 vie_encoder_->TriggerQualityHigh(); 1201 vie_encoder_->TriggerQualityHigh();
1026 new_video_source.IncomingCapturedFrame( 1202 new_video_source.IncomingCapturedFrame(
1027 CreateFrame(4, frame_width, frame_height)); 1203 CreateFrame(4, frame_width, frame_height));
1028 sink_.WaitForEncodedFrame(4); 1204 sink_.WaitForEncodedFrame(4);
1029 1205
1030 // Expect nothing to change, still no scaling 1206 // Expect nothing to change, still no scaling
1031 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 1207 EXPECT_EQ(std::numeric_limits<int>::max(),
1208 new_video_source.sink_wants().max_pixel_count);
1032 1209
1033 vie_encoder_->Stop(); 1210 vie_encoder_->Stop();
1034 } 1211 }
1035 1212
1036 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) { 1213 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) {
1037 int frame_width = 1280; 1214 int frame_width = 1280;
1038 int frame_height = 720; 1215 int frame_height = 720;
1039 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1216 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1040 1217
1041 for (size_t i = 1; i <= 10; i++) { 1218 for (size_t i = 1; i <= 10; i++) {
1042 video_source_.IncomingCapturedFrame( 1219 video_source_.IncomingCapturedFrame(
1043 CreateFrame(i, frame_width, frame_height)); 1220 CreateFrame(i, frame_width, frame_height));
1044 sink_.WaitForEncodedFrame(i); 1221 sink_.WaitForEncodedFrame(i);
1045 // Trigger scale down 1222 // Trigger scale down
1046 vie_encoder_->TriggerQualityLow(); 1223 vie_encoder_->TriggerQualityLow();
1047 EXPECT_GE(*video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame); 1224 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
1048 } 1225 }
1049 1226
1050 vie_encoder_->Stop(); 1227 vie_encoder_->Stop();
1051 } 1228 }
1052 1229
1053 TEST_F(ViEEncoderTest, UMACpuLimitedResolutionInPercent) { 1230 TEST_F(ViEEncoderTest, UMACpuLimitedResolutionInPercent) {
1054 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1231 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1055 1232
1056 int frame_width = 640; 1233 int frame_width = 640;
1057 int frame_height = 360; 1234 int frame_height = 360;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 int frame_width = 640; 1307 int frame_width = 640;
1131 int frame_height = 360; 1308 int frame_height = 360;
1132 1309
1133 video_source_.IncomingCapturedFrame( 1310 video_source_.IncomingCapturedFrame(
1134 CreateFrame(1, frame_width, frame_height)); 1311 CreateFrame(1, frame_width, frame_height));
1135 1312
1136 // Expect to drop this frame, the wait should time out. 1313 // Expect to drop this frame, the wait should time out.
1137 sink_.ExpectDroppedFrame(); 1314 sink_.ExpectDroppedFrame();
1138 1315
1139 // Expect the sink_wants to specify a scaled frame. 1316 // Expect the sink_wants to specify a scaled frame.
1140 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1317 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1141 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1142 1318
1143 int last_pixel_count = *video_source_.sink_wants().max_pixel_count; 1319 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
1144 1320
1145 // Next frame is scaled 1321 // Next frame is scaled
1146 video_source_.IncomingCapturedFrame( 1322 video_source_.IncomingCapturedFrame(
1147 CreateFrame(2, frame_width * 3 / 4, frame_height * 3 / 4)); 1323 CreateFrame(2, frame_width * 3 / 4, frame_height * 3 / 4));
1148 1324
1149 // Expect to drop this frame, the wait should time out. 1325 // Expect to drop this frame, the wait should time out.
1150 sink_.ExpectDroppedFrame(); 1326 sink_.ExpectDroppedFrame();
1151 1327
1152 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, last_pixel_count); 1328 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
1153 1329
1154 vie_encoder_->Stop(); 1330 vie_encoder_->Stop();
1155 } 1331 }
1156 1332
1157 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimited) { 1333 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimited) {
1158 // 1kbps. This can never be achieved. 1334 // 1kbps. This can never be achieved.
1159 vie_encoder_->OnBitrateUpdated(1000, 0, 0); 1335 vie_encoder_->OnBitrateUpdated(1000, 0, 0);
1160 int frame_width = 640; 1336 int frame_width = 640;
1161 int frame_height = 360; 1337 int frame_height = 360;
1162 1338
1163 // We expect the n initial frames to get dropped. 1339 // We expect the n initial frames to get dropped.
1164 int i; 1340 int i;
1165 for (i = 1; i <= kMaxInitialFramedrop; ++i) { 1341 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
1166 video_source_.IncomingCapturedFrame( 1342 video_source_.IncomingCapturedFrame(
1167 CreateFrame(i, frame_width, frame_height)); 1343 CreateFrame(i, frame_width, frame_height));
1168 sink_.ExpectDroppedFrame(); 1344 sink_.ExpectDroppedFrame();
1169 } 1345 }
1170 // The n+1th frame should not be dropped, even though it's size is too large. 1346 // The n+1th frame should not be dropped, even though it's size is too large.
1171 video_source_.IncomingCapturedFrame( 1347 video_source_.IncomingCapturedFrame(
1172 CreateFrame(i, frame_width, frame_height)); 1348 CreateFrame(i, frame_width, frame_height));
1173 sink_.WaitForEncodedFrame(i); 1349 sink_.WaitForEncodedFrame(i);
1174 1350
1175 // Expect the sink_wants to specify a scaled frame. 1351 // Expect the sink_wants to specify a scaled frame.
1176 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1352 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1177 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1178 1353
1179 vie_encoder_->Stop(); 1354 vie_encoder_->Stop();
1180 } 1355 }
1181 1356
1182 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) { 1357 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) {
1183 int frame_width = 640; 1358 int frame_width = 640;
1184 int frame_height = 360; 1359 int frame_height = 360;
1185 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); 1360 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0);
1186 1361
1187 // Set degradation preference. 1362 // Set degradation preference.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 video_source_.IncomingCapturedFrame( 1403 video_source_.IncomingCapturedFrame(
1229 CreateFrame(1, kFrameWidth, kFrameHeight)); 1404 CreateFrame(1, kFrameWidth, kFrameHeight));
1230 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1405 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1231 1406
1232 // Trigger CPU overuse, downscale by 3/4. 1407 // Trigger CPU overuse, downscale by 3/4.
1233 vie_encoder_->TriggerCpuOveruse(); 1408 vie_encoder_->TriggerCpuOveruse();
1234 video_source_.IncomingCapturedFrame( 1409 video_source_.IncomingCapturedFrame(
1235 CreateFrame(2, kFrameWidth, kFrameHeight)); 1410 CreateFrame(2, kFrameWidth, kFrameHeight));
1236 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); 1411 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
1237 1412
1238 // Trigger CPU normal use, return to original resoluton; 1413 // Trigger CPU normal use, return to original resolution;
1239 vie_encoder_->TriggerCpuNormalUsage(); 1414 vie_encoder_->TriggerCpuNormalUsage();
1240 video_source_.IncomingCapturedFrame( 1415 video_source_.IncomingCapturedFrame(
1241 CreateFrame(3, kFrameWidth, kFrameHeight)); 1416 CreateFrame(3, kFrameWidth, kFrameHeight));
1242 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1417 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1243 1418
1244 vie_encoder_->Stop(); 1419 vie_encoder_->Stop();
1245 } 1420 }
1421
1422 TEST_F(ViEEncoderTest, AdaptsFrameOnOveruseWithMaintainResolution) {
1423 const int kDefaultFramerateFps = 30;
1424 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerateFps;
1425 const int kFrameWidth = 1280;
1426 const int kFrameHeight = 720;
1427 rtc::ScopedFakeClock fake_clock;
1428
1429 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1430 vie_encoder_->SetSource(
1431 &video_source_,
1432 VideoSendStream::DegradationPreference::kMaintainResolution);
1433 video_source_.set_adaptation_enabled(true);
1434
1435 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1436 int64_t timestamp_ms = kFrameIntervalMs;
1437
1438 video_source_.IncomingCapturedFrame(
1439 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1440 sink_.WaitForEncodedFrame(timestamp_ms);
1441
1442 // Try to trigger overuse. No fps estimate available => no effect.
1443 vie_encoder_->TriggerCpuOveruse();
1444
1445 // Insert frames for one second to get a stable estimate.
1446 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1447 timestamp_ms += kFrameIntervalMs;
1448 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1449 video_source_.IncomingCapturedFrame(
1450 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1451 sink_.WaitForEncodedFrame(timestamp_ms);
1452 }
1453
1454 // Trigger CPU overuse, reduce framerate by 2/3.
1455 vie_encoder_->TriggerCpuOveruse();
1456 int num_frames_dropped = 0;
1457 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1458 timestamp_ms += kFrameIntervalMs;
1459 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1460 video_source_.IncomingCapturedFrame(
1461 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1462 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1463 ++num_frames_dropped;
1464 } else {
1465 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1466 }
1467 }
1468
1469 // TODO(sprang): Find where there's rounding errors or stuff causing the
1470 // margin here to be a little larger than we'd like (input fps estimate is
1471 // off) and the frame dropping is a little too aggressive.
1472 const int kErrorMargin = 5;
1473 EXPECT_NEAR(num_frames_dropped,
1474 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1475 kErrorMargin);
1476
1477 // Trigger CPU overuse, reduce framerate by 2/3 again.
1478 vie_encoder_->TriggerCpuOveruse();
1479 num_frames_dropped = 0;
1480 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1481 timestamp_ms += kFrameIntervalMs;
1482 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1483 video_source_.IncomingCapturedFrame(
1484 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1485 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1486 ++num_frames_dropped;
1487 } else {
1488 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1489 }
1490 }
1491 EXPECT_NEAR(num_frames_dropped,
1492 kDefaultFramerateFps - (kDefaultFramerateFps * 4 / 9),
1493 kErrorMargin);
1494
1495 // Go back up one step.
1496 vie_encoder_->TriggerCpuNormalUsage();
1497 num_frames_dropped = 0;
1498 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1499 timestamp_ms += kFrameIntervalMs;
1500 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1501 video_source_.IncomingCapturedFrame(
1502 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1503 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1504 ++num_frames_dropped;
1505 } else {
1506 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1507 }
1508 }
1509 EXPECT_NEAR(num_frames_dropped,
1510 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1511 kErrorMargin);
1512
1513 // Go back up to original mode.
1514 vie_encoder_->TriggerCpuNormalUsage();
1515 num_frames_dropped = 0;
1516 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1517 timestamp_ms += kFrameIntervalMs;
1518 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1519 video_source_.IncomingCapturedFrame(
1520 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1521 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1522 ++num_frames_dropped;
1523 } else {
1524 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1525 }
1526 }
1527 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
1528
1529 vie_encoder_->Stop();
1530 }
1531
1532 TEST_F(ViEEncoderTest, DoesntAdaptDownPastMinFramerate) {
1533 const int kFramerateFps = 5;
1534 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
1535 const int kMinFpsFrameInterval = rtc::kNumMillisecsPerSec / kMinFramerateFps;
1536 const int kFrameWidth = 1280;
1537 const int kFrameHeight = 720;
1538
1539 rtc::ScopedFakeClock fake_clock;
1540 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1541 vie_encoder_->SetSource(
1542 &video_source_,
1543 VideoSendStream::DegradationPreference::kMaintainResolution);
1544 video_source_.set_adaptation_enabled(true);
1545
1546 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1547 int64_t timestamp_ms = kFrameIntervalMs;
1548
1549 // Trigger overuse as much as we can.
1550 for (int i = 0; i < ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
1551 // Insert frames to get a new fps estimate...
1552 for (int j = 0; j < kFramerateFps; ++j) {
1553 video_source_.IncomingCapturedFrame(
1554 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1555 timestamp_ms += kFrameIntervalMs;
1556 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1557 }
1558 // ...and then try to adapt again.
1559 vie_encoder_->TriggerCpuOveruse();
1560 }
1561
1562 // Drain any frame in the pipeline.
1563 sink_.WaitForFrame(kDefaultTimeoutMs);
1564
1565 // Insert frames at min fps, all should go through.
1566 for (int i = 0; i < 10; ++i) {
1567 timestamp_ms += kMinFpsFrameInterval;
1568 fake_clock.AdvanceTimeMicros(kMinFpsFrameInterval * 1000);
1569 video_source_.IncomingCapturedFrame(
1570 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1571 sink_.WaitForEncodedFrame(timestamp_ms);
1572 }
1573
1574 vie_encoder_->Stop();
1575 }
1246 } // namespace webrtc 1576 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/vie_encoder.cc ('k') | webrtc/video_send_stream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698