blob: a0350d999d99573fe9a393f64930fe27646f9f64 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22using webrtc::FakeConstraints;
perkja3ede6c2016-03-08 01:27:48 +010023using webrtc::VideoCapturerTrackSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024using webrtc::MediaConstraintsInterface;
25using webrtc::MediaSourceInterface;
26using webrtc::ObserverInterface;
perkja3ede6c2016-03-08 01:27:48 +010027using webrtc::VideoTrackSourceInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace {
30
31// Max wait time for a test.
32const int kMaxWaitMs = 100;
33
34} // anonymous namespace
35
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036// TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for
37// testing without known camera formats.
38// It keeps its own lists of cricket::VideoFormats for the unit tests in this
39// file.
40class TestVideoCapturer : public cricket::FakeVideoCapturer {
41 public:
perkja3ede6c2016-03-08 01:27:48 +010042 explicit TestVideoCapturer(bool is_screencast)
43 : FakeVideoCapturer(is_screencast), test_without_formats_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 std::vector<cricket::VideoFormat> formats;
perkja3ede6c2016-03-08 01:27:48 +010045 formats.push_back(
46 cricket::VideoFormat(1280, 720, cricket::VideoFormat::FpsToInterval(30),
47 cricket::FOURCC_I420));
48 formats.push_back(
49 cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
50 cricket::FOURCC_I420));
51 formats.push_back(
52 cricket::VideoFormat(640, 400, cricket::VideoFormat::FpsToInterval(30),
53 cricket::FOURCC_I420));
54 formats.push_back(
55 cricket::VideoFormat(320, 240, cricket::VideoFormat::FpsToInterval(30),
56 cricket::FOURCC_I420));
57 formats.push_back(
58 cricket::VideoFormat(352, 288, cricket::VideoFormat::FpsToInterval(30),
59 cricket::FOURCC_I420));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 ResetSupportedFormats(formats);
61 }
62
63 // This function is used for resetting the supported capture formats and
64 // simulating a cricket::VideoCapturer implementation that don't support
65 // capture format enumeration. This is used to simulate the current
66 // Chrome implementation.
67 void TestWithoutCameraFormats() {
68 test_without_formats_ = true;
69 std::vector<cricket::VideoFormat> formats;
70 ResetSupportedFormats(formats);
71 }
72
73 virtual cricket::CaptureState Start(
74 const cricket::VideoFormat& capture_format) {
75 if (test_without_formats_) {
76 std::vector<cricket::VideoFormat> formats;
77 formats.push_back(capture_format);
78 ResetSupportedFormats(formats);
79 }
80 return FakeVideoCapturer::Start(capture_format);
81 }
82
83 virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
84 cricket::VideoFormat* best_format) {
85 if (test_without_formats_) {
86 *best_format = desired;
87 return true;
88 }
perkja3ede6c2016-03-08 01:27:48 +010089 return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 }
91
92 private:
93 bool test_without_formats_;
94};
95
96class StateObserver : public ObserverInterface {
97 public:
perkja3ede6c2016-03-08 01:27:48 +010098 explicit StateObserver(VideoTrackSourceInterface* source)
99 : state_(source->state()), source_(source) {}
100 virtual void OnChanged() { state_ = source_->state(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 MediaSourceInterface::SourceState state() const { return state_; }
102
103 private:
104 MediaSourceInterface::SourceState state_;
perkja3ede6c2016-03-08 01:27:48 +0100105 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106};
107
perkja3ede6c2016-03-08 01:27:48 +0100108class VideoCapturerTrackSourceTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 protected:
perkja3ede6c2016-03-08 01:27:48 +0100110 VideoCapturerTrackSourceTest() { InitCapturer(false); }
Niels Möller60653ba2016-03-02 11:41:36 +0100111 void InitCapturer(bool is_screencast) {
kwibergd1fe2812016-04-27 06:47:29 -0700112 capturer_cleanup_ = std::unique_ptr<TestVideoCapturer>(
Niels Möller60653ba2016-03-02 11:41:36 +0100113 new TestVideoCapturer(is_screencast));
114 capturer_ = capturer_cleanup_.get();
115 }
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) {
123 // VideoSource take ownership of |capturer_|
perkja3ede6c2016-03-08 01:27:48 +0100124 source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(),
125 capturer_cleanup_.release(),
126 constraints, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000128 ASSERT_TRUE(source_.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000130 state_observer_.reset(new StateObserver(source_));
131 source_->RegisterObserver(state_observer_.get());
perkjf2880a02016-03-03 01:51:52 -0800132 source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 }
134
kwibergd1fe2812016-04-27 06:47:29 -0700135 std::unique_ptr<TestVideoCapturer> capturer_cleanup_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000136 TestVideoCapturer* capturer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 cricket::FakeVideoRenderer renderer_;
kwibergd1fe2812016-04-27 06:47:29 -0700138 std::unique_ptr<StateObserver> state_observer_;
perkja3ede6c2016-03-08 01:27:48 +0100139 rtc::scoped_refptr<VideoTrackSourceInterface> source_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000142// Test that a VideoSource transition to kLive state when the capture
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143// device have started and kEnded if it is stopped.
144// It also test that an output can receive video frames.
perkja3ede6c2016-03-08 01:27:48 +0100145TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100147 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
149 kMaxWaitMs);
150
151 ASSERT_TRUE(capturer_->CaptureFrame());
152 EXPECT_EQ(1, renderer_.num_rendered_frames());
153
154 capturer_->Stop();
155 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
156 kMaxWaitMs);
157}
158
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000159// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160// fails.
perkja3ede6c2016-03-08 01:27:48 +0100161TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
162 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
164 kMaxWaitMs);
165
166 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
167 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
168 kMaxWaitMs);
169}
170
171// Test that the capture output is CIF if we set max constraints to CIF.
172// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100173TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 FakeConstraints constraints;
175 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
176 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
177 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
178
perkja3ede6c2016-03-08 01:27:48 +0100179 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
181 kMaxWaitMs);
182 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
183 ASSERT_TRUE(format != NULL);
184 EXPECT_EQ(352, format->width);
185 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700186 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187}
188
189// Test that the capture output is 720P if the camera support it and the
190// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100191TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 FakeConstraints constraints;
193 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
194 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
195 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
196 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
197 1280.0 / 720);
198
perkja3ede6c2016-03-08 01:27:48 +0100199 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
201 kMaxWaitMs);
202 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
203 ASSERT_TRUE(format != NULL);
204 EXPECT_EQ(1280, format->width);
205 EXPECT_EQ(720, format->height);
206 EXPECT_EQ(30, format->framerate());
207}
208
209// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
210// require it even if an optional constraint request a higher resolution
211// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100212TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 FakeConstraints constraints;
214 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
215 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
216 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
217 640.0 / 480);
218 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
219
perkja3ede6c2016-03-08 01:27:48 +0100220 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
222 kMaxWaitMs);
223 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
224 ASSERT_TRUE(format != NULL);
225 EXPECT_EQ(640, format->width);
226 EXPECT_EQ(480, format->height);
227 EXPECT_EQ(30, format->framerate());
228}
229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230// Test that the source state transition to kEnded if the mandatory aspect ratio
231// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100232TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 FakeConstraints constraints;
234 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100235 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
237 kMaxWaitMs);
238}
239
240// Test that the source ignores an optional aspect ratio that is higher than
241// supported.
perkja3ede6c2016-03-08 01:27:48 +0100242TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 FakeConstraints constraints;
244 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
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 double aspect_ratio = static_cast<double>(format->width) / format->height;
251 EXPECT_LT(aspect_ratio, 2);
252}
253
254// Test that the source starts video with the default resolution if the
255// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100256TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 capturer_->TestWithoutCameraFormats();
258
perkja3ede6c2016-03-08 01:27:48 +0100259 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
261 kMaxWaitMs);
262 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
263 ASSERT_TRUE(format != NULL);
264 EXPECT_EQ(640, format->width);
265 EXPECT_EQ(480, format->height);
266 EXPECT_EQ(30, format->framerate());
267}
268
269// Test that the source can start the video and get the requested aspect ratio
270// if the camera doesn't support capability enumeration and the aspect ratio is
271// set.
perkja3ede6c2016-03-08 01:27:48 +0100272TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 capturer_->TestWithoutCameraFormats();
274
275 FakeConstraints constraints;
276 double requested_aspect_ratio = 640.0 / 360;
277 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
278 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
279 requested_aspect_ratio);
280
perkja3ede6c2016-03-08 01:27:48 +0100281 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
283 kMaxWaitMs);
284 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
285 double aspect_ratio = static_cast<double>(format->width) / format->height;
286 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
287}
288
289// Test that the source state transitions to kEnded if an unknown mandatory
290// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100291TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 FakeConstraints constraints;
293 constraints.AddMandatory("weird key", 640);
294
perkja3ede6c2016-03-08 01:27:48 +0100295 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
297 kMaxWaitMs);
298}
299
300// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100301TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 FakeConstraints constraints;
303 constraints.AddOptional("weird key", 640);
304
perkja3ede6c2016-03-08 01:27:48 +0100305 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
307 kMaxWaitMs);
308}
309
perkj0d3eef22016-03-09 02:39:17 +0100310TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 FakeConstraints constraints;
Perc0d31e92016-03-31 17:23:39 +0200312 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313
perkja3ede6c2016-03-08 01:27:48 +0100314 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
Perc0d31e92016-03-31 17:23:39 +0200316 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317}
318
perkj0d3eef22016-03-09 02:39:17 +0100319TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100321 CreateVideoCapturerSource(&constraints);
Perc0d31e92016-03-31 17:23:39 +0200322 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323}
324
perkj0d3eef22016-03-09 02:39:17 +0100325TEST_F(VideoCapturerTrackSourceTest,
326 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100328 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
329 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330
perkja3ede6c2016-03-08 01:27:48 +0100331 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332
Perc0d31e92016-03-31 17:23:39 +0200333 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334}
335
perkj0d3eef22016-03-09 02:39:17 +0100336TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100338 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 constraints.AddOptional("invalidKey", false);
340
perkja3ede6c2016-03-08 01:27:48 +0100341 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342
343 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100344 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200345 EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346}
347
perkj0d3eef22016-03-09 02:39:17 +0100348TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100350 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 constraints.AddMandatory("invalidKey", false);
352
perkja3ede6c2016-03-08 01:27:48 +0100353 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354
355 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100356 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200357 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
359
perkj0d3eef22016-03-09 02:39:17 +0100360TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100362 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
363 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364
perkja3ede6c2016-03-08 01:27:48 +0100365 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366
367 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100368 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200369
370 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
perkj0d3eef22016-03-09 02:39:17 +0100373TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 FakeConstraints constraints;
375 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100376 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100378 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379
perkja3ede6c2016-03-08 01:27:48 +0100380 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381
382 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100383 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200384 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385}
386
perkja3ede6c2016-03-08 01:27:48 +0100387TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 FakeConstraints constraints;
389 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
390 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
391 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
392
perkja3ede6c2016-03-08 01:27:48 +0100393 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
394 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
perkja3ede6c2016-03-08 01:27:48 +0100396 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
398 kMaxWaitMs);
399 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
400 ASSERT_TRUE(format != NULL);
401 EXPECT_EQ(352, format->width);
402 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700403 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
Perc0d31e92016-03-31 17:23:39 +0200405 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
408// Tests that the source starts video with the default resolution for
409// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100410TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100411 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413
perkja3ede6c2016-03-08 01:27:48 +0100414 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100415 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
417 kMaxWaitMs);
418 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
419 ASSERT_TRUE(format != NULL);
420 EXPECT_EQ(640, format->width);
421 EXPECT_EQ(480, format->height);
422 EXPECT_EQ(30, format->framerate());
423}
424
425// Tests that the source starts video with the max width and height set by
426// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100427TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 FakeConstraints constraints;
429 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
430 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
431
Niels Möller60653ba2016-03-02 11:41:36 +0100432 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
perkja3ede6c2016-03-08 01:27:48 +0100435 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100436 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
438 kMaxWaitMs);
439 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
440 ASSERT_TRUE(format != NULL);
441 EXPECT_EQ(480, format->width);
442 EXPECT_EQ(270, format->height);
443 EXPECT_EQ(30, format->framerate());
444}
445
perkja3ede6c2016-03-08 01:27:48 +0100446TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 FakeConstraints constraints;
448 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
449
perkja3ede6c2016-03-08 01:27:48 +0100450 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
452 kMaxWaitMs);
453 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
454}
455
perkja3ede6c2016-03-08 01:27:48 +0100456TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 FakeConstraints constraints;
458 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
459
perkja3ede6c2016-03-08 01:27:48 +0100460 CreateVideoCapturerSource(&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);
perkjfa10b552016-10-02 23:45:26 -0700465 EXPECT_EQ(1, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}