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

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

Issue 2883963002: Periodically update codec bit/frame rate settings. (Closed)
Patch Set: cleanup Created 3 years, 7 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
« webrtc/video/vie_encoder.cc ('K') | « webrtc/video/vie_encoder.cc ('k') | no next file » | 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
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } // namespace 205 } // namespace
206 206
207 class ViEEncoderTest : public ::testing::Test { 207 class ViEEncoderTest : public ::testing::Test {
208 public: 208 public:
209 static const int kDefaultTimeoutMs = 30 * 1000; 209 static const int kDefaultTimeoutMs = 30 * 1000;
210 210
211 ViEEncoderTest() 211 ViEEncoderTest()
212 : video_send_config_(VideoSendStream::Config(nullptr)), 212 : video_send_config_(VideoSendStream::Config(nullptr)),
213 codec_width_(320), 213 codec_width_(320),
214 codec_height_(240), 214 codec_height_(240),
215 max_framerate_(30),
215 fake_encoder_(), 216 fake_encoder_(),
216 stats_proxy_(new MockableSendStatisticsProxy( 217 stats_proxy_(new MockableSendStatisticsProxy(
217 Clock::GetRealTimeClock(), 218 Clock::GetRealTimeClock(),
218 video_send_config_, 219 video_send_config_,
219 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), 220 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
220 sink_(&fake_encoder_) {} 221 sink_(&fake_encoder_) {}
221 222
222 void SetUp() override { 223 void SetUp() override {
223 metrics::Reset(); 224 metrics::Reset();
224 video_send_config_ = VideoSendStream::Config(nullptr); 225 video_send_config_ = VideoSendStream::Config(nullptr);
225 video_send_config_.encoder_settings.encoder = &fake_encoder_; 226 video_send_config_.encoder_settings.encoder = &fake_encoder_;
226 video_send_config_.encoder_settings.payload_name = "FAKE"; 227 video_send_config_.encoder_settings.payload_name = "FAKE";
227 video_send_config_.encoder_settings.payload_type = 125; 228 video_send_config_.encoder_settings.payload_type = 125;
228 229
229 VideoEncoderConfig video_encoder_config; 230 VideoEncoderConfig video_encoder_config;
230 test::FillEncoderConfiguration(1, &video_encoder_config); 231 test::FillEncoderConfiguration(1, &video_encoder_config);
232 video_encoder_config.video_stream_factory =
233 new rtc::RefCountedObject<VideoStreamFactory>(1);
231 video_encoder_config_ = video_encoder_config.Copy(); 234 video_encoder_config_ = video_encoder_config.Copy();
235
236 // Framerate limit is specified by the VideoStreamFactory.
237 std::vector<VideoStream> streams =
238 video_encoder_config.video_stream_factory->CreateEncoderStreams(
239 codec_width_, codec_height_, video_encoder_config);
240 max_framerate_ = streams[0].max_framerate;
241 fake_clock_.SetTimeMicros(1234);
242
232 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */); 243 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */);
233 } 244 }
234 245
235 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, 246 void ConfigureEncoder(VideoEncoderConfig video_encoder_config,
236 bool nack_enabled) { 247 bool nack_enabled) {
237 if (vie_encoder_) 248 if (vie_encoder_)
238 vie_encoder_->Stop(); 249 vie_encoder_->Stop();
239 vie_encoder_.reset(new ViEEncoderUnderTest( 250 vie_encoder_.reset(new ViEEncoderUnderTest(
240 stats_proxy_.get(), video_send_config_.encoder_settings)); 251 stats_proxy_.get(), video_send_config_.encoder_settings));
241 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); 252 vie_encoder_->SetSink(&sink_, false /* rotation_applied */);
242 vie_encoder_->SetSource( 253 vie_encoder_->SetSource(
243 &video_source_, 254 &video_source_,
244 VideoSendStream::DegradationPreference::kMaintainFramerate); 255 VideoSendStream::DegradationPreference::kMaintainFramerate);
245 vie_encoder_->SetStartBitrate(kTargetBitrateBps); 256 vie_encoder_->SetStartBitrate(kTargetBitrateBps);
246 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 257 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config),
247 kMaxPayloadLength, nack_enabled); 258 kMaxPayloadLength, nack_enabled);
248 vie_encoder_->WaitUntilTaskQueueIsIdle(); 259 vie_encoder_->WaitUntilTaskQueueIsIdle();
249 } 260 }
250 261
251 void ResetEncoder(const std::string& payload_name, 262 void ResetEncoder(const std::string& payload_name,
252 size_t num_streams, 263 size_t num_streams,
253 size_t num_temporal_layers, 264 size_t num_temporal_layers,
254 bool nack_enabled) { 265 bool nack_enabled,
266 bool screenshare) {
255 video_send_config_.encoder_settings.payload_name = payload_name; 267 video_send_config_.encoder_settings.payload_name = payload_name;
256 268
257 VideoEncoderConfig video_encoder_config; 269 VideoEncoderConfig video_encoder_config;
258 video_encoder_config.number_of_streams = num_streams; 270 video_encoder_config.number_of_streams = num_streams;
259 video_encoder_config.max_bitrate_bps = kTargetBitrateBps; 271 video_encoder_config.max_bitrate_bps = kTargetBitrateBps;
260 video_encoder_config.video_stream_factory = 272 video_encoder_config.video_stream_factory =
261 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers); 273 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers);
274 video_encoder_config.content_type =
275 screenshare ? VideoEncoderConfig::ContentType::kScreen
276 : VideoEncoderConfig::ContentType::kRealtimeVideo;
262 ConfigureEncoder(std::move(video_encoder_config), nack_enabled); 277 ConfigureEncoder(std::move(video_encoder_config), nack_enabled);
263 } 278 }
264 279
265 VideoFrame CreateFrame(int64_t ntp_time_ms, 280 VideoFrame CreateFrame(int64_t ntp_time_ms,
266 rtc::Event* destruction_event) const { 281 rtc::Event* destruction_event) const {
267 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>( 282 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>(
268 destruction_event, codec_width_, codec_height_), 283 destruction_event, codec_width_, codec_height_),
269 99, 99, kVideoRotation_0); 284 99, 99, kVideoRotation_0);
270 frame.set_ntp_time_ms(ntp_time_ms); 285 frame.set_ntp_time_ms(ntp_time_ms);
271 return frame; 286 return frame;
(...skipping 14 matching lines...) Expand all
286 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps); 301 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
287 } 302 }
288 303
289 void VerifyResolutionLimitationLessThan(const rtc::VideoSinkWants& wants, 304 void VerifyResolutionLimitationLessThan(const rtc::VideoSinkWants& wants,
290 int pixel_count) { 305 int pixel_count) {
291 EXPECT_LT(wants.max_pixel_count, pixel_count); 306 EXPECT_LT(wants.max_pixel_count, pixel_count);
292 EXPECT_GT(wants.max_pixel_count, 0); 307 EXPECT_GT(wants.max_pixel_count, 0);
293 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps); 308 EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps);
294 } 309 }
295 310
311 void WaitForEncodedFrame(int64_t expected_ntp_time) {
312 sink_.WaitForEncodedFrame(expected_ntp_time);
313 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_);
314 }
315
316 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) {
317 bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms);
318 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_);
319 return ok;
320 }
321
322 void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) {
323 sink_.WaitForEncodedFrame(expected_width, expected_height);
324 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_);
325 }
326
327 void ExpectDroppedFrame() {
328 sink_.ExpectDroppedFrame();
329 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_);
330 }
331
332 bool WaitForFrame(int64_t timeout_ms) {
333 bool ok = sink_.WaitForFrame(timeout_ms);
334 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerSec / max_framerate_);
335 return ok;
336 }
337
296 class TestEncoder : public test::FakeEncoder { 338 class TestEncoder : public test::FakeEncoder {
297 public: 339 public:
298 TestEncoder() 340 TestEncoder()
299 : FakeEncoder(Clock::GetRealTimeClock()), 341 : FakeEncoder(Clock::GetRealTimeClock()),
300 continue_encode_event_(false, false) {} 342 continue_encode_event_(false, false) {}
301 343
302 VideoCodec codec_config() const { 344 VideoCodec codec_config() const {
303 rtc::CritScope lock(&crit_sect_); 345 rtc::CritScope lock(&crit_sect_);
304 return config_; 346 return config_;
305 } 347 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 GUARDED_BY(local_crit_sect_); 436 GUARDED_BY(local_crit_sect_);
395 bool force_init_encode_failed_ GUARDED_BY(local_crit_sect_) = false; 437 bool force_init_encode_failed_ GUARDED_BY(local_crit_sect_) = false;
396 }; 438 };
397 439
398 class TestSink : public ViEEncoder::EncoderSink { 440 class TestSink : public ViEEncoder::EncoderSink {
399 public: 441 public:
400 explicit TestSink(TestEncoder* test_encoder) 442 explicit TestSink(TestEncoder* test_encoder)
401 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {} 443 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {}
402 444
403 void WaitForEncodedFrame(int64_t expected_ntp_time) { 445 void WaitForEncodedFrame(int64_t expected_ntp_time) {
446 EXPECT_TRUE(
447 TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs));
448 }
449
450 bool TimedWaitForEncodedFrame(int64_t expected_ntp_time,
451 int64_t timeout_ms) {
404 uint32_t timestamp = 0; 452 uint32_t timestamp = 0;
405 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 453 if (!encoded_frame_event_.Wait(timeout_ms))
454 return false;
406 { 455 {
407 rtc::CritScope lock(&crit_); 456 rtc::CritScope lock(&crit_);
408 timestamp = last_timestamp_; 457 timestamp = last_timestamp_;
409 } 458 }
410 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); 459 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
460 return true;
411 } 461 }
412 462
413 void WaitForEncodedFrame(uint32_t expected_width, 463 void WaitForEncodedFrame(uint32_t expected_width,
414 uint32_t expected_height) { 464 uint32_t expected_height) {
415 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 465 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
416 CheckLastFrameSizeMathces(expected_width, expected_height); 466 CheckLastFrameSizeMathces(expected_width, expected_height);
417 } 467 }
418 468
419 void CheckLastFrameSizeMathces(uint32_t expected_width, 469 void CheckLastFrameSizeMathces(uint32_t expected_width,
420 uint32_t expected_height) { 470 uint32_t expected_height) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 uint32_t last_width_ = 0; 529 uint32_t last_width_ = 0;
480 bool expect_frames_ = true; 530 bool expect_frames_ = true;
481 int number_of_reconfigurations_ = 0; 531 int number_of_reconfigurations_ = 0;
482 int min_transmit_bitrate_bps_ = 0; 532 int min_transmit_bitrate_bps_ = 0;
483 }; 533 };
484 534
485 VideoSendStream::Config video_send_config_; 535 VideoSendStream::Config video_send_config_;
486 VideoEncoderConfig video_encoder_config_; 536 VideoEncoderConfig video_encoder_config_;
487 int codec_width_; 537 int codec_width_;
488 int codec_height_; 538 int codec_height_;
539 int max_framerate_;
489 TestEncoder fake_encoder_; 540 TestEncoder fake_encoder_;
490 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_; 541 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
491 TestSink sink_; 542 TestSink sink_;
492 AdaptingFrameForwarder video_source_; 543 AdaptingFrameForwarder video_source_;
493 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_; 544 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_;
545 rtc::ScopedFakeClock fake_clock_;
494 }; 546 };
495 547
496 TEST_F(ViEEncoderTest, EncodeOneFrame) { 548 TEST_F(ViEEncoderTest, EncodeOneFrame) {
497 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 549 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
498 rtc::Event frame_destroyed_event(false, false); 550 rtc::Event frame_destroyed_event(false, false);
499 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); 551 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
500 sink_.WaitForEncodedFrame(1); 552 WaitForEncodedFrame(1);
501 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); 553 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
502 vie_encoder_->Stop(); 554 vie_encoder_->Stop();
503 } 555 }
504 556
505 TEST_F(ViEEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) { 557 TEST_F(ViEEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) {
506 // Dropped since no target bitrate has been set. 558 // Dropped since no target bitrate has been set.
507 rtc::Event frame_destroyed_event(false, false); 559 rtc::Event frame_destroyed_event(false, false);
508 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); 560 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
509 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); 561 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
510 562
511 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 563 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
512 564
513 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 565 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
514 sink_.WaitForEncodedFrame(2); 566 WaitForEncodedFrame(2);
515 vie_encoder_->Stop(); 567 vie_encoder_->Stop();
516 } 568 }
517 569
518 TEST_F(ViEEncoderTest, DropsFramesWhenRateSetToZero) { 570 TEST_F(ViEEncoderTest, DropsFramesWhenRateSetToZero) {
519 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 571 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
520 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 572 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
521 sink_.WaitForEncodedFrame(1); 573 WaitForEncodedFrame(1);
522 574
523 vie_encoder_->OnBitrateUpdated(0, 0, 0); 575 vie_encoder_->OnBitrateUpdated(0, 0, 0);
524 // Dropped since bitrate is zero. 576 // Dropped since bitrate is zero.
525 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 577 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
526 578
527 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 579 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
528 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); 580 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
529 sink_.WaitForEncodedFrame(3); 581 WaitForEncodedFrame(3);
530 vie_encoder_->Stop(); 582 vie_encoder_->Stop();
531 } 583 }
532 584
533 TEST_F(ViEEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) { 585 TEST_F(ViEEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) {
534 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 586 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
535 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 587 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
536 sink_.WaitForEncodedFrame(1); 588 WaitForEncodedFrame(1);
537 589
538 // This frame will be dropped since it has the same ntp timestamp. 590 // This frame will be dropped since it has the same ntp timestamp.
539 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 591 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
540 592
541 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 593 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
542 sink_.WaitForEncodedFrame(2); 594 WaitForEncodedFrame(2);
543 vie_encoder_->Stop(); 595 vie_encoder_->Stop();
544 } 596 }
545 597
546 TEST_F(ViEEncoderTest, DropsFrameAfterStop) { 598 TEST_F(ViEEncoderTest, DropsFrameAfterStop) {
547 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 599 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
548 600
549 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 601 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
550 sink_.WaitForEncodedFrame(1); 602 WaitForEncodedFrame(1);
551 603
552 vie_encoder_->Stop(); 604 vie_encoder_->Stop();
553 sink_.SetExpectNoFrames(); 605 sink_.SetExpectNoFrames();
554 rtc::Event frame_destroyed_event(false, false); 606 rtc::Event frame_destroyed_event(false, false);
555 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event)); 607 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event));
556 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); 608 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs));
557 } 609 }
558 610
559 TEST_F(ViEEncoderTest, DropsPendingFramesOnSlowEncode) { 611 TEST_F(ViEEncoderTest, DropsPendingFramesOnSlowEncode) {
560 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 612 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
561 613
562 fake_encoder_.BlockNextEncode(); 614 fake_encoder_.BlockNextEncode();
563 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 615 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
564 sink_.WaitForEncodedFrame(1); 616 WaitForEncodedFrame(1);
565 // Here, the encoder thread will be blocked in the TestEncoder waiting for a 617 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
566 // call to ContinueEncode. 618 // call to ContinueEncode.
567 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 619 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
568 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); 620 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
569 fake_encoder_.ContinueEncode(); 621 fake_encoder_.ContinueEncode();
570 sink_.WaitForEncodedFrame(3); 622 WaitForEncodedFrame(3);
571 623
572 vie_encoder_->Stop(); 624 vie_encoder_->Stop();
573 } 625 }
574 626
575 TEST_F(ViEEncoderTest, ConfigureEncoderTriggersOnEncoderConfigurationChanged) { 627 TEST_F(ViEEncoderTest, ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
576 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 628 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
577 EXPECT_EQ(0, sink_.number_of_reconfigurations()); 629 EXPECT_EQ(0, sink_.number_of_reconfigurations());
578 630
579 // Capture a frame and wait for it to synchronize with the encoder thread. 631 // Capture a frame and wait for it to synchronize with the encoder thread.
580 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 632 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
581 sink_.WaitForEncodedFrame(1); 633 WaitForEncodedFrame(1);
582 // The encoder will have been configured once when the first frame is 634 // The encoder will have been configured once when the first frame is
583 // received. 635 // received.
584 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 636 EXPECT_EQ(1, sink_.number_of_reconfigurations());
585 637
586 VideoEncoderConfig video_encoder_config; 638 VideoEncoderConfig video_encoder_config;
587 test::FillEncoderConfiguration(1, &video_encoder_config); 639 test::FillEncoderConfiguration(1, &video_encoder_config);
588 video_encoder_config.min_transmit_bitrate_bps = 9999; 640 video_encoder_config.min_transmit_bitrate_bps = 9999;
589 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 641 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config),
590 kMaxPayloadLength, true /* nack_enabled */); 642 kMaxPayloadLength, true /* nack_enabled */);
591 643
592 // Capture a frame and wait for it to synchronize with the encoder thread. 644 // Capture a frame and wait for it to synchronize with the encoder thread.
593 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 645 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
594 sink_.WaitForEncodedFrame(2); 646 WaitForEncodedFrame(2);
595 EXPECT_EQ(2, sink_.number_of_reconfigurations()); 647 EXPECT_EQ(2, sink_.number_of_reconfigurations());
596 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate()); 648 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
597 649
598 vie_encoder_->Stop(); 650 vie_encoder_->Stop();
599 } 651 }
600 652
601 TEST_F(ViEEncoderTest, FrameResolutionChangeReconfigureEncoder) { 653 TEST_F(ViEEncoderTest, FrameResolutionChangeReconfigureEncoder) {
602 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 654 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
603 655
604 // Capture a frame and wait for it to synchronize with the encoder thread. 656 // Capture a frame and wait for it to synchronize with the encoder thread.
605 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 657 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
606 sink_.WaitForEncodedFrame(1); 658 WaitForEncodedFrame(1);
607 // The encoder will have been configured once. 659 // The encoder will have been configured once.
608 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 660 EXPECT_EQ(1, sink_.number_of_reconfigurations());
609 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); 661 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
610 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); 662 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
611 663
612 codec_width_ *= 2; 664 codec_width_ *= 2;
613 codec_height_ *= 2; 665 codec_height_ *= 2;
614 // Capture a frame with a higher resolution and wait for it to synchronize 666 // Capture a frame with a higher resolution and wait for it to synchronize
615 // with the encoder thread. 667 // with the encoder thread.
616 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 668 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
617 sink_.WaitForEncodedFrame(2); 669 WaitForEncodedFrame(2);
618 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); 670 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width);
619 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); 671 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height);
620 EXPECT_EQ(2, sink_.number_of_reconfigurations()); 672 EXPECT_EQ(2, sink_.number_of_reconfigurations());
621 673
622 vie_encoder_->Stop(); 674 vie_encoder_->Stop();
623 } 675 }
624 676
625 TEST_F(ViEEncoderTest, Vp8ResilienceIsOffFor1S1TLWithNackEnabled) { 677 TEST_F(ViEEncoderTest, Vp8ResilienceIsOffFor1S1TLWithNackEnabled) {
626 const bool kNackEnabled = true; 678 const bool kNackEnabled = true;
627 const size_t kNumStreams = 1; 679 const size_t kNumStreams = 1;
628 const size_t kNumTl = 1; 680 const size_t kNumTl = 1;
629 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled); 681 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled, false);
630 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 682 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
631 683
632 // Capture a frame and wait for it to synchronize with the encoder thread. 684 // Capture a frame and wait for it to synchronize with the encoder thread.
633 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 685 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
634 sink_.WaitForEncodedFrame(1); 686 WaitForEncodedFrame(1);
635 // The encoder have been configured once when the first frame is received. 687 // The encoder have been configured once when the first frame is received.
636 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 688 EXPECT_EQ(1, sink_.number_of_reconfigurations());
637 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType); 689 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType);
638 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 690 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
639 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 691 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
640 // Resilience is off for no temporal layers with nack on. 692 // Resilience is off for no temporal layers with nack on.
641 EXPECT_EQ(kResilienceOff, fake_encoder_.codec_config().VP8()->resilience); 693 EXPECT_EQ(kResilienceOff, fake_encoder_.codec_config().VP8()->resilience);
642 vie_encoder_->Stop(); 694 vie_encoder_->Stop();
643 } 695 }
644 696
645 TEST_F(ViEEncoderTest, Vp8ResilienceIsOffFor2S1TlWithNackEnabled) { 697 TEST_F(ViEEncoderTest, Vp8ResilienceIsOffFor2S1TlWithNackEnabled) {
646 const bool kNackEnabled = true; 698 const bool kNackEnabled = true;
647 const size_t kNumStreams = 2; 699 const size_t kNumStreams = 2;
648 const size_t kNumTl = 1; 700 const size_t kNumTl = 1;
649 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled); 701 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled, false);
650 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 702 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
651 703
652 // Capture a frame and wait for it to synchronize with the encoder thread. 704 // Capture a frame and wait for it to synchronize with the encoder thread.
653 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 705 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
654 sink_.WaitForEncodedFrame(1); 706 WaitForEncodedFrame(1);
655 // The encoder have been configured once when the first frame is received. 707 // The encoder have been configured once when the first frame is received.
656 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 708 EXPECT_EQ(1, sink_.number_of_reconfigurations());
657 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType); 709 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType);
658 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 710 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
659 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 711 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
660 // Resilience is off for no temporal layers and >1 streams with nack on. 712 // Resilience is off for no temporal layers and >1 streams with nack on.
661 EXPECT_EQ(kResilienceOff, fake_encoder_.codec_config().VP8()->resilience); 713 EXPECT_EQ(kResilienceOff, fake_encoder_.codec_config().VP8()->resilience);
662 vie_encoder_->Stop(); 714 vie_encoder_->Stop();
663 } 715 }
664 716
665 TEST_F(ViEEncoderTest, Vp8ResilienceIsOnFor1S1TLWithNackDisabled) { 717 TEST_F(ViEEncoderTest, Vp8ResilienceIsOnFor1S1TLWithNackDisabled) {
666 const bool kNackEnabled = false; 718 const bool kNackEnabled = false;
667 const size_t kNumStreams = 1; 719 const size_t kNumStreams = 1;
668 const size_t kNumTl = 1; 720 const size_t kNumTl = 1;
669 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled); 721 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled, false);
670 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 722 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
671 723
672 // Capture a frame and wait for it to synchronize with the encoder thread. 724 // Capture a frame and wait for it to synchronize with the encoder thread.
673 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 725 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
674 sink_.WaitForEncodedFrame(1); 726 WaitForEncodedFrame(1);
675 // The encoder have been configured once when the first frame is received. 727 // The encoder have been configured once when the first frame is received.
676 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 728 EXPECT_EQ(1, sink_.number_of_reconfigurations());
677 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType); 729 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType);
678 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 730 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
679 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 731 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
680 // Resilience is on for no temporal layers with nack off. 732 // Resilience is on for no temporal layers with nack off.
681 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience); 733 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience);
682 vie_encoder_->Stop(); 734 vie_encoder_->Stop();
683 } 735 }
684 736
685 TEST_F(ViEEncoderTest, Vp8ResilienceIsOnFor1S2TlWithNackEnabled) { 737 TEST_F(ViEEncoderTest, Vp8ResilienceIsOnFor1S2TlWithNackEnabled) {
686 const bool kNackEnabled = true; 738 const bool kNackEnabled = true;
687 const size_t kNumStreams = 1; 739 const size_t kNumStreams = 1;
688 const size_t kNumTl = 2; 740 const size_t kNumTl = 2;
689 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled); 741 ResetEncoder("VP8", kNumStreams, kNumTl, kNackEnabled, false);
690 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 742 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
691 743
692 // Capture a frame and wait for it to synchronize with the encoder thread. 744 // Capture a frame and wait for it to synchronize with the encoder thread.
693 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); 745 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
694 sink_.WaitForEncodedFrame(1); 746 WaitForEncodedFrame(1);
695 // The encoder have been configured once when the first frame is received. 747 // The encoder have been configured once when the first frame is received.
696 EXPECT_EQ(1, sink_.number_of_reconfigurations()); 748 EXPECT_EQ(1, sink_.number_of_reconfigurations());
697 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType); 749 EXPECT_EQ(kVideoCodecVP8, fake_encoder_.codec_config().codecType);
698 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 750 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
699 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 751 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
700 // Resilience is on for temporal layers. 752 // Resilience is on for temporal layers.
701 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience); 753 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience);
702 vie_encoder_->Stop(); 754 vie_encoder_->Stop();
703 } 755 }
704 756
(...skipping 23 matching lines...) Expand all
728 VerifyNoLimitation(video_source_.sink_wants()); 780 VerifyNoLimitation(video_source_.sink_wants());
729 781
730 int frame_width = 1280; 782 int frame_width = 1280;
731 int frame_height = 720; 783 int frame_height = 720;
732 784
733 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should 785 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should
734 // request lower resolution. 786 // request lower resolution.
735 for (int i = 1; i <= kMaxDowngrades; ++i) { 787 for (int i = 1; i <= kMaxDowngrades; ++i) {
736 video_source_.IncomingCapturedFrame( 788 video_source_.IncomingCapturedFrame(
737 CreateFrame(i, frame_width, frame_height)); 789 CreateFrame(i, frame_width, frame_height));
738 sink_.WaitForEncodedFrame(i); 790 WaitForEncodedFrame(i);
739 791
740 vie_encoder_->TriggerCpuOveruse(); 792 vie_encoder_->TriggerCpuOveruse();
741 793
742 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 794 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
743 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 795 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
744 frame_width * frame_height); 796 frame_width * frame_height);
745 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 797 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
746 EXPECT_EQ(i, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 798 EXPECT_EQ(i, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
747 799
748 frame_width /= 2; 800 frame_width /= 2;
749 frame_height /= 2; 801 frame_height /= 2;
750 } 802 }
751 803
752 // Trigger CPU overuse one more time. This should not trigger a request for 804 // Trigger CPU overuse one more time. This should not trigger a request for
753 // lower resolution. 805 // lower resolution.
754 rtc::VideoSinkWants current_wants = video_source_.sink_wants(); 806 rtc::VideoSinkWants current_wants = video_source_.sink_wants();
755 video_source_.IncomingCapturedFrame( 807 video_source_.IncomingCapturedFrame(
756 CreateFrame(kMaxDowngrades + 1, frame_width, frame_height)); 808 CreateFrame(kMaxDowngrades + 1, frame_width, frame_height));
757 sink_.WaitForEncodedFrame(kMaxDowngrades + 1); 809 WaitForEncodedFrame(kMaxDowngrades + 1);
758 vie_encoder_->TriggerCpuOveruse(); 810 vie_encoder_->TriggerCpuOveruse();
759 EXPECT_EQ(video_source_.sink_wants().target_pixel_count, 811 EXPECT_EQ(video_source_.sink_wants().target_pixel_count,
760 current_wants.target_pixel_count); 812 current_wants.target_pixel_count);
761 EXPECT_EQ(video_source_.sink_wants().max_pixel_count, 813 EXPECT_EQ(video_source_.sink_wants().max_pixel_count,
762 current_wants.max_pixel_count); 814 current_wants.max_pixel_count);
763 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 815 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
764 EXPECT_EQ(kMaxDowngrades, 816 EXPECT_EQ(kMaxDowngrades,
765 stats_proxy_->GetStats().number_of_cpu_adapt_changes); 817 stats_proxy_->GetStats().number_of_cpu_adapt_changes);
766 818
767 // Trigger CPU normal use. 819 // Trigger CPU normal use.
(...skipping 14 matching lines...) Expand all
782 VerifyNoLimitation(video_source_.sink_wants()); 834 VerifyNoLimitation(video_source_.sink_wants());
783 835
784 const int kFrameWidth = 1280; 836 const int kFrameWidth = 1280;
785 const int kFrameHeight = 720; 837 const int kFrameHeight = 720;
786 const int kFrameIntervalMs = 1000 / 30; 838 const int kFrameIntervalMs = 1000 / 30;
787 839
788 int frame_timestamp = 1; 840 int frame_timestamp = 1;
789 841
790 video_source_.IncomingCapturedFrame( 842 video_source_.IncomingCapturedFrame(
791 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); 843 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
792 sink_.WaitForEncodedFrame(frame_timestamp); 844 WaitForEncodedFrame(frame_timestamp);
793 frame_timestamp += kFrameIntervalMs; 845 frame_timestamp += kFrameIntervalMs;
794 846
795 // Trigger CPU overuse. 847 // Trigger CPU overuse.
796 vie_encoder_->TriggerCpuOveruse(); 848 vie_encoder_->TriggerCpuOveruse();
797 video_source_.IncomingCapturedFrame( 849 video_source_.IncomingCapturedFrame(
798 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); 850 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
799 sink_.WaitForEncodedFrame(frame_timestamp); 851 WaitForEncodedFrame(frame_timestamp);
800 frame_timestamp += kFrameIntervalMs; 852 frame_timestamp += kFrameIntervalMs;
801 853
802 // Default degradation preference is maintain-framerate, so will lower max 854 // Default degradation preference is maintain-framerate, so will lower max
803 // wanted resolution. 855 // wanted resolution.
804 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 856 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
805 EXPECT_LT(video_source_.sink_wants().max_pixel_count, 857 EXPECT_LT(video_source_.sink_wants().max_pixel_count,
806 kFrameWidth * kFrameHeight); 858 kFrameWidth * kFrameHeight);
807 EXPECT_EQ(std::numeric_limits<int>::max(), 859 EXPECT_EQ(std::numeric_limits<int>::max(),
808 video_source_.sink_wants().max_framerate_fps); 860 video_source_.sink_wants().max_framerate_fps);
809 861
810 // Set new source, switch to maintain-resolution. 862 // Set new source, switch to maintain-resolution.
811 test::FrameForwarder new_video_source; 863 test::FrameForwarder new_video_source;
812 vie_encoder_->SetSource( 864 vie_encoder_->SetSource(
813 &new_video_source, 865 &new_video_source,
814 VideoSendStream::DegradationPreference::kMaintainResolution); 866 VideoSendStream::DegradationPreference::kMaintainResolution);
815 867
816 // Initially no degradation registered. 868 // Initially no degradation registered.
817 VerifyNoLimitation(new_video_source.sink_wants()); 869 VerifyNoLimitation(new_video_source.sink_wants());
818 870
819 // Force an input frame rate to be available, or the adaptation call won't 871 // Force an input frame rate to be available, or the adaptation call won't
820 // know what framerate to adapt form. 872 // know what framerate to adapt form.
821 const int kInputFps = 30; 873 const int kInputFps = 30;
822 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 874 VideoSendStream::Stats stats = stats_proxy_->GetStats();
823 stats.input_frame_rate = kInputFps; 875 stats.input_frame_rate = kInputFps;
824 stats_proxy_->SetMockStats(stats); 876 stats_proxy_->SetMockStats(stats);
825 877
826 vie_encoder_->TriggerCpuOveruse(); 878 vie_encoder_->TriggerCpuOveruse();
827 new_video_source.IncomingCapturedFrame( 879 new_video_source.IncomingCapturedFrame(
828 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); 880 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
829 sink_.WaitForEncodedFrame(frame_timestamp); 881 WaitForEncodedFrame(frame_timestamp);
830 frame_timestamp += kFrameIntervalMs; 882 frame_timestamp += kFrameIntervalMs;
831 883
832 // Some framerate constraint should be set. 884 // Some framerate constraint should be set.
833 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 885 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
834 EXPECT_EQ(std::numeric_limits<int>::max(), 886 EXPECT_EQ(std::numeric_limits<int>::max(),
835 new_video_source.sink_wants().max_pixel_count); 887 new_video_source.sink_wants().max_pixel_count);
836 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps); 888 EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps);
837 889
838 // Turn off degradation completely. 890 // Turn off degradation completely.
839 vie_encoder_->SetSource( 891 vie_encoder_->SetSource(
840 &new_video_source, 892 &new_video_source,
841 VideoSendStream::DegradationPreference::kDegradationDisabled); 893 VideoSendStream::DegradationPreference::kDegradationDisabled);
842 VerifyNoLimitation(new_video_source.sink_wants()); 894 VerifyNoLimitation(new_video_source.sink_wants());
843 895
844 vie_encoder_->TriggerCpuOveruse(); 896 vie_encoder_->TriggerCpuOveruse();
845 new_video_source.IncomingCapturedFrame( 897 new_video_source.IncomingCapturedFrame(
846 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); 898 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
847 sink_.WaitForEncodedFrame(frame_timestamp); 899 WaitForEncodedFrame(frame_timestamp);
848 frame_timestamp += kFrameIntervalMs; 900 frame_timestamp += kFrameIntervalMs;
849 901
850 // Still no degradation. 902 // Still no degradation.
851 VerifyNoLimitation(new_video_source.sink_wants()); 903 VerifyNoLimitation(new_video_source.sink_wants());
852 904
853 // Calling SetSource with resolution scaling enabled apply the old SinkWants. 905 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
854 vie_encoder_->SetSource( 906 vie_encoder_->SetSource(
855 &new_video_source, 907 &new_video_source,
856 VideoSendStream::DegradationPreference::kMaintainFramerate); 908 VideoSendStream::DegradationPreference::kMaintainFramerate);
857 EXPECT_LT(new_video_source.sink_wants().max_pixel_count, 909 EXPECT_LT(new_video_source.sink_wants().max_pixel_count,
(...skipping 13 matching lines...) Expand all
871 923
872 vie_encoder_->Stop(); 924 vie_encoder_->Stop();
873 } 925 }
874 926
875 TEST_F(ViEEncoderTest, StatsTracksQualityAdaptationStats) { 927 TEST_F(ViEEncoderTest, StatsTracksQualityAdaptationStats) {
876 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 928 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
877 929
878 const int kWidth = 1280; 930 const int kWidth = 1280;
879 const int kHeight = 720; 931 const int kHeight = 720;
880 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 932 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
881 sink_.WaitForEncodedFrame(1); 933 WaitForEncodedFrame(1);
882 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 934 VideoSendStream::Stats stats = stats_proxy_->GetStats();
883 EXPECT_FALSE(stats.bw_limited_resolution); 935 EXPECT_FALSE(stats.bw_limited_resolution);
884 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 936 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
885 937
886 // Trigger adapt down. 938 // Trigger adapt down.
887 vie_encoder_->TriggerQualityLow(); 939 vie_encoder_->TriggerQualityLow();
888 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 940 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
889 sink_.WaitForEncodedFrame(2); 941 WaitForEncodedFrame(2);
890 942
891 stats = stats_proxy_->GetStats(); 943 stats = stats_proxy_->GetStats();
892 EXPECT_TRUE(stats.bw_limited_resolution); 944 EXPECT_TRUE(stats.bw_limited_resolution);
893 EXPECT_EQ(1, stats.number_of_quality_adapt_changes); 945 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
894 946
895 // Trigger adapt up. 947 // Trigger adapt up.
896 vie_encoder_->TriggerQualityHigh(); 948 vie_encoder_->TriggerQualityHigh();
897 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 949 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
898 sink_.WaitForEncodedFrame(3); 950 WaitForEncodedFrame(3);
899 951
900 stats = stats_proxy_->GetStats(); 952 stats = stats_proxy_->GetStats();
901 EXPECT_FALSE(stats.bw_limited_resolution); 953 EXPECT_FALSE(stats.bw_limited_resolution);
902 EXPECT_EQ(2, stats.number_of_quality_adapt_changes); 954 EXPECT_EQ(2, stats.number_of_quality_adapt_changes);
903 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); 955 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
904 956
905 vie_encoder_->Stop(); 957 vie_encoder_->Stop();
906 } 958 }
907 959
908 TEST_F(ViEEncoderTest, StatsTracksCpuAdaptationStats) { 960 TEST_F(ViEEncoderTest, StatsTracksCpuAdaptationStats) {
909 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 961 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
910 962
911 const int kWidth = 1280; 963 const int kWidth = 1280;
912 const int kHeight = 720; 964 const int kHeight = 720;
913 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 965 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
914 sink_.WaitForEncodedFrame(1); 966 WaitForEncodedFrame(1);
915 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 967 VideoSendStream::Stats stats = stats_proxy_->GetStats();
916 EXPECT_FALSE(stats.cpu_limited_resolution); 968 EXPECT_FALSE(stats.cpu_limited_resolution);
917 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); 969 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
918 970
919 // Trigger CPU overuse. 971 // Trigger CPU overuse.
920 vie_encoder_->TriggerCpuOveruse(); 972 vie_encoder_->TriggerCpuOveruse();
921 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 973 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
922 sink_.WaitForEncodedFrame(2); 974 WaitForEncodedFrame(2);
923 975
924 stats = stats_proxy_->GetStats(); 976 stats = stats_proxy_->GetStats();
925 EXPECT_TRUE(stats.cpu_limited_resolution); 977 EXPECT_TRUE(stats.cpu_limited_resolution);
926 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 978 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
927 979
928 // Trigger CPU normal use. 980 // Trigger CPU normal use.
929 vie_encoder_->TriggerCpuNormalUsage(); 981 vie_encoder_->TriggerCpuNormalUsage();
930 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 982 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
931 sink_.WaitForEncodedFrame(3); 983 WaitForEncodedFrame(3);
932 984
933 stats = stats_proxy_->GetStats(); 985 stats = stats_proxy_->GetStats();
934 EXPECT_FALSE(stats.cpu_limited_resolution); 986 EXPECT_FALSE(stats.cpu_limited_resolution);
935 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 987 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
936 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 988 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
937 989
938 vie_encoder_->Stop(); 990 vie_encoder_->Stop();
939 } 991 }
940 992
941 TEST_F(ViEEncoderTest, SwitchingSourceKeepsCpuAdaptation) { 993 TEST_F(ViEEncoderTest, SwitchingSourceKeepsCpuAdaptation) {
942 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 994 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
943 995
944 const int kWidth = 1280; 996 const int kWidth = 1280;
945 const int kHeight = 720; 997 const int kHeight = 720;
946 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 998 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
947 sink_.WaitForEncodedFrame(1); 999 WaitForEncodedFrame(1);
948 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1000 VideoSendStream::Stats stats = stats_proxy_->GetStats();
949 EXPECT_FALSE(stats.cpu_limited_resolution); 1001 EXPECT_FALSE(stats.cpu_limited_resolution);
950 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); 1002 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
951 1003
952 // Trigger CPU overuse. 1004 // Trigger CPU overuse.
953 vie_encoder_->TriggerCpuOveruse(); 1005 vie_encoder_->TriggerCpuOveruse();
954 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1006 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
955 sink_.WaitForEncodedFrame(2); 1007 WaitForEncodedFrame(2);
956 stats = stats_proxy_->GetStats(); 1008 stats = stats_proxy_->GetStats();
957 EXPECT_TRUE(stats.cpu_limited_resolution); 1009 EXPECT_TRUE(stats.cpu_limited_resolution);
958 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1010 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
959 1011
960 // Set new source with adaptation still enabled. 1012 // Set new source with adaptation still enabled.
961 test::FrameForwarder new_video_source; 1013 test::FrameForwarder new_video_source;
962 vie_encoder_->SetSource( 1014 vie_encoder_->SetSource(
963 &new_video_source, 1015 &new_video_source,
964 VideoSendStream::DegradationPreference::kMaintainFramerate); 1016 VideoSendStream::DegradationPreference::kMaintainFramerate);
965 1017
966 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1018 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
967 sink_.WaitForEncodedFrame(3); 1019 WaitForEncodedFrame(3);
968 stats = stats_proxy_->GetStats(); 1020 stats = stats_proxy_->GetStats();
969 EXPECT_TRUE(stats.cpu_limited_resolution); 1021 EXPECT_TRUE(stats.cpu_limited_resolution);
970 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1022 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
971 1023
972 // Set adaptation disabled. 1024 // Set adaptation disabled.
973 vie_encoder_->SetSource( 1025 vie_encoder_->SetSource(
974 &new_video_source, 1026 &new_video_source,
975 VideoSendStream::DegradationPreference::kDegradationDisabled); 1027 VideoSendStream::DegradationPreference::kDegradationDisabled);
976 1028
977 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1029 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
978 sink_.WaitForEncodedFrame(4); 1030 WaitForEncodedFrame(4);
979 stats = stats_proxy_->GetStats(); 1031 stats = stats_proxy_->GetStats();
980 EXPECT_FALSE(stats.cpu_limited_resolution); 1032 EXPECT_FALSE(stats.cpu_limited_resolution);
981 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1033 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
982 1034
983 // Set adaptation back to enabled. 1035 // Set adaptation back to enabled.
984 vie_encoder_->SetSource( 1036 vie_encoder_->SetSource(
985 &new_video_source, 1037 &new_video_source,
986 VideoSendStream::DegradationPreference::kMaintainFramerate); 1038 VideoSendStream::DegradationPreference::kMaintainFramerate);
987 1039
988 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); 1040 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
989 sink_.WaitForEncodedFrame(5); 1041 WaitForEncodedFrame(5);
990 stats = stats_proxy_->GetStats(); 1042 stats = stats_proxy_->GetStats();
991 EXPECT_TRUE(stats.cpu_limited_resolution); 1043 EXPECT_TRUE(stats.cpu_limited_resolution);
992 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1044 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
993 1045
994 // Trigger CPU normal use. 1046 // Trigger CPU normal use.
995 vie_encoder_->TriggerCpuNormalUsage(); 1047 vie_encoder_->TriggerCpuNormalUsage();
996 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight)); 1048 new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
997 sink_.WaitForEncodedFrame(6); 1049 WaitForEncodedFrame(6);
998 stats = stats_proxy_->GetStats(); 1050 stats = stats_proxy_->GetStats();
999 EXPECT_FALSE(stats.cpu_limited_resolution); 1051 EXPECT_FALSE(stats.cpu_limited_resolution);
1000 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1052 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1001 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 1053 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1002 1054
1003 vie_encoder_->Stop(); 1055 vie_encoder_->Stop();
1004 } 1056 }
1005 1057
1006 TEST_F(ViEEncoderTest, SwitchingSourceKeepsQualityAdaptation) { 1058 TEST_F(ViEEncoderTest, SwitchingSourceKeepsQualityAdaptation) {
1007 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1059 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1008 1060
1009 const int kWidth = 1280; 1061 const int kWidth = 1280;
1010 const int kHeight = 720; 1062 const int kHeight = 720;
1011 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1063 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1012 sink_.WaitForEncodedFrame(1); 1064 WaitForEncodedFrame(1);
1013 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1065 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1014 EXPECT_FALSE(stats.cpu_limited_resolution); 1066 EXPECT_FALSE(stats.cpu_limited_resolution);
1015 EXPECT_FALSE(stats.bw_limited_resolution); 1067 EXPECT_FALSE(stats.bw_limited_resolution);
1016 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 1068 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1017 1069
1018 // Set new source with adaptation still enabled. 1070 // Set new source with adaptation still enabled.
1019 test::FrameForwarder new_video_source; 1071 test::FrameForwarder new_video_source;
1020 vie_encoder_->SetSource(&new_video_source, 1072 vie_encoder_->SetSource(&new_video_source,
1021 VideoSendStream::DegradationPreference::kBalanced); 1073 VideoSendStream::DegradationPreference::kBalanced);
1022 1074
1023 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1075 new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1024 sink_.WaitForEncodedFrame(2); 1076 WaitForEncodedFrame(2);
1025 stats = stats_proxy_->GetStats(); 1077 stats = stats_proxy_->GetStats();
1026 EXPECT_FALSE(stats.cpu_limited_resolution); 1078 EXPECT_FALSE(stats.cpu_limited_resolution);
1027 EXPECT_FALSE(stats.bw_limited_resolution); 1079 EXPECT_FALSE(stats.bw_limited_resolution);
1028 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 1080 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1029 1081
1030 // Trigger adapt down. 1082 // Trigger adapt down.
1031 vie_encoder_->TriggerQualityLow(); 1083 vie_encoder_->TriggerQualityLow();
1032 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1084 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
1033 sink_.WaitForEncodedFrame(3); 1085 WaitForEncodedFrame(3);
1034 stats = stats_proxy_->GetStats(); 1086 stats = stats_proxy_->GetStats();
1035 EXPECT_FALSE(stats.cpu_limited_resolution); 1087 EXPECT_FALSE(stats.cpu_limited_resolution);
1036 EXPECT_TRUE(stats.bw_limited_resolution); 1088 EXPECT_TRUE(stats.bw_limited_resolution);
1037 EXPECT_EQ(1, stats.number_of_quality_adapt_changes); 1089 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1038 1090
1039 // Set new source with adaptation still enabled. 1091 // Set new source with adaptation still enabled.
1040 vie_encoder_->SetSource(&new_video_source, 1092 vie_encoder_->SetSource(&new_video_source,
1041 VideoSendStream::DegradationPreference::kBalanced); 1093 VideoSendStream::DegradationPreference::kBalanced);
1042 1094
1043 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1095 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
1044 sink_.WaitForEncodedFrame(4); 1096 WaitForEncodedFrame(4);
1045 stats = stats_proxy_->GetStats(); 1097 stats = stats_proxy_->GetStats();
1046 EXPECT_FALSE(stats.cpu_limited_resolution); 1098 EXPECT_FALSE(stats.cpu_limited_resolution);
1047 EXPECT_TRUE(stats.bw_limited_resolution); 1099 EXPECT_TRUE(stats.bw_limited_resolution);
1048 EXPECT_EQ(1, stats.number_of_quality_adapt_changes); 1100 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1049 1101
1050 // Disable resolution scaling. 1102 // Disable resolution scaling.
1051 vie_encoder_->SetSource( 1103 vie_encoder_->SetSource(
1052 &new_video_source, 1104 &new_video_source,
1053 VideoSendStream::DegradationPreference::kMaintainResolution); 1105 VideoSendStream::DegradationPreference::kMaintainResolution);
1054 1106
1055 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); 1107 new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
1056 sink_.WaitForEncodedFrame(5); 1108 WaitForEncodedFrame(5);
1057 stats = stats_proxy_->GetStats(); 1109 stats = stats_proxy_->GetStats();
1058 EXPECT_FALSE(stats.cpu_limited_resolution); 1110 EXPECT_FALSE(stats.cpu_limited_resolution);
1059 EXPECT_FALSE(stats.bw_limited_resolution); 1111 EXPECT_FALSE(stats.bw_limited_resolution);
1060 EXPECT_EQ(1, stats.number_of_quality_adapt_changes); 1112 EXPECT_EQ(1, stats.number_of_quality_adapt_changes);
1061 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); 1113 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1062 1114
1063 vie_encoder_->Stop(); 1115 vie_encoder_->Stop();
1064 } 1116 }
1065 1117
1066 TEST_F(ViEEncoderTest, QualityAdaptationStatsAreResetWhenScalerIsDisabled) { 1118 TEST_F(ViEEncoderTest, QualityAdaptationStatsAreResetWhenScalerIsDisabled) {
1067 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1119 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1068 1120
1069 const int kWidth = 1280; 1121 const int kWidth = 1280;
1070 const int kHeight = 720; 1122 const int kHeight = 720;
1071 video_source_.set_adaptation_enabled(true); 1123 video_source_.set_adaptation_enabled(true);
1072 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1124 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1073 sink_.WaitForEncodedFrame(1); 1125 WaitForEncodedFrame(1);
1074 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1126 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1075 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1127 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1076 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1128 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1077 1129
1078 // Trigger adapt down. 1130 // Trigger adapt down.
1079 vie_encoder_->TriggerQualityLow(); 1131 vie_encoder_->TriggerQualityLow();
1080 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1132 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1081 sink_.WaitForEncodedFrame(2); 1133 WaitForEncodedFrame(2);
1082 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1134 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1083 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1135 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1084 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1136 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1085 1137
1086 // Trigger overuse. 1138 // Trigger overuse.
1087 vie_encoder_->TriggerCpuOveruse(); 1139 vie_encoder_->TriggerCpuOveruse();
1088 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1140 video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
1089 sink_.WaitForEncodedFrame(3); 1141 WaitForEncodedFrame(3);
1090 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1142 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1091 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1143 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1092 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1144 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1093 1145
1094 // Set source with adaptation still enabled but quality scaler is off. 1146 // Set source with adaptation still enabled but quality scaler is off.
1095 fake_encoder_.SetQualityScaling(false); 1147 fake_encoder_.SetQualityScaling(false);
1096 vie_encoder_->SetSource( 1148 vie_encoder_->SetSource(
1097 &video_source_, 1149 &video_source_,
1098 VideoSendStream::DegradationPreference::kMaintainFramerate); 1150 VideoSendStream::DegradationPreference::kMaintainFramerate);
1099 1151
1100 video_source_.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1152 video_source_.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
1101 sink_.WaitForEncodedFrame(4); 1153 WaitForEncodedFrame(4);
1102 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1154 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1103 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1155 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1104 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1156 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1105 1157
1106 vie_encoder_->Stop(); 1158 vie_encoder_->Stop();
1107 } 1159 }
1108 1160
1109 TEST_F(ViEEncoderTest, StatsTracksCpuAdaptationStatsWhenSwitchingSource) { 1161 TEST_F(ViEEncoderTest, StatsTracksCpuAdaptationStatsWhenSwitchingSource) {
1110 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1162 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1111 1163
1112 const int kWidth = 1280; 1164 const int kWidth = 1280;
1113 const int kHeight = 720; 1165 const int kHeight = 720;
1114 int sequence = 1; 1166 int sequence = 1;
1115 1167
1116 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); 1168 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
1117 sink_.WaitForEncodedFrame(sequence++); 1169 WaitForEncodedFrame(sequence++);
1118 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1170 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1119 EXPECT_FALSE(stats.cpu_limited_resolution); 1171 EXPECT_FALSE(stats.cpu_limited_resolution);
1120 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); 1172 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes);
1121 1173
1122 // Trigger CPU overuse, should now adapt down. 1174 // Trigger CPU overuse, should now adapt down.
1123 vie_encoder_->TriggerCpuOveruse(); 1175 vie_encoder_->TriggerCpuOveruse();
1124 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); 1176 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
1125 sink_.WaitForEncodedFrame(sequence++); 1177 WaitForEncodedFrame(sequence++);
1126 stats = stats_proxy_->GetStats(); 1178 stats = stats_proxy_->GetStats();
1127 EXPECT_TRUE(stats.cpu_limited_resolution); 1179 EXPECT_TRUE(stats.cpu_limited_resolution);
1128 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1180 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1129 1181
1130 // Set new source with adaptation still enabled. 1182 // Set new source with adaptation still enabled.
1131 test::FrameForwarder new_video_source; 1183 test::FrameForwarder new_video_source;
1132 vie_encoder_->SetSource( 1184 vie_encoder_->SetSource(
1133 &new_video_source, 1185 &new_video_source,
1134 VideoSendStream::DegradationPreference::kMaintainFramerate); 1186 VideoSendStream::DegradationPreference::kMaintainFramerate);
1135 1187
1136 new_video_source.IncomingCapturedFrame( 1188 new_video_source.IncomingCapturedFrame(
1137 CreateFrame(sequence, kWidth, kHeight)); 1189 CreateFrame(sequence, kWidth, kHeight));
1138 sink_.WaitForEncodedFrame(sequence++); 1190 WaitForEncodedFrame(sequence++);
1139 stats = stats_proxy_->GetStats(); 1191 stats = stats_proxy_->GetStats();
1140 EXPECT_TRUE(stats.cpu_limited_resolution); 1192 EXPECT_TRUE(stats.cpu_limited_resolution);
1141 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1193 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1142 1194
1143 // Set cpu adaptation by frame dropping. 1195 // Set cpu adaptation by frame dropping.
1144 vie_encoder_->SetSource( 1196 vie_encoder_->SetSource(
1145 &new_video_source, 1197 &new_video_source,
1146 VideoSendStream::DegradationPreference::kMaintainResolution); 1198 VideoSendStream::DegradationPreference::kMaintainResolution);
1147 new_video_source.IncomingCapturedFrame( 1199 new_video_source.IncomingCapturedFrame(
1148 CreateFrame(sequence, kWidth, kHeight)); 1200 CreateFrame(sequence, kWidth, kHeight));
1149 sink_.WaitForEncodedFrame(sequence++); 1201 WaitForEncodedFrame(sequence++);
1150 stats = stats_proxy_->GetStats(); 1202 stats = stats_proxy_->GetStats();
1151 // Not adapted at first. 1203 // Not adapted at first.
1152 EXPECT_FALSE(stats.cpu_limited_resolution); 1204 EXPECT_FALSE(stats.cpu_limited_resolution);
1153 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1205 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
1154 1206
1155 // Force an input frame rate to be available, or the adaptation call won't 1207 // Force an input frame rate to be available, or the adaptation call won't
1156 // know what framerate to adapt form. 1208 // know what framerate to adapt form.
1157 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); 1209 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1158 mock_stats.input_frame_rate = 30; 1210 mock_stats.input_frame_rate = 30;
1159 stats_proxy_->SetMockStats(mock_stats); 1211 stats_proxy_->SetMockStats(mock_stats);
1160 vie_encoder_->TriggerCpuOveruse(); 1212 vie_encoder_->TriggerCpuOveruse();
1161 stats_proxy_->ResetMockStats(); 1213 stats_proxy_->ResetMockStats();
1162 1214
1163 new_video_source.IncomingCapturedFrame( 1215 new_video_source.IncomingCapturedFrame(
1164 CreateFrame(sequence, kWidth, kHeight)); 1216 CreateFrame(sequence, kWidth, kHeight));
1165 sink_.WaitForEncodedFrame(sequence++); 1217 WaitForEncodedFrame(sequence++);
1166 1218
1167 // Framerate now adapted. 1219 // Framerate now adapted.
1168 stats = stats_proxy_->GetStats(); 1220 stats = stats_proxy_->GetStats();
1169 EXPECT_TRUE(stats.cpu_limited_resolution); 1221 EXPECT_TRUE(stats.cpu_limited_resolution);
1170 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1222 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1171 1223
1172 // Disable CPU adaptation. 1224 // Disable CPU adaptation.
1173 vie_encoder_->SetSource( 1225 vie_encoder_->SetSource(
1174 &new_video_source, 1226 &new_video_source,
1175 VideoSendStream::DegradationPreference::kDegradationDisabled); 1227 VideoSendStream::DegradationPreference::kDegradationDisabled);
1176 new_video_source.IncomingCapturedFrame( 1228 new_video_source.IncomingCapturedFrame(
1177 CreateFrame(sequence, kWidth, kHeight)); 1229 CreateFrame(sequence, kWidth, kHeight));
1178 sink_.WaitForEncodedFrame(sequence++); 1230 WaitForEncodedFrame(sequence++);
1179 1231
1180 stats = stats_proxy_->GetStats(); 1232 stats = stats_proxy_->GetStats();
1181 EXPECT_FALSE(stats.cpu_limited_resolution); 1233 EXPECT_FALSE(stats.cpu_limited_resolution);
1182 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1234 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1183 1235
1184 // Try to trigger overuse. Should not succeed. 1236 // Try to trigger overuse. Should not succeed.
1185 stats_proxy_->SetMockStats(mock_stats); 1237 stats_proxy_->SetMockStats(mock_stats);
1186 vie_encoder_->TriggerCpuOveruse(); 1238 vie_encoder_->TriggerCpuOveruse();
1187 stats_proxy_->ResetMockStats(); 1239 stats_proxy_->ResetMockStats();
1188 1240
1189 stats = stats_proxy_->GetStats(); 1241 stats = stats_proxy_->GetStats();
1190 EXPECT_FALSE(stats.cpu_limited_resolution); 1242 EXPECT_FALSE(stats.cpu_limited_resolution);
1191 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1243 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1192 1244
1193 // Switch back the source with resolution adaptation enabled. 1245 // Switch back the source with resolution adaptation enabled.
1194 vie_encoder_->SetSource( 1246 vie_encoder_->SetSource(
1195 &video_source_, 1247 &video_source_,
1196 VideoSendStream::DegradationPreference::kMaintainFramerate); 1248 VideoSendStream::DegradationPreference::kMaintainFramerate);
1197 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); 1249 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
1198 sink_.WaitForEncodedFrame(sequence++); 1250 WaitForEncodedFrame(sequence++);
1199 stats = stats_proxy_->GetStats(); 1251 stats = stats_proxy_->GetStats();
1200 EXPECT_TRUE(stats.cpu_limited_resolution); 1252 EXPECT_TRUE(stats.cpu_limited_resolution);
1201 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1253 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1202 1254
1203 // Trigger CPU normal usage. 1255 // Trigger CPU normal usage.
1204 vie_encoder_->TriggerCpuNormalUsage(); 1256 vie_encoder_->TriggerCpuNormalUsage();
1205 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); 1257 video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight));
1206 sink_.WaitForEncodedFrame(sequence++); 1258 WaitForEncodedFrame(sequence++);
1207 stats = stats_proxy_->GetStats(); 1259 stats = stats_proxy_->GetStats();
1208 EXPECT_FALSE(stats.cpu_limited_resolution); 1260 EXPECT_FALSE(stats.cpu_limited_resolution);
1209 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); 1261 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1210 1262
1211 // Back to the source with adaptation off, set it back to maintain-resolution. 1263 // Back to the source with adaptation off, set it back to maintain-resolution.
1212 vie_encoder_->SetSource( 1264 vie_encoder_->SetSource(
1213 &new_video_source, 1265 &new_video_source,
1214 VideoSendStream::DegradationPreference::kMaintainResolution); 1266 VideoSendStream::DegradationPreference::kMaintainResolution);
1215 new_video_source.IncomingCapturedFrame( 1267 new_video_source.IncomingCapturedFrame(
1216 CreateFrame(sequence, kWidth, kHeight)); 1268 CreateFrame(sequence, kWidth, kHeight));
1217 sink_.WaitForEncodedFrame(sequence++); 1269 WaitForEncodedFrame(sequence++);
1218 stats = stats_proxy_->GetStats(); 1270 stats = stats_proxy_->GetStats();
1219 // Disabled, since we previously switched the source too disabled. 1271 // Disabled, since we previously switched the source too disabled.
1220 EXPECT_FALSE(stats.cpu_limited_resolution); 1272 EXPECT_FALSE(stats.cpu_limited_resolution);
1221 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); 1273 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1222 1274
1223 // Trigger CPU normal usage. 1275 // Trigger CPU normal usage.
1224 vie_encoder_->TriggerCpuNormalUsage(); 1276 vie_encoder_->TriggerCpuNormalUsage();
1225 new_video_source.IncomingCapturedFrame( 1277 new_video_source.IncomingCapturedFrame(
1226 CreateFrame(sequence, kWidth, kHeight)); 1278 CreateFrame(sequence, kWidth, kHeight));
1227 sink_.WaitForEncodedFrame(sequence++); 1279 WaitForEncodedFrame(sequence++);
1228 stats = stats_proxy_->GetStats(); 1280 stats = stats_proxy_->GetStats();
1229 EXPECT_FALSE(stats.cpu_limited_resolution); 1281 EXPECT_FALSE(stats.cpu_limited_resolution);
1230 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes); 1282 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
1231 EXPECT_EQ(0, stats.number_of_quality_adapt_changes); 1283 EXPECT_EQ(0, stats.number_of_quality_adapt_changes);
1232 1284
1233 vie_encoder_->Stop(); 1285 vie_encoder_->Stop();
1234 } 1286 }
1235 1287
1236 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) { 1288 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) {
1237 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1289 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1238 1290
1239 const int kWidth = 1280; 1291 const int kWidth = 1280;
1240 const int kHeight = 720; 1292 const int kHeight = 720;
1241 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1293 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1242 sink_.WaitForEncodedFrame(1); 1294 WaitForEncodedFrame(1);
1243 1295
1244 VideoSendStream::Stats stats = stats_proxy_->GetStats(); 1296 VideoSendStream::Stats stats = stats_proxy_->GetStats();
1245 EXPECT_EQ(video_encoder_config_.max_bitrate_bps, 1297 EXPECT_EQ(video_encoder_config_.max_bitrate_bps,
1246 stats.preferred_media_bitrate_bps); 1298 stats.preferred_media_bitrate_bps);
1247 1299
1248 vie_encoder_->Stop(); 1300 vie_encoder_->Stop();
1249 } 1301 }
1250 1302
1251 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) { 1303 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) {
1252 const int kWidth = 1280; 1304 const int kWidth = 1280;
1253 const int kHeight = 720; 1305 const int kHeight = 720;
1254 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1306 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1255 1307
1256 // Expect no scaling to begin with. 1308 // Expect no scaling to begin with.
1257 VerifyNoLimitation(video_source_.sink_wants()); 1309 VerifyNoLimitation(video_source_.sink_wants());
1258 1310
1259 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1311 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1260 sink_.WaitForEncodedFrame(1); 1312 WaitForEncodedFrame(1);
1261 1313
1262 // Trigger scale down. 1314 // Trigger scale down.
1263 vie_encoder_->TriggerQualityLow(); 1315 vie_encoder_->TriggerQualityLow();
1264 1316
1265 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1317 video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1266 sink_.WaitForEncodedFrame(2); 1318 WaitForEncodedFrame(2);
1267 1319
1268 // Expect a scale down. 1320 // Expect a scale down.
1269 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); 1321 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count);
1270 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); 1322 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
1271 1323
1272 // Set resolution scaling disabled. 1324 // Set resolution scaling disabled.
1273 test::FrameForwarder new_video_source; 1325 test::FrameForwarder new_video_source;
1274 vie_encoder_->SetSource( 1326 vie_encoder_->SetSource(
1275 &new_video_source, 1327 &new_video_source,
1276 VideoSendStream::DegradationPreference::kMaintainResolution); 1328 VideoSendStream::DegradationPreference::kMaintainResolution);
1277 1329
1278 // Trigger scale down. 1330 // Trigger scale down.
1279 vie_encoder_->TriggerQualityLow(); 1331 vie_encoder_->TriggerQualityLow();
1280 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1332 new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
1281 sink_.WaitForEncodedFrame(3); 1333 WaitForEncodedFrame(3);
1282 1334
1283 // Expect no scaling. 1335 // Expect no scaling.
1284 EXPECT_EQ(std::numeric_limits<int>::max(), 1336 EXPECT_EQ(std::numeric_limits<int>::max(),
1285 new_video_source.sink_wants().max_pixel_count); 1337 new_video_source.sink_wants().max_pixel_count);
1286 1338
1287 // Trigger scale up. 1339 // Trigger scale up.
1288 vie_encoder_->TriggerQualityHigh(); 1340 vie_encoder_->TriggerQualityHigh();
1289 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1341 new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
1290 sink_.WaitForEncodedFrame(4); 1342 WaitForEncodedFrame(4);
1291 1343
1292 // Expect nothing to change, still no scaling. 1344 // Expect nothing to change, still no scaling.
1293 EXPECT_EQ(std::numeric_limits<int>::max(), 1345 EXPECT_EQ(std::numeric_limits<int>::max(),
1294 new_video_source.sink_wants().max_pixel_count); 1346 new_video_source.sink_wants().max_pixel_count);
1295 1347
1296 vie_encoder_->Stop(); 1348 vie_encoder_->Stop();
1297 } 1349 }
1298 1350
1299 TEST_F(ViEEncoderTest, SkipsSameAdaptDownRequest_MaintainFramerateMode) { 1351 TEST_F(ViEEncoderTest, SkipsSameAdaptDownRequest_MaintainFramerateMode) {
1300 const int kWidth = 1280; 1352 const int kWidth = 1280;
1301 const int kHeight = 720; 1353 const int kHeight = 720;
1302 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1354 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1303 1355
1304 // Enable kMaintainFramerate preference, no initial limitation. 1356 // Enable kMaintainFramerate preference, no initial limitation.
1305 test::FrameForwarder source; 1357 test::FrameForwarder source;
1306 vie_encoder_->SetSource( 1358 vie_encoder_->SetSource(
1307 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1359 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1308 1360
1309 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1361 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1310 sink_.WaitForEncodedFrame(1); 1362 WaitForEncodedFrame(1);
1311 VerifyNoLimitation(source.sink_wants()); 1363 VerifyNoLimitation(source.sink_wants());
1312 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1364 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1313 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1365 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1314 1366
1315 // Trigger adapt down, expect scaled down resolution. 1367 // Trigger adapt down, expect scaled down resolution.
1316 vie_encoder_->TriggerCpuOveruse(); 1368 vie_encoder_->TriggerCpuOveruse();
1317 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight); 1369 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight);
1318 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count; 1370 const int kLastMaxPixelCount = source.sink_wants().max_pixel_count;
1319 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1371 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1320 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1372 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
(...skipping 11 matching lines...) Expand all
1332 const int kWidth = 1280; 1384 const int kWidth = 1280;
1333 const int kHeight = 720; 1385 const int kHeight = 720;
1334 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1386 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1335 1387
1336 // Enable kMaintainFramerate preference, no initial limitation. 1388 // Enable kMaintainFramerate preference, no initial limitation.
1337 test::FrameForwarder source; 1389 test::FrameForwarder source;
1338 vie_encoder_->SetSource( 1390 vie_encoder_->SetSource(
1339 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1391 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1340 1392
1341 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1393 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1342 sink_.WaitForEncodedFrame(kWidth, kHeight); 1394 WaitForEncodedFrame(kWidth, kHeight);
1343 VerifyNoLimitation(source.sink_wants()); 1395 VerifyNoLimitation(source.sink_wants());
1344 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1396 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1345 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1397 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1346 1398
1347 // Trigger adapt up, expect no change. 1399 // Trigger adapt up, expect no change.
1348 vie_encoder_->TriggerCpuNormalUsage(); 1400 vie_encoder_->TriggerCpuNormalUsage();
1349 VerifyNoLimitation(source.sink_wants()); 1401 VerifyNoLimitation(source.sink_wants());
1350 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1402 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1351 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1403 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1352 1404
1353 vie_encoder_->Stop(); 1405 vie_encoder_->Stop();
1354 } 1406 }
1355 1407
1356 TEST_F(ViEEncoderTest, NoChangeForInitialNormalUsage_MaintainResolutionMode) { 1408 TEST_F(ViEEncoderTest, NoChangeForInitialNormalUsage_MaintainResolutionMode) {
1357 const int kWidth = 1280; 1409 const int kWidth = 1280;
1358 const int kHeight = 720; 1410 const int kHeight = 720;
1359 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1411 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1360 1412
1361 // Enable kMaintainResolution preference, no initial limitation. 1413 // Enable kMaintainResolution preference, no initial limitation.
1362 test::FrameForwarder source; 1414 test::FrameForwarder source;
1363 vie_encoder_->SetSource( 1415 vie_encoder_->SetSource(
1364 &source, VideoSendStream::DegradationPreference::kMaintainResolution); 1416 &source, VideoSendStream::DegradationPreference::kMaintainResolution);
1365 1417
1366 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1418 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1367 sink_.WaitForEncodedFrame(kWidth, kHeight); 1419 WaitForEncodedFrame(kWidth, kHeight);
1368 VerifyNoLimitation(source.sink_wants()); 1420 VerifyNoLimitation(source.sink_wants());
1369 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1421 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1370 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1422 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1371 1423
1372 // Trigger adapt up, expect no change. 1424 // Trigger adapt up, expect no change.
1373 vie_encoder_->TriggerCpuNormalUsage(); 1425 vie_encoder_->TriggerCpuNormalUsage();
1374 VerifyNoLimitation(source.sink_wants()); 1426 VerifyNoLimitation(source.sink_wants());
1375 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1427 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1376 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1428 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1377 1429
1378 vie_encoder_->Stop(); 1430 vie_encoder_->Stop();
1379 } 1431 }
1380 1432
1381 TEST_F(ViEEncoderTest, AdaptsResolutionForLowQuality_MaintainFramerateMode) { 1433 TEST_F(ViEEncoderTest, AdaptsResolutionForLowQuality_MaintainFramerateMode) {
1382 const int kWidth = 1280; 1434 const int kWidth = 1280;
1383 const int kHeight = 720; 1435 const int kHeight = 720;
1384 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1436 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1385 1437
1386 // Enable kMaintainFramerate preference, no initial limitation. 1438 // Enable kMaintainFramerate preference, no initial limitation.
1387 AdaptingFrameForwarder source; 1439 AdaptingFrameForwarder source;
1388 source.set_adaptation_enabled(true); 1440 source.set_adaptation_enabled(true);
1389 vie_encoder_->SetSource( 1441 vie_encoder_->SetSource(
1390 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1442 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1391 1443
1392 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1444 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1393 sink_.WaitForEncodedFrame(1); 1445 WaitForEncodedFrame(1);
1394 VerifyNoLimitation(source.sink_wants()); 1446 VerifyNoLimitation(source.sink_wants());
1395 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1447 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1396 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1448 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1397 1449
1398 // Trigger adapt down, expect scaled down resolution. 1450 // Trigger adapt down, expect scaled down resolution.
1399 vie_encoder_->TriggerQualityLow(); 1451 vie_encoder_->TriggerQualityLow();
1400 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1452 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1401 sink_.WaitForEncodedFrame(2); 1453 WaitForEncodedFrame(2);
1402 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight); 1454 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight);
1403 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1455 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1404 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1456 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1405 1457
1406 // Trigger adapt up, expect no restriction. 1458 // Trigger adapt up, expect no restriction.
1407 vie_encoder_->TriggerQualityHigh(); 1459 vie_encoder_->TriggerQualityHigh();
1408 VerifyNoLimitation(source.sink_wants()); 1460 VerifyNoLimitation(source.sink_wants());
1409 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1461 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1410 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1462 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1411 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1463 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
(...skipping 11 matching lines...) Expand all
1423 // Enable adapter, expected input resolutions when downscaling: 1475 // Enable adapter, expected input resolutions when downscaling:
1424 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (min resolution limit) 1476 // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (min resolution limit)
1425 video_source_.set_adaptation_enabled(true); 1477 video_source_.set_adaptation_enabled(true);
1426 1478
1427 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1479 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1428 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1480 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1429 1481
1430 int downscales = 0; 1482 int downscales = 0;
1431 for (size_t i = 1; i <= kNumFrames; i++) { 1483 for (size_t i = 1; i <= kNumFrames; i++) {
1432 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); 1484 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
1433 sink_.WaitForEncodedFrame(i); 1485 WaitForEncodedFrame(i);
1434 1486
1435 // Trigger scale down. 1487 // Trigger scale down.
1436 rtc::VideoSinkWants last_wants = video_source_.sink_wants(); 1488 rtc::VideoSinkWants last_wants = video_source_.sink_wants();
1437 vie_encoder_->TriggerQualityLow(); 1489 vie_encoder_->TriggerQualityLow();
1438 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame); 1490 EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame);
1439 1491
1440 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count) 1492 if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count)
1441 ++downscales; 1493 ++downscales;
1442 1494
1443 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1495 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
(...skipping 10 matching lines...) Expand all
1454 const int kHeight = 720; 1506 const int kHeight = 720;
1455 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1507 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1456 1508
1457 // Enable kMaintainFramerate preference, no initial limitation. 1509 // Enable kMaintainFramerate preference, no initial limitation.
1458 AdaptingFrameForwarder source; 1510 AdaptingFrameForwarder source;
1459 source.set_adaptation_enabled(true); 1511 source.set_adaptation_enabled(true);
1460 vie_encoder_->SetSource( 1512 vie_encoder_->SetSource(
1461 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1513 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1462 1514
1463 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1515 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1464 sink_.WaitForEncodedFrame(1); 1516 WaitForEncodedFrame(1);
1465 VerifyNoLimitation(source.sink_wants()); 1517 VerifyNoLimitation(source.sink_wants());
1466 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1518 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1467 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1519 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1468 1520
1469 // Trigger adapt down, expect scaled down resolution. 1521 // Trigger adapt down, expect scaled down resolution.
1470 vie_encoder_->TriggerCpuOveruse(); 1522 vie_encoder_->TriggerCpuOveruse();
1471 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1523 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1472 sink_.WaitForEncodedFrame(2); 1524 WaitForEncodedFrame(2);
1473 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight); 1525 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight);
1474 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1526 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1475 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1527 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1476 1528
1477 // Trigger adapt up, expect no restriction. 1529 // Trigger adapt up, expect no restriction.
1478 vie_encoder_->TriggerCpuNormalUsage(); 1530 vie_encoder_->TriggerCpuNormalUsage();
1479 source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1531 source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
1480 sink_.WaitForEncodedFrame(3); 1532 WaitForEncodedFrame(3);
1481 VerifyNoLimitation(source.sink_wants()); 1533 VerifyNoLimitation(source.sink_wants());
1482 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1534 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1483 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1535 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1484 1536
1485 // Trigger adapt down, expect scaled down resolution. 1537 // Trigger adapt down, expect scaled down resolution.
1486 vie_encoder_->TriggerCpuOveruse(); 1538 vie_encoder_->TriggerCpuOveruse();
1487 source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1539 source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
1488 sink_.WaitForEncodedFrame(4); 1540 WaitForEncodedFrame(4);
1489 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight); 1541 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight);
1490 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1542 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1491 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1543 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1492 1544
1493 // Trigger adapt up, expect no restriction. 1545 // Trigger adapt up, expect no restriction.
1494 vie_encoder_->TriggerCpuNormalUsage(); 1546 vie_encoder_->TriggerCpuNormalUsage();
1495 VerifyNoLimitation(source.sink_wants()); 1547 VerifyNoLimitation(source.sink_wants());
1496 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1548 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1497 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1549 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1498 1550
1499 vie_encoder_->Stop(); 1551 vie_encoder_->Stop();
1500 } 1552 }
1501 1553
1502 TEST_F(ViEEncoderTest, 1554 TEST_F(ViEEncoderTest,
1503 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) { 1555 AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) {
1504 const int kWidth = 1280; 1556 const int kWidth = 1280;
1505 const int kHeight = 720; 1557 const int kHeight = 720;
1506 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1558 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1507 1559
1508 // Enable kMaintainFramerate preference, no initial limitation. 1560 // Enable kMaintainFramerate preference, no initial limitation.
1509 AdaptingFrameForwarder source; 1561 AdaptingFrameForwarder source;
1510 source.set_adaptation_enabled(true); 1562 source.set_adaptation_enabled(true);
1511 vie_encoder_->SetSource( 1563 vie_encoder_->SetSource(
1512 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1564 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1513 1565
1514 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1566 source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1515 sink_.WaitForEncodedFrame(kWidth, kHeight); 1567 WaitForEncodedFrame(kWidth, kHeight);
1516 VerifyNoLimitation(source.sink_wants()); 1568 VerifyNoLimitation(source.sink_wants());
1517 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1569 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1518 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1570 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1519 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1571 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1520 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1572 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1521 1573
1522 // Trigger cpu adapt down, expect scaled down resolution (960x540). 1574 // Trigger cpu adapt down, expect scaled down resolution (960x540).
1523 vie_encoder_->TriggerCpuOveruse(); 1575 vie_encoder_->TriggerCpuOveruse();
1524 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); 1576 source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight));
1525 sink_.WaitForEncodedFrame(2); 1577 WaitForEncodedFrame(2);
1526 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight); 1578 VerifyResolutionLimitationLessThan(source.sink_wants(), kWidth * kHeight);
1527 rtc::VideoSinkWants last_wants = source.sink_wants(); 1579 rtc::VideoSinkWants last_wants = source.sink_wants();
1528 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1580 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1529 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1581 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1530 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1582 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1531 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1583 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1532 1584
1533 // Trigger cpu adapt down, expect scaled down resolution (640x360). 1585 // Trigger cpu adapt down, expect scaled down resolution (640x360).
1534 vie_encoder_->TriggerCpuOveruse(); 1586 vie_encoder_->TriggerCpuOveruse();
1535 source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); 1587 source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight));
1536 sink_.WaitForEncodedFrame(3); 1588 WaitForEncodedFrame(3);
1537 EXPECT_LT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count); 1589 EXPECT_LT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count);
1538 last_wants = source.sink_wants(); 1590 last_wants = source.sink_wants();
1539 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1591 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1540 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1592 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1541 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1593 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1542 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1594 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1543 1595
1544 // Trigger cpu adapt down, max cpu downgrades reached, expect no change. 1596 // Trigger cpu adapt down, max cpu downgrades reached, expect no change.
1545 vie_encoder_->TriggerCpuOveruse(); 1597 vie_encoder_->TriggerCpuOveruse();
1546 source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); 1598 source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight));
1547 sink_.WaitForEncodedFrame(4); 1599 WaitForEncodedFrame(4);
1548 EXPECT_EQ(last_wants.max_pixel_count, source.sink_wants().max_pixel_count); 1600 EXPECT_EQ(last_wants.max_pixel_count, source.sink_wants().max_pixel_count);
1549 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1601 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1550 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1602 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1551 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1603 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1552 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1604 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1553 1605
1554 // Trigger quality adapt down, expect scaled down resolution (480x270). 1606 // Trigger quality adapt down, expect scaled down resolution (480x270).
1555 vie_encoder_->TriggerQualityLow(); 1607 vie_encoder_->TriggerQualityLow();
1556 source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); 1608 source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight));
1557 sink_.WaitForEncodedFrame(5); 1609 WaitForEncodedFrame(5);
1558 EXPECT_LT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count); 1610 EXPECT_LT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count);
1559 last_wants = source.sink_wants(); 1611 last_wants = source.sink_wants();
1560 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1612 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1561 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1613 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1562 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1614 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1563 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1615 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1564 1616
1565 // Trigger cpu adapt up, expect upscaled resolution (640x360). 1617 // Trigger cpu adapt up, expect upscaled resolution (640x360).
1566 vie_encoder_->TriggerCpuNormalUsage(); 1618 vie_encoder_->TriggerCpuNormalUsage();
1567 source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight)); 1619 source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight));
1568 sink_.WaitForEncodedFrame(6); 1620 WaitForEncodedFrame(6);
1569 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count); 1621 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count);
1570 last_wants = source.sink_wants(); 1622 last_wants = source.sink_wants();
1571 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); 1623 EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution);
1572 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1624 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1573 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1625 EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1574 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1626 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1575 1627
1576 // Trigger cpu adapt up, expect upscaled resolution (960x540). 1628 // Trigger cpu adapt up, expect upscaled resolution (960x540).
1577 vie_encoder_->TriggerCpuNormalUsage(); 1629 vie_encoder_->TriggerCpuNormalUsage();
1578 source.IncomingCapturedFrame(CreateFrame(7, kWidth, kHeight)); 1630 source.IncomingCapturedFrame(CreateFrame(7, kWidth, kHeight));
1579 sink_.WaitForEncodedFrame(7); 1631 WaitForEncodedFrame(7);
1580 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count); 1632 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count);
1581 last_wants = source.sink_wants(); 1633 last_wants = source.sink_wants();
1582 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1634 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1583 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1635 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1584 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1636 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1585 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1637 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1586 1638
1587 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540). 1639 // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540).
1588 vie_encoder_->TriggerCpuNormalUsage(); 1640 vie_encoder_->TriggerCpuNormalUsage();
1589 source.IncomingCapturedFrame(CreateFrame(8, kWidth, kHeight)); 1641 source.IncomingCapturedFrame(CreateFrame(8, kWidth, kHeight));
1590 sink_.WaitForEncodedFrame(8); 1642 WaitForEncodedFrame(8);
1591 EXPECT_EQ(last_wants.max_pixel_count, source.sink_wants().max_pixel_count); 1643 EXPECT_EQ(last_wants.max_pixel_count, source.sink_wants().max_pixel_count);
1592 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1644 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1593 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); 1645 EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution);
1594 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1646 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1595 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1647 EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1596 1648
1597 // Trigger quality adapt up, expect no restriction (1280x720). 1649 // Trigger quality adapt up, expect no restriction (1280x720).
1598 vie_encoder_->TriggerQualityHigh(); 1650 vie_encoder_->TriggerQualityHigh();
1599 source.IncomingCapturedFrame(CreateFrame(9, kWidth, kHeight)); 1651 source.IncomingCapturedFrame(CreateFrame(9, kWidth, kHeight));
1600 sink_.WaitForEncodedFrame(kWidth, kHeight); 1652 WaitForEncodedFrame(kWidth, kHeight);
1601 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count); 1653 EXPECT_GT(source.sink_wants().max_pixel_count, last_wants.max_pixel_count);
1602 VerifyNoLimitation(source.sink_wants()); 1654 VerifyNoLimitation(source.sink_wants());
1603 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1655 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1604 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); 1656 EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution);
1605 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1657 EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1606 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); 1658 EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes);
1607 1659
1608 vie_encoder_->Stop(); 1660 vie_encoder_->Stop();
1609 } 1661 }
1610 1662
1611 TEST_F(ViEEncoderTest, CpuLimitedHistogramIsReported) { 1663 TEST_F(ViEEncoderTest, CpuLimitedHistogramIsReported) {
1612 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1613 const int kWidth = 640; 1664 const int kWidth = 640;
1614 const int kHeight = 360; 1665 const int kHeight = 360;
1615 1666
1667 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1668
1616 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { 1669 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
1617 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); 1670 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
1618 sink_.WaitForEncodedFrame(i); 1671 WaitForEncodedFrame(i);
1619 } 1672 }
1620 1673
1621 vie_encoder_->TriggerCpuOveruse(); 1674 vie_encoder_->TriggerCpuOveruse();
1622 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { 1675 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
1623 video_source_.IncomingCapturedFrame(CreateFrame( 1676 video_source_.IncomingCapturedFrame(CreateFrame(
1624 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight)); 1677 SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight));
1625 sink_.WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + 1678 WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i);
1626 i);
1627 } 1679 }
1628 1680
1629 vie_encoder_->Stop(); 1681 vie_encoder_->Stop();
1630 vie_encoder_.reset(); 1682 vie_encoder_.reset();
1631 stats_proxy_.reset(); 1683 stats_proxy_.reset();
1632 1684
1633 EXPECT_EQ(1, 1685 EXPECT_EQ(1,
1634 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); 1686 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
1635 EXPECT_EQ( 1687 EXPECT_EQ(
1636 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50)); 1688 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50));
1637 } 1689 }
1638 1690
1639 TEST_F(ViEEncoderTest, CpuLimitedHistogramIsNotReportedForDisabledDegradation) { 1691 TEST_F(ViEEncoderTest, CpuLimitedHistogramIsNotReportedForDisabledDegradation) {
1640 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1692 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1641 const int kWidth = 640; 1693 const int kWidth = 640;
1642 const int kHeight = 360; 1694 const int kHeight = 360;
1643 1695
1644 vie_encoder_->SetSource( 1696 vie_encoder_->SetSource(
1645 &video_source_, 1697 &video_source_,
1646 VideoSendStream::DegradationPreference::kDegradationDisabled); 1698 VideoSendStream::DegradationPreference::kDegradationDisabled);
1647 1699
1648 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { 1700 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) {
1649 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); 1701 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
1650 sink_.WaitForEncodedFrame(i); 1702 WaitForEncodedFrame(i);
1651 } 1703 }
1652 1704
1653 vie_encoder_->Stop(); 1705 vie_encoder_->Stop();
1654 vie_encoder_.reset(); 1706 vie_encoder_.reset();
1655 stats_proxy_.reset(); 1707 stats_proxy_.reset();
1656 1708
1657 EXPECT_EQ(0, 1709 EXPECT_EQ(0,
1658 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); 1710 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent"));
1659 } 1711 }
1660 1712
(...skipping 10 matching lines...) Expand all
1671 .GetAllocation(kLowTargetBitrateBps, kDefaultFps); 1723 .GetAllocation(kLowTargetBitrateBps, kDefaultFps);
1672 1724
1673 // First called on bitrate updated, then again on first frame. 1725 // First called on bitrate updated, then again on first frame.
1674 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) 1726 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
1675 .Times(2); 1727 .Times(2);
1676 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); 1728 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0);
1677 1729
1678 const int64_t kStartTimeMs = 1; 1730 const int64_t kStartTimeMs = 1;
1679 video_source_.IncomingCapturedFrame( 1731 video_source_.IncomingCapturedFrame(
1680 CreateFrame(kStartTimeMs, codec_width_, codec_height_)); 1732 CreateFrame(kStartTimeMs, codec_width_, codec_height_));
1681 sink_.WaitForEncodedFrame(kStartTimeMs); 1733 WaitForEncodedFrame(kStartTimeMs);
1682 1734
1683 // Not called on second frame. 1735 // Not called on second frame.
1684 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) 1736 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
1685 .Times(0); 1737 .Times(0);
1686 video_source_.IncomingCapturedFrame( 1738 video_source_.IncomingCapturedFrame(
1687 CreateFrame(kStartTimeMs + 1, codec_width_, codec_height_)); 1739 CreateFrame(kStartTimeMs + 1, codec_width_, codec_height_));
1688 sink_.WaitForEncodedFrame(kStartTimeMs + 1); 1740 WaitForEncodedFrame(kStartTimeMs + 1);
1689 1741
1690 // Called after a process interval. 1742 // Called after a process interval.
1691 const int64_t kProcessIntervalMs = 1743 const int64_t kProcessIntervalMs =
1692 vcm::VCMProcessTimer::kDefaultProcessIntervalMs; 1744 vcm::VCMProcessTimer::kDefaultProcessIntervalMs;
1693 // TODO(sprang): ViEEncoder should die and/or get injectable clock. 1745 fake_clock_.AdvanceTimeMicros(rtc::kNumMicrosecsPerMillisec *
1694 // Sleep for one processing interval plus one frame to avoid flakiness. 1746 (kProcessIntervalMs + (1000 / kDefaultFps)));
1695 SleepMs(kProcessIntervalMs + 1000 / kDefaultFps);
1696 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) 1747 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate))
1697 .Times(1); 1748 .Times(1);
1698 video_source_.IncomingCapturedFrame(CreateFrame( 1749 video_source_.IncomingCapturedFrame(CreateFrame(
1699 kStartTimeMs + kProcessIntervalMs, codec_width_, codec_height_)); 1750 kStartTimeMs + kProcessIntervalMs, codec_width_, codec_height_));
1700 sink_.WaitForEncodedFrame(kStartTimeMs + kProcessIntervalMs); 1751 WaitForEncodedFrame(kStartTimeMs + kProcessIntervalMs);
1701 1752
1702 vie_encoder_->Stop(); 1753 vie_encoder_->Stop();
1703 } 1754 }
1704 1755
1705 TEST_F(ViEEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) { 1756 TEST_F(ViEEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) {
1706 const int kTooLowBitrateForFrameSizeBps = 10000; 1757 const int kTooLowBitrateForFrameSizeBps = 10000;
1707 vie_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0); 1758 vie_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0);
1708 const int kWidth = 640; 1759 const int kWidth = 640;
1709 const int kHeight = 360; 1760 const int kHeight = 360;
1710 1761
1711 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1762 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1712 1763
1713 // Expect to drop this frame, the wait should time out. 1764 // Expect to drop this frame, the wait should time out.
1714 sink_.ExpectDroppedFrame(); 1765 ExpectDroppedFrame();
1715 1766
1716 // Expect the sink_wants to specify a scaled frame. 1767 // Expect the sink_wants to specify a scaled frame.
1717 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); 1768 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
1718 1769
1719 int last_pixel_count = video_source_.sink_wants().max_pixel_count; 1770 int last_pixel_count = video_source_.sink_wants().max_pixel_count;
1720 1771
1721 // Next frame is scaled. 1772 // Next frame is scaled.
1722 video_source_.IncomingCapturedFrame( 1773 video_source_.IncomingCapturedFrame(
1723 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4)); 1774 CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4));
1724 1775
1725 // Expect to drop this frame, the wait should time out. 1776 // Expect to drop this frame, the wait should time out.
1726 sink_.ExpectDroppedFrame(); 1777 ExpectDroppedFrame();
1727 1778
1728 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count); 1779 EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count);
1729 1780
1730 vie_encoder_->Stop(); 1781 vie_encoder_->Stop();
1731 } 1782 }
1732 1783
1733 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimitedWhenBitrateIsTooLow) { 1784 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimitedWhenBitrateIsTooLow) {
1734 const int kTooLowBitrateForFrameSizeBps = 10000; 1785 const int kTooLowBitrateForFrameSizeBps = 10000;
1735 vie_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0); 1786 vie_encoder_->OnBitrateUpdated(kTooLowBitrateForFrameSizeBps, 0, 0);
1736 const int kWidth = 640; 1787 const int kWidth = 640;
1737 const int kHeight = 360; 1788 const int kHeight = 360;
1738 1789
1739 // We expect the n initial frames to get dropped. 1790 // We expect the n initial frames to get dropped.
1740 int i; 1791 int i;
1741 for (i = 1; i <= kMaxInitialFramedrop; ++i) { 1792 for (i = 1; i <= kMaxInitialFramedrop; ++i) {
1742 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); 1793 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
1743 sink_.ExpectDroppedFrame(); 1794 ExpectDroppedFrame();
1744 } 1795 }
1745 // The n+1th frame should not be dropped, even though it's size is too large. 1796 // The n+1th frame should not be dropped, even though it's size is too large.
1746 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); 1797 video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight));
1747 sink_.WaitForEncodedFrame(i); 1798 WaitForEncodedFrame(i);
1748 1799
1749 // Expect the sink_wants to specify a scaled frame. 1800 // Expect the sink_wants to specify a scaled frame.
1750 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); 1801 EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight);
1751 1802
1752 vie_encoder_->Stop(); 1803 vie_encoder_->Stop();
1753 } 1804 }
1754 1805
1755 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) { 1806 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) {
1756 const int kWidth = 640; 1807 const int kWidth = 640;
1757 const int kHeight = 360; 1808 const int kHeight = 360;
1758 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); 1809 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0);
1759 1810
1760 // Set degradation preference. 1811 // Set degradation preference.
1761 vie_encoder_->SetSource( 1812 vie_encoder_->SetSource(
1762 &video_source_, 1813 &video_source_,
1763 VideoSendStream::DegradationPreference::kMaintainResolution); 1814 VideoSendStream::DegradationPreference::kMaintainResolution);
1764 1815
1765 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1816 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1766 // Frame should not be dropped, even if it's too large. 1817 // Frame should not be dropped, even if it's too large.
1767 sink_.WaitForEncodedFrame(1); 1818 WaitForEncodedFrame(1);
1768 1819
1769 vie_encoder_->Stop(); 1820 vie_encoder_->Stop();
1770 } 1821 }
1771 1822
1772 TEST_F(ViEEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) { 1823 TEST_F(ViEEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) {
1773 const int kWidth = 640; 1824 const int kWidth = 640;
1774 const int kHeight = 360; 1825 const int kHeight = 360;
1775 fake_encoder_.SetQualityScaling(false); 1826 fake_encoder_.SetQualityScaling(false);
1776 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); 1827 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0);
1777 // Force quality scaler reconfiguration by resetting the source. 1828 // Force quality scaler reconfiguration by resetting the source.
1778 vie_encoder_->SetSource(&video_source_, 1829 vie_encoder_->SetSource(&video_source_,
1779 VideoSendStream::DegradationPreference::kBalanced); 1830 VideoSendStream::DegradationPreference::kBalanced);
1780 1831
1781 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); 1832 video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight));
1782 // Frame should not be dropped, even if it's too large. 1833 // Frame should not be dropped, even if it's too large.
1783 sink_.WaitForEncodedFrame(1); 1834 WaitForEncodedFrame(1);
1784 1835
1785 vie_encoder_->Stop(); 1836 vie_encoder_->Stop();
1786 fake_encoder_.SetQualityScaling(true); 1837 fake_encoder_.SetQualityScaling(true);
1787 } 1838 }
1788 1839
1789 TEST_F(ViEEncoderTest, 1840 TEST_F(ViEEncoderTest,
1790 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) { 1841 ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) {
1791 const int kTooSmallWidth = 10; 1842 const int kTooSmallWidth = 10;
1792 const int kTooSmallHeight = 10; 1843 const int kTooSmallHeight = 10;
1793 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1844 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1794 1845
1795 // Enable kMaintainFramerate preference, no initial limitation. 1846 // Enable kMaintainFramerate preference, no initial limitation.
1796 test::FrameForwarder source; 1847 test::FrameForwarder source;
1797 vie_encoder_->SetSource( 1848 vie_encoder_->SetSource(
1798 &source, VideoSendStream::DegradationPreference::kMaintainFramerate); 1849 &source, VideoSendStream::DegradationPreference::kMaintainFramerate);
1799 VerifyNoLimitation(source.sink_wants()); 1850 VerifyNoLimitation(source.sink_wants());
1800 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1851 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1801 1852
1802 // Trigger adapt down, too small frame, expect no change. 1853 // Trigger adapt down, too small frame, expect no change.
1803 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight)); 1854 source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight));
1804 sink_.WaitForEncodedFrame(1); 1855 WaitForEncodedFrame(1);
1805 vie_encoder_->TriggerCpuOveruse(); 1856 vie_encoder_->TriggerCpuOveruse();
1806 VerifyNoLimitation(source.sink_wants()); 1857 VerifyNoLimitation(source.sink_wants());
1807 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); 1858 EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution);
1808 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); 1859 EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes);
1809 1860
1810 vie_encoder_->Stop(); 1861 vie_encoder_->Stop();
1811 } 1862 }
1812 1863
1813 TEST_F(ViEEncoderTest, FailingInitEncodeDoesntCauseCrash) { 1864 TEST_F(ViEEncoderTest, FailingInitEncodeDoesntCauseCrash) {
1814 fake_encoder_.ForceInitEncodeFailure(true); 1865 fake_encoder_.ForceInitEncodeFailure(true);
1815 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1866 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1816 ResetEncoder("VP8", 2, 1, true); 1867 ResetEncoder("VP8", 2, 1, true, false);
1817 const int kFrameWidth = 1280; 1868 const int kFrameWidth = 1280;
1818 const int kFrameHeight = 720; 1869 const int kFrameHeight = 720;
1819 video_source_.IncomingCapturedFrame( 1870 video_source_.IncomingCapturedFrame(
1820 CreateFrame(1, kFrameWidth, kFrameHeight)); 1871 CreateFrame(1, kFrameWidth, kFrameHeight));
1821 sink_.ExpectDroppedFrame(); 1872 ExpectDroppedFrame();
1822 vie_encoder_->Stop(); 1873 vie_encoder_->Stop();
1823 } 1874 }
1824 1875
1825 // TODO(sprang): Extend this with fps throttling and any "balanced" extensions. 1876 // TODO(sprang): Extend this with fps throttling and any "balanced" extensions.
1826 TEST_F(ViEEncoderTest, AdaptsResolutionOnOveruse_MaintainFramerateMode) { 1877 TEST_F(ViEEncoderTest, AdaptsResolutionOnOveruse_MaintainFramerateMode) {
1827 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1878 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1828 1879
1829 const int kFrameWidth = 1280; 1880 const int kFrameWidth = 1280;
1830 const int kFrameHeight = 720; 1881 const int kFrameHeight = 720;
1831 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as 1882 // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as
1832 // requested by ViEEncoder::VideoSourceProxy::RequestResolutionLowerThan(). 1883 // requested by ViEEncoder::VideoSourceProxy::RequestResolutionLowerThan().
1833 video_source_.set_adaptation_enabled(true); 1884 video_source_.set_adaptation_enabled(true);
1834 1885
1835 video_source_.IncomingCapturedFrame( 1886 video_source_.IncomingCapturedFrame(
1836 CreateFrame(1, kFrameWidth, kFrameHeight)); 1887 CreateFrame(1, kFrameWidth, kFrameHeight));
1837 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1888 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1838 1889
1839 // Trigger CPU overuse, downscale by 3/4. 1890 // Trigger CPU overuse, downscale by 3/4.
1840 vie_encoder_->TriggerCpuOveruse(); 1891 vie_encoder_->TriggerCpuOveruse();
1841 video_source_.IncomingCapturedFrame( 1892 video_source_.IncomingCapturedFrame(
1842 CreateFrame(2, kFrameWidth, kFrameHeight)); 1893 CreateFrame(2, kFrameWidth, kFrameHeight));
1843 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); 1894 WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
1844 1895
1845 // Trigger CPU normal use, return to original resolution. 1896 // Trigger CPU normal use, return to original resolution.
1846 vie_encoder_->TriggerCpuNormalUsage(); 1897 vie_encoder_->TriggerCpuNormalUsage();
1847 video_source_.IncomingCapturedFrame( 1898 video_source_.IncomingCapturedFrame(
1848 CreateFrame(3, kFrameWidth, kFrameHeight)); 1899 CreateFrame(3, kFrameWidth, kFrameHeight));
1849 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1900 WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1850 1901
1851 vie_encoder_->Stop(); 1902 vie_encoder_->Stop();
1852 } 1903 }
1853 1904
1854 TEST_F(ViEEncoderTest, AdaptsFramerateOnOveruse_MaintainResolutionMode) { 1905 TEST_F(ViEEncoderTest, AdaptsFramerateOnOveruse_MaintainResolutionMode) {
1855 const int kDefaultFramerateFps = 30; 1906 // const int kDefaultFramerateFps = 30;
1856 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerateFps;
1857 const int kFrameWidth = 1280; 1907 const int kFrameWidth = 1280;
1858 const int kFrameHeight = 720; 1908 const int kFrameHeight = 720;
1859 rtc::ScopedFakeClock fake_clock; 1909 int kFrameIntervalMs = rtc::kNumMillisecsPerSec / max_framerate_;
1860 1910
1861 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1911 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1862 vie_encoder_->SetSource( 1912 vie_encoder_->SetSource(
1863 &video_source_, 1913 &video_source_,
1864 VideoSendStream::DegradationPreference::kMaintainResolution); 1914 VideoSendStream::DegradationPreference::kMaintainResolution);
1865 video_source_.set_adaptation_enabled(true); 1915 video_source_.set_adaptation_enabled(true);
1866 1916
1867 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000); 1917 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
1868 int64_t timestamp_ms = kFrameIntervalMs;
1869 1918
1870 video_source_.IncomingCapturedFrame( 1919 video_source_.IncomingCapturedFrame(
1871 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1920 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1872 sink_.WaitForEncodedFrame(timestamp_ms); 1921 WaitForEncodedFrame(timestamp_ms);
1873 1922
1874 // Try to trigger overuse. No fps estimate available => no effect. 1923 // Try to trigger overuse. No fps estimate available => no effect.
1875 vie_encoder_->TriggerCpuOveruse(); 1924 vie_encoder_->TriggerCpuOveruse();
1876 1925
1877 // Insert frames for one second to get a stable estimate. 1926 // Insert frames for one second to get a stable estimate.
1878 for (int i = 0; i < kDefaultFramerateFps; ++i) { 1927 for (int i = 0; i < max_framerate_; ++i) {
1879 timestamp_ms += kFrameIntervalMs; 1928 timestamp_ms += kFrameIntervalMs;
1880 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1881 video_source_.IncomingCapturedFrame( 1929 video_source_.IncomingCapturedFrame(
1882 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1930 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1883 sink_.WaitForEncodedFrame(timestamp_ms); 1931 WaitForEncodedFrame(timestamp_ms);
1884 } 1932 }
1885 1933
1886 // Trigger CPU overuse, reduce framerate by 2/3. 1934 // Trigger CPU overuse, reduce framerate by 2/3.
1887 vie_encoder_->TriggerCpuOveruse(); 1935 vie_encoder_->TriggerCpuOveruse();
1888 int num_frames_dropped = 0; 1936 int num_frames_dropped = 0;
1889 for (int i = 0; i < kDefaultFramerateFps; ++i) { 1937 for (int i = 0; i < max_framerate_; ++i) {
1890 timestamp_ms += kFrameIntervalMs; 1938 timestamp_ms += kFrameIntervalMs;
1891 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1892 video_source_.IncomingCapturedFrame( 1939 video_source_.IncomingCapturedFrame(
1893 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1940 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1894 if (!sink_.WaitForFrame(kFrameTimeoutMs)) { 1941 if (!WaitForFrame(kFrameTimeoutMs)) {
1895 ++num_frames_dropped; 1942 ++num_frames_dropped;
1896 } else { 1943 } else {
1897 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight); 1944 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1898 } 1945 }
1899 } 1946 }
1900 1947
1901 // TODO(sprang): Find where there's rounding errors or stuff causing the 1948 // Add some slack to account for frames dropped by the frame dropper.
1902 // margin here to be a little larger than we'd like (input fps estimate is 1949 const int kErrorMargin = 1;
1903 // off) and the frame dropping is a little too aggressive. 1950 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
1904 const int kErrorMargin = 5;
1905 EXPECT_NEAR(num_frames_dropped,
1906 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1907 kErrorMargin); 1951 kErrorMargin);
1908 1952
1909 // Trigger CPU overuse, reduce framerate by 2/3 again. 1953 // Trigger CPU overuse, reduce framerate by 2/3 again.
1910 vie_encoder_->TriggerCpuOveruse(); 1954 vie_encoder_->TriggerCpuOveruse();
1911 num_frames_dropped = 0; 1955 num_frames_dropped = 0;
1912 for (int i = 0; i < kDefaultFramerateFps; ++i) { 1956 for (int i = 0; i < max_framerate_; ++i) {
1913 timestamp_ms += kFrameIntervalMs; 1957 timestamp_ms += kFrameIntervalMs;
1914 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1915 video_source_.IncomingCapturedFrame( 1958 video_source_.IncomingCapturedFrame(
1916 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1959 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1917 if (!sink_.WaitForFrame(kFrameTimeoutMs)) { 1960 if (!WaitForFrame(kFrameTimeoutMs)) {
1918 ++num_frames_dropped; 1961 ++num_frames_dropped;
1919 } else { 1962 } else {
1920 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight); 1963 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1921 } 1964 }
1922 } 1965 }
1923 EXPECT_NEAR(num_frames_dropped, 1966 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9),
1924 kDefaultFramerateFps - (kDefaultFramerateFps * 4 / 9),
1925 kErrorMargin); 1967 kErrorMargin);
1926 1968
1927 // Go back up one step. 1969 // Go back up one step.
1928 vie_encoder_->TriggerCpuNormalUsage(); 1970 vie_encoder_->TriggerCpuNormalUsage();
1929 num_frames_dropped = 0; 1971 num_frames_dropped = 0;
1930 for (int i = 0; i < kDefaultFramerateFps; ++i) { 1972 for (int i = 0; i < max_framerate_; ++i) {
1931 timestamp_ms += kFrameIntervalMs; 1973 timestamp_ms += kFrameIntervalMs;
1932 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1933 video_source_.IncomingCapturedFrame( 1974 video_source_.IncomingCapturedFrame(
1934 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1975 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1935 if (!sink_.WaitForFrame(kFrameTimeoutMs)) { 1976 if (!WaitForFrame(kFrameTimeoutMs)) {
1936 ++num_frames_dropped; 1977 ++num_frames_dropped;
1937 } else { 1978 } else {
1938 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight); 1979 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1939 } 1980 }
1940 } 1981 }
1941 EXPECT_NEAR(num_frames_dropped, 1982 EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3),
1942 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1943 kErrorMargin); 1983 kErrorMargin);
1944 1984
1945 // Go back up to original mode. 1985 // Go back up to original mode.
1946 vie_encoder_->TriggerCpuNormalUsage(); 1986 vie_encoder_->TriggerCpuNormalUsage();
1947 num_frames_dropped = 0; 1987 num_frames_dropped = 0;
1948 for (int i = 0; i < kDefaultFramerateFps; ++i) { 1988 for (int i = 0; i < max_framerate_; ++i) {
1949 timestamp_ms += kFrameIntervalMs; 1989 timestamp_ms += kFrameIntervalMs;
1950 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1951 video_source_.IncomingCapturedFrame( 1990 video_source_.IncomingCapturedFrame(
1952 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 1991 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1953 if (!sink_.WaitForFrame(kFrameTimeoutMs)) { 1992 if (!WaitForFrame(kFrameTimeoutMs)) {
1954 ++num_frames_dropped; 1993 ++num_frames_dropped;
1955 } else { 1994 } else {
1956 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight); 1995 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1957 } 1996 }
1958 } 1997 }
1959 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin); 1998 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
1960 1999
1961 vie_encoder_->Stop(); 2000 vie_encoder_->Stop();
1962 } 2001 }
1963 2002
1964 TEST_F(ViEEncoderTest, DoesntAdaptDownPastMinFramerate) { 2003 TEST_F(ViEEncoderTest, DoesntAdaptDownPastMinFramerate) {
1965 const int kFramerateFps = 5; 2004 const int kFramerateFps = 5;
1966 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps; 2005 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
1967 const int kMinFpsFrameInterval = rtc::kNumMillisecsPerSec / kMinFramerateFps; 2006 const int kMinFpsFrameInterval = rtc::kNumMillisecsPerSec / kMinFramerateFps;
1968 const int kFrameWidth = 1280; 2007 const int kFrameWidth = 1280;
1969 const int kFrameHeight = 720; 2008 const int kFrameHeight = 720;
1970 2009
1971 rtc::ScopedFakeClock fake_clock; 2010 // Reconfigure encoder with two temporal layers and screensharing, which will
2011 // disable frame dropping and make testing easier.
2012 ResetEncoder("VP8", 1, 2, true, true);
2013
1972 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 2014 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1973 vie_encoder_->SetSource( 2015 vie_encoder_->SetSource(
1974 &video_source_, 2016 &video_source_,
1975 VideoSendStream::DegradationPreference::kMaintainResolution); 2017 VideoSendStream::DegradationPreference::kMaintainResolution);
1976 video_source_.set_adaptation_enabled(true); 2018 video_source_.set_adaptation_enabled(true);
1977 2019
1978 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000); 2020 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
1979 int64_t timestamp_ms = kFrameIntervalMs;
1980 2021
1981 // Trigger overuse as much as we can. 2022 // Trigger overuse as much as we can.
1982 for (int i = 0; i < ViEEncoder::kMaxCpuResolutionDowngrades; ++i) { 2023 for (int i = 0; i < ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
1983 // Insert frames to get a new fps estimate... 2024 // Insert frames to get a new fps estimate...
1984 for (int j = 0; j < kFramerateFps; ++j) { 2025 for (int j = 0; j < kFramerateFps; ++j) {
1985 video_source_.IncomingCapturedFrame( 2026 video_source_.IncomingCapturedFrame(
1986 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 2027 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1987 timestamp_ms += kFrameIntervalMs; 2028 timestamp_ms += kFrameIntervalMs;
1988 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1989 } 2029 }
1990 // ...and then try to adapt again. 2030 // ...and then try to adapt again.
1991 vie_encoder_->TriggerCpuOveruse(); 2031 vie_encoder_->TriggerCpuOveruse();
1992 } 2032 }
1993 2033
1994 // Drain any frame in the pipeline. 2034 // Drain any frame in the pipeline.
1995 sink_.WaitForFrame(kDefaultTimeoutMs); 2035 WaitForFrame(kDefaultTimeoutMs);
1996 2036
1997 // Insert frames at min fps, all should go through. 2037 // Insert frames at min fps, all should go through.
1998 for (int i = 0; i < 10; ++i) { 2038 for (int i = 0; i < 10; ++i) {
1999 timestamp_ms += kMinFpsFrameInterval; 2039 timestamp_ms += kMinFpsFrameInterval;
2000 fake_clock.AdvanceTimeMicros(kMinFpsFrameInterval * 1000);
2001 video_source_.IncomingCapturedFrame( 2040 video_source_.IncomingCapturedFrame(
2002 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); 2041 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
2003 sink_.WaitForEncodedFrame(timestamp_ms); 2042 WaitForEncodedFrame(timestamp_ms);
2004 } 2043 }
2005 vie_encoder_->Stop(); 2044 vie_encoder_->Stop();
2006 } 2045 }
2046
2047 TEST_F(ViEEncoderTest, PriodicallyUpdatesChannelParameters) {
2048 const int kFrameWidth = 1280;
2049 const int kFrameHeight = 720;
2050 const int kLowFps = 2;
2051 const int kHighFps = 30;
2052
2053 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
2054
2055 int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec;
2056 max_framerate_ = kLowFps;
2057
2058 // Insert 2 seconds of 2fps video.
2059 for (int i = 0; i < kLowFps * 2; ++i) {
2060 video_source_.IncomingCapturedFrame(
2061 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
2062 WaitForEncodedFrame(timestamp_ms);
2063 timestamp_ms += 1000 / kLowFps;
2064 }
2065
2066 // Make sure encoder is updated with new target.
2067 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
2068 video_source_.IncomingCapturedFrame(
2069 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
2070 WaitForEncodedFrame(timestamp_ms);
2071 timestamp_ms += 1000 / kLowFps;
2072
2073 EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate());
2074
2075 // Insert 30fps frames for just a little more than the forced update period.
2076 const int kVcmTimerIntervalFrames =
2077 (vcm::VCMProcessTimer::kDefaultProcessIntervalMs * kHighFps) / 1000;
2078 const int kFrameIntervalMs = 1000 / kHighFps;
2079 max_framerate_ = kHighFps;
2080 for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) {
2081 video_source_.IncomingCapturedFrame(
2082 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
2083 // Wait for encoded frame, but skip ahead if it doesn't arrive as it might
2084 // be dropped if the encoder hans't been updated with the new higher target
2085 // framerate yet, causing it to overshoot the target bitrate and then
2086 // suffering the wrath of the media optimizer.
2087 TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs);
2088 timestamp_ms += kFrameIntervalMs;
2089 }
2090
2091 // Don expect correct measurement just yet, but it should be higher than
2092 // before.
2093 EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps);
2094
2095 vie_encoder_->Stop();
2096 }
2007 } // namespace webrtc 2097 } // namespace webrtc
OLDNEW
« webrtc/video/vie_encoder.cc ('K') | « webrtc/video/vie_encoder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698