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