| 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 "media/capture/video/video_capture_device.h" | 5 #include "media/capture/video/video_capture_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 void(const base::Callback<void(mojom::BlobPtr)>&)); | 161 void(const base::Callback<void(mojom::BlobPtr)>&)); |
| 162 | 162 |
| 163 private: | 163 private: |
| 164 friend class base::RefCounted<MockImageCaptureClient>; | 164 friend class base::RefCounted<MockImageCaptureClient>; |
| 165 virtual ~MockImageCaptureClient() {} | 165 virtual ~MockImageCaptureClient() {} |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 class DeviceEnumerationListener | 168 class DeviceEnumerationListener |
| 169 : public base::RefCounted<DeviceEnumerationListener> { | 169 : public base::RefCounted<DeviceEnumerationListener> { |
| 170 public: | 170 public: |
| 171 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr, | 171 MOCK_METHOD1(OnEnumerateDeviceDescriptorsCallbackPtr, |
| 172 void(VideoCaptureDevice::Names* names)); | 172 void(VideoCaptureDeviceDescriptors* device_descriptors)); |
| 173 // GMock doesn't support move-only arguments, so we use this forward method. | 173 // GMock doesn't support move-only arguments, so we use this forward method. |
| 174 void OnEnumeratedDevicesCallback( | 174 void OnEnumerateDeviceDescriptorsCallback( |
| 175 std::unique_ptr<VideoCaptureDevice::Names> names) { | 175 std::unique_ptr<VideoCaptureDeviceDescriptors> device_descriptors) { |
| 176 OnEnumeratedDevicesCallbackPtr(names.release()); | 176 OnEnumerateDeviceDescriptorsCallbackPtr(device_descriptors.release()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 private: | 179 private: |
| 180 friend class base::RefCounted<DeviceEnumerationListener>; | 180 friend class base::RefCounted<DeviceEnumerationListener>; |
| 181 virtual ~DeviceEnumerationListener() {} | 181 virtual ~DeviceEnumerationListener() {} |
| 182 }; | 182 }; |
| 183 | 183 |
| 184 } // namespace | 184 } // namespace |
| 185 | 185 |
| 186 class VideoCaptureDeviceTest : public testing::TestWithParam<gfx::Size> { | 186 class VideoCaptureDeviceTest : public testing::TestWithParam<gfx::Size> { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void OnFrameCaptured(const VideoCaptureFormat& format) { | 219 void OnFrameCaptured(const VideoCaptureFormat& format) { |
| 220 last_format_ = format; | 220 last_format_ = format; |
| 221 run_loop_->QuitClosure().Run(); | 221 run_loop_->QuitClosure().Run(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void WaitForCapturedFrame() { | 224 void WaitForCapturedFrame() { |
| 225 run_loop_.reset(new base::RunLoop()); | 225 run_loop_.reset(new base::RunLoop()); |
| 226 run_loop_->Run(); | 226 run_loop_->Run(); |
| 227 } | 227 } |
| 228 | 228 |
| 229 std::unique_ptr<VideoCaptureDevice::Names> EnumerateDevices() { | 229 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDeviceDescriptors() { |
| 230 VideoCaptureDevice::Names* names; | 230 VideoCaptureDeviceDescriptors* device_descriptors; |
| 231 EXPECT_CALL(*device_enumeration_listener_.get(), | 231 EXPECT_CALL(*device_enumeration_listener_.get(), |
| 232 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names)); | 232 OnEnumerateDeviceDescriptorsCallbackPtr(_)) |
| 233 .WillOnce(SaveArg<0>(&device_descriptors)); |
| 233 | 234 |
| 234 video_capture_device_factory_->EnumerateDeviceNames( | 235 video_capture_device_factory_->EnumerateDeviceDescriptors(base::Bind( |
| 235 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, | 236 &DeviceEnumerationListener::OnEnumerateDeviceDescriptorsCallback, |
| 236 device_enumeration_listener_)); | 237 device_enumeration_listener_)); |
| 237 base::RunLoop().RunUntilIdle(); | 238 base::RunLoop().RunUntilIdle(); |
| 238 return std::unique_ptr<VideoCaptureDevice::Names>(names); | 239 return std::unique_ptr<VideoCaptureDeviceDescriptors>(device_descriptors); |
| 239 } | 240 } |
| 240 | 241 |
| 241 const VideoCaptureFormat& last_format() const { return last_format_; } | 242 const VideoCaptureFormat& last_format() const { return last_format_; } |
| 242 | 243 |
| 243 std::unique_ptr<VideoCaptureDevice::Name> | 244 std::unique_ptr<VideoCaptureDeviceDescriptor> |
| 244 GetFirstDeviceNameSupportingPixelFormat( | 245 GetFirstDeviceDescriptorSupportingPixelFormat( |
| 245 const VideoPixelFormat& pixel_format) { | 246 const VideoPixelFormat& pixel_format) { |
| 246 names_ = EnumerateDevices(); | 247 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 247 if (names_->empty()) { | 248 if (device_descriptors_->empty()) { |
| 248 DVLOG(1) << "No camera available."; | 249 DVLOG(1) << "No camera available."; |
| 249 return std::unique_ptr<VideoCaptureDevice::Name>(); | 250 return std::unique_ptr<VideoCaptureDeviceDescriptor>(); |
| 250 } | 251 } |
| 251 for (const auto& names_iterator : *names_) { | 252 for (const auto& descriptors_iterator : *device_descriptors_) { |
| 252 VideoCaptureFormats supported_formats; | 253 VideoCaptureFormats supported_formats; |
| 253 video_capture_device_factory_->GetDeviceSupportedFormats( | 254 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, |
| 254 names_iterator, &supported_formats); | 255 &supported_formats); |
| 255 for (const auto& formats_iterator : supported_formats) { | 256 for (const auto& formats_iterator : supported_formats) { |
| 256 if (formats_iterator.pixel_format == pixel_format) { | 257 if (formats_iterator.pixel_format == pixel_format) { |
| 257 return std::unique_ptr<VideoCaptureDevice::Name>( | 258 return std::unique_ptr<VideoCaptureDeviceDescriptor>( |
| 258 new VideoCaptureDevice::Name(names_iterator)); | 259 new VideoCaptureDeviceDescriptor(descriptors_iterator)); |
| 259 } | 260 } |
| 260 } | 261 } |
| 261 } | 262 } |
| 262 DVLOG_IF(1, pixel_format != PIXEL_FORMAT_MAX) | 263 DVLOG_IF(1, pixel_format != PIXEL_FORMAT_MAX) |
| 263 << "No camera can capture the" | 264 << "No camera can capture the" |
| 264 << " format: " << VideoPixelFormatToString(pixel_format); | 265 << " format: " << VideoPixelFormatToString(pixel_format); |
| 265 return std::unique_ptr<VideoCaptureDevice::Name>(); | 266 return std::unique_ptr<VideoCaptureDeviceDescriptor>(); |
| 266 } | 267 } |
| 267 | 268 |
| 268 bool IsCaptureSizeSupported(const VideoCaptureDevice::Name& device, | 269 bool IsCaptureSizeSupported(const VideoCaptureDeviceDescriptor& device, |
| 269 const gfx::Size& size) { | 270 const gfx::Size& size) { |
| 270 VideoCaptureFormats supported_formats; | 271 VideoCaptureFormats supported_formats; |
| 271 video_capture_device_factory_->GetDeviceSupportedFormats( | 272 video_capture_device_factory_->GetSupportedFormats(device, |
| 272 device, &supported_formats); | 273 &supported_formats); |
| 273 const auto it = std::find_if( | 274 const auto it = std::find_if( |
| 274 supported_formats.begin(), supported_formats.end(), | 275 supported_formats.begin(), supported_formats.end(), |
| 275 [&size](VideoCaptureFormat const& f) { return f.frame_size == size; }); | 276 [&size](VideoCaptureFormat const& f) { return f.frame_size == size; }); |
| 276 if (it == supported_formats.end()) { | 277 if (it == supported_formats.end()) { |
| 277 DVLOG(1) << "Size " << size.ToString() << " is not supported."; | 278 DVLOG(1) << "Size " << size.ToString() << " is not supported."; |
| 278 return false; | 279 return false; |
| 279 } | 280 } |
| 280 return true; | 281 return true; |
| 281 } | 282 } |
| 282 | 283 |
| 283 #if defined(OS_WIN) | 284 #if defined(OS_WIN) |
| 284 base::win::ScopedCOMInitializer initialize_com_; | 285 base::win::ScopedCOMInitializer initialize_com_; |
| 285 #endif | 286 #endif |
| 286 std::unique_ptr<VideoCaptureDevice::Names> names_; | 287 std::unique_ptr<VideoCaptureDeviceDescriptors> device_descriptors_; |
| 287 const std::unique_ptr<base::MessageLoop> loop_; | 288 const std::unique_ptr<base::MessageLoop> loop_; |
| 288 std::unique_ptr<base::RunLoop> run_loop_; | 289 std::unique_ptr<base::RunLoop> run_loop_; |
| 289 std::unique_ptr<MockVideoCaptureClient> video_capture_client_; | 290 std::unique_ptr<MockVideoCaptureClient> video_capture_client_; |
| 290 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 291 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
| 291 const scoped_refptr<MockImageCaptureClient> image_capture_client_; | 292 const scoped_refptr<MockImageCaptureClient> image_capture_client_; |
| 292 VideoCaptureFormat last_format_; | 293 VideoCaptureFormat last_format_; |
| 293 const std::unique_ptr<VideoCaptureDeviceFactory> | 294 const std::unique_ptr<VideoCaptureDeviceFactory> |
| 294 video_capture_device_factory_; | 295 video_capture_device_factory_; |
| 295 }; | 296 }; |
| 296 | 297 |
| 297 // Cause hangs on Windows Debug. http://crbug.com/417824 | 298 // Cause hangs on Windows Debug. http://crbug.com/417824 |
| 298 #if defined(OS_WIN) && !defined(NDEBUG) | 299 #if defined(OS_WIN) && !defined(NDEBUG) |
| 299 #define MAYBE_OpenInvalidDevice DISABLED_OpenInvalidDevice | 300 #define MAYBE_OpenInvalidDevice DISABLED_OpenInvalidDevice |
| 300 #else | 301 #else |
| 301 #define MAYBE_OpenInvalidDevice OpenInvalidDevice | 302 #define MAYBE_OpenInvalidDevice OpenInvalidDevice |
| 302 #endif | 303 #endif |
| 303 | 304 |
| 304 TEST_F(VideoCaptureDeviceTest, MAYBE_OpenInvalidDevice) { | 305 TEST_F(VideoCaptureDeviceTest, MAYBE_OpenInvalidDevice) { |
| 306 VideoCaptureDeviceDescriptor invalid_descriptor; |
| 307 invalid_descriptor.device_id = "jibberish"; |
| 308 invalid_descriptor.display_name = "jibberish"; |
| 305 #if defined(OS_WIN) | 309 #if defined(OS_WIN) |
| 306 VideoCaptureDevice::Name::CaptureApiType api_type = | 310 invalid_descriptor.capture_api = |
| 307 VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() | 311 VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() |
| 308 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION | 312 ? VideoCaptureApi::WIN_MEDIA_FOUNDATION |
| 309 : VideoCaptureDevice::Name::DIRECT_SHOW; | 313 : VideoCaptureApi::WIN_DIRECT_SHOW; |
| 310 VideoCaptureDevice::Name device_name("jibberish", "jibberish", api_type); | |
| 311 #elif defined(OS_MACOSX) | 314 #elif defined(OS_MACOSX) |
| 312 VideoCaptureDevice::Name device_name("jibberish", "jibberish", | 315 invalid_descriptor.capture_api = VideoCaptureApi::MACOSX_AVFOUNDATION; |
| 313 VideoCaptureDevice::Name::AVFOUNDATION); | |
| 314 #else | |
| 315 VideoCaptureDevice::Name device_name("jibberish", "jibberish"); | |
| 316 #endif | 316 #endif |
| 317 std::unique_ptr<VideoCaptureDevice> device = | 317 std::unique_ptr<VideoCaptureDevice> device = |
| 318 video_capture_device_factory_->Create(device_name); | 318 video_capture_device_factory_->CreateDevice(invalid_descriptor); |
| 319 | 319 |
| 320 #if !defined(OS_MACOSX) | 320 #if !defined(OS_MACOSX) |
| 321 EXPECT_TRUE(device == NULL); | 321 EXPECT_TRUE(device == NULL); |
| 322 #else | 322 #else |
| 323 // The presence of the actual device is only checked on AllocateAndStart() | 323 // The presence of the actual device is only checked on AllocateAndStart() |
| 324 // and not on creation. | 324 // and not on creation. |
| 325 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(1); | 325 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(1); |
| 326 | 326 |
| 327 VideoCaptureParams capture_params; | 327 VideoCaptureParams capture_params; |
| 328 capture_params.requested_format.frame_size.SetSize(640, 480); | 328 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 329 capture_params.requested_format.frame_rate = 30; | 329 capture_params.requested_format.frame_rate = 30; |
| 330 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 330 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 331 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 331 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 332 device->StopAndDeAllocate(); | 332 device->StopAndDeAllocate(); |
| 333 #endif | 333 #endif |
| 334 } | 334 } |
| 335 | 335 |
| 336 TEST_P(VideoCaptureDeviceTest, CaptureWithSize) { | 336 TEST_P(VideoCaptureDeviceTest, CaptureWithSize) { |
| 337 names_ = EnumerateDevices(); | 337 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 338 if (names_->empty()) { | 338 if (device_descriptors_->empty()) { |
| 339 VLOG(1) << "No camera available. Exiting test."; | 339 VLOG(1) << "No camera available. Exiting test."; |
| 340 return; | 340 return; |
| 341 } | 341 } |
| 342 | 342 |
| 343 const gfx::Size& size = GetParam(); | 343 const gfx::Size& size = GetParam(); |
| 344 if (!IsCaptureSizeSupported(names_->front(), size)) | 344 if (!IsCaptureSizeSupported(device_descriptors_->front(), size)) |
| 345 return; | 345 return; |
| 346 const int width = size.width(); | 346 const int width = size.width(); |
| 347 const int height = size.height(); | 347 const int height = size.height(); |
| 348 | 348 |
| 349 std::unique_ptr<VideoCaptureDevice> device( | 349 std::unique_ptr<VideoCaptureDevice> device( |
| 350 video_capture_device_factory_->Create(names_->front())); | 350 video_capture_device_factory_->CreateDevice( |
| 351 device_descriptors_->front())); |
| 351 ASSERT_TRUE(device); | 352 ASSERT_TRUE(device); |
| 352 DVLOG(1) << names_->front().id(); | 353 DVLOG(1) << device_descriptors_->front().device_id; |
| 353 | 354 |
| 354 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | 355 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); |
| 355 | 356 |
| 356 VideoCaptureParams capture_params; | 357 VideoCaptureParams capture_params; |
| 357 capture_params.requested_format.frame_size.SetSize(width, height); | 358 capture_params.requested_format.frame_size.SetSize(width, height); |
| 358 capture_params.requested_format.frame_rate = 30.0f; | 359 capture_params.requested_format.frame_rate = 30.0f; |
| 359 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 360 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 360 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 361 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 361 // Get captured video frames. | 362 // Get captured video frames. |
| 362 WaitForCapturedFrame(); | 363 WaitForCapturedFrame(); |
| 363 EXPECT_EQ(last_format().frame_size.width(), width); | 364 EXPECT_EQ(last_format().frame_size.width(), width); |
| 364 EXPECT_EQ(last_format().frame_size.height(), height); | 365 EXPECT_EQ(last_format().frame_size.height(), height); |
| 365 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) | 366 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) |
| 366 EXPECT_EQ(size.GetArea(), last_format().frame_size.GetArea()); | 367 EXPECT_EQ(size.GetArea(), last_format().frame_size.GetArea()); |
| 367 device->StopAndDeAllocate(); | 368 device->StopAndDeAllocate(); |
| 368 } | 369 } |
| 369 | 370 |
| 370 #if !defined(OS_ANDROID) | 371 #if !defined(OS_ANDROID) |
| 371 const gfx::Size kCaptureSizes[] = {gfx::Size(640, 480), gfx::Size(1280, 720)}; | 372 const gfx::Size kCaptureSizes[] = {gfx::Size(640, 480), gfx::Size(1280, 720)}; |
| 372 | 373 |
| 373 INSTANTIATE_TEST_CASE_P(VideoCaptureDeviceTests, | 374 INSTANTIATE_TEST_CASE_P(VideoCaptureDeviceTests, |
| 374 VideoCaptureDeviceTest, | 375 VideoCaptureDeviceTest, |
| 375 testing::ValuesIn(kCaptureSizes)); | 376 testing::ValuesIn(kCaptureSizes)); |
| 376 #endif | 377 #endif |
| 377 | 378 |
| 378 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { | 379 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { |
| 379 names_ = EnumerateDevices(); | 380 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 380 if (names_->empty()) { | 381 if (device_descriptors_->empty()) { |
| 381 VLOG(1) << "No camera available. Exiting test."; | 382 VLOG(1) << "No camera available. Exiting test."; |
| 382 return; | 383 return; |
| 383 } | 384 } |
| 384 std::unique_ptr<VideoCaptureDevice> device( | 385 std::unique_ptr<VideoCaptureDevice> device( |
| 385 video_capture_device_factory_->Create(names_->front())); | 386 video_capture_device_factory_->CreateDevice( |
| 387 device_descriptors_->front())); |
| 386 ASSERT_TRUE(device); | 388 ASSERT_TRUE(device); |
| 387 | 389 |
| 388 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | 390 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); |
| 389 | 391 |
| 390 const gfx::Size input_size(640, 480); | 392 const gfx::Size input_size(640, 480); |
| 391 VideoCaptureParams capture_params; | 393 VideoCaptureParams capture_params; |
| 392 capture_params.requested_format.frame_size.SetSize(637, 472); | 394 capture_params.requested_format.frame_size.SetSize(637, 472); |
| 393 capture_params.requested_format.frame_rate = 35; | 395 capture_params.requested_format.frame_rate = 35; |
| 394 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 396 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 395 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 397 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 396 WaitForCapturedFrame(); | 398 WaitForCapturedFrame(); |
| 397 device->StopAndDeAllocate(); | 399 device->StopAndDeAllocate(); |
| 398 EXPECT_EQ(last_format().frame_size.width(), input_size.width()); | 400 EXPECT_EQ(last_format().frame_size.width(), input_size.width()); |
| 399 EXPECT_EQ(last_format().frame_size.height(), input_size.height()); | 401 EXPECT_EQ(last_format().frame_size.height(), input_size.height()); |
| 400 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) | 402 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) |
| 401 EXPECT_EQ(input_size.GetArea(), last_format().frame_size.GetArea()); | 403 EXPECT_EQ(input_size.GetArea(), last_format().frame_size.GetArea()); |
| 402 } | 404 } |
| 403 | 405 |
| 404 // Cause hangs on Windows, Linux. Fails Android. http://crbug.com/417824 | 406 // Cause hangs on Windows, Linux. Fails Android. http://crbug.com/417824 |
| 405 TEST_F(VideoCaptureDeviceTest, DISABLED_ReAllocateCamera) { | 407 TEST_F(VideoCaptureDeviceTest, DISABLED_ReAllocateCamera) { |
| 406 names_ = EnumerateDevices(); | 408 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 407 if (names_->empty()) { | 409 if (device_descriptors_->empty()) { |
| 408 VLOG(1) << "No camera available. Exiting test."; | 410 VLOG(1) << "No camera available. Exiting test."; |
| 409 return; | 411 return; |
| 410 } | 412 } |
| 411 | 413 |
| 412 // First, do a number of very fast device start/stops. | 414 // First, do a number of very fast device start/stops. |
| 413 for (int i = 0; i <= 5; i++) { | 415 for (int i = 0; i <= 5; i++) { |
| 414 ResetWithNewClient(); | 416 ResetWithNewClient(); |
| 415 std::unique_ptr<VideoCaptureDevice> device( | 417 std::unique_ptr<VideoCaptureDevice> device( |
| 416 video_capture_device_factory_->Create(names_->front())); | 418 video_capture_device_factory_->CreateDevice( |
| 419 device_descriptors_->front())); |
| 417 gfx::Size resolution; | 420 gfx::Size resolution; |
| 418 if (i % 2) { | 421 if (i % 2) { |
| 419 resolution = gfx::Size(640, 480); | 422 resolution = gfx::Size(640, 480); |
| 420 } else { | 423 } else { |
| 421 resolution = gfx::Size(1280, 1024); | 424 resolution = gfx::Size(1280, 1024); |
| 422 } | 425 } |
| 423 VideoCaptureParams capture_params; | 426 VideoCaptureParams capture_params; |
| 424 capture_params.requested_format.frame_size = resolution; | 427 capture_params.requested_format.frame_size = resolution; |
| 425 capture_params.requested_format.frame_rate = 30; | 428 capture_params.requested_format.frame_rate = 30; |
| 426 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 429 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 427 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 430 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 428 device->StopAndDeAllocate(); | 431 device->StopAndDeAllocate(); |
| 429 } | 432 } |
| 430 | 433 |
| 431 // Finally, do a device start and wait for it to finish. | 434 // Finally, do a device start and wait for it to finish. |
| 432 VideoCaptureParams capture_params; | 435 VideoCaptureParams capture_params; |
| 433 capture_params.requested_format.frame_size.SetSize(320, 240); | 436 capture_params.requested_format.frame_size.SetSize(320, 240); |
| 434 capture_params.requested_format.frame_rate = 30; | 437 capture_params.requested_format.frame_rate = 30; |
| 435 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 438 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 436 | 439 |
| 437 ResetWithNewClient(); | 440 ResetWithNewClient(); |
| 438 std::unique_ptr<VideoCaptureDevice> device( | 441 std::unique_ptr<VideoCaptureDevice> device( |
| 439 video_capture_device_factory_->Create(names_->front())); | 442 video_capture_device_factory_->CreateDevice( |
| 443 device_descriptors_->front())); |
| 440 | 444 |
| 441 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 445 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 442 WaitForCapturedFrame(); | 446 WaitForCapturedFrame(); |
| 443 device->StopAndDeAllocate(); | 447 device->StopAndDeAllocate(); |
| 444 device.reset(); | 448 device.reset(); |
| 445 EXPECT_EQ(last_format().frame_size.width(), 320); | 449 EXPECT_EQ(last_format().frame_size.width(), 320); |
| 446 EXPECT_EQ(last_format().frame_size.height(), 240); | 450 EXPECT_EQ(last_format().frame_size.height(), 240); |
| 447 } | 451 } |
| 448 | 452 |
| 449 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | 453 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
| 450 names_ = EnumerateDevices(); | 454 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 451 if (names_->empty()) { | 455 if (device_descriptors_->empty()) { |
| 452 VLOG(1) << "No camera available. Exiting test."; | 456 VLOG(1) << "No camera available. Exiting test."; |
| 453 return; | 457 return; |
| 454 } | 458 } |
| 455 std::unique_ptr<VideoCaptureDevice> device( | 459 std::unique_ptr<VideoCaptureDevice> device( |
| 456 video_capture_device_factory_->Create(names_->front())); | 460 video_capture_device_factory_->CreateDevice( |
| 461 device_descriptors_->front())); |
| 457 ASSERT_TRUE(device); | 462 ASSERT_TRUE(device); |
| 458 | 463 |
| 459 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | 464 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); |
| 460 | 465 |
| 461 VideoCaptureParams capture_params; | 466 VideoCaptureParams capture_params; |
| 462 capture_params.requested_format.frame_size.SetSize(640, 480); | 467 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 463 capture_params.requested_format.frame_rate = 30; | 468 capture_params.requested_format.frame_rate = 30; |
| 464 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 469 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 465 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 470 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 466 // Get captured video frames. | 471 // Get captured video frames. |
| 467 WaitForCapturedFrame(); | 472 WaitForCapturedFrame(); |
| 468 EXPECT_EQ(last_format().frame_size.width(), 640); | 473 EXPECT_EQ(last_format().frame_size.width(), 640); |
| 469 EXPECT_EQ(last_format().frame_size.height(), 480); | 474 EXPECT_EQ(last_format().frame_size.height(), 480); |
| 470 EXPECT_EQ(last_format().frame_rate, 30); | 475 EXPECT_EQ(last_format().frame_rate, 30); |
| 471 device->StopAndDeAllocate(); | 476 device->StopAndDeAllocate(); |
| 472 } | 477 } |
| 473 | 478 |
| 474 // Start the camera in 720p to capture MJPEG instead of a raw format. | 479 // Start the camera in 720p to capture MJPEG instead of a raw format. |
| 475 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { | 480 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { |
| 476 std::unique_ptr<VideoCaptureDevice::Name> name = | 481 std::unique_ptr<VideoCaptureDeviceDescriptor> device_descriptor = |
| 477 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MJPEG); | 482 GetFirstDeviceDescriptorSupportingPixelFormat(PIXEL_FORMAT_MJPEG); |
| 478 if (!name) { | 483 if (!device_descriptor) { |
| 479 VLOG(1) << "No camera supports MJPEG format. Exiting test."; | 484 VLOG(1) << "No camera supports MJPEG format. Exiting test."; |
| 480 return; | 485 return; |
| 481 } | 486 } |
| 482 #if defined(OS_WIN) | 487 #if defined(OS_WIN) |
| 483 base::win::Version version = base::win::GetVersion(); | 488 base::win::Version version = base::win::GetVersion(); |
| 484 VLOG(1) << "Windows version: " << (int)version; | 489 VLOG(1) << "Windows version: " << (int)version; |
| 485 if (version >= base::win::VERSION_WIN10) { | 490 if (version >= base::win::VERSION_WIN10) { |
| 486 VLOG(1) << "Skipped on Win10: http://crbug.com/570604."; | 491 VLOG(1) << "Skipped on Win10: http://crbug.com/570604."; |
| 487 return; | 492 return; |
| 488 } | 493 } |
| 489 #endif | 494 #endif |
| 490 std::unique_ptr<VideoCaptureDevice> device( | 495 std::unique_ptr<VideoCaptureDevice> device( |
| 491 video_capture_device_factory_->Create(*name)); | 496 video_capture_device_factory_->CreateDevice(*device_descriptor)); |
| 492 ASSERT_TRUE(device); | 497 ASSERT_TRUE(device); |
| 493 | 498 |
| 494 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | 499 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); |
| 495 | 500 |
| 496 VideoCaptureParams capture_params; | 501 VideoCaptureParams capture_params; |
| 497 capture_params.requested_format.frame_size.SetSize(1280, 720); | 502 capture_params.requested_format.frame_size.SetSize(1280, 720); |
| 498 capture_params.requested_format.frame_rate = 30; | 503 capture_params.requested_format.frame_rate = 30; |
| 499 capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG; | 504 capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG; |
| 500 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 505 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 501 // Get captured video frames. | 506 // Get captured video frames. |
| 502 WaitForCapturedFrame(); | 507 WaitForCapturedFrame(); |
| 503 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 | 508 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 |
| 504 // @ 30 fps, so we don't care about the exact resolution we get. | 509 // @ 30 fps, so we don't care about the exact resolution we get. |
| 505 EXPECT_EQ(last_format().pixel_format, PIXEL_FORMAT_MJPEG); | 510 EXPECT_EQ(last_format().pixel_format, PIXEL_FORMAT_MJPEG); |
| 506 EXPECT_GE(static_cast<size_t>(1280 * 720), | 511 EXPECT_GE(static_cast<size_t>(1280 * 720), |
| 507 last_format().ImageAllocationSize()); | 512 last_format().ImageAllocationSize()); |
| 508 device->StopAndDeAllocate(); | 513 device->StopAndDeAllocate(); |
| 509 } | 514 } |
| 510 | 515 |
| 511 TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 516 TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
| 512 // Use PIXEL_FORMAT_MAX to iterate all device names for testing | 517 // Use PIXEL_FORMAT_MAX to iterate all device names for testing |
| 513 // GetDeviceSupportedFormats(). | 518 // GetDeviceSupportedFormats(). |
| 514 std::unique_ptr<VideoCaptureDevice::Name> name = | 519 std::unique_ptr<VideoCaptureDeviceDescriptor> device_descriptor = |
| 515 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX); | 520 GetFirstDeviceDescriptorSupportingPixelFormat(PIXEL_FORMAT_MAX); |
| 516 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here | 521 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here |
| 517 // since we cannot forecast the hardware capabilities. | 522 // since we cannot forecast the hardware capabilities. |
| 518 ASSERT_FALSE(name); | 523 ASSERT_FALSE(device_descriptor); |
| 519 } | 524 } |
| 520 | 525 |
| 521 // Start the camera and take a photo. | 526 // Start the camera and take a photo. |
| 522 TEST_F(VideoCaptureDeviceTest, MAYBE_TakePhoto) { | 527 TEST_F(VideoCaptureDeviceTest, MAYBE_TakePhoto) { |
| 523 names_ = EnumerateDevices(); | 528 device_descriptors_ = EnumerateDeviceDescriptors(); |
| 524 if (names_->empty()) { | 529 if (device_descriptors_->empty()) { |
| 525 VLOG(1) << "No camera available. Exiting test."; | 530 VLOG(1) << "No camera available. Exiting test."; |
| 526 return; | 531 return; |
| 527 } | 532 } |
| 528 std::unique_ptr<VideoCaptureDevice> device( | 533 std::unique_ptr<VideoCaptureDevice> device( |
| 529 video_capture_device_factory_->Create(names_->front())); | 534 video_capture_device_factory_->CreateDevice( |
| 535 device_descriptors_->front())); |
| 530 ASSERT_TRUE(device); | 536 ASSERT_TRUE(device); |
| 531 | 537 |
| 532 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | 538 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); |
| 533 | 539 |
| 534 VideoCaptureParams capture_params; | 540 VideoCaptureParams capture_params; |
| 535 capture_params.requested_format.frame_size.SetSize(640, 480); | 541 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 536 capture_params.requested_format.frame_rate = 30; | 542 capture_params.requested_format.frame_rate = 30; |
| 537 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 543 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 538 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | 544 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); |
| 539 WaitForCapturedFrame(); | 545 WaitForCapturedFrame(); |
| 540 | 546 |
| 541 VideoCaptureDevice::TakePhotoCallback scoped_callback( | 547 VideoCaptureDevice::TakePhotoCallback scoped_callback( |
| 542 base::Bind(&MockImageCaptureClient::DoOnPhotoTaken, | 548 base::Bind(&MockImageCaptureClient::DoOnPhotoTaken, |
| 543 image_capture_client_), | 549 image_capture_client_), |
| 544 media::BindToCurrentLoop(base::Bind( | 550 media::BindToCurrentLoop(base::Bind( |
| 545 &MockImageCaptureClient::OnTakePhotoFailure, image_capture_client_))); | 551 &MockImageCaptureClient::OnTakePhotoFailure, image_capture_client_))); |
| 546 | 552 |
| 547 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); | 553 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); |
| 548 device->TakePhoto(std::move(scoped_callback)); | 554 device->TakePhoto(std::move(scoped_callback)); |
| 549 WaitForCapturedFrame(); | 555 WaitForCapturedFrame(); |
| 550 | 556 |
| 551 device->StopAndDeAllocate(); | 557 device->StopAndDeAllocate(); |
| 552 } | 558 } |
| 553 | 559 |
| 554 }; // namespace media | 560 }; // namespace media |
| OLD | NEW |