1 // Copyright 2014 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/memory/scoped_ptr.h"
7 #include "base/run_loop.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/thread.h"
10 #include "media/video/capture/fake_video_capture_device.h"
11 #include "media/video/capture/fake_video_capture_device_factory.h"
12 #include "media/video/capture/video_capture_device.h"
13 #include "media/video/capture/video_capture_types.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 class MockClient
: public media::VideoCaptureDevice::Client
{
23 MOCK_METHOD2(ReserveOutputBuffer
,
24 scoped_refptr
<Buffer
>(media::VideoFrame::Format format
,
25 const gfx::Size
& dimensions
));
26 MOCK_METHOD0(OnErr
, void());
28 explicit MockClient(base::Callback
<void(const VideoCaptureFormat
&)> frame_cb
)
29 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb
) {}
31 virtual void OnError(const std::string
& error_message
) OVERRIDE
{
35 virtual void OnIncomingCapturedData(const uint8
* data
,
37 const VideoCaptureFormat
& format
,
39 base::TimeTicks timestamp
) OVERRIDE
{
40 main_thread_
->PostTask(FROM_HERE
, base::Bind(frame_cb_
, format
));
43 virtual void OnIncomingCapturedVideoFrame(
44 const scoped_refptr
<Buffer
>& buffer
,
45 const media::VideoCaptureFormat
& buffer_format
,
46 const scoped_refptr
<media::VideoFrame
>& frame
,
47 base::TimeTicks timestamp
) OVERRIDE
{
52 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
53 base::Callback
<void(const VideoCaptureFormat
&)> frame_cb_
;
56 class FakeVideoCaptureDeviceTest
: public testing::Test
{
58 typedef media::VideoCaptureDevice::Client Client
;
60 FakeVideoCaptureDeviceTest()
61 : loop_(new base::MessageLoop()),
62 client_(new MockClient(
63 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured
,
64 base::Unretained(this)))),
65 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {}
67 virtual void SetUp() {
70 void OnFrameCaptured(const VideoCaptureFormat
& format
) {
71 last_format_
= format
;
72 run_loop_
->QuitClosure().Run();
75 void WaitForCapturedFrame() {
76 run_loop_
.reset(new base::RunLoop());
80 const VideoCaptureFormat
& last_format() const { return last_format_
; }
82 VideoCaptureDevice::Names names_
;
83 scoped_ptr
<base::MessageLoop
> loop_
;
84 scoped_ptr
<base::RunLoop
> run_loop_
;
85 scoped_ptr
<MockClient
> client_
;
86 VideoCaptureFormat last_format_
;
87 scoped_ptr
<VideoCaptureDeviceFactory
> video_capture_device_factory_
;
90 TEST_F(FakeVideoCaptureDeviceTest
, Capture
) {
91 VideoCaptureDevice::Names names
;
93 video_capture_device_factory_
->GetDeviceNames(&names
);
95 ASSERT_GT(static_cast<int>(names
.size()), 0);
97 scoped_ptr
<VideoCaptureDevice
> device(
98 video_capture_device_factory_
->Create(
99 base::MessageLoopProxy::current(), names
.front()));
102 EXPECT_CALL(*client_
, OnErr()).Times(0);
104 VideoCaptureParams capture_params
;
105 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
106 capture_params
.requested_format
.frame_rate
= 30;
107 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
108 capture_params
.allow_resolution_change
= false;
109 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
110 WaitForCapturedFrame();
111 EXPECT_EQ(last_format().frame_size
.width(), 640);
112 EXPECT_EQ(last_format().frame_size
.height(), 480);
113 EXPECT_EQ(last_format().frame_rate
, 30);
114 device
->StopAndDeAllocate();
117 TEST_F(FakeVideoCaptureDeviceTest
, GetDeviceSupportedFormats
) {
118 VideoCaptureDevice::Names names
;
119 video_capture_device_factory_
->GetDeviceNames(&names
);
121 VideoCaptureFormats supported_formats
;
122 VideoCaptureDevice::Names::iterator names_iterator
;
124 for (names_iterator
= names
.begin(); names_iterator
!= names
.end();
126 video_capture_device_factory_
->GetDeviceSupportedFormats(
127 *names_iterator
, &supported_formats
);
128 EXPECT_EQ(supported_formats
.size(), 3u);
129 EXPECT_EQ(supported_formats
[0].frame_size
.width(), 320);
130 EXPECT_EQ(supported_formats
[0].frame_size
.height(), 240);
131 EXPECT_EQ(supported_formats
[0].pixel_format
, media::PIXEL_FORMAT_I420
);
132 EXPECT_GE(supported_formats
[0].frame_rate
, 20);
133 EXPECT_EQ(supported_formats
[1].frame_size
.width(), 640);
134 EXPECT_EQ(supported_formats
[1].frame_size
.height(), 480);
135 EXPECT_EQ(supported_formats
[1].pixel_format
, media::PIXEL_FORMAT_I420
);
136 EXPECT_GE(supported_formats
[1].frame_rate
, 20);
137 EXPECT_EQ(supported_formats
[2].frame_size
.width(), 1280);
138 EXPECT_EQ(supported_formats
[2].frame_size
.height(), 720);
139 EXPECT_EQ(supported_formats
[2].pixel_format
, media::PIXEL_FORMAT_I420
);
140 EXPECT_GE(supported_formats
[2].frame_rate
, 20);
144 TEST_F(FakeVideoCaptureDeviceTest
, CaptureVariableResolution
) {
145 VideoCaptureDevice::Names names
;
147 video_capture_device_factory_
->GetDeviceNames(&names
);
148 VideoCaptureParams capture_params
;
149 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
150 capture_params
.requested_format
.frame_rate
= 30;
151 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
152 capture_params
.allow_resolution_change
= true;
154 ASSERT_GT(static_cast<int>(names
.size()), 0);
156 scoped_ptr
<VideoCaptureDevice
> device(
157 video_capture_device_factory_
->Create(
158 base::MessageLoopProxy::current(), names
.front()));
161 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
162 VideoCaptureFormats formats
;
163 video_capture_device_factory_
->GetDeviceSupportedFormats(names
.front(),
165 static_cast<FakeVideoCaptureDevice
*>(device
.get())->
166 PopulateVariableFormatsRoster(formats
);
168 EXPECT_CALL(*client_
, OnErr())
170 int action_count
= 200;
172 device
->AllocateAndStart(capture_params
, client_
.PassAs
<Client
>());
174 // We set TimeWait to 200 action timeouts and this should be enough for at
175 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
176 for (int i
= 0; i
< action_count
; ++i
) {
177 WaitForCapturedFrame();
179 device
->StopAndDeAllocate();
182 }; // namespace media