1 /* 2 * libjingle 3 * Copyright 2013, 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 <string> 29 30 #include "talk/app/webrtc/remotevideocapturer.h" 31 #include "talk/media/webrtc/webrtcvideoframe.h" 32 #include "webrtc/base/gunit.h" 33 34 using cricket::CaptureState; 35 using cricket::VideoCapturer; 36 using cricket::VideoFormat; 37 using cricket::VideoFormatPod; 38 using cricket::VideoFrame; 39 40 static const int kMaxWaitMs = 1000; 41 static const VideoFormatPod kTestFormat = 42 {640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}; 43 44 class RemoteVideoCapturerTest : public testing::Test, 45 public sigslot::has_slots<> { 46 protected: 47 RemoteVideoCapturerTest() 48 : captured_frame_num_(0), 49 capture_state_(cricket::CS_STOPPED) {} 50 51 virtual void SetUp() { 52 capturer_.SignalStateChange.connect( 53 this, &RemoteVideoCapturerTest::OnStateChange); 54 capturer_.SignalVideoFrame.connect( 55 this, &RemoteVideoCapturerTest::OnVideoFrame); 56 } 57 58 ~RemoteVideoCapturerTest() { 59 capturer_.SignalStateChange.disconnect(this); 60 capturer_.SignalVideoFrame.disconnect(this); 61 } 62 63 int captured_frame_num() const { 64 return captured_frame_num_; 65 } 66 67 CaptureState capture_state() const { 68 return capture_state_; 69 } 70 71 webrtc::RemoteVideoCapturer capturer_; 72 73 private: 74 void OnStateChange(VideoCapturer* capturer, 75 CaptureState capture_state) { 76 EXPECT_EQ(&capturer_, capturer); 77 capture_state_ = capture_state; 78 } 79 80 void OnVideoFrame(VideoCapturer* capturer, const VideoFrame* frame) { 81 EXPECT_EQ(&capturer_, capturer); 82 ++captured_frame_num_; 83 } 84 85 int captured_frame_num_; 86 CaptureState capture_state_; 87 }; 88 89 TEST_F(RemoteVideoCapturerTest, StartStop) { 90 // Start 91 EXPECT_TRUE( 92 capturer_.StartCapturing(VideoFormat(kTestFormat))); 93 EXPECT_TRUE_WAIT((cricket::CS_RUNNING == capture_state()), kMaxWaitMs); 94 EXPECT_EQ(VideoFormat(kTestFormat), 95 *capturer_.GetCaptureFormat()); 96 EXPECT_TRUE(capturer_.IsRunning()); 97 98 // Stop 99 capturer_.Stop(); 100 EXPECT_TRUE_WAIT((cricket::CS_STOPPED == capture_state()), kMaxWaitMs); 101 EXPECT_TRUE(NULL == capturer_.GetCaptureFormat()); 102 } 103 104 TEST_F(RemoteVideoCapturerTest, GetPreferredFourccs) { 105 EXPECT_FALSE(capturer_.GetPreferredFourccs(NULL)); 106 107 std::vector<uint32> fourccs; 108 EXPECT_TRUE(capturer_.GetPreferredFourccs(&fourccs)); 109 EXPECT_EQ(1u, fourccs.size()); 110 EXPECT_EQ(cricket::FOURCC_I420, fourccs.at(0)); 111 } 112 113 TEST_F(RemoteVideoCapturerTest, GetBestCaptureFormat) { 114 VideoFormat desired = VideoFormat(kTestFormat); 115 EXPECT_FALSE(capturer_.GetBestCaptureFormat(desired, NULL)); 116 117 VideoFormat expected_format = VideoFormat(kTestFormat); 118 expected_format.fourcc = cricket::FOURCC_I420; 119 VideoFormat best_format; 120 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best_format)); 121 EXPECT_EQ(expected_format, best_format); 122 } 123 124 TEST_F(RemoteVideoCapturerTest, InputFrame) { 125 EXPECT_EQ(0, captured_frame_num()); 126 127 cricket::WebRtcVideoFrame test_frame; 128 capturer_.SignalVideoFrame(&capturer_, &test_frame); 129 EXPECT_EQ(1, captured_frame_num()); 130 capturer_.SignalVideoFrame(&capturer_, &test_frame); 131 EXPECT_EQ(2, captured_frame_num()); 132 } 133