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