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