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

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

Issue 2781433002: Reland of Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: Rebase error, initial scaling fix Created 3 years, 8 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 <algorithm> 11 #include <algorithm>
12 #include <limits> 12 #include <limits>
13 #include <utility> 13 #include <utility>
14 14
15 #include "webrtc/api/video/i420_buffer.h" 15 #include "webrtc/api/video/i420_buffer.h"
16 #include "webrtc/base/fakeclock.h"
16 #include "webrtc/base/logging.h" 17 #include "webrtc/base/logging.h"
17 #include "webrtc/media/base/videoadapter.h" 18 #include "webrtc/media/base/videoadapter.h"
18 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" 19 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
19 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" 20 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
20 #include "webrtc/system_wrappers/include/metrics_default.h" 21 #include "webrtc/system_wrappers/include/metrics_default.h"
21 #include "webrtc/system_wrappers/include/sleep.h" 22 #include "webrtc/system_wrappers/include/sleep.h"
22 #include "webrtc/test/encoder_settings.h" 23 #include "webrtc/test/encoder_settings.h"
23 #include "webrtc/test/fake_encoder.h" 24 #include "webrtc/test/fake_encoder.h"
24 #include "webrtc/test/frame_generator.h" 25 #include "webrtc/test/frame_generator.h"
25 #include "webrtc/test/gmock.h" 26 #include "webrtc/test/gmock.h"
26 #include "webrtc/test/gtest.h" 27 #include "webrtc/test/gtest.h"
27 #include "webrtc/video/send_statistics_proxy.h" 28 #include "webrtc/video/send_statistics_proxy.h"
28 #include "webrtc/video/vie_encoder.h" 29 #include "webrtc/video/vie_encoder.h"
29 30
30 namespace { 31 namespace {
31 #if defined(WEBRTC_ANDROID) 32 #if defined(WEBRTC_ANDROID)
32 // TODO(kthelgason): Lower this limit when better testing 33 // TODO(kthelgason): Lower this limit when better testing
33 // on MediaCodec and fallback implementations are in place. 34 // on MediaCodec and fallback implementations are in place.
34 const int kMinPixelsPerFrame = 320 * 180; 35 const int kMinPixelsPerFrame = 320 * 180;
35 #else 36 #else
36 const int kMinPixelsPerFrame = 120 * 90; 37 const int kMinPixelsPerFrame = 120 * 90;
37 #endif 38 #endif
38 } 39 const int kMinFramerateFps = 2;
40 const int64_t kFrameTimeoutMs = 100;
41 } // namespace
39 42
40 namespace webrtc { 43 namespace webrtc {
41 44
42 using DegredationPreference = VideoSendStream::DegradationPreference; 45 using DegredationPreference = VideoSendStream::DegradationPreference;
43 using ScaleReason = AdaptationObserverInterface::AdaptReason; 46 using ScaleReason = AdaptationObserverInterface::AdaptReason;
44 using ::testing::_; 47 using ::testing::_;
45 using ::testing::Return; 48 using ::testing::Return;
46 49
47 namespace { 50 namespace {
48 const size_t kMaxPayloadLength = 1440; 51 const size_t kMaxPayloadLength = 1440;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 bool adaption_enabled() { 141 bool adaption_enabled() {
139 rtc::CritScope cs(&crit_); 142 rtc::CritScope cs(&crit_);
140 return adaptation_enabled_; 143 return adaptation_enabled_;
141 } 144 }
142 145
143 void IncomingCapturedFrame(const VideoFrame& video_frame) override { 146 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
144 int cropped_width = 0; 147 int cropped_width = 0;
145 int cropped_height = 0; 148 int cropped_height = 0;
146 int out_width = 0; 149 int out_width = 0;
147 int out_height = 0; 150 int out_height = 0;
148 if (adaption_enabled() && 151 if (adaption_enabled()) {
149 adapter_.AdaptFrameResolution(video_frame.width(), video_frame.height(), 152 if (adapter_.AdaptFrameResolution(
150 video_frame.timestamp_us() * 1000, 153 video_frame.width(), video_frame.height(),
151 &cropped_width, &cropped_height, 154 video_frame.timestamp_us() * 1000, &cropped_width,
152 &out_width, &out_height)) { 155 &cropped_height, &out_width, &out_height)) {
153 VideoFrame adapted_frame( 156 VideoFrame adapted_frame(new rtc::RefCountedObject<TestBuffer>(
154 new rtc::RefCountedObject<TestBuffer>(nullptr, out_width, out_height), 157 nullptr, out_width, out_height),
155 99, 99, kVideoRotation_0); 158 99, 99, kVideoRotation_0);
156 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); 159 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
157 test::FrameForwarder::IncomingCapturedFrame(adapted_frame); 160 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
161 }
158 } else { 162 } else {
159 test::FrameForwarder::IncomingCapturedFrame(video_frame); 163 test::FrameForwarder::IncomingCapturedFrame(video_frame);
160 } 164 }
161 } 165 }
162 166
163 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, 167 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
164 const rtc::VideoSinkWants& wants) override { 168 const rtc::VideoSinkWants& wants) override {
165 rtc::CritScope cs(&crit_); 169 rtc::CritScope cs(&crit_);
166 adapter_.OnResolutionRequest(wants.target_pixel_count, 170 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
167 wants.max_pixel_count); 171 wants.max_pixel_count,
172 wants.max_framerate_fps);
168 test::FrameForwarder::AddOrUpdateSink(sink, wants); 173 test::FrameForwarder::AddOrUpdateSink(sink, wants);
169 } 174 }
170 175
171 cricket::VideoAdapter adapter_; 176 cricket::VideoAdapter adapter_;
172 bool adaptation_enabled_ GUARDED_BY(crit_); 177 bool adaptation_enabled_ GUARDED_BY(crit_);
173 }; 178 };
179
180 class MockableSendStatisticsProxy : public SendStatisticsProxy {
181 public:
182 MockableSendStatisticsProxy(Clock* clock,
183 const VideoSendStream::Config& config,
184 VideoEncoderConfig::ContentType content_type)
185 : SendStatisticsProxy(clock, config, content_type) {}
186
187 VideoSendStream::Stats GetStats() override {
188 rtc::CritScope cs(&lock_);
189 if (mock_stats_)
190 return *mock_stats_;
191 return SendStatisticsProxy::GetStats();
192 }
193
194 void SetMockStats(const VideoSendStream::Stats& stats) {
195 rtc::CritScope cs(&lock_);
196 mock_stats_.emplace(stats);
197 }
198
199 void ResetMockStats() {
200 rtc::CritScope cs(&lock_);
201 mock_stats_.reset();
202 }
203
204 private:
205 rtc::CriticalSection lock_;
206 rtc::Optional<VideoSendStream::Stats> mock_stats_ GUARDED_BY(lock_);
207 };
208
174 } // namespace 209 } // namespace
175 210
176 class ViEEncoderTest : public ::testing::Test { 211 class ViEEncoderTest : public ::testing::Test {
177 public: 212 public:
178 static const int kDefaultTimeoutMs = 30 * 1000; 213 static const int kDefaultTimeoutMs = 30 * 1000;
179 214
180 ViEEncoderTest() 215 ViEEncoderTest()
181 : video_send_config_(VideoSendStream::Config(nullptr)), 216 : video_send_config_(VideoSendStream::Config(nullptr)),
182 codec_width_(320), 217 codec_width_(320),
183 codec_height_(240), 218 codec_height_(240),
184 fake_encoder_(), 219 fake_encoder_(),
185 stats_proxy_(new SendStatisticsProxy( 220 stats_proxy_(new MockableSendStatisticsProxy(
186 Clock::GetRealTimeClock(), 221 Clock::GetRealTimeClock(),
187 video_send_config_, 222 video_send_config_,
188 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), 223 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
189 sink_(&fake_encoder_) {} 224 sink_(&fake_encoder_) {}
190 225
191 void SetUp() override { 226 void SetUp() override {
192 metrics::Reset(); 227 metrics::Reset();
193 video_send_config_ = VideoSendStream::Config(nullptr); 228 video_send_config_ = VideoSendStream::Config(nullptr);
194 video_send_config_.encoder_settings.encoder = &fake_encoder_; 229 video_send_config_.encoder_settings.encoder = &fake_encoder_;
195 video_send_config_.encoder_settings.payload_name = "FAKE"; 230 video_send_config_.encoder_settings.payload_name = "FAKE";
196 video_send_config_.encoder_settings.payload_type = 125; 231 video_send_config_.encoder_settings.payload_type = 125;
197 232
198 VideoEncoderConfig video_encoder_config; 233 VideoEncoderConfig video_encoder_config;
199 test::FillEncoderConfiguration(1, &video_encoder_config); 234 test::FillEncoderConfiguration(1, &video_encoder_config);
200 video_encoder_config_ = video_encoder_config.Copy(); 235 video_encoder_config_ = video_encoder_config.Copy();
201 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */); 236 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */);
202 } 237 }
203 238
204 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, 239 void ConfigureEncoder(VideoEncoderConfig video_encoder_config,
205 bool nack_enabled) { 240 bool nack_enabled) {
206 if (vie_encoder_) 241 if (vie_encoder_)
207 vie_encoder_->Stop(); 242 vie_encoder_->Stop();
208 vie_encoder_.reset(new ViEEncoderUnderTest( 243 vie_encoder_.reset(new ViEEncoderUnderTest(
209 stats_proxy_.get(), video_send_config_.encoder_settings)); 244 stats_proxy_.get(), video_send_config_.encoder_settings));
210 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); 245 vie_encoder_->SetSink(&sink_, false /* rotation_applied */);
211 vie_encoder_->SetSource(&video_source_, 246 vie_encoder_->SetSource(
212 VideoSendStream::DegradationPreference::kBalanced); 247 &video_source_,
248 VideoSendStream::DegradationPreference::kMaintainFramerate);
213 vie_encoder_->SetStartBitrate(kTargetBitrateBps); 249 vie_encoder_->SetStartBitrate(kTargetBitrateBps);
214 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 250 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config),
215 kMaxPayloadLength, nack_enabled); 251 kMaxPayloadLength, nack_enabled);
216 vie_encoder_->WaitUntilTaskQueueIsIdle(); 252 vie_encoder_->WaitUntilTaskQueueIsIdle();
217 } 253 }
218 254
219 void ResetEncoder(const std::string& payload_name, 255 void ResetEncoder(const std::string& payload_name,
220 size_t num_streams, 256 size_t num_streams,
221 size_t num_temporal_layers, 257 size_t num_temporal_layers,
222 bool nack_enabled) { 258 bool nack_enabled) {
(...skipping 14 matching lines...) Expand all
237 99, 99, kVideoRotation_0); 273 99, 99, kVideoRotation_0);
238 frame.set_ntp_time_ms(ntp_time_ms); 274 frame.set_ntp_time_ms(ntp_time_ms);
239 return frame; 275 return frame;
240 } 276 }
241 277
242 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const { 278 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
243 VideoFrame frame( 279 VideoFrame frame(
244 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99, 280 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99,
245 kVideoRotation_0); 281 kVideoRotation_0);
246 frame.set_ntp_time_ms(ntp_time_ms); 282 frame.set_ntp_time_ms(ntp_time_ms);
283 frame.set_timestamp_us(ntp_time_ms * 1000);
247 return frame; 284 return frame;
248 } 285 }
249 286
250 class TestEncoder : public test::FakeEncoder { 287 class TestEncoder : public test::FakeEncoder {
251 public: 288 public:
252 TestEncoder() 289 TestEncoder()
253 : FakeEncoder(Clock::GetRealTimeClock()), 290 : FakeEncoder(Clock::GetRealTimeClock()),
254 continue_encode_event_(false, false) {} 291 continue_encode_event_(false, false) {}
255 292
256 VideoCodec codec_config() { 293 VideoCodec codec_config() {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 396 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
360 { 397 {
361 rtc::CritScope lock(&crit_); 398 rtc::CritScope lock(&crit_);
362 timestamp = last_timestamp_; 399 timestamp = last_timestamp_;
363 } 400 }
364 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); 401 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
365 } 402 }
366 403
367 void WaitForEncodedFrame(uint32_t expected_width, 404 void WaitForEncodedFrame(uint32_t expected_width,
368 uint32_t expected_height) { 405 uint32_t expected_height) {
406 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
407 CheckLastFrameSizeMathces(expected_width, expected_height);
408 }
409
410 void CheckLastFrameSizeMathces(uint32_t expected_width,
411 uint32_t expected_height) {
369 uint32_t width = 0; 412 uint32_t width = 0;
370 uint32_t height = 0; 413 uint32_t height = 0;
371 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
372 { 414 {
373 rtc::CritScope lock(&crit_); 415 rtc::CritScope lock(&crit_);
374 width = last_width_; 416 width = last_width_;
375 height = last_height_; 417 height = last_height_;
376 } 418 }
377 EXPECT_EQ(expected_height, height); 419 EXPECT_EQ(expected_height, height);
378 EXPECT_EQ(expected_width, width); 420 EXPECT_EQ(expected_width, width);
379 } 421 }
380 422
381 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); } 423 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); }
382 424
425 bool WaitForFrame(int64_t timeout_ms) {
426 return encoded_frame_event_.Wait(timeout_ms);
427 }
428
383 void SetExpectNoFrames() { 429 void SetExpectNoFrames() {
384 rtc::CritScope lock(&crit_); 430 rtc::CritScope lock(&crit_);
385 expect_frames_ = false; 431 expect_frames_ = false;
386 } 432 }
387 433
388 int number_of_reconfigurations() { 434 int number_of_reconfigurations() {
389 rtc::CritScope lock(&crit_); 435 rtc::CritScope lock(&crit_);
390 return number_of_reconfigurations_; 436 return number_of_reconfigurations_;
391 } 437 }
392 438
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 bool expect_frames_ = true; 471 bool expect_frames_ = true;
426 int number_of_reconfigurations_ = 0; 472 int number_of_reconfigurations_ = 0;
427 int min_transmit_bitrate_bps_ = 0; 473 int min_transmit_bitrate_bps_ = 0;
428 }; 474 };
429 475
430 VideoSendStream::Config video_send_config_; 476 VideoSendStream::Config video_send_config_;
431 VideoEncoderConfig video_encoder_config_; 477 VideoEncoderConfig video_encoder_config_;
432 int codec_width_; 478 int codec_width_;
433 int codec_height_; 479 int codec_height_;
434 TestEncoder fake_encoder_; 480 TestEncoder fake_encoder_;
435 std::unique_ptr<SendStatisticsProxy> stats_proxy_; 481 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
436 TestSink sink_; 482 TestSink sink_;
437 AdaptingFrameForwarder video_source_; 483 AdaptingFrameForwarder video_source_;
438 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_; 484 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_;
439 }; 485 };
440 486
441 TEST_F(ViEEncoderTest, EncodeOneFrame) { 487 TEST_F(ViEEncoderTest, EncodeOneFrame) {
442 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 488 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
443 rtc::Event frame_destroyed_event(false, false); 489 rtc::Event frame_destroyed_event(false, false);
444 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); 490 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
445 sink_.WaitForEncodedFrame(1); 491 sink_.WaitForEncodedFrame(1);
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 689 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
644 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 690 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
645 // Resilience is on for temporal layers. 691 // Resilience is on for temporal layers.
646 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience); 692 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience);
647 vie_encoder_->Stop(); 693 vie_encoder_->Stop();
648 } 694 }
649 695
650 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) { 696 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
651 EXPECT_TRUE(video_source_.has_sinks()); 697 EXPECT_TRUE(video_source_.has_sinks());
652 test::FrameForwarder new_video_source; 698 test::FrameForwarder new_video_source;
653 vie_encoder_->SetSource(&new_video_source, 699 vie_encoder_->SetSource(
654 VideoSendStream::DegradationPreference::kBalanced); 700 &new_video_source,
701 VideoSendStream::DegradationPreference::kMaintainFramerate);
655 EXPECT_FALSE(video_source_.has_sinks()); 702 EXPECT_FALSE(video_source_.has_sinks());
656 EXPECT_TRUE(new_video_source.has_sinks()); 703 EXPECT_TRUE(new_video_source.has_sinks());
657 704
658 vie_encoder_->Stop(); 705 vie_encoder_->Stop();
659 } 706 }
660 707
661 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) { 708 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) {
662 EXPECT_FALSE(video_source_.sink_wants().rotation_applied); 709 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
663 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/); 710 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/);
664 EXPECT_TRUE(video_source_.sink_wants().rotation_applied); 711 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
665 vie_encoder_->Stop(); 712 vie_encoder_->Stop();
666 } 713 }
667 714
668 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) { 715 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) {
669 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 716 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
670 717
671 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 718 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
672 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 719 EXPECT_EQ(std::numeric_limits<int>::max(),
720 video_source_.sink_wants().max_pixel_count);
673 721
674 int frame_width = 1280; 722 int frame_width = 1280;
675 int frame_height = 720; 723 int frame_height = 720;
676 724
677 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should 725 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should
678 // request lower resolution. 726 // request lower resolution.
679 for (int i = 1; i <= ViEEncoder::kMaxCpuDowngrades; ++i) { 727 for (int i = 1; i <= ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
680 video_source_.IncomingCapturedFrame( 728 video_source_.IncomingCapturedFrame(
681 CreateFrame(i, frame_width, frame_height)); 729 CreateFrame(i, frame_width, frame_height));
682 sink_.WaitForEncodedFrame(i); 730 sink_.WaitForEncodedFrame(i);
683 731
684 vie_encoder_->TriggerCpuOveruse(); 732 vie_encoder_->TriggerCpuOveruse();
685 733
686 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 734 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
687 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 735 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
688 std::numeric_limits<int>::max()),
689 frame_width * frame_height); 736 frame_width * frame_height);
690 737
691 frame_width /= 2; 738 frame_width /= 2;
692 frame_height /= 2; 739 frame_height /= 2;
693 } 740 }
694 741
695 // Trigger CPU overuse one more time. This should not trigger a request for 742 // Trigger CPU overuse one more time. This should not trigger a request for
696 // lower resolution. 743 // lower resolution.
697 rtc::VideoSinkWants current_wants = video_source_.sink_wants(); 744 rtc::VideoSinkWants current_wants = video_source_.sink_wants();
698 video_source_.IncomingCapturedFrame(CreateFrame( 745 video_source_.IncomingCapturedFrame(CreateFrame(
699 ViEEncoder::kMaxCpuDowngrades + 1, frame_width, frame_height)); 746 ViEEncoder::kMaxCpuResolutionDowngrades + 1, frame_width, frame_height));
700 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuDowngrades + 1); 747 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuResolutionDowngrades + 1);
701 vie_encoder_->TriggerCpuOveruse(); 748 vie_encoder_->TriggerCpuOveruse();
702 EXPECT_EQ(video_source_.sink_wants().target_pixel_count, 749 EXPECT_EQ(video_source_.sink_wants().target_pixel_count,
703 current_wants.target_pixel_count); 750 current_wants.target_pixel_count);
704 EXPECT_EQ(video_source_.sink_wants().max_pixel_count, 751 EXPECT_EQ(video_source_.sink_wants().max_pixel_count,
705 current_wants.max_pixel_count); 752 current_wants.max_pixel_count);
706 753
707 // Trigger CPU normal use. 754 // Trigger CPU normal use.
708 vie_encoder_->TriggerCpuNormalUsage(); 755 vie_encoder_->TriggerCpuNormalUsage();
709 EXPECT_EQ(frame_width * frame_height * 5 / 3, 756 EXPECT_EQ(frame_width * frame_height * 5 / 3,
710 video_source_.sink_wants().target_pixel_count.value_or(0)); 757 video_source_.sink_wants().target_pixel_count.value_or(0));
711 EXPECT_EQ(frame_width * frame_height * 4, 758 EXPECT_EQ(frame_width * frame_height * 4,
712 video_source_.sink_wants().max_pixel_count.value_or(0)); 759 video_source_.sink_wants().max_pixel_count);
713 760
714 vie_encoder_->Stop(); 761 vie_encoder_->Stop();
715 } 762 }
716 763
717 TEST_F(ViEEncoderTest, 764 TEST_F(ViEEncoderTest, SinkWantsStoredByDegradationPreference) {
718 ResolutionSinkWantsResetOnSetSourceWithDisabledResolutionScaling) {
719 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 765 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
720 766
721 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 767 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
722 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 768 EXPECT_EQ(std::numeric_limits<int>::max(),
769 video_source_.sink_wants().max_pixel_count);
770 EXPECT_EQ(std::numeric_limits<int>::max(),
771 video_source_.sink_wants().max_framerate_fps);
723 772
724 int frame_width = 1280; 773 const int kFrameWidth = 1280;
725 int frame_height = 720; 774 const int kFrameHeight = 720;
775 const int kFrameIntervalMs = 1000 / 30;
776
777 int frame_timestamp = 1;
726 778
727 video_source_.IncomingCapturedFrame( 779 video_source_.IncomingCapturedFrame(
728 CreateFrame(1, frame_width, frame_height)); 780 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
729 sink_.WaitForEncodedFrame(1); 781 sink_.WaitForEncodedFrame(frame_timestamp);
782 frame_timestamp += kFrameIntervalMs;
783
730 // Trigger CPU overuse. 784 // Trigger CPU overuse.
731 vie_encoder_->TriggerCpuOveruse(); 785 vie_encoder_->TriggerCpuOveruse();
786 video_source_.IncomingCapturedFrame(
787 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
788 sink_.WaitForEncodedFrame(frame_timestamp);
789 frame_timestamp += kFrameIntervalMs;
732 790
733 video_source_.IncomingCapturedFrame( 791 // Default degradation preference in maintain-framerate, so will lower max
734 CreateFrame(2, frame_width, frame_height)); 792 // wanted resolution.
735 sink_.WaitForEncodedFrame(2);
736 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 793 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
737 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 794 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
738 std::numeric_limits<int>::max()), 795 kFrameWidth * kFrameHeight);
739 frame_width * frame_height); 796 EXPECT_EQ(std::numeric_limits<int>::max(),
797 video_source_.sink_wants().max_framerate_fps);
740 798
741 // Set new source. 799 // Set new source, switch to maintain-resolution.
742 test::FrameForwarder new_video_source; 800 test::FrameForwarder new_video_source;
743 vie_encoder_->SetSource( 801 vie_encoder_->SetSource(
744 &new_video_source, 802 &new_video_source,
745 VideoSendStream::DegradationPreference::kMaintainResolution); 803 VideoSendStream::DegradationPreference::kMaintainResolution);
746 804
805 // Initially no degradation registered.
747 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 806 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
748 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 807 EXPECT_EQ(std::numeric_limits<int>::max(),
808 new_video_source.sink_wants().max_pixel_count);
809 EXPECT_EQ(std::numeric_limits<int>::max(),
810 new_video_source.sink_wants().max_framerate_fps);
749 811
812 // Force an input frame rate to be available, or the adaptation call won't
813 // know what framerate to adapt form.
814 VideoSendStream::Stats stats = stats_proxy_->GetStats();
815 stats.input_frame_rate = 30;
816 stats_proxy_->SetMockStats(stats);
817
818 vie_encoder_->TriggerCpuOveruse();
750 new_video_source.IncomingCapturedFrame( 819 new_video_source.IncomingCapturedFrame(
751 CreateFrame(3, frame_width, frame_height)); 820 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
752 sink_.WaitForEncodedFrame(3); 821 sink_.WaitForEncodedFrame(frame_timestamp);
822 frame_timestamp += kFrameIntervalMs;
823
824 // Some framerate constraint should be set.
753 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 825 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
754 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 826 EXPECT_EQ(std::numeric_limits<int>::max(),
827 new_video_source.sink_wants().max_pixel_count);
828 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
829
830 // Turn of degradation completely.
831 vie_encoder_->SetSource(
832 &new_video_source,
833 VideoSendStream::DegradationPreference::kDegradationDisabled);
834
835 // Initially no degradation registered.
836 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
837 EXPECT_EQ(std::numeric_limits<int>::max(),
838 new_video_source.sink_wants().max_pixel_count);
839 EXPECT_EQ(std::numeric_limits<int>::max(),
840 new_video_source.sink_wants().max_framerate_fps);
841
842 vie_encoder_->TriggerCpuOveruse();
843 new_video_source.IncomingCapturedFrame(
844 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
845 sink_.WaitForEncodedFrame(frame_timestamp);
846 frame_timestamp += kFrameIntervalMs;
847
848 // Still no degradation.
849 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
850 EXPECT_EQ(std::numeric_limits<int>::max(),
851 new_video_source.sink_wants().max_pixel_count);
852 EXPECT_EQ(std::numeric_limits<int>::max(),
853 new_video_source.sink_wants().max_framerate_fps);
755 854
756 // Calling SetSource with resolution scaling enabled apply the old SinkWants. 855 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
757 vie_encoder_->SetSource(&new_video_source, 856 vie_encoder_->SetSource(
758 VideoSendStream::DegradationPreference::kBalanced); 857 &new_video_source,
759 EXPECT_LT(new_video_source.sink_wants().max_pixel_count.value_or( 858 VideoSendStream::DegradationPreference::kMaintainFramerate);
760 std::numeric_limits<int>::max()), 859 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
761 frame_width * frame_height); 860 kFrameWidth * kFrameHeight);
762 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 861 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
862 EXPECT_EQ(std::numeric_limits<int>::max(),
863 new_video_source.sink_wants().max_framerate_fps);
864
865 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
866 vie_encoder_->SetSource(
867 &new_video_source,
868 VideoSendStream::DegradationPreference::kMaintainResolution);
869 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
870 EXPECT_EQ(std::numeric_limits<int>::max(),
871 new_video_source.sink_wants().max_pixel_count);
872 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
763 873
764 vie_encoder_->Stop(); 874 vie_encoder_->Stop();
765 } 875 }
766 876
767 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) { 877 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) {
768 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 878 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
769 879
770 int frame_width = 1280; 880 int frame_width = 1280;
771 int frame_height = 720; 881 int frame_height = 720;
772 882
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 927
818 video_source_.IncomingCapturedFrame( 928 video_source_.IncomingCapturedFrame(
819 CreateFrame(2, frame_width, frame_height)); 929 CreateFrame(2, frame_width, frame_height));
820 sink_.WaitForEncodedFrame(2); 930 sink_.WaitForEncodedFrame(2);
821 stats = stats_proxy_->GetStats(); 931 stats = stats_proxy_->GetStats();
822 EXPECT_TRUE(stats.cpu_limited_resolution); 932 EXPECT_TRUE(stats.cpu_limited_resolution);
823 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 933 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
824 934
825 // Set new source with adaptation still enabled. 935 // Set new source with adaptation still enabled.
826 test::FrameForwarder new_video_source; 936 test::FrameForwarder new_video_source;
827 vie_encoder_->SetSource(&new_video_source, 937 vie_encoder_->SetSource(
828 VideoSendStream::DegradationPreference::kBalanced); 938 &new_video_source,
939 VideoSendStream::DegradationPreference::kMaintainFramerate);
829 940
830 new_video_source.IncomingCapturedFrame( 941 new_video_source.IncomingCapturedFrame(
831 CreateFrame(3, frame_width, frame_height)); 942 CreateFrame(3, frame_width, frame_height));
832 sink_.WaitForEncodedFrame(3); 943 sink_.WaitForEncodedFrame(3);
833 stats = stats_proxy_->GetStats(); 944 stats = stats_proxy_->GetStats();
834 EXPECT_TRUE(stats.cpu_limited_resolution); 945 EXPECT_TRUE(stats.cpu_limited_resolution);
835 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 946 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
836 947
837 // Set adaptation disabled. 948 // Set adaptation disabled.
838 vie_encoder_->SetSource( 949 vie_encoder_->SetSource(
839 &new_video_source, 950 &new_video_source,
840 VideoSendStream::DegradationPreference::kMaintainResolution); 951 VideoSendStream::DegradationPreference::kDegradationDisabled);
841 952
842 new_video_source.IncomingCapturedFrame( 953 new_video_source.IncomingCapturedFrame(
843 CreateFrame(4, frame_width, frame_height)); 954 CreateFrame(4, frame_width, frame_height));
844 sink_.WaitForEncodedFrame(4); 955 sink_.WaitForEncodedFrame(4);
845 stats = stats_proxy_->GetStats(); 956 stats = stats_proxy_->GetStats();
846 EXPECT_FALSE(stats.cpu_limited_resolution); 957 EXPECT_FALSE(stats.cpu_limited_resolution);
847 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 958 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
848 959
849 // Set adaptation back to enabled. 960 // Set adaptation back to enabled.
850 vie_encoder_->SetSource(&new_video_source, 961 vie_encoder_->SetSource(
851 VideoSendStream::DegradationPreference::kBalanced); 962 &new_video_source,
963 VideoSendStream::DegradationPreference::kMaintainFramerate);
852 964
853 new_video_source.IncomingCapturedFrame( 965 new_video_source.IncomingCapturedFrame(
854 CreateFrame(5, frame_width, frame_height)); 966 CreateFrame(5, frame_width, frame_height));
855 sink_.WaitForEncodedFrame(5); 967 sink_.WaitForEncodedFrame(5);
856 stats = stats_proxy_->GetStats(); 968 stats = stats_proxy_->GetStats();
857 EXPECT_TRUE(stats.cpu_limited_resolution); 969 EXPECT_TRUE(stats.cpu_limited_resolution);
858 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 970 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
859 971
860 vie_encoder_->TriggerCpuNormalUsage(); 972 vie_encoder_->TriggerCpuNormalUsage();
861 973
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 video_source_.IncomingCapturedFrame( 1062 video_source_.IncomingCapturedFrame(
951 CreateFrame(sequence, frame_width, frame_height)); 1063 CreateFrame(sequence, frame_width, frame_height));
952 sink_.WaitForEncodedFrame(sequence++); 1064 sink_.WaitForEncodedFrame(sequence++);
953 1065
954 stats = stats_proxy_->GetStats(); 1066 stats = stats_proxy_->GetStats();
955 EXPECT_TRUE(stats.cpu_limited_resolution); 1067 EXPECT_TRUE(stats.cpu_limited_resolution);
956 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1068 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
957 1069
958 // Set new source with adaptation still enabled. 1070 // Set new source with adaptation still enabled.
959 test::FrameForwarder new_video_source; 1071 test::FrameForwarder new_video_source;
960 vie_encoder_->SetSource(&new_video_source, 1072 vie_encoder_->SetSource(
961 VideoSendStream::DegradationPreference::kBalanced); 1073 &new_video_source,
1074 VideoSendStream::DegradationPreference::kMaintainFramerate);
962 1075
963 new_video_source.IncomingCapturedFrame( 1076 new_video_source.IncomingCapturedFrame(
964 CreateFrame(sequence, frame_width, frame_height)); 1077 CreateFrame(sequence, frame_width, frame_height));
965 sink_.WaitForEncodedFrame(sequence++); 1078 sink_.WaitForEncodedFrame(sequence++);
966 stats = stats_proxy_->GetStats(); 1079 stats = stats_proxy_->GetStats();
967 EXPECT_TRUE(stats.cpu_limited_resolution); 1080 EXPECT_TRUE(stats.cpu_limited_resolution);
968 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1081 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
969 1082
970 // Set adaptation disabled. 1083 // Set cpu adaptation by frame dropping.
971 vie_encoder_->SetSource( 1084 vie_encoder_->SetSource(
972 &new_video_source, 1085 &new_video_source,
973 VideoSendStream::DegradationPreference::kMaintainResolution); 1086 VideoSendStream::DegradationPreference::kMaintainResolution);
974 new_video_source.IncomingCapturedFrame( 1087 new_video_source.IncomingCapturedFrame(
975 CreateFrame(sequence, frame_width, frame_height)); 1088 CreateFrame(sequence, frame_width, frame_height));
976 sink_.WaitForEncodedFrame(sequence++); 1089 sink_.WaitForEncodedFrame(sequence++);
977 stats = stats_proxy_->GetStats(); 1090 stats = stats_proxy_->GetStats();
1091 // Not adapted at first.
978 EXPECT_FALSE(stats.cpu_limited_resolution); 1092 EXPECT_FALSE(stats.cpu_limited_resolution);
979 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1093 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
980 1094
981 // Switch back the source with adaptation enabled. 1095 // Force an input frame rate to be available, or the adaptation call won't
982 vie_encoder_->SetSource(&video_source_, 1096 // know what framerate to adapt form.
983 VideoSendStream::DegradationPreference::kBalanced); 1097 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1098 mock_stats.input_frame_rate = 30;
1099 stats_proxy_->SetMockStats(mock_stats);
1100 vie_encoder_->TriggerCpuOveruse();
1101 stats_proxy_->ResetMockStats();
1102
1103 new_video_source.IncomingCapturedFrame(
1104 CreateFrame(sequence, frame_width, frame_height));
1105 sink_.WaitForEncodedFrame(sequence++);
1106
1107 // Framerate now adapted.
1108 stats = stats_proxy_->GetStats();
1109 EXPECT_TRUE(stats.cpu_limited_resolution);
1110 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1111
1112 // Disable CPU adaptation.
1113 vie_encoder_->SetSource(
1114 &new_video_source,
1115 VideoSendStream::DegradationPreference::kDegradationDisabled);
1116 new_video_source.IncomingCapturedFrame(
1117 CreateFrame(sequence, frame_width, frame_height));
1118 sink_.WaitForEncodedFrame(sequence++);
1119
1120 stats = stats_proxy_->GetStats();
1121 EXPECT_FALSE(stats.cpu_limited_resolution);
1122 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1123
1124 // Try to trigger overuse. Should not succeed.
1125 stats_proxy_->SetMockStats(mock_stats);
1126 vie_encoder_->TriggerCpuOveruse();
1127 stats_proxy_->ResetMockStats();
1128
1129 stats = stats_proxy_->GetStats();
1130 EXPECT_FALSE(stats.cpu_limited_resolution);
1131 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1132
1133 // Switch back the source with resolution adaptation enabled.
1134 vie_encoder_->SetSource(
1135 &video_source_,
1136 VideoSendStream::DegradationPreference::kMaintainFramerate);
984 video_source_.IncomingCapturedFrame( 1137 video_source_.IncomingCapturedFrame(
985 CreateFrame(sequence, frame_width, frame_height)); 1138 CreateFrame(sequence, frame_width, frame_height));
986 sink_.WaitForEncodedFrame(sequence++); 1139 sink_.WaitForEncodedFrame(sequence++);
987 stats = stats_proxy_->GetStats(); 1140 stats = stats_proxy_->GetStats();
988 EXPECT_TRUE(stats.cpu_limited_resolution); 1141 EXPECT_TRUE(stats.cpu_limited_resolution);
989 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1142 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
990 1143
991 // Trigger CPU normal usage. 1144 // Trigger CPU normal usage.
992 vie_encoder_->TriggerCpuNormalUsage(); 1145 vie_encoder_->TriggerCpuNormalUsage();
993 video_source_.IncomingCapturedFrame( 1146 video_source_.IncomingCapturedFrame(
994 CreateFrame(sequence, frame_width, frame_height)); 1147 CreateFrame(sequence, frame_width, frame_height));
995 sink_.WaitForEncodedFrame(sequence++); 1148 sink_.WaitForEncodedFrame(sequence++);
996 stats = stats_proxy_->GetStats(); 1149 stats = stats_proxy_->GetStats();
997 EXPECT_FALSE(stats.cpu_limited_resolution); 1150 EXPECT_FALSE(stats.cpu_limited_resolution);
998 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1151 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1152
1153 // Back to the source with adaptation off, set it back to maintain-resolution.
1154 vie_encoder_->SetSource(
1155 &new_video_source,
1156 VideoSendStream::DegradationPreference::kMaintainResolution);
1157 new_video_source.IncomingCapturedFrame(
1158 CreateFrame(sequence, frame_width, frame_height));
1159 sink_.WaitForEncodedFrame(sequence++);
1160 stats = stats_proxy_->GetStats();
1161 // Disabled, since we previously switched the source too disabled.
1162 EXPECT_FALSE(stats.cpu_limited_resolution);
1163 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1164
1165 // Trigger CPU normal usage.
1166 vie_encoder_->TriggerCpuNormalUsage();
1167 new_video_source.IncomingCapturedFrame(
1168 CreateFrame(sequence, frame_width, frame_height));
1169 sink_.WaitForEncodedFrame(sequence++);
1170 stats = stats_proxy_->GetStats();
1171 EXPECT_FALSE(stats.cpu_limited_resolution);
1172 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
999 1173
1000 vie_encoder_->Stop(); 1174 vie_encoder_->Stop();
1001 } 1175 }
1002 1176
1003 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) { 1177 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) {
1004 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1178 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1005 1179
1006 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720)); 1180 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
1007 sink_.WaitForEncodedFrame(1); 1181 sink_.WaitForEncodedFrame(1);
1008 1182
1009 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1183 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1010 EXPECT_EQ(video_encoder_config_.max_bitrate_bps, 1184 EXPECT_EQ(video_encoder_config_.max_bitrate_bps,
1011 stats.preferred_media_bitrate_bps); 1185 stats.preferred_media_bitrate_bps);
1012 1186
1013 vie_encoder_->Stop(); 1187 vie_encoder_->Stop();
1014 } 1188 }
1015 1189
1016 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) { 1190 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) {
1017 int frame_width = 1280; 1191 int frame_width = 1280;
1018 int frame_height = 720; 1192 int frame_height = 720;
1019 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1193 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1020 1194
1021 // Expect no scaling to begin with 1195 // Expect no scaling to begin with
1022 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 1196 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
1023 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 1197 EXPECT_EQ(std::numeric_limits<int>::max(),
1198 video_source_.sink_wants().max_pixel_count);
1024 1199
1025 video_source_.IncomingCapturedFrame( 1200 video_source_.IncomingCapturedFrame(
1026 CreateFrame(1, frame_width, frame_height)); 1201 CreateFrame(1, frame_width, frame_height));
1027 sink_.WaitForEncodedFrame(1); 1202 sink_.WaitForEncodedFrame(1);
1028 1203
1029 // Trigger scale down 1204 // Trigger scale down
1030 vie_encoder_->TriggerQualityLow(); 1205 vie_encoder_->TriggerQualityLow();
1031 1206
1032 video_source_.IncomingCapturedFrame( 1207 video_source_.IncomingCapturedFrame(
1033 CreateFrame(2, frame_width, frame_height)); 1208 CreateFrame(2, frame_width, frame_height));
1034 sink_.WaitForEncodedFrame(2); 1209 sink_.WaitForEncodedFrame(2);
1035 1210
1036 // Expect a scale down. 1211 // Expect a scale down.
1037 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1212 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
1038 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1213 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
1039 frame_width * frame_height); 1214 frame_width * frame_height);
1040 1215
1041 // Set adaptation disabled. 1216 // Set adaptation disabled.
1042 test::FrameForwarder new_video_source; 1217 test::FrameForwarder new_video_source;
1043 vie_encoder_->SetSource( 1218 vie_encoder_->SetSource(
1044 &new_video_source, 1219 &new_video_source,
1045 VideoSendStream::DegradationPreference::kMaintainResolution); 1220 VideoSendStream::DegradationPreference::kMaintainResolution);
1046 1221
1047 // Trigger scale down 1222 // Trigger scale down
1048 vie_encoder_->TriggerQualityLow(); 1223 vie_encoder_->TriggerQualityLow();
1049 new_video_source.IncomingCapturedFrame( 1224 new_video_source.IncomingCapturedFrame(
1050 CreateFrame(3, frame_width, frame_height)); 1225 CreateFrame(3, frame_width, frame_height));
1051 sink_.WaitForEncodedFrame(3); 1226 sink_.WaitForEncodedFrame(3);
1052 1227
1053 // Expect no scaling 1228 // Expect no scaling
1054 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 1229 EXPECT_EQ(std::numeric_limits<int>::max(),
1230 new_video_source.sink_wants().max_pixel_count);
1055 1231
1056 // Trigger scale up 1232 // Trigger scale up
1057 vie_encoder_->TriggerQualityHigh(); 1233 vie_encoder_->TriggerQualityHigh();
1058 new_video_source.IncomingCapturedFrame( 1234 new_video_source.IncomingCapturedFrame(
1059 CreateFrame(4, frame_width, frame_height)); 1235 CreateFrame(4, frame_width, frame_height));
1060 sink_.WaitForEncodedFrame(4); 1236 sink_.WaitForEncodedFrame(4);
1061 1237
1062 // Expect nothing to change, still no scaling 1238 // Expect nothing to change, still no scaling
1063 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 1239 EXPECT_EQ(std::numeric_limits<int>::max(),
1240 new_video_source.sink_wants().max_pixel_count);
1064 1241
1065 vie_encoder_->Stop(); 1242 vie_encoder_->Stop();
1066 } 1243 }
1067 1244
1068 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) { 1245 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) {
1069 int frame_width = 1280; 1246 int frame_width = 1280;
1070 int frame_height = 720; 1247 int frame_height = 720;
1071 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1248 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1072 1249
1073 for (size_t i = 1; i <= 10; i++) { 1250 for (size_t i = 1; i <= 10; i++) {
1074 video_source_.IncomingCapturedFrame( 1251 video_source_.IncomingCapturedFrame(
1075 CreateFrame(i, frame_width, frame_height)); 1252 CreateFrame(i, frame_width, frame_height));
1076 sink_.WaitForEncodedFrame(i); 1253 sink_.WaitForEncodedFrame(i);
1077 // Trigger scale down 1254 // Trigger scale down
1078 vie_encoder_->TriggerQualityLow(); 1255 vie_encoder_->TriggerQualityLow();
1079 EXPECT_GE(*video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame); 1256 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
1080 } 1257 }
1081 1258
1082 vie_encoder_->Stop(); 1259 vie_encoder_->Stop();
1083 } 1260 }
1084 1261
1085 TEST_F(ViEEncoderTest, UMACpuLimitedResolutionInPercent) { 1262 TEST_F(ViEEncoderTest, UMACpuLimitedResolutionInPercent) {
1086 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1263 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1087 1264
1088 int frame_width = 640; 1265 int frame_width = 640;
1089 int frame_height = 360; 1266 int frame_height = 360;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1162 int frame_width = 640; 1339 int frame_width = 640;
1163 int frame_height = 360; 1340 int frame_height = 360;
1164 1341
1165 video_source_.IncomingCapturedFrame( 1342 video_source_.IncomingCapturedFrame(
1166 CreateFrame(1, frame_width, frame_height)); 1343 CreateFrame(1, frame_width, frame_height));
1167 1344
1168 // Expect to drop this frame, the wait should time out. 1345 // Expect to drop this frame, the wait should time out.
1169 sink_.ExpectDroppedFrame(); 1346 sink_.ExpectDroppedFrame();
1170 1347
1171 // Expect the sink_wants to specify a scaled frame. 1348 // Expect the sink_wants to specify a scaled frame.
1172 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1349 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1173 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1174 1350
1175 int last_pixel_count = *video_source_.sink_wants().max_pixel_count; 1351 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
1176 1352
1177 // Next frame is scaled 1353 // Next frame is scaled
1178 video_source_.IncomingCapturedFrame( 1354 video_source_.IncomingCapturedFrame(
1179 CreateFrame(2, frame_width * 3 / 4, frame_height * 3 / 4)); 1355 CreateFrame(2, frame_width * 3 / 4, frame_height * 3 / 4));
1180 1356
1181 // Expect to drop this frame, the wait should time out. 1357 // Expect to drop this frame, the wait should time out.
1182 sink_.ExpectDroppedFrame(); 1358 sink_.ExpectDroppedFrame();
1183 1359
1184 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, last_pixel_count); 1360 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
1185 1361
1186 vie_encoder_->Stop(); 1362 vie_encoder_->Stop();
1187 } 1363 }
1188 1364
1189 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimited) { 1365 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimited) {
1190 // 1kbps. This can never be achieved. 1366 // 1kbps. This can never be achieved.
1191 vie_encoder_->OnBitrateUpdated(1000, 0, 0); 1367 vie_encoder_->OnBitrateUpdated(1000, 0, 0);
1192 int frame_width = 640; 1368 int frame_width = 640;
1193 int frame_height = 360; 1369 int frame_height = 360;
1194 1370
1195 // We expect the n initial frames to get dropped. 1371 // We expect the n initial frames to get dropped.
1196 int i; 1372 int i;
1197 for (i = 1; i <= kMaxInitialFramedrop; ++i) { 1373 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
1198 video_source_.IncomingCapturedFrame( 1374 video_source_.IncomingCapturedFrame(
1199 CreateFrame(i, frame_width, frame_height)); 1375 CreateFrame(i, frame_width, frame_height));
1200 sink_.ExpectDroppedFrame(); 1376 sink_.ExpectDroppedFrame();
1201 } 1377 }
1202 // The n+1th frame should not be dropped, even though it's size is too large. 1378 // The n+1th frame should not be dropped, even though it's size is too large.
1203 video_source_.IncomingCapturedFrame( 1379 video_source_.IncomingCapturedFrame(
1204 CreateFrame(i, frame_width, frame_height)); 1380 CreateFrame(i, frame_width, frame_height));
1205 sink_.WaitForEncodedFrame(i); 1381 sink_.WaitForEncodedFrame(i);
1206 1382
1207 // Expect the sink_wants to specify a scaled frame. 1383 // Expect the sink_wants to specify a scaled frame.
1208 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1384 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1209 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000);
1210 1385
1211 vie_encoder_->Stop(); 1386 vie_encoder_->Stop();
1212 } 1387 }
1213 1388
1214 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) { 1389 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) {
1215 int frame_width = 640; 1390 int frame_width = 640;
1216 int frame_height = 360; 1391 int frame_height = 360;
1217 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); 1392 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0);
1218 1393
1219 // Set degradation preference. 1394 // Set degradation preference.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 video_source_.IncomingCapturedFrame( 1435 video_source_.IncomingCapturedFrame(
1261 CreateFrame(1, kFrameWidth, kFrameHeight)); 1436 CreateFrame(1, kFrameWidth, kFrameHeight));
1262 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1437 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1263 1438
1264 // Trigger CPU overuse, downscale by 3/4. 1439 // Trigger CPU overuse, downscale by 3/4.
1265 vie_encoder_->TriggerCpuOveruse(); 1440 vie_encoder_->TriggerCpuOveruse();
1266 video_source_.IncomingCapturedFrame( 1441 video_source_.IncomingCapturedFrame(
1267 CreateFrame(2, kFrameWidth, kFrameHeight)); 1442 CreateFrame(2, kFrameWidth, kFrameHeight));
1268 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); 1443 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
1269 1444
1270 // Trigger CPU normal use, return to original resoluton; 1445 // Trigger CPU normal use, return to original resolution;
1271 vie_encoder_->TriggerCpuNormalUsage(); 1446 vie_encoder_->TriggerCpuNormalUsage();
1272 video_source_.IncomingCapturedFrame( 1447 video_source_.IncomingCapturedFrame(
1273 CreateFrame(3, kFrameWidth, kFrameHeight)); 1448 CreateFrame(3, kFrameWidth, kFrameHeight));
1274 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1449 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1275 1450
1276 vie_encoder_->Stop(); 1451 vie_encoder_->Stop();
1277 } 1452 }
1278 1453
1279 TEST_F(ViEEncoderTest, FailingInitEncodeDoesntCauseCrash) { 1454 TEST_F(ViEEncoderTest, FailingInitEncodeDoesntCauseCrash) {
1280 fake_encoder_.ForceInitEncodeFailure(true); 1455 fake_encoder_.ForceInitEncodeFailure(true);
1281 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1456 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1282 ResetEncoder("VP8", 2, 1, true); 1457 ResetEncoder("VP8", 2, 1, true);
1283 const int kFrameWidth = 1280; 1458 const int kFrameWidth = 1280;
1284 const int kFrameHeight = 720; 1459 const int kFrameHeight = 720;
1285 video_source_.IncomingCapturedFrame( 1460 video_source_.IncomingCapturedFrame(
1286 CreateFrame(1, kFrameWidth, kFrameHeight)); 1461 CreateFrame(1, kFrameWidth, kFrameHeight));
1287 sink_.ExpectDroppedFrame(); 1462 sink_.ExpectDroppedFrame();
1288 vie_encoder_->Stop(); 1463 vie_encoder_->Stop();
1289 } 1464 }
1465
1466 TEST_F(ViEEncoderTest, AdaptsFrameOnOveruseWithMaintainResolution) {
1467 const int kDefaultFramerateFps = 30;
1468 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerateFps;
1469 const int kFrameWidth = 1280;
1470 const int kFrameHeight = 720;
1471 rtc::ScopedFakeClock fake_clock;
1472
1473 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1474 vie_encoder_->SetSource(
1475 &video_source_,
1476 VideoSendStream::DegradationPreference::kMaintainResolution);
1477 video_source_.set_adaptation_enabled(true);
1478
1479 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1480 int64_t timestamp_ms = kFrameIntervalMs;
1481
1482 video_source_.IncomingCapturedFrame(
1483 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1484 sink_.WaitForEncodedFrame(timestamp_ms);
1485
1486 // Try to trigger overuse. No fps estimate available => no effect.
1487 vie_encoder_->TriggerCpuOveruse();
1488
1489 // Insert frames for one second to get a stable estimate.
1490 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1491 timestamp_ms += kFrameIntervalMs;
1492 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1493 video_source_.IncomingCapturedFrame(
1494 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1495 sink_.WaitForEncodedFrame(timestamp_ms);
1496 }
1497
1498 // Trigger CPU overuse, reduce framerate by 2/3.
1499 vie_encoder_->TriggerCpuOveruse();
1500 int num_frames_dropped = 0;
1501 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1502 timestamp_ms += kFrameIntervalMs;
1503 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1504 video_source_.IncomingCapturedFrame(
1505 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1506 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1507 ++num_frames_dropped;
1508 } else {
1509 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1510 }
1511 }
1512
1513 // TODO(sprang): Find where there's rounding errors or stuff causing the
1514 // margin here to be a little larger than we'd like (input fps estimate is
1515 // off) and the frame dropping is a little too aggressive.
1516 const int kErrorMargin = 5;
1517 EXPECT_NEAR(num_frames_dropped,
1518 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1519 kErrorMargin);
1520
1521 // Trigger CPU overuse, reduce framerate by 2/3 again.
1522 vie_encoder_->TriggerCpuOveruse();
1523 num_frames_dropped = 0;
1524 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1525 timestamp_ms += kFrameIntervalMs;
1526 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1527 video_source_.IncomingCapturedFrame(
1528 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1529 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1530 ++num_frames_dropped;
1531 } else {
1532 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1533 }
1534 }
1535 EXPECT_NEAR(num_frames_dropped,
1536 kDefaultFramerateFps - (kDefaultFramerateFps * 4 / 9),
1537 kErrorMargin);
1538
1539 // Go back up one step.
1540 vie_encoder_->TriggerCpuNormalUsage();
1541 num_frames_dropped = 0;
1542 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1543 timestamp_ms += kFrameIntervalMs;
1544 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1545 video_source_.IncomingCapturedFrame(
1546 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1547 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1548 ++num_frames_dropped;
1549 } else {
1550 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1551 }
1552 }
1553 EXPECT_NEAR(num_frames_dropped,
1554 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1555 kErrorMargin);
1556
1557 // Go back up to original mode.
1558 vie_encoder_->TriggerCpuNormalUsage();
1559 num_frames_dropped = 0;
1560 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1561 timestamp_ms += kFrameIntervalMs;
1562 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1563 video_source_.IncomingCapturedFrame(
1564 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1565 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1566 ++num_frames_dropped;
1567 } else {
1568 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1569 }
1570 }
1571 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
1572
1573 vie_encoder_->Stop();
1574 }
1575
1576 TEST_F(ViEEncoderTest, DoesntAdaptDownPastMinFramerate) {
1577 const int kFramerateFps = 5;
1578 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
1579 const int kMinFpsFrameInterval = rtc::kNumMillisecsPerSec / kMinFramerateFps;
1580 const int kFrameWidth = 1280;
1581 const int kFrameHeight = 720;
1582
1583 rtc::ScopedFakeClock fake_clock;
1584 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1585 vie_encoder_->SetSource(
1586 &video_source_,
1587 VideoSendStream::DegradationPreference::kMaintainResolution);
1588 video_source_.set_adaptation_enabled(true);
1589
1590 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1591 int64_t timestamp_ms = kFrameIntervalMs;
1592
1593 // Trigger overuse as much as we can.
1594 for (int i = 0; i < ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
1595 // Insert frames to get a new fps estimate...
1596 for (int j = 0; j < kFramerateFps; ++j) {
1597 video_source_.IncomingCapturedFrame(
1598 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1599 timestamp_ms += kFrameIntervalMs;
1600 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1601 }
1602 // ...and then try to adapt again.
1603 vie_encoder_->TriggerCpuOveruse();
1604 }
1605
1606 // Drain any frame in the pipeline.
1607 sink_.WaitForFrame(kDefaultTimeoutMs);
1608
1609 // Insert frames at min fps, all should go through.
1610 for (int i = 0; i < 10; ++i) {
1611 timestamp_ms += kMinFpsFrameInterval;
1612 fake_clock.AdvanceTimeMicros(kMinFpsFrameInterval * 1000);
1613 video_source_.IncomingCapturedFrame(
1614 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1615 sink_.WaitForEncodedFrame(timestamp_ms);
1616 }
1617 vie_encoder_->Stop();
1618 }
1290 } // namespace webrtc 1619 } // 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