1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "media/video/capture/fake_video_capture_device.h"
#include "media/video/capture/video_capture_device.h"
#include "media/video/capture/video_capture_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_MACOSX)
// Mac/QTKit will always give you the size you ask for and this case will fail.
#define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
// We will always get ARGB from the Mac/QTKit implementation.
#define MAYBE_MJPEG DISABLED_CaptureMjpeg
#elif defined(OS_WINDOWS)
#define MAYBE_AllocateBadSize AllocateBadSizei
// Windows currently uses DirectShow to convert from MJPEG and a raw format is
// always delivered.
#define MAYBE_MJPEG DISABLED_CaptureMjpeg
#else
#define MAYBE_AllocateBadSize AllocateBadSize
#define MAYBE_MJPEG CaptureMjpeg
#endif
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
using ::testing::AtLeast;
namespace media {
class MockFrameObserver : public media::VideoCaptureDevice::EventHandler {
public:
MOCK_METHOD0(OnErr, void());
MOCK_METHOD4(OnFrameInfo, void(int width, int height, int frame_rate,
VideoCaptureCapability::Format format));
explicit MockFrameObserver(base::WaitableEvent* wait_event)
: wait_event_(wait_event) {}
virtual void OnError() OVERRIDE {
OnErr();
}
virtual void OnFrameInfo(
const VideoCaptureCapability& info) OVERRIDE {
OnFrameInfo(info.width, info.height, info.frame_rate, info.color);
}
virtual void OnIncomingCapturedFrame(const uint8* data, int length,
base::Time timestamp) OVERRIDE {
wait_event_->Signal();
}
private:
base::WaitableEvent* wait_event_;
};
class VideoCaptureDeviceTest : public testing::Test {
public:
VideoCaptureDeviceTest(): wait_event_(false, false) { }
void PostQuitTask() {
loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
loop_->Run();
}
protected:
virtual void SetUp() {
frame_observer_.reset(new MockFrameObserver(&wait_event_));
loop_.reset(new MessageLoopForUI());
}
virtual void TearDown() {
}
base::WaitableEvent wait_event_;
scoped_ptr<MockFrameObserver> frame_observer_;
VideoCaptureDevice::Names names_;
scoped_ptr<MessageLoop> loop_;
};
TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) {
VideoCaptureDevice::Name device_name;
device_name.device_name = "jibberish";
device_name.unique_id = "jibberish";
VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name);
EXPECT_TRUE(device == NULL);
}
TEST_F(VideoCaptureDeviceTest, CaptureVGA) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_FALSE(device.get() == NULL);
// Get info about the new resolution.
EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30, _))
.Times(1);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
device->Allocate(640, 480, 30, frame_observer_.get());
device->Start();
// Get captured video frames.
PostQuitTask();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->Stop();
device->DeAllocate();
}
TEST_F(VideoCaptureDeviceTest, Capture720p) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_FALSE(device.get() == NULL);
// Get info about the new resolution.
// We don't care about the resulting resolution or frame rate as it might
// be different from one machine to the next.
EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _, _))
.Times(1);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
device->Allocate(1280, 720, 30, frame_observer_.get());
device->Start();
// Get captured video frames.
PostQuitTask();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->Stop();
device->DeAllocate();
}
TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_TRUE(device.get() != NULL);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
// get info about the new resolution
EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480 , _, _))
.Times(AtLeast(1));
device->Allocate(637, 472, 35, frame_observer_.get());
device->DeAllocate();
}
TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_TRUE(device.get() != NULL);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
// get info about the new resolution
EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, _, _));
EXPECT_CALL(*frame_observer_, OnFrameInfo(320, 240, _, _));
device->Allocate(640, 480, 30, frame_observer_.get());
device->Start();
// Nothing shall happen.
device->Allocate(1280, 1024, 30, frame_observer_.get());
device->DeAllocate();
// Allocate new size 320, 240
device->Allocate(320, 240, 30, frame_observer_.get());
device->Start();
// Get captured video frames.
PostQuitTask();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->Stop();
device->DeAllocate();
}
TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_TRUE(device.get() != NULL);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
// Get info about the new resolution.
EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30, _));
device->Allocate(640, 480, 30, frame_observer_.get());
device->Start();
// Get captured video frames.
PostQuitTask();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->DeAllocate();
}
TEST_F(VideoCaptureDeviceTest, TestFakeCapture) {
VideoCaptureDevice::Names names;
FakeVideoCaptureDevice::GetDeviceNames(&names);
ASSERT_GT(static_cast<int>(names.size()), 0);
scoped_ptr<VideoCaptureDevice> device(
FakeVideoCaptureDevice::Create(names.front()));
ASSERT_TRUE(device.get() != NULL);
// Get info about the new resolution.
EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30, _))
.Times(1);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
device->Allocate(640, 480, 30, frame_observer_.get());
device->Start();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->Stop();
device->DeAllocate();
}
// Start the camera in 720p to capture MJPEG instead of a raw format.
TEST_F(VideoCaptureDeviceTest, CaptureMjpeg) {
VideoCaptureDevice::GetDeviceNames(&names_);
if (!names_.size()) {
DVLOG(1) << "No camera available. Exiting test.";
return;
}
scoped_ptr<VideoCaptureDevice> device(
VideoCaptureDevice::Create(names_.front()));
ASSERT_TRUE(device.get() != NULL);
EXPECT_CALL(*frame_observer_, OnErr())
.Times(0);
// Verify we get MJPEG from the device. Not all devices can capture 1280x720
// @ 30 fps, so we don't care about the exact resolution we get.
EXPECT_CALL(*frame_observer_,
OnFrameInfo(_, _, _, VideoCaptureCapability::kMJPEG));
device->Allocate(1280, 720, 30, frame_observer_.get());
device->Start();
// Get captured video frames.
PostQuitTask();
EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
device->DeAllocate();
}
}; // namespace media
|