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/remotevideocapturer.h" |
| 15 | #include "webrtc/api/test/fakeconstraints.h" |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 16 | #include "webrtc/api/videocapturertracksource.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 17 | #include "webrtc/base/gunit.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #include "webrtc/media/base/fakemediaengine.h" |
| 19 | #include "webrtc/media/base/fakevideocapturer.h" |
| 20 | #include "webrtc/media/base/fakevideorenderer.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 21 | #include "webrtc/media/engine/webrtcvideoframe.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | using webrtc::FakeConstraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 24 | using webrtc::VideoCapturerTrackSource; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | using webrtc::MediaConstraintsInterface; |
| 26 | using webrtc::MediaSourceInterface; |
| 27 | using webrtc::ObserverInterface; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 28 | using webrtc::VideoTrackSourceInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Max wait time for a test. |
| 33 | const int kMaxWaitMs = 100; |
| 34 | |
| 35 | } // anonymous namespace |
| 36 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | // TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for |
| 38 | // testing without known camera formats. |
| 39 | // It keeps its own lists of cricket::VideoFormats for the unit tests in this |
| 40 | // file. |
| 41 | class TestVideoCapturer : public cricket::FakeVideoCapturer { |
| 42 | public: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 43 | explicit TestVideoCapturer(bool is_screencast) |
| 44 | : FakeVideoCapturer(is_screencast), test_without_formats_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | std::vector<cricket::VideoFormat> formats; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 46 | formats.push_back( |
| 47 | cricket::VideoFormat(1280, 720, cricket::VideoFormat::FpsToInterval(30), |
| 48 | cricket::FOURCC_I420)); |
| 49 | formats.push_back( |
| 50 | cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), |
| 51 | cricket::FOURCC_I420)); |
| 52 | formats.push_back( |
| 53 | cricket::VideoFormat(640, 400, cricket::VideoFormat::FpsToInterval(30), |
| 54 | cricket::FOURCC_I420)); |
| 55 | formats.push_back( |
| 56 | cricket::VideoFormat(320, 240, cricket::VideoFormat::FpsToInterval(30), |
| 57 | cricket::FOURCC_I420)); |
| 58 | formats.push_back( |
| 59 | cricket::VideoFormat(352, 288, cricket::VideoFormat::FpsToInterval(30), |
| 60 | cricket::FOURCC_I420)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | ResetSupportedFormats(formats); |
| 62 | } |
| 63 | |
| 64 | // This function is used for resetting the supported capture formats and |
| 65 | // simulating a cricket::VideoCapturer implementation that don't support |
| 66 | // capture format enumeration. This is used to simulate the current |
| 67 | // Chrome implementation. |
| 68 | void TestWithoutCameraFormats() { |
| 69 | test_without_formats_ = true; |
| 70 | std::vector<cricket::VideoFormat> formats; |
| 71 | ResetSupportedFormats(formats); |
| 72 | } |
| 73 | |
| 74 | virtual cricket::CaptureState Start( |
| 75 | const cricket::VideoFormat& capture_format) { |
| 76 | if (test_without_formats_) { |
| 77 | std::vector<cricket::VideoFormat> formats; |
| 78 | formats.push_back(capture_format); |
| 79 | ResetSupportedFormats(formats); |
| 80 | } |
| 81 | return FakeVideoCapturer::Start(capture_format); |
| 82 | } |
| 83 | |
| 84 | virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired, |
| 85 | cricket::VideoFormat* best_format) { |
| 86 | if (test_without_formats_) { |
| 87 | *best_format = desired; |
| 88 | return true; |
| 89 | } |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 90 | return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | bool test_without_formats_; |
| 95 | }; |
| 96 | |
| 97 | class StateObserver : public ObserverInterface { |
| 98 | public: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 99 | explicit StateObserver(VideoTrackSourceInterface* source) |
| 100 | : state_(source->state()), source_(source) {} |
| 101 | virtual void OnChanged() { state_ = source_->state(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | MediaSourceInterface::SourceState state() const { return state_; } |
| 103 | |
| 104 | private: |
| 105 | MediaSourceInterface::SourceState state_; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 106 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 109 | class VideoCapturerTrackSourceTest : public testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | protected: |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 111 | VideoCapturerTrackSourceTest() { InitCapturer(false); } |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 112 | void InitCapturer(bool is_screencast) { |
| 113 | capturer_cleanup_ = rtc::scoped_ptr<TestVideoCapturer>( |
| 114 | new TestVideoCapturer(is_screencast)); |
| 115 | capturer_ = capturer_cleanup_.get(); |
| 116 | } |
| 117 | |
| 118 | void InitScreencast() { InitCapturer(true); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 120 | void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 122 | void CreateVideoCapturerSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | const webrtc::MediaConstraintsInterface* constraints) { |
| 124 | // VideoSource take ownership of |capturer_| |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 125 | source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(), |
| 126 | capturer_cleanup_.release(), |
| 127 | constraints, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 129 | ASSERT_TRUE(source_.get() != NULL); |
| 130 | EXPECT_EQ(capturer_, source_->GetVideoCapturer()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 132 | state_observer_.reset(new StateObserver(source_)); |
| 133 | source_->RegisterObserver(state_observer_.get()); |
perkj | f2880a0 | 2016-03-03 01:51:52 -0800 | [diff] [blame] | 134 | source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | } |
| 136 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 137 | rtc::scoped_ptr<TestVideoCapturer> capturer_cleanup_; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 138 | TestVideoCapturer* capturer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | cricket::FakeVideoRenderer renderer_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 140 | rtc::scoped_ptr<StateObserver> state_observer_; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 141 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 144 | // Test that a VideoSource transition to kLive state when the capture |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | // device have started and kEnded if it is stopped. |
| 146 | // It also test that an output can receive video frames. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 147 | TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | // Initialize without constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 149 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 151 | kMaxWaitMs); |
| 152 | |
| 153 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 154 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 155 | |
| 156 | capturer_->Stop(); |
| 157 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 158 | kMaxWaitMs); |
| 159 | } |
| 160 | |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 161 | // Test that a VideoSource can be stopped and restarted. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 162 | TEST_F(VideoCapturerTrackSourceTest, StopRestart) { |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 163 | // Initialize without constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 164 | CreateVideoCapturerSource(); |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 165 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 166 | kMaxWaitMs); |
| 167 | |
| 168 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 169 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 170 | |
| 171 | source_->Stop(); |
| 172 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 173 | kMaxWaitMs); |
| 174 | |
| 175 | source_->Restart(); |
| 176 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 177 | kMaxWaitMs); |
| 178 | |
| 179 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 180 | EXPECT_EQ(2, renderer_.num_rendered_frames()); |
| 181 | |
| 182 | source_->Stop(); |
| 183 | } |
| 184 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 185 | // Test start stop with a remote VideoSource - the video source that has a |
| 186 | // RemoteVideoCapturer and takes video frames from FrameInput. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 187 | TEST_F(VideoCapturerTrackSourceTest, StartStopRemote) { |
| 188 | source_ = VideoCapturerTrackSource::Create( |
| 189 | rtc::Thread::Current(), new webrtc::RemoteVideoCapturer(), NULL, true); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 190 | |
| 191 | ASSERT_TRUE(source_.get() != NULL); |
| 192 | EXPECT_TRUE(NULL != source_->GetVideoCapturer()); |
| 193 | |
| 194 | state_observer_.reset(new StateObserver(source_)); |
| 195 | source_->RegisterObserver(state_observer_.get()); |
perkj | f2880a0 | 2016-03-03 01:51:52 -0800 | [diff] [blame] | 196 | source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants()); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 197 | |
| 198 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 199 | kMaxWaitMs); |
| 200 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 201 | source_->GetVideoCapturer()->Stop(); |
| 202 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 203 | kMaxWaitMs); |
| 204 | } |
| 205 | |
| 206 | // Test that a VideoSource transition to kEnded if the capture device |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | // fails. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 208 | TEST_F(VideoCapturerTrackSourceTest, CameraFailed) { |
| 209 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 211 | kMaxWaitMs); |
| 212 | |
| 213 | capturer_->SignalStateChange(capturer_, cricket::CS_FAILED); |
| 214 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 215 | kMaxWaitMs); |
| 216 | } |
| 217 | |
| 218 | // Test that the capture output is CIF if we set max constraints to CIF. |
| 219 | // and the capture device support CIF. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 220 | TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 221 | FakeConstraints constraints; |
| 222 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 223 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 224 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 225 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 226 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 227 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 228 | kMaxWaitMs); |
| 229 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 230 | ASSERT_TRUE(format != NULL); |
| 231 | EXPECT_EQ(352, format->width); |
| 232 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 233 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Test that the capture output is 720P if the camera support it and the |
| 237 | // optional constraint is set to 720P. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 238 | TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 239 | FakeConstraints constraints; |
| 240 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 241 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 242 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 243 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, |
| 244 | 1280.0 / 720); |
| 245 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 246 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 248 | kMaxWaitMs); |
| 249 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 250 | ASSERT_TRUE(format != NULL); |
| 251 | EXPECT_EQ(1280, format->width); |
| 252 | EXPECT_EQ(720, format->height); |
| 253 | EXPECT_EQ(30, format->framerate()); |
| 254 | } |
| 255 | |
| 256 | // Test that the capture output have aspect ratio 4:3 if a mandatory constraint |
| 257 | // require it even if an optional constraint request a higher resolution |
| 258 | // that don't have this aspect ratio. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 259 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | FakeConstraints constraints; |
| 261 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 262 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 263 | constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio, |
| 264 | 640.0 / 480); |
| 265 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 266 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 267 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 269 | kMaxWaitMs); |
| 270 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 271 | ASSERT_TRUE(format != NULL); |
| 272 | EXPECT_EQ(640, format->width); |
| 273 | EXPECT_EQ(480, format->height); |
| 274 | EXPECT_EQ(30, format->framerate()); |
| 275 | } |
| 276 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 277 | // Test that the source state transition to kEnded if the mandatory aspect ratio |
| 278 | // is set higher than supported. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 279 | TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | FakeConstraints constraints; |
| 281 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 282 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 284 | kMaxWaitMs); |
| 285 | } |
| 286 | |
| 287 | // Test that the source ignores an optional aspect ratio that is higher than |
| 288 | // supported. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 289 | TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | FakeConstraints constraints; |
| 291 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 292 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 293 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 294 | kMaxWaitMs); |
| 295 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 296 | ASSERT_TRUE(format != NULL); |
| 297 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 298 | EXPECT_LT(aspect_ratio, 2); |
| 299 | } |
| 300 | |
| 301 | // Test that the source starts video with the default resolution if the |
| 302 | // camera doesn't support capability enumeration and there are no constraints. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 303 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 304 | capturer_->TestWithoutCameraFormats(); |
| 305 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 306 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 308 | kMaxWaitMs); |
| 309 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 310 | ASSERT_TRUE(format != NULL); |
| 311 | EXPECT_EQ(640, format->width); |
| 312 | EXPECT_EQ(480, format->height); |
| 313 | EXPECT_EQ(30, format->framerate()); |
| 314 | } |
| 315 | |
| 316 | // Test that the source can start the video and get the requested aspect ratio |
| 317 | // if the camera doesn't support capability enumeration and the aspect ratio is |
| 318 | // set. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 319 | TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 320 | capturer_->TestWithoutCameraFormats(); |
| 321 | |
| 322 | FakeConstraints constraints; |
| 323 | double requested_aspect_ratio = 640.0 / 360; |
| 324 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 325 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, |
| 326 | requested_aspect_ratio); |
| 327 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 328 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 329 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 330 | kMaxWaitMs); |
| 331 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 332 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 333 | EXPECT_LE(requested_aspect_ratio, aspect_ratio); |
| 334 | } |
| 335 | |
| 336 | // Test that the source state transitions to kEnded if an unknown mandatory |
| 337 | // constraint is found. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 338 | TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 339 | FakeConstraints constraints; |
| 340 | constraints.AddMandatory("weird key", 640); |
| 341 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 342 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 343 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 344 | kMaxWaitMs); |
| 345 | } |
| 346 | |
| 347 | // Test that the source ignores an unknown optional constraint. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 348 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 349 | FakeConstraints constraints; |
| 350 | constraints.AddOptional("weird key", 640); |
| 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 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 354 | kMaxWaitMs); |
| 355 | } |
| 356 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 357 | TEST_F(VideoCapturerTrackSourceTest, SetValidOptionValues) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | FakeConstraints constraints; |
| 359 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 360 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 361 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 363 | EXPECT_EQ(rtc::Optional<bool>(false), |
| 364 | source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | } |
| 366 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 367 | TEST_F(VideoCapturerTrackSourceTest, OptionNotSet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 368 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 369 | CreateVideoCapturerSource(&constraints); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 370 | EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 371 | } |
| 372 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 373 | TEST_F(VideoCapturerTrackSourceTest, MandatoryOptionOverridesOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 374 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 375 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, true); |
| 376 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 377 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 378 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 379 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 380 | EXPECT_EQ(rtc::Optional<bool>(true), |
| 381 | source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | } |
| 383 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 384 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionKeyOptional) { |
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, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | constraints.AddOptional("invalidKey", false); |
| 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); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 393 | EXPECT_EQ(rtc::Optional<bool>(false), |
| 394 | source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | } |
| 396 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 397 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionKeyMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 398 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 399 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 400 | constraints.AddMandatory("invalidKey", false); |
| 401 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 402 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 403 | |
| 404 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 405 | kMaxWaitMs); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 406 | EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 407 | } |
| 408 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 409 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionValueOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 410 | FakeConstraints constraints; |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 411 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, |
| 412 | "not a boolean"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 413 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 414 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 415 | |
| 416 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 417 | kMaxWaitMs); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 418 | EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | } |
| 420 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 421 | TEST_F(VideoCapturerTrackSourceTest, InvalidOptionValueMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 422 | FakeConstraints constraints; |
| 423 | // Optional constraints should be ignored if the mandatory constraints fail. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 424 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 425 | // Values are case-sensitive and must be all lower-case. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 426 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 427 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 428 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | |
| 430 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 431 | kMaxWaitMs); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 432 | EXPECT_EQ(rtc::Optional<bool>(), source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 433 | } |
| 434 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 435 | TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | FakeConstraints constraints; |
| 437 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 438 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 439 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 440 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 441 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); |
| 442 | constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 443 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 444 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 445 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 446 | kMaxWaitMs); |
| 447 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 448 | ASSERT_TRUE(format != NULL); |
| 449 | EXPECT_EQ(352, format->width); |
| 450 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 451 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 453 | EXPECT_EQ(rtc::Optional<bool>(false), |
| 454 | source_->options()->video_noise_reduction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Tests that the source starts video with the default resolution for |
| 458 | // screencast if no constraint is set. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 459 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 460 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 461 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 462 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 463 | CreateVideoCapturerSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 465 | kMaxWaitMs); |
| 466 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 467 | ASSERT_TRUE(format != NULL); |
| 468 | EXPECT_EQ(640, format->width); |
| 469 | EXPECT_EQ(480, format->height); |
| 470 | EXPECT_EQ(30, format->framerate()); |
| 471 | } |
| 472 | |
| 473 | // Tests that the source starts video with the max width and height set by |
| 474 | // constraints for screencast. |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 475 | TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | FakeConstraints constraints; |
| 477 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); |
| 478 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); |
| 479 | |
Niels Möller | 60653ba | 2016-03-02 11:41:36 +0100 | [diff] [blame] | 480 | InitScreencast(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | capturer_->TestWithoutCameraFormats(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 482 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 483 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 485 | kMaxWaitMs); |
| 486 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 487 | ASSERT_TRUE(format != NULL); |
| 488 | EXPECT_EQ(480, format->width); |
| 489 | EXPECT_EQ(270, format->height); |
| 490 | EXPECT_EQ(30, format->framerate()); |
| 491 | } |
| 492 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 493 | TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 494 | FakeConstraints constraints; |
| 495 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 496 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 497 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 498 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 499 | kMaxWaitMs); |
| 500 | ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); |
| 501 | } |
| 502 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 503 | TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 504 | FakeConstraints constraints; |
| 505 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 506 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame^] | 507 | CreateVideoCapturerSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 508 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 509 | kMaxWaitMs); |
| 510 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 511 | ASSERT_TRUE(format != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 512 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 513 | } |