blob: a49264363ce16760fc998692e32321c1c50266fb [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>
Steve Anton36b29d12017-10-30 09:57:42 -070013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/test/fakeconstraints.h"
17#include "media/base/fakemediaengine.h"
18#include "media/base/fakevideocapturer.h"
19#include "media/base/fakevideorenderer.h"
20#include "pc/videocapturertracksource.h"
21#include "rtc_base/gunit.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) {
deadbeef112b2e92017-02-10 20:13:37 -0800113 capturer_ = new TestVideoCapturer(is_screencast);
114 capturer_cleanup_.reset(capturer_);
Niels Möller60653ba2016-03-02 11:41:36 +0100115 }
116
117 void InitScreencast() { InitCapturer(true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
perkja3ede6c2016-03-08 01:27:48 +0100119 void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120
perkja3ede6c2016-03-08 01:27:48 +0100121 void CreateVideoCapturerSource(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 const webrtc::MediaConstraintsInterface* constraints) {
perkja3ede6c2016-03-08 01:27:48 +0100123 source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(),
deadbeef112b2e92017-02-10 20:13:37 -0800124 std::move(capturer_cleanup_),
perkja3ede6c2016-03-08 01:27:48 +0100125 constraints, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000127 ASSERT_TRUE(source_.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000129 state_observer_.reset(new StateObserver(source_));
130 source_->RegisterObserver(state_observer_.get());
perkjf2880a02016-03-03 01:51:52 -0800131 source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 }
133
deadbeef112b2e92017-02-10 20:13:37 -0800134 std::unique_ptr<cricket::VideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000135 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 cricket::FakeVideoRenderer renderer_;
kwibergd1fe2812016-04-27 06:47:29 -0700137 std::unique_ptr<StateObserver> state_observer_;
perkja3ede6c2016-03-08 01:27:48 +0100138 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139};
140
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000141// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142// device have started and kEnded if it is stopped.
143// It also test that an output can receive video frames.
perkja3ede6c2016-03-08 01:27:48 +0100144TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100146 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
148 kMaxWaitMs);
149
150 ASSERT_TRUE(capturer_->CaptureFrame());
151 EXPECT_EQ(1, renderer_.num_rendered_frames());
152
153 capturer_->Stop();
154 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
155 kMaxWaitMs);
156}
157
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000158// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159// fails.
perkja3ede6c2016-03-08 01:27:48 +0100160TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
161 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
163 kMaxWaitMs);
164
165 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
166 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
167 kMaxWaitMs);
168}
169
170// Test that the capture output is CIF if we set max constraints to CIF.
171// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100172TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 FakeConstraints constraints;
174 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
175 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
176 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
177
perkja3ede6c2016-03-08 01:27:48 +0100178 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
180 kMaxWaitMs);
181 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
182 ASSERT_TRUE(format != NULL);
183 EXPECT_EQ(352, format->width);
184 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700185 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186}
187
188// Test that the capture output is 720P if the camera support it and the
189// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100190TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 FakeConstraints constraints;
192 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
193 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
194 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
195 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
196 1280.0 / 720);
197
perkja3ede6c2016-03-08 01:27:48 +0100198 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
200 kMaxWaitMs);
201 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
202 ASSERT_TRUE(format != NULL);
203 EXPECT_EQ(1280, format->width);
204 EXPECT_EQ(720, format->height);
205 EXPECT_EQ(30, format->framerate());
206}
207
208// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
209// require it even if an optional constraint request a higher resolution
210// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100211TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 FakeConstraints constraints;
213 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
214 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
215 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
216 640.0 / 480);
217 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
218
perkja3ede6c2016-03-08 01:27:48 +0100219 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
221 kMaxWaitMs);
222 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
223 ASSERT_TRUE(format != NULL);
224 EXPECT_EQ(640, format->width);
225 EXPECT_EQ(480, format->height);
226 EXPECT_EQ(30, format->framerate());
227}
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229// Test that the source state transition to kEnded if the mandatory aspect ratio
230// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100231TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 FakeConstraints constraints;
233 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100234 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
236 kMaxWaitMs);
237}
238
239// Test that the source ignores an optional aspect ratio that is higher than
240// supported.
perkja3ede6c2016-03-08 01:27:48 +0100241TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 FakeConstraints constraints;
243 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100244 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
246 kMaxWaitMs);
247 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
248 ASSERT_TRUE(format != NULL);
249 double aspect_ratio = static_cast<double>(format->width) / format->height;
250 EXPECT_LT(aspect_ratio, 2);
251}
252
253// Test that the source starts video with the default resolution if the
254// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100255TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 capturer_->TestWithoutCameraFormats();
257
perkja3ede6c2016-03-08 01:27:48 +0100258 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
260 kMaxWaitMs);
261 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
262 ASSERT_TRUE(format != NULL);
263 EXPECT_EQ(640, format->width);
264 EXPECT_EQ(480, format->height);
265 EXPECT_EQ(30, format->framerate());
266}
267
268// Test that the source can start the video and get the requested aspect ratio
269// if the camera doesn't support capability enumeration and the aspect ratio is
270// set.
perkja3ede6c2016-03-08 01:27:48 +0100271TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 capturer_->TestWithoutCameraFormats();
273
274 FakeConstraints constraints;
275 double requested_aspect_ratio = 640.0 / 360;
276 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
277 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
278 requested_aspect_ratio);
279
perkja3ede6c2016-03-08 01:27:48 +0100280 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
282 kMaxWaitMs);
283 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
284 double aspect_ratio = static_cast<double>(format->width) / format->height;
285 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
286}
287
288// Test that the source state transitions to kEnded if an unknown mandatory
289// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100290TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 FakeConstraints constraints;
292 constraints.AddMandatory("weird key", 640);
293
perkja3ede6c2016-03-08 01:27:48 +0100294 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
296 kMaxWaitMs);
297}
298
299// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100300TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 FakeConstraints constraints;
302 constraints.AddOptional("weird key", 640);
303
perkja3ede6c2016-03-08 01:27:48 +0100304 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
306 kMaxWaitMs);
307}
308
perkj0d3eef22016-03-09 02:39:17 +0100309TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 FakeConstraints constraints;
Perc0d31e92016-03-31 17:23:39 +0200311 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312
perkja3ede6c2016-03-08 01:27:48 +0100313 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314
Perc0d31e92016-03-31 17:23:39 +0200315 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316}
317
perkj0d3eef22016-03-09 02:39:17 +0100318TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100320 CreateVideoCapturerSource(&constraints);
Perc0d31e92016-03-31 17:23:39 +0200321 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322}
323
perkj0d3eef22016-03-09 02:39:17 +0100324TEST_F(VideoCapturerTrackSourceTest,
325 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100327 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
328 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
perkja3ede6c2016-03-08 01:27:48 +0100330 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331
Perc0d31e92016-03-31 17:23:39 +0200332 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333}
334
perkj0d3eef22016-03-09 02:39:17 +0100335TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100337 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 constraints.AddOptional("invalidKey", false);
339
perkja3ede6c2016-03-08 01:27:48 +0100340 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341
342 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100343 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200344 EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345}
346
perkj0d3eef22016-03-09 02:39:17 +0100347TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100349 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 constraints.AddMandatory("invalidKey", false);
351
perkja3ede6c2016-03-08 01:27:48 +0100352 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353
354 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100355 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200356 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357}
358
perkj0d3eef22016-03-09 02:39:17 +0100359TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100361 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
362 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363
perkja3ede6c2016-03-08 01:27:48 +0100364 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
366 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100367 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200368
369 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
perkj0d3eef22016-03-09 02:39:17 +0100372TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 FakeConstraints constraints;
374 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100375 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100377 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
perkja3ede6c2016-03-08 01:27:48 +0100379 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
381 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100382 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200383 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384}
385
perkja3ede6c2016-03-08 01:27:48 +0100386TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 FakeConstraints constraints;
388 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
389 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
390 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
391
perkja3ede6c2016-03-08 01:27:48 +0100392 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
393 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394
perkja3ede6c2016-03-08 01:27:48 +0100395 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
397 kMaxWaitMs);
398 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
399 ASSERT_TRUE(format != NULL);
400 EXPECT_EQ(352, format->width);
401 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700402 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403
Perc0d31e92016-03-31 17:23:39 +0200404 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
407// Tests that the source starts video with the default resolution for
408// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100409TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100410 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412
perkja3ede6c2016-03-08 01:27:48 +0100413 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100414 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
416 kMaxWaitMs);
417 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
418 ASSERT_TRUE(format != NULL);
419 EXPECT_EQ(640, format->width);
420 EXPECT_EQ(480, format->height);
421 EXPECT_EQ(30, format->framerate());
422}
423
424// Tests that the source starts video with the max width and height set by
425// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100426TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 FakeConstraints constraints;
428 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
429 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
430
Niels Möller60653ba2016-03-02 11:41:36 +0100431 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433
perkja3ede6c2016-03-08 01:27:48 +0100434 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100435 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
437 kMaxWaitMs);
438 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
439 ASSERT_TRUE(format != NULL);
440 EXPECT_EQ(480, format->width);
441 EXPECT_EQ(270, format->height);
442 EXPECT_EQ(30, format->framerate());
443}
444
perkja3ede6c2016-03-08 01:27:48 +0100445TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 FakeConstraints constraints;
447 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
448
perkja3ede6c2016-03-08 01:27:48 +0100449 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
451 kMaxWaitMs);
452 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
453}
454
perkja3ede6c2016-03-08 01:27:48 +0100455TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 FakeConstraints constraints;
457 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
458
perkja3ede6c2016-03-08 01:27:48 +0100459 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
461 kMaxWaitMs);
462 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
463 ASSERT_TRUE(format != NULL);
perkjfa10b552016-10-02 23:45:26 -0700464 EXPECT_EQ(1, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465}