| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/video/video_capture_device.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "base/bind.h" | |
| 14 #include "base/bind_helpers.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "base/single_thread_task_runner.h" | |
| 18 #include "base/test/test_timeouts.h" | |
| 19 #include "base/threading/thread.h" | |
| 20 #include "base/threading/thread_task_runner_handle.h" | |
| 21 #include "build/build_config.h" | |
| 22 #include "media/base/bind_to_current_loop.h" | |
| 23 #include "media/base/video_capture_types.h" | |
| 24 #include "media/capture/video/video_capture_device_factory.h" | |
| 25 #include "testing/gmock/include/gmock/gmock.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | |
| 27 | |
| 28 #if defined(OS_WIN) | |
| 29 #include "base/win/scoped_com_initializer.h" | |
| 30 #include "base/win/windows_version.h" // For fine-grained suppression. | |
| 31 #include "media/capture/video/win/video_capture_device_factory_win.h" | |
| 32 #endif | |
| 33 | |
| 34 #if defined(OS_MACOSX) | |
| 35 #include "media/base/mac/avfoundation_glue.h" | |
| 36 #include "media/capture/video/mac/video_capture_device_factory_mac.h" | |
| 37 #endif | |
| 38 | |
| 39 #if defined(OS_ANDROID) | |
| 40 #include "base/android/jni_android.h" | |
| 41 #include "media/capture/video/android/video_capture_device_android.h" | |
| 42 #endif | |
| 43 | |
| 44 #if defined(OS_MACOSX) | |
| 45 // Mac will always give you the size you ask for and this case will fail. | |
| 46 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize | |
| 47 // We will always get YUYV from the Mac AVFoundation implementations. | |
| 48 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg | |
| 49 #define MAYBE_TakePhoto TakePhoto | |
| 50 #elif defined(OS_WIN) | |
| 51 #define MAYBE_AllocateBadSize AllocateBadSize | |
| 52 #define MAYBE_CaptureMjpeg CaptureMjpeg | |
| 53 #define MAYBE_TakePhoto DISABLED_TakePhoto | |
| 54 #elif defined(OS_ANDROID) | |
| 55 // TODO(wjia): enable those tests on Android. | |
| 56 // On Android, native camera (JAVA) delivers frames on UI thread which is the | |
| 57 // main thread for tests. This results in no frame received by | |
| 58 // VideoCaptureAndroid. | |
| 59 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize | |
| 60 #define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning | |
| 61 #define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning | |
| 62 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg | |
| 63 #define MAYBE_TakePhoto DISABLED_TakePhoto | |
| 64 #elif defined(OS_LINUX) | |
| 65 // AllocateBadSize will hang when a real camera is attached and if more than one | |
| 66 // test is trying to use the camera (even across processes). Do NOT renable | |
| 67 // this test without fixing the many bugs associated with it: | |
| 68 // http://crbug.com/94134 http://crbug.com/137260 http://crbug.com/417824 | |
| 69 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize | |
| 70 #define MAYBE_CaptureMjpeg CaptureMjpeg | |
| 71 #define MAYBE_TakePhoto DISABLED_TakePhoto | |
| 72 #else | |
| 73 #define MAYBE_AllocateBadSize AllocateBadSize | |
| 74 #define MAYBE_CaptureMjpeg CaptureMjpeg | |
| 75 #define MAYBE_TakePhoto DISABLED_TakePhoto | |
| 76 #endif | |
| 77 | |
| 78 using ::testing::_; | |
| 79 using ::testing::SaveArg; | |
| 80 | |
| 81 namespace media { | |
| 82 namespace { | |
| 83 | |
| 84 class MockVideoCaptureClient : public VideoCaptureDevice::Client { | |
| 85 public: | |
| 86 MOCK_METHOD0(DoReserveOutputBuffer, void(void)); | |
| 87 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void)); | |
| 88 MOCK_METHOD0(DoOnIncomingCapturedVideoFrame, void(void)); | |
| 89 MOCK_METHOD0(DoResurrectLastOutputBuffer, void(void)); | |
| 90 MOCK_METHOD2(OnError, | |
| 91 void(const tracked_objects::Location& from_here, | |
| 92 const std::string& reason)); | |
| 93 MOCK_CONST_METHOD0(GetBufferPoolUtilization, double(void)); | |
| 94 | |
| 95 explicit MockVideoCaptureClient( | |
| 96 base::Callback<void(const VideoCaptureFormat&)> frame_cb) | |
| 97 : main_thread_(base::ThreadTaskRunnerHandle::Get()), | |
| 98 frame_cb_(frame_cb) {} | |
| 99 | |
| 100 void OnIncomingCapturedData(const uint8_t* data, | |
| 101 int length, | |
| 102 const VideoCaptureFormat& format, | |
| 103 int rotation, | |
| 104 base::TimeTicks reference_time, | |
| 105 base::TimeDelta timestamp) override { | |
| 106 ASSERT_GT(length, 0); | |
| 107 ASSERT_TRUE(data != NULL); | |
| 108 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); | |
| 109 } | |
| 110 | |
| 111 // Trampoline methods to workaround GMOCK problems with std::unique_ptr<>. | |
| 112 std::unique_ptr<Buffer> ReserveOutputBuffer( | |
| 113 const gfx::Size& dimensions, | |
| 114 media::VideoPixelFormat format, | |
| 115 media::VideoPixelStorage storage) override { | |
| 116 DoReserveOutputBuffer(); | |
| 117 NOTREACHED() << "This should never be called"; | |
| 118 return std::unique_ptr<Buffer>(); | |
| 119 } | |
| 120 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, | |
| 121 const VideoCaptureFormat& frame_format, | |
| 122 base::TimeTicks reference_time, | |
| 123 base::TimeDelta timestamp) override { | |
| 124 DoOnIncomingCapturedBuffer(); | |
| 125 } | |
| 126 void OnIncomingCapturedVideoFrame( | |
| 127 std::unique_ptr<Buffer> buffer, | |
| 128 const scoped_refptr<VideoFrame>& frame) override { | |
| 129 DoOnIncomingCapturedVideoFrame(); | |
| 130 } | |
| 131 std::unique_ptr<Buffer> ResurrectLastOutputBuffer( | |
| 132 const gfx::Size& dimensions, | |
| 133 media::VideoPixelFormat format, | |
| 134 media::VideoPixelStorage storage) { | |
| 135 DoResurrectLastOutputBuffer(); | |
| 136 NOTREACHED() << "This should never be called"; | |
| 137 return std::unique_ptr<Buffer>(); | |
| 138 } | |
| 139 | |
| 140 private: | |
| 141 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | |
| 142 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; | |
| 143 }; | |
| 144 | |
| 145 class MockImageCaptureClient : public base::RefCounted<MockImageCaptureClient> { | |
| 146 public: | |
| 147 // GMock doesn't support move-only arguments, so we use this forward method. | |
| 148 void DoOnPhotoTaken(mojom::BlobPtr blob) { | |
| 149 EXPECT_STREQ("image/jpeg", blob->mime_type.c_str()); | |
| 150 ASSERT_GT(blob->data.size(), 4u); | |
| 151 // Check some bytes that univocally identify |data| as a JPEG File. | |
| 152 // https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#File_format_st
ructure | |
| 153 EXPECT_EQ(0xFF, blob->data[0]); // First SOI byte | |
| 154 EXPECT_EQ(0xD8, blob->data[1]); // Second SOI byte | |
| 155 EXPECT_EQ(0xFF, blob->data[2]); // First JFIF-APP0 byte | |
| 156 EXPECT_EQ(0xE0, blob->data[3]); // Second JFIF-APP0 byte | |
| 157 OnCorrectPhotoTaken(); | |
| 158 } | |
| 159 MOCK_METHOD0(OnCorrectPhotoTaken, void(void)); | |
| 160 MOCK_METHOD1(OnTakePhotoFailure, | |
| 161 void(const base::Callback<void(mojom::BlobPtr)>&)); | |
| 162 | |
| 163 private: | |
| 164 friend class base::RefCounted<MockImageCaptureClient>; | |
| 165 virtual ~MockImageCaptureClient() {} | |
| 166 }; | |
| 167 | |
| 168 class DeviceEnumerationListener | |
| 169 : public base::RefCounted<DeviceEnumerationListener> { | |
| 170 public: | |
| 171 MOCK_METHOD1(OnEnumerateDeviceDescriptorsCallbackPtr, | |
| 172 void(VideoCaptureDeviceDescriptors* device_descriptors)); | |
| 173 // GMock doesn't support move-only arguments, so we use this forward method. | |
| 174 void OnEnumerateDeviceDescriptorsCallback( | |
| 175 std::unique_ptr<VideoCaptureDeviceDescriptors> device_descriptors) { | |
| 176 OnEnumerateDeviceDescriptorsCallbackPtr(device_descriptors.release()); | |
| 177 } | |
| 178 | |
| 179 private: | |
| 180 friend class base::RefCounted<DeviceEnumerationListener>; | |
| 181 virtual ~DeviceEnumerationListener() {} | |
| 182 }; | |
| 183 | |
| 184 } // namespace | |
| 185 | |
| 186 class VideoCaptureDeviceTest : public testing::TestWithParam<gfx::Size> { | |
| 187 protected: | |
| 188 typedef VideoCaptureDevice::Client Client; | |
| 189 | |
| 190 VideoCaptureDeviceTest() | |
| 191 : loop_(new base::MessageLoop()), | |
| 192 video_capture_client_(new MockVideoCaptureClient( | |
| 193 base::Bind(&VideoCaptureDeviceTest::OnFrameCaptured, | |
| 194 base::Unretained(this)))), | |
| 195 device_enumeration_listener_(new DeviceEnumerationListener()), | |
| 196 image_capture_client_(new MockImageCaptureClient()), | |
| 197 video_capture_device_factory_(VideoCaptureDeviceFactory::CreateFactory( | |
| 198 base::ThreadTaskRunnerHandle::Get())) {} | |
| 199 | |
| 200 void SetUp() override { | |
| 201 #if defined(OS_ANDROID) | |
| 202 VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice( | |
| 203 base::android::AttachCurrentThread()); | |
| 204 #endif | |
| 205 #if defined(OS_MACOSX) | |
| 206 AVFoundationGlue::InitializeAVFoundation(); | |
| 207 #endif | |
| 208 EXPECT_CALL(*video_capture_client_, DoReserveOutputBuffer()).Times(0); | |
| 209 EXPECT_CALL(*video_capture_client_, DoOnIncomingCapturedBuffer()).Times(0); | |
| 210 EXPECT_CALL(*video_capture_client_, DoOnIncomingCapturedVideoFrame()) | |
| 211 .Times(0); | |
| 212 } | |
| 213 | |
| 214 void ResetWithNewClient() { | |
| 215 video_capture_client_.reset(new MockVideoCaptureClient(base::Bind( | |
| 216 &VideoCaptureDeviceTest::OnFrameCaptured, base::Unretained(this)))); | |
| 217 } | |
| 218 | |
| 219 void OnFrameCaptured(const VideoCaptureFormat& format) { | |
| 220 last_format_ = format; | |
| 221 run_loop_->QuitClosure().Run(); | |
| 222 } | |
| 223 | |
| 224 void WaitForCapturedFrame() { | |
| 225 run_loop_.reset(new base::RunLoop()); | |
| 226 run_loop_->Run(); | |
| 227 } | |
| 228 | |
| 229 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDeviceDescriptors() { | |
| 230 VideoCaptureDeviceDescriptors* device_descriptors; | |
| 231 EXPECT_CALL(*device_enumeration_listener_.get(), | |
| 232 OnEnumerateDeviceDescriptorsCallbackPtr(_)) | |
| 233 .WillOnce(SaveArg<0>(&device_descriptors)); | |
| 234 | |
| 235 video_capture_device_factory_->EnumerateDeviceDescriptors(base::Bind( | |
| 236 &DeviceEnumerationListener::OnEnumerateDeviceDescriptorsCallback, | |
| 237 device_enumeration_listener_)); | |
| 238 base::RunLoop().RunUntilIdle(); | |
| 239 return std::unique_ptr<VideoCaptureDeviceDescriptors>(device_descriptors); | |
| 240 } | |
| 241 | |
| 242 const VideoCaptureFormat& last_format() const { return last_format_; } | |
| 243 | |
| 244 std::unique_ptr<VideoCaptureDeviceDescriptor> | |
| 245 GetFirstDeviceDescriptorSupportingPixelFormat( | |
| 246 const VideoPixelFormat& pixel_format) { | |
| 247 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 248 if (device_descriptors_->empty()) { | |
| 249 DVLOG(1) << "No camera available."; | |
| 250 return std::unique_ptr<VideoCaptureDeviceDescriptor>(); | |
| 251 } | |
| 252 for (const auto& descriptors_iterator : *device_descriptors_) { | |
| 253 VideoCaptureFormats supported_formats; | |
| 254 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, | |
| 255 &supported_formats); | |
| 256 for (const auto& formats_iterator : supported_formats) { | |
| 257 if (formats_iterator.pixel_format == pixel_format) { | |
| 258 return std::unique_ptr<VideoCaptureDeviceDescriptor>( | |
| 259 new VideoCaptureDeviceDescriptor(descriptors_iterator)); | |
| 260 } | |
| 261 } | |
| 262 } | |
| 263 DVLOG_IF(1, pixel_format != PIXEL_FORMAT_MAX) | |
| 264 << "No camera can capture the" | |
| 265 << " format: " << VideoPixelFormatToString(pixel_format); | |
| 266 return std::unique_ptr<VideoCaptureDeviceDescriptor>(); | |
| 267 } | |
| 268 | |
| 269 bool IsCaptureSizeSupported(const VideoCaptureDeviceDescriptor& device, | |
| 270 const gfx::Size& size) { | |
| 271 VideoCaptureFormats supported_formats; | |
| 272 video_capture_device_factory_->GetSupportedFormats(device, | |
| 273 &supported_formats); | |
| 274 const auto it = std::find_if( | |
| 275 supported_formats.begin(), supported_formats.end(), | |
| 276 [&size](VideoCaptureFormat const& f) { return f.frame_size == size; }); | |
| 277 if (it == supported_formats.end()) { | |
| 278 DVLOG(1) << "Size " << size.ToString() << " is not supported."; | |
| 279 return false; | |
| 280 } | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 #if defined(OS_WIN) | |
| 285 base::win::ScopedCOMInitializer initialize_com_; | |
| 286 #endif | |
| 287 std::unique_ptr<VideoCaptureDeviceDescriptors> device_descriptors_; | |
| 288 const std::unique_ptr<base::MessageLoop> loop_; | |
| 289 std::unique_ptr<base::RunLoop> run_loop_; | |
| 290 std::unique_ptr<MockVideoCaptureClient> video_capture_client_; | |
| 291 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | |
| 292 const scoped_refptr<MockImageCaptureClient> image_capture_client_; | |
| 293 VideoCaptureFormat last_format_; | |
| 294 const std::unique_ptr<VideoCaptureDeviceFactory> | |
| 295 video_capture_device_factory_; | |
| 296 }; | |
| 297 | |
| 298 // Cause hangs on Windows Debug. http://crbug.com/417824 | |
| 299 #if defined(OS_WIN) && !defined(NDEBUG) | |
| 300 #define MAYBE_OpenInvalidDevice DISABLED_OpenInvalidDevice | |
| 301 #else | |
| 302 #define MAYBE_OpenInvalidDevice OpenInvalidDevice | |
| 303 #endif | |
| 304 | |
| 305 TEST_F(VideoCaptureDeviceTest, MAYBE_OpenInvalidDevice) { | |
| 306 VideoCaptureDeviceDescriptor invalid_descriptor; | |
| 307 invalid_descriptor.device_id = "jibberish"; | |
| 308 invalid_descriptor.display_name = "jibberish"; | |
| 309 #if defined(OS_WIN) | |
| 310 invalid_descriptor.capture_api = | |
| 311 VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() | |
| 312 ? VideoCaptureApi::WIN_MEDIA_FOUNDATION | |
| 313 : VideoCaptureApi::WIN_DIRECT_SHOW; | |
| 314 #elif defined(OS_MACOSX) | |
| 315 invalid_descriptor.capture_api = VideoCaptureApi::MACOSX_AVFOUNDATION; | |
| 316 #endif | |
| 317 std::unique_ptr<VideoCaptureDevice> device = | |
| 318 video_capture_device_factory_->CreateDevice(invalid_descriptor); | |
| 319 | |
| 320 #if !defined(OS_MACOSX) | |
| 321 EXPECT_TRUE(device == NULL); | |
| 322 #else | |
| 323 // The presence of the actual device is only checked on AllocateAndStart() | |
| 324 // and not on creation. | |
| 325 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(1); | |
| 326 | |
| 327 VideoCaptureParams capture_params; | |
| 328 capture_params.requested_format.frame_size.SetSize(640, 480); | |
| 329 capture_params.requested_format.frame_rate = 30; | |
| 330 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 331 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 332 device->StopAndDeAllocate(); | |
| 333 #endif | |
| 334 } | |
| 335 | |
| 336 TEST_P(VideoCaptureDeviceTest, CaptureWithSize) { | |
| 337 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 338 if (device_descriptors_->empty()) { | |
| 339 VLOG(1) << "No camera available. Exiting test."; | |
| 340 return; | |
| 341 } | |
| 342 | |
| 343 const gfx::Size& size = GetParam(); | |
| 344 if (!IsCaptureSizeSupported(device_descriptors_->front(), size)) | |
| 345 return; | |
| 346 const int width = size.width(); | |
| 347 const int height = size.height(); | |
| 348 | |
| 349 std::unique_ptr<VideoCaptureDevice> device( | |
| 350 video_capture_device_factory_->CreateDevice( | |
| 351 device_descriptors_->front())); | |
| 352 ASSERT_TRUE(device); | |
| 353 DVLOG(1) << device_descriptors_->front().device_id; | |
| 354 | |
| 355 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | |
| 356 | |
| 357 VideoCaptureParams capture_params; | |
| 358 capture_params.requested_format.frame_size.SetSize(width, height); | |
| 359 capture_params.requested_format.frame_rate = 30.0f; | |
| 360 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 361 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 362 // Get captured video frames. | |
| 363 WaitForCapturedFrame(); | |
| 364 EXPECT_EQ(last_format().frame_size.width(), width); | |
| 365 EXPECT_EQ(last_format().frame_size.height(), height); | |
| 366 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) | |
| 367 EXPECT_EQ(size.GetArea(), last_format().frame_size.GetArea()); | |
| 368 device->StopAndDeAllocate(); | |
| 369 } | |
| 370 | |
| 371 #if !defined(OS_ANDROID) | |
| 372 const gfx::Size kCaptureSizes[] = {gfx::Size(640, 480), gfx::Size(1280, 720)}; | |
| 373 | |
| 374 INSTANTIATE_TEST_CASE_P(VideoCaptureDeviceTests, | |
| 375 VideoCaptureDeviceTest, | |
| 376 testing::ValuesIn(kCaptureSizes)); | |
| 377 #endif | |
| 378 | |
| 379 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { | |
| 380 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 381 if (device_descriptors_->empty()) { | |
| 382 VLOG(1) << "No camera available. Exiting test."; | |
| 383 return; | |
| 384 } | |
| 385 std::unique_ptr<VideoCaptureDevice> device( | |
| 386 video_capture_device_factory_->CreateDevice( | |
| 387 device_descriptors_->front())); | |
| 388 ASSERT_TRUE(device); | |
| 389 | |
| 390 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | |
| 391 | |
| 392 const gfx::Size input_size(640, 480); | |
| 393 VideoCaptureParams capture_params; | |
| 394 capture_params.requested_format.frame_size.SetSize(637, 472); | |
| 395 capture_params.requested_format.frame_rate = 35; | |
| 396 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 397 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 398 WaitForCapturedFrame(); | |
| 399 device->StopAndDeAllocate(); | |
| 400 EXPECT_EQ(last_format().frame_size.width(), input_size.width()); | |
| 401 EXPECT_EQ(last_format().frame_size.height(), input_size.height()); | |
| 402 if (last_format().pixel_format != PIXEL_FORMAT_MJPEG) | |
| 403 EXPECT_EQ(input_size.GetArea(), last_format().frame_size.GetArea()); | |
| 404 } | |
| 405 | |
| 406 // Cause hangs on Windows, Linux. Fails Android. http://crbug.com/417824 | |
| 407 TEST_F(VideoCaptureDeviceTest, DISABLED_ReAllocateCamera) { | |
| 408 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 409 if (device_descriptors_->empty()) { | |
| 410 VLOG(1) << "No camera available. Exiting test."; | |
| 411 return; | |
| 412 } | |
| 413 | |
| 414 // First, do a number of very fast device start/stops. | |
| 415 for (int i = 0; i <= 5; i++) { | |
| 416 ResetWithNewClient(); | |
| 417 std::unique_ptr<VideoCaptureDevice> device( | |
| 418 video_capture_device_factory_->CreateDevice( | |
| 419 device_descriptors_->front())); | |
| 420 gfx::Size resolution; | |
| 421 if (i % 2) { | |
| 422 resolution = gfx::Size(640, 480); | |
| 423 } else { | |
| 424 resolution = gfx::Size(1280, 1024); | |
| 425 } | |
| 426 VideoCaptureParams capture_params; | |
| 427 capture_params.requested_format.frame_size = resolution; | |
| 428 capture_params.requested_format.frame_rate = 30; | |
| 429 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 430 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 431 device->StopAndDeAllocate(); | |
| 432 } | |
| 433 | |
| 434 // Finally, do a device start and wait for it to finish. | |
| 435 VideoCaptureParams capture_params; | |
| 436 capture_params.requested_format.frame_size.SetSize(320, 240); | |
| 437 capture_params.requested_format.frame_rate = 30; | |
| 438 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 439 | |
| 440 ResetWithNewClient(); | |
| 441 std::unique_ptr<VideoCaptureDevice> device( | |
| 442 video_capture_device_factory_->CreateDevice( | |
| 443 device_descriptors_->front())); | |
| 444 | |
| 445 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 446 WaitForCapturedFrame(); | |
| 447 device->StopAndDeAllocate(); | |
| 448 device.reset(); | |
| 449 EXPECT_EQ(last_format().frame_size.width(), 320); | |
| 450 EXPECT_EQ(last_format().frame_size.height(), 240); | |
| 451 } | |
| 452 | |
| 453 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | |
| 454 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 455 if (device_descriptors_->empty()) { | |
| 456 VLOG(1) << "No camera available. Exiting test."; | |
| 457 return; | |
| 458 } | |
| 459 std::unique_ptr<VideoCaptureDevice> device( | |
| 460 video_capture_device_factory_->CreateDevice( | |
| 461 device_descriptors_->front())); | |
| 462 ASSERT_TRUE(device); | |
| 463 | |
| 464 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | |
| 465 | |
| 466 VideoCaptureParams capture_params; | |
| 467 capture_params.requested_format.frame_size.SetSize(640, 480); | |
| 468 capture_params.requested_format.frame_rate = 30; | |
| 469 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 470 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 471 // Get captured video frames. | |
| 472 WaitForCapturedFrame(); | |
| 473 EXPECT_EQ(last_format().frame_size.width(), 640); | |
| 474 EXPECT_EQ(last_format().frame_size.height(), 480); | |
| 475 EXPECT_EQ(last_format().frame_rate, 30); | |
| 476 device->StopAndDeAllocate(); | |
| 477 } | |
| 478 | |
| 479 // Start the camera in 720p to capture MJPEG instead of a raw format. | |
| 480 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { | |
| 481 std::unique_ptr<VideoCaptureDeviceDescriptor> device_descriptor = | |
| 482 GetFirstDeviceDescriptorSupportingPixelFormat(PIXEL_FORMAT_MJPEG); | |
| 483 if (!device_descriptor) { | |
| 484 VLOG(1) << "No camera supports MJPEG format. Exiting test."; | |
| 485 return; | |
| 486 } | |
| 487 #if defined(OS_WIN) | |
| 488 base::win::Version version = base::win::GetVersion(); | |
| 489 VLOG(1) << "Windows version: " << (int)version; | |
| 490 if (version >= base::win::VERSION_WIN10) { | |
| 491 VLOG(1) << "Skipped on Win10: http://crbug.com/570604."; | |
| 492 return; | |
| 493 } | |
| 494 #endif | |
| 495 std::unique_ptr<VideoCaptureDevice> device( | |
| 496 video_capture_device_factory_->CreateDevice(*device_descriptor)); | |
| 497 ASSERT_TRUE(device); | |
| 498 | |
| 499 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | |
| 500 | |
| 501 VideoCaptureParams capture_params; | |
| 502 capture_params.requested_format.frame_size.SetSize(1280, 720); | |
| 503 capture_params.requested_format.frame_rate = 30; | |
| 504 capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG; | |
| 505 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 506 // Get captured video frames. | |
| 507 WaitForCapturedFrame(); | |
| 508 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 | |
| 509 // @ 30 fps, so we don't care about the exact resolution we get. | |
| 510 EXPECT_EQ(last_format().pixel_format, PIXEL_FORMAT_MJPEG); | |
| 511 EXPECT_GE(static_cast<size_t>(1280 * 720), | |
| 512 last_format().ImageAllocationSize()); | |
| 513 device->StopAndDeAllocate(); | |
| 514 } | |
| 515 | |
| 516 TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { | |
| 517 // Use PIXEL_FORMAT_MAX to iterate all device names for testing | |
| 518 // GetDeviceSupportedFormats(). | |
| 519 std::unique_ptr<VideoCaptureDeviceDescriptor> device_descriptor = | |
| 520 GetFirstDeviceDescriptorSupportingPixelFormat(PIXEL_FORMAT_MAX); | |
| 521 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here | |
| 522 // since we cannot forecast the hardware capabilities. | |
| 523 ASSERT_FALSE(device_descriptor); | |
| 524 } | |
| 525 | |
| 526 // Start the camera and take a photo. | |
| 527 TEST_F(VideoCaptureDeviceTest, MAYBE_TakePhoto) { | |
| 528 device_descriptors_ = EnumerateDeviceDescriptors(); | |
| 529 if (device_descriptors_->empty()) { | |
| 530 VLOG(1) << "No camera available. Exiting test."; | |
| 531 return; | |
| 532 } | |
| 533 std::unique_ptr<VideoCaptureDevice> device( | |
| 534 video_capture_device_factory_->CreateDevice( | |
| 535 device_descriptors_->front())); | |
| 536 ASSERT_TRUE(device); | |
| 537 | |
| 538 EXPECT_CALL(*video_capture_client_, OnError(_, _)).Times(0); | |
| 539 | |
| 540 VideoCaptureParams capture_params; | |
| 541 capture_params.requested_format.frame_size.SetSize(640, 480); | |
| 542 capture_params.requested_format.frame_rate = 30; | |
| 543 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | |
| 544 device->AllocateAndStart(capture_params, std::move(video_capture_client_)); | |
| 545 WaitForCapturedFrame(); | |
| 546 | |
| 547 VideoCaptureDevice::TakePhotoCallback scoped_callback( | |
| 548 base::Bind(&MockImageCaptureClient::DoOnPhotoTaken, | |
| 549 image_capture_client_), | |
| 550 media::BindToCurrentLoop(base::Bind( | |
| 551 &MockImageCaptureClient::OnTakePhotoFailure, image_capture_client_))); | |
| 552 | |
| 553 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); | |
| 554 device->TakePhoto(std::move(scoped_callback)); | |
| 555 WaitForCapturedFrame(); | |
| 556 | |
| 557 device->StopAndDeAllocate(); | |
| 558 } | |
| 559 | |
| 560 }; // namespace media | |
| OLD | NEW |