OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // If we don't have a WebRtcVideoFrame, just skip all of these tests. | 11 // If we don't have a WebRtcVideoFrame, just skip all of these tests. |
12 #if defined(HAVE_WEBRTC_VIDEO) | 12 #if defined(HAVE_WEBRTC_VIDEO) |
13 #include <limits.h> // For INT_MAX | 13 #include <limits.h> // For INT_MAX |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/gunit.h" | 17 #include "webrtc/base/gunit.h" |
18 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
19 #include "webrtc/base/sigslot.h" | 19 #include "webrtc/base/sigslot.h" |
20 #include "webrtc/media/base/fakevideocapturer.h" | 20 #include "webrtc/media/base/fakevideocapturer.h" |
21 #include "webrtc/media/base/mediachannel.h" | 21 #include "webrtc/media/base/mediachannel.h" |
22 #include "webrtc/media/base/testutils.h" | 22 #include "webrtc/media/base/testutils.h" |
23 #include "webrtc/media/base/videoadapter.h" | 23 #include "webrtc/media/base/videoadapter.h" |
24 | 24 |
25 namespace cricket { | 25 namespace cricket { |
26 | 26 |
27 namespace { | |
28 static const uint32_t kWaitTimeout = 3000U; // 3 seconds. | |
29 static const uint32_t kShortWaitTimeout = 1000U; // 1 second. | |
30 void UpdateCpuLoad(CoordinatedVideoAdapter* adapter, | |
31 int current_cpus, int max_cpus, float process_load, float system_load) { | |
32 adapter->set_cpu_load_min_samples(1); | |
33 adapter->OnCpuLoadUpdated(current_cpus, max_cpus, | |
34 process_load, system_load); | |
35 } | |
36 } | |
37 | |
38 class VideoAdapterTest : public testing::Test { | 27 class VideoAdapterTest : public testing::Test { |
39 public: | 28 public: |
40 virtual void SetUp() { | 29 virtual void SetUp() { |
41 capturer_.reset(new FakeVideoCapturer); | 30 capturer_.reset(new FakeVideoCapturer); |
42 capture_format_ = capturer_->GetSupportedFormats()->at(0); | 31 capture_format_ = capturer_->GetSupportedFormats()->at(0); |
43 capture_format_.interval = VideoFormat::FpsToInterval(50); | 32 capture_format_.interval = VideoFormat::FpsToInterval(50); |
44 adapter_.reset(new VideoAdapter()); | 33 adapter_.reset(new VideoAdapter()); |
45 adapter_->SetInputFormat(capture_format_); | 34 adapter_->SetInputFormat(capture_format_); |
46 | 35 |
47 listener_.reset(new VideoCapturerListener(adapter_.get())); | 36 listener_.reset(new VideoCapturerListener(adapter_.get())); |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 EXPECT_EQ(640, adapter.output_format().width); | 394 EXPECT_EQ(640, adapter.output_format().width); |
406 EXPECT_EQ(400, adapter.output_format().height); | 395 EXPECT_EQ(400, adapter.output_format().height); |
407 | 396 |
408 // Server format request 1280x720, higher than input. Adapt nothing. | 397 // Server format request 1280x720, higher than input. Adapt nothing. |
409 format.width = 1280; | 398 format.width = 1280; |
410 format.height = 720; | 399 format.height = 720; |
411 adapter.OnOutputFormatRequest(format); | 400 adapter.OnOutputFormatRequest(format); |
412 EXPECT_EQ(640, adapter.output_format().width); | 401 EXPECT_EQ(640, adapter.output_format().width); |
413 EXPECT_EQ(400, adapter.output_format().height); | 402 EXPECT_EQ(400, adapter.output_format().height); |
414 | 403 |
415 // Cpu load is high, but cpu adaptation is disabled. Adapt nothing. | |
416 adapter.OnCpuLoadUpdated(1, 1, 0.99f, 0.99f); | |
417 EXPECT_EQ(640, adapter.output_format().width); | |
418 EXPECT_EQ(400, adapter.output_format().height); | |
419 | |
420 // Encoder resolution request: downgrade with different size. Adapt nothing. | 404 // Encoder resolution request: downgrade with different size. Adapt nothing. |
421 adapter.OnEncoderResolutionRequest(320, 200, | 405 adapter.OnEncoderResolutionRequest(320, 200, |
422 CoordinatedVideoAdapter::DOWNGRADE); | 406 CoordinatedVideoAdapter::DOWNGRADE); |
423 EXPECT_EQ(640, adapter.output_format().width); | 407 EXPECT_EQ(640, adapter.output_format().width); |
424 EXPECT_EQ(400, adapter.output_format().height); | 408 EXPECT_EQ(400, adapter.output_format().height); |
425 | 409 |
426 // Encoder resolution request: downgrade. | 410 // Encoder resolution request: downgrade. |
427 adapter.OnEncoderResolutionRequest(640, 400, | 411 adapter.OnEncoderResolutionRequest(640, 400, |
428 CoordinatedVideoAdapter::DOWNGRADE); | 412 CoordinatedVideoAdapter::DOWNGRADE); |
429 EXPECT_EQ(480, adapter.output_format().width); | 413 EXPECT_EQ(480, adapter.output_format().width); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 EXPECT_EQ(200, adapter.output_format().height); | 508 EXPECT_EQ(200, adapter.output_format().height); |
525 | 509 |
526 // Server format request 480x300. | 510 // Server format request 480x300. |
527 format.width = 480; | 511 format.width = 480; |
528 format.height = 300; | 512 format.height = 300; |
529 adapter.OnOutputFormatRequest(format); | 513 adapter.OnOutputFormatRequest(format); |
530 EXPECT_EQ(480, adapter.output_format().width); | 514 EXPECT_EQ(480, adapter.output_format().width); |
531 EXPECT_EQ(300, adapter.output_format().height); | 515 EXPECT_EQ(300, adapter.output_format().height); |
532 } | 516 } |
533 | 517 |
534 TEST(CoordinatedVideoAdapterTest, TestCoordinatedWithCpuAdaptation) { | |
535 CoordinatedVideoAdapter adapter; | |
536 adapter.set_cpu_adaptation(true); | |
537 EXPECT_FALSE(adapter.cpu_smoothing()); | |
538 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
539 adapter.SetInputFormat(format); | |
540 | |
541 // Server format request 640x400. | |
542 format.height = 400; | |
543 adapter.OnOutputFormatRequest(format); | |
544 EXPECT_EQ(640, adapter.output_format().width); | |
545 EXPECT_EQ(400, adapter.output_format().height); | |
546 | |
547 // Process load is medium, but system load is high. Downgrade. | |
548 UpdateCpuLoad(&adapter, 1, 1, 0.55f, 0.98f); | |
549 EXPECT_EQ(480, adapter.output_format().width); | |
550 EXPECT_EQ(300, adapter.output_format().height); | |
551 | |
552 // CPU high, but cpu adaptation disabled. Adapt nothing. | |
553 adapter.set_cpu_adaptation(false); | |
554 adapter.OnCpuLoadUpdated(1, 1, 0.55f, 0.98f); | |
555 EXPECT_EQ(480, adapter.output_format().width); | |
556 EXPECT_EQ(300, adapter.output_format().height); | |
557 adapter.set_cpu_adaptation(true); | |
558 | |
559 // System load is high, but time has not elaspsed. Adapt nothing. | |
560 adapter.set_cpu_load_min_samples(2); | |
561 adapter.OnCpuLoadUpdated(1, 1, 0.55f, 0.98f); | |
562 EXPECT_EQ(480, adapter.output_format().width); | |
563 EXPECT_EQ(300, adapter.output_format().height); | |
564 | |
565 // Process load is medium, but system load is high. Downgrade. | |
566 UpdateCpuLoad(&adapter, 1, 1, 0.55f, 0.98f); | |
567 EXPECT_EQ(320, adapter.output_format().width); | |
568 EXPECT_EQ(200, adapter.output_format().height); | |
569 | |
570 // Test reason for adapting is CPU. | |
571 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, | |
572 adapter.adapt_reason()); | |
573 | |
574 // Server format request 320x200. Same as CPU. Do nothing. | |
575 format.width = 320; | |
576 format.height = 200; | |
577 adapter.OnOutputFormatRequest(format); | |
578 EXPECT_EQ(320, adapter.output_format().width); | |
579 EXPECT_EQ(200, adapter.output_format().height); | |
580 | |
581 // Test reason for adapting is CPU and VIEW. | |
582 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU + | |
583 CoordinatedVideoAdapter::ADAPTREASON_VIEW, | |
584 adapter.adapt_reason()); | |
585 | |
586 // Process load and system load are normal. Adapt nothing. | |
587 UpdateCpuLoad(&adapter, 1, 1, 0.5f, 0.8f); | |
588 EXPECT_EQ(320, adapter.output_format().width); | |
589 EXPECT_EQ(200, adapter.output_format().height); | |
590 | |
591 // Process load and system load are low, but view is still low. Adapt nothing. | |
592 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f); | |
593 EXPECT_EQ(320, adapter.output_format().width); | |
594 EXPECT_EQ(200, adapter.output_format().height); | |
595 | |
596 // Test reason for adapting is VIEW. | |
597 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, | |
598 adapter.adapt_reason()); | |
599 | |
600 // Server format request 640x400. Cpu is still low. Upgrade. | |
601 format.width = 640; | |
602 format.height = 400; | |
603 adapter.OnOutputFormatRequest(format); | |
604 EXPECT_EQ(480, adapter.output_format().width); | |
605 EXPECT_EQ(300, adapter.output_format().height); | |
606 | |
607 // Test reason for adapting is CPU. | |
608 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, | |
609 adapter.adapt_reason()); | |
610 | |
611 // Encoder resolution request: downgrade. | |
612 adapter.OnEncoderResolutionRequest(480, 300, | |
613 CoordinatedVideoAdapter::DOWNGRADE); | |
614 EXPECT_EQ(320, adapter.output_format().width); | |
615 EXPECT_EQ(200, adapter.output_format().height); | |
616 | |
617 // Test reason for adapting is BANDWIDTH. | |
618 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH, | |
619 adapter.adapt_reason()); | |
620 | |
621 // Process load and system load are low. Constrained by GD. Adapt nothing | |
622 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f); | |
623 EXPECT_EQ(320, adapter.output_format().width); | |
624 EXPECT_EQ(200, adapter.output_format().height); | |
625 | |
626 // Encoder resolution request: upgrade. | |
627 adapter.OnEncoderResolutionRequest(320, 200, | |
628 CoordinatedVideoAdapter::UPGRADE); | |
629 EXPECT_EQ(480, adapter.output_format().width); | |
630 EXPECT_EQ(300, adapter.output_format().height); | |
631 | |
632 // Encoder resolution request: upgrade. Constrained by CPU. | |
633 adapter.OnEncoderResolutionRequest(480, 300, | |
634 CoordinatedVideoAdapter::UPGRADE); | |
635 EXPECT_EQ(480, adapter.output_format().width); | |
636 EXPECT_EQ(300, adapter.output_format().height); | |
637 | |
638 // Server format request 640x400. Constrained by CPU. | |
639 format.width = 640; | |
640 format.height = 400; | |
641 adapter.OnOutputFormatRequest(format); | |
642 EXPECT_EQ(480, adapter.output_format().width); | |
643 EXPECT_EQ(300, adapter.output_format().height); | |
644 } | |
645 | |
646 TEST(CoordinatedVideoAdapterTest, TestCoordinatedWithCpuRequest) { | |
647 CoordinatedVideoAdapter adapter; | |
648 adapter.set_cpu_adaptation(true); | |
649 EXPECT_FALSE(adapter.cpu_smoothing()); | |
650 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
651 adapter.SetInputFormat(format); | |
652 | |
653 // Server format request 640x400. | |
654 format.height = 400; | |
655 adapter.OnOutputFormatRequest(format); | |
656 EXPECT_EQ(640, adapter.output_format().width); | |
657 EXPECT_EQ(400, adapter.output_format().height); | |
658 | |
659 // CPU resolution request: downgrade. Adapt down. | |
660 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE); | |
661 EXPECT_EQ(480, adapter.output_format().width); | |
662 EXPECT_EQ(300, adapter.output_format().height); | |
663 | |
664 // CPU resolution request: keep. Do nothing. | |
665 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::KEEP); | |
666 EXPECT_EQ(480, adapter.output_format().width); | |
667 EXPECT_EQ(300, adapter.output_format().height); | |
668 | |
669 // CPU resolution request: downgrade, but cpu adaptation disabled. | |
670 // Adapt nothing. | |
671 adapter.set_cpu_adaptation(false); | |
672 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE); | |
673 EXPECT_EQ(480, adapter.output_format().width); | |
674 EXPECT_EQ(300, adapter.output_format().height); | |
675 | |
676 // CPU resolution request: downgrade. Adapt down. | |
677 adapter.set_cpu_adaptation(true); | |
678 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE); | |
679 EXPECT_EQ(320, adapter.output_format().width); | |
680 EXPECT_EQ(200, adapter.output_format().height); | |
681 | |
682 // Test reason for adapting is CPU. | |
683 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, | |
684 adapter.adapt_reason()); | |
685 | |
686 // CPU resolution request: downgrade, but already at minimum. Do nothing. | |
687 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE); | |
688 EXPECT_EQ(320, adapter.output_format().width); | |
689 EXPECT_EQ(200, adapter.output_format().height); | |
690 | |
691 // Server format request 320x200. Same as CPU. Do nothing. | |
692 format.width = 320; | |
693 format.height = 200; | |
694 adapter.OnOutputFormatRequest(format); | |
695 EXPECT_EQ(320, adapter.output_format().width); | |
696 EXPECT_EQ(200, adapter.output_format().height); | |
697 | |
698 // Test reason for adapting is CPU and VIEW. | |
699 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU + | |
700 CoordinatedVideoAdapter::ADAPTREASON_VIEW, | |
701 adapter.adapt_reason()); | |
702 | |
703 // CPU resolution request: upgrade, but view request still low. Do nothing. | |
704 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE); | |
705 EXPECT_EQ(320, adapter.output_format().width); | |
706 EXPECT_EQ(200, adapter.output_format().height); | |
707 | |
708 // Test reason for adapting is VIEW. | |
709 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, | |
710 adapter.adapt_reason()); | |
711 | |
712 // Server format request 640x400. Cpu is still low. Upgrade. | |
713 format.width = 640; | |
714 format.height = 400; | |
715 adapter.OnOutputFormatRequest(format); | |
716 EXPECT_EQ(480, adapter.output_format().width); | |
717 EXPECT_EQ(300, adapter.output_format().height); | |
718 | |
719 // Test reason for adapting is CPU. | |
720 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, | |
721 adapter.adapt_reason()); | |
722 | |
723 // Encoder resolution request: downgrade. | |
724 adapter.OnEncoderResolutionRequest(480, 300, | |
725 CoordinatedVideoAdapter::DOWNGRADE); | |
726 EXPECT_EQ(320, adapter.output_format().width); | |
727 EXPECT_EQ(200, adapter.output_format().height); | |
728 | |
729 // Test reason for adapting is BANDWIDTH. | |
730 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH, | |
731 adapter.adapt_reason()); | |
732 | |
733 // Process load and system load are low. Constrained by GD. Adapt nothing | |
734 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f); | |
735 EXPECT_EQ(320, adapter.output_format().width); | |
736 EXPECT_EQ(200, adapter.output_format().height); | |
737 | |
738 // Encoder resolution request: upgrade. | |
739 adapter.OnEncoderResolutionRequest(320, 200, | |
740 CoordinatedVideoAdapter::UPGRADE); | |
741 EXPECT_EQ(480, adapter.output_format().width); | |
742 EXPECT_EQ(300, adapter.output_format().height); | |
743 | |
744 // Encoder resolution request: upgrade. Constrained by CPU. | |
745 adapter.OnEncoderResolutionRequest(480, 300, | |
746 CoordinatedVideoAdapter::UPGRADE); | |
747 EXPECT_EQ(480, adapter.output_format().width); | |
748 EXPECT_EQ(300, adapter.output_format().height); | |
749 | |
750 // Server format request 640x400. Constrained by CPU. | |
751 format.width = 640; | |
752 format.height = 400; | |
753 adapter.OnOutputFormatRequest(format); | |
754 EXPECT_EQ(480, adapter.output_format().width); | |
755 EXPECT_EQ(300, adapter.output_format().height); | |
756 } | |
757 | |
758 TEST(CoordinatedVideoAdapterTest, TestViewRequestPlusCameraSwitch) { | 518 TEST(CoordinatedVideoAdapterTest, TestViewRequestPlusCameraSwitch) { |
759 CoordinatedVideoAdapter adapter; | 519 CoordinatedVideoAdapter adapter; |
760 adapter.set_view_switch(true); | 520 adapter.set_view_switch(true); |
761 | 521 |
762 // Start at HD. | 522 // Start at HD. |
763 VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), FOURCC_I420); | 523 VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), FOURCC_I420); |
764 adapter.SetInputFormat(format); | 524 adapter.SetInputFormat(format); |
765 EXPECT_EQ(format, adapter.input_format()); | 525 EXPECT_EQ(format, adapter.input_format()); |
766 EXPECT_TRUE(adapter.output_format().IsSize0x0()); | 526 EXPECT_TRUE(adapter.output_format().IsSize0x0()); |
767 | 527 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
809 adapter.OnOutputFormatRequest(format); | 569 adapter.OnOutputFormatRequest(format); |
810 // At this point, we have to adapt down to something lower. | 570 // At this point, we have to adapt down to something lower. |
811 EXPECT_EQ(480, adapter.output_format().width); | 571 EXPECT_EQ(480, adapter.output_format().width); |
812 EXPECT_EQ(360, adapter.output_format().height); | 572 EXPECT_EQ(360, adapter.output_format().height); |
813 | 573 |
814 // But if frames come in at 640x360, we shouldn't adapt them down. | 574 // But if frames come in at 640x360, we shouldn't adapt them down. |
815 // Fake a 640x360 frame. | 575 // Fake a 640x360 frame. |
816 VideoFormat out_format = adapter.AdaptFrameResolution(640, 360); | 576 VideoFormat out_format = adapter.AdaptFrameResolution(640, 360); |
817 EXPECT_EQ(640, out_format.width); | 577 EXPECT_EQ(640, out_format.width); |
818 EXPECT_EQ(360, out_format.height); | 578 EXPECT_EQ(360, out_format.height); |
819 | |
820 // Similarly, no-op adapt requests for other reasons shouldn't change | |
821 // adaptation state (before a previous bug, the previous EXPECTs would | |
822 // fail and the following would succeed, as the no-op CPU request would | |
823 // fix the adaptation state). | |
824 adapter.set_cpu_adaptation(true); | |
825 UpdateCpuLoad(&adapter, 1, 1, 0.7f, 0.7f); | |
826 out_format = adapter.AdaptFrameResolution(640, 360); | |
827 | |
828 EXPECT_EQ(640, out_format.width); | |
829 EXPECT_EQ(360, out_format.height); | |
830 } | 579 } |
831 | 580 |
832 // When adapting resolution for CPU or GD, the quantity of pixels that the | 581 // When adapting resolution for CPU or GD, the quantity of pixels that the |
833 // request is based on is reduced to half or double, and then an actual | 582 // request is based on is reduced to half or double, and then an actual |
834 // resolution is snapped to, rounding to the closest actual resolution. | 583 // resolution is snapped to, rounding to the closest actual resolution. |
835 // This works well for some tolerance to 3/4, odd widths and aspect ratios | 584 // This works well for some tolerance to 3/4, odd widths and aspect ratios |
836 // that dont exactly match, but is not best behavior for ViewRequests which | 585 // that dont exactly match, but is not best behavior for ViewRequests which |
837 // need to be be strictly respected to avoid going over the resolution budget | 586 // need to be be strictly respected to avoid going over the resolution budget |
838 // given to the codec - 854x480 total pixels. | 587 // given to the codec - 854x480 total pixels. |
839 // ViewRequest must find a lower resolution. | 588 // ViewRequest must find a lower resolution. |
(...skipping 11 matching lines...) Expand all Loading... |
851 format.width = 640; | 600 format.width = 640; |
852 format.height = 400; | 601 format.height = 400; |
853 adapter.OnOutputFormatRequest(format); | 602 adapter.OnOutputFormatRequest(format); |
854 EXPECT_EQ(640, adapter.output_format().width); | 603 EXPECT_EQ(640, adapter.output_format().width); |
855 EXPECT_EQ(360, adapter.output_format().height); | 604 EXPECT_EQ(360, adapter.output_format().height); |
856 | 605 |
857 // Test reason for adapting is VIEW. | 606 // Test reason for adapting is VIEW. |
858 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, adapter.adapt_reason()); | 607 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, adapter.adapt_reason()); |
859 } | 608 } |
860 | 609 |
861 // Test that we downgrade video for cpu up to two times. | |
862 TEST(CoordinatedVideoAdapterTest, TestCpuDowngradeTimes) { | |
863 CoordinatedVideoAdapter adapter; | |
864 adapter.set_cpu_adaptation(true); | |
865 EXPECT_FALSE(adapter.cpu_smoothing()); | |
866 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
867 adapter.SetInputFormat(format); | |
868 | |
869 // Server format request 640x400. | |
870 format.height = 400; | |
871 adapter.OnOutputFormatRequest(format); | |
872 EXPECT_EQ(640, adapter.output_format().width); | |
873 EXPECT_EQ(400, adapter.output_format().height); | |
874 | |
875 // Process load and system load are low. Do not change the cpu desired format | |
876 // and do not adapt. | |
877 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f); | |
878 EXPECT_EQ(640, adapter.output_format().width); | |
879 EXPECT_EQ(400, adapter.output_format().height); | |
880 | |
881 // System load is high. Downgrade. | |
882 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
883 EXPECT_EQ(480, adapter.output_format().width); | |
884 EXPECT_EQ(300, adapter.output_format().height); | |
885 | |
886 // System load is high. Downgrade again. | |
887 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
888 EXPECT_EQ(320, adapter.output_format().width); | |
889 EXPECT_EQ(200, adapter.output_format().height); | |
890 | |
891 // System load is still high. Do not downgrade any more. | |
892 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
893 EXPECT_EQ(320, adapter.output_format().width); | |
894 EXPECT_EQ(200, adapter.output_format().height); | |
895 | |
896 // Process load and system load are low. Upgrade. | |
897 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f); | |
898 EXPECT_EQ(480, adapter.output_format().width); | |
899 EXPECT_EQ(300, adapter.output_format().height); | |
900 | |
901 // System load is high. Downgrade. | |
902 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
903 EXPECT_EQ(320, adapter.output_format().width); | |
904 EXPECT_EQ(200, adapter.output_format().height); | |
905 | |
906 // System load is still high. Do not downgrade any more. | |
907 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
908 EXPECT_EQ(320, adapter.output_format().width); | |
909 EXPECT_EQ(200, adapter.output_format().height); | |
910 } | |
911 | |
912 // Test that we respect CPU adapter threshold values. | |
913 TEST(CoordinatedVideoAdapterTest, TestAdapterCpuThreshold) { | |
914 CoordinatedVideoAdapter adapter; | |
915 adapter.set_cpu_adaptation(true); | |
916 EXPECT_FALSE(adapter.cpu_smoothing()); | |
917 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
918 adapter.SetInputFormat(format); | |
919 | |
920 // Server format request 640x400. | |
921 format.height = 400; | |
922 adapter.OnOutputFormatRequest(format); | |
923 EXPECT_EQ(640, adapter.output_format().width); | |
924 EXPECT_EQ(400, adapter.output_format().height); | |
925 | |
926 // Process load and system load are low. Do not change the cpu desired format | |
927 // and do not adapt. | |
928 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f); | |
929 EXPECT_EQ(640, adapter.output_format().width); | |
930 EXPECT_EQ(400, adapter.output_format().height); | |
931 | |
932 // System load is high. Downgrade. | |
933 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
934 EXPECT_EQ(480, adapter.output_format().width); | |
935 EXPECT_EQ(300, adapter.output_format().height); | |
936 | |
937 // Test reason for adapting is CPU. | |
938 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, adapter.adapt_reason()); | |
939 | |
940 // System load is high. Normally downgrade but threshold is high. Do nothing. | |
941 adapter.set_high_system_threshold(0.98f); // Set threshold high. | |
942 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
943 EXPECT_EQ(480, adapter.output_format().width); | |
944 EXPECT_EQ(300, adapter.output_format().height); | |
945 | |
946 // System load is medium. Normally do nothing, threshold is low. Adapt down. | |
947 adapter.set_high_system_threshold(0.75f); // Set threshold low. | |
948 UpdateCpuLoad(&adapter, 1, 1, 0.8f, 0.8f); | |
949 EXPECT_EQ(320, adapter.output_format().width); | |
950 EXPECT_EQ(200, adapter.output_format().height); | |
951 } | |
952 | |
953 | |
954 // Test that for an upgrade cpu request, we actually upgrade the desired format; | |
955 // for a downgrade request, we downgrade from the output format. | |
956 TEST(CoordinatedVideoAdapterTest, TestRealCpuUpgrade) { | |
957 CoordinatedVideoAdapter adapter; | |
958 adapter.set_cpu_adaptation(true); | |
959 adapter.set_cpu_smoothing(true); | |
960 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
961 adapter.SetInputFormat(format); | |
962 | |
963 // Server format request 640x400. | |
964 format.width = 640; | |
965 format.height = 400; | |
966 adapter.OnOutputFormatRequest(format); | |
967 EXPECT_EQ(640, adapter.output_format().width); | |
968 EXPECT_EQ(400, adapter.output_format().height); | |
969 | |
970 // Process load and system load are low. Do not change the cpu desired format | |
971 // and do not adapt. | |
972 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f); | |
973 EXPECT_EQ(640, adapter.output_format().width); | |
974 EXPECT_EQ(400, adapter.output_format().height); | |
975 | |
976 // Server format request 320x200. | |
977 format.width = 320; | |
978 format.height = 200; | |
979 adapter.OnOutputFormatRequest(format); | |
980 EXPECT_EQ(320, adapter.output_format().width); | |
981 EXPECT_EQ(200, adapter.output_format().height); | |
982 | |
983 // Process load and system load are low. Do not change the cpu desired format | |
984 // and do not adapt. | |
985 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f); | |
986 EXPECT_EQ(320, adapter.output_format().width); | |
987 EXPECT_EQ(200, adapter.output_format().height); | |
988 | |
989 // Server format request 640x400. Set to 640x400 immediately. | |
990 format.width = 640; | |
991 format.height = 400; | |
992 adapter.OnOutputFormatRequest(format); | |
993 EXPECT_EQ(640, adapter.output_format().width); | |
994 EXPECT_EQ(400, adapter.output_format().height); | |
995 | |
996 // Server format request 320x200. | |
997 format.width = 320; | |
998 format.height = 200; | |
999 adapter.OnOutputFormatRequest(format); | |
1000 EXPECT_EQ(320, adapter.output_format().width); | |
1001 EXPECT_EQ(200, adapter.output_format().height); | |
1002 | |
1003 // Process load is high, but system is not. Do not change the cpu desired | |
1004 // format and do not adapt. | |
1005 for (size_t i = 0; i < 10; ++i) { | |
1006 UpdateCpuLoad(&adapter, 1, 1, 0.75f, 0.8f); | |
1007 } | |
1008 EXPECT_EQ(320, adapter.output_format().width); | |
1009 EXPECT_EQ(200, adapter.output_format().height); | |
1010 } | |
1011 | |
1012 // Test that for an upgrade encoder request, we actually upgrade the desired | 610 // Test that for an upgrade encoder request, we actually upgrade the desired |
1013 // format; for a downgrade request, we downgrade from the output format. | 611 // format; for a downgrade request, we downgrade from the output format. |
1014 TEST(CoordinatedVideoAdapterTest, TestRealEncoderUpgrade) { | 612 TEST(CoordinatedVideoAdapterTest, TestRealEncoderUpgrade) { |
1015 CoordinatedVideoAdapter adapter; | 613 CoordinatedVideoAdapter adapter; |
1016 adapter.set_cpu_adaptation(true); | 614 adapter.set_cpu_adaptation(true); |
1017 adapter.set_cpu_smoothing(true); | |
1018 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); | 615 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420); |
1019 adapter.SetInputFormat(format); | 616 adapter.SetInputFormat(format); |
1020 | 617 |
1021 // Server format request 640x400. | 618 // Server format request 640x400. |
1022 format.width = 640; | 619 format.width = 640; |
1023 format.height = 400; | 620 format.height = 400; |
1024 adapter.OnOutputFormatRequest(format); | 621 adapter.OnOutputFormatRequest(format); |
1025 EXPECT_EQ(640, adapter.output_format().width); | 622 EXPECT_EQ(640, adapter.output_format().width); |
1026 EXPECT_EQ(400, adapter.output_format().height); | 623 EXPECT_EQ(400, adapter.output_format().height); |
1027 | 624 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1132 | 729 |
1133 format.width = 640; | 730 format.width = 640; |
1134 format.height = 480; | 731 format.height = 480; |
1135 format.interval = VideoFormat::FpsToInterval(15); | 732 format.interval = VideoFormat::FpsToInterval(15); |
1136 adapter.SetInputFormat(format); | 733 adapter.SetInputFormat(format); |
1137 EXPECT_EQ(320, adapter.output_format().width); | 734 EXPECT_EQ(320, adapter.output_format().width); |
1138 EXPECT_EQ(240, adapter.output_format().height); | 735 EXPECT_EQ(240, adapter.output_format().height); |
1139 EXPECT_EQ(VideoFormat::FpsToInterval(15), adapter.output_format().interval); | 736 EXPECT_EQ(VideoFormat::FpsToInterval(15), adapter.output_format().interval); |
1140 } | 737 } |
1141 | 738 |
1142 // Test that we downgrade video for cpu up to two times. | |
1143 TEST_F(VideoAdapterTest, CpuDowngradeAndSignal) { | |
1144 CoordinatedVideoAdapter adapter; | |
1145 CpuAdapterListener cpu_listener; | |
1146 adapter.SignalCpuAdaptationUnable.connect( | |
1147 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled); | |
1148 | |
1149 adapter.set_cpu_adaptation(true); | |
1150 EXPECT_FALSE(adapter.cpu_smoothing()); | |
1151 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
1152 adapter.SetInputFormat(format); | |
1153 adapter.OnOutputFormatRequest(format); | |
1154 | |
1155 // System load is high. Downgrade. | |
1156 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1157 | |
1158 // System load is high. Downgrade again. | |
1159 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1160 | |
1161 // System load is still high. Do not downgrade any more. Ensure we have not | |
1162 // signalled until after the cpu warning though. | |
1163 EXPECT_TRUE(!cpu_listener.received_cpu_signal()); | |
1164 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1165 EXPECT_TRUE_WAIT(cpu_listener.received_cpu_signal(), kWaitTimeout); | |
1166 } | |
1167 | |
1168 // Test that we downgrade video for cpu up to two times. | |
1169 TEST_F(VideoAdapterTest, CpuDowngradeAndDontSignal) { | |
1170 CoordinatedVideoAdapter adapter; | |
1171 CpuAdapterListener cpu_listener; | |
1172 adapter.SignalCpuAdaptationUnable.connect( | |
1173 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled); | |
1174 | |
1175 adapter.set_cpu_adaptation(true); | |
1176 adapter.set_cpu_smoothing(true); | |
1177 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
1178 adapter.SetInputFormat(format); | |
1179 adapter.OnOutputFormatRequest(format); | |
1180 | |
1181 // System load is high. Downgrade. | |
1182 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1183 | |
1184 // System load is high, process is not, Do not downgrade again. | |
1185 UpdateCpuLoad(&adapter, 1, 1, 0.25f, 0.95f); | |
1186 | |
1187 // System load is high, process is not, Do not downgrade again and do not | |
1188 // signal. | |
1189 adapter.set_cpu_adaptation(false); | |
1190 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1191 rtc::Thread::Current()->ProcessMessages(kShortWaitTimeout); | |
1192 EXPECT_TRUE(!cpu_listener.received_cpu_signal()); | |
1193 adapter.set_cpu_adaptation(true); | |
1194 } | |
1195 | |
1196 // Test that we require enough time before we downgrade. | |
1197 TEST_F(VideoAdapterTest, CpuMinTimeRequirement) { | |
1198 CoordinatedVideoAdapter adapter; | |
1199 CpuAdapterListener cpu_listener; | |
1200 adapter.SignalCpuAdaptationUnable.connect( | |
1201 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled); | |
1202 | |
1203 adapter.set_cpu_adaptation(true); | |
1204 adapter.set_cpu_smoothing(true); | |
1205 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
1206 adapter.SetInputFormat(format); | |
1207 adapter.OnOutputFormatRequest(format); | |
1208 | |
1209 EXPECT_EQ(3, adapter.cpu_load_min_samples()); | |
1210 adapter.set_cpu_load_min_samples(5); | |
1211 | |
1212 for (size_t i = 0; i < 4; ++i) { | |
1213 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f); | |
1214 EXPECT_EQ(640, adapter.output_format().width); | |
1215 EXPECT_EQ(360, adapter.output_format().height); | |
1216 } | |
1217 // The computed cpu load should now be around 93.5%, with the coefficient of | |
1218 // 0.4 and a seed value of 0.5. That should be high enough to adapt, but it | |
1219 // isn't enough samples, so we shouldn't have adapted on any of the previous | |
1220 // samples. | |
1221 | |
1222 // One more sample is enough, though, once enough time has passed. | |
1223 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f); | |
1224 EXPECT_EQ(480, adapter.output_format().width); | |
1225 EXPECT_EQ(270, adapter.output_format().height); | |
1226 | |
1227 // Now the cpu is lower, but we still need enough samples to upgrade. | |
1228 for (size_t i = 0; i < 4; ++i) { | |
1229 adapter.OnCpuLoadUpdated(1, 1, 0.1f, 0.1f); | |
1230 EXPECT_EQ(480, adapter.output_format().width); | |
1231 EXPECT_EQ(270, adapter.output_format().height); | |
1232 } | |
1233 | |
1234 // One more sample is enough, once time has elapsed. | |
1235 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f); | |
1236 EXPECT_EQ(640, adapter.output_format().width); | |
1237 EXPECT_EQ(360, adapter.output_format().height); | |
1238 } | |
1239 | |
1240 TEST_F(VideoAdapterTest, CpuIgnoresSpikes) { | |
1241 CoordinatedVideoAdapter adapter; | |
1242 CpuAdapterListener cpu_listener; | |
1243 adapter.SignalCpuAdaptationUnable.connect( | |
1244 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled); | |
1245 | |
1246 adapter.set_cpu_adaptation(true); | |
1247 adapter.set_cpu_smoothing(true); | |
1248 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); | |
1249 adapter.SetInputFormat(format); | |
1250 adapter.OnOutputFormatRequest(format); | |
1251 | |
1252 // System load is high. Downgrade. | |
1253 for (size_t i = 0; i < 5; ++i) { | |
1254 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f); | |
1255 } | |
1256 EXPECT_EQ(480, adapter.output_format().width); | |
1257 EXPECT_EQ(270, adapter.output_format().height); | |
1258 | |
1259 // Now we're in a state where we could upgrade or downgrade, so get to a | |
1260 // steady state of about 75% cpu usage. | |
1261 for (size_t i = 0; i < 5; ++i) { | |
1262 UpdateCpuLoad(&adapter, 1, 1, 0.75f, 0.75f); | |
1263 EXPECT_EQ(480, adapter.output_format().width); | |
1264 EXPECT_EQ(270, adapter.output_format().height); | |
1265 } | |
1266 | |
1267 // Now, the cpu spikes for two samples, but then goes back to | |
1268 // normal. This shouldn't cause adaptation. | |
1269 UpdateCpuLoad(&adapter, 1, 1, 0.90f, 0.90f); | |
1270 UpdateCpuLoad(&adapter, 1, 1, 0.90f, 0.90f); | |
1271 EXPECT_EQ(480, adapter.output_format().width); | |
1272 EXPECT_EQ(270, adapter.output_format().height); | |
1273 // Back to the steady state for awhile. | |
1274 for (size_t i = 0; i < 5; ++i) { | |
1275 UpdateCpuLoad(&adapter, 1, 1, 0.75, 0.75); | |
1276 EXPECT_EQ(480, adapter.output_format().width); | |
1277 EXPECT_EQ(270, adapter.output_format().height); | |
1278 } | |
1279 | |
1280 // Now, system cpu usage is starting to drop down. But it takes a bit before | |
1281 // it gets all the way there. | |
1282 for (size_t i = 0; i < 10; ++i) { | |
1283 UpdateCpuLoad(&adapter, 1, 1, 0.5f, 0.5f); | |
1284 } | |
1285 EXPECT_EQ(640, adapter.output_format().width); | |
1286 EXPECT_EQ(360, adapter.output_format().height); | |
1287 } | |
1288 | |
1289 } // namespace cricket | 739 } // namespace cricket |
1290 #endif // HAVE_WEBRTC_VIDEO | 740 #endif // HAVE_WEBRTC_VIDEO |
OLD | NEW |