blob: d8f4b0c761dc7837e31ce9d7bc39769df77ff449 [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
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000160// Test that a VideoSource transition to kEnded if the capture device
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// fails.
perkja3ede6c2016-03-08 01:27:48 +0100162TEST_F(VideoCapturerTrackSourceTest, CameraFailed) {
163 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
165 kMaxWaitMs);
166
167 capturer_->SignalStateChange(capturer_, cricket::CS_FAILED);
168 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
169 kMaxWaitMs);
170}
171
172// Test that the capture output is CIF if we set max constraints to CIF.
173// and the capture device support CIF.
perkja3ede6c2016-03-08 01:27:48 +0100174TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 FakeConstraints constraints;
176 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
177 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
178 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5);
179
perkja3ede6c2016-03-08 01:27:48 +0100180 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
182 kMaxWaitMs);
183 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
184 ASSERT_TRUE(format != NULL);
185 EXPECT_EQ(352, format->width);
186 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700187 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188}
189
190// Test that the capture output is 720P if the camera support it and the
191// optional constraint is set to 720P.
perkja3ede6c2016-03-08 01:27:48 +0100192TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 FakeConstraints constraints;
194 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
195 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
196 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
197 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio,
198 1280.0 / 720);
199
perkja3ede6c2016-03-08 01:27:48 +0100200 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
202 kMaxWaitMs);
203 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
204 ASSERT_TRUE(format != NULL);
205 EXPECT_EQ(1280, format->width);
206 EXPECT_EQ(720, format->height);
207 EXPECT_EQ(30, format->framerate());
208}
209
210// Test that the capture output have aspect ratio 4:3 if a mandatory constraint
211// require it even if an optional constraint request a higher resolution
212// that don't have this aspect ratio.
perkja3ede6c2016-03-08 01:27:48 +0100213TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 FakeConstraints constraints;
215 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
216 constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480);
217 constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio,
218 640.0 / 480);
219 constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280);
220
perkja3ede6c2016-03-08 01:27:48 +0100221 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
223 kMaxWaitMs);
224 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
225 ASSERT_TRUE(format != NULL);
226 EXPECT_EQ(640, format->width);
227 EXPECT_EQ(480, format->height);
228 EXPECT_EQ(30, format->framerate());
229}
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231// Test that the source state transition to kEnded if the mandatory aspect ratio
232// is set higher than supported.
perkja3ede6c2016-03-08 01:27:48 +0100233TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 FakeConstraints constraints;
235 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100236 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
238 kMaxWaitMs);
239}
240
241// Test that the source ignores an optional aspect ratio that is higher than
242// supported.
perkja3ede6c2016-03-08 01:27:48 +0100243TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 FakeConstraints constraints;
245 constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2);
perkja3ede6c2016-03-08 01:27:48 +0100246 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
248 kMaxWaitMs);
249 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
250 ASSERT_TRUE(format != NULL);
251 double aspect_ratio = static_cast<double>(format->width) / format->height;
252 EXPECT_LT(aspect_ratio, 2);
253}
254
255// Test that the source starts video with the default resolution if the
256// camera doesn't support capability enumeration and there are no constraints.
perkja3ede6c2016-03-08 01:27:48 +0100257TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 capturer_->TestWithoutCameraFormats();
259
perkja3ede6c2016-03-08 01:27:48 +0100260 CreateVideoCapturerSource();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
262 kMaxWaitMs);
263 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
264 ASSERT_TRUE(format != NULL);
265 EXPECT_EQ(640, format->width);
266 EXPECT_EQ(480, format->height);
267 EXPECT_EQ(30, format->framerate());
268}
269
270// Test that the source can start the video and get the requested aspect ratio
271// if the camera doesn't support capability enumeration and the aspect ratio is
272// set.
perkja3ede6c2016-03-08 01:27:48 +0100273TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 capturer_->TestWithoutCameraFormats();
275
276 FakeConstraints constraints;
277 double requested_aspect_ratio = 640.0 / 360;
278 constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640);
279 constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio,
280 requested_aspect_ratio);
281
perkja3ede6c2016-03-08 01:27:48 +0100282 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
284 kMaxWaitMs);
285 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
286 double aspect_ratio = static_cast<double>(format->width) / format->height;
287 EXPECT_LE(requested_aspect_ratio, aspect_ratio);
288}
289
290// Test that the source state transitions to kEnded if an unknown mandatory
291// constraint is found.
perkja3ede6c2016-03-08 01:27:48 +0100292TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 FakeConstraints constraints;
294 constraints.AddMandatory("weird key", 640);
295
perkja3ede6c2016-03-08 01:27:48 +0100296 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
298 kMaxWaitMs);
299}
300
301// Test that the source ignores an unknown optional constraint.
perkja3ede6c2016-03-08 01:27:48 +0100302TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 FakeConstraints constraints;
304 constraints.AddOptional("weird key", 640);
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}
310
perkj0d3eef22016-03-09 02:39:17 +0100311TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 FakeConstraints constraints;
Perc0d31e92016-03-31 17:23:39 +0200313 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314
perkja3ede6c2016-03-08 01:27:48 +0100315 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
Perc0d31e92016-03-31 17:23:39 +0200317 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318}
319
perkj0d3eef22016-03-09 02:39:17 +0100320TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100322 CreateVideoCapturerSource(&constraints);
Perc0d31e92016-03-31 17:23:39 +0200323 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324}
325
perkj0d3eef22016-03-09 02:39:17 +0100326TEST_F(VideoCapturerTrackSourceTest,
327 MandatoryDenoisingConstraintOverridesOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100329 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
330 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331
perkja3ede6c2016-03-08 01:27:48 +0100332 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
Perc0d31e92016-03-31 17:23:39 +0200334 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335}
336
perkj0d3eef22016-03-09 02:39:17 +0100337TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 FakeConstraints constraints;
perkj0d3eef22016-03-09 02:39:17 +0100339 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 constraints.AddOptional("invalidKey", false);
341
perkja3ede6c2016-03-08 01:27:48 +0100342 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343
344 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100345 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200346 EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347}
348
perkj0d3eef22016-03-09 02:39:17 +0100349TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100351 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 constraints.AddMandatory("invalidKey", false);
353
perkja3ede6c2016-03-08 01:27:48 +0100354 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
356 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100357 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200358 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359}
360
perkj0d3eef22016-03-09 02:39:17 +0100361TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 FakeConstraints constraints;
perkja3ede6c2016-03-08 01:27:48 +0100363 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction,
364 "not a boolean");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
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
371 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
perkj0d3eef22016-03-09 02:39:17 +0100374TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 FakeConstraints constraints;
376 // Optional constraints should be ignored if the mandatory constraints fail.
perkja3ede6c2016-03-08 01:27:48 +0100377 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 // Values are case-sensitive and must be all lower-case.
perkja3ede6c2016-03-08 01:27:48 +0100379 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
perkja3ede6c2016-03-08 01:27:48 +0100381 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382
383 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
perkja3ede6c2016-03-08 01:27:48 +0100384 kMaxWaitMs);
Perc0d31e92016-03-31 17:23:39 +0200385 EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
perkja3ede6c2016-03-08 01:27:48 +0100388TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 FakeConstraints constraints;
390 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352);
391 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288);
392 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5);
393
perkja3ede6c2016-03-08 01:27:48 +0100394 constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false);
395 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
perkja3ede6c2016-03-08 01:27:48 +0100397 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
399 kMaxWaitMs);
400 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
401 ASSERT_TRUE(format != NULL);
402 EXPECT_EQ(352, format->width);
403 EXPECT_EQ(288, format->height);
perkjfa10b552016-10-02 23:45:26 -0700404 EXPECT_EQ(5, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405
Perc0d31e92016-03-31 17:23:39 +0200406 EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407}
408
409// Tests that the source starts video with the default resolution for
410// screencast if no constraint is set.
perkja3ede6c2016-03-08 01:27:48 +0100411TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) {
Niels Möller60653ba2016-03-02 11:41:36 +0100412 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414
perkja3ede6c2016-03-08 01:27:48 +0100415 CreateVideoCapturerSource();
perkj0d3eef22016-03-09 02:39:17 +0100416 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
418 kMaxWaitMs);
419 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
420 ASSERT_TRUE(format != NULL);
421 EXPECT_EQ(640, format->width);
422 EXPECT_EQ(480, format->height);
423 EXPECT_EQ(30, format->framerate());
424}
425
426// Tests that the source starts video with the max width and height set by
427// constraints for screencast.
perkja3ede6c2016-03-08 01:27:48 +0100428TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 FakeConstraints constraints;
430 constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480);
431 constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270);
432
Niels Möller60653ba2016-03-02 11:41:36 +0100433 InitScreencast();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 capturer_->TestWithoutCameraFormats();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435
perkja3ede6c2016-03-08 01:27:48 +0100436 CreateVideoCapturerSource(&constraints);
perkj0d3eef22016-03-09 02:39:17 +0100437 ASSERT_TRUE(source_->is_screencast());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
439 kMaxWaitMs);
440 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
441 ASSERT_TRUE(format != NULL);
442 EXPECT_EQ(480, format->width);
443 EXPECT_EQ(270, format->height);
444 EXPECT_EQ(30, format->framerate());
445}
446
nisse0d14c6a2016-09-14 12:03:17 -0700447TEST_F(VideoCapturerTrackSourceTest, DenoisingDefault) {
448 CreateVideoCapturerSource();
449 EXPECT_FALSE(source_->needs_denoising());
450}
451
452TEST_F(VideoCapturerTrackSourceTest, DenoisingConstraintOn) {
453 FakeConstraints constraints;
454 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
455 CreateVideoCapturerSource(&constraints);
456 ASSERT_TRUE(source_->needs_denoising());
457 EXPECT_TRUE(*source_->needs_denoising());
458}
459
460TEST_F(VideoCapturerTrackSourceTest, DenoisingCapturerOff) {
461 capturer_->SetNeedsDenoising(rtc::Optional<bool>(false));
462 CreateVideoCapturerSource();
463 ASSERT_TRUE(source_->needs_denoising());
464 EXPECT_FALSE(*source_->needs_denoising());
465}
466
467TEST_F(VideoCapturerTrackSourceTest, DenoisingConstraintOverridesCapturer) {
468 capturer_->SetNeedsDenoising(rtc::Optional<bool>(false));
469 FakeConstraints constraints;
470 constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true);
471 CreateVideoCapturerSource(&constraints);
472 ASSERT_TRUE(source_->needs_denoising());
473 EXPECT_TRUE(*source_->needs_denoising());
474}
475
perkja3ede6c2016-03-08 01:27:48 +0100476TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 FakeConstraints constraints;
478 constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5);
479
perkja3ede6c2016-03-08 01:27:48 +0100480 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(),
482 kMaxWaitMs);
483 ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL);
484}
485
perkja3ede6c2016-03-08 01:27:48 +0100486TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 FakeConstraints constraints;
488 constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5);
489
perkja3ede6c2016-03-08 01:27:48 +0100490 CreateVideoCapturerSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(),
492 kMaxWaitMs);
493 const cricket::VideoFormat* format = capturer_->GetCaptureFormat();
494 ASSERT_TRUE(format != NULL);
perkjfa10b552016-10-02 23:45:26 -0700495 EXPECT_EQ(1, format->framerate());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496}