henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012, Google Inc. |
| 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 | |
| 31 | #include "talk/app/webrtc/test/fakeconstraints.h" |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 32 | #include "talk/app/webrtc/remotevideocapturer.h" |
| 33 | #include "talk/app/webrtc/videosource.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "talk/base/gunit.h" |
| 35 | #include "talk/media/base/fakemediaengine.h" |
| 36 | #include "talk/media/base/fakevideorenderer.h" |
| 37 | #include "talk/media/devices/fakedevicemanager.h" |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 38 | #include "talk/media/webrtc/webrtcvideoframe.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | #include "talk/session/media/channelmanager.h" |
| 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_; |
| 124 | talk_base::scoped_refptr<VideoSourceInterface> source_; |
| 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( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | new cricket::FakeMediaEngine(), |
| 134 | new cricket::FakeDeviceManager(), talk_base::Thread::Current())) { |
| 135 | } |
| 136 | |
| 137 | void SetUp() { |
| 138 | ASSERT_TRUE(channel_manager_->Init()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | } |
| 140 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 141 | void CreateVideoSource() { |
| 142 | CreateVideoSource(NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | } |
| 144 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 145 | void CreateVideoSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | const webrtc::MediaConstraintsInterface* constraints) { |
| 147 | // VideoSource take ownership of |capturer_| |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 148 | source_ = VideoSource::Create(channel_manager_.get(), |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 149 | capturer_cleanup_.release(), |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 150 | constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 152 | ASSERT_TRUE(source_.get() != NULL); |
| 153 | EXPECT_EQ(capturer_, source_->GetVideoCapturer()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 155 | state_observer_.reset(new StateObserver(source_)); |
| 156 | source_->RegisterObserver(state_observer_.get()); |
| 157 | source_->AddSink(&renderer_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | } |
| 159 | |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 160 | talk_base::scoped_ptr<TestVideoCapturer> capturer_cleanup_; |
| 161 | TestVideoCapturer* capturer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | cricket::FakeVideoRenderer renderer_; |
| 163 | talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_; |
| 164 | talk_base::scoped_ptr<StateObserver> state_observer_; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 165 | talk_base::scoped_refptr<VideoSource> source_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 169 | // Test that a VideoSource transition to kLive state when the capture |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | // device have started and kEnded if it is stopped. |
| 171 | // It also test that an output can receive video frames. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 172 | TEST_F(VideoSourceTest, StartStop) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | // Initialize without constraints. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 174 | CreateVideoSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 176 | kMaxWaitMs); |
| 177 | |
| 178 | ASSERT_TRUE(capturer_->CaptureFrame()); |
| 179 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 180 | |
| 181 | capturer_->Stop(); |
| 182 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 183 | kMaxWaitMs); |
| 184 | } |
| 185 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 186 | // Test start stop with a remote VideoSource - the video source that has a |
| 187 | // RemoteVideoCapturer and takes video frames from FrameInput. |
| 188 | TEST_F(VideoSourceTest, StartStopRemote) { |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 189 | source_ = VideoSource::Create(channel_manager_.get(), |
| 190 | new webrtc::RemoteVideoCapturer(), |
| 191 | NULL); |
| 192 | |
| 193 | ASSERT_TRUE(source_.get() != NULL); |
| 194 | EXPECT_TRUE(NULL != source_->GetVideoCapturer()); |
| 195 | |
| 196 | state_observer_.reset(new StateObserver(source_)); |
| 197 | source_->RegisterObserver(state_observer_.get()); |
| 198 | source_->AddSink(&renderer_); |
| 199 | |
| 200 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 201 | kMaxWaitMs); |
| 202 | |
| 203 | cricket::VideoRenderer* frameinput = source_->FrameInput(); |
| 204 | cricket::WebRtcVideoFrame test_frame; |
| 205 | frameinput->SetSize(1280, 720, 0); |
| 206 | frameinput->RenderFrame(&test_frame); |
| 207 | EXPECT_EQ(1, renderer_.num_rendered_frames()); |
| 208 | |
| 209 | source_->GetVideoCapturer()->Stop(); |
| 210 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 211 | kMaxWaitMs); |
| 212 | } |
| 213 | |
| 214 | // Test that a VideoSource transition to kEnded if the capture device |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | // fails. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 216 | TEST_F(VideoSourceTest, CameraFailed) { |
| 217 | CreateVideoSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 219 | kMaxWaitMs); |
| 220 | |
| 221 | capturer_->SignalStateChange(capturer_, cricket::CS_FAILED); |
| 222 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 223 | kMaxWaitMs); |
| 224 | } |
| 225 | |
| 226 | // Test that the capture output is CIF if we set max constraints to CIF. |
| 227 | // and the capture device support CIF. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 228 | TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | FakeConstraints constraints; |
| 230 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 231 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 232 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 233 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 234 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 236 | kMaxWaitMs); |
| 237 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 238 | ASSERT_TRUE(format != NULL); |
| 239 | EXPECT_EQ(352, format->width); |
| 240 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 241 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Test that the capture output is 720P if the camera support it and the |
| 245 | // optional constraint is set to 720P. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 246 | TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | FakeConstraints constraints; |
| 248 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 249 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 250 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 251 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, |
| 252 | 1280.0 / 720); |
| 253 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 254 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 256 | kMaxWaitMs); |
| 257 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 258 | ASSERT_TRUE(format != NULL); |
| 259 | EXPECT_EQ(1280, format->width); |
| 260 | EXPECT_EQ(720, format->height); |
| 261 | EXPECT_EQ(30, format->framerate()); |
| 262 | } |
| 263 | |
| 264 | // Test that the capture output have aspect ratio 4:3 if a mandatory constraint |
| 265 | // require it even if an optional constraint request a higher resolution |
| 266 | // that don't have this aspect ratio. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 267 | TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | FakeConstraints constraints; |
| 269 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 270 | constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); |
| 271 | constraints.AddMandatory(MediaConstraintsInterface::kMaxAspectRatio, |
| 272 | 640.0 / 480); |
| 273 | constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); |
| 274 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 275 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 277 | kMaxWaitMs); |
| 278 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 279 | ASSERT_TRUE(format != NULL); |
| 280 | EXPECT_EQ(640, format->width); |
| 281 | EXPECT_EQ(480, format->height); |
| 282 | EXPECT_EQ(30, format->framerate()); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | // Test that the source state transition to kEnded if the mandatory aspect ratio |
| 287 | // is set higher than supported. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 288 | TEST_F(VideoSourceTest, MandatoryAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 289 | FakeConstraints constraints; |
| 290 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); |
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::kEnded, state_observer_->state(), |
| 293 | kMaxWaitMs); |
| 294 | } |
| 295 | |
| 296 | // Test that the source ignores an optional aspect ratio that is higher than |
| 297 | // supported. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 298 | TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 299 | FakeConstraints constraints; |
| 300 | constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 301 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 302 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 303 | kMaxWaitMs); |
| 304 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 305 | ASSERT_TRUE(format != NULL); |
| 306 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 307 | EXPECT_LT(aspect_ratio, 2); |
| 308 | } |
| 309 | |
| 310 | // Test that the source starts video with the default resolution if the |
| 311 | // camera doesn't support capability enumeration and there are no constraints. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 312 | TEST_F(VideoSourceTest, NoCameraCapability) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 313 | capturer_->TestWithoutCameraFormats(); |
| 314 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 315 | CreateVideoSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 316 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 317 | kMaxWaitMs); |
| 318 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 319 | ASSERT_TRUE(format != NULL); |
| 320 | EXPECT_EQ(640, format->width); |
| 321 | EXPECT_EQ(480, format->height); |
| 322 | EXPECT_EQ(30, format->framerate()); |
| 323 | } |
| 324 | |
| 325 | // Test that the source can start the video and get the requested aspect ratio |
| 326 | // if the camera doesn't support capability enumeration and the aspect ratio is |
| 327 | // set. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 328 | TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 329 | capturer_->TestWithoutCameraFormats(); |
| 330 | |
| 331 | FakeConstraints constraints; |
| 332 | double requested_aspect_ratio = 640.0 / 360; |
| 333 | constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); |
| 334 | constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, |
| 335 | requested_aspect_ratio); |
| 336 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 337 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 338 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 339 | kMaxWaitMs); |
| 340 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 341 | double aspect_ratio = static_cast<double>(format->width) / format->height; |
| 342 | EXPECT_LE(requested_aspect_ratio, aspect_ratio); |
| 343 | } |
| 344 | |
| 345 | // Test that the source state transitions to kEnded if an unknown mandatory |
| 346 | // constraint is found. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 347 | TEST_F(VideoSourceTest, InvalidMandatoryConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 348 | FakeConstraints constraints; |
| 349 | constraints.AddMandatory("weird key", 640); |
| 350 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 351 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 353 | kMaxWaitMs); |
| 354 | } |
| 355 | |
| 356 | // Test that the source ignores an unknown optional constraint. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 357 | TEST_F(VideoSourceTest, InvalidOptionalConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | FakeConstraints constraints; |
| 359 | constraints.AddOptional("weird key", 640); |
| 360 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 361 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 363 | kMaxWaitMs); |
| 364 | } |
| 365 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 366 | TEST_F(VideoSourceTest, SetValidOptionValues) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | FakeConstraints constraints; |
| 368 | constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); |
| 369 | constraints.AddMandatory( |
| 370 | MediaConstraintsInterface::kTemporalLayeredScreencast, "false"); |
| 371 | constraints.AddOptional( |
| 372 | MediaConstraintsInterface::kLeakyBucket, "true"); |
| 373 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 374 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | |
| 376 | bool value = true; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 377 | EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 378 | EXPECT_FALSE(value); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 379 | EXPECT_TRUE(source_->options()-> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | video_temporal_layer_screencast.Get(&value)); |
| 381 | EXPECT_FALSE(value); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 382 | EXPECT_TRUE(source_->options()->video_leaky_bucket.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 383 | EXPECT_TRUE(value); |
| 384 | } |
| 385 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 386 | TEST_F(VideoSourceTest, OptionNotSet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | FakeConstraints constraints; |
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 | bool value; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 390 | EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 391 | } |
| 392 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 393 | TEST_F(VideoSourceTest, MandatoryOptionOverridesOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 394 | FakeConstraints constraints; |
| 395 | constraints.AddMandatory( |
| 396 | MediaConstraintsInterface::kNoiseReduction, true); |
| 397 | constraints.AddOptional( |
| 398 | MediaConstraintsInterface::kNoiseReduction, 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 | bool value = false; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 403 | EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 404 | EXPECT_TRUE(value); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 405 | EXPECT_FALSE(source_->options()->video_leaky_bucket.Get(&value)); |
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, InvalidOptionKeyOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 409 | FakeConstraints constraints; |
| 410 | constraints.AddOptional( |
| 411 | MediaConstraintsInterface::kNoiseReduction, false); |
| 412 | constraints.AddOptional("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::kLive, state_observer_->state(), |
| 417 | kMaxWaitMs); |
| 418 | bool value = true; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 419 | EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | EXPECT_FALSE(value); |
| 421 | } |
| 422 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 423 | TEST_F(VideoSourceTest, InvalidOptionKeyMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 424 | FakeConstraints constraints; |
| 425 | constraints.AddMandatory( |
| 426 | MediaConstraintsInterface::kNoiseReduction, false); |
| 427 | constraints.AddMandatory("invalidKey", false); |
| 428 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 429 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 430 | |
| 431 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 432 | kMaxWaitMs); |
| 433 | bool value; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 434 | EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 435 | } |
| 436 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 437 | TEST_F(VideoSourceTest, InvalidOptionValueOptional) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 438 | FakeConstraints constraints; |
| 439 | constraints.AddOptional( |
| 440 | MediaConstraintsInterface::kNoiseReduction, "true"); |
| 441 | constraints.AddOptional( |
| 442 | MediaConstraintsInterface::kLeakyBucket, "not boolean"); |
| 443 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 444 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 445 | |
| 446 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 447 | kMaxWaitMs); |
| 448 | bool value = false; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 449 | EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 450 | EXPECT_TRUE(value); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 451 | EXPECT_FALSE(source_->options()->video_leaky_bucket.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | } |
| 453 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 454 | TEST_F(VideoSourceTest, InvalidOptionValueMandatory) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | FakeConstraints constraints; |
| 456 | // Optional constraints should be ignored if the mandatory constraints fail. |
| 457 | constraints.AddOptional( |
| 458 | MediaConstraintsInterface::kNoiseReduction, "false"); |
| 459 | // Values are case-sensitive and must be all lower-case. |
| 460 | constraints.AddMandatory( |
| 461 | MediaConstraintsInterface::kLeakyBucket, "True"); |
| 462 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 463 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | |
| 465 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 466 | kMaxWaitMs); |
| 467 | bool value; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 468 | EXPECT_FALSE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 469 | } |
| 470 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 471 | TEST_F(VideoSourceTest, MixedOptionsAndConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 472 | FakeConstraints constraints; |
| 473 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); |
| 474 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); |
| 475 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); |
| 476 | |
| 477 | constraints.AddMandatory( |
| 478 | MediaConstraintsInterface::kNoiseReduction, false); |
| 479 | constraints.AddOptional( |
| 480 | MediaConstraintsInterface::kNoiseReduction, true); |
| 481 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 482 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 483 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 484 | kMaxWaitMs); |
| 485 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 486 | ASSERT_TRUE(format != NULL); |
| 487 | EXPECT_EQ(352, format->width); |
| 488 | EXPECT_EQ(288, format->height); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 489 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | |
| 491 | bool value = true; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 492 | EXPECT_TRUE(source_->options()->video_noise_reduction.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 493 | EXPECT_FALSE(value); |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 494 | EXPECT_FALSE(source_->options()->video_leaky_bucket.Get(&value)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Tests that the source starts video with the default resolution for |
| 498 | // screencast if no constraint is set. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 499 | TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 500 | capturer_->TestWithoutCameraFormats(); |
| 501 | capturer_->SetScreencast(true); |
| 502 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 503 | CreateVideoSource(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 504 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 505 | kMaxWaitMs); |
| 506 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 507 | ASSERT_TRUE(format != NULL); |
| 508 | EXPECT_EQ(640, format->width); |
| 509 | EXPECT_EQ(480, format->height); |
| 510 | EXPECT_EQ(30, format->framerate()); |
| 511 | } |
| 512 | |
| 513 | // Tests that the source starts video with the max width and height set by |
| 514 | // constraints for screencast. |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 515 | TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 516 | FakeConstraints constraints; |
| 517 | constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); |
| 518 | constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); |
| 519 | |
| 520 | capturer_->TestWithoutCameraFormats(); |
| 521 | capturer_->SetScreencast(true); |
| 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); |
| 528 | EXPECT_EQ(480, format->width); |
| 529 | EXPECT_EQ(270, format->height); |
| 530 | EXPECT_EQ(30, format->framerate()); |
| 531 | } |
| 532 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 533 | TEST_F(VideoSourceTest, MandatorySubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 534 | FakeConstraints constraints; |
| 535 | constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 536 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 537 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 538 | EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), |
| 539 | kMaxWaitMs); |
| 540 | ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); |
| 541 | } |
| 542 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 543 | TEST_F(VideoSourceTest, OptionalSubOneFpsConstraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 544 | FakeConstraints constraints; |
| 545 | constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); |
| 546 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 547 | CreateVideoSource(&constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 548 | EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), |
| 549 | kMaxWaitMs); |
| 550 | const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); |
| 551 | ASSERT_TRUE(format != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 552 | EXPECT_EQ(30, format->framerate()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | } |
| 554 | |