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