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/base/video_capture_types.h"
11 #include "media/video/capture/fake_video_capture_device.h"
12 #include "media/video/capture/fake_video_capture_device_factory.h"
13 #include "media/video/capture/video_capture_device.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::SaveArg
;
24 class MockClient
: public VideoCaptureDevice::Client
{
26 MOCK_METHOD2(ReserveOutputBuffer
,
27 scoped_refptr
<Buffer
>(VideoFrame::Format format
,
28 const gfx::Size
& dimensions
));
29 MOCK_METHOD3(OnIncomingCapturedVideoFrame
,
30 void(const scoped_refptr
<Buffer
>& buffer
,
31 const scoped_refptr
<media::VideoFrame
>& frame
,
32 const base::TimeTicks
& timestamp
));
33 MOCK_METHOD1(OnError
, void(const std::string
& reason
));
35 explicit MockClient(base::Callback
<void(const VideoCaptureFormat
&)> frame_cb
)
36 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb
) {}
38 void OnIncomingCapturedData(const uint8
* data
,
40 const VideoCaptureFormat
& format
,
42 const base::TimeTicks
& timestamp
) override
{
43 main_thread_
->PostTask(FROM_HERE
, base::Bind(frame_cb_
, format
));
47 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
48 base::Callback
<void(const VideoCaptureFormat
&)> frame_cb_
;
51 class DeviceEnumerationListener
:
52 public base::RefCounted
<DeviceEnumerationListener
> {
54 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr
,
55 void(VideoCaptureDevice::Names
* names
));
56 // GMock doesn't support move-only arguments, so we use this forward method.
57 void OnEnumeratedDevicesCallback(
58 scoped_ptr
<VideoCaptureDevice::Names
> names
) {
59 OnEnumeratedDevicesCallbackPtr(names
.release());
63 friend class base::RefCounted
<DeviceEnumerationListener
>;
64 virtual ~DeviceEnumerationListener() {}
69 class FakeVideoCaptureDeviceTest
: public testing::Test
{
71 typedef VideoCaptureDevice::Client Client
;
73 FakeVideoCaptureDeviceTest()
74 : loop_(new base::MessageLoop()),
75 client_(new MockClient(
76 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured
,
77 base::Unretained(this)))),
78 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {
79 device_enumeration_listener_
= new DeviceEnumerationListener();
82 void SetUp() override
{
83 EXPECT_CALL(*client_
, ReserveOutputBuffer(_
,_
)).Times(0);
84 EXPECT_CALL(*client_
, OnIncomingCapturedVideoFrame(_
,_
,_
)).Times(0);
87 void OnFrameCaptured(const VideoCaptureFormat
& format
) {
88 last_format_
= format
;
89 run_loop_
->QuitClosure().Run();
92 void WaitForCapturedFrame() {
93 run_loop_
.reset(new base::RunLoop());
97 scoped_ptr
<VideoCaptureDevice::Names
> EnumerateDevices() {
98 VideoCaptureDevice::Names
* names
;
99 EXPECT_CALL(*device_enumeration_listener_
.get(),
100 OnEnumeratedDevicesCallbackPtr(_
)).WillOnce(SaveArg
<0>(&names
));
102 video_capture_device_factory_
->EnumerateDeviceNames(
103 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback
,
104 device_enumeration_listener_
));
105 base::MessageLoop::current()->RunUntilIdle();
106 return scoped_ptr
<VideoCaptureDevice::Names
>(names
);
109 const VideoCaptureFormat
& last_format() const { return last_format_
; }
111 VideoCaptureDevice::Names names_
;
112 scoped_ptr
<base::MessageLoop
> loop_
;
113 scoped_ptr
<base::RunLoop
> run_loop_
;
114 scoped_ptr
<MockClient
> client_
;
115 scoped_refptr
<DeviceEnumerationListener
> device_enumeration_listener_
;
116 VideoCaptureFormat last_format_
;
117 scoped_ptr
<VideoCaptureDeviceFactory
> video_capture_device_factory_
;
120 TEST_F(FakeVideoCaptureDeviceTest
, Capture
) {
121 scoped_ptr
<VideoCaptureDevice::Names
> names(EnumerateDevices());
123 ASSERT_FALSE(names
->empty());
125 scoped_ptr
<VideoCaptureDevice
> device(
126 video_capture_device_factory_
->Create(names
->front()));
129 EXPECT_CALL(*client_
, OnError(_
)).Times(0);
131 VideoCaptureParams capture_params
;
132 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
133 capture_params
.requested_format
.frame_rate
= 30;
134 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
135 device
->AllocateAndStart(capture_params
, client_
.Pass());
136 WaitForCapturedFrame();
137 EXPECT_EQ(last_format().frame_size
.width(), 640);
138 EXPECT_EQ(last_format().frame_size
.height(), 480);
139 EXPECT_EQ(last_format().frame_rate
, 30);
140 device
->StopAndDeAllocate();
143 TEST_F(FakeVideoCaptureDeviceTest
, GetDeviceSupportedFormats
) {
144 scoped_ptr
<VideoCaptureDevice::Names
> names(EnumerateDevices());
146 VideoCaptureFormats supported_formats
;
148 for (const auto& names_iterator
: *names
) {
149 video_capture_device_factory_
->GetDeviceSupportedFormats(
150 names_iterator
, &supported_formats
);
151 ASSERT_EQ(supported_formats
.size(), 4u);
152 EXPECT_EQ(supported_formats
[0].frame_size
.width(), 320);
153 EXPECT_EQ(supported_formats
[0].frame_size
.height(), 240);
154 EXPECT_EQ(supported_formats
[0].pixel_format
, PIXEL_FORMAT_I420
);
155 EXPECT_GE(supported_formats
[0].frame_rate
, 20);
156 EXPECT_EQ(supported_formats
[1].frame_size
.width(), 640);
157 EXPECT_EQ(supported_formats
[1].frame_size
.height(), 480);
158 EXPECT_EQ(supported_formats
[1].pixel_format
, PIXEL_FORMAT_I420
);
159 EXPECT_GE(supported_formats
[1].frame_rate
, 20);
160 EXPECT_EQ(supported_formats
[2].frame_size
.width(), 1280);
161 EXPECT_EQ(supported_formats
[2].frame_size
.height(), 720);
162 EXPECT_EQ(supported_formats
[2].pixel_format
, PIXEL_FORMAT_I420
);
163 EXPECT_GE(supported_formats
[2].frame_rate
, 20);
164 EXPECT_EQ(supported_formats
[3].frame_size
.width(), 1920);
165 EXPECT_EQ(supported_formats
[3].frame_size
.height(), 1080);
166 EXPECT_EQ(supported_formats
[3].pixel_format
, PIXEL_FORMAT_I420
);
167 EXPECT_GE(supported_formats
[3].frame_rate
, 20);
171 // Disabled, http://crbug.com/407061 .
172 TEST_F(FakeVideoCaptureDeviceTest
, DISABLED_CaptureVariableResolution
) {
173 scoped_ptr
<VideoCaptureDevice::Names
> names(EnumerateDevices());
175 VideoCaptureParams capture_params
;
176 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
177 capture_params
.requested_format
.frame_rate
= 30;
178 capture_params
.requested_format
.pixel_format
= PIXEL_FORMAT_I420
;
179 capture_params
.resolution_change_policy
=
180 RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT
;
182 ASSERT_FALSE(names
->empty());
184 scoped_ptr
<VideoCaptureDevice
> device(
185 video_capture_device_factory_
->Create(names
->front()));
188 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
189 VideoCaptureFormats formats
;
190 video_capture_device_factory_
->GetDeviceSupportedFormats(names
->front(),
192 static_cast<FakeVideoCaptureDevice
*>(device
.get())->
193 PopulateVariableFormatsRoster(formats
);
195 EXPECT_CALL(*client_
, OnError(_
)).Times(0);
196 int action_count
= 200;
198 device
->AllocateAndStart(capture_params
, client_
.Pass());
200 // We set TimeWait to 200 action timeouts and this should be enough for at
201 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
202 for (int i
= 0; i
< action_count
; ++i
) {
203 WaitForCapturedFrame();
205 device
->StopAndDeAllocate();
208 }; // namespace media