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