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 #include <limits.h> // For INT_MAX | 11 #include <limits.h> // For INT_MAX |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/gunit.h" | |
18 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
19 #include "webrtc/media/base/fakevideocapturer.h" | 18 #include "webrtc/media/base/fakevideocapturer.h" |
20 #include "webrtc/media/base/mediachannel.h" | 19 #include "webrtc/media/base/mediachannel.h" |
21 #include "webrtc/media/base/testutils.h" | 20 #include "webrtc/media/base/testutils.h" |
22 #include "webrtc/media/base/videoadapter.h" | 21 #include "webrtc/media/base/videoadapter.h" |
| 22 #include "webrtc/test/gtest.h" |
23 | 23 |
24 namespace cricket { | 24 namespace cricket { |
| 25 namespace { |
| 26 // Shorthand for no upper bound. |
| 27 static constexpr uint32_t kMax = std::numeric_limits<uint32_t>::max(); |
| 28 } // namespace |
25 | 29 |
26 class VideoAdapterTest : public testing::Test { | 30 class VideoAdapterTest : public testing::Test { |
27 public: | 31 public: |
28 virtual void SetUp() { | 32 virtual void SetUp() { |
29 capturer_.reset(new FakeVideoCapturer); | 33 capturer_.reset(new FakeVideoCapturer); |
30 capture_format_ = capturer_->GetSupportedFormats()->at(0); | 34 capture_format_ = capturer_->GetSupportedFormats()->at(0); |
31 capture_format_.interval = VideoFormat::FpsToInterval(30); | 35 capture_format_.interval = VideoFormat::FpsToInterval(30); |
32 | 36 |
33 listener_.reset(new VideoCapturerListener(&adapter_)); | 37 listener_.reset(new VideoCapturerListener(&adapter_)); |
34 capturer_->AddOrUpdateSink(listener_.get(), rtc::VideoSinkWants()); | 38 capturer_->AddOrUpdateSink(listener_.get(), rtc::VideoSinkWants()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 VideoAdapter* video_adapter_; | 112 VideoAdapter* video_adapter_; |
109 int cropped_width_; | 113 int cropped_width_; |
110 int cropped_height_; | 114 int cropped_height_; |
111 int out_width_; | 115 int out_width_; |
112 int out_height_; | 116 int out_height_; |
113 int captured_frames_; | 117 int captured_frames_; |
114 int dropped_frames_; | 118 int dropped_frames_; |
115 bool last_adapt_was_no_op_; | 119 bool last_adapt_was_no_op_; |
116 }; | 120 }; |
117 | 121 |
118 | |
119 void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats, | 122 void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats, |
120 int cropped_width, | 123 int cropped_width, |
121 int cropped_height, | 124 int cropped_height, |
122 int out_width, | 125 int out_width, |
123 int out_height) { | 126 int out_height) { |
124 EXPECT_EQ(cropped_width, stats.cropped_width); | 127 EXPECT_EQ(cropped_width, stats.cropped_width); |
125 EXPECT_EQ(cropped_height, stats.cropped_height); | 128 EXPECT_EQ(cropped_height, stats.cropped_height); |
126 EXPECT_EQ(out_width, stats.out_width); | 129 EXPECT_EQ(out_width, stats.out_width); |
127 EXPECT_EQ(out_height, stats.out_height); | 130 EXPECT_EQ(out_height, stats.out_height); |
128 } | 131 } |
129 | 132 |
| 133 void OnResolutionRequest(uint32_t min, uint32_t target, uint32_t max) { |
| 134 target = std::min<uint32_t>(target, max); |
| 135 EXPECT_LE(min, target); |
| 136 EXPECT_LE(target, max); |
| 137 adapter_.OnResolutionFramerateRequest( |
| 138 rtc::Optional<rtc::VideoSinkWants::Range>( |
| 139 rtc::VideoSinkWants::Range(min, target, max)), |
| 140 rtc::Optional<rtc::VideoSinkWants::Range>()); |
| 141 } |
| 142 |
130 std::unique_ptr<FakeVideoCapturer> capturer_; | 143 std::unique_ptr<FakeVideoCapturer> capturer_; |
131 VideoAdapter adapter_; | 144 VideoAdapter adapter_; |
132 int cropped_width_; | 145 int cropped_width_; |
133 int cropped_height_; | 146 int cropped_height_; |
134 int out_width_; | 147 int out_width_; |
135 int out_height_; | 148 int out_height_; |
136 std::unique_ptr<VideoCapturerListener> listener_; | 149 std::unique_ptr<VideoCapturerListener> listener_; |
137 VideoFormat capture_format_; | 150 VideoFormat capture_format_; |
138 }; | 151 }; |
139 | 152 |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) { | 702 TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) { |
690 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 703 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
691 &cropped_width_, &cropped_height_, | 704 &cropped_width_, &cropped_height_, |
692 &out_width_, &out_height_)); | 705 &out_width_, &out_height_)); |
693 EXPECT_EQ(1280, cropped_width_); | 706 EXPECT_EQ(1280, cropped_width_); |
694 EXPECT_EQ(720, cropped_height_); | 707 EXPECT_EQ(720, cropped_height_); |
695 EXPECT_EQ(1280, out_width_); | 708 EXPECT_EQ(1280, out_width_); |
696 EXPECT_EQ(720, out_height_); | 709 EXPECT_EQ(720, out_height_); |
697 | 710 |
698 // Adapt down one step. | 711 // Adapt down one step. |
699 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 712 OnResolutionRequest(0, kMax, 1280 * 720 - 1); |
700 rtc::Optional<int>(1280 * 720 - 1)); | |
701 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 713 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
702 &cropped_width_, &cropped_height_, | 714 &cropped_width_, &cropped_height_, |
703 &out_width_, &out_height_)); | 715 &out_width_, &out_height_)); |
704 EXPECT_EQ(1280, cropped_width_); | 716 EXPECT_EQ(1280, cropped_width_); |
705 EXPECT_EQ(720, cropped_height_); | 717 EXPECT_EQ(720, cropped_height_); |
706 EXPECT_EQ(960, out_width_); | 718 EXPECT_EQ(960, out_width_); |
707 EXPECT_EQ(540, out_height_); | 719 EXPECT_EQ(540, out_height_); |
708 | 720 |
709 // Adapt down one step more. | 721 // Adapt down one step more. |
710 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 722 OnResolutionRequest(0, kMax, 960 * 540 - 1); |
711 rtc::Optional<int>(960 * 540 - 1)); | |
712 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 723 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
713 &cropped_width_, &cropped_height_, | 724 &cropped_width_, &cropped_height_, |
714 &out_width_, &out_height_)); | 725 &out_width_, &out_height_)); |
715 EXPECT_EQ(1280, cropped_width_); | 726 EXPECT_EQ(1280, cropped_width_); |
716 EXPECT_EQ(720, cropped_height_); | 727 EXPECT_EQ(720, cropped_height_); |
717 EXPECT_EQ(640, out_width_); | 728 EXPECT_EQ(640, out_width_); |
718 EXPECT_EQ(360, out_height_); | 729 EXPECT_EQ(360, out_height_); |
719 | 730 |
720 // Adapt down one step more. | 731 // Adapt down one step more. |
721 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 732 OnResolutionRequest(0, kMax, 640 * 360 - 1); |
722 rtc::Optional<int>(640 * 360 - 1)); | |
723 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 733 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
724 &cropped_width_, &cropped_height_, | 734 &cropped_width_, &cropped_height_, |
725 &out_width_, &out_height_)); | 735 &out_width_, &out_height_)); |
726 EXPECT_EQ(1280, cropped_width_); | 736 EXPECT_EQ(1280, cropped_width_); |
727 EXPECT_EQ(720, cropped_height_); | 737 EXPECT_EQ(720, cropped_height_); |
728 EXPECT_EQ(480, out_width_); | 738 EXPECT_EQ(480, out_width_); |
729 EXPECT_EQ(270, out_height_); | 739 EXPECT_EQ(270, out_height_); |
730 | 740 |
731 // Adapt up one step. | 741 // Adapt up one step. |
732 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360), | 742 OnResolutionRequest(0, 640 * 360, 960 * 540); |
733 rtc::Optional<int>(960 * 540)); | |
734 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 743 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
735 &cropped_width_, &cropped_height_, | 744 &cropped_width_, &cropped_height_, |
736 &out_width_, &out_height_)); | 745 &out_width_, &out_height_)); |
737 EXPECT_EQ(1280, cropped_width_); | 746 EXPECT_EQ(1280, cropped_width_); |
738 EXPECT_EQ(720, cropped_height_); | 747 EXPECT_EQ(720, cropped_height_); |
739 EXPECT_EQ(640, out_width_); | 748 EXPECT_EQ(640, out_width_); |
740 EXPECT_EQ(360, out_height_); | 749 EXPECT_EQ(360, out_height_); |
741 | 750 |
742 // Adapt up one step more. | 751 // Adapt up one step more. |
743 adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540), | 752 OnResolutionRequest(0, 960 * 540, 1280 * 720); |
744 rtc::Optional<int>(1280 * 720)); | |
745 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 753 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
746 &cropped_width_, &cropped_height_, | 754 &cropped_width_, &cropped_height_, |
747 &out_width_, &out_height_)); | 755 &out_width_, &out_height_)); |
748 EXPECT_EQ(1280, cropped_width_); | 756 EXPECT_EQ(1280, cropped_width_); |
749 EXPECT_EQ(720, cropped_height_); | 757 EXPECT_EQ(720, cropped_height_); |
750 EXPECT_EQ(960, out_width_); | 758 EXPECT_EQ(960, out_width_); |
751 EXPECT_EQ(540, out_height_); | 759 EXPECT_EQ(540, out_height_); |
752 | 760 |
753 // Adapt up one step more. | 761 // Adapt up one step more. |
754 adapter_.OnResolutionRequest(rtc::Optional<int>(1280 * 720), | 762 OnResolutionRequest(0, 1280 * 720, 1920 * 1080); |
755 rtc::Optional<int>(1920 * 1080)); | |
756 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 763 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
757 &cropped_width_, &cropped_height_, | 764 &cropped_width_, &cropped_height_, |
758 &out_width_, &out_height_)); | 765 &out_width_, &out_height_)); |
759 EXPECT_EQ(1280, cropped_width_); | 766 EXPECT_EQ(1280, cropped_width_); |
760 EXPECT_EQ(720, cropped_height_); | 767 EXPECT_EQ(720, cropped_height_); |
761 EXPECT_EQ(1280, out_width_); | 768 EXPECT_EQ(1280, out_width_); |
762 EXPECT_EQ(720, out_height_); | 769 EXPECT_EQ(720, out_height_); |
763 } | 770 } |
764 | 771 |
765 TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) { | 772 TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) { |
766 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 773 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
767 &cropped_width_, &cropped_height_, | 774 &cropped_width_, &cropped_height_, |
768 &out_width_, &out_height_)); | 775 &out_width_, &out_height_)); |
769 EXPECT_EQ(1280, cropped_width_); | 776 EXPECT_EQ(1280, cropped_width_); |
770 EXPECT_EQ(720, cropped_height_); | 777 EXPECT_EQ(720, cropped_height_); |
771 EXPECT_EQ(1280, out_width_); | 778 EXPECT_EQ(1280, out_width_); |
772 EXPECT_EQ(720, out_height_); | 779 EXPECT_EQ(720, out_height_); |
773 | 780 |
774 adapter_.OnResolutionRequest(rtc::Optional<int>(), rtc::Optional<int>(0)); | 781 OnResolutionRequest(0, kMax, 0); |
775 EXPECT_FALSE(adapter_.AdaptFrameResolution(1280, 720, 0, | 782 EXPECT_FALSE(adapter_.AdaptFrameResolution(1280, 720, 0, |
776 &cropped_width_, &cropped_height_, | 783 &cropped_width_, &cropped_height_, |
777 &out_width_, &out_height_)); | 784 &out_width_, &out_height_)); |
778 } | 785 } |
779 | 786 |
780 TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) { | 787 TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) { |
781 // Large step down. | 788 // Large step down. |
782 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 789 OnResolutionRequest(0, kMax, 640 * 360 - 1); |
783 rtc::Optional<int>(640 * 360 - 1)); | |
784 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 790 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
785 &cropped_width_, &cropped_height_, | 791 &cropped_width_, &cropped_height_, |
786 &out_width_, &out_height_)); | 792 &out_width_, &out_height_)); |
787 EXPECT_EQ(1280, cropped_width_); | 793 EXPECT_EQ(1280, cropped_width_); |
788 EXPECT_EQ(720, cropped_height_); | 794 EXPECT_EQ(720, cropped_height_); |
789 EXPECT_EQ(480, out_width_); | 795 EXPECT_EQ(480, out_width_); |
790 EXPECT_EQ(270, out_height_); | 796 EXPECT_EQ(270, out_height_); |
791 | 797 |
792 // Large step up. | 798 // Large step up. |
793 adapter_.OnResolutionRequest(rtc::Optional<int>(1280 * 720), | 799 OnResolutionRequest(0, 1280 * 720, 1920 * 1080); |
794 rtc::Optional<int>(1920 * 1080)); | |
795 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 800 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
796 &cropped_width_, &cropped_height_, | 801 &cropped_width_, &cropped_height_, |
797 &out_width_, &out_height_)); | 802 &out_width_, &out_height_)); |
798 EXPECT_EQ(1280, cropped_width_); | 803 EXPECT_EQ(1280, cropped_width_); |
799 EXPECT_EQ(720, cropped_height_); | 804 EXPECT_EQ(720, cropped_height_); |
800 EXPECT_EQ(1280, out_width_); | 805 EXPECT_EQ(1280, out_width_); |
801 EXPECT_EQ(720, out_height_); | 806 EXPECT_EQ(720, out_height_); |
802 } | 807 } |
803 | 808 |
804 TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) { | 809 TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) { |
805 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 810 OnResolutionRequest(0, kMax, 640 * 360 - 1); |
806 rtc::Optional<int>(640 * 360 - 1)); | |
807 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 811 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
808 &cropped_width_, &cropped_height_, | 812 &cropped_width_, &cropped_height_, |
809 &out_width_, &out_height_)); | 813 &out_width_, &out_height_)); |
810 EXPECT_EQ(1280, cropped_width_); | 814 EXPECT_EQ(1280, cropped_width_); |
811 EXPECT_EQ(720, cropped_height_); | 815 EXPECT_EQ(720, cropped_height_); |
812 EXPECT_EQ(480, out_width_); | 816 EXPECT_EQ(480, out_width_); |
813 EXPECT_EQ(270, out_height_); | 817 EXPECT_EQ(270, out_height_); |
814 | 818 |
815 VideoFormat new_format(640, 360, 0, FOURCC_I420); | 819 VideoFormat new_format(640, 360, 0, FOURCC_I420); |
816 adapter_.OnOutputFormatRequest(new_format); | 820 adapter_.OnOutputFormatRequest(new_format); |
817 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 821 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
818 &cropped_width_, &cropped_height_, | 822 &cropped_width_, &cropped_height_, |
819 &out_width_, &out_height_)); | 823 &out_width_, &out_height_)); |
820 EXPECT_EQ(1280, cropped_width_); | 824 EXPECT_EQ(1280, cropped_width_); |
821 EXPECT_EQ(720, cropped_height_); | 825 EXPECT_EQ(720, cropped_height_); |
822 EXPECT_EQ(480, out_width_); | 826 EXPECT_EQ(480, out_width_); |
823 EXPECT_EQ(270, out_height_); | 827 EXPECT_EQ(270, out_height_); |
824 | 828 |
825 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 829 OnResolutionRequest(0, 960 * 720, kMax); |
826 rtc::Optional<int>(960 * 720)); | |
827 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 830 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
828 &cropped_width_, &cropped_height_, | 831 &cropped_width_, &cropped_height_, |
829 &out_width_, &out_height_)); | 832 &out_width_, &out_height_)); |
830 EXPECT_EQ(1280, cropped_width_); | 833 EXPECT_EQ(1280, cropped_width_); |
831 EXPECT_EQ(720, cropped_height_); | 834 EXPECT_EQ(720, cropped_height_); |
832 EXPECT_EQ(640, out_width_); | 835 EXPECT_EQ(640, out_width_); |
833 EXPECT_EQ(360, out_height_); | 836 EXPECT_EQ(360, out_height_); |
834 } | 837 } |
835 | 838 |
836 TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) { | 839 TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) { |
837 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 840 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
838 &cropped_width_, &cropped_height_, | 841 &cropped_width_, &cropped_height_, |
839 &out_width_, &out_height_)); | 842 &out_width_, &out_height_)); |
840 EXPECT_EQ(1280, cropped_width_); | 843 EXPECT_EQ(1280, cropped_width_); |
841 EXPECT_EQ(720, cropped_height_); | 844 EXPECT_EQ(720, cropped_height_); |
842 EXPECT_EQ(1280, out_width_); | 845 EXPECT_EQ(1280, out_width_); |
843 EXPECT_EQ(720, out_height_); | 846 EXPECT_EQ(720, out_height_); |
844 | 847 |
845 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 848 OnResolutionRequest(0, kMax, 640 * 360 - 1); |
846 rtc::Optional<int>(640 * 360 - 1)); | |
847 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 849 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
848 &cropped_width_, &cropped_height_, | 850 &cropped_width_, &cropped_height_, |
849 &out_width_, &out_height_)); | 851 &out_width_, &out_height_)); |
850 EXPECT_EQ(1280, cropped_width_); | 852 EXPECT_EQ(1280, cropped_width_); |
851 EXPECT_EQ(720, cropped_height_); | 853 EXPECT_EQ(720, cropped_height_); |
852 EXPECT_EQ(480, out_width_); | 854 EXPECT_EQ(480, out_width_); |
853 EXPECT_EQ(270, out_height_); | 855 EXPECT_EQ(270, out_height_); |
854 | 856 |
855 adapter_.OnResolutionRequest(rtc::Optional<int>(), rtc::Optional<int>()); | 857 OnResolutionRequest(0, kMax, kMax); |
856 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, | 858 EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
857 &cropped_width_, &cropped_height_, | 859 &cropped_width_, &cropped_height_, |
858 &out_width_, &out_height_)); | 860 &out_width_, &out_height_)); |
859 EXPECT_EQ(1280, cropped_width_); | 861 EXPECT_EQ(1280, cropped_width_); |
860 EXPECT_EQ(720, cropped_height_); | 862 EXPECT_EQ(720, cropped_height_); |
861 EXPECT_EQ(1280, out_width_); | 863 EXPECT_EQ(1280, out_width_); |
862 EXPECT_EQ(720, out_height_); | 864 EXPECT_EQ(720, out_height_); |
863 } | 865 } |
864 | 866 |
865 TEST_F(VideoAdapterTest, TestCroppingWithResolutionRequest) { | 867 TEST_F(VideoAdapterTest, TestCroppingWithResolutionRequest) { |
866 // Ask for 640x360 (16:9 aspect). | 868 // Ask for 640x360 (16:9 aspect). |
867 adapter_.OnOutputFormatRequest(VideoFormat(640, 360, 0, FOURCC_I420)); | 869 adapter_.OnOutputFormatRequest(VideoFormat(640, 360, 0, FOURCC_I420)); |
868 // Send 640x480 (4:3 aspect). | 870 // Send 640x480 (4:3 aspect). |
869 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 871 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
870 &cropped_width_, &cropped_height_, | 872 &cropped_width_, &cropped_height_, |
871 &out_width_, &out_height_)); | 873 &out_width_, &out_height_)); |
872 // Expect cropping to 16:9 format and no scaling. | 874 // Expect cropping to 16:9 format and no scaling. |
873 EXPECT_EQ(640, cropped_width_); | 875 EXPECT_EQ(640, cropped_width_); |
874 EXPECT_EQ(360, cropped_height_); | 876 EXPECT_EQ(360, cropped_height_); |
875 EXPECT_EQ(640, out_width_); | 877 EXPECT_EQ(640, out_width_); |
876 EXPECT_EQ(360, out_height_); | 878 EXPECT_EQ(360, out_height_); |
877 | 879 |
878 // Adapt down one step. | 880 // Adapt down one step. |
879 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 881 OnResolutionRequest(0, kMax, 640 * 360 - 1); |
880 rtc::Optional<int>(640 * 360 - 1)); | |
881 // Expect cropping to 16:9 format and 3/4 scaling. | 882 // Expect cropping to 16:9 format and 3/4 scaling. |
882 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 883 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
883 &cropped_width_, &cropped_height_, | 884 &cropped_width_, &cropped_height_, |
884 &out_width_, &out_height_)); | 885 &out_width_, &out_height_)); |
885 EXPECT_EQ(640, cropped_width_); | 886 EXPECT_EQ(640, cropped_width_); |
886 EXPECT_EQ(360, cropped_height_); | 887 EXPECT_EQ(360, cropped_height_); |
887 EXPECT_EQ(480, out_width_); | 888 EXPECT_EQ(480, out_width_); |
888 EXPECT_EQ(270, out_height_); | 889 EXPECT_EQ(270, out_height_); |
889 | 890 |
890 // Adapt down one step more. | 891 // Adapt down one step more. |
891 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 892 OnResolutionRequest(0, kMax, 480 * 270 - 1); |
892 rtc::Optional<int>(480 * 270 - 1)); | |
893 // Expect cropping to 16:9 format and 1/2 scaling. | 893 // Expect cropping to 16:9 format and 1/2 scaling. |
894 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 894 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
895 &cropped_width_, &cropped_height_, | 895 &cropped_width_, &cropped_height_, |
896 &out_width_, &out_height_)); | 896 &out_width_, &out_height_)); |
897 EXPECT_EQ(640, cropped_width_); | 897 EXPECT_EQ(640, cropped_width_); |
898 EXPECT_EQ(360, cropped_height_); | 898 EXPECT_EQ(360, cropped_height_); |
899 EXPECT_EQ(320, out_width_); | 899 EXPECT_EQ(320, out_width_); |
900 EXPECT_EQ(180, out_height_); | 900 EXPECT_EQ(180, out_height_); |
901 | 901 |
902 // Adapt up one step. | 902 // Adapt up one step. |
903 adapter_.OnResolutionRequest(rtc::Optional<int>(480 * 270), | 903 OnResolutionRequest(0, 480 * 270, 640 * 360); |
904 rtc::Optional<int>(640 * 360)); | |
905 // Expect cropping to 16:9 format and 3/4 scaling. | 904 // Expect cropping to 16:9 format and 3/4 scaling. |
906 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 905 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
907 &cropped_width_, &cropped_height_, | 906 &cropped_width_, &cropped_height_, |
908 &out_width_, &out_height_)); | 907 &out_width_, &out_height_)); |
909 EXPECT_EQ(640, cropped_width_); | 908 EXPECT_EQ(640, cropped_width_); |
910 EXPECT_EQ(360, cropped_height_); | 909 EXPECT_EQ(360, cropped_height_); |
911 EXPECT_EQ(480, out_width_); | 910 EXPECT_EQ(480, out_width_); |
912 EXPECT_EQ(270, out_height_); | 911 EXPECT_EQ(270, out_height_); |
913 | 912 |
914 // Adapt up one step more. | 913 // Adapt up one step more. |
915 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360), | 914 OnResolutionRequest(0, 640 * 360, 960 * 540); |
916 rtc::Optional<int>(960 * 540)); | |
917 // Expect cropping to 16:9 format and no scaling. | 915 // Expect cropping to 16:9 format and no scaling. |
918 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 916 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
919 &cropped_width_, &cropped_height_, | 917 &cropped_width_, &cropped_height_, |
920 &out_width_, &out_height_)); | 918 &out_width_, &out_height_)); |
921 EXPECT_EQ(640, cropped_width_); | 919 EXPECT_EQ(640, cropped_width_); |
922 EXPECT_EQ(360, cropped_height_); | 920 EXPECT_EQ(360, cropped_height_); |
923 EXPECT_EQ(640, out_width_); | 921 EXPECT_EQ(640, out_width_); |
924 EXPECT_EQ(360, out_height_); | 922 EXPECT_EQ(360, out_height_); |
925 | 923 |
926 // Try to adapt up one step more. | 924 // Try to adapt up one step more. |
927 adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540), | 925 OnResolutionRequest(0, 960 * 540, 1280 * 720); |
928 rtc::Optional<int>(1280 * 720)); | |
929 // Expect cropping to 16:9 format and no scaling. | 926 // Expect cropping to 16:9 format and no scaling. |
930 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 927 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
931 &cropped_width_, &cropped_height_, | 928 &cropped_width_, &cropped_height_, |
932 &out_width_, &out_height_)); | 929 &out_width_, &out_height_)); |
933 EXPECT_EQ(640, cropped_width_); | 930 EXPECT_EQ(640, cropped_width_); |
934 EXPECT_EQ(360, cropped_height_); | 931 EXPECT_EQ(360, cropped_height_); |
935 EXPECT_EQ(640, out_width_); | 932 EXPECT_EQ(640, out_width_); |
936 EXPECT_EQ(360, out_height_); | 933 EXPECT_EQ(360, out_height_); |
937 } | 934 } |
938 | 935 |
939 TEST_F(VideoAdapterTest, TestCroppingOddResolution) { | 936 TEST_F(VideoAdapterTest, TestCroppingOddResolution) { |
940 // Ask for 640x360 (16:9 aspect), with 3/16 scaling. | 937 // Ask for 640x360 (16:9 aspect), with 3/16 scaling. |
941 adapter_.OnOutputFormatRequest( | 938 adapter_.OnOutputFormatRequest( |
942 VideoFormat(640, 360, 0, FOURCC_I420)); | 939 VideoFormat(640, 360, 0, FOURCC_I420)); |
943 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 940 OnResolutionRequest(0, kMax, 640 * 360 * 3 / 16 * 3 / 16); |
944 rtc::Optional<int>(640 * 360 * 3 / 16 * 3 / 16)); | |
945 | 941 |
946 // Send 640x480 (4:3 aspect). | 942 // Send 640x480 (4:3 aspect). |
947 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, | 943 EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
948 &cropped_width_, &cropped_height_, | 944 &cropped_width_, &cropped_height_, |
949 &out_width_, &out_height_)); | 945 &out_width_, &out_height_)); |
950 | 946 |
951 // Instead of getting the exact aspect ratio with cropped resolution 640x360, | 947 // Instead of getting the exact aspect ratio with cropped resolution 640x360, |
952 // the resolution should be adjusted to get a perfect scale factor instead. | 948 // the resolution should be adjusted to get a perfect scale factor instead. |
953 EXPECT_EQ(640, cropped_width_); | 949 EXPECT_EQ(640, cropped_width_); |
954 EXPECT_EQ(368, cropped_height_); | 950 EXPECT_EQ(368, cropped_height_); |
955 EXPECT_EQ(120, out_width_); | 951 EXPECT_EQ(120, out_width_); |
956 EXPECT_EQ(69, out_height_); | 952 EXPECT_EQ(69, out_height_); |
957 } | 953 } |
958 | 954 |
959 TEST_F(VideoAdapterTest, TestAdaptToVerySmallResolution) { | 955 TEST_F(VideoAdapterTest, TestAdaptToVerySmallResolution) { |
960 // Ask for 1920x1080 (16:9 aspect), with 1/16 scaling. | 956 // Ask for 1920x1080 (16:9 aspect), with 1/16 scaling. |
961 const int w = 1920; | 957 const int w = 1920; |
962 const int h = 1080; | 958 const int h = 1080; |
963 adapter_.OnOutputFormatRequest(VideoFormat(w, h, 0, FOURCC_I420)); | 959 adapter_.OnOutputFormatRequest(VideoFormat(w, h, 0, FOURCC_I420)); |
964 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 960 OnResolutionRequest(0, kMax, w * h * 1 / 16 * 1 / 16); |
965 rtc::Optional<int>(w * h * 1 / 16 * 1 / 16)); | |
966 | 961 |
967 // Send 1920x1080 (16:9 aspect). | 962 // Send 1920x1080 (16:9 aspect). |
968 EXPECT_TRUE(adapter_.AdaptFrameResolution( | 963 EXPECT_TRUE(adapter_.AdaptFrameResolution( |
969 w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); | 964 w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); |
970 | 965 |
971 // Instead of getting the exact aspect ratio with cropped resolution 1920x1080 | 966 // Instead of getting the exact aspect ratio with cropped resolution 1920x1080 |
972 // the resolution should be adjusted to get a perfect scale factor instead. | 967 // the resolution should be adjusted to get a perfect scale factor instead. |
973 EXPECT_EQ(1920, cropped_width_); | 968 EXPECT_EQ(1920, cropped_width_); |
974 EXPECT_EQ(1072, cropped_height_); | 969 EXPECT_EQ(1072, cropped_height_); |
975 EXPECT_EQ(120, out_width_); | 970 EXPECT_EQ(120, out_width_); |
976 EXPECT_EQ(67, out_height_); | 971 EXPECT_EQ(67, out_height_); |
977 | 972 |
978 // Adapt back up one step to 3/32. | 973 // Adapt back up one step to 3/32. |
979 adapter_.OnResolutionRequest(rtc::Optional<int>(w * h * 3 / 32 * 3 / 32), | 974 OnResolutionRequest(0, w * h * 3 / 32 * 3 / 32, w * h * 1 / 8 * 1 / 8); |
980 rtc::Optional<int>(w * h * 1 / 8 * 1 / 8)); | |
981 | 975 |
982 // Send 1920x1080 (16:9 aspect). | 976 // Send 1920x1080 (16:9 aspect). |
983 EXPECT_TRUE(adapter_.AdaptFrameResolution( | 977 EXPECT_TRUE(adapter_.AdaptFrameResolution( |
984 w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); | 978 w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); |
985 | 979 |
986 EXPECT_EQ(180, out_width_); | 980 EXPECT_EQ(180, out_width_); |
987 EXPECT_EQ(99, out_height_); | 981 EXPECT_EQ(99, out_height_); |
988 } | 982 } |
989 | 983 |
990 TEST_F(VideoAdapterTest, AdaptFrameResolutionDropWithResolutionRequest) { | 984 TEST_F(VideoAdapterTest, AdaptFrameResolutionDropWithResolutionRequest) { |
991 VideoFormat output_format = capture_format_; | 985 VideoFormat output_format = capture_format_; |
992 output_format.width = 0; | 986 output_format.width = 0; |
993 output_format.height = 0; | 987 output_format.height = 0; |
994 adapter_.OnOutputFormatRequest(output_format); | 988 adapter_.OnOutputFormatRequest(output_format); |
995 EXPECT_FALSE(adapter_.AdaptFrameResolution( | 989 EXPECT_FALSE(adapter_.AdaptFrameResolution( |
996 capture_format_.width, capture_format_.height, 0, | 990 capture_format_.width, capture_format_.height, 0, |
997 &cropped_width_, &cropped_height_, | 991 &cropped_width_, &cropped_height_, |
998 &out_width_, &out_height_)); | 992 &out_width_, &out_height_)); |
999 | 993 |
1000 adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540), | 994 OnResolutionRequest(0, 960 * 540, kMax); |
1001 rtc::Optional<int>()); | |
1002 | 995 |
1003 // Still expect all frames to be dropped | 996 // Still expect all frames to be dropped |
1004 EXPECT_FALSE(adapter_.AdaptFrameResolution( | 997 EXPECT_FALSE(adapter_.AdaptFrameResolution( |
1005 capture_format_.width, capture_format_.height, 0, | 998 capture_format_.width, capture_format_.height, 0, |
1006 &cropped_width_, &cropped_height_, | 999 &cropped_width_, &cropped_height_, |
1007 &out_width_, &out_height_)); | 1000 &out_width_, &out_height_)); |
1008 | 1001 |
1009 adapter_.OnResolutionRequest(rtc::Optional<int>(), | 1002 OnResolutionRequest(0, kMax, 640 * 480 - 1); |
1010 rtc::Optional<int>(640 * 480 - 1)); | |
1011 | 1003 |
1012 // Still expect all frames to be dropped | 1004 // Still expect all frames to be dropped |
1013 EXPECT_FALSE(adapter_.AdaptFrameResolution( | 1005 EXPECT_FALSE(adapter_.AdaptFrameResolution( |
1014 capture_format_.width, capture_format_.height, 0, | 1006 capture_format_.width, capture_format_.height, 0, |
1015 &cropped_width_, &cropped_height_, | 1007 &cropped_width_, &cropped_height_, |
1016 &out_width_, &out_height_)); | 1008 &out_width_, &out_height_)); |
1017 } | 1009 } |
1018 | 1010 |
1019 } // namespace cricket | 1011 } // namespace cricket |
OLD | NEW |