blob: b183421153c51f5bf549e1e3aacb5032e1aceb2b [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"
perkja3ede6c2016-03-08 01:27:48 +010016#include "webrtc/api/videocapturertracksource.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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23using webrtc::FakeConstraints;
perkja3ede6c2016-03-08 01:27:48 +010024using webrtc::VideoCapturerTrackSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025using webrtc::MediaConstraintsInterface;
26using webrtc::MediaSourceInterface;
27using webrtc::ObserverInterface;
perkja3ede6c2016-03-08 01:27:48 +010028using webrtc::VideoTrackSourceInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace {
31
32// Max wait time for a test.
33const int kMaxWaitMs = 100;
34
35} // anonymous namespace
36
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037// TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for
38// testing without known camera formats.
39// It keeps its own lists of cricket::VideoFormats for the unit tests in this
40// file.
41class TestVideoCapturer : public cricket::FakeVideoCapturer {
42 public:
perkja3ede6c2016-03-08 01:27:48 +010043 explicit TestVideoCapturer(bool is_screencast)
44 : FakeVideoCapturer(is_screencast), test_without_formats_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 std::vector<cricket::VideoFormat> formats;
perkja3ede6c2016-03-08 01:27:48 +010046 formats.push_back(
47 cricket::VideoFormat(1280, 720, cricket::VideoFormat::FpsToInterval(30),
48 cricket::FOURCC_I420));
49 formats.push_back(
50 cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
51 cricket::FOURCC_I420));
52 formats.push_back(
53 cricket::VideoFormat(640, 400, cricket::VideoFormat::FpsToInterval(30),
54 cricket::FOURCC_I420));
55 formats.push_back(
56 cricket::VideoFormat(320, 240, cricket::VideoFormat::FpsToInterval(30),
57 cricket::FOURCC_I420));
58 formats.push_back(
59 cricket::VideoFormat(352, 288, cricket::VideoFormat::FpsToInterval(30),
60 cricket::FOURCC_I420));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 ResetSupportedFormats(formats);
62 }
63
64 // This function is used for resetting the supported capture formats and
65 // simulating a cricket::VideoCapturer implementation that don't support
66 // capture format enumeration. This is used to simulate the current
67 // Chrome implementation.
68 void TestWithoutCameraFormats() {
69 test_without_formats_ = true;
70 std::vector<cricket::VideoFormat> formats;
71 ResetSupportedFormats(formats);
72 }
73
74 virtual cricket::CaptureState Start(
75 const cricket::VideoFormat& capture_format) {
76 if (test_without_formats_) {
77 std::vector<cricket::VideoFormat> formats;
78 formats.push_back(capture_format);
79 ResetSupportedFormats(formats);
80 }
81 return FakeVideoCapturer::Start(capture_format);
82 }
83
84 virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
85 cricket::VideoFormat* best_format) {
86 if (test_without_formats_) {
87 *best_format = desired;
88 return true;
89 }
perkja3ede6c2016-03-08 01:27:48 +010090 return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 }
92
93 private:
94 bool test_without_formats_;
95};
96
97class StateObserver : public ObserverInterface {
98 public:
perkja3ede6c2016-03-08 01:27:48 +010099 explicit StateObserver(VideoTrackSourceInterface* source)
100 : state_(source->state()), source_(source) {}
101 virtual void OnChanged() { state_ = source_->state(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 MediaSourceInterface::SourceState state() const { return state_; }
103
104 private:
105 MediaSourceInterface::SourceState state_;
perkja3ede6c2016-03-08 01:27:48 +0100106 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107};
108
perkja3ede6c2016-03-08 01:27:48 +0100109class VideoCapturerTrackSourceTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 protected:
perkja3ede6c2016-03-08 01:27:48 +0100111 VideoCapturerTrackSourceTest() { InitCapturer(false); }
Niels Möller60653ba2016-03-02 11:41:36 +0100112 void InitCapturer(bool is_screencast) {
113 capturer_cleanup_ = rtc::scoped_ptr<TestVideoCapturer>(
114 new TestVideoCapturer(is_screencast));
115 capturer_ = capturer_cleanup_.get();
116 }
117
118 void InitScreencast() { InitCapturer(true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
perkja3ede6c2016-03-08 01:27:48 +0100120 void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
perkja3ede6c2016-03-08 01:27:48 +0100122 void CreateVideoCapturerSource(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 const webrtc::MediaConstraintsInterface* constraints) {
124 // VideoSource take ownership of |capturer_|
perkja3ede6c2016-03-08 01:27:48 +0100125 source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(),
126 capturer_cleanup_.release(),
127 constraints, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000129 ASSERT_TRUE(source_.get() != NULL);
130 EXPECT_EQ(capturer_, source_->GetVideoCapturer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000132 state_observer_.reset(new StateObserver(source_));
133 source_->RegisterObserver(state_observer_.get());
perkjf2880a02016-03-03 01:51:52 -0800134 source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 }
136
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 rtc::scoped_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000138 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 cricket::FakeVideoRenderer renderer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 rtc::scoped_ptr<StateObserver> state_observer_;
perkja3ede6c2016-03-08 01:27:48 +0100141 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142};
143
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000144// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145// device have started and kEnded if it is stopped.
146// It also test that an output can receive video frames.
perkja3ede6c2016-03-08 01:27:48 +0100147TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100149 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
151 kMaxWaitMs);
152
153 ASSERT_TRUE(capturer_->CaptureFrame());
154 EXPECT_EQ(1, renderer_.num_rendered_frames());
155
156 capturer_->Stop();
157 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
158 kMaxWaitMs);
159}
160
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000161// Test that a VideoSource can be stopped and restarted.
perkja3ede6c2016-03-08 01:27:48 +0100162TEST_F(VideoCapturerTrackSourceTest, StopRestart) {
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000163 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100164 CreateVideoCapturerSource();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +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 source_->Stop();
172 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
173 kMaxWaitMs);
174
175 source_->Restart();
176 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
177 kMaxWaitMs);
178
179 ASSERT_TRUE(capturer_->CaptureFrame());
180 EXPECT_EQ(2, renderer_.num_rendered_frames());
181
182 source_->Stop();
183}
184
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000185// Test start stop with a remote VideoSource - the video source that has a
186// RemoteVideoCapturer and takes video frames from FrameInput.
perkja3ede6c2016-03-08 01:27:48 +0100187TEST_F(VideoCapturerTrackSourceTest, StartStopRemote) {
188 source_ = VideoCapturerTrackSource::Create(
189 rtc::Thread::Current(), new webrtc::RemoteVideoCapturer(), NULL, true);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000190
191 ASSERT_TRUE(source_.get() != NULL);
192 EXPECT_TRUE(NULL != source_->GetVideoCapturer());
193
194 state_observer_.reset(new StateObserver(source_));
195 source_->RegisterObserver(state_observer_.get());
perkjf2880a02016-03-03 01:51:52 -0800196 source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000197
198 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
199 kMaxWaitMs);
200
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000201 source_->GetVideoCapturer()->Stop();
202 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
203 kMaxWaitMs);
204}
205
206// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207// fails.
perkja3ede6c2016-03-08 01:27:48 +0100208TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
209 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
211 kMaxWaitMs);
212
213 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
214 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
215 kMaxWaitMs);
216}
217
218// Test that the capture output is CIF if we set max constraints to CIF.
219// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100220TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 FakeConstraints constraints;
222 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
223 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
224 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
225
perkja3ede6c2016-03-08 01:27:48 +0100226 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
228 kMaxWaitMs);
229 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
230 ASSERT_TRUE(format != NULL);
231 EXPECT_EQ(352, format->width);
232 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000233 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234}
235
236// Test that the capture output is 720P if the camera support it and the
237// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100238TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 FakeConstraints constraints;
240 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
241 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
242 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
243 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
244 1280.0 / 720);
245
perkja3ede6c2016-03-08 01:27:48 +0100246 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
248 kMaxWaitMs);
249 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
250 ASSERT_TRUE(format != NULL);
251 EXPECT_EQ(1280, format->width);
252 EXPECT_EQ(720, format->height);
253 EXPECT_EQ(30, format->framerate());
254}
255
256// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
257// require it even if an optional constraint request a higher resolution
258// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100259TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 FakeConstraints constraints;
261 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
262 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
263 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
264 640.0 / 480);
265 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
266
perkja3ede6c2016-03-08 01:27:48 +0100267 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
269 kMaxWaitMs);
270 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
271 ASSERT_TRUE(format != NULL);
272 EXPECT_EQ(640, format->width);
273 EXPECT_EQ(480, format->height);
274 EXPECT_EQ(30, format->framerate());
275}
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277// Test that the source state transition to kEnded if the mandatory aspect ratio
278// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100279TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 FakeConstraints constraints;
281 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100282 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
284 kMaxWaitMs);
285}
286
287// Test that the source ignores an optional aspect ratio that is higher than
288// supported.
perkja3ede6c2016-03-08 01:27:48 +0100289TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 FakeConstraints constraints;
291 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100292 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
294 kMaxWaitMs);
295 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
296 ASSERT_TRUE(format != NULL);
297 double aspect_ratio = static_cast<double>(format->width) / format->height;
298 EXPECT_LT(aspect_ratio, 2);
299}
300
301// Test that the source starts video with the default resolution if the
302// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100303TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 capturer_->TestWithoutCameraFormats();
305
perkja3ede6c2016-03-08 01:27:48 +0100306 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
308 kMaxWaitMs);
309 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
310 ASSERT_TRUE(format != NULL);
311 EXPECT_EQ(640, format->width);
312 EXPECT_EQ(480, format->height);
313 EXPECT_EQ(30, format->framerate());
314}
315
316// Test that the source can start the video and get the requested aspect ratio
317// if the camera doesn't support capability enumeration and the aspect ratio is
318// set.
perkja3ede6c2016-03-08 01:27:48 +0100319TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 capturer_->TestWithoutCameraFormats();
321
322 FakeConstraints constraints;
323 double requested_aspect_ratio = 640.0 / 360;
324 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
325 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
326 requested_aspect_ratio);
327
perkja3ede6c2016-03-08 01:27:48 +0100328 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
330 kMaxWaitMs);
331 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
332 double aspect_ratio = static_cast<double>(format->width) / format->height;
333 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
334}
335
336// Test that the source state transitions to kEnded if an unknown mandatory
337// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100338TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 FakeConstraints constraints;
340 constraints.AddMandatory("weird key", 640);
341
perkja3ede6c2016-03-08 01:27:48 +0100342 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
344 kMaxWaitMs);
345}
346
347// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100348TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 FakeConstraints constraints;
350 constraints.AddOptional("weird key", 640);
351
perkja3ede6c2016-03-08 01:27:48 +0100352 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
354 kMaxWaitMs);
355}
356
perkj0d3eef22016-03-09 02:39:17 +0100357TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100359 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "true");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360
perkja3ede6c2016-03-08 01:27:48 +0100361 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362
perkj0d3eef22016-03-09 02:39:17 +0100363 EXPECT_TRUE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364}
365
perkj0d3eef22016-03-09 02:39:17 +0100366TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100368 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100369 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
perkj0d3eef22016-03-09 02:39:17 +0100372TEST_F(VideoCapturerTrackSourceTest,
373 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100375 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
376 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377
perkja3ede6c2016-03-08 01:27:48 +0100378 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379
perkj0d3eef22016-03-09 02:39:17 +0100380 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381}
382
perkj0d3eef22016-03-09 02:39:17 +0100383TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100385 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 constraints.AddOptional("invalidKey", false);
387
perkja3ede6c2016-03-08 01:27:48 +0100388 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389
390 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100391 kMaxWaitMs);
perkj0d3eef22016-03-09 02:39:17 +0100392 EXPECT_TRUE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393}
394
perkj0d3eef22016-03-09 02:39:17 +0100395TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100397 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 constraints.AddMandatory("invalidKey", false);
399
perkja3ede6c2016-03-08 01:27:48 +0100400 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401
402 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100403 kMaxWaitMs);
perkj0d3eef22016-03-09 02:39:17 +0100404 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
perkj0d3eef22016-03-09 02:39:17 +0100407TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100409 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
410 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411
perkja3ede6c2016-03-08 01:27:48 +0100412 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413
414 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100415 kMaxWaitMs);
perkj0d3eef22016-03-09 02:39:17 +0100416 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417}
418
perkj0d3eef22016-03-09 02:39:17 +0100419TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 FakeConstraints constraints;
421 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100422 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100424 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425
perkja3ede6c2016-03-08 01:27:48 +0100426 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
428 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100429 kMaxWaitMs);
perkj0d3eef22016-03-09 02:39:17 +0100430 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431}
432
perkja3ede6c2016-03-08 01:27:48 +0100433TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 FakeConstraints constraints;
435 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
436 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
437 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
438
perkja3ede6c2016-03-08 01:27:48 +0100439 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
440 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441
perkja3ede6c2016-03-08 01:27:48 +0100442 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
444 kMaxWaitMs);
445 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
446 ASSERT_TRUE(format != NULL);
447 EXPECT_EQ(352, format->width);
448 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000449 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450
perkj0d3eef22016-03-09 02:39:17 +0100451 EXPECT_FALSE(source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452}
453
454// Tests that the source starts video with the default resolution for
455// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100456TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100457 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
perkja3ede6c2016-03-08 01:27:48 +0100460 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100461 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
463 kMaxWaitMs);
464 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
465 ASSERT_TRUE(format != NULL);
466 EXPECT_EQ(640, format->width);
467 EXPECT_EQ(480, format->height);
468 EXPECT_EQ(30, format->framerate());
469}
470
471// Tests that the source starts video with the max width and height set by
472// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100473TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 FakeConstraints constraints;
475 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
476 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
477
Niels Möller60653ba2016-03-02 11:41:36 +0100478 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
perkja3ede6c2016-03-08 01:27:48 +0100481 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100482 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
484 kMaxWaitMs);
485 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
486 ASSERT_TRUE(format != NULL);
487 EXPECT_EQ(480, format->width);
488 EXPECT_EQ(270, format->height);
489 EXPECT_EQ(30, format->framerate());
490}
491
perkja3ede6c2016-03-08 01:27:48 +0100492TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 FakeConstraints constraints;
494 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
495
perkja3ede6c2016-03-08 01:27:48 +0100496 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
498 kMaxWaitMs);
499 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
500}
501
perkja3ede6c2016-03-08 01:27:48 +0100502TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 FakeConstraints constraints;
504 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
505
perkja3ede6c2016-03-08 01:27:48 +0100506 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
508 kMaxWaitMs);
509 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
510 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000511 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512}