webrtcvideocapturer_unittest.cc revision 5f93d0a140515e3b8cdd1b9a4c6f5871144e5dee
1/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <stdio.h>
29#include <vector>
30#include "talk/media/base/testutils.h"
31#include "talk/media/base/videocommon.h"
32#include "talk/media/webrtc/fakewebrtcvcmfactory.h"
33#include "talk/media/webrtc/webrtcvideocapturer.h"
34#include "webrtc/base/gunit.h"
35#include "webrtc/base/logging.h"
36#include "webrtc/base/stringutils.h"
37#include "webrtc/base/thread.h"
38
39using cricket::VideoFormat;
40
41static const std::string kTestDeviceName = "JuberTech FakeCam Q123";
42static const std::string kTestDeviceId = "foo://bar/baz";
43const VideoFormat kDefaultVideoFormat =
44    VideoFormat(640, 400, VideoFormat::FpsToInterval(30), cricket::FOURCC_ANY);
45
46class WebRtcVideoCapturerTest : public testing::Test {
47 public:
48  WebRtcVideoCapturerTest()
49      : factory_(new FakeWebRtcVcmFactory),
50        capturer_(new cricket::WebRtcVideoCapturer(factory_)),
51        listener_(capturer_.get()) {
52    factory_->device_info.AddDevice(kTestDeviceName, kTestDeviceId);
53    // add a VGA/I420 capability
54    webrtc::VideoCaptureCapability vga;
55    vga.width = 640;
56    vga.height = 480;
57    vga.maxFPS = 30;
58    vga.rawType = webrtc::kVideoI420;
59    factory_->device_info.AddCapability(kTestDeviceId, vga);
60  }
61
62 protected:
63  FakeWebRtcVcmFactory* factory_;  // owned by capturer_
64  rtc::scoped_ptr<cricket::WebRtcVideoCapturer> capturer_;
65  cricket::VideoCapturerListener listener_;
66};
67
68TEST_F(WebRtcVideoCapturerTest, TestNotOpened) {
69  EXPECT_EQ("", capturer_->GetId());
70  EXPECT_TRUE(capturer_->GetSupportedFormats()->empty());
71  EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL);
72  EXPECT_FALSE(capturer_->IsRunning());
73}
74
75TEST_F(WebRtcVideoCapturerTest, TestBadInit) {
76  EXPECT_FALSE(capturer_->Init(cricket::Device("bad-name", "bad-id")));
77  EXPECT_FALSE(capturer_->IsRunning());
78}
79
80TEST_F(WebRtcVideoCapturerTest, TestInit) {
81  EXPECT_TRUE(capturer_->Init(cricket::Device(kTestDeviceName, kTestDeviceId)));
82  EXPECT_EQ(kTestDeviceId, capturer_->GetId());
83  EXPECT_TRUE(NULL != capturer_->GetSupportedFormats());
84  ASSERT_EQ(1U, capturer_->GetSupportedFormats()->size());
85  EXPECT_EQ(640, (*capturer_->GetSupportedFormats())[0].width);
86  EXPECT_EQ(480, (*capturer_->GetSupportedFormats())[0].height);
87  EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL);  // not started yet
88  EXPECT_FALSE(capturer_->IsRunning());
89}
90
91TEST_F(WebRtcVideoCapturerTest, TestInitVcm) {
92  EXPECT_TRUE(capturer_->Init(factory_->Create(0,
93      reinterpret_cast<const char*>(kTestDeviceId.c_str()))));
94}
95
96TEST_F(WebRtcVideoCapturerTest, TestCapture) {
97  EXPECT_TRUE(capturer_->Init(cricket::Device(kTestDeviceName, kTestDeviceId)));
98  cricket::VideoFormat format(
99      capturer_->GetSupportedFormats()->at(0));
100  EXPECT_EQ(cricket::CS_STARTING, capturer_->Start(format));
101  EXPECT_TRUE(capturer_->IsRunning());
102  ASSERT_TRUE(capturer_->GetCaptureFormat() != NULL);
103  EXPECT_EQ(format, *capturer_->GetCaptureFormat());
104  EXPECT_EQ_WAIT(cricket::CS_RUNNING, listener_.last_capture_state(), 1000);
105  EXPECT_TRUE(factory_->modules[0]->SendFrame(640, 480));
106  EXPECT_TRUE_WAIT(listener_.frame_count() > 0, 5000);
107  EXPECT_EQ(capturer_->GetCaptureFormat()->fourcc, listener_.frame_fourcc());
108  EXPECT_EQ(640, listener_.frame_width());
109  EXPECT_EQ(480, listener_.frame_height());
110  EXPECT_EQ(cricket::CS_FAILED, capturer_->Start(format));
111  capturer_->Stop();
112  EXPECT_FALSE(capturer_->IsRunning());
113  EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL);
114}
115
116TEST_F(WebRtcVideoCapturerTest, TestCaptureVcm) {
117  EXPECT_TRUE(capturer_->Init(factory_->Create(0,
118      reinterpret_cast<const char*>(kTestDeviceId.c_str()))));
119  EXPECT_TRUE(capturer_->GetSupportedFormats()->empty());
120  VideoFormat format;
121  EXPECT_TRUE(capturer_->GetBestCaptureFormat(kDefaultVideoFormat, &format));
122  EXPECT_EQ(kDefaultVideoFormat.width, format.width);
123  EXPECT_EQ(kDefaultVideoFormat.height, format.height);
124  EXPECT_EQ(kDefaultVideoFormat.interval, format.interval);
125  EXPECT_EQ(cricket::FOURCC_I420, format.fourcc);
126  EXPECT_EQ(cricket::CS_STARTING, capturer_->Start(format));
127  EXPECT_TRUE(capturer_->IsRunning());
128  ASSERT_TRUE(capturer_->GetCaptureFormat() != NULL);
129  EXPECT_EQ(format, *capturer_->GetCaptureFormat());
130  EXPECT_EQ_WAIT(cricket::CS_RUNNING, listener_.last_capture_state(), 1000);
131  EXPECT_TRUE(factory_->modules[0]->SendFrame(640, 480));
132  EXPECT_TRUE_WAIT(listener_.frame_count() > 0, 5000);
133  EXPECT_EQ(capturer_->GetCaptureFormat()->fourcc, listener_.frame_fourcc());
134  EXPECT_EQ(640, listener_.frame_width());
135  EXPECT_EQ(480, listener_.frame_height());
136  EXPECT_EQ(cricket::CS_FAILED, capturer_->Start(format));
137  capturer_->Stop();
138  EXPECT_FALSE(capturer_->IsRunning());
139  EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL);
140}
141
142TEST_F(WebRtcVideoCapturerTest, TestCaptureWithoutInit) {
143  cricket::VideoFormat format;
144  EXPECT_EQ(cricket::CS_NO_DEVICE, capturer_->Start(format));
145  EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL);
146  EXPECT_FALSE(capturer_->IsRunning());
147}
148