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