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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 14 | #include "webrtc/api/test/fakeconstraints.h" |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 15 | #include "webrtc/api/videocapturertracksource.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/gunit.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 17 | #include "webrtc/media/base/fakemediaengine.h" |
| 18 | #include "webrtc/media/base/fakevideocapturer.h" |
| 19 | #include "webrtc/media/base/fakevideorenderer.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 20 | #include "webrtc/media/engine/webrtcvideoframe.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) { |
| 112 | capturer_cleanup_ = rtc::scoped_ptr<TestVideoCapturer>( |
| 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 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::scoped_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_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 138 | rtc::scoped_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 | |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 159 | // Test that a VideoSource can be stopped and restarted. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 160 | TEST_F(VideoCapturerTrackSourceTest, StopRestart) { |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 161 | // Initialize without constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 162 | CreateVideoCapturerSource(); |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 163 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 164 | kMaxWaitMs); |
| 165 | |
| 166 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 167 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 168 | |
| 169 | source_->Stop(); |
| 170 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 171 | kMaxWaitMs); |
| 172 | |
| 173 | source_->Restart(); |
| 174 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 175 | kMaxWaitMs); |
| 176 | |
| 177 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 178 | EXPECT_EQ(2, renderer_.num_rendered_frames()); |
| 179 | |
| 180 | source_->Stop(); |
| 181 | } |
| 182 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 183 | // Test that a VideoSource transition to kEnded if the capture device |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | // fails. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 185 | TEST_F(VideoCapturerTrackSourceTest, CameraFailed) { |
| 186 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 188 | kMaxWaitMs); |
| 189 | |
| 190 | capturer_->SignalStateChange(capturer_, cricket::CS_FAILED); |
| 191 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 192 | kMaxWaitMs); |
| 193 | } |
| 194 | |
| 195 | // Test that the capture output is CIF if we set max constraints to CIF. |
| 196 | // and the capture device support CIF. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 197 | TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | FakeConstraints constraints; |
| 199 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 200 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 201 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 202 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 203 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 205 | kMaxWaitMs); |
| 206 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 207 | ASSERT_TRUE(format != NULL); |
| 208 | EXPECT_EQ(352, format->width); |
| 209 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 210 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Test that the capture output is 720P if the camera support it and the |
| 214 | // optional constraint is set to 720P. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 215 | TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | FakeConstraints constraints; |
| 217 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 218 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 219 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 220 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, |
| 221 | 1280.0 / 720); |
| 222 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 223 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 225 | kMaxWaitMs); |
| 226 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 227 | ASSERT_TRUE(format != NULL); |
| 228 | EXPECT_EQ(1280, format->width); |
| 229 | EXPECT_EQ(720, format->height); |
| 230 | EXPECT_EQ(30, format->framerate()); |
| 231 | } |
| 232 | |
| 233 | // Test that the capture output have aspect ratio 4:3 if a mandatory constraint |
| 234 | // require it even if an optional constraint request a higher resolution |
| 235 | // that don't have this aspect ratio. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 236 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | FakeConstraints constraints; |
| 238 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 239 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 240 | constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio, |
| 241 | 640.0 / 480); |
| 242 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 243 | |
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 | EXPECT_EQ(640, format->width); |
| 250 | EXPECT_EQ(480, format->height); |
| 251 | EXPECT_EQ(30, format->framerate()); |
| 252 | } |
| 253 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 254 | // Test that the source state transition to kEnded if the mandatory aspect ratio |
| 255 | // is set higher than supported. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 256 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 257 | FakeConstraints constraints; |
| 258 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 259 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 261 | kMaxWaitMs); |
| 262 | } |
| 263 | |
| 264 | // Test that the source ignores an optional aspect ratio that is higher than |
| 265 | // supported. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 266 | TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 267 | FakeConstraints constraints; |
| 268 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 269 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 270 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 271 | kMaxWaitMs); |
| 272 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 273 | ASSERT_TRUE(format != NULL); |
| 274 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 275 | EXPECT_LT(aspect_ratio, 2); |
| 276 | } |
| 277 | |
| 278 | // Test that the source starts video with the default resolution if the |
| 279 | // camera doesn't support capability enumeration and there are no constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 280 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | capturer_->TestWithoutCameraFormats(); |
| 282 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 283 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 284 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 285 | kMaxWaitMs); |
| 286 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 287 | ASSERT_TRUE(format != NULL); |
| 288 | EXPECT_EQ(640, format->width); |
| 289 | EXPECT_EQ(480, format->height); |
| 290 | EXPECT_EQ(30, format->framerate()); |
| 291 | } |
| 292 | |
| 293 | // Test that the source can start the video and get the requested aspect ratio |
| 294 | // if the camera doesn't support capability enumeration and the aspect ratio is |
| 295 | // set. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 296 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 297 | capturer_->TestWithoutCameraFormats(); |
| 298 | |
| 299 | FakeConstraints constraints; |
| 300 | double requested_aspect_ratio = 640.0 / 360; |
| 301 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 302 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, |
| 303 | requested_aspect_ratio); |
| 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 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 309 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 310 | EXPECT_LE(requested_aspect_ratio, aspect_ratio); |
| 311 | } |
| 312 | |
| 313 | // Test that the source state transitions to kEnded if an unknown mandatory |
| 314 | // constraint is found. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 315 | TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 316 | FakeConstraints constraints; |
| 317 | constraints.AddMandatory("weird key", 640); |
| 318 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 319 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 320 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 321 | kMaxWaitMs); |
| 322 | } |
| 323 | |
| 324 | // Test that the source ignores an unknown optional constraint. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 325 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | FakeConstraints constraints; |
| 327 | constraints.AddOptional("weird key", 640); |
| 328 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 329 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 330 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 331 | kMaxWaitMs); |
| 332 | } |
| 333 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 334 | TEST_F(VideoCapturerTrackSourceTest, SetValidDenoisingConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 335 | FakeConstraints constraints; |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 336 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 337 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 338 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 339 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 340 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 341 | } |
| 342 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 343 | TEST_F(VideoCapturerTrackSourceTest, NoiseReductionConstraintNotSet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 344 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 345 | CreateVideoCapturerSource(&constraints); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 346 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 347 | } |
| 348 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 349 | TEST_F(VideoCapturerTrackSourceTest, |
| 350 | MandatoryDenoisingConstraintOverridesOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 351 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 352 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 353 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 354 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 355 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 356 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 357 | EXPECT_EQ(rtc::Optional<bool>(false), 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, NoiseReductionAndInvalidKeyOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | FakeConstraints constraints; |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 362 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 363 | constraints.AddOptional("invalidKey", false); |
| 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 | EXPECT_EQ(rtc::Optional<bool>(true), 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, NoiseReductionAndInvalidKeyMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 373 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 374 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | constraints.AddMandatory("invalidKey", false); |
| 376 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 377 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 378 | |
| 379 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 380 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 381 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | } |
| 383 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 384 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 386 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, |
| 387 | "not a boolean"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 388 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 389 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 390 | |
| 391 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 392 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 393 | |
| 394 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | } |
| 396 | |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 397 | TEST_F(VideoCapturerTrackSourceTest, InvalidDenoisingValueMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 398 | FakeConstraints constraints; |
| 399 | // Optional constraints should be ignored if the mandatory constraints fail. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 400 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 401 | // Values are case-sensitive and must be all lower-case. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 402 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 403 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 404 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 405 | |
| 406 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 407 | kMaxWaitMs); |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 408 | EXPECT_EQ(rtc::Optional<bool>(), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 409 | } |
| 410 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 411 | TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 412 | FakeConstraints constraints; |
| 413 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 414 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 415 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 416 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 417 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 418 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 420 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 421 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 422 | kMaxWaitMs); |
| 423 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 424 | ASSERT_TRUE(format != NULL); |
| 425 | EXPECT_EQ(352, format->width); |
| 426 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 427 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 428 | |
Per | c0d31e9 | 2016-03-31 17:23:39 +0200 | [diff] [blame] | 429 | EXPECT_EQ(rtc::Optional<bool>(false), source_->needs_denoising()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Tests that the source starts video with the default resolution for |
| 433 | // screencast if no constraint is set. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 434 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 435 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 437 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 438 | CreateVideoCapturerSource(); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 439 | ASSERT_TRUE(source_->is_screencast()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 440 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 441 | kMaxWaitMs); |
| 442 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 443 | ASSERT_TRUE(format != NULL); |
| 444 | EXPECT_EQ(640, format->width); |
| 445 | EXPECT_EQ(480, format->height); |
| 446 | EXPECT_EQ(30, format->framerate()); |
| 447 | } |
| 448 | |
| 449 | // Tests that the source starts video with the max width and height set by |
| 450 | // constraints for screencast. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 451 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | FakeConstraints constraints; |
| 453 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); |
| 454 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); |
| 455 | |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 456 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 457 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 459 | CreateVideoCapturerSource(&constraints); |
perkj | 0d3eef2 | 2016-03-09 02:39:17 +0100 | [diff] [blame] | 460 | ASSERT_TRUE(source_->is_screencast()); |
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); |
| 465 | EXPECT_EQ(480, format->width); |
| 466 | EXPECT_EQ(270, format->height); |
| 467 | EXPECT_EQ(30, format->framerate()); |
| 468 | } |
| 469 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 470 | TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | FakeConstraints constraints; |
| 472 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 473 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 474 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 475 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 476 | kMaxWaitMs); |
| 477 | ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); |
| 478 | } |
| 479 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 480 | TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | FakeConstraints constraints; |
| 482 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 483 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 484 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 485 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 486 | kMaxWaitMs); |
| 487 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 488 | ASSERT_TRUE(format != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 489 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | } |