henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 15 | #include "webrtc/api/test/fakeconstraints.h" |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 16 | #include "webrtc/api/videocapturertracksource.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 17 | #include "webrtc/base/gunit.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #include "webrtc/media/base/fakemediaengine.h" |
| 19 | #include "webrtc/media/base/fakevideocapturer.h" |
| 20 | #include "webrtc/media/base/fakevideorenderer.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 21 | #include "webrtc/media/engine/webrtcvideoframe.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | using webrtc::FakeConstraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 24 | using webrtc::VideoCapturerTrackSource; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | using webrtc::MediaConstraintsInterface; |
| 26 | using webrtc::MediaSourceInterface; |
| 27 | using webrtc::ObserverInterface; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 28 | using webrtc::VideoTrackSourceInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Max wait time for a test. |
| 33 | const int kMaxWaitMs = 100; |
| 34 | |
| 35 | } // anonymous namespace |
| 36 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | // 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. |
| 41 | class TestVideoCapturer : public cricket::FakeVideoCapturer { |
| 42 | public: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 43 | explicit TestVideoCapturer(bool is_screencast) |
| 44 | : FakeVideoCapturer(is_screencast), test_without_formats_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | std::vector<cricket::VideoFormat> formats; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 46 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | 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 | } |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 90 | return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | bool test_without_formats_; |
| 95 | }; |
| 96 | |
| 97 | class StateObserver : public ObserverInterface { |
| 98 | public: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 99 | explicit StateObserver(VideoTrackSourceInterface* source) |
| 100 | : state_(source->state()), source_(source) {} |
| 101 | virtual void OnChanged() { state_ = source_->state(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | MediaSourceInterface::SourceState state() const { return state_; } |
| 103 | |
| 104 | private: |
| 105 | MediaSourceInterface::SourceState state_; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 106 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 109 | class VideoCapturerTrackSourceTest : public testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | protected: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 111 | VideoCapturerTrackSourceTest() { InitCapturer(false); } |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 112 | void InitCapturer(bool is_screencast) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 113 | capturer_cleanup_ = std::unique_ptr<TestVideoCapturer>( |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 114 | new TestVideoCapturer(is_screencast)); |
| 115 | capturer_ = capturer_cleanup_.get(); |
| 116 | } |
| 117 | |
| 118 | void InitScreencast() { InitCapturer(true); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 120 | void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 122 | void CreateVideoCapturerSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | const webrtc::MediaConstraintsInterface* constraints) { |
| 124 | // VideoSource take ownership of |capturer_| |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 125 | source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(), |
| 126 | capturer_cleanup_.release(), |
| 127 | constraints, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 129 | ASSERT_TRUE(source_.get() != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 131 | state_observer_.reset(new StateObserver(source_)); |
| 132 | source_->RegisterObserver(state_observer_.get()); |
perkj | f2880a0 | 2016-03-03 01:51:52 -0800 | [diff] [blame] | 133 | source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 136 | std::unique_ptr<TestVideoCapturer> capturer_cleanup_; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 137 | TestVideoCapturer* capturer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | cricket::FakeVideoRenderer renderer_; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 139 | std::unique_ptr<StateObserver> state_observer_; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 140 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 143 | // Test that a VideoSource transition to kLive state when the capture |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | // device have started and kEnded if it is stopped. |
| 145 | // It also test that an output can receive video frames. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 146 | TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | // Initialize without constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 148 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | 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.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 160 | // Test that a VideoSource transition to kEnded if the capture device |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | // fails. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 162 | TEST_F(VideoCapturerTrackSourceTest, CameraFailed) { |
| 163 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 174 | TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | FakeConstraints constraints; |
| 176 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 177 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 178 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 179 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 180 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | 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); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame^] | 187 | EXPECT_EQ(5, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Test that the capture output is 720P if the camera support it and the |
| 191 | // optional constraint is set to 720P. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 192 | TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 193 | 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 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 200 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 213 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | 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 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 221 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | // Test that the source state transition to kEnded if the mandatory aspect ratio |
| 232 | // is set higher than supported. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 233 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | FakeConstraints constraints; |
| 235 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 236 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 243 | TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | FakeConstraints constraints; |
| 245 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 246 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 257 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 258 | capturer_->TestWithoutCameraFormats(); |
| 259 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 260 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 261 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 273 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | 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 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 282 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 292 | TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 293 | FakeConstraints constraints; |
| 294 | constraints.AddMandatory("weird key", 640); |
| 295 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 296 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 297 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 298 | kMaxWaitMs); |
| 299 | } |
| 300 | |
| 301 | // Test that the source ignores an unknown optional constraint. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 302 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 303 | FakeConstraints constraints; |
| 304 | constraints.AddOptional("weird key", 640); |
| 305 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 306 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 308 | kMaxWaitMs); |
| 309 | } |
| 310 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 311 | TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | FakeConstraints constraints; |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 313 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 314 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 315 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 316 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 317 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 318 | } |
| 319 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 320 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 321 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 322 | CreateVideoCapturerSource(&constraints); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 323 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | } |
| 325 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 326 | TEST_F(VideoCapturerTrackSourceTest, |
| 327 | MandatoryDenoisingConstraintOverridesOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 329 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 330 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 332 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 334 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 335 | } |
| 336 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 337 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 338 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 339 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 340 | constraints.AddOptional("invalidKey", false); |
| 341 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 342 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 343 | |
| 344 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 345 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 346 | EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 347 | } |
| 348 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 349 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 350 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 351 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | constraints.AddMandatory("invalidKey", false); |
| 353 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 354 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 355 | |
| 356 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 357 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 358 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 359 | } |
| 360 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 361 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 363 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, |
| 364 | "not a boolean"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 366 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | |
| 368 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 369 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 370 | |
| 371 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 372 | } |
| 373 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 374 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | FakeConstraints constraints; |
| 376 | // Optional constraints should be ignored if the mandatory constraints fail. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 377 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 378 | // Values are case-sensitive and must be all lower-case. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 379 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 381 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | |
| 383 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 384 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 385 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 386 | } |
| 387 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 388 | TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 389 | FakeConstraints constraints; |
| 390 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 391 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 392 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 393 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 394 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 395 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 396 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 397 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 398 | 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); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame^] | 404 | EXPECT_EQ(5, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 405 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 406 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Tests that the source starts video with the default resolution for |
| 410 | // screencast if no constraint is set. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 411 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 412 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 413 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 414 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 415 | CreateVideoCapturerSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 416 | ASSERT_TRUE(source_->is_screencast()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 417 | 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. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 428 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | FakeConstraints constraints; |
| 430 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); |
| 431 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); |
| 432 | |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 433 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 434 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 435 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 436 | CreateVideoCapturerSource(&constraints); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 437 | ASSERT_TRUE(source_->is_screencast()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 438 | 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 | |
nisse | 0d14c6a | 2016-09-14 12:03:17 -0700 | [diff] [blame] | 447 | TEST_F(VideoCapturerTrackSourceTest, DenoisingDefault) { |
| 448 | CreateVideoCapturerSource(); |
| 449 | EXPECT_FALSE(source_->needs_denoising()); |
| 450 | } |
| 451 | |
| 452 | TEST_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 | |
| 460 | TEST_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 | |
| 467 | TEST_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 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 476 | TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 477 | FakeConstraints constraints; |
| 478 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 479 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 480 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 482 | kMaxWaitMs); |
| 483 | ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); |
| 484 | } |
| 485 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 486 | TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 487 | FakeConstraints constraints; |
| 488 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 489 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 490 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 491 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 492 | kMaxWaitMs); |
| 493 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 494 | ASSERT_TRUE(format != NULL); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame^] | 495 | EXPECT_EQ(1, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 496 | } |