blob: ac072bd9051adb94e3298ef505391c3da8b9cf9b [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028#include <string>
29#include <vector>
30
wu@webrtc.org967bfff2013-09-19 05:49:50 +000031#include "talk/app/webrtc/remotevideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000032#include "talk/app/webrtc/test/fakeconstraints.h"
wu@webrtc.org967bfff2013-09-19 05:49:50 +000033#include "talk/app/webrtc/videosource.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/media/base/fakemediaengine.h"
35#include "talk/media/base/fakevideorenderer.h"
36#include "talk/media/devices/fakedevicemanager.h"
wu@webrtc.org967bfff2013-09-19 05:49:50 +000037#include "talk/media/webrtc/webrtcvideoframe.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/session/media/channelmanager.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000039#include "webrtc/base/gunit.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41using webrtc::FakeConstraints;
wu@webrtc.org967bfff2013-09-19 05:49:50 +000042using webrtc::VideoSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043using webrtc::MediaConstraintsInterface;
44using webrtc::MediaSourceInterface;
45using webrtc::ObserverInterface;
46using webrtc::VideoSourceInterface;
47
48namespace {
49
50// Max wait time for a test.
51const int kMaxWaitMs = 100;
52
53} // anonymous namespace
54
55
56// TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for
57// testing without known camera formats.
58// It keeps its own lists of cricket::VideoFormats for the unit tests in this
59// file.
60class TestVideoCapturer : public cricket::FakeVideoCapturer {
61 public:
62 TestVideoCapturer() : test_without_formats_(false) {
63 std::vector<cricket::VideoFormat> formats;
64 formats.push_back(cricket::VideoFormat(1280, 720,
65 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
66 formats.push_back(cricket::VideoFormat(640, 480,
67 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
68 formats.push_back(cricket::VideoFormat(640, 400,
69 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
70 formats.push_back(cricket::VideoFormat(320, 240,
71 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
72 formats.push_back(cricket::VideoFormat(352, 288,
73 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
74 ResetSupportedFormats(formats);
75 }
76
77 // This function is used for resetting the supported capture formats and
78 // simulating a cricket::VideoCapturer implementation that don't support
79 // capture format enumeration. This is used to simulate the current
80 // Chrome implementation.
81 void TestWithoutCameraFormats() {
82 test_without_formats_ = true;
83 std::vector<cricket::VideoFormat> formats;
84 ResetSupportedFormats(formats);
85 }
86
87 virtual cricket::CaptureState Start(
88 const cricket::VideoFormat& capture_format) {
89 if (test_without_formats_) {
90 std::vector<cricket::VideoFormat> formats;
91 formats.push_back(capture_format);
92 ResetSupportedFormats(formats);
93 }
94 return FakeVideoCapturer::Start(capture_format);
95 }
96
97 virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
98 cricket::VideoFormat* best_format) {
99 if (test_without_formats_) {
100 *best_format = desired;
101 return true;
102 }
103 return FakeVideoCapturer::GetBestCaptureFormat(desired,
104 best_format);
105 }
106
107 private:
108 bool test_without_formats_;
109};
110
111class StateObserver : public ObserverInterface {
112 public:
113 explicit StateObserver(VideoSourceInterface* source)
114 : state_(source->state()),
115 source_(source) {
116 }
117 virtual void OnChanged() {
118 state_ = source_->state();
119 }
120 MediaSourceInterface::SourceState state() const { return state_; }
121
122 private:
123 MediaSourceInterface::SourceState state_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000124 rtc::scoped_refptr<VideoSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125};
126
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000127class VideoSourceTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 protected:
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000129 VideoSourceTest()
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000130 : capturer_cleanup_(new TestVideoCapturer()),
131 capturer_(capturer_cleanup_.get()),
132 channel_manager_(new cricket::ChannelManager(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 new cricket::FakeMediaEngine(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000134 new cricket::FakeDeviceManager(), rtc::Thread::Current())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 }
136
137 void SetUp() {
138 ASSERT_TRUE(channel_manager_->Init());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 }
140
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000141 void CreateVideoSource() {
142 CreateVideoSource(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 }
144
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000145 void CreateVideoSource(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 const webrtc::MediaConstraintsInterface* constraints) {
147 // VideoSource take ownership of |capturer_|
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000148 source_ = VideoSource::Create(channel_manager_.get(),
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000149 capturer_cleanup_.release(),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000150 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000152 ASSERT_TRUE(source_.get() != NULL);
153 EXPECT_EQ(capturer_, source_->GetVideoCapturer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000155 state_observer_.reset(new StateObserver(source_));
156 source_->RegisterObserver(state_observer_.get());
157 source_->AddSink(&renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 }
159
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160 rtc::scoped_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000161 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 cricket::FakeVideoRenderer renderer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 rtc::scoped_ptr<cricket::ChannelManager> channel_manager_;
164 rtc::scoped_ptr<StateObserver> state_observer_;
165 rtc::scoped_refptr<VideoSource> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166};
167
168
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000169// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170// device have started and kEnded if it is stopped.
171// It also test that an output can receive video frames.
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000172TEST_F(VideoSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 // Initialize without constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000174 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
176 kMaxWaitMs);
177
178 ASSERT_TRUE(capturer_->CaptureFrame());
179 EXPECT_EQ(1, renderer_.num_rendered_frames());
180
181 capturer_->Stop();
182 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
183 kMaxWaitMs);
184}
185
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000186// Test that a VideoSource can be stopped and restarted.
187TEST_F(VideoSourceTest, StopRestart) {
188 // Initialize without constraints.
189 CreateVideoSource();
190 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
191 kMaxWaitMs);
192
193 ASSERT_TRUE(capturer_->CaptureFrame());
194 EXPECT_EQ(1, renderer_.num_rendered_frames());
195
196 source_->Stop();
197 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
198 kMaxWaitMs);
199
200 source_->Restart();
201 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
202 kMaxWaitMs);
203
204 ASSERT_TRUE(capturer_->CaptureFrame());
205 EXPECT_EQ(2, renderer_.num_rendered_frames());
206
207 source_->Stop();
208}
209
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000210// Test start stop with a remote VideoSource - the video source that has a
211// RemoteVideoCapturer and takes video frames from FrameInput.
212TEST_F(VideoSourceTest, StartStopRemote) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000213 source_ = VideoSource::Create(channel_manager_.get(),
214 new webrtc::RemoteVideoCapturer(),
215 NULL);
216
217 ASSERT_TRUE(source_.get() != NULL);
218 EXPECT_TRUE(NULL != source_->GetVideoCapturer());
219
220 state_observer_.reset(new StateObserver(source_));
221 source_->RegisterObserver(state_observer_.get());
222 source_->AddSink(&renderer_);
223
224 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
225 kMaxWaitMs);
226
227 cricket::VideoRenderer* frameinput = source_->FrameInput();
228 cricket::WebRtcVideoFrame test_frame;
229 frameinput->SetSize(1280, 720, 0);
230 frameinput->RenderFrame(&test_frame);
231 EXPECT_EQ(1, renderer_.num_rendered_frames());
232
233 source_->GetVideoCapturer()->Stop();
234 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
235 kMaxWaitMs);
236}
237
238// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239// fails.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000240TEST_F(VideoSourceTest, CameraFailed) {
241 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
243 kMaxWaitMs);
244
245 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
246 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
247 kMaxWaitMs);
248}
249
250// Test that the capture output is CIF if we set max constraints to CIF.
251// and the capture device support CIF.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000252TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 FakeConstraints constraints;
254 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
255 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
256 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
257
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000258 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
260 kMaxWaitMs);
261 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
262 ASSERT_TRUE(format != NULL);
263 EXPECT_EQ(352, format->width);
264 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000265 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266}
267
268// Test that the capture output is 720P if the camera support it and the
269// optional constraint is set to 720P.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000270TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 FakeConstraints constraints;
272 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
273 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
274 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
275 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
276 1280.0 / 720);
277
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000278 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
280 kMaxWaitMs);
281 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
282 ASSERT_TRUE(format != NULL);
283 EXPECT_EQ(1280, format->width);
284 EXPECT_EQ(720, format->height);
285 EXPECT_EQ(30, format->framerate());
286}
287
288// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
289// require it even if an optional constraint request a higher resolution
290// that don't have this aspect ratio.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000291TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 FakeConstraints constraints;
293 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
294 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
295 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
296 640.0 / 480);
297 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
298
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000299 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
301 kMaxWaitMs);
302 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
303 ASSERT_TRUE(format != NULL);
304 EXPECT_EQ(640, format->width);
305 EXPECT_EQ(480, format->height);
306 EXPECT_EQ(30, format->framerate());
307}
308
309
310// Test that the source state transition to kEnded if the mandatory aspect ratio
311// is set higher than supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000312TEST_F(VideoSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 FakeConstraints constraints;
314 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000315 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
317 kMaxWaitMs);
318}
319
320// Test that the source ignores an optional aspect ratio that is higher than
321// supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000322TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 FakeConstraints constraints;
324 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000325 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
327 kMaxWaitMs);
328 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
329 ASSERT_TRUE(format != NULL);
330 double aspect_ratio = static_cast<double>(format->width) / format->height;
331 EXPECT_LT(aspect_ratio, 2);
332}
333
334// Test that the source starts video with the default resolution if the
335// camera doesn't support capability enumeration and there are no constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000336TEST_F(VideoSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 capturer_->TestWithoutCameraFormats();
338
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000339 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
341 kMaxWaitMs);
342 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
343 ASSERT_TRUE(format != NULL);
344 EXPECT_EQ(640, format->width);
345 EXPECT_EQ(480, format->height);
346 EXPECT_EQ(30, format->framerate());
347}
348
349// Test that the source can start the video and get the requested aspect ratio
350// if the camera doesn't support capability enumeration and the aspect ratio is
351// set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000352TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 capturer_->TestWithoutCameraFormats();
354
355 FakeConstraints constraints;
356 double requested_aspect_ratio = 640.0 / 360;
357 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
358 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
359 requested_aspect_ratio);
360
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000361 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
363 kMaxWaitMs);
364 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
365 double aspect_ratio = static_cast<double>(format->width) / format->height;
366 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
367}
368
369// Test that the source state transitions to kEnded if an unknown mandatory
370// constraint is found.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000371TEST_F(VideoSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 FakeConstraints constraints;
373 constraints.AddMandatory("weird key", 640);
374
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000375 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
377 kMaxWaitMs);
378}
379
380// Test that the source ignores an unknown optional constraint.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000381TEST_F(VideoSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 FakeConstraints constraints;
383 constraints.AddOptional("weird key", 640);
384
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000385 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
387 kMaxWaitMs);
388}
389
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000390TEST_F(VideoSourceTest, SetValidOptionValues) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 FakeConstraints constraints;
392 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000394 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
396 bool value = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000397 EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 EXPECT_FALSE(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399}
400
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000401TEST_F(VideoSourceTest, OptionNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 FakeConstraints constraints;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000403 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 bool value;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000405 EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000408TEST_F(VideoSourceTest, MandatoryOptionOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 FakeConstraints constraints;
410 constraints.AddMandatory(
411 MediaConstraintsInterface::kNoiseReduction, true);
412 constraints.AddOptional(
413 MediaConstraintsInterface::kNoiseReduction, false);
414
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000415 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416
417 bool value = false;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000418 EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 EXPECT_TRUE(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420}
421
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000422TEST_F(VideoSourceTest, InvalidOptionKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 FakeConstraints constraints;
424 constraints.AddOptional(
425 MediaConstraintsInterface::kNoiseReduction, false);
426 constraints.AddOptional("invalidKey", false);
427
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000428 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429
430 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
431 kMaxWaitMs);
432 bool value = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000433 EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 EXPECT_FALSE(value);
435}
436
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000437TEST_F(VideoSourceTest, InvalidOptionKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 FakeConstraints constraints;
439 constraints.AddMandatory(
440 MediaConstraintsInterface::kNoiseReduction, false);
441 constraints.AddMandatory("invalidKey", false);
442
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000443 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444
445 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
446 kMaxWaitMs);
447 bool value;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000448 EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000451TEST_F(VideoSourceTest, InvalidOptionValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 FakeConstraints constraints;
453 constraints.AddOptional(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000454 MediaConstraintsInterface::kNoiseReduction, "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000456 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
458 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
459 kMaxWaitMs);
460 bool value = false;
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000461 EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462}
463
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000464TEST_F(VideoSourceTest, InvalidOptionValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 FakeConstraints constraints;
466 // Optional constraints should be ignored if the mandatory constraints fail.
467 constraints.AddOptional(
468 MediaConstraintsInterface::kNoiseReduction, "false");
469 // Values are case-sensitive and must be all lower-case.
470 constraints.AddMandatory(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000471 MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000473 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474
475 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
476 kMaxWaitMs);
477 bool value;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000478 EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000481TEST_F(VideoSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 FakeConstraints constraints;
483 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
484 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
485 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
486
487 constraints.AddMandatory(
488 MediaConstraintsInterface::kNoiseReduction, false);
489 constraints.AddOptional(
490 MediaConstraintsInterface::kNoiseReduction, true);
491
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000492 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
494 kMaxWaitMs);
495 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
496 ASSERT_TRUE(format != NULL);
497 EXPECT_EQ(352, format->width);
498 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000499 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
501 bool value = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000502 EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 EXPECT_FALSE(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
506// Tests that the source starts video with the default resolution for
507// screencast if no constraint is set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000508TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 capturer_->TestWithoutCameraFormats();
510 capturer_->SetScreencast(true);
511
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000512 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
514 kMaxWaitMs);
515 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
516 ASSERT_TRUE(format != NULL);
517 EXPECT_EQ(640, format->width);
518 EXPECT_EQ(480, format->height);
519 EXPECT_EQ(30, format->framerate());
520}
521
522// Tests that the source starts video with the max width and height set by
523// constraints for screencast.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000524TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 FakeConstraints constraints;
526 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
527 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
528
529 capturer_->TestWithoutCameraFormats();
530 capturer_->SetScreencast(true);
531
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000532 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
534 kMaxWaitMs);
535 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
536 ASSERT_TRUE(format != NULL);
537 EXPECT_EQ(480, format->width);
538 EXPECT_EQ(270, format->height);
539 EXPECT_EQ(30, format->framerate());
540}
541
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000542TEST_F(VideoSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 FakeConstraints constraints;
544 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
545
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000546 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
548 kMaxWaitMs);
549 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
550}
551
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000552TEST_F(VideoSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 FakeConstraints constraints;
554 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
555
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000556 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
558 kMaxWaitMs);
559 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
560 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000561 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562}
563