blob: 02a88b1dc178afcb37e2ed2f39814c524445fcdc [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/capturemanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
tfarina5237aaf2015-11-10 23:44:30 -080030#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000031#include "webrtc/base/gunit.h"
32#include "webrtc/base/sigslot.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/fakevideocapturer.h"
34#include "webrtc/media/base/fakevideorenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36const int kMsCallbackWait = 50;
37
38const int kFps = 30;
39const cricket::VideoFormatPod kCameraFormats[] = {
40 {640, 480, cricket::VideoFormat::FpsToInterval(kFps), cricket::FOURCC_I420},
41 {320, 240, cricket::VideoFormat::FpsToInterval(kFps), cricket::FOURCC_I420}
42};
43
44class CaptureManagerTest : public ::testing::Test, public sigslot::has_slots<> {
45 public:
46 CaptureManagerTest()
47 : capture_manager_(),
48 callback_count_(0),
49 format_vga_(kCameraFormats[0]),
50 format_qvga_(kCameraFormats[1]) {
51 }
52 virtual void SetUp() {
53 PopulateSupportedFormats();
54 capture_state_ = cricket::CS_STOPPED;
55 capture_manager_.SignalCapturerStateChange.connect(
56 this,
57 &CaptureManagerTest::OnCapturerStateChange);
58 }
59 void PopulateSupportedFormats() {
60 std::vector<cricket::VideoFormat> formats;
kjellandera96e2d72016-02-04 23:52:28 -080061 for (uint32_t i = 0; i < arraysize(kCameraFormats); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 formats.push_back(cricket::VideoFormat(kCameraFormats[i]));
63 }
64 video_capturer_.ResetSupportedFormats(formats);
65 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 int NumFramesRendered() { return video_renderer_.num_rendered_frames(); }
67 bool WasRenderedResolution(cricket::VideoFormat format) {
68 return format.width == video_renderer_.width() &&
69 format.height == video_renderer_.height();
70 }
71 cricket::CaptureState capture_state() { return capture_state_; }
72 int callback_count() { return callback_count_; }
73 void OnCapturerStateChange(cricket::VideoCapturer* capturer,
74 cricket::CaptureState capture_state) {
75 capture_state_ = capture_state;
76 ++callback_count_;
77 }
78
79 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 cricket::FakeVideoCapturer video_capturer_;
81 cricket::FakeVideoRenderer video_renderer_;
82
83 cricket::CaptureManager capture_manager_;
84
85 cricket::CaptureState capture_state_;
86 int callback_count_;
87 cricket::VideoFormat format_vga_;
88 cricket::VideoFormat format_qvga_;
89};
90
91// Incorrect use cases.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092TEST_F(CaptureManagerTest, InvalidAddingRemoving) {
93 EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
94 cricket::VideoFormat()));
95 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
96 format_vga_));
97 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
98 EXPECT_EQ(1, callback_count());
nissee73afba2016-01-28 04:47:08 -080099 // NULL argument currently allowed, and does nothing.
100 capture_manager_.AddVideoSink(&video_capturer_, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
102}
103
104// Valid use cases
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105TEST_F(CaptureManagerTest, KeepFirstResolutionHigh) {
106 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
107 format_vga_));
108 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
109 EXPECT_EQ(1, callback_count());
nissee73afba2016-01-28 04:47:08 -0800110 capture_manager_.AddVideoSink(&video_capturer_, &video_renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 EXPECT_TRUE(video_capturer_.CaptureFrame());
112 EXPECT_EQ(1, NumFramesRendered());
113 // Renderer should be fed frames with the resolution of format_vga_.
114 EXPECT_TRUE(WasRenderedResolution(format_vga_));
115
116 // Start again with one more format.
117 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
118 format_qvga_));
119 // Existing renderers should be fed frames with the resolution of format_vga_.
120 EXPECT_TRUE(video_capturer_.CaptureFrame());
121 EXPECT_TRUE(WasRenderedResolution(format_vga_));
122 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
123 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
124 format_qvga_));
125 EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
126 format_vga_));
127 EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
128 format_qvga_));
129}
130
131// Should pick the lowest resolution as the highest resolution is not chosen
132// until after capturing has started. This ensures that no particular resolution
133// is favored over others.
134TEST_F(CaptureManagerTest, KeepFirstResolutionLow) {
135 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
136 format_qvga_));
137 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
138 format_vga_));
nissee73afba2016-01-28 04:47:08 -0800139 capture_manager_.AddVideoSink(&video_capturer_, &video_renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
141 EXPECT_TRUE(video_capturer_.CaptureFrame());
142 EXPECT_EQ(1, NumFramesRendered());
143 EXPECT_TRUE(WasRenderedResolution(format_qvga_));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000144 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
145 format_qvga_));
146 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
147 format_vga_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148}
149
150// Ensure that the reference counting is working when multiple start and
151// multiple stop calls are made.
152TEST_F(CaptureManagerTest, MultipleStartStops) {
153 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
154 format_vga_));
155 // Add video capturer but with different format.
156 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
157 format_qvga_));
158 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
159 EXPECT_EQ(1, callback_count());
nissee73afba2016-01-28 04:47:08 -0800160 capture_manager_.AddVideoSink(&video_capturer_, &video_renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 // Ensure that a frame can be captured when two start calls have been made.
162 EXPECT_TRUE(video_capturer_.CaptureFrame());
163 EXPECT_EQ(1, NumFramesRendered());
164
165 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
166 // Video should still render since there has been two start calls but only
167 // one stop call.
168 EXPECT_TRUE(video_capturer_.CaptureFrame());
169 EXPECT_EQ(2, NumFramesRendered());
170
171 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
172 format_qvga_));
173 EXPECT_EQ_WAIT(cricket::CS_STOPPED, capture_state(), kMsCallbackWait);
174 EXPECT_EQ(2, callback_count());
175 // Last stop call should fail as it is one more than the number of start
176 // calls.
177 EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
178 format_vga_));
179}
180
181TEST_F(CaptureManagerTest, TestForceRestart) {
182 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
183 format_qvga_));
nissee73afba2016-01-28 04:47:08 -0800184 capture_manager_.AddVideoSink(&video_capturer_, &video_renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
186 EXPECT_TRUE(video_capturer_.CaptureFrame());
187 EXPECT_EQ(1, NumFramesRendered());
188 EXPECT_TRUE(WasRenderedResolution(format_qvga_));
189 // Now restart with vga.
190 EXPECT_TRUE(capture_manager_.RestartVideoCapture(
191 &video_capturer_, format_qvga_, format_vga_,
192 cricket::CaptureManager::kForceRestart));
193 EXPECT_TRUE(video_capturer_.CaptureFrame());
194 EXPECT_EQ(2, NumFramesRendered());
195 EXPECT_TRUE(WasRenderedResolution(format_vga_));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000196 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
197 format_vga_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198}
199
200TEST_F(CaptureManagerTest, TestRequestRestart) {
201 EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
202 format_vga_));
nissee73afba2016-01-28 04:47:08 -0800203 capture_manager_.AddVideoSink(&video_capturer_, &video_renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
205 EXPECT_TRUE(video_capturer_.CaptureFrame());
206 EXPECT_EQ(1, NumFramesRendered());
207 EXPECT_TRUE(WasRenderedResolution(format_vga_));
208 // Now request restart with qvga.
209 EXPECT_TRUE(capture_manager_.RestartVideoCapture(
210 &video_capturer_, format_vga_, format_qvga_,
211 cricket::CaptureManager::kRequestRestart));
212 EXPECT_TRUE(video_capturer_.CaptureFrame());
213 EXPECT_EQ(2, NumFramesRendered());
214 EXPECT_TRUE(WasRenderedResolution(format_vga_));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000215 EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
216 format_qvga_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217}