blob: cd4ca3ae3da324fa2c9412a4f148ac600bb30f07 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#include <string>
12#include <vector>
13
Henrik Kjellander15583c12016-02-10 10:53:12 +010014#include "webrtc/api/remotevideocapturer.h"
15#include "webrtc/api/test/fakeconstraints.h"
16#include "webrtc/api/videosource.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000017#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080018#include "webrtc/media/base/fakemediaengine.h"
19#include "webrtc/media/base/fakevideocapturer.h"
20#include "webrtc/media/base/fakevideorenderer.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010021#include "webrtc/media/engine/webrtcvideoframe.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010022#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
24using webrtc::FakeConstraints;
wu@webrtc.org967bfff2013-09-19 05:49:50 +000025using webrtc::VideoSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026using webrtc::MediaConstraintsInterface;
27using webrtc::MediaSourceInterface;
28using webrtc::ObserverInterface;
29using webrtc::VideoSourceInterface;
30
31namespace {
32
33// Max wait time for a test.
34const int kMaxWaitMs = 100;
35
36} // anonymous namespace
37
38
39// TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for
40// testing without known camera formats.
41// It keeps its own lists of cricket::VideoFormats for the unit tests in this
42// file.
43class TestVideoCapturer : public cricket::FakeVideoCapturer {
44 public:
Niels Möller60653ba2016-03-02 11:41:36 +010045 TestVideoCapturer(bool is_screencast)
46 : FakeVideoCapturer(is_screencast),
47 test_without_formats_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 std::vector<cricket::VideoFormat> formats;
49 formats.push_back(cricket::VideoFormat(1280, 720,
50 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
51 formats.push_back(cricket::VideoFormat(640, 480,
52 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
53 formats.push_back(cricket::VideoFormat(640, 400,
54 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
55 formats.push_back(cricket::VideoFormat(320, 240,
56 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
57 formats.push_back(cricket::VideoFormat(352, 288,
58 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
59 ResetSupportedFormats(formats);
60 }
61
62 // This function is used for resetting the supported capture formats and
63 // simulating a cricket::VideoCapturer implementation that don't support
64 // capture format enumeration. This is used to simulate the current
65 // Chrome implementation.
66 void TestWithoutCameraFormats() {
67 test_without_formats_ = true;
68 std::vector<cricket::VideoFormat> formats;
69 ResetSupportedFormats(formats);
70 }
71
72 virtual cricket::CaptureState Start(
73 const cricket::VideoFormat& capture_format) {
74 if (test_without_formats_) {
75 std::vector<cricket::VideoFormat> formats;
76 formats.push_back(capture_format);
77 ResetSupportedFormats(formats);
78 }
79 return FakeVideoCapturer::Start(capture_format);
80 }
81
82 virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
83 cricket::VideoFormat* best_format) {
84 if (test_without_formats_) {
85 *best_format = desired;
86 return true;
87 }
88 return FakeVideoCapturer::GetBestCaptureFormat(desired,
89 best_format);
90 }
91
92 private:
93 bool test_without_formats_;
94};
95
96class StateObserver : public ObserverInterface {
97 public:
98 explicit StateObserver(VideoSourceInterface* source)
99 : state_(source->state()),
100 source_(source) {
101 }
102 virtual void OnChanged() {
103 state_ = source_->state();
104 }
105 MediaSourceInterface::SourceState state() const { return state_; }
106
107 private:
108 MediaSourceInterface::SourceState state_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 rtc::scoped_refptr<VideoSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110};
111
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000112class VideoSourceTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 protected:
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000114 VideoSourceTest()
Niels Möller60653ba2016-03-02 11:41:36 +0100115 : channel_manager_(new cricket::ChannelManager(
solenbergfacbbec2015-09-24 00:41:50 -0700116 new cricket::FakeMediaEngine(), rtc::Thread::Current())) {
Niels Möller60653ba2016-03-02 11:41:36 +0100117 InitCapturer(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 }
Niels Möller60653ba2016-03-02 11:41:36 +0100119 void InitCapturer(bool is_screencast) {
120 capturer_cleanup_ = rtc::scoped_ptr<TestVideoCapturer>(
121 new TestVideoCapturer(is_screencast));
122 capturer_ = capturer_cleanup_.get();
123 }
124
125 void InitScreencast() { InitCapturer(true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 void SetUp() {
128 ASSERT_TRUE(channel_manager_->Init());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 }
130
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000131 void CreateVideoSource() {
132 CreateVideoSource(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 }
134
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000135 void CreateVideoSource(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 const webrtc::MediaConstraintsInterface* constraints) {
137 // VideoSource take ownership of |capturer_|
tommi6eca7e32015-12-15 04:27:11 -0800138 source_ =
139 VideoSource::Create(channel_manager_.get(), capturer_cleanup_.release(),
140 constraints, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000142 ASSERT_TRUE(source_.get() != NULL);
143 EXPECT_EQ(capturer_, source_->GetVideoCapturer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000145 state_observer_.reset(new StateObserver(source_));
146 source_->RegisterObserver(state_observer_.get());
147 source_->AddSink(&renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000150 rtc::scoped_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000151 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 cricket::FakeVideoRenderer renderer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 rtc::scoped_ptr<cricket::ChannelManager> channel_manager_;
154 rtc::scoped_ptr<StateObserver> state_observer_;
155 rtc::scoped_refptr<VideoSource> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156};
157
158
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000159// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160// device have started and kEnded if it is stopped.
161// It also test that an output can receive video frames.
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000162TEST_F(VideoSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 // Initialize without constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000164 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
166 kMaxWaitMs);
167
168 ASSERT_TRUE(capturer_->CaptureFrame());
169 EXPECT_EQ(1, renderer_.num_rendered_frames());
170
171 capturer_->Stop();
172 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
173 kMaxWaitMs);
174}
175
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000176// Test that a VideoSource can be stopped and restarted.
177TEST_F(VideoSourceTest, StopRestart) {
178 // Initialize without constraints.
179 CreateVideoSource();
180 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
181 kMaxWaitMs);
182
183 ASSERT_TRUE(capturer_->CaptureFrame());
184 EXPECT_EQ(1, renderer_.num_rendered_frames());
185
186 source_->Stop();
187 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
188 kMaxWaitMs);
189
190 source_->Restart();
191 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
192 kMaxWaitMs);
193
194 ASSERT_TRUE(capturer_->CaptureFrame());
195 EXPECT_EQ(2, renderer_.num_rendered_frames());
196
197 source_->Stop();
198}
199
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000200// Test start stop with a remote VideoSource - the video source that has a
201// RemoteVideoCapturer and takes video frames from FrameInput.
202TEST_F(VideoSourceTest, StartStopRemote) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000203 source_ = VideoSource::Create(channel_manager_.get(),
tommi6eca7e32015-12-15 04:27:11 -0800204 new webrtc::RemoteVideoCapturer(), NULL, true);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000205
206 ASSERT_TRUE(source_.get() != NULL);
207 EXPECT_TRUE(NULL != source_->GetVideoCapturer());
208
209 state_observer_.reset(new StateObserver(source_));
210 source_->RegisterObserver(state_observer_.get());
211 source_->AddSink(&renderer_);
212
213 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
214 kMaxWaitMs);
215
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000216 source_->GetVideoCapturer()->Stop();
217 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
218 kMaxWaitMs);
219}
220
221// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222// fails.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000223TEST_F(VideoSourceTest, CameraFailed) {
224 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
226 kMaxWaitMs);
227
228 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
229 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
230 kMaxWaitMs);
231}
232
233// Test that the capture output is CIF if we set max constraints to CIF.
234// and the capture device support CIF.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000235TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 FakeConstraints constraints;
237 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
238 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
239 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
240
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000241 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
243 kMaxWaitMs);
244 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
245 ASSERT_TRUE(format != NULL);
246 EXPECT_EQ(352, format->width);
247 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000248 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249}
250
251// Test that the capture output is 720P if the camera support it and the
252// optional constraint is set to 720P.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000253TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 FakeConstraints constraints;
255 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
256 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
257 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
258 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
259 1280.0 / 720);
260
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000261 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
263 kMaxWaitMs);
264 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
265 ASSERT_TRUE(format != NULL);
266 EXPECT_EQ(1280, format->width);
267 EXPECT_EQ(720, format->height);
268 EXPECT_EQ(30, format->framerate());
269}
270
271// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
272// require it even if an optional constraint request a higher resolution
273// that don't have this aspect ratio.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000274TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 FakeConstraints constraints;
276 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
277 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
278 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
279 640.0 / 480);
280 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
281
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000282 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
284 kMaxWaitMs);
285 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
286 ASSERT_TRUE(format != NULL);
287 EXPECT_EQ(640, format->width);
288 EXPECT_EQ(480, format->height);
289 EXPECT_EQ(30, format->framerate());
290}
291
292
293// Test that the source state transition to kEnded if the mandatory aspect ratio
294// is set higher than supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000295TEST_F(VideoSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 FakeConstraints constraints;
297 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000298 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
300 kMaxWaitMs);
301}
302
303// Test that the source ignores an optional aspect ratio that is higher than
304// supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000305TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 FakeConstraints constraints;
307 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000308 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
310 kMaxWaitMs);
311 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
312 ASSERT_TRUE(format != NULL);
313 double aspect_ratio = static_cast<double>(format->width) / format->height;
314 EXPECT_LT(aspect_ratio, 2);
315}
316
317// Test that the source starts video with the default resolution if the
318// camera doesn't support capability enumeration and there are no constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000319TEST_F(VideoSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 capturer_->TestWithoutCameraFormats();
321
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000322 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
324 kMaxWaitMs);
325 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
326 ASSERT_TRUE(format != NULL);
327 EXPECT_EQ(640, format->width);
328 EXPECT_EQ(480, format->height);
329 EXPECT_EQ(30, format->framerate());
330}
331
332// Test that the source can start the video and get the requested aspect ratio
333// if the camera doesn't support capability enumeration and the aspect ratio is
334// set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000335TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 capturer_->TestWithoutCameraFormats();
337
338 FakeConstraints constraints;
339 double requested_aspect_ratio = 640.0 / 360;
340 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
341 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
342 requested_aspect_ratio);
343
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000344 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
346 kMaxWaitMs);
347 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
348 double aspect_ratio = static_cast<double>(format->width) / format->height;
349 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
350}
351
352// Test that the source state transitions to kEnded if an unknown mandatory
353// constraint is found.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000354TEST_F(VideoSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 FakeConstraints constraints;
356 constraints.AddMandatory("weird key", 640);
357
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000358 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
360 kMaxWaitMs);
361}
362
363// Test that the source ignores an unknown optional constraint.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000364TEST_F(VideoSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 FakeConstraints constraints;
366 constraints.AddOptional("weird key", 640);
367
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000368 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
370 kMaxWaitMs);
371}
372
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000373TEST_F(VideoSourceTest, SetValidOptionValues) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 FakeConstraints constraints;
375 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000377 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
Karl Wibergbe579832015-11-10 22:34:18 +0100379 EXPECT_EQ(rtc::Optional<bool>(false),
380 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381}
382
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000383TEST_F(VideoSourceTest, OptionNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 FakeConstraints constraints;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000385 CreateVideoSource(&constraints);
Karl Wibergbe579832015-11-10 22:34:18 +0100386 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000389TEST_F(VideoSourceTest, MandatoryOptionOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390 FakeConstraints constraints;
391 constraints.AddMandatory(
392 MediaConstraintsInterface::kNoiseReduction, true);
393 constraints.AddOptional(
394 MediaConstraintsInterface::kNoiseReduction, false);
395
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000396 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397
Karl Wibergbe579832015-11-10 22:34:18 +0100398 EXPECT_EQ(rtc::Optional<bool>(true),
399 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400}
401
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000402TEST_F(VideoSourceTest, InvalidOptionKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 FakeConstraints constraints;
404 constraints.AddOptional(
405 MediaConstraintsInterface::kNoiseReduction, false);
406 constraints.AddOptional("invalidKey", false);
407
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000408 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
410 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
411 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100412 EXPECT_EQ(rtc::Optional<bool>(false),
413 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414}
415
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000416TEST_F(VideoSourceTest, InvalidOptionKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 FakeConstraints constraints;
418 constraints.AddMandatory(
419 MediaConstraintsInterface::kNoiseReduction, false);
420 constraints.AddMandatory("invalidKey", false);
421
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000422 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423
424 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
425 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100426 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427}
428
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000429TEST_F(VideoSourceTest, InvalidOptionValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 FakeConstraints constraints;
431 constraints.AddOptional(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000432 MediaConstraintsInterface::kNoiseReduction, "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000434 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435
436 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
437 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100438 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439}
440
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000441TEST_F(VideoSourceTest, InvalidOptionValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 FakeConstraints constraints;
443 // Optional constraints should be ignored if the mandatory constraints fail.
444 constraints.AddOptional(
445 MediaConstraintsInterface::kNoiseReduction, "false");
446 // Values are case-sensitive and must be all lower-case.
447 constraints.AddMandatory(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000448 MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000450 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451
452 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
453 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100454 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455}
456
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000457TEST_F(VideoSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 FakeConstraints constraints;
459 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
460 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
461 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
462
463 constraints.AddMandatory(
464 MediaConstraintsInterface::kNoiseReduction, false);
465 constraints.AddOptional(
466 MediaConstraintsInterface::kNoiseReduction, true);
467
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000468 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
470 kMaxWaitMs);
471 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
472 ASSERT_TRUE(format != NULL);
473 EXPECT_EQ(352, format->width);
474 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000475 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476
Karl Wibergbe579832015-11-10 22:34:18 +0100477 EXPECT_EQ(rtc::Optional<bool>(false),
478 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
481// Tests that the source starts video with the default resolution for
482// screencast if no constraint is set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000483TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100484 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000487 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
489 kMaxWaitMs);
490 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
491 ASSERT_TRUE(format != NULL);
492 EXPECT_EQ(640, format->width);
493 EXPECT_EQ(480, format->height);
494 EXPECT_EQ(30, format->framerate());
495}
496
497// Tests that the source starts video with the max width and height set by
498// constraints for screencast.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000499TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 FakeConstraints constraints;
501 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
502 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
503
Niels Möller60653ba2016-03-02 11:41:36 +0100504 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000507 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
509 kMaxWaitMs);
510 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
511 ASSERT_TRUE(format != NULL);
512 EXPECT_EQ(480, format->width);
513 EXPECT_EQ(270, format->height);
514 EXPECT_EQ(30, format->framerate());
515}
516
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000517TEST_F(VideoSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 FakeConstraints constraints;
519 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
520
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000521 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
523 kMaxWaitMs);
524 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
525}
526
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000527TEST_F(VideoSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 FakeConstraints constraints;
529 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
530
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000531 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
533 kMaxWaitMs);
534 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
535 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000536 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537}