blob: 6b0d07b0e085f99f5f981037d461cb515a19d564 [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/test/fakeconstraints.h"
perkja3ede6c2016-03-08 01:27:48 +010015#include "webrtc/api/videocapturertracksource.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000016#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080017#include "webrtc/media/base/fakemediaengine.h"
18#include "webrtc/media/base/fakevideocapturer.h"
19#include "webrtc/media/base/fakevideorenderer.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010020#include "webrtc/media/engine/webrtcvideoframe.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) {
112 capturer_cleanup_ = rtc::scoped_ptr<TestVideoCapturer>(
113 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000135 rtc::scoped_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_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000138 rtc::scoped_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
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000159// Test that a VideoSource can be stopped and restarted.
perkja3ede6c2016-03-08 01:27:48 +0100160TEST_F(VideoCapturerTrackSourceTest, StopRestart) {
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000161 // Initialize without constraints.
perkja3ede6c2016-03-08 01:27:48 +0100162 CreateVideoCapturerSource();
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000163 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
164 kMaxWaitMs);
165
166 ASSERT_TRUE(capturer_->CaptureFrame());
167 EXPECT_EQ(1, renderer_.num_rendered_frames());
168
169 source_->Stop();
170 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
171 kMaxWaitMs);
172
173 source_->Restart();
174 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
175 kMaxWaitMs);
176
177 ASSERT_TRUE(capturer_->CaptureFrame());
178 EXPECT_EQ(2, renderer_.num_rendered_frames());
179
180 source_->Stop();
181}
182
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000183// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184// fails.
perkja3ede6c2016-03-08 01:27:48 +0100185TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
186 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
188 kMaxWaitMs);
189
190 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
191 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
192 kMaxWaitMs);
193}
194
195// Test that the capture output is CIF if we set max constraints to CIF.
196// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100197TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 FakeConstraints constraints;
199 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
200 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
201 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
202
perkja3ede6c2016-03-08 01:27:48 +0100203 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
205 kMaxWaitMs);
206 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
207 ASSERT_TRUE(format != NULL);
208 EXPECT_EQ(352, format->width);
209 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000210 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
213// Test that the capture output is 720P if the camera support it and the
214// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100215TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 FakeConstraints constraints;
217 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
218 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
219 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
220 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
221 1280.0 / 720);
222
perkja3ede6c2016-03-08 01:27:48 +0100223 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
225 kMaxWaitMs);
226 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
227 ASSERT_TRUE(format != NULL);
228 EXPECT_EQ(1280, format->width);
229 EXPECT_EQ(720, format->height);
230 EXPECT_EQ(30, format->framerate());
231}
232
233// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
234// require it even if an optional constraint request a higher resolution
235// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100236TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 FakeConstraints constraints;
238 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
239 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
240 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
241 640.0 / 480);
242 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
243
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 EXPECT_EQ(640, format->width);
250 EXPECT_EQ(480, format->height);
251 EXPECT_EQ(30, format->framerate());
252}
253
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254// Test that the source state transition to kEnded if the mandatory aspect ratio
255// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100256TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 FakeConstraints constraints;
258 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100259 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
261 kMaxWaitMs);
262}
263
264// Test that the source ignores an optional aspect ratio that is higher than
265// supported.
perkja3ede6c2016-03-08 01:27:48 +0100266TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 FakeConstraints constraints;
268 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100269 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
271 kMaxWaitMs);
272 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
273 ASSERT_TRUE(format != NULL);
274 double aspect_ratio = static_cast<double>(format->width) / format->height;
275 EXPECT_LT(aspect_ratio, 2);
276}
277
278// Test that the source starts video with the default resolution if the
279// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100280TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 capturer_->TestWithoutCameraFormats();
282
perkja3ede6c2016-03-08 01:27:48 +0100283 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
285 kMaxWaitMs);
286 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
287 ASSERT_TRUE(format != NULL);
288 EXPECT_EQ(640, format->width);
289 EXPECT_EQ(480, format->height);
290 EXPECT_EQ(30, format->framerate());
291}
292
293// Test that the source can start the video and get the requested aspect ratio
294// if the camera doesn't support capability enumeration and the aspect ratio is
295// set.
perkja3ede6c2016-03-08 01:27:48 +0100296TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 capturer_->TestWithoutCameraFormats();
298
299 FakeConstraints constraints;
300 double requested_aspect_ratio = 640.0 / 360;
301 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
302 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
303 requested_aspect_ratio);
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 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
309 double aspect_ratio = static_cast<double>(format->width) / format->height;
310 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
311}
312
313// Test that the source state transitions to kEnded if an unknown mandatory
314// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100315TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 FakeConstraints constraints;
317 constraints.AddMandatory("weird key", 640);
318
perkja3ede6c2016-03-08 01:27:48 +0100319 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
321 kMaxWaitMs);
322}
323
324// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100325TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 FakeConstraints constraints;
327 constraints.AddOptional("weird key", 640);
328
perkja3ede6c2016-03-08 01:27:48 +0100329 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
331 kMaxWaitMs);
332}
333
perkj0d3eef22016-03-09 02:39:17 +0100334TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 FakeConstraints constraints;
Perc0d31e92016-03-31 17:23:39 +0200336 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337
perkja3ede6c2016-03-08 01:27:48 +0100338 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339
Perc0d31e92016-03-31 17:23:39 +0200340 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341}
342
perkj0d3eef22016-03-09 02:39:17 +0100343TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100345 CreateVideoCapturerSource(&constraints);
Perc0d31e92016-03-31 17:23:39 +0200346 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347}
348
perkj0d3eef22016-03-09 02:39:17 +0100349TEST_F(VideoCapturerTrackSourceTest,
350 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100352 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
353 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354
perkja3ede6c2016-03-08 01:27:48 +0100355 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
Perc0d31e92016-03-31 17:23:39 +0200357 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
359
perkj0d3eef22016-03-09 02:39:17 +0100360TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100362 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 constraints.AddOptional("invalidKey", false);
364
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 EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
perkj0d3eef22016-03-09 02:39:17 +0100372TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100374 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 constraints.AddMandatory("invalidKey", false);
376
perkja3ede6c2016-03-08 01:27:48 +0100377 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
379 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100380 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200381 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382}
383
perkj0d3eef22016-03-09 02:39:17 +0100384TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100386 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
387 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388
perkja3ede6c2016-03-08 01:27:48 +0100389 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390
391 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100392 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200393
394 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395}
396
perkj0d3eef22016-03-09 02:39:17 +0100397TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 FakeConstraints constraints;
399 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100400 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100402 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403
perkja3ede6c2016-03-08 01:27:48 +0100404 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405
406 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100407 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200408 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409}
410
perkja3ede6c2016-03-08 01:27:48 +0100411TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 FakeConstraints constraints;
413 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
414 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
415 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
416
perkja3ede6c2016-03-08 01:27:48 +0100417 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
418 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419
perkja3ede6c2016-03-08 01:27:48 +0100420 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
422 kMaxWaitMs);
423 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
424 ASSERT_TRUE(format != NULL);
425 EXPECT_EQ(352, format->width);
426 EXPECT_EQ(288, format->height);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000427 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428
Perc0d31e92016-03-31 17:23:39 +0200429 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430}
431
432// Tests that the source starts video with the default resolution for
433// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100434TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100435 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437
perkja3ede6c2016-03-08 01:27:48 +0100438 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100439 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
441 kMaxWaitMs);
442 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
443 ASSERT_TRUE(format != NULL);
444 EXPECT_EQ(640, format->width);
445 EXPECT_EQ(480, format->height);
446 EXPECT_EQ(30, format->framerate());
447}
448
449// Tests that the source starts video with the max width and height set by
450// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100451TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 FakeConstraints constraints;
453 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
454 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
455
Niels Möller60653ba2016-03-02 11:41:36 +0100456 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
perkja3ede6c2016-03-08 01:27:48 +0100459 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100460 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
462 kMaxWaitMs);
463 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
464 ASSERT_TRUE(format != NULL);
465 EXPECT_EQ(480, format->width);
466 EXPECT_EQ(270, format->height);
467 EXPECT_EQ(30, format->framerate());
468}
469
perkja3ede6c2016-03-08 01:27:48 +0100470TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 FakeConstraints constraints;
472 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
473
perkja3ede6c2016-03-08 01:27:48 +0100474 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
476 kMaxWaitMs);
477 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
478}
479
perkja3ede6c2016-03-08 01:27:48 +0100480TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 FakeConstraints constraints;
482 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
483
perkja3ede6c2016-03-08 01:27:48 +0100484 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
486 kMaxWaitMs);
487 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
488 ASSERT_TRUE(format != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000489 EXPECT_EQ(30, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}