henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 09:53:12 | [diff] [blame] | 15 | #include "webrtc/api/test/fakeconstraints.h" |
kjellander | a96e2d7 | 2016-02-05 07:52:28 | [diff] [blame] | 16 | #include "webrtc/media/base/fakemediaengine.h" |
| 17 | #include "webrtc/media/base/fakevideocapturer.h" |
| 18 | #include "webrtc/media/base/fakevideorenderer.h" |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 19 | #include "webrtc/pc/videocapturertracksource.h" |
Edward Lemur | c20978e | 2017-07-06 17:44:34 | [diff] [blame] | 20 | #include "webrtc/rtc_base/gunit.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
| 22 | using webrtc::FakeConstraints; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 23 | using webrtc::VideoCapturerTrackSource; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 24 | using webrtc::MediaConstraintsInterface; |
| 25 | using webrtc::MediaSourceInterface; |
| 26 | using webrtc::ObserverInterface; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 27 | using webrtc::VideoTrackSourceInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [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 | [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 00:27:48 | [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 | [diff] [blame] | 44 | std::vector<cricket::VideoFormat> formats; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [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 | [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 00:27:48 | [diff] [blame] | 89 | return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [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 00:27:48 | [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 | [diff] [blame] | 101 | MediaSourceInterface::SourceState state() const { return state_; } |
| 102 | |
| 103 | private: |
| 104 | MediaSourceInterface::SourceState state_; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 105 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 106 | }; |
| 107 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 108 | class VideoCapturerTrackSourceTest : public testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 109 | protected: |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 110 | VideoCapturerTrackSourceTest() { InitCapturer(false); } |
Niels Möller | 60653ba | 2016-03-02 10:41:36 | [diff] [blame] | 111 | void InitCapturer(bool is_screencast) { |
deadbeef | 112b2e9 | 2017-02-11 04:13:37 | [diff] [blame] | 112 | capturer_ = new TestVideoCapturer(is_screencast); |
| 113 | capturer_cleanup_.reset(capturer_); |
Niels Möller | 60653ba | 2016-03-02 10:41:36 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void InitScreencast() { InitCapturer(true); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 117 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 118 | void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 119 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 120 | void CreateVideoCapturerSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 121 | const webrtc::MediaConstraintsInterface* constraints) { |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 122 | source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(), |
deadbeef | 112b2e9 | 2017-02-11 04:13:37 | [diff] [blame] | 123 | std::move(capturer_cleanup_), |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 124 | constraints, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 125 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 | [diff] [blame] | 126 | ASSERT_TRUE(source_.get() != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 127 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 | [diff] [blame] | 128 | state_observer_.reset(new StateObserver(source_)); |
| 129 | source_->RegisterObserver(state_observer_.get()); |
perkj | f2880a0 | 2016-03-03 09:51:52 | [diff] [blame] | 130 | source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 131 | } |
| 132 | |
deadbeef | 112b2e9 | 2017-02-11 04:13:37 | [diff] [blame] | 133 | std::unique_ptr<cricket::VideoCapturer> capturer_cleanup_; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 | [diff] [blame] | 134 | TestVideoCapturer* capturer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 135 | cricket::FakeVideoRenderer renderer_; |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 136 | std::unique_ptr<StateObserver> state_observer_; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 137 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 138 | }; |
| 139 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 | [diff] [blame] | 140 | // Test that a VideoSource transition to kLive state when the capture |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 141 | // device have started and kEnded if it is stopped. |
| 142 | // It also test that an output can receive video frames. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 143 | TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 144 | // Initialize without constraints. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 145 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 146 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 147 | kMaxWaitMs); |
| 148 | |
| 149 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 150 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 151 | |
| 152 | capturer_->Stop(); |
| 153 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 154 | kMaxWaitMs); |
| 155 | } |
| 156 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 | [diff] [blame] | 157 | // Test that a VideoSource transition to kEnded if the capture device |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 158 | // fails. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 159 | TEST_F(VideoCapturerTrackSourceTest, CameraFailed) { |
| 160 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 161 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 162 | kMaxWaitMs); |
| 163 | |
| 164 | capturer_->SignalStateChange(capturer_, cricket::CS_FAILED); |
| 165 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 166 | kMaxWaitMs); |
| 167 | } |
| 168 | |
| 169 | // Test that the capture output is CIF if we set max constraints to CIF. |
| 170 | // and the capture device support CIF. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 171 | TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 172 | FakeConstraints constraints; |
| 173 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 174 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 175 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 176 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 177 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 178 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 179 | kMaxWaitMs); |
| 180 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 181 | ASSERT_TRUE(format != NULL); |
| 182 | EXPECT_EQ(352, format->width); |
| 183 | EXPECT_EQ(288, format->height); |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 184 | EXPECT_EQ(5, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Test that the capture output is 720P if the camera support it and the |
| 188 | // optional constraint is set to 720P. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 189 | TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 190 | FakeConstraints constraints; |
| 191 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 192 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 193 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 194 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, |
| 195 | 1280.0 / 720); |
| 196 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 197 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 198 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 199 | kMaxWaitMs); |
| 200 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 201 | ASSERT_TRUE(format != NULL); |
| 202 | EXPECT_EQ(1280, format->width); |
| 203 | EXPECT_EQ(720, format->height); |
| 204 | EXPECT_EQ(30, format->framerate()); |
| 205 | } |
| 206 | |
| 207 | // Test that the capture output have aspect ratio 4:3 if a mandatory constraint |
| 208 | // require it even if an optional constraint request a higher resolution |
| 209 | // that don't have this aspect ratio. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 210 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 211 | FakeConstraints constraints; |
| 212 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 213 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 214 | constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio, |
| 215 | 640.0 / 480); |
| 216 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 217 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 218 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 219 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 220 | kMaxWaitMs); |
| 221 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 222 | ASSERT_TRUE(format != NULL); |
| 223 | EXPECT_EQ(640, format->width); |
| 224 | EXPECT_EQ(480, format->height); |
| 225 | EXPECT_EQ(30, format->framerate()); |
| 226 | } |
| 227 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 228 | // Test that the source state transition to kEnded if the mandatory aspect ratio |
| 229 | // is set higher than supported. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 230 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 231 | FakeConstraints constraints; |
| 232 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 233 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 234 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 235 | kMaxWaitMs); |
| 236 | } |
| 237 | |
| 238 | // Test that the source ignores an optional aspect ratio that is higher than |
| 239 | // supported. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 240 | TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 241 | FakeConstraints constraints; |
| 242 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 243 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 244 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 245 | kMaxWaitMs); |
| 246 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 247 | ASSERT_TRUE(format != NULL); |
| 248 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 249 | EXPECT_LT(aspect_ratio, 2); |
| 250 | } |
| 251 | |
| 252 | // Test that the source starts video with the default resolution if the |
| 253 | // camera doesn't support capability enumeration and there are no constraints. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 254 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 255 | capturer_->TestWithoutCameraFormats(); |
| 256 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 257 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 258 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 259 | kMaxWaitMs); |
| 260 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 261 | ASSERT_TRUE(format != NULL); |
| 262 | EXPECT_EQ(640, format->width); |
| 263 | EXPECT_EQ(480, format->height); |
| 264 | EXPECT_EQ(30, format->framerate()); |
| 265 | } |
| 266 | |
| 267 | // Test that the source can start the video and get the requested aspect ratio |
| 268 | // if the camera doesn't support capability enumeration and the aspect ratio is |
| 269 | // set. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 270 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 271 | capturer_->TestWithoutCameraFormats(); |
| 272 | |
| 273 | FakeConstraints constraints; |
| 274 | double requested_aspect_ratio = 640.0 / 360; |
| 275 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 276 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, |
| 277 | requested_aspect_ratio); |
| 278 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 279 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 280 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 281 | kMaxWaitMs); |
| 282 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 283 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 284 | EXPECT_LE(requested_aspect_ratio, aspect_ratio); |
| 285 | } |
| 286 | |
| 287 | // Test that the source state transitions to kEnded if an unknown mandatory |
| 288 | // constraint is found. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 289 | TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 290 | FakeConstraints constraints; |
| 291 | constraints.AddMandatory("weird key", 640); |
| 292 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 293 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 294 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 295 | kMaxWaitMs); |
| 296 | } |
| 297 | |
| 298 | // Test that the source ignores an unknown optional constraint. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 299 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 300 | FakeConstraints constraints; |
| 301 | constraints.AddOptional("weird key", 640); |
| 302 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 303 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 304 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 305 | kMaxWaitMs); |
| 306 | } |
| 307 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 308 | TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 309 | FakeConstraints constraints; |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 310 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 311 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 312 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 313 | |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 314 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 315 | } |
| 316 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 317 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 318 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 319 | CreateVideoCapturerSource(&constraints); |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 320 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 321 | } |
| 322 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 323 | TEST_F(VideoCapturerTrackSourceTest, |
| 324 | MandatoryDenoisingConstraintOverridesOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 325 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 326 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 327 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 328 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 329 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 330 | |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 331 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 332 | } |
| 333 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 334 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 335 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 336 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 337 | constraints.AddOptional("invalidKey", false); |
| 338 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 339 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 340 | |
| 341 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 342 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 343 | EXPECT_EQ(rtc::Optional<bool>(true), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 344 | } |
| 345 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 346 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionAndInvalidKeyMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 347 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 348 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 349 | constraints.AddMandatory("invalidKey", false); |
| 350 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 351 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 352 | |
| 353 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 354 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 355 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 356 | } |
| 357 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 358 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 359 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 360 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, |
| 361 | "not a boolean"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 362 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 363 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 364 | |
| 365 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 366 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 367 | |
| 368 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 369 | } |
| 370 | |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 371 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 372 | FakeConstraints constraints; |
| 373 | // Optional constraints should be ignored if the mandatory constraints fail. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 374 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 375 | // Values are case-sensitive and must be all lower-case. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 376 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 377 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 378 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 379 | |
| 380 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 381 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 382 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 383 | } |
| 384 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 385 | TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 386 | FakeConstraints constraints; |
| 387 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 388 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 389 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 390 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 391 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 392 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 393 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 394 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 395 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 396 | kMaxWaitMs); |
| 397 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 398 | ASSERT_TRUE(format != NULL); |
| 399 | EXPECT_EQ(352, format->width); |
| 400 | EXPECT_EQ(288, format->height); |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 401 | EXPECT_EQ(5, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 402 | |
Per | c0d31e9 | 2016-03-31 15:23:39 | [diff] [blame] | 403 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Tests that the source starts video with the default resolution for |
| 407 | // screencast if no constraint is set. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 408 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { |
Niels Möller | 60653ba | 2016-03-02 10:41:36 | [diff] [blame] | 409 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 410 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 411 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 412 | CreateVideoCapturerSource(); |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 413 | ASSERT_TRUE(source_->is_screencast()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 414 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 415 | kMaxWaitMs); |
| 416 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 417 | ASSERT_TRUE(format != NULL); |
| 418 | EXPECT_EQ(640, format->width); |
| 419 | EXPECT_EQ(480, format->height); |
| 420 | EXPECT_EQ(30, format->framerate()); |
| 421 | } |
| 422 | |
| 423 | // Tests that the source starts video with the max width and height set by |
| 424 | // constraints for screencast. |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 425 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 426 | FakeConstraints constraints; |
| 427 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); |
| 428 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); |
| 429 | |
Niels Möller | 60653ba | 2016-03-02 10:41:36 | [diff] [blame] | 430 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 431 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 432 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 433 | CreateVideoCapturerSource(&constraints); |
perkj | 0d3eef2 | 2016-03-09 01:39:17 | [diff] [blame] | 434 | ASSERT_TRUE(source_->is_screencast()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 435 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 436 | kMaxWaitMs); |
| 437 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 438 | ASSERT_TRUE(format != NULL); |
| 439 | EXPECT_EQ(480, format->width); |
| 440 | EXPECT_EQ(270, format->height); |
| 441 | EXPECT_EQ(30, format->framerate()); |
| 442 | } |
| 443 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 444 | TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 445 | FakeConstraints constraints; |
| 446 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 447 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 448 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 449 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 450 | kMaxWaitMs); |
| 451 | ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); |
| 452 | } |
| 453 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 454 | TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 455 | FakeConstraints constraints; |
| 456 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 457 | |
perkj | a3ede6c | 2016-03-08 00:27:48 | [diff] [blame] | 458 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 459 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 460 | kMaxWaitMs); |
| 461 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 462 | ASSERT_TRUE(format != NULL); |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 463 | EXPECT_EQ(1, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 464 | } |