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 39 using cricket::VideoFormat; 40 41 static const std::string kTestDeviceName = "JuberTech FakeCam Q123"; 42 static const std::string kTestDeviceId = "foo://bar/baz"; 43 const VideoFormat kDefaultVideoFormat = 44 VideoFormat(640, 400, VideoFormat::FpsToInterval(30), cricket::FOURCC_ANY); 45 46 class 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 68 TEST_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 75 TEST_F(WebRtcVideoCapturerTest, TestBadInit) { 76 EXPECT_FALSE(capturer_->Init(cricket::Device("bad-name", "bad-id"))); 77 EXPECT_FALSE(capturer_->IsRunning()); 78 } 79 80 TEST_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 91 TEST_F(WebRtcVideoCapturerTest, TestInitVcm) { 92 EXPECT_TRUE(capturer_->Init(factory_->Create(0, 93 reinterpret_cast<const char*>(kTestDeviceId.c_str())))); 94 } 95 96 TEST_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 EXPECT_EQ_WAIT(cricket::CS_STOPPED, listener_.last_capture_state(), 1000); 115 } 116 117 TEST_F(WebRtcVideoCapturerTest, TestCaptureVcm) { 118 EXPECT_TRUE(capturer_->Init(factory_->Create(0, 119 reinterpret_cast<const char*>(kTestDeviceId.c_str())))); 120 EXPECT_TRUE(capturer_->GetSupportedFormats()->empty()); 121 VideoFormat format; 122 EXPECT_TRUE(capturer_->GetBestCaptureFormat(kDefaultVideoFormat, &format)); 123 EXPECT_EQ(kDefaultVideoFormat.width, format.width); 124 EXPECT_EQ(kDefaultVideoFormat.height, format.height); 125 EXPECT_EQ(kDefaultVideoFormat.interval, format.interval); 126 EXPECT_EQ(cricket::FOURCC_I420, format.fourcc); 127 EXPECT_EQ(cricket::CS_STARTING, capturer_->Start(format)); 128 EXPECT_TRUE(capturer_->IsRunning()); 129 ASSERT_TRUE(capturer_->GetCaptureFormat() != NULL); 130 EXPECT_EQ(format, *capturer_->GetCaptureFormat()); 131 EXPECT_EQ_WAIT(cricket::CS_RUNNING, listener_.last_capture_state(), 1000); 132 EXPECT_TRUE(factory_->modules[0]->SendFrame(640, 480)); 133 EXPECT_TRUE_WAIT(listener_.frame_count() > 0, 5000); 134 EXPECT_EQ(capturer_->GetCaptureFormat()->fourcc, listener_.frame_fourcc()); 135 EXPECT_EQ(640, listener_.frame_width()); 136 EXPECT_EQ(480, listener_.frame_height()); 137 EXPECT_EQ(cricket::CS_FAILED, capturer_->Start(format)); 138 capturer_->Stop(); 139 EXPECT_FALSE(capturer_->IsRunning()); 140 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 141 } 142 143 TEST_F(WebRtcVideoCapturerTest, TestCaptureWithoutInit) { 144 cricket::VideoFormat format; 145 EXPECT_EQ(cricket::CS_NO_DEVICE, capturer_->Start(format)); 146 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 147 EXPECT_FALSE(capturer_->IsRunning()); 148 } 149