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