1/*
2 * libjingle
3 * Copyright 2012 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 "talk/media/base/mutedvideocapturer.h"
29
30#include "talk/base/gunit.h"
31#include "talk/media/base/videoframe.h"
32
33class MutedVideoCapturerTest : public sigslot::has_slots<>,
34                               public testing::Test {
35 protected:
36  void SetUp() {
37    frames_received_ = 0;
38    capturer_.SignalVideoFrame
39        .connect(this, &MutedVideoCapturerTest::OnVideoFrame);
40  }
41  void OnVideoFrame(cricket::VideoCapturer* capturer,
42                    const cricket::VideoFrame* muted_frame) {
43    EXPECT_EQ(capturer, &capturer_);
44    ++frames_received_;
45    received_width_ = muted_frame->GetWidth();
46    received_height_ = muted_frame->GetHeight();
47  }
48  int frames_received() { return frames_received_; }
49  bool ReceivedCorrectFormat() {
50    return (received_width_ == capturer_.GetCaptureFormat()->width) &&
51           (received_height_ == capturer_.GetCaptureFormat()->height);
52  }
53
54  cricket::MutedVideoCapturer capturer_;
55  int frames_received_;
56  cricket::VideoFormat capture_format_;
57  int received_width_;
58  int received_height_;
59};
60
61TEST_F(MutedVideoCapturerTest, GetBestCaptureFormat) {
62  cricket::VideoFormat format(640, 360, cricket::VideoFormat::FpsToInterval(30),
63                              cricket::FOURCC_I420);
64  cricket::VideoFormat best_format;
65  EXPECT_TRUE(capturer_.GetBestCaptureFormat(format, &best_format));
66  EXPECT_EQ(format.width, best_format.width);
67  EXPECT_EQ(format.height, best_format.height);
68  EXPECT_EQ(format.interval, best_format.interval);
69  EXPECT_EQ(format.fourcc, best_format.fourcc);
70}
71
72TEST_F(MutedVideoCapturerTest, IsScreencast) {
73  EXPECT_FALSE(capturer_.IsScreencast());
74}
75
76TEST_F(MutedVideoCapturerTest, GetPreferredFourccs) {
77  std::vector<uint32> fourccs;
78  EXPECT_TRUE(capturer_.GetPreferredFourccs(&fourccs));
79  EXPECT_EQ(fourccs.size(), 1u);
80  EXPECT_TRUE(capturer_.GetPreferredFourccs(&fourccs));
81  EXPECT_EQ(fourccs.size(), 1u);
82  EXPECT_EQ(fourccs[0], cricket::FOURCC_I420);
83}
84
85TEST_F(MutedVideoCapturerTest, Capturing) {
86  cricket::VideoFormat format(640, 360, cricket::VideoFormat::FpsToInterval(30),
87                              cricket::FOURCC_I420);
88  EXPECT_EQ(capturer_.Start(format), cricket::CS_RUNNING);
89  EXPECT_EQ(capturer_.Start(format), cricket::CS_RUNNING);
90  EXPECT_TRUE(capturer_.IsRunning());
91  // 100 ms should be enough to receive 3 frames at FPS of 30.
92  EXPECT_EQ_WAIT(frames_received(), 1, 100);
93  EXPECT_TRUE(ReceivedCorrectFormat());
94  capturer_.Stop();
95  EXPECT_FALSE(capturer_.IsRunning());
96}
97