OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/test/test_timeouts.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "media/video/capture/fake_video_capture_device.h" |
| 11 #include "media/video/capture/fake_video_capture_device_factory.h" |
| 12 #include "media/video/capture/video_capture_device.h" |
| 13 #include "media/video/capture/video_capture_types.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using ::testing::_; |
| 18 |
| 19 namespace media { |
| 20 |
| 21 class MockClient : public media::VideoCaptureDevice::Client { |
| 22 public: |
| 23 MOCK_METHOD2(ReserveOutputBuffer, |
| 24 scoped_refptr<Buffer>(media::VideoFrame::Format format, |
| 25 const gfx::Size& dimensions)); |
| 26 MOCK_METHOD0(OnErr, void()); |
| 27 |
| 28 explicit MockClient(base::Callback<void(const VideoCaptureFormat&)> frame_cb) |
| 29 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb) {} |
| 30 |
| 31 virtual void OnError(const std::string& error_message) OVERRIDE { |
| 32 OnErr(); |
| 33 } |
| 34 |
| 35 virtual void OnIncomingCapturedData(const uint8* data, |
| 36 int length, |
| 37 const VideoCaptureFormat& format, |
| 38 int rotation, |
| 39 base::TimeTicks timestamp) OVERRIDE { |
| 40 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); |
| 41 } |
| 42 |
| 43 virtual void OnIncomingCapturedVideoFrame( |
| 44 const scoped_refptr<Buffer>& buffer, |
| 45 const media::VideoCaptureFormat& buffer_format, |
| 46 const scoped_refptr<media::VideoFrame>& frame, |
| 47 base::TimeTicks timestamp) OVERRIDE { |
| 48 NOTREACHED(); |
| 49 } |
| 50 |
| 51 private: |
| 52 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| 53 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; |
| 54 }; |
| 55 |
| 56 class FakeVideoCaptureDeviceTest : public testing::Test { |
| 57 protected: |
| 58 typedef media::VideoCaptureDevice::Client Client; |
| 59 |
| 60 FakeVideoCaptureDeviceTest() |
| 61 : loop_(new base::MessageLoop()), |
| 62 client_(new MockClient( |
| 63 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, |
| 64 base::Unretained(this)))), |
| 65 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} |
| 66 |
| 67 virtual void SetUp() { |
| 68 } |
| 69 |
| 70 void OnFrameCaptured(const VideoCaptureFormat& format) { |
| 71 last_format_ = format; |
| 72 run_loop_->QuitClosure().Run(); |
| 73 } |
| 74 |
| 75 void WaitForCapturedFrame() { |
| 76 run_loop_.reset(new base::RunLoop()); |
| 77 run_loop_->Run(); |
| 78 } |
| 79 |
| 80 const VideoCaptureFormat& last_format() const { return last_format_; } |
| 81 |
| 82 VideoCaptureDevice::Names names_; |
| 83 scoped_ptr<base::MessageLoop> loop_; |
| 84 scoped_ptr<base::RunLoop> run_loop_; |
| 85 scoped_ptr<MockClient> client_; |
| 86 VideoCaptureFormat last_format_; |
| 87 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; |
| 88 }; |
| 89 |
| 90 TEST_F(FakeVideoCaptureDeviceTest, Capture) { |
| 91 VideoCaptureDevice::Names names; |
| 92 |
| 93 video_capture_device_factory_->GetDeviceNames(&names); |
| 94 |
| 95 ASSERT_GT(static_cast<int>(names.size()), 0); |
| 96 |
| 97 scoped_ptr<VideoCaptureDevice> device( |
| 98 video_capture_device_factory_->Create(names.front())); |
| 99 ASSERT_TRUE(device); |
| 100 |
| 101 EXPECT_CALL(*client_, OnErr()).Times(0); |
| 102 |
| 103 VideoCaptureParams capture_params; |
| 104 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 105 capture_params.requested_format.frame_rate = 30; |
| 106 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 107 capture_params.allow_resolution_change = false; |
| 108 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
| 109 WaitForCapturedFrame(); |
| 110 EXPECT_EQ(last_format().frame_size.width(), 640); |
| 111 EXPECT_EQ(last_format().frame_size.height(), 480); |
| 112 EXPECT_EQ(last_format().frame_rate, 30); |
| 113 device->StopAndDeAllocate(); |
| 114 } |
| 115 |
| 116 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
| 117 VideoCaptureDevice::Names names; |
| 118 video_capture_device_factory_->GetDeviceNames(&names); |
| 119 |
| 120 VideoCaptureFormats supported_formats; |
| 121 VideoCaptureDevice::Names::iterator names_iterator; |
| 122 |
| 123 for (names_iterator = names.begin(); names_iterator != names.end(); |
| 124 ++names_iterator) { |
| 125 video_capture_device_factory_->GetDeviceSupportedFormats( |
| 126 *names_iterator, &supported_formats); |
| 127 EXPECT_EQ(supported_formats.size(), 3u); |
| 128 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); |
| 129 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); |
| 130 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); |
| 131 EXPECT_GE(supported_formats[0].frame_rate, 20); |
| 132 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); |
| 133 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); |
| 134 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); |
| 135 EXPECT_GE(supported_formats[1].frame_rate, 20); |
| 136 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
| 137 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
| 138 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); |
| 139 EXPECT_GE(supported_formats[2].frame_rate, 20); |
| 140 } |
| 141 } |
| 142 |
| 143 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) { |
| 144 VideoCaptureDevice::Names names; |
| 145 |
| 146 video_capture_device_factory_->GetDeviceNames(&names); |
| 147 VideoCaptureParams capture_params; |
| 148 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 149 capture_params.requested_format.frame_rate = 30; |
| 150 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 151 capture_params.allow_resolution_change = true; |
| 152 |
| 153 ASSERT_GT(static_cast<int>(names.size()), 0); |
| 154 |
| 155 scoped_ptr<VideoCaptureDevice> device( |
| 156 video_capture_device_factory_->Create(names.front())); |
| 157 ASSERT_TRUE(device); |
| 158 |
| 159 // Configure the FakeVideoCaptureDevice to use all its formats as roster. |
| 160 VideoCaptureFormats formats; |
| 161 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(), |
| 162 &formats); |
| 163 static_cast<FakeVideoCaptureDevice*>(device.get())-> |
| 164 PopulateVariableFormatsRoster(formats); |
| 165 |
| 166 EXPECT_CALL(*client_, OnErr()) |
| 167 .Times(0); |
| 168 int action_count = 200; |
| 169 |
| 170 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); |
| 171 |
| 172 // We set TimeWait to 200 action timeouts and this should be enough for at |
| 173 // least action_count/kFakeCaptureCapabilityChangePeriod calls. |
| 174 for (int i = 0; i < action_count; ++i) { |
| 175 WaitForCapturedFrame(); |
| 176 } |
| 177 device->StopAndDeAllocate(); |
| 178 } |
| 179 |
| 180 }; // namespace media |
OLD | NEW |