OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/capture/video/fake_video_capture_device.h" | 5 #include "media/capture/video/fake_video_capture_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "media/base/media_switches.h" | 20 #include "media/base/media_switches.h" |
21 #include "media/base/video_capture_types.h" | 21 #include "media/base/video_capture_types.h" |
22 #include "media/capture/video/fake_video_capture_device_factory.h" | 22 #include "media/capture/video/fake_video_capture_device_factory.h" |
23 #include "media/capture/video/video_capture_device.h" | 23 #include "media/capture/video/video_capture_device.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "ui/gfx/codec/png_codec.h" | |
26 | 27 |
27 using ::testing::_; | 28 using ::testing::_; |
28 using ::testing::Bool; | 29 using ::testing::Bool; |
29 using ::testing::Combine; | 30 using ::testing::Combine; |
30 using ::testing::SaveArg; | 31 using ::testing::SaveArg; |
31 using ::testing::Values; | 32 using ::testing::Values; |
32 | 33 |
33 namespace media { | 34 namespace media { |
34 | 35 |
35 namespace { | 36 namespace { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 void OnEnumeratedDevicesCallback( | 126 void OnEnumeratedDevicesCallback( |
126 std::unique_ptr<VideoCaptureDevice::Names> names) { | 127 std::unique_ptr<VideoCaptureDevice::Names> names) { |
127 OnEnumeratedDevicesCallbackPtr(names.release()); | 128 OnEnumeratedDevicesCallbackPtr(names.release()); |
128 } | 129 } |
129 | 130 |
130 private: | 131 private: |
131 friend class base::RefCounted<DeviceEnumerationListener>; | 132 friend class base::RefCounted<DeviceEnumerationListener>; |
132 virtual ~DeviceEnumerationListener() {} | 133 virtual ~DeviceEnumerationListener() {} |
133 }; | 134 }; |
134 | 135 |
136 class PhotoTakenListener : public base::RefCounted<PhotoTakenListener> { | |
137 public: | |
138 MOCK_METHOD0(OnCorrectPhotoTaken, void(void)); | |
139 // GMock doesn't support move-only arguments, so we use this forward method. | |
140 void DoOnPhotoTaken(const std::string& mime_type, | |
141 std::unique_ptr<std::vector<uint8_t>> data) { | |
142 // Only PNG images are supported right now. | |
143 EXPECT_STREQ("image/png", mime_type.c_str()); | |
144 | |
145 std::vector<unsigned char> decoded; | |
146 int width, height; | |
147 ASSERT_TRUE(gfx::PNGCodec::Decode(data->data(), data->size(), | |
miu
2016/05/05 22:04:24
Your call, but it feels a bit heavyweight for the
mcasas
2016/05/05 23:05:50
Done.
| |
148 gfx::PNGCodec::FORMAT_RGB, &decoded, | |
149 &width, &height)); | |
150 | |
151 EXPECT_GT(decoded.size(), 0u); | |
152 EXPECT_GT(width, 0); | |
153 EXPECT_GT(height, 0); | |
154 OnCorrectPhotoTaken(); | |
155 } | |
156 | |
157 private: | |
158 friend class base::RefCounted<PhotoTakenListener>; | |
159 virtual ~PhotoTakenListener() {} | |
160 }; | |
161 | |
135 } // namespace | 162 } // namespace |
136 | 163 |
137 class FakeVideoCaptureDeviceBase : public ::testing::Test { | 164 class FakeVideoCaptureDeviceBase : public ::testing::Test { |
138 protected: | 165 protected: |
139 FakeVideoCaptureDeviceBase() | 166 FakeVideoCaptureDeviceBase() |
140 : loop_(new base::MessageLoop()), | 167 : loop_(new base::MessageLoop()), |
141 client_(new MockClient( | 168 client_(new MockClient( |
142 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, | 169 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, |
143 base::Unretained(this)))), | 170 base::Unretained(this)))), |
144 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) { | 171 device_enumeration_listener_(new DeviceEnumerationListener()), |
145 device_enumeration_listener_ = new DeviceEnumerationListener(); | 172 photo_taken_listener_(new PhotoTakenListener()), |
146 } | 173 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} |
147 | 174 |
148 void SetUp() override { EXPECT_CALL(*client_, OnError(_, _)).Times(0); } | 175 void SetUp() override { EXPECT_CALL(*client_, OnError(_, _)).Times(0); } |
149 | 176 |
150 void OnFrameCaptured(const VideoCaptureFormat& format) { | 177 void OnFrameCaptured(const VideoCaptureFormat& format) { |
151 last_format_ = format; | 178 last_format_ = format; |
152 run_loop_->QuitClosure().Run(); | 179 run_loop_->QuitClosure().Run(); |
153 } | 180 } |
154 | 181 |
155 void WaitForCapturedFrame() { | 182 void WaitForCapturedFrame() { |
156 run_loop_.reset(new base::RunLoop()); | 183 run_loop_.reset(new base::RunLoop()); |
(...skipping 11 matching lines...) Expand all Loading... | |
168 base::MessageLoop::current()->RunUntilIdle(); | 195 base::MessageLoop::current()->RunUntilIdle(); |
169 return std::unique_ptr<VideoCaptureDevice::Names>(names); | 196 return std::unique_ptr<VideoCaptureDevice::Names>(names); |
170 } | 197 } |
171 | 198 |
172 const VideoCaptureFormat& last_format() const { return last_format_; } | 199 const VideoCaptureFormat& last_format() const { return last_format_; } |
173 | 200 |
174 VideoCaptureDevice::Names names_; | 201 VideoCaptureDevice::Names names_; |
175 const std::unique_ptr<base::MessageLoop> loop_; | 202 const std::unique_ptr<base::MessageLoop> loop_; |
176 std::unique_ptr<base::RunLoop> run_loop_; | 203 std::unique_ptr<base::RunLoop> run_loop_; |
177 std::unique_ptr<MockClient> client_; | 204 std::unique_ptr<MockClient> client_; |
178 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 205 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
206 const scoped_refptr<PhotoTakenListener> photo_taken_listener_; | |
179 VideoCaptureFormat last_format_; | 207 VideoCaptureFormat last_format_; |
180 const std::unique_ptr<VideoCaptureDeviceFactory> | 208 const std::unique_ptr<VideoCaptureDeviceFactory> |
181 video_capture_device_factory_; | 209 video_capture_device_factory_; |
182 }; | 210 }; |
183 | 211 |
184 class FakeVideoCaptureDeviceTest | 212 class FakeVideoCaptureDeviceTest |
185 : public FakeVideoCaptureDeviceBase, | 213 : public FakeVideoCaptureDeviceBase, |
186 public ::testing::WithParamInterface< | 214 public ::testing::WithParamInterface< |
187 ::testing::tuple<FakeVideoCaptureDevice::BufferOwnership, float>> {}; | 215 ::testing::tuple<FakeVideoCaptureDevice::BufferOwnership, float>> {}; |
188 | 216 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 273 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
246 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); | 274 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); |
247 EXPECT_GE(supported_formats[2].frame_rate, 20.0); | 275 EXPECT_GE(supported_formats[2].frame_rate, 20.0); |
248 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 276 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); |
249 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 277 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); |
250 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); | 278 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); |
251 EXPECT_GE(supported_formats[3].frame_rate, 20.0); | 279 EXPECT_GE(supported_formats[3].frame_rate, 20.0); |
252 } | 280 } |
253 } | 281 } |
254 | 282 |
283 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { | |
284 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | |
285 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | |
286 ASSERT_TRUE(device); | |
287 | |
288 VideoCaptureParams capture_params; | |
289 capture_params.requested_format.frame_size.SetSize(640, 480); | |
290 capture_params.requested_format.frame_rate = 30.0; | |
291 device->AllocateAndStart(capture_params, std::move(client_)); | |
292 | |
293 const VideoCaptureDevice::TakePhotoCallback photo_callback = | |
294 base::Bind(&PhotoTakenListener::DoOnPhotoTaken, photo_taken_listener_); | |
295 EXPECT_CALL(*photo_taken_listener_.get(), OnCorrectPhotoTaken()).Times(1); | |
296 ASSERT_TRUE(device->TakePhoto(photo_callback)); | |
297 | |
298 run_loop_.reset(new base::RunLoop()); | |
299 run_loop_->Run(); | |
300 device->StopAndDeAllocate(); | |
301 } | |
302 | |
255 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { | 303 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { |
256 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 304 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
257 switches::kUseFakeDeviceForMediaStream, GetParam().argument); | 305 switches::kUseFakeDeviceForMediaStream, GetParam().argument); |
258 const std::unique_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); | 306 const std::unique_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
259 ASSERT_FALSE(names->empty()); | 307 ASSERT_FALSE(names->empty()); |
260 | 308 |
261 for (const auto& names_iterator : *names) { | 309 for (const auto& names_iterator : *names) { |
262 std::unique_ptr<VideoCaptureDevice> device = | 310 std::unique_ptr<VideoCaptureDevice> device = |
263 video_capture_device_factory_->Create(names_iterator); | 311 video_capture_device_factory_->Create(names_iterator); |
264 ASSERT_TRUE(device); | 312 ASSERT_TRUE(device); |
(...skipping 11 matching lines...) Expand all Loading... | |
276 } | 324 } |
277 } | 325 } |
278 | 326 |
279 INSTANTIATE_TEST_CASE_P(, | 327 INSTANTIATE_TEST_CASE_P(, |
280 FakeVideoCaptureDeviceCommandLineTest, | 328 FakeVideoCaptureDeviceCommandLineTest, |
281 Values(CommandLineTestData{"fps=-1", 5}, | 329 Values(CommandLineTestData{"fps=-1", 5}, |
282 CommandLineTestData{"fps=29.97", 29.97f}, | 330 CommandLineTestData{"fps=29.97", 29.97f}, |
283 CommandLineTestData{"fps=60", 60}, | 331 CommandLineTestData{"fps=60", 60}, |
284 CommandLineTestData{"fps=1000", 60})); | 332 CommandLineTestData{"fps=1000", 60})); |
285 }; // namespace media | 333 }; // namespace media |
OLD | NEW |