1 // libjingle 2 // Copyright 2004 Google Inc. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // 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. 14 // 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. 25 26 #include <stdio.h> 27 #include <vector> 28 #include "talk/media/base/testutils.h" 29 #include "talk/media/base/videocommon.h" 30 #include "talk/media/webrtc/fakewebrtcvcmfactory.h" 31 #include "talk/media/webrtc/webrtcvideocapturer.h" 32 #include "webrtc/base/gunit.h" 33 #include "webrtc/base/logging.h" 34 #include "webrtc/base/stringutils.h" 35 #include "webrtc/base/thread.h" 36 37 using cricket::VideoFormat; 38 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); 43 44 class WebRtcVideoCapturerTest : public testing::Test { 45 public: 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; 53 vga.width = 640; 54 vga.height = 480; 55 vga.maxFPS = 30; 56 vga.rawType = webrtc::kVideoI420; 57 factory_->device_info.AddCapability(kTestDeviceId, vga); 58 } 59 60 protected: 61 FakeWebRtcVcmFactory* factory_; // owned by capturer_ 62 rtc::scoped_ptr<cricket::WebRtcVideoCapturer> capturer_; 63 cricket::VideoCapturerListener listener_; 64 }; 65 66 TEST_F(WebRtcVideoCapturerTest, TestNotOpened) { 67 EXPECT_EQ("", capturer_->GetId()); 68 EXPECT_TRUE(capturer_->GetSupportedFormats()->empty()); 69 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 70 EXPECT_FALSE(capturer_->IsRunning()); 71 } 72 73 TEST_F(WebRtcVideoCapturerTest, TestBadInit) { 74 EXPECT_FALSE(capturer_->Init(cricket::Device("bad-name", "bad-id"))); 75 EXPECT_FALSE(capturer_->IsRunning()); 76 } 77 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()); 87 } 88 89 TEST_F(WebRtcVideoCapturerTest, TestInitVcm) { 90 EXPECT_TRUE(capturer_->Init(factory_->Create(0, 91 reinterpret_cast<const char*>(kTestDeviceId.c_str())))); 92 } 93 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::CS_STARTING, 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::CS_RUNNING, listener_.last_capture_state(), 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::CS_FAILED, capturer_->Start(format)); 109 capturer_->Stop(); 110 EXPECT_FALSE(capturer_->IsRunning()); 111 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 112 } 113 114 TEST_F(WebRtcVideoCapturerTest, TestCaptureVcm) { 115 EXPECT_TRUE(capturer_->Init(factory_->Create(0, 116 reinterpret_cast<const char*>(kTestDeviceId.c_str())))); 117 EXPECT_TRUE(capturer_->GetSupportedFormats()->empty()); 118 VideoFormat format; 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::CS_STARTING, 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::CS_RUNNING, listener_.last_capture_state(), 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::CS_FAILED, capturer_->Start(format)); 135 capturer_->Stop(); 136 EXPECT_FALSE(capturer_->IsRunning()); 137 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 138 } 139 140 TEST_F(WebRtcVideoCapturerTest, TestCaptureWithoutInit) { 141 cricket::VideoFormat format; 142 EXPECT_EQ(cricket::CS_NO_DEVICE, capturer_->Start(format)); 143 EXPECT_TRUE(capturer_->GetCaptureFormat() == NULL); 144 EXPECT_FALSE(capturer_->IsRunning()); 145 } 146