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 |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "webrtc/base/logging.h" | 12 #include "webrtc/base/logging.h" |
13 #include "webrtc/test/encoder_settings.h" | 13 #include "webrtc/test/encoder_settings.h" |
14 #include "webrtc/test/fake_encoder.h" | 14 #include "webrtc/test/fake_encoder.h" |
15 #include "webrtc/test/frame_generator.h" | |
16 #include "webrtc/video/send_statistics_proxy.h" | 15 #include "webrtc/video/send_statistics_proxy.h" |
17 #include "webrtc/video/vie_encoder.h" | 16 #include "webrtc/video/vie_encoder.h" |
18 | 17 |
19 namespace webrtc { | 18 namespace webrtc { |
20 | 19 |
21 class ViEEncoderTest : public ::testing::Test { | 20 class ViEEncoderTest : public ::testing::Test { |
22 public: | 21 public: |
23 static const int kDefaultTimeoutMs = 30 * 1000; | 22 static const int kDefaultTimeoutMs = 30 * 1000; |
24 | 23 |
25 ViEEncoderTest() | 24 ViEEncoderTest() |
(...skipping 10 matching lines...) Expand all Loading... |
36 video_send_config_.encoder_settings.payload_name = "FAKE"; | 35 video_send_config_.encoder_settings.payload_name = "FAKE"; |
37 video_send_config_.encoder_settings.payload_type = 125; | 36 video_send_config_.encoder_settings.payload_type = 125; |
38 | 37 |
39 video_encoder_config_.streams = test::CreateVideoStreams(1); | 38 video_encoder_config_.streams = test::CreateVideoStreams(1); |
40 | 39 |
41 vie_encoder_.reset(new ViEEncoder( | 40 vie_encoder_.reset(new ViEEncoder( |
42 1 /* number_of_cores */, &stats_proxy_, | 41 1 /* number_of_cores */, &stats_proxy_, |
43 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, | 42 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, |
44 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); | 43 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); |
45 vie_encoder_->SetSink(&sink_); | 44 vie_encoder_->SetSink(&sink_); |
46 vie_encoder_->SetSource(&video_source_); | |
47 vie_encoder_->SetStartBitrate(10000); | 45 vie_encoder_->SetStartBitrate(10000); |
48 vie_encoder_->ConfigureEncoder(video_encoder_config_, 1440); | 46 vie_encoder_->ConfigureEncoder(video_encoder_config_, 1440); |
49 } | 47 } |
50 | 48 |
51 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const { | 49 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const { |
52 class TestBuffer : public webrtc::I420Buffer { | 50 class TestBuffer : public webrtc::I420Buffer { |
53 public: | 51 public: |
54 TestBuffer(rtc::Event* event, int width, int height) | 52 TestBuffer(rtc::Event* event, int width, int height) |
55 : I420Buffer(width, height), event_(event) {} | 53 : I420Buffer(width, height), event_(event) {} |
56 | 54 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); | 88 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); |
91 | 89 |
92 timestamp_ = input_image.timestamp(); | 90 timestamp_ = input_image.timestamp(); |
93 ntp_time_ms_ = input_image.ntp_time_ms(); | 91 ntp_time_ms_ = input_image.ntp_time_ms(); |
94 block_encode = block_next_encode_; | 92 block_encode = block_next_encode_; |
95 block_next_encode_ = false; | 93 block_next_encode_ = false; |
96 } | 94 } |
97 int32_t result = | 95 int32_t result = |
98 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 96 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
99 if (block_encode) | 97 if (block_encode) |
100 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); | 98 continue_encode_event_.Wait(kDefaultTimeoutMs); |
101 return result; | 99 return result; |
102 } | 100 } |
103 | 101 |
104 void BlockNextEncode() { | 102 void BlockNextEncode() { |
105 rtc::CritScope lock(&crit_); | 103 rtc::CritScope lock(&crit_); |
106 block_next_encode_ = true; | 104 block_next_encode_ = true; |
107 } | 105 } |
108 | 106 |
109 void ContinueEncode() { continue_encode_event_.Set(); } | 107 void ContinueEncode() { continue_encode_event_.Set(); } |
110 | 108 |
(...skipping 22 matching lines...) Expand all Loading... |
133 const RTPFragmentationHeader* fragmentation) override { | 131 const RTPFragmentationHeader* fragmentation) override { |
134 rtc::CritScope lock(&crit_); | 132 rtc::CritScope lock(&crit_); |
135 EXPECT_TRUE(expect_frames_); | 133 EXPECT_TRUE(expect_frames_); |
136 timestamp_ = encoded_image._timeStamp; | 134 timestamp_ = encoded_image._timeStamp; |
137 encoded_frame_event_.Set(); | 135 encoded_frame_event_.Set(); |
138 return 0; | 136 return 0; |
139 } | 137 } |
140 | 138 |
141 void WaitForEncodedFrame(int64_t expected_ntp_time) { | 139 void WaitForEncodedFrame(int64_t expected_ntp_time) { |
142 uint32_t timestamp = 0; | 140 uint32_t timestamp = 0; |
143 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); | 141 encoded_frame_event_.Wait(kDefaultTimeoutMs); |
144 { | 142 { |
145 rtc::CritScope lock(&crit_); | 143 rtc::CritScope lock(&crit_); |
146 timestamp = timestamp_; | 144 timestamp = timestamp_; |
147 } | 145 } |
148 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); | 146 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); |
149 } | 147 } |
150 | 148 |
151 void SetExpectNoFrames() { | 149 void SetExpectNoFrames() { |
152 rtc::CritScope lock(&crit_); | 150 rtc::CritScope lock(&crit_); |
153 expect_frames_ = false; | 151 expect_frames_ = false; |
154 } | 152 } |
155 | 153 |
156 private: | 154 private: |
157 rtc::CriticalSection crit_; | 155 rtc::CriticalSection crit_; |
158 TestEncoder* test_encoder_; | 156 TestEncoder* test_encoder_; |
159 rtc::Event encoded_frame_event_; | 157 rtc::Event encoded_frame_event_; |
160 uint32_t timestamp_ = 0; | 158 uint32_t timestamp_ = 0; |
161 bool expect_frames_ = true; | 159 bool expect_frames_ = true; |
162 }; | 160 }; |
163 | 161 |
164 VideoSendStream::Config video_send_config_; | 162 VideoSendStream::Config video_send_config_; |
165 VideoEncoderConfig video_encoder_config_; | 163 VideoEncoderConfig video_encoder_config_; |
166 TestEncoder fake_encoder_; | 164 TestEncoder fake_encoder_; |
167 SendStatisticsProxy stats_proxy_; | 165 SendStatisticsProxy stats_proxy_; |
168 TestSink sink_; | 166 TestSink sink_; |
169 test::FrameForwarder video_source_; | |
170 std::unique_ptr<ViEEncoder> vie_encoder_; | 167 std::unique_ptr<ViEEncoder> vie_encoder_; |
171 }; | 168 }; |
172 | 169 |
173 TEST_F(ViEEncoderTest, EncodeOneFrame) { | 170 TEST_F(ViEEncoderTest, EncodeOneFrame) { |
174 const int kTargetBitrateBps = 100000; | 171 const int kTargetBitrateBps = 100000; |
175 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 172 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
176 rtc::Event frame_destroyed_event(false, false); | 173 rtc::Event frame_destroyed_event(false, false); |
177 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); | 174 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
178 sink_.WaitForEncodedFrame(1); | 175 sink_.WaitForEncodedFrame(1); |
179 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); | 176 frame_destroyed_event.Wait(kDefaultTimeoutMs); |
180 vie_encoder_->Stop(); | 177 vie_encoder_->Stop(); |
181 } | 178 } |
182 | 179 |
183 TEST_F(ViEEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) { | 180 TEST_F(ViEEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) { |
184 // Dropped since no target bitrate has been set. | 181 // Dropped since no target bitrate has been set. |
185 rtc::Event frame_destroyed_event(false, false); | 182 rtc::Event frame_destroyed_event(false, false); |
186 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); | 183 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
187 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); | 184 frame_destroyed_event.Wait(kDefaultTimeoutMs); |
188 | 185 |
189 const int kTargetBitrateBps = 100000; | 186 const int kTargetBitrateBps = 100000; |
190 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 187 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
191 | 188 |
192 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 189 vie_encoder_->IncomingCapturedFrame(CreateFrame(2, nullptr)); |
193 sink_.WaitForEncodedFrame(2); | 190 sink_.WaitForEncodedFrame(2); |
194 vie_encoder_->Stop(); | 191 vie_encoder_->Stop(); |
195 } | 192 } |
196 | 193 |
197 TEST_F(ViEEncoderTest, DropsFramesWhenRateSetToZero) { | 194 TEST_F(ViEEncoderTest, DropsFramesWhenRateSetToZero) { |
198 const int kTargetBitrateBps = 100000; | 195 const int kTargetBitrateBps = 100000; |
199 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 196 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
200 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 197 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, nullptr)); |
201 sink_.WaitForEncodedFrame(1); | 198 sink_.WaitForEncodedFrame(1); |
202 | 199 |
203 vie_encoder_->OnBitrateUpdated(0, 0, 0); | 200 vie_encoder_->OnBitrateUpdated(0, 0, 0); |
204 // Dropped since bitrate is zero. | 201 // Dropped since bitrate is zero. |
205 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 202 vie_encoder_->IncomingCapturedFrame(CreateFrame(2, nullptr)); |
206 | 203 |
207 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 204 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
208 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); | 205 vie_encoder_->IncomingCapturedFrame(CreateFrame(3, nullptr)); |
209 sink_.WaitForEncodedFrame(3); | 206 sink_.WaitForEncodedFrame(3); |
210 vie_encoder_->Stop(); | 207 vie_encoder_->Stop(); |
211 } | 208 } |
212 | 209 |
213 TEST_F(ViEEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) { | 210 TEST_F(ViEEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) { |
214 const int kTargetBitrateBps = 100000; | 211 const int kTargetBitrateBps = 100000; |
215 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 212 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
216 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 213 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, nullptr)); |
217 sink_.WaitForEncodedFrame(1); | 214 sink_.WaitForEncodedFrame(1); |
218 | 215 |
219 // This frame will be dropped since it has the same ntp timestamp. | 216 // This frame will be dropped since it has the same ntp timestamp. |
220 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 217 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, nullptr)); |
221 | 218 |
222 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 219 vie_encoder_->IncomingCapturedFrame(CreateFrame(2, nullptr)); |
223 sink_.WaitForEncodedFrame(2); | 220 sink_.WaitForEncodedFrame(2); |
224 vie_encoder_->Stop(); | 221 vie_encoder_->Stop(); |
225 } | 222 } |
226 | 223 |
227 TEST_F(ViEEncoderTest, DropsFrameAfterStop) { | 224 TEST_F(ViEEncoderTest, DropsFrameAfterStop) { |
228 const int kTargetBitrateBps = 100000; | 225 const int kTargetBitrateBps = 100000; |
229 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 226 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
230 | 227 |
231 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 228 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, nullptr)); |
232 sink_.WaitForEncodedFrame(1); | 229 sink_.WaitForEncodedFrame(1); |
233 | 230 |
234 vie_encoder_->Stop(); | 231 vie_encoder_->Stop(); |
235 sink_.SetExpectNoFrames(); | 232 sink_.SetExpectNoFrames(); |
236 rtc::Event frame_destroyed_event(false, false); | 233 rtc::Event frame_destroyed_event(false, false); |
237 video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event)); | 234 vie_encoder_->IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event)); |
238 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); | 235 frame_destroyed_event.Wait(kDefaultTimeoutMs); |
239 } | 236 } |
240 | 237 |
241 TEST_F(ViEEncoderTest, DropsPendingFramesOnSlowEncode) { | 238 TEST_F(ViEEncoderTest, DropsPendingFramesOnSlowEncode) { |
242 const int kTargetBitrateBps = 100000; | 239 const int kTargetBitrateBps = 100000; |
243 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 240 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
244 | 241 |
245 fake_encoder_.BlockNextEncode(); | 242 fake_encoder_.BlockNextEncode(); |
246 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 243 vie_encoder_->IncomingCapturedFrame(CreateFrame(1, nullptr)); |
247 sink_.WaitForEncodedFrame(1); | 244 sink_.WaitForEncodedFrame(1); |
248 // Here, the encoder thread will be blocked in the TestEncoder waiting for a | 245 // Here, the encoder thread will be blocked in the TestEncoder waiting for a |
249 // call to ContinueEncode. | 246 // call to ContinueEncode. |
250 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 247 vie_encoder_->IncomingCapturedFrame(CreateFrame(2, nullptr)); |
251 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); | 248 vie_encoder_->IncomingCapturedFrame(CreateFrame(3, nullptr)); |
252 fake_encoder_.ContinueEncode(); | 249 fake_encoder_.ContinueEncode(); |
253 sink_.WaitForEncodedFrame(3); | 250 sink_.WaitForEncodedFrame(3); |
254 | 251 |
255 vie_encoder_->Stop(); | 252 vie_encoder_->Stop(); |
256 } | 253 } |
257 | 254 |
258 } // namespace webrtc | 255 } // namespace webrtc |
OLD | NEW |