blob: 90d2cd2563dfaccb79f91840eae6fa22bd8a83b2 [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
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
13#include <vector>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#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) {
kwibergd1fe2812016-04-27 06:47:29 -0700113 capturer_cleanup_ = std::unique_ptr<TestVideoCapturer>(
Niels Möller60653ba2016-03-02 11:41:36 +0100114 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000131 state_observer_.reset(new StateObserver(source_));
132 source_->RegisterObserver(state_observer_.get());
perkjf2880a02016-03-03 01:51:52 -0800133 source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 }
135
kwibergd1fe2812016-04-27 06:47:29 -0700136 std::unique_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000137 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 cricket::FakeVideoRenderer renderer_;
kwibergd1fe2812016-04-27 06:47:29 -0700139 std::unique_ptr<StateObserver> state_observer_;
perkja3ede6c2016-03-08 01:27:48 +0100140 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141};
142
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000143// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144// device have started and kEnded if it is stopped.
145// It also test that an output can receive video frames.
perkja3ede6c2016-03-08 01:27:48 +0100146TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100148 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
150 kMaxWaitMs);
151
152 ASSERT_TRUE(capturer_->CaptureFrame());
153 EXPECT_EQ(1, renderer_.num_rendered_frames());
154
155 capturer_->Stop();
156 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
157 kMaxWaitMs);
158}
159
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000160// Test that a VideoSource can be stopped and restarted.
perkja3ede6c2016-03-08 01:27:48 +0100161TEST_F(VideoCapturerTrackSourceTest, StopRestart) {
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000162 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100163 CreateVideoCapturerSource();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000164 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
165 kMaxWaitMs);
166
167 ASSERT_TRUE(capturer_->CaptureFrame());
168 EXPECT_EQ(1, renderer_.num_rendered_frames());
169
170 source_->Stop();
171 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
172 kMaxWaitMs);
173
174 source_->Restart();
175 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
176 kMaxWaitMs);
177
178 ASSERT_TRUE(capturer_->CaptureFrame());
179 EXPECT_EQ(2, renderer_.num_rendered_frames());
180
181 source_->Stop();
182}
183
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000184// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185// fails.
perkja3ede6c2016-03-08 01:27:48 +0100186TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
187 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
189 kMaxWaitMs);
190
191 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
192 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
193 kMaxWaitMs);
194}
195
196// Test that the capture output is CIF if we set max constraints to CIF.
197// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100198TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 FakeConstraints constraints;
200 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
201 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
202 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
203
perkja3ede6c2016-03-08 01:27:48 +0100204 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
206 kMaxWaitMs);
207 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
208 ASSERT_TRUE(format != NULL);
209 EXPECT_EQ(352, format->width);
210 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000211 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212}
213
214// Test that the capture output is 720P if the camera support it and the
215// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100216TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 FakeConstraints constraints;
218 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
219 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
220 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
221 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
222 1280.0 / 720);
223
perkja3ede6c2016-03-08 01:27:48 +0100224 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
226 kMaxWaitMs);
227 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
228 ASSERT_TRUE(format != NULL);
229 EXPECT_EQ(1280, format->width);
230 EXPECT_EQ(720, format->height);
231 EXPECT_EQ(30, format->framerate());
232}
233
234// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
235// require it even if an optional constraint request a higher resolution
236// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100237TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 FakeConstraints constraints;
239 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
240 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
241 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
242 640.0 / 480);
243 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
244
perkja3ede6c2016-03-08 01:27:48 +0100245 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
247 kMaxWaitMs);
248 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
249 ASSERT_TRUE(format != NULL);
250 EXPECT_EQ(640, format->width);
251 EXPECT_EQ(480, format->height);
252 EXPECT_EQ(30, format->framerate());
253}
254
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255// Test that the source state transition to kEnded if the mandatory aspect ratio
256// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100257TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 FakeConstraints constraints;
259 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100260 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
262 kMaxWaitMs);
263}
264
265// Test that the source ignores an optional aspect ratio that is higher than
266// supported.
perkja3ede6c2016-03-08 01:27:48 +0100267TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 FakeConstraints constraints;
269 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100270 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
272 kMaxWaitMs);
273 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
274 ASSERT_TRUE(format != NULL);
275 double aspect_ratio = static_cast<double>(format->width) / format->height;
276 EXPECT_LT(aspect_ratio, 2);
277}
278
279// Test that the source starts video with the default resolution if the
280// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100281TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 capturer_->TestWithoutCameraFormats();
283
perkja3ede6c2016-03-08 01:27:48 +0100284 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
286 kMaxWaitMs);
287 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
288 ASSERT_TRUE(format != NULL);
289 EXPECT_EQ(640, format->width);
290 EXPECT_EQ(480, format->height);
291 EXPECT_EQ(30, format->framerate());
292}
293
294// Test that the source can start the video and get the requested aspect ratio
295// if the camera doesn't support capability enumeration and the aspect ratio is
296// set.
perkja3ede6c2016-03-08 01:27:48 +0100297TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 capturer_->TestWithoutCameraFormats();
299
300 FakeConstraints constraints;
301 double requested_aspect_ratio = 640.0 / 360;
302 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
303 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
304 requested_aspect_ratio);
305
perkja3ede6c2016-03-08 01:27:48 +0100306 CreateVideoCapturerSource(&constraints);
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 double aspect_ratio = static_cast<double>(format->width) / format->height;
311 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
312}
313
314// Test that the source state transitions to kEnded if an unknown mandatory
315// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100316TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 FakeConstraints constraints;
318 constraints.AddMandatory("weird key", 640);
319
perkja3ede6c2016-03-08 01:27:48 +0100320 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
322 kMaxWaitMs);
323}
324
325// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100326TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 FakeConstraints constraints;
328 constraints.AddOptional("weird key", 640);
329
perkja3ede6c2016-03-08 01:27:48 +0100330 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
332 kMaxWaitMs);
333}
334
perkj0d3eef22016-03-09 02:39:17 +0100335TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 FakeConstraints constraints;
Perc0d31e92016-03-31 17:23:39 +0200337 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
perkja3ede6c2016-03-08 01:27:48 +0100339 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340
Perc0d31e92016-03-31 17:23:39 +0200341 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342}
343
perkj0d3eef22016-03-09 02:39:17 +0100344TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100346 CreateVideoCapturerSource(&constraints);
Perc0d31e92016-03-31 17:23:39 +0200347 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348}
349
perkj0d3eef22016-03-09 02:39:17 +0100350TEST_F(VideoCapturerTrackSourceTest,
351 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100353 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
354 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
perkja3ede6c2016-03-08 01:27:48 +0100356 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
Perc0d31e92016-03-31 17:23:39 +0200358 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359}
360
perkj0d3eef22016-03-09 02:39:17 +0100361TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100363 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 constraints.AddOptional("invalidKey", false);
365
perkja3ede6c2016-03-08 01:27:48 +0100366 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367
368 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100369 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200370 EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
perkj0d3eef22016-03-09 02:39:17 +0100373TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100375 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 constraints.AddMandatory("invalidKey", false);
377
perkja3ede6c2016-03-08 01:27:48 +0100378 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379
380 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100381 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200382 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
perkj0d3eef22016-03-09 02:39:17 +0100385TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100387 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
388 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389
perkja3ede6c2016-03-08 01:27:48 +0100390 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391
392 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100393 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200394
395 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396}
397
perkj0d3eef22016-03-09 02:39:17 +0100398TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 FakeConstraints constraints;
400 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100401 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100403 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
perkja3ede6c2016-03-08 01:27:48 +0100405 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406
407 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100408 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200409 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410}
411
perkja3ede6c2016-03-08 01:27:48 +0100412TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 FakeConstraints constraints;
414 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
415 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
416 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
417
perkja3ede6c2016-03-08 01:27:48 +0100418 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
419 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420
perkja3ede6c2016-03-08 01:27:48 +0100421 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
423 kMaxWaitMs);
424 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
425 ASSERT_TRUE(format != NULL);
426 EXPECT_EQ(352, format->width);
427 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000428 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429
Perc0d31e92016-03-31 17:23:39 +0200430 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431}
432
433// Tests that the source starts video with the default resolution for
434// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100435TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100436 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438
perkja3ede6c2016-03-08 01:27:48 +0100439 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100440 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
442 kMaxWaitMs);
443 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
444 ASSERT_TRUE(format != NULL);
445 EXPECT_EQ(640, format->width);
446 EXPECT_EQ(480, format->height);
447 EXPECT_EQ(30, format->framerate());
448}
449
450// Tests that the source starts video with the max width and height set by
451// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100452TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 FakeConstraints constraints;
454 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
455 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
456
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(&constraints);
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(480, format->width);
467 EXPECT_EQ(270, format->height);
468 EXPECT_EQ(30, format->framerate());
469}
470
perkja3ede6c2016-03-08 01:27:48 +0100471TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 FakeConstraints constraints;
473 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
474
perkja3ede6c2016-03-08 01:27:48 +0100475 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
477 kMaxWaitMs);
478 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
479}
480
perkja3ede6c2016-03-08 01:27:48 +0100481TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 FakeConstraints constraints;
483 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
484
perkja3ede6c2016-03-08 01:27:48 +0100485 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
487 kMaxWaitMs);
488 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
489 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000490 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}