OLD | NEW |
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 25 matching lines...) Expand all Loading... |
36 | 36 |
37 namespace webrtc { | 37 namespace webrtc { |
38 | 38 |
39 using DegredationPreference = VideoSendStream::DegradationPreference; | 39 using DegredationPreference = VideoSendStream::DegradationPreference; |
40 using ScaleReason = ScalingObserverInterface::ScaleReason; | 40 using ScaleReason = ScalingObserverInterface::ScaleReason; |
41 using ::testing::_; | 41 using ::testing::_; |
42 using ::testing::Return; | 42 using ::testing::Return; |
43 | 43 |
44 namespace { | 44 namespace { |
45 const size_t kMaxPayloadLength = 1440; | 45 const size_t kMaxPayloadLength = 1440; |
46 const int kTargetBitrateBps = 1000000; | 46 const int kTargetBitrateBps = 100000; |
47 const int kLowTargetBitrateBps = kTargetBitrateBps / 10; | |
48 const int kMaxInitialFramedrop = 4; | |
49 | 47 |
50 class TestBuffer : public webrtc::I420Buffer { | 48 class TestBuffer : public webrtc::I420Buffer { |
51 public: | 49 public: |
52 TestBuffer(rtc::Event* event, int width, int height) | 50 TestBuffer(rtc::Event* event, int width, int height) |
53 : I420Buffer(width, height), event_(event) {} | 51 : I420Buffer(width, height), event_(event) {} |
54 | 52 |
55 private: | 53 private: |
56 friend class rtc::RefCountedObject<TestBuffer>; | 54 friend class rtc::RefCountedObject<TestBuffer>; |
57 ~TestBuffer() override { | 55 ~TestBuffer() override { |
58 if (event_) | 56 if (event_) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 142 |
145 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, | 143 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, |
146 bool nack_enabled) { | 144 bool nack_enabled) { |
147 if (vie_encoder_) | 145 if (vie_encoder_) |
148 vie_encoder_->Stop(); | 146 vie_encoder_->Stop(); |
149 vie_encoder_.reset(new ViEEncoderUnderTest( | 147 vie_encoder_.reset(new ViEEncoderUnderTest( |
150 stats_proxy_.get(), video_send_config_.encoder_settings)); | 148 stats_proxy_.get(), video_send_config_.encoder_settings)); |
151 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); | 149 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); |
152 vie_encoder_->SetSource(&video_source_, | 150 vie_encoder_->SetSource(&video_source_, |
153 VideoSendStream::DegradationPreference::kBalanced); | 151 VideoSendStream::DegradationPreference::kBalanced); |
154 vie_encoder_->SetStartBitrate(kTargetBitrateBps); | 152 vie_encoder_->SetStartBitrate(10000); |
155 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), | 153 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
156 kMaxPayloadLength, nack_enabled); | 154 kMaxPayloadLength, nack_enabled); |
157 } | 155 } |
158 | 156 |
159 void ResetEncoder(const std::string& payload_name, | 157 void ResetEncoder(const std::string& payload_name, |
160 size_t num_streams, | 158 size_t num_streams, |
161 size_t num_temporal_layers, | 159 size_t num_temporal_layers, |
162 bool nack_enabled) { | 160 bool nack_enabled) { |
163 video_send_config_.encoder_settings.payload_name = payload_name; | 161 video_send_config_.encoder_settings.payload_name = payload_name; |
164 | 162 |
165 VideoEncoderConfig video_encoder_config; | 163 VideoEncoderConfig video_encoder_config; |
166 video_encoder_config.number_of_streams = num_streams; | 164 video_encoder_config.number_of_streams = num_streams; |
167 video_encoder_config.max_bitrate_bps = kTargetBitrateBps; | 165 video_encoder_config.max_bitrate_bps = 1000000; |
168 video_encoder_config.video_stream_factory = | 166 video_encoder_config.video_stream_factory = |
169 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers); | 167 new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers); |
170 ConfigureEncoder(std::move(video_encoder_config), nack_enabled); | 168 ConfigureEncoder(std::move(video_encoder_config), nack_enabled); |
171 } | 169 } |
172 | 170 |
173 VideoFrame CreateFrame(int64_t ntp_time_ms, | 171 VideoFrame CreateFrame(int64_t ntp_time_ms, |
174 rtc::Event* destruction_event) const { | 172 rtc::Event* destruction_event) const { |
175 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>( | 173 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>( |
176 destruction_event, codec_width_, codec_height_), | 174 destruction_event, codec_width_, codec_height_), |
177 99, 99, kVideoRotation_0); | 175 99, 99, kVideoRotation_0); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 void WaitForEncodedFrame(int64_t expected_ntp_time) { | 256 void WaitForEncodedFrame(int64_t expected_ntp_time) { |
259 uint32_t timestamp = 0; | 257 uint32_t timestamp = 0; |
260 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); | 258 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); |
261 { | 259 { |
262 rtc::CritScope lock(&crit_); | 260 rtc::CritScope lock(&crit_); |
263 timestamp = timestamp_; | 261 timestamp = timestamp_; |
264 } | 262 } |
265 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); | 263 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); |
266 } | 264 } |
267 | 265 |
268 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(20)); } | |
269 | |
270 void SetExpectNoFrames() { | 266 void SetExpectNoFrames() { |
271 rtc::CritScope lock(&crit_); | 267 rtc::CritScope lock(&crit_); |
272 expect_frames_ = false; | 268 expect_frames_ = false; |
273 } | 269 } |
274 | 270 |
275 int number_of_reconfigurations() { | 271 int number_of_reconfigurations() { |
276 rtc::CritScope lock(&crit_); | 272 rtc::CritScope lock(&crit_); |
277 return number_of_reconfigurations_; | 273 return number_of_reconfigurations_; |
278 } | 274 } |
279 | 275 |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 sink_.WaitForEncodedFrame(3); | 672 sink_.WaitForEncodedFrame(3); |
677 | 673 |
678 stats = stats_proxy_->GetStats(); | 674 stats = stats_proxy_->GetStats(); |
679 EXPECT_FALSE(stats.cpu_limited_resolution); | 675 EXPECT_FALSE(stats.cpu_limited_resolution); |
680 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); | 676 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
681 | 677 |
682 vie_encoder_->Stop(); | 678 vie_encoder_->Stop(); |
683 } | 679 } |
684 | 680 |
685 TEST_F(ViEEncoderTest, SwitchingSourceKeepsCpuAdaptation) { | 681 TEST_F(ViEEncoderTest, SwitchingSourceKeepsCpuAdaptation) { |
| 682 const int kTargetBitrateBps = 100000; |
686 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 683 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
687 | 684 |
688 int frame_width = 1280; | 685 int frame_width = 1280; |
689 int frame_height = 720; | 686 int frame_height = 720; |
690 video_source_.IncomingCapturedFrame( | 687 video_source_.IncomingCapturedFrame( |
691 CreateFrame(1, frame_width, frame_height)); | 688 CreateFrame(1, frame_width, frame_height)); |
692 sink_.WaitForEncodedFrame(1); | 689 sink_.WaitForEncodedFrame(1); |
693 | 690 |
694 VideoSendStream::Stats stats = stats_proxy_->GetStats(); | 691 VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
695 EXPECT_FALSE(stats.cpu_limited_resolution); | 692 EXPECT_FALSE(stats.cpu_limited_resolution); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 CreateFrame(6, frame_width, frame_height)); | 742 CreateFrame(6, frame_width, frame_height)); |
746 sink_.WaitForEncodedFrame(6); | 743 sink_.WaitForEncodedFrame(6); |
747 stats = stats_proxy_->GetStats(); | 744 stats = stats_proxy_->GetStats(); |
748 EXPECT_FALSE(stats.cpu_limited_resolution); | 745 EXPECT_FALSE(stats.cpu_limited_resolution); |
749 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); | 746 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
750 | 747 |
751 vie_encoder_->Stop(); | 748 vie_encoder_->Stop(); |
752 } | 749 } |
753 | 750 |
754 TEST_F(ViEEncoderTest, SwitchingSourceKeepsQualityAdaptation) { | 751 TEST_F(ViEEncoderTest, SwitchingSourceKeepsQualityAdaptation) { |
| 752 const int kTargetBitrateBps = 100000; |
755 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 753 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
756 | 754 |
757 int frame_width = 1280; | 755 int frame_width = 1280; |
758 int frame_height = 720; | 756 int frame_height = 720; |
759 video_source_.IncomingCapturedFrame( | 757 video_source_.IncomingCapturedFrame( |
760 CreateFrame(1, frame_width, frame_height)); | 758 CreateFrame(1, frame_width, frame_height)); |
761 sink_.WaitForEncodedFrame(1); | 759 sink_.WaitForEncodedFrame(1); |
762 | 760 |
763 VideoSendStream::Stats stats = stats_proxy_->GetStats(); | 761 VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
764 EXPECT_FALSE(stats.cpu_limited_resolution); | 762 EXPECT_FALSE(stats.cpu_limited_resolution); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 sink_.WaitForEncodedFrame(1); | 878 sink_.WaitForEncodedFrame(1); |
881 | 879 |
882 VideoSendStream::Stats stats = stats_proxy_->GetStats(); | 880 VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
883 EXPECT_EQ(video_encoder_config_.max_bitrate_bps, | 881 EXPECT_EQ(video_encoder_config_.max_bitrate_bps, |
884 stats.preferred_media_bitrate_bps); | 882 stats.preferred_media_bitrate_bps); |
885 | 883 |
886 vie_encoder_->Stop(); | 884 vie_encoder_->Stop(); |
887 } | 885 } |
888 | 886 |
889 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) { | 887 TEST_F(ViEEncoderTest, ScalingUpAndDownDoesNothingWithMaintainResolution) { |
| 888 const int kTargetBitrateBps = 100000; |
890 int frame_width = 1280; | 889 int frame_width = 1280; |
891 int frame_height = 720; | 890 int frame_height = 720; |
892 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 891 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
893 | 892 |
894 // Expect no scaling to begin with | 893 // Expect no scaling to begin with |
895 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); | 894 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); |
896 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count_step_up); | 895 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count_step_up); |
897 | 896 |
898 video_source_.IncomingCapturedFrame( | 897 video_source_.IncomingCapturedFrame( |
899 CreateFrame(1, frame_width, frame_height)); | 898 CreateFrame(1, frame_width, frame_height)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 CreateFrame(4, frame_width, frame_height)); | 931 CreateFrame(4, frame_width, frame_height)); |
933 sink_.WaitForEncodedFrame(4); | 932 sink_.WaitForEncodedFrame(4); |
934 | 933 |
935 // Expect nothing to change, still no scaling | 934 // Expect nothing to change, still no scaling |
936 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); | 935 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); |
937 | 936 |
938 vie_encoder_->Stop(); | 937 vie_encoder_->Stop(); |
939 } | 938 } |
940 | 939 |
941 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) { | 940 TEST_F(ViEEncoderTest, DoesNotScaleBelowSetLimit) { |
| 941 const int kTargetBitrateBps = 100000; |
942 int frame_width = 1280; | 942 int frame_width = 1280; |
943 int frame_height = 720; | 943 int frame_height = 720; |
944 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 944 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
945 | 945 |
946 for (size_t i = 1; i <= 10; i++) { | 946 for (size_t i = 1; i <= 10; i++) { |
947 video_source_.IncomingCapturedFrame( | 947 video_source_.IncomingCapturedFrame( |
948 CreateFrame(i, frame_width, frame_height)); | 948 CreateFrame(i, frame_width, frame_height)); |
949 sink_.WaitForEncodedFrame(i); | 949 sink_.WaitForEncodedFrame(i); |
950 // Trigger scale down | 950 // Trigger scale down |
951 vie_encoder_->TriggerQualityLow(); | 951 vie_encoder_->TriggerQualityLow(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 TEST_F(ViEEncoderTest, CallsBitrateObserver) { | 988 TEST_F(ViEEncoderTest, CallsBitrateObserver) { |
989 class MockBitrateObserver : public VideoBitrateAllocationObserver { | 989 class MockBitrateObserver : public VideoBitrateAllocationObserver { |
990 public: | 990 public: |
991 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const BitrateAllocation&)); | 991 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const BitrateAllocation&)); |
992 } bitrate_observer; | 992 } bitrate_observer; |
993 vie_encoder_->SetBitrateObserver(&bitrate_observer); | 993 vie_encoder_->SetBitrateObserver(&bitrate_observer); |
994 | 994 |
995 const int kDefaultFps = 30; | 995 const int kDefaultFps = 30; |
996 const BitrateAllocation expected_bitrate = | 996 const BitrateAllocation expected_bitrate = |
997 DefaultVideoBitrateAllocator(fake_encoder_.codec_config()) | 997 DefaultVideoBitrateAllocator(fake_encoder_.codec_config()) |
998 .GetAllocation(kLowTargetBitrateBps, kDefaultFps); | 998 .GetAllocation(kTargetBitrateBps, kDefaultFps); |
999 | 999 |
1000 // First called on bitrate updated, then again on first frame. | 1000 // First called on bitrate updated, then again on first frame. |
1001 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) | 1001 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
1002 .Times(2); | 1002 .Times(2); |
1003 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); | 1003 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
1004 | 1004 |
1005 const int64_t kStartTimeMs = 1; | 1005 const int64_t kStartTimeMs = 1; |
1006 video_source_.IncomingCapturedFrame( | 1006 video_source_.IncomingCapturedFrame( |
1007 CreateFrame(kStartTimeMs, codec_width_, codec_height_)); | 1007 CreateFrame(kStartTimeMs, codec_width_, codec_height_)); |
1008 sink_.WaitForEncodedFrame(kStartTimeMs); | 1008 sink_.WaitForEncodedFrame(kStartTimeMs); |
1009 | 1009 |
1010 // Not called on second frame. | 1010 // Not called on second frame. |
1011 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) | 1011 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
1012 .Times(0); | 1012 .Times(0); |
1013 video_source_.IncomingCapturedFrame( | 1013 video_source_.IncomingCapturedFrame( |
1014 CreateFrame(kStartTimeMs + 1, codec_width_, codec_height_)); | 1014 CreateFrame(kStartTimeMs + 1, codec_width_, codec_height_)); |
1015 sink_.WaitForEncodedFrame(kStartTimeMs + 1); | 1015 sink_.WaitForEncodedFrame(kStartTimeMs + 1); |
1016 | 1016 |
1017 // Called after a process interval. | 1017 // Called after a process interval. |
1018 const int64_t kProcessIntervalMs = | 1018 const int64_t kProcessIntervalMs = |
1019 vcm::VCMProcessTimer::kDefaultProcessIntervalMs; | 1019 vcm::VCMProcessTimer::kDefaultProcessIntervalMs; |
1020 // TODO(sprang): ViEEncoder should die and/or get injectable clock. | 1020 // TODO(sprang): ViEEncoder should die and/or get injectable clock. |
1021 // Sleep for one processing interval plus one frame to avoid flakiness. | 1021 // Sleep for one processing interval plus one frame to avoid flakiness. |
1022 SleepMs(kProcessIntervalMs + 1000 / kDefaultFps); | 1022 SleepMs(kProcessIntervalMs + 1000 / kDefaultFps); |
1023 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) | 1023 EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
1024 .Times(1); | 1024 .Times(1); |
1025 video_source_.IncomingCapturedFrame(CreateFrame( | 1025 video_source_.IncomingCapturedFrame(CreateFrame( |
1026 kStartTimeMs + kProcessIntervalMs, codec_width_, codec_height_)); | 1026 kStartTimeMs + kProcessIntervalMs, codec_width_, codec_height_)); |
1027 sink_.WaitForEncodedFrame(kStartTimeMs + kProcessIntervalMs); | 1027 sink_.WaitForEncodedFrame(kStartTimeMs + kProcessIntervalMs); |
1028 | 1028 |
1029 vie_encoder_->Stop(); | 1029 vie_encoder_->Stop(); |
1030 } | 1030 } |
1031 | 1031 |
1032 TEST_F(ViEEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) { | |
1033 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); | |
1034 int frame_width = 640; | |
1035 int frame_height = 360; | |
1036 | |
1037 video_source_.IncomingCapturedFrame( | |
1038 CreateFrame(1, frame_width, frame_height)); | |
1039 | |
1040 // Expect to drop this frame, the wait should time out. | |
1041 sink_.ExpectDroppedFrame(); | |
1042 | |
1043 // Expect the sink_wants to specify a scaled frame. | |
1044 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); | |
1045 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000); | |
1046 | |
1047 int last_pixel_count = *video_source_.sink_wants().max_pixel_count; | |
1048 | |
1049 // Next frame is scaled | |
1050 video_source_.IncomingCapturedFrame( | |
1051 CreateFrame(2, frame_width * 3 / 4, frame_height * 3 / 4)); | |
1052 | |
1053 // Expect to drop this frame, the wait should time out. | |
1054 sink_.ExpectDroppedFrame(); | |
1055 | |
1056 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, last_pixel_count); | |
1057 | |
1058 vie_encoder_->Stop(); | |
1059 } | |
1060 | |
1061 TEST_F(ViEEncoderTest, NrOfDroppedFramesLimited) { | |
1062 // 1kbps. This can never be achieved. | |
1063 vie_encoder_->OnBitrateUpdated(1000, 0, 0); | |
1064 int frame_width = 640; | |
1065 int frame_height = 360; | |
1066 | |
1067 // We expect the n initial frames to get dropped. | |
1068 int i; | |
1069 for (i = 1; i <= kMaxInitialFramedrop; ++i) { | |
1070 video_source_.IncomingCapturedFrame( | |
1071 CreateFrame(i, frame_width, frame_height)); | |
1072 sink_.ExpectDroppedFrame(); | |
1073 } | |
1074 // The n+1th frame should not be dropped, even though it's size is too large. | |
1075 video_source_.IncomingCapturedFrame( | |
1076 CreateFrame(i, frame_width, frame_height)); | |
1077 sink_.WaitForEncodedFrame(i); | |
1078 | |
1079 // Expect the sink_wants to specify a scaled frame. | |
1080 EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); | |
1081 EXPECT_LT(*video_source_.sink_wants().max_pixel_count, 1000 * 1000); | |
1082 | |
1083 vie_encoder_->Stop(); | |
1084 } | |
1085 | |
1086 TEST_F(ViEEncoderTest, InitialFrameDropOffWithMaintainResolutionPreference) { | |
1087 int frame_width = 640; | |
1088 int frame_height = 360; | |
1089 vie_encoder_->OnBitrateUpdated(kLowTargetBitrateBps, 0, 0); | |
1090 | |
1091 // Set degradation preference. | |
1092 vie_encoder_->SetSource( | |
1093 &video_source_, | |
1094 VideoSendStream::DegradationPreference::kMaintainResolution); | |
1095 | |
1096 video_source_.IncomingCapturedFrame( | |
1097 CreateFrame(1, frame_width, frame_height)); | |
1098 // Frame should not be dropped, even if it's too large. | |
1099 sink_.WaitForEncodedFrame(1); | |
1100 | |
1101 vie_encoder_->Stop(); | |
1102 } | |
1103 | |
1104 } // namespace webrtc | 1032 } // namespace webrtc |
OLD | NEW |