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.
6 #include "base/bind_helpers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/threading/thread.h"
12 #include "media/video/capture/fake_video_capture_device.h"
13 #include "media/video/capture/fake_video_capture_device_factory.h"
14 #include "media/video/capture/video_capture_device.h"
15 #include "media/video/capture/video_capture_types.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 #include "base/win/scoped_com_initializer.h"
21 #include "media/video/capture/win/video_capture_device_mf_win.h"
24 #if defined(OS_ANDROID)
25 #include "base/android/jni_android.h"
26 #include "media/video/capture/android/video_capture_device_android.h"
29 #if defined(OS_MACOSX)
30 // Mac/QTKit will always give you the size you ask for and this case will fail.
31 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
32 // We will always get YUYV from the Mac QTKit/AVFoundation implementations.
33 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
35 #define MAYBE_AllocateBadSize AllocateBadSize
36 // Windows currently uses DirectShow to convert from MJPEG and a raw format is
38 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
39 #elif defined(OS_ANDROID)
40 // TODO(wjia): enable those tests on Android.
41 // On Android, native camera (JAVA) delivers frames on UI thread which is the
42 // main thread for tests. This results in no frame received by
43 // VideoCaptureAndroid.
44 #define CaptureVGA DISABLED_CaptureVGA
45 #define Capture720p DISABLED_Capture720p
46 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
47 #define ReAllocateCamera DISABLED_ReAllocateCamera
48 #define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning
49 #define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning
50 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
52 #define MAYBE_AllocateBadSize AllocateBadSize
53 #define MAYBE_CaptureMjpeg CaptureMjpeg
57 using ::testing::AnyNumber
;
58 using ::testing::Return
;
59 using ::testing::AtLeast
;
63 class MockClient
: public media::VideoCaptureDevice::Client
{
65 MOCK_METHOD2(ReserveOutputBuffer
,
66 scoped_refptr
<Buffer
>(media::VideoFrame::Format format
,
67 const gfx::Size
& dimensions
));
68 MOCK_METHOD0(OnErr
, void());
70 explicit MockClient(base::Callback
<void(const VideoCaptureFormat
&)> frame_cb
)
71 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb
) {}
73 virtual void OnError(const std::string
& error_message
) OVERRIDE
{
77 virtual void OnIncomingCapturedData(const uint8
* data
,
79 const VideoCaptureFormat
& format
,
81 base::TimeTicks timestamp
) OVERRIDE
{
82 main_thread_
->PostTask(FROM_HERE
, base::Bind(frame_cb_
, format
));
85 virtual void OnIncomingCapturedVideoFrame(
86 const scoped_refptr
<Buffer
>& buffer
,
87 const media::VideoCaptureFormat
& buffer_format
,
88 const scoped_refptr
<media::VideoFrame
>& frame
,
89 base::TimeTicks timestamp
) OVERRIDE
{
94 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
95 base::Callback
<void(const VideoCaptureFormat
&)> frame_cb_
;
98 class VideoCaptureDeviceTest
: public testing::Test
{
100 typedef media::VideoCaptureDevice::Client Client
;
102 VideoCaptureDeviceTest()
103 : loop_(new base::MessageLoop()),
105 new MockClient(base::Bind(&VideoCaptureDeviceTest::OnFrameCaptured
,
106 base::Unretained(this)))),
107 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {}
109 virtual void SetUp() {
110 #if defined(OS_ANDROID)
111 media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(
112 base::android::AttachCurrentThread());
116 void ResetWithNewClient() {
117 client_
.reset(new MockClient(base::Bind(
118 &VideoCaptureDeviceTest::OnFrameCaptured
, base::Unretained(this))));
121 void OnFrameCaptured(const VideoCaptureFormat
& format
) {
122 last_format_
= format
;
123 run_loop_
->QuitClosure().Run();
126 void WaitForCapturedFrame() {
127 run_loop_
.reset(new base::RunLoop());
131 const VideoCaptureFormat
& last_format() const { return last_format_
; }
133 scoped_ptr
<VideoCaptureDevice::Name
> GetFirstDeviceNameSupportingPixelFormat(
134 const VideoPixelFormat
& pixel_format
) {
135 VideoCaptureDevice::GetDeviceNames(&names_
);
136 if (!names_
.size()) {
137 DVLOG(1) << "No camera available.";
138 return scoped_ptr
<VideoCaptureDevice::Name
>();
140 VideoCaptureDevice::Names::iterator names_iterator
;
141 for (names_iterator
= names_
.begin(); names_iterator
!= names_
.end();
143 VideoCaptureFormats supported_formats
;
144 VideoCaptureDevice::GetDeviceSupportedFormats(*names_iterator
,
146 VideoCaptureFormats::iterator formats_iterator
;
147 for (formats_iterator
= supported_formats
.begin();
148 formats_iterator
!= supported_formats
.end(); ++formats_iterator
) {
149 if (formats_iterator
->pixel_format
== pixel_format
) {
150 return scoped_ptr
<VideoCaptureDevice::Name
>(
151 new VideoCaptureDevice::Name(*names_iterator
));
155 DVLOG(1) << "No camera can capture the format: " << pixel_format
;
156 return scoped_ptr
<VideoCaptureDevice::Name
>();
160 base::win::ScopedCOMInitializer initialize_com_
;
162 VideoCaptureDevice::Names names_
;
163 scoped_ptr
<base::MessageLoop
> loop_
;
164 scoped_ptr
<base::RunLoop
> run_loop_
;
165 scoped_ptr
<MockClient
> client_
;
166 VideoCaptureFormat last_format_
;
167 scoped_ptr
<VideoCaptureDeviceFactory
> video_capture_device_factory_
;
170 TEST_F(VideoCaptureDeviceTest
, OpenInvalidDevice
) {
172 VideoCaptureDevice::Name::CaptureApiType api_type
=
173 VideoCaptureDeviceMFWin::PlatformSupported()
174 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION
175 : VideoCaptureDevice::Name::DIRECT_SHOW
;
176 VideoCaptureDevice::Name
device_name("jibberish", "jibberish", api_type
);
178 VideoCaptureDevice::Name
device_name("jibberish", "jibberish");
180 VideoCaptureDevice
* device
= VideoCaptureDevice::Create(device_name
);
181 EXPECT_TRUE(device
== NULL
);
184 TEST_F(VideoCaptureDeviceTest
, CaptureVGA
) {
185 VideoCaptureDevice::GetDeviceNames(&names_
);
186 if (!names_
.size()) {
187 DVLOG(1) << "No camera available. Exiting test.";
191 scoped_ptr
<VideoCaptureDevice
> device(
192 VideoCaptureDevice::Create(names_
.front()));
194 DVLOG(1) << names_
.front().id();
196 EXPECT_CALL(*client_
, OnErr())
199 VideoCaptureParams capture_params
;
200 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
201 capture_params
.requested_format
.frame_rate
= 30;
202 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
203 capture_params
.allow_resolution_change
= false;
204 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
205 // Get captured video frames.
206 WaitForCapturedFrame();
207 EXPECT_EQ(last_format().frame_size
.width(), 640);
208 EXPECT_EQ(last_format().frame_size
.height(), 480);
209 device
->StopAndDeAllocate();
212 TEST_F(VideoCaptureDeviceTest
, Capture720p
) {
213 VideoCaptureDevice::GetDeviceNames(&names_
);
214 if (!names_
.size()) {
215 DVLOG(1) << "No camera available. Exiting test.";
219 scoped_ptr
<VideoCaptureDevice
> device(
220 VideoCaptureDevice::Create(names_
.front()));
223 EXPECT_CALL(*client_
, OnErr())
226 VideoCaptureParams capture_params
;
227 capture_params
.requested_format
.frame_size
.SetSize(1280, 720);
228 capture_params
.requested_format
.frame_rate
= 30;
229 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
230 capture_params
.allow_resolution_change
= false;
231 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
232 // Get captured video frames.
233 WaitForCapturedFrame();
234 device
->StopAndDeAllocate();
237 TEST_F(VideoCaptureDeviceTest
, MAYBE_AllocateBadSize
) {
238 VideoCaptureDevice::GetDeviceNames(&names_
);
239 if (!names_
.size()) {
240 DVLOG(1) << "No camera available. Exiting test.";
243 scoped_ptr
<VideoCaptureDevice
> device(
244 VideoCaptureDevice::Create(names_
.front()));
247 EXPECT_CALL(*client_
, OnErr())
250 VideoCaptureParams capture_params
;
251 capture_params
.requested_format
.frame_size
.SetSize(637, 472);
252 capture_params
.requested_format
.frame_rate
= 35;
253 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
254 capture_params
.allow_resolution_change
= false;
255 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
256 WaitForCapturedFrame();
257 device
->StopAndDeAllocate();
258 EXPECT_EQ(last_format().frame_size
.width(), 640);
259 EXPECT_EQ(last_format().frame_size
.height(), 480);
262 TEST_F(VideoCaptureDeviceTest
, ReAllocateCamera
) {
263 VideoCaptureDevice::GetDeviceNames(&names_
);
264 if (!names_
.size()) {
265 DVLOG(1) << "No camera available. Exiting test.";
269 // First, do a number of very fast device start/stops.
270 for (int i
= 0; i
<= 5; i
++) {
271 ResetWithNewClient();
272 scoped_ptr
<VideoCaptureDevice
> device(
273 VideoCaptureDevice::Create(names_
.front()));
274 gfx::Size resolution
;
276 resolution
= gfx::Size(640, 480);
278 resolution
= gfx::Size(1280, 1024);
280 VideoCaptureParams capture_params
;
281 capture_params
.requested_format
.frame_size
= resolution
;
282 capture_params
.requested_format
.frame_rate
= 30;
283 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
284 capture_params
.allow_resolution_change
= false;
285 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
286 device
->StopAndDeAllocate();
289 // Finally, do a device start and wait for it to finish.
290 VideoCaptureParams capture_params
;
291 capture_params
.requested_format
.frame_size
.SetSize(320, 240);
292 capture_params
.requested_format
.frame_rate
= 30;
293 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
294 capture_params
.allow_resolution_change
= false;
296 ResetWithNewClient();
297 scoped_ptr
<VideoCaptureDevice
> device(
298 VideoCaptureDevice::Create(names_
.front()));
300 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
301 WaitForCapturedFrame();
302 device
->StopAndDeAllocate();
304 EXPECT_EQ(last_format().frame_size
.width(), 320);
305 EXPECT_EQ(last_format().frame_size
.height(), 240);
308 TEST_F(VideoCaptureDeviceTest
, DeAllocateCameraWhileRunning
) {
309 VideoCaptureDevice::GetDeviceNames(&names_
);
310 if (!names_
.size()) {
311 DVLOG(1) << "No camera available. Exiting test.";
314 scoped_ptr
<VideoCaptureDevice
> device(
315 VideoCaptureDevice::Create(names_
.front()));
318 EXPECT_CALL(*client_
, OnErr())
321 VideoCaptureParams capture_params
;
322 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
323 capture_params
.requested_format
.frame_rate
= 30;
324 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
325 capture_params
.allow_resolution_change
= false;
326 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
327 // Get captured video frames.
328 WaitForCapturedFrame();
329 EXPECT_EQ(last_format().frame_size
.width(), 640);
330 EXPECT_EQ(last_format().frame_size
.height(), 480);
331 EXPECT_EQ(last_format().frame_rate
, 30);
332 device
->StopAndDeAllocate();
335 TEST_F(VideoCaptureDeviceTest
, FakeCapture
) {
336 VideoCaptureDevice::Names names
;
338 video_capture_device_factory_
->GetDeviceNames(&names
);
340 ASSERT_GT(static_cast<int>(names
.size()), 0);
342 scoped_ptr
<VideoCaptureDevice
> device(
343 video_capture_device_factory_
->Create(names
.front()));
346 EXPECT_CALL(*client_
, OnErr())
349 VideoCaptureParams capture_params
;
350 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
351 capture_params
.requested_format
.frame_rate
= 30;
352 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
353 capture_params
.allow_resolution_change
= false;
354 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
355 WaitForCapturedFrame();
356 EXPECT_EQ(last_format().frame_size
.width(), 640);
357 EXPECT_EQ(last_format().frame_size
.height(), 480);
358 EXPECT_EQ(last_format().frame_rate
, 30);
359 device
->StopAndDeAllocate();
362 // Start the camera in 720p to capture MJPEG instead of a raw format.
363 TEST_F(VideoCaptureDeviceTest
, MAYBE_CaptureMjpeg
) {
364 scoped_ptr
<VideoCaptureDevice::Name
> name
=
365 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MJPEG
);
367 DVLOG(1) << "No camera supports MJPEG format. Exiting test.";
370 scoped_ptr
<VideoCaptureDevice
> device(VideoCaptureDevice::Create(*name
));
373 EXPECT_CALL(*client_
, OnErr())
376 VideoCaptureParams capture_params
;
377 capture_params
.requested_format
.frame_size
.SetSize(1280, 720);
378 capture_params
.requested_format
.frame_rate
= 30;
379 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_MJPEG
;
380 capture_params
.allow_resolution_change
= false;
381 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
382 // Get captured video frames.
383 WaitForCapturedFrame();
384 // Verify we get MJPEG from the device. Not all devices can capture 1280x720
385 // @ 30 fps, so we don't care about the exact resolution we get.
386 EXPECT_EQ(last_format().pixel_format
, PIXEL_FORMAT_MJPEG
);
387 device
->StopAndDeAllocate();
390 TEST_F(VideoCaptureDeviceTest
, GetDeviceSupportedFormats
) {
391 // Use PIXEL_FORMAT_MAX to iterate all device names for testing
392 // GetDeviceSupportedFormats().
393 scoped_ptr
<VideoCaptureDevice::Name
> name
=
394 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX
);
395 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here
396 // since we cannot forecast the hardware capabilities.
400 TEST_F(VideoCaptureDeviceTest
, FakeCaptureVariableResolution
) {
401 VideoCaptureDevice::Names names
;
403 video_capture_device_factory_
->GetDeviceNames(&names
);
404 VideoCaptureParams capture_params
;
405 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
406 capture_params
.requested_format
.frame_rate
= 30;
407 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
408 capture_params
.allow_resolution_change
= true;
410 ASSERT_GT(static_cast<int>(names
.size()), 0);
412 scoped_ptr
<VideoCaptureDevice
> device(
413 video_capture_device_factory_
->Create(names
.front()));
416 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
417 VideoCaptureFormats formats
;
418 video_capture_device_factory_
->GetDeviceSupportedFormats(names
.front(),
420 static_cast<FakeVideoCaptureDevice
*>(device
.get())->
421 PopulateVariableFormatsRoster(formats
);
423 EXPECT_CALL(*client_
, OnErr())
425 int action_count
= 200;
427 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
429 // We set TimeWait to 200 action timeouts and this should be enough for at
430 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
431 for (int i
= 0; i
< action_count
; ++i
) {
432 WaitForCapturedFrame();
434 device
->StopAndDeAllocate();
437 TEST_F(VideoCaptureDeviceTest
, FakeGetDeviceSupportedFormats
) {
438 VideoCaptureDevice::Names names
;
439 video_capture_device_factory_
->GetDeviceNames(&names
);
441 VideoCaptureFormats supported_formats
;
442 VideoCaptureDevice::Names::iterator names_iterator
;
444 for (names_iterator
= names
.begin(); names_iterator
!= names
.end();
446 video_capture_device_factory_
->GetDeviceSupportedFormats(
447 *names_iterator
, &supported_formats
);
448 EXPECT_EQ(supported_formats
.size(), 3u);
449 EXPECT_EQ(supported_formats
[0].frame_size
.width(), 320);
450 EXPECT_EQ(supported_formats
[0].frame_size
.height(), 240);
451 EXPECT_EQ(supported_formats
[0].pixel_format
, media::PIXEL_FORMAT_I420
);
452 EXPECT_GE(supported_formats
[0].frame_rate
, 20);
453 EXPECT_EQ(supported_formats
[1].frame_size
.width(), 640);
454 EXPECT_EQ(supported_formats
[1].frame_size
.height(), 480);
455 EXPECT_EQ(supported_formats
[1].pixel_format
, media::PIXEL_FORMAT_I420
);
456 EXPECT_GE(supported_formats
[1].frame_rate
, 20);
457 EXPECT_EQ(supported_formats
[2].frame_size
.width(), 1280);
458 EXPECT_EQ(supported_formats
[2].frame_size
.height(), 720);
459 EXPECT_EQ(supported_formats
[2].pixel_format
, media::PIXEL_FORMAT_I420
);
460 EXPECT_GE(supported_formats
[2].frame_rate
, 20);
464 }; // namespace media