Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: media/video/capture/video_capture_device_unittest.cc

Issue 235353002: Extract VideoCaptureDeviceFactory out of VideoCaptureDevice and use for File and FakeVCD. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tommi@s comments. FakeVCD PopulateFormatRoster() removed, the list comes from unit tests. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "media/video/capture/fake_video_capture_device.h" 12 #include "media/video/capture/fake_video_capture_device.h"
13 #include "media/video/capture/fake_video_capture_device_factory.h"
13 #include "media/video/capture/video_capture_device.h" 14 #include "media/video/capture/video_capture_device.h"
14 #include "media/video/capture/video_capture_types.h" 15 #include "media/video/capture/video_capture_types.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 #if defined(OS_WIN) 19 #if defined(OS_WIN)
19 #include "base/win/scoped_com_initializer.h" 20 #include "base/win/scoped_com_initializer.h"
20 #include "media/video/capture/win/video_capture_device_mf_win.h" 21 #include "media/video/capture/win/video_capture_device_mf_win.h"
21 #endif 22 #endif
22 23
23 #if defined(OS_ANDROID) 24 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h" 25 #include "base/android/jni_android.h"
25 #include "media/video/capture/android/video_capture_device_android.h" 26 #include "media/video/capture/android/video_capture_device_android.h"
26 #endif 27 #endif
27 28
28 #if defined(OS_MACOSX) 29 #if defined(OS_MACOSX)
29 // Mac/QTKit will always give you the size you ask for and this case will fail. 30 // Mac/QTKit will always give you the size you ask for and this case will fail.
30 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize 31 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
31 // We will always get ARGB from the Mac/QTKit implementation. 32 // We will always get YUYV from the Mac QTKit/AVFoundation implementations.
32 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg 33 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
33 #elif defined(OS_WIN) 34 #elif defined(OS_WIN)
34 #define MAYBE_AllocateBadSize AllocateBadSize 35 #define MAYBE_AllocateBadSize AllocateBadSize
35 // Windows currently uses DirectShow to convert from MJPEG and a raw format is 36 // Windows currently uses DirectShow to convert from MJPEG and a raw format is
36 // always delivered. 37 // always delivered.
37 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg 38 #define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
38 #elif defined(OS_ANDROID) 39 #elif defined(OS_ANDROID)
39 // TODO(wjia): enable those tests on Android. 40 // TODO(wjia): enable those tests on Android.
40 // On Android, native camera (JAVA) delivers frames on UI thread which is the 41 // On Android, native camera (JAVA) delivers frames on UI thread which is the
41 // main thread for tests. This results in no frame received by 42 // main thread for tests. This results in no frame received by
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 }; 96 };
96 97
97 class VideoCaptureDeviceTest : public testing::Test { 98 class VideoCaptureDeviceTest : public testing::Test {
98 protected: 99 protected:
99 typedef media::VideoCaptureDevice::Client Client; 100 typedef media::VideoCaptureDevice::Client Client;
100 101
101 VideoCaptureDeviceTest() 102 VideoCaptureDeviceTest()
102 : loop_(new base::MessageLoop()), 103 : loop_(new base::MessageLoop()),
103 client_( 104 client_(
104 new MockClient(base::Bind(&VideoCaptureDeviceTest::OnFrameCaptured, 105 new MockClient(base::Bind(&VideoCaptureDeviceTest::OnFrameCaptured,
105 base::Unretained(this)))) {} 106 base::Unretained(this)))),
107 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {}
106 108
107 virtual void SetUp() { 109 virtual void SetUp() {
108 #if defined(OS_ANDROID) 110 #if defined(OS_ANDROID)
109 media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice( 111 media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(
110 base::android::AttachCurrentThread()); 112 base::android::AttachCurrentThread());
111 #endif 113 #endif
112 } 114 }
113 115
114 void ResetWithNewClient() { 116 void ResetWithNewClient() {
115 client_.reset(new MockClient(base::Bind( 117 client_.reset(new MockClient(base::Bind(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 157 }
156 158
157 #if defined(OS_WIN) 159 #if defined(OS_WIN)
158 base::win::ScopedCOMInitializer initialize_com_; 160 base::win::ScopedCOMInitializer initialize_com_;
159 #endif 161 #endif
160 VideoCaptureDevice::Names names_; 162 VideoCaptureDevice::Names names_;
161 scoped_ptr<base::MessageLoop> loop_; 163 scoped_ptr<base::MessageLoop> loop_;
162 scoped_ptr<base::RunLoop> run_loop_; 164 scoped_ptr<base::RunLoop> run_loop_;
163 scoped_ptr<MockClient> client_; 165 scoped_ptr<MockClient> client_;
164 VideoCaptureFormat last_format_; 166 VideoCaptureFormat last_format_;
167 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_;
165 }; 168 };
166 169
167 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { 170 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) {
168 #if defined(OS_WIN) 171 #if defined(OS_WIN)
169 VideoCaptureDevice::Name::CaptureApiType api_type = 172 VideoCaptureDevice::Name::CaptureApiType api_type =
170 VideoCaptureDeviceMFWin::PlatformSupported() 173 VideoCaptureDeviceMFWin::PlatformSupported()
171 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION 174 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION
172 : VideoCaptureDevice::Name::DIRECT_SHOW; 175 : VideoCaptureDevice::Name::DIRECT_SHOW;
173 VideoCaptureDevice::Name device_name("jibberish", "jibberish", api_type); 176 VideoCaptureDevice::Name device_name("jibberish", "jibberish", api_type);
174 #else 177 #else
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 WaitForCapturedFrame(); 328 WaitForCapturedFrame();
326 EXPECT_EQ(last_format().frame_size.width(), 640); 329 EXPECT_EQ(last_format().frame_size.width(), 640);
327 EXPECT_EQ(last_format().frame_size.height(), 480); 330 EXPECT_EQ(last_format().frame_size.height(), 480);
328 EXPECT_EQ(last_format().frame_rate, 30); 331 EXPECT_EQ(last_format().frame_rate, 30);
329 device->StopAndDeAllocate(); 332 device->StopAndDeAllocate();
330 } 333 }
331 334
332 TEST_F(VideoCaptureDeviceTest, FakeCapture) { 335 TEST_F(VideoCaptureDeviceTest, FakeCapture) {
333 VideoCaptureDevice::Names names; 336 VideoCaptureDevice::Names names;
334 337
335 FakeVideoCaptureDevice::GetDeviceNames(&names); 338 video_capture_device_factory_->GetDeviceNames(&names);
336 339
337 ASSERT_GT(static_cast<int>(names.size()), 0); 340 ASSERT_GT(static_cast<int>(names.size()), 0);
338 341
339 scoped_ptr<VideoCaptureDevice> device( 342 scoped_ptr<VideoCaptureDevice> device(
340 FakeVideoCaptureDevice::Create(names.front())); 343 video_capture_device_factory_->Create(names.front()));
341 ASSERT_TRUE(device); 344 ASSERT_TRUE(device);
342 345
343 EXPECT_CALL(*client_, OnErr()) 346 EXPECT_CALL(*client_, OnErr())
344 .Times(0); 347 .Times(0);
345 348
346 VideoCaptureParams capture_params; 349 VideoCaptureParams capture_params;
347 capture_params.requested_format.frame_size.SetSize(640, 480); 350 capture_params.requested_format.frame_size.SetSize(640, 480);
348 capture_params.requested_format.frame_rate = 30; 351 capture_params.requested_format.frame_rate = 30;
349 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 352 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
350 capture_params.allow_resolution_change = false; 353 capture_params.allow_resolution_change = false;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 scoped_ptr<VideoCaptureDevice::Name> name = 393 scoped_ptr<VideoCaptureDevice::Name> name =
391 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX); 394 GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX);
392 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here 395 // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here
393 // since we cannot forecast the hardware capabilities. 396 // since we cannot forecast the hardware capabilities.
394 ASSERT_FALSE(name); 397 ASSERT_FALSE(name);
395 } 398 }
396 399
397 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) { 400 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) {
398 VideoCaptureDevice::Names names; 401 VideoCaptureDevice::Names names;
399 402
400 FakeVideoCaptureDevice::GetDeviceNames(&names); 403 video_capture_device_factory_->GetDeviceNames(&names);
401 VideoCaptureParams capture_params; 404 VideoCaptureParams capture_params;
402 capture_params.requested_format.frame_size.SetSize(640, 480); 405 capture_params.requested_format.frame_size.SetSize(640, 480);
403 capture_params.requested_format.frame_rate = 30; 406 capture_params.requested_format.frame_rate = 30;
404 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 407 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
405 capture_params.allow_resolution_change = true; 408 capture_params.allow_resolution_change = true;
406 409
407 ASSERT_GT(static_cast<int>(names.size()), 0); 410 ASSERT_GT(static_cast<int>(names.size()), 0);
408 411
409 scoped_ptr<VideoCaptureDevice> device( 412 scoped_ptr<VideoCaptureDevice> device(
410 FakeVideoCaptureDevice::Create(names.front())); 413 video_capture_device_factory_->Create(names.front()));
411 ASSERT_TRUE(device); 414 ASSERT_TRUE(device);
412 415
416 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
417 VideoCaptureFormats formats;
418 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(),
419 &formats);
420 static_cast<FakeVideoCaptureDevice*>(device.get())->
421 PopulateVariableFormatsRoster(formats);
422
413 EXPECT_CALL(*client_, OnErr()) 423 EXPECT_CALL(*client_, OnErr())
414 .Times(0); 424 .Times(0);
415 int action_count = 200; 425 int action_count = 200;
416 426
417 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 427 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
418 428
419 // We set TimeWait to 200 action timeouts and this should be enough for at 429 // We set TimeWait to 200 action timeouts and this should be enough for at
420 // least action_count/kFakeCaptureCapabilityChangePeriod calls. 430 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
421 for (int i = 0; i < action_count; ++i) { 431 for (int i = 0; i < action_count; ++i) {
422 WaitForCapturedFrame(); 432 WaitForCapturedFrame();
423 } 433 }
424 device->StopAndDeAllocate(); 434 device->StopAndDeAllocate();
425 } 435 }
426 436
427 TEST_F(VideoCaptureDeviceTest, FakeGetDeviceSupportedFormats) { 437 TEST_F(VideoCaptureDeviceTest, FakeGetDeviceSupportedFormats) {
428 VideoCaptureDevice::Names names; 438 VideoCaptureDevice::Names names;
429 FakeVideoCaptureDevice::GetDeviceNames(&names); 439 video_capture_device_factory_->GetDeviceNames(&names);
430 440
431 VideoCaptureFormats supported_formats; 441 VideoCaptureFormats supported_formats;
432 VideoCaptureDevice::Names::iterator names_iterator; 442 VideoCaptureDevice::Names::iterator names_iterator;
433 443
434 for (names_iterator = names.begin(); names_iterator != names.end(); 444 for (names_iterator = names.begin(); names_iterator != names.end();
435 ++names_iterator) { 445 ++names_iterator) {
436 FakeVideoCaptureDevice::GetDeviceSupportedFormats(*names_iterator, 446 video_capture_device_factory_->GetDeviceSupportedFormats(
437 &supported_formats); 447 *names_iterator, &supported_formats);
438 EXPECT_EQ(supported_formats.size(), 3u); 448 EXPECT_EQ(supported_formats.size(), 3u);
439 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); 449 EXPECT_EQ(supported_formats[0].frame_size.width(), 320);
440 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); 450 EXPECT_EQ(supported_formats[0].frame_size.height(), 240);
441 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); 451 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420);
442 EXPECT_GE(supported_formats[0].frame_rate, 20); 452 EXPECT_GE(supported_formats[0].frame_rate, 20);
443 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); 453 EXPECT_EQ(supported_formats[1].frame_size.width(), 640);
444 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); 454 EXPECT_EQ(supported_formats[1].frame_size.height(), 480);
445 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); 455 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420);
446 EXPECT_GE(supported_formats[1].frame_rate, 20); 456 EXPECT_GE(supported_formats[1].frame_rate, 20);
447 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); 457 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280);
448 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); 458 EXPECT_EQ(supported_formats[2].frame_size.height(), 720);
449 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); 459 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420);
450 EXPECT_GE(supported_formats[2].frame_rate, 20); 460 EXPECT_GE(supported_formats[2].frame_rate, 20);
451 } 461 }
452 } 462 }
453 463
454 }; // namespace media 464 }; // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698