summaryrefslogtreecommitdiff
path: root/chromium/media/video/capture/video_capture_device_unittest.cc
blob: e39c59b0541c76db88513d72b01d51e23e0e274c (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// 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/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_WIN)
#include "base/win/scoped_com_initializer.h"
#include "media/video/capture/win/video_capture_device_mf_win.h"
#endif

#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#include "media/video/capture/android/video_capture_device_android.h"
#endif

#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_CaptureMjpeg DISABLED_CaptureMjpeg
#elif defined(OS_WIN)
#define MAYBE_AllocateBadSize AllocateBadSize
// Windows currently uses DirectShow to convert from MJPEG and a raw format is
// always delivered.
#define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
#elif defined(OS_ANDROID)
// TODO(wjia): enable those tests on Android.
// On Android, native camera (JAVA) delivers frames on UI thread which is the
// main thread for tests. This results in no frame received by
// VideoCaptureAndroid.
#define CaptureVGA DISABLED_CaptureVGA
#define Capture720p DISABLED_Capture720p
#define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
#define ReAllocateCamera DISABLED_ReAllocateCamera
#define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning
#define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning
#define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg
#else
#define MAYBE_AllocateBadSize AllocateBadSize
#define MAYBE_CaptureMjpeg CaptureMjpeg
#endif

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
using ::testing::AtLeast;
using ::testing::SaveArg;

namespace media {

class MockFrameObserver : public media::VideoCaptureDevice::EventHandler {
 public:
  MOCK_METHOD0(ReserveOutputBuffer, scoped_refptr<media::VideoFrame>());
  MOCK_METHOD0(OnErr, void());
  MOCK_METHOD1(OnFrameInfo, void(const VideoCaptureCapability&));
  MOCK_METHOD1(OnFrameInfoChanged, void(const VideoCaptureCapability&));

  explicit MockFrameObserver(base::WaitableEvent* wait_event)
     : wait_event_(wait_event) {}

  virtual void OnError() OVERRIDE {
    OnErr();
  }

  virtual void OnIncomingCapturedFrame(
      const uint8* data,
      int length,
      base::Time timestamp,
      int rotation,
      bool flip_vert,
      bool flip_horiz) OVERRIDE {
    wait_event_->Signal();
  }

  virtual void OnIncomingCapturedVideoFrame(
      const scoped_refptr<media::VideoFrame>& frame,
      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, base::MessageLoop::QuitClosure());
    loop_->Run();
  }

 protected:
  virtual void SetUp() {
    frame_observer_.reset(new MockFrameObserver(&wait_event_));
    loop_.reset(new base::MessageLoopForUI());
#if defined(OS_ANDROID)
    media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(
        base::android::AttachCurrentThread());
#endif
  }

  virtual void TearDown() {
  }

#if defined(OS_WIN)
  base::win::ScopedCOMInitializer initialize_com_;
#endif
  base::WaitableEvent wait_event_;
  scoped_ptr<MockFrameObserver> frame_observer_;
  VideoCaptureDevice::Names names_;
  scoped_ptr<base::MessageLoop> loop_;
};

TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) {
#if defined(OS_WIN)
  VideoCaptureDevice::Name::CaptureApiType api_type =
      VideoCaptureDeviceMFWin::PlatformSupported()
          ? VideoCaptureDevice::Name::MEDIA_FOUNDATION
          : VideoCaptureDevice::Name::DIRECT_SHOW;
  VideoCaptureDevice::Name device_name("jibberish", "jibberish", api_type);
#else
  VideoCaptureDevice::Name device_name("jibberish", "jibberish");
#endif
  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);
  DVLOG(1) << names_.front().id();
  // Get info about the new resolution.
  VideoCaptureCapability rx_capability;
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .Times(1).WillOnce(SaveArg<0>(&rx_capability));

  EXPECT_CALL(*frame_observer_, OnErr())
      .Times(0);

  VideoCaptureCapability capture_format(640,
                                        480,
                                        30,
                                        VideoCaptureCapability::kI420,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, frame_observer_.get());
  device->Start();
  // Get captured video frames.
  PostQuitTask();
  EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
  EXPECT_EQ(rx_capability.width, 640);
  EXPECT_EQ(rx_capability.height, 480);
  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);

  VideoCaptureCapability capture_format(1280,
                                        720,
                                        30,
                                        VideoCaptureCapability::kI420,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, 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.
  VideoCaptureCapability rx_capability;
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .Times(AtLeast(1)).WillOnce(SaveArg<0>(&rx_capability));

  VideoCaptureCapability capture_format(637,
                                        472,
                                        35,
                                        VideoCaptureCapability::kI420,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, frame_observer_.get());
  device->DeAllocate();
  EXPECT_EQ(rx_capability.width, 640);
  EXPECT_EQ(rx_capability.height, 480);
}

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.
  VideoCaptureCapability rx_capability_1;
  VideoCaptureCapability rx_capability_2;
  VideoCaptureCapability capture_format_1(640,
                                          480,
                                          30,
                                          VideoCaptureCapability::kI420,
                                          0,
                                          false,
                                          ConstantResolutionVideoCaptureDevice);
  VideoCaptureCapability capture_format_2(1280,
                                          1024,
                                          30,
                                          VideoCaptureCapability::kI420,
                                          0,
                                          false,
                                          ConstantResolutionVideoCaptureDevice);
  VideoCaptureCapability capture_format_3(320,
                                          240,
                                          30,
                                          VideoCaptureCapability::kI420,
                                          0,
                                          false,
                                          ConstantResolutionVideoCaptureDevice);

  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .WillOnce(SaveArg<0>(&rx_capability_1));
  device->Allocate(capture_format_1, frame_observer_.get());
  device->Start();
  // Nothing shall happen.
  device->Allocate(capture_format_2, frame_observer_.get());
  device->DeAllocate();
  // Allocate new size 320, 240
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .WillOnce(SaveArg<0>(&rx_capability_2));
  device->Allocate(capture_format_3, frame_observer_.get());

  device->Start();
  // Get captured video frames.
  PostQuitTask();
  EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
  EXPECT_EQ(rx_capability_1.width, 640);
  EXPECT_EQ(rx_capability_1.height, 480);
  EXPECT_EQ(rx_capability_2.width, 320);
  EXPECT_EQ(rx_capability_2.height, 240);
  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.
  VideoCaptureCapability rx_capability;
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .WillOnce(SaveArg<0>(&rx_capability));

  VideoCaptureCapability capture_format(640,
                                        480,
                                        30,
                                        VideoCaptureCapability::kI420,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, frame_observer_.get());

  device->Start();
  // Get captured video frames.
  PostQuitTask();
  EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
  EXPECT_EQ(rx_capability.width, 640);
  EXPECT_EQ(rx_capability.height, 480);
  EXPECT_EQ(rx_capability.frame_rate, 30);
  device->DeAllocate();
}

TEST_F(VideoCaptureDeviceTest, FakeCapture) {
  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.
  VideoCaptureCapability rx_capability;
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .Times(1).WillOnce(SaveArg<0>(&rx_capability));

  EXPECT_CALL(*frame_observer_, OnErr())
      .Times(0);

  VideoCaptureCapability capture_format(640,
                                        480,
                                        30,
                                        VideoCaptureCapability::kI420,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, frame_observer_.get());

  device->Start();
  EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
  EXPECT_EQ(rx_capability.width, 640);
  EXPECT_EQ(rx_capability.height, 480);
  EXPECT_EQ(rx_capability.frame_rate, 30);
  device->Stop();
  device->DeAllocate();
}

// Start the camera in 720p to capture MJPEG instead of a raw format.
TEST_F(VideoCaptureDeviceTest, MAYBE_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.
  VideoCaptureCapability rx_capability;
  EXPECT_CALL(*frame_observer_, OnFrameInfo(_))
      .WillOnce(SaveArg<0>(&rx_capability));

  VideoCaptureCapability capture_format(1280,
                                        720,
                                        30,
                                        VideoCaptureCapability::kMJPEG,
                                        0,
                                        false,
                                        ConstantResolutionVideoCaptureDevice);
  device->Allocate(capture_format, frame_observer_.get());

  device->Start();
  // Get captured video frames.
  PostQuitTask();
  EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout()));
  EXPECT_EQ(rx_capability.color, VideoCaptureCapability::kMJPEG);
  device->DeAllocate();
}

TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) {
  VideoCaptureDevice::Names names;

  FakeVideoCaptureDevice::GetDeviceNames(&names);
  media::VideoCaptureCapability capture_format;
  capture_format.width = 640;
  capture_format.height = 480;
  capture_format.frame_rate = 30;
  capture_format.frame_size_type = media::VariableResolutionVideoCaptureDevice;

  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(_))
      .Times(1);

  EXPECT_CALL(*frame_observer_, OnErr())
      .Times(0);

  device->Allocate(capture_format, frame_observer_.get());

  // The amount of times the OnFrameInfoChanged gets called depends on how often
  // FakeDevice is supposed to change and what is its actual frame rate.
  // We set TimeWait to 200 action timeouts and this should be enough for at
  // least action_count/kFakeCaptureCapabilityChangePeriod calls.
  int action_count = 200;
  EXPECT_CALL(*frame_observer_, OnFrameInfoChanged(_))
      .Times(AtLeast(action_count / 30));
  device->Start();
  for (int i = 0; i < action_count; ++i) {
    EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_timeout()));
  }
  device->Stop();
  device->DeAllocate();
}

};  // namespace media