OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 run_loop_->QuitClosure().Run(); | 121 run_loop_->QuitClosure().Run(); |
122 } | 122 } |
123 | 123 |
124 void WaitForCapturedFrame() { | 124 void WaitForCapturedFrame() { |
125 run_loop_.reset(new base::RunLoop()); | 125 run_loop_.reset(new base::RunLoop()); |
126 run_loop_->Run(); | 126 run_loop_->Run(); |
127 } | 127 } |
128 | 128 |
129 const VideoCaptureFormat& last_format() const { return last_format_; } | 129 const VideoCaptureFormat& last_format() const { return last_format_; } |
130 | 130 |
| 131 scoped_ptr<VideoCaptureDevice::Name> GetFirstDeviceNameSupportingPixelFormat( |
| 132 const VideoPixelFormat& pixel_format) { |
| 133 VideoCaptureDevice::GetDeviceNames(&names_); |
| 134 if (!names_.size()) { |
| 135 DVLOG(1) << "No camera available."; |
| 136 return scoped_ptr<VideoCaptureDevice::Name>(); |
| 137 } |
| 138 VideoCaptureDevice::Names::iterator names_iterator; |
| 139 for (names_iterator = names_.begin(); names_iterator != names_.end(); |
| 140 ++names_iterator) { |
| 141 VideoCaptureFormats supported_formats; |
| 142 VideoCaptureDevice::GetDeviceSupportedFormats(*names_iterator, |
| 143 &supported_formats); |
| 144 VideoCaptureFormats::iterator formats_iterator; |
| 145 for (formats_iterator = supported_formats.begin(); |
| 146 formats_iterator != supported_formats.end(); ++formats_iterator) { |
| 147 if (formats_iterator->pixel_format == pixel_format) { |
| 148 return scoped_ptr<VideoCaptureDevice::Name>( |
| 149 new VideoCaptureDevice::Name(*names_iterator)); |
| 150 } |
| 151 } |
| 152 } |
| 153 DVLOG(1) << "No camera can capture the format: " << pixel_format; |
| 154 return scoped_ptr<VideoCaptureDevice::Name>(); |
| 155 } |
| 156 |
131 #if defined(OS_WIN) | 157 #if defined(OS_WIN) |
132 base::win::ScopedCOMInitializer initialize_com_; | 158 base::win::ScopedCOMInitializer initialize_com_; |
133 #endif | 159 #endif |
134 VideoCaptureDevice::Names names_; | 160 VideoCaptureDevice::Names names_; |
135 scoped_ptr<base::MessageLoop> loop_; | 161 scoped_ptr<base::MessageLoop> loop_; |
136 scoped_ptr<base::RunLoop> run_loop_; | 162 scoped_ptr<base::RunLoop> run_loop_; |
137 scoped_ptr<MockClient> client_; | 163 scoped_ptr<MockClient> client_; |
138 VideoCaptureFormat last_format_; | 164 VideoCaptureFormat last_format_; |
139 }; | 165 }; |
140 | 166 |
(...skipping 13 matching lines...) Expand all Loading... |
154 | 180 |
155 TEST_F(VideoCaptureDeviceTest, CaptureVGA) { | 181 TEST_F(VideoCaptureDeviceTest, CaptureVGA) { |
156 VideoCaptureDevice::GetDeviceNames(&names_); | 182 VideoCaptureDevice::GetDeviceNames(&names_); |
157 if (!names_.size()) { | 183 if (!names_.size()) { |
158 DVLOG(1) << "No camera available. Exiting test."; | 184 DVLOG(1) << "No camera available. Exiting test."; |
159 return; | 185 return; |
160 } | 186 } |
161 | 187 |
162 scoped_ptr<VideoCaptureDevice> device( | 188 scoped_ptr<VideoCaptureDevice> device( |
163 VideoCaptureDevice::Create(names_.front())); | 189 VideoCaptureDevice::Create(names_.front())); |
164 ASSERT_FALSE(device.get() == NULL); | 190 ASSERT_TRUE(device); |
165 DVLOG(1) << names_.front().id(); | 191 DVLOG(1) << names_.front().id(); |
166 | 192 |
167 EXPECT_CALL(*client_, OnErr()) | 193 EXPECT_CALL(*client_, OnErr()) |
168 .Times(0); | 194 .Times(0); |
169 | 195 |
170 VideoCaptureParams capture_params; | 196 VideoCaptureParams capture_params; |
171 capture_params.requested_format.frame_size.SetSize(640, 480); | 197 capture_params.requested_format.frame_size.SetSize(640, 480); |
172 capture_params.requested_format.frame_rate = 30; | 198 capture_params.requested_format.frame_rate = 30; |
173 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 199 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
174 capture_params.allow_resolution_change = false; | 200 capture_params.allow_resolution_change = false; |
175 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 201 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
176 // Get captured video frames. | 202 // Get captured video frames. |
177 WaitForCapturedFrame(); | 203 WaitForCapturedFrame(); |
178 EXPECT_EQ(last_format().frame_size.width(), 640); | 204 EXPECT_EQ(last_format().frame_size.width(), 640); |
179 EXPECT_EQ(last_format().frame_size.height(), 480); | 205 EXPECT_EQ(last_format().frame_size.height(), 480); |
180 device->StopAndDeAllocate(); | 206 device->StopAndDeAllocate(); |
181 } | 207 } |
182 | 208 |
183 TEST_F(VideoCaptureDeviceTest, Capture720p) { | 209 TEST_F(VideoCaptureDeviceTest, Capture720p) { |
184 VideoCaptureDevice::GetDeviceNames(&names_); | 210 VideoCaptureDevice::GetDeviceNames(&names_); |
185 if (!names_.size()) { | 211 if (!names_.size()) { |
186 DVLOG(1) << "No camera available. Exiting test."; | 212 DVLOG(1) << "No camera available. Exiting test."; |
187 return; | 213 return; |
188 } | 214 } |
189 | 215 |
190 scoped_ptr<VideoCaptureDevice> device( | 216 scoped_ptr<VideoCaptureDevice> device( |
191 VideoCaptureDevice::Create(names_.front())); | 217 VideoCaptureDevice::Create(names_.front())); |
192 ASSERT_FALSE(device.get() == NULL); | 218 ASSERT_TRUE(device); |
193 | 219 |
194 EXPECT_CALL(*client_, OnErr()) | 220 EXPECT_CALL(*client_, OnErr()) |
195 .Times(0); | 221 .Times(0); |
196 | 222 |
197 VideoCaptureParams capture_params; | 223 VideoCaptureParams capture_params; |
198 capture_params.requested_format.frame_size.SetSize(1280, 720); | 224 capture_params.requested_format.frame_size.SetSize(1280, 720); |
199 capture_params.requested_format.frame_rate = 30; | 225 capture_params.requested_format.frame_rate = 30; |
200 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 226 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
201 capture_params.allow_resolution_change = false; | 227 capture_params.allow_resolution_change = false; |
202 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 228 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
203 // Get captured video frames. | 229 // Get captured video frames. |
204 WaitForCapturedFrame(); | 230 WaitForCapturedFrame(); |
205 device->StopAndDeAllocate(); | 231 device->StopAndDeAllocate(); |
206 } | 232 } |
207 | 233 |
208 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { | 234 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { |
209 VideoCaptureDevice::GetDeviceNames(&names_); | 235 VideoCaptureDevice::GetDeviceNames(&names_); |
210 if (!names_.size()) { | 236 if (!names_.size()) { |
211 DVLOG(1) << "No camera available. Exiting test."; | 237 DVLOG(1) << "No camera available. Exiting test."; |
212 return; | 238 return; |
213 } | 239 } |
214 scoped_ptr<VideoCaptureDevice> device( | 240 scoped_ptr<VideoCaptureDevice> device( |
215 VideoCaptureDevice::Create(names_.front())); | 241 VideoCaptureDevice::Create(names_.front())); |
216 ASSERT_TRUE(device.get() != NULL); | 242 ASSERT_TRUE(device); |
217 | 243 |
218 EXPECT_CALL(*client_, OnErr()) | 244 EXPECT_CALL(*client_, OnErr()) |
219 .Times(0); | 245 .Times(0); |
220 | 246 |
221 VideoCaptureParams capture_params; | 247 VideoCaptureParams capture_params; |
222 capture_params.requested_format.frame_size.SetSize(637, 472); | 248 capture_params.requested_format.frame_size.SetSize(637, 472); |
223 capture_params.requested_format.frame_rate = 35; | 249 capture_params.requested_format.frame_rate = 35; |
224 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 250 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
225 capture_params.allow_resolution_change = false; | 251 capture_params.allow_resolution_change = false; |
226 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 252 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 303 } |
278 | 304 |
279 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | 305 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
280 VideoCaptureDevice::GetDeviceNames(&names_); | 306 VideoCaptureDevice::GetDeviceNames(&names_); |
281 if (!names_.size()) { | 307 if (!names_.size()) { |
282 DVLOG(1) << "No camera available. Exiting test."; | 308 DVLOG(1) << "No camera available. Exiting test."; |
283 return; | 309 return; |
284 } | 310 } |
285 scoped_ptr<VideoCaptureDevice> device( | 311 scoped_ptr<VideoCaptureDevice> device( |
286 VideoCaptureDevice::Create(names_.front())); | 312 VideoCaptureDevice::Create(names_.front())); |
287 ASSERT_TRUE(device.get() != NULL); | 313 ASSERT_TRUE(device); |
288 | 314 |
289 EXPECT_CALL(*client_, OnErr()) | 315 EXPECT_CALL(*client_, OnErr()) |
290 .Times(0); | 316 .Times(0); |
291 | 317 |
292 VideoCaptureParams capture_params; | 318 VideoCaptureParams capture_params; |
293 capture_params.requested_format.frame_size.SetSize(640, 480); | 319 capture_params.requested_format.frame_size.SetSize(640, 480); |
294 capture_params.requested_format.frame_rate = 30; | 320 capture_params.requested_format.frame_rate = 30; |
295 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 321 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
296 capture_params.allow_resolution_change = false; | 322 capture_params.allow_resolution_change = false; |
297 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 323 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
298 // Get captured video frames. | 324 // Get captured video frames. |
299 WaitForCapturedFrame(); | 325 WaitForCapturedFrame(); |
300 EXPECT_EQ(last_format().frame_size.width(), 640); | 326 EXPECT_EQ(last_format().frame_size.width(), 640); |
301 EXPECT_EQ(last_format().frame_size.height(), 480); | 327 EXPECT_EQ(last_format().frame_size.height(), 480); |
302 EXPECT_EQ(last_format().frame_rate, 30); | 328 EXPECT_EQ(last_format().frame_rate, 30); |
303 device->StopAndDeAllocate(); | 329 device->StopAndDeAllocate(); |
304 } | 330 } |
305 | 331 |
306 TEST_F(VideoCaptureDeviceTest, FakeCapture) { | 332 TEST_F(VideoCaptureDeviceTest, FakeCapture) { |
307 VideoCaptureDevice::Names names; | 333 VideoCaptureDevice::Names names; |
308 | 334 |
309 FakeVideoCaptureDevice::GetDeviceNames(&names); | 335 FakeVideoCaptureDevice::GetDeviceNames(&names); |
310 | 336 |
311 ASSERT_GT(static_cast<int>(names.size()), 0); | 337 ASSERT_GT(static_cast<int>(names.size()), 0); |
312 | 338 |
313 scoped_ptr<VideoCaptureDevice> device( | 339 scoped_ptr<VideoCaptureDevice> device( |
314 FakeVideoCaptureDevice::Create(names.front())); | 340 FakeVideoCaptureDevice::Create(names.front())); |
315 ASSERT_TRUE(device.get() != NULL); | 341 ASSERT_TRUE(device); |
316 | 342 |
317 EXPECT_CALL(*client_, OnErr()) | 343 EXPECT_CALL(*client_, OnErr()) |
318 .Times(0); | 344 .Times(0); |
319 | 345 |
320 VideoCaptureParams capture_params; | 346 VideoCaptureParams capture_params; |
321 capture_params.requested_format.frame_size.SetSize(640, 480); | 347 capture_params.requested_format.frame_size.SetSize(640, 480); |
322 capture_params.requested_format.frame_rate = 30; | 348 capture_params.requested_format.frame_rate = 30; |
323 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 349 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
324 capture_params.allow_resolution_change = false; | 350 capture_params.allow_resolution_change = false; |
325 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 351 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
326 WaitForCapturedFrame(); | 352 WaitForCapturedFrame(); |
327 EXPECT_EQ(last_format().frame_size.width(), 640); | 353 EXPECT_EQ(last_format().frame_size.width(), 640); |
328 EXPECT_EQ(last_format().frame_size.height(), 480); | 354 EXPECT_EQ(last_format().frame_size.height(), 480); |
329 EXPECT_EQ(last_format().frame_rate, 30); | 355 EXPECT_EQ(last_format().frame_rate, 30); |
330 device->StopAndDeAllocate(); | 356 device->StopAndDeAllocate(); |
331 } | 357 } |
332 | 358 |
333 // Start the camera in 720p to capture MJPEG instead of a raw format. | 359 // Start the camera in 720p to capture MJPEG instead of a raw format. |
334 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { | 360 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { |
335 VideoCaptureDevice::GetDeviceNames(&names_); | 361 scoped_ptr<VideoCaptureDevice::Name> name = |
336 if (!names_.size()) { | 362 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MJPEG); |
337 DVLOG(1) << "No camera available. Exiting test."; | 363 if (!name) { |
| 364 DVLOG(1) << "No camera supports MJPEG format. Exiting test."; |
338 return; | 365 return; |
339 } | 366 } |
340 scoped_ptr<VideoCaptureDevice> device( | 367 scoped_ptr<VideoCaptureDevice> device(VideoCaptureDevice::Create(*name)); |
341 VideoCaptureDevice::Create(names_.front())); | 368 ASSERT_TRUE(device); |
342 ASSERT_TRUE(device.get() != NULL); | |
343 | 369 |
344 EXPECT_CALL(*client_, OnErr()) | 370 EXPECT_CALL(*client_, OnErr()) |
345 .Times(0); | 371 .Times(0); |
346 | 372 |
347 VideoCaptureParams capture_params; | 373 VideoCaptureParams capture_params; |
348 capture_params.requested_format.frame_size.SetSize(1280, 720); | 374 capture_params.requested_format.frame_size.SetSize(1280, 720); |
349 capture_params.requested_format.frame_rate = 30; | 375 capture_params.requested_format.frame_rate = 30; |
350 capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG; | 376 capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG; |
351 capture_params.allow_resolution_change = false; | 377 capture_params.allow_resolution_change = false; |
352 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 378 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
353 // Get captured video frames. | 379 // Get captured video frames. |
354 WaitForCapturedFrame(); | 380 WaitForCapturedFrame(); |
355 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 | 381 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 |
356 // @ 30 fps, so we don't care about the exact resolution we get. | 382 // @ 30 fps, so we don't care about the exact resolution we get. |
357 EXPECT_EQ(last_format().pixel_format, PIXEL_FORMAT_MJPEG); | 383 EXPECT_EQ(last_format().pixel_format, PIXEL_FORMAT_MJPEG); |
358 device->StopAndDeAllocate(); | 384 device->StopAndDeAllocate(); |
359 } | 385 } |
360 | 386 |
361 TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 387 TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
362 VideoCaptureDevice::GetDeviceNames(&names_); | 388 // Use PIXEL_FORMAT_MAX to iterate all device names for testing |
363 if (!names_.size()) { | 389 // GetDeviceSupportedFormats(). |
364 DVLOG(1) << "No camera available. Exiting test."; | 390 scoped_ptr<VideoCaptureDevice::Name> name = |
365 return; | 391 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX); |
366 } | 392 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here |
367 VideoCaptureFormats supported_formats; | 393 // since we cannot forecast the hardware capabilities. |
368 VideoCaptureDevice::Names::iterator names_iterator; | 394 ASSERT_FALSE(name); |
369 for (names_iterator = names_.begin(); names_iterator != names_.end(); | |
370 ++names_iterator) { | |
371 VideoCaptureDevice::GetDeviceSupportedFormats(*names_iterator, | |
372 &supported_formats); | |
373 // Nothing to test here since we cannot forecast the hardware capabilities. | |
374 } | |
375 } | 395 } |
376 | 396 |
377 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) { | 397 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) { |
378 VideoCaptureDevice::Names names; | 398 VideoCaptureDevice::Names names; |
379 | 399 |
380 FakeVideoCaptureDevice::GetDeviceNames(&names); | 400 FakeVideoCaptureDevice::GetDeviceNames(&names); |
381 VideoCaptureParams capture_params; | 401 VideoCaptureParams capture_params; |
382 capture_params.requested_format.frame_size.SetSize(640, 480); | 402 capture_params.requested_format.frame_size.SetSize(640, 480); |
383 capture_params.requested_format.frame_rate = 30; | 403 capture_params.requested_format.frame_rate = 30; |
384 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 404 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
385 capture_params.allow_resolution_change = true; | 405 capture_params.allow_resolution_change = true; |
386 | 406 |
387 ASSERT_GT(static_cast<int>(names.size()), 0); | 407 ASSERT_GT(static_cast<int>(names.size()), 0); |
388 | 408 |
389 scoped_ptr<VideoCaptureDevice> device( | 409 scoped_ptr<VideoCaptureDevice> device( |
390 FakeVideoCaptureDevice::Create(names.front())); | 410 FakeVideoCaptureDevice::Create(names.front())); |
391 ASSERT_TRUE(device.get() != NULL); | 411 ASSERT_TRUE(device); |
392 | 412 |
393 EXPECT_CALL(*client_, OnErr()) | 413 EXPECT_CALL(*client_, OnErr()) |
394 .Times(0); | 414 .Times(0); |
395 int action_count = 200; | 415 int action_count = 200; |
396 | 416 |
397 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); | 417 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
398 | 418 |
399 // We set TimeWait to 200 action timeouts and this should be enough for at | 419 // We set TimeWait to 200 action timeouts and this should be enough for at |
400 // least action_count/kFakeCaptureCapabilityChangePeriod calls. | 420 // least action_count/kFakeCaptureCapabilityChangePeriod calls. |
401 for (int i = 0; i < action_count; ++i) { | 421 for (int i = 0; i < action_count; ++i) { |
(...skipping 23 matching lines...) Expand all Loading... |
425 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); | 445 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); |
426 EXPECT_GE(supported_formats[1].frame_rate, 20); | 446 EXPECT_GE(supported_formats[1].frame_rate, 20); |
427 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 447 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
428 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 448 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
429 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); | 449 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); |
430 EXPECT_GE(supported_formats[2].frame_rate, 20); | 450 EXPECT_GE(supported_formats[2].frame_rate, 20); |
431 } | 451 } |
432 } | 452 } |
433 | 453 |
434 }; // namespace media | 454 }; // namespace media |
OLD | NEW |