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