blob: d8ca1a578776dfc9d83ecf540fdef9412d168b36 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include "talk/session/media/channelmanager.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/remotevideocapturer.h"
16#include "webrtc/api/test/fakeconstraints.h"
17#include "webrtc/api/videosource.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000018#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080019#include "webrtc/media/base/fakemediaengine.h"
20#include "webrtc/media/base/fakevideocapturer.h"
21#include "webrtc/media/base/fakevideorenderer.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010022#include "webrtc/media/engine/webrtcvideoframe.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:
45 TestVideoCapturer() : test_without_formats_(false) {
46 std::vector<cricket::VideoFormat> formats;
47 formats.push_back(cricket::VideoFormat(1280, 720,
48 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
49 formats.push_back(cricket::VideoFormat(640, 480,
50 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
51 formats.push_back(cricket::VideoFormat(640, 400,
52 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
53 formats.push_back(cricket::VideoFormat(320, 240,
54 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
55 formats.push_back(cricket::VideoFormat(352, 288,
56 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
57 ResetSupportedFormats(formats);
58 }
59
60 // This function is used for resetting the supported capture formats and
61 // simulating a cricket::VideoCapturer implementation that don't support
62 // capture format enumeration. This is used to simulate the current
63 // Chrome implementation.
64 void TestWithoutCameraFormats() {
65 test_without_formats_ = true;
66 std::vector<cricket::VideoFormat> formats;
67 ResetSupportedFormats(formats);
68 }
69
70 virtual cricket::CaptureState Start(
71 const cricket::VideoFormat& capture_format) {
72 if (test_without_formats_) {
73 std::vector<cricket::VideoFormat> formats;
74 formats.push_back(capture_format);
75 ResetSupportedFormats(formats);
76 }
77 return FakeVideoCapturer::Start(capture_format);
78 }
79
80 virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
81 cricket::VideoFormat* best_format) {
82 if (test_without_formats_) {
83 *best_format = desired;
84 return true;
85 }
86 return FakeVideoCapturer::GetBestCaptureFormat(desired,
87 best_format);
88 }
89
90 private:
91 bool test_without_formats_;
92};
93
94class StateObserver : public ObserverInterface {
95 public:
96 explicit StateObserver(VideoSourceInterface* source)
97 : state_(source->state()),
98 source_(source) {
99 }
100 virtual void OnChanged() {
101 state_ = source_->state();
102 }
103 MediaSourceInterface::SourceState state() const { return state_; }
104
105 private:
106 MediaSourceInterface::SourceState state_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 rtc::scoped_refptr<VideoSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000110class VideoSourceTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 protected:
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000112 VideoSourceTest()
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000113 : capturer_cleanup_(new TestVideoCapturer()),
114 capturer_(capturer_cleanup_.get()),
115 channel_manager_(new cricket::ChannelManager(
solenbergfacbbec2015-09-24 00:41:50 -0700116 new cricket::FakeMediaEngine(), rtc::Thread::Current())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 }
118
119 void SetUp() {
120 ASSERT_TRUE(channel_manager_->Init());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
122
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000123 void CreateVideoSource() {
124 CreateVideoSource(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 }
126
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000127 void CreateVideoSource(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 const webrtc::MediaConstraintsInterface* constraints) {
129 // VideoSource take ownership of |capturer_|
tommi6eca7e32015-12-15 04:27:11 -0800130 source_ =
131 VideoSource::Create(channel_manager_.get(), capturer_cleanup_.release(),
132 constraints, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000134 ASSERT_TRUE(source_.get() != NULL);
135 EXPECT_EQ(capturer_, source_->GetVideoCapturer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000137 state_observer_.reset(new StateObserver(source_));
138 source_->RegisterObserver(state_observer_.get());
139 source_->AddSink(&renderer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 }
141
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000142 rtc::scoped_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000143 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 cricket::FakeVideoRenderer renderer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145 rtc::scoped_ptr<cricket::ChannelManager> channel_manager_;
146 rtc::scoped_ptr<StateObserver> state_observer_;
147 rtc::scoped_refptr<VideoSource> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
150
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000151// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152// device have started and kEnded if it is stopped.
153// It also test that an output can receive video frames.
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000154TEST_F(VideoSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 // Initialize without constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000156 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
158 kMaxWaitMs);
159
160 ASSERT_TRUE(capturer_->CaptureFrame());
161 EXPECT_EQ(1, renderer_.num_rendered_frames());
162
163 capturer_->Stop();
164 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
165 kMaxWaitMs);
166}
167
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000168// Test that a VideoSource can be stopped and restarted.
169TEST_F(VideoSourceTest, StopRestart) {
170 // Initialize without constraints.
171 CreateVideoSource();
172 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
173 kMaxWaitMs);
174
175 ASSERT_TRUE(capturer_->CaptureFrame());
176 EXPECT_EQ(1, renderer_.num_rendered_frames());
177
178 source_->Stop();
179 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
180 kMaxWaitMs);
181
182 source_->Restart();
183 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
184 kMaxWaitMs);
185
186 ASSERT_TRUE(capturer_->CaptureFrame());
187 EXPECT_EQ(2, renderer_.num_rendered_frames());
188
189 source_->Stop();
190}
191
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000192// Test start stop with a remote VideoSource - the video source that has a
193// RemoteVideoCapturer and takes video frames from FrameInput.
194TEST_F(VideoSourceTest, StartStopRemote) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000195 source_ = VideoSource::Create(channel_manager_.get(),
tommi6eca7e32015-12-15 04:27:11 -0800196 new webrtc::RemoteVideoCapturer(), NULL, true);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000197
198 ASSERT_TRUE(source_.get() != NULL);
199 EXPECT_TRUE(NULL != source_->GetVideoCapturer());
200
201 state_observer_.reset(new StateObserver(source_));
202 source_->RegisterObserver(state_observer_.get());
203 source_->AddSink(&renderer_);
204
205 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
206 kMaxWaitMs);
207
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000208 source_->GetVideoCapturer()->Stop();
209 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
210 kMaxWaitMs);
211}
212
213// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214// fails.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000215TEST_F(VideoSourceTest, CameraFailed) {
216 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
218 kMaxWaitMs);
219
220 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
221 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
222 kMaxWaitMs);
223}
224
225// Test that the capture output is CIF if we set max constraints to CIF.
226// and the capture device support CIF.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000227TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 FakeConstraints constraints;
229 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
230 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
231 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
232
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000233 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
235 kMaxWaitMs);
236 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
237 ASSERT_TRUE(format != NULL);
238 EXPECT_EQ(352, format->width);
239 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000240 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241}
242
243// Test that the capture output is 720P if the camera support it and the
244// optional constraint is set to 720P.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000245TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 FakeConstraints constraints;
247 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
248 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
249 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
250 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
251 1280.0 / 720);
252
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000253 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
255 kMaxWaitMs);
256 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
257 ASSERT_TRUE(format != NULL);
258 EXPECT_EQ(1280, format->width);
259 EXPECT_EQ(720, format->height);
260 EXPECT_EQ(30, format->framerate());
261}
262
263// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
264// require it even if an optional constraint request a higher resolution
265// that don't have this aspect ratio.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000266TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 FakeConstraints constraints;
268 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
269 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
270 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
271 640.0 / 480);
272 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
273
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000274 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
276 kMaxWaitMs);
277 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
278 ASSERT_TRUE(format != NULL);
279 EXPECT_EQ(640, format->width);
280 EXPECT_EQ(480, format->height);
281 EXPECT_EQ(30, format->framerate());
282}
283
284
285// Test that the source state transition to kEnded if the mandatory aspect ratio
286// is set higher than supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000287TEST_F(VideoSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 FakeConstraints constraints;
289 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000290 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
292 kMaxWaitMs);
293}
294
295// Test that the source ignores an optional aspect ratio that is higher than
296// supported.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000297TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 FakeConstraints constraints;
299 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000300 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
302 kMaxWaitMs);
303 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
304 ASSERT_TRUE(format != NULL);
305 double aspect_ratio = static_cast<double>(format->width) / format->height;
306 EXPECT_LT(aspect_ratio, 2);
307}
308
309// Test that the source starts video with the default resolution if the
310// camera doesn't support capability enumeration and there are no constraints.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000311TEST_F(VideoSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 capturer_->TestWithoutCameraFormats();
313
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000314 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
316 kMaxWaitMs);
317 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
318 ASSERT_TRUE(format != NULL);
319 EXPECT_EQ(640, format->width);
320 EXPECT_EQ(480, format->height);
321 EXPECT_EQ(30, format->framerate());
322}
323
324// Test that the source can start the video and get the requested aspect ratio
325// if the camera doesn't support capability enumeration and the aspect ratio is
326// set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000327TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 capturer_->TestWithoutCameraFormats();
329
330 FakeConstraints constraints;
331 double requested_aspect_ratio = 640.0 / 360;
332 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
333 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
334 requested_aspect_ratio);
335
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000336 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
338 kMaxWaitMs);
339 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
340 double aspect_ratio = static_cast<double>(format->width) / format->height;
341 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
342}
343
344// Test that the source state transitions to kEnded if an unknown mandatory
345// constraint is found.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000346TEST_F(VideoSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 FakeConstraints constraints;
348 constraints.AddMandatory("weird key", 640);
349
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000350 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
352 kMaxWaitMs);
353}
354
355// Test that the source ignores an unknown optional constraint.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000356TEST_F(VideoSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 FakeConstraints constraints;
358 constraints.AddOptional("weird key", 640);
359
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000360 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
362 kMaxWaitMs);
363}
364
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000365TEST_F(VideoSourceTest, SetValidOptionValues) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 FakeConstraints constraints;
367 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000369 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370
Karl Wibergbe579832015-11-10 22:34:18 +0100371 EXPECT_EQ(rtc::Optional<bool>(false),
372 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373}
374
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000375TEST_F(VideoSourceTest, OptionNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 FakeConstraints constraints;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000377 CreateVideoSource(&constraints);
Karl Wibergbe579832015-11-10 22:34:18 +0100378 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000381TEST_F(VideoSourceTest, MandatoryOptionOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 FakeConstraints constraints;
383 constraints.AddMandatory(
384 MediaConstraintsInterface::kNoiseReduction, true);
385 constraints.AddOptional(
386 MediaConstraintsInterface::kNoiseReduction, false);
387
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000388 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389
Karl Wibergbe579832015-11-10 22:34:18 +0100390 EXPECT_EQ(rtc::Optional<bool>(true),
391 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392}
393
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000394TEST_F(VideoSourceTest, InvalidOptionKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 FakeConstraints constraints;
396 constraints.AddOptional(
397 MediaConstraintsInterface::kNoiseReduction, false);
398 constraints.AddOptional("invalidKey", false);
399
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000400 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401
402 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
403 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100404 EXPECT_EQ(rtc::Optional<bool>(false),
405 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000408TEST_F(VideoSourceTest, InvalidOptionKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 FakeConstraints constraints;
410 constraints.AddMandatory(
411 MediaConstraintsInterface::kNoiseReduction, false);
412 constraints.AddMandatory("invalidKey", false);
413
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000414 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415
416 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
417 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100418 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419}
420
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000421TEST_F(VideoSourceTest, InvalidOptionValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 FakeConstraints constraints;
423 constraints.AddOptional(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000424 MediaConstraintsInterface::kNoiseReduction, "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000426 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
428 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
429 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100430 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431}
432
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000433TEST_F(VideoSourceTest, InvalidOptionValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 FakeConstraints constraints;
435 // Optional constraints should be ignored if the mandatory constraints fail.
436 constraints.AddOptional(
437 MediaConstraintsInterface::kNoiseReduction, "false");
438 // Values are case-sensitive and must be all lower-case.
439 constraints.AddMandatory(
buildbot@webrtc.org81ddc782014-10-14 22:39:24 +0000440 MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000442 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
444 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
445 kMaxWaitMs);
Karl Wibergbe579832015-11-10 22:34:18 +0100446 EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447}
448
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000449TEST_F(VideoSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 FakeConstraints constraints;
451 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
452 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
453 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
454
455 constraints.AddMandatory(
456 MediaConstraintsInterface::kNoiseReduction, false);
457 constraints.AddOptional(
458 MediaConstraintsInterface::kNoiseReduction, true);
459
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000460 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
462 kMaxWaitMs);
463 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
464 ASSERT_TRUE(format != NULL);
465 EXPECT_EQ(352, format->width);
466 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000467 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468
Karl Wibergbe579832015-11-10 22:34:18 +0100469 EXPECT_EQ(rtc::Optional<bool>(false),
470 source_->options()->video_noise_reduction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471}
472
473// Tests that the source starts video with the default resolution for
474// screencast if no constraint is set.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000475TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 capturer_->TestWithoutCameraFormats();
477 capturer_->SetScreencast(true);
478
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000479 CreateVideoSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
481 kMaxWaitMs);
482 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
483 ASSERT_TRUE(format != NULL);
484 EXPECT_EQ(640, format->width);
485 EXPECT_EQ(480, format->height);
486 EXPECT_EQ(30, format->framerate());
487}
488
489// Tests that the source starts video with the max width and height set by
490// constraints for screencast.
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000491TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 FakeConstraints constraints;
493 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
494 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
495
496 capturer_->TestWithoutCameraFormats();
497 capturer_->SetScreencast(true);
498
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000499 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
501 kMaxWaitMs);
502 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
503 ASSERT_TRUE(format != NULL);
504 EXPECT_EQ(480, format->width);
505 EXPECT_EQ(270, format->height);
506 EXPECT_EQ(30, format->framerate());
507}
508
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000509TEST_F(VideoSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 FakeConstraints constraints;
511 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
512
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000513 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
515 kMaxWaitMs);
516 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
517}
518
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000519TEST_F(VideoSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 FakeConstraints constraints;
521 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
522
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000523 CreateVideoSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
525 kMaxWaitMs);
526 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
527 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000528 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}