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> |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 171 |
172 mojom::PhotoCapabilitiesPtr capabilities_; | 172 mojom::PhotoCapabilitiesPtr capabilities_; |
173 }; | 173 }; |
174 | 174 |
175 } // namespace | 175 } // namespace |
176 | 176 |
177 class FakeVideoCaptureDeviceBase : public ::testing::Test { | 177 class FakeVideoCaptureDeviceBase : public ::testing::Test { |
178 protected: | 178 protected: |
179 FakeVideoCaptureDeviceBase() | 179 FakeVideoCaptureDeviceBase() |
180 : loop_(new base::MessageLoop()), | 180 : loop_(new base::MessageLoop()), |
181 client_(new MockClient( | |
182 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, | |
183 base::Unretained(this)))), | |
184 device_enumeration_listener_(new DeviceEnumerationListener()), | 181 device_enumeration_listener_(new DeviceEnumerationListener()), |
185 image_capture_client_(new ImageCaptureClient()), | 182 image_capture_client_(new ImageCaptureClient()), |
186 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} | 183 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} |
187 | 184 |
188 void SetUp() override { EXPECT_CALL(*client_, OnError(_, _)).Times(0); } | 185 void SetUp() override {} |
| 186 |
| 187 std::unique_ptr<MockClient> CreateClient() { |
| 188 std::unique_ptr<MockClient> client = std::unique_ptr<MockClient>( |
| 189 new MockClient(base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, |
| 190 base::Unretained(this)))); |
| 191 EXPECT_CALL(*client, OnError(_, _)).Times(0); |
| 192 return client; |
| 193 } |
189 | 194 |
190 void OnFrameCaptured(const VideoCaptureFormat& format) { | 195 void OnFrameCaptured(const VideoCaptureFormat& format) { |
191 last_format_ = format; | 196 last_format_ = format; |
192 run_loop_->QuitClosure().Run(); | 197 run_loop_->QuitClosure().Run(); |
193 } | 198 } |
194 | 199 |
195 void WaitForCapturedFrame() { | 200 void WaitForCapturedFrame() { |
196 run_loop_.reset(new base::RunLoop()); | 201 run_loop_.reset(new base::RunLoop()); |
197 run_loop_->Run(); | 202 run_loop_->Run(); |
198 } | 203 } |
199 | 204 |
200 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDevices() { | 205 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDevices() { |
201 VideoCaptureDeviceDescriptors* descriptors; | 206 VideoCaptureDeviceDescriptors* descriptors; |
202 EXPECT_CALL(*device_enumeration_listener_.get(), | 207 EXPECT_CALL(*device_enumeration_listener_.get(), |
203 OnEnumeratedDevicesCallbackPtr(_)) | 208 OnEnumeratedDevicesCallbackPtr(_)) |
204 .WillOnce(SaveArg<0>(&descriptors)); | 209 .WillOnce(SaveArg<0>(&descriptors)); |
205 | 210 |
206 video_capture_device_factory_->EnumerateDeviceDescriptors( | 211 video_capture_device_factory_->EnumerateDeviceDescriptors( |
207 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, | 212 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, |
208 device_enumeration_listener_)); | 213 device_enumeration_listener_)); |
209 base::RunLoop().RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
210 return std::unique_ptr<VideoCaptureDeviceDescriptors>(descriptors); | 215 return std::unique_ptr<VideoCaptureDeviceDescriptors>(descriptors); |
211 } | 216 } |
212 | 217 |
213 const VideoCaptureFormat& last_format() const { return last_format_; } | 218 const VideoCaptureFormat& last_format() const { return last_format_; } |
214 | 219 |
215 VideoCaptureDeviceDescriptors descriptors_; | 220 VideoCaptureDeviceDescriptors descriptors_; |
216 const std::unique_ptr<base::MessageLoop> loop_; | 221 const std::unique_ptr<base::MessageLoop> loop_; |
217 std::unique_ptr<base::RunLoop> run_loop_; | 222 std::unique_ptr<base::RunLoop> run_loop_; |
218 std::unique_ptr<MockClient> client_; | |
219 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 223 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
220 const scoped_refptr<ImageCaptureClient> image_capture_client_; | 224 const scoped_refptr<ImageCaptureClient> image_capture_client_; |
221 VideoCaptureFormat last_format_; | 225 VideoCaptureFormat last_format_; |
222 const std::unique_ptr<VideoCaptureDeviceFactory> | 226 const std::unique_ptr<VideoCaptureDeviceFactory> |
223 video_capture_device_factory_; | 227 video_capture_device_factory_; |
224 }; | 228 }; |
225 | 229 |
226 class FakeVideoCaptureDeviceTest | 230 class FakeVideoCaptureDeviceTest |
227 : public FakeVideoCaptureDeviceBase, | 231 : public FakeVideoCaptureDeviceBase, |
228 public ::testing::WithParamInterface< | 232 public ::testing::WithParamInterface< |
(...skipping 15 matching lines...) Expand all Loading... |
244 EnumerateDevices()); | 248 EnumerateDevices()); |
245 ASSERT_FALSE(descriptors->empty()); | 249 ASSERT_FALSE(descriptors->empty()); |
246 | 250 |
247 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 251 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
248 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); | 252 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); |
249 ASSERT_TRUE(device); | 253 ASSERT_TRUE(device); |
250 | 254 |
251 VideoCaptureParams capture_params; | 255 VideoCaptureParams capture_params; |
252 capture_params.requested_format.frame_size.SetSize(640, 480); | 256 capture_params.requested_format.frame_size.SetSize(640, 480); |
253 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); | 257 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); |
254 device->AllocateAndStart(capture_params, std::move(client_)); | 258 device->AllocateAndStart(capture_params, CreateClient()); |
255 | 259 |
256 WaitForCapturedFrame(); | 260 WaitForCapturedFrame(); |
257 EXPECT_EQ(last_format().frame_size.width(), 640); | 261 EXPECT_EQ(last_format().frame_size.width(), 640); |
258 EXPECT_EQ(last_format().frame_size.height(), 480); | 262 EXPECT_EQ(last_format().frame_size.height(), 480); |
259 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); | 263 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); |
260 device->StopAndDeAllocate(); | 264 device->StopAndDeAllocate(); |
261 } | 265 } |
262 | 266 |
263 INSTANTIATE_TEST_CASE_P( | 267 INSTANTIATE_TEST_CASE_P( |
264 , | 268 , |
265 FakeVideoCaptureDeviceTest, | 269 FakeVideoCaptureDeviceTest, |
266 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, | 270 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, |
267 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), | 271 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), |
268 Values(20, 29.97, 30, 50, 60))); | 272 Values(20, 29.97, 30, 50, 60))); |
269 | 273 |
270 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 274 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
271 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 275 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
272 EnumerateDevices()); | 276 EnumerateDevices()); |
273 | 277 |
274 for (const auto& descriptors_iterator : *descriptors) { | 278 for (const auto& descriptors_iterator : *descriptors) { |
275 VideoCaptureFormats supported_formats; | 279 VideoCaptureFormats supported_formats; |
276 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, | 280 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, |
277 &supported_formats); | 281 &supported_formats); |
278 ASSERT_EQ(supported_formats.size(), 4u); | 282 ASSERT_EQ(supported_formats.size(), 4u); |
| 283 const std::string device_id = descriptors_iterator.device_id; |
| 284 VideoPixelFormat expected_format = |
| 285 (device_id == "/dev/video1") ? PIXEL_FORMAT_Y16 : PIXEL_FORMAT_I420; |
279 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); | 286 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); |
280 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); | 287 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); |
281 EXPECT_EQ(supported_formats[0].pixel_format, PIXEL_FORMAT_I420); | 288 EXPECT_EQ(supported_formats[0].pixel_format, expected_format); |
282 EXPECT_GE(supported_formats[0].frame_rate, 20.0); | 289 EXPECT_GE(supported_formats[0].frame_rate, 20.0); |
283 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); | 290 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); |
284 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); | 291 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); |
285 EXPECT_EQ(supported_formats[1].pixel_format, PIXEL_FORMAT_I420); | 292 EXPECT_EQ(supported_formats[1].pixel_format, expected_format); |
286 EXPECT_GE(supported_formats[1].frame_rate, 20.0); | 293 EXPECT_GE(supported_formats[1].frame_rate, 20.0); |
287 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 294 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
288 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 295 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
289 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); | 296 EXPECT_EQ(supported_formats[2].pixel_format, expected_format); |
290 EXPECT_GE(supported_formats[2].frame_rate, 20.0); | 297 EXPECT_GE(supported_formats[2].frame_rate, 20.0); |
291 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 298 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); |
292 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 299 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); |
293 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); | 300 EXPECT_EQ(supported_formats[3].pixel_format, expected_format); |
294 EXPECT_GE(supported_formats[3].frame_rate, 20.0); | 301 EXPECT_GE(supported_formats[3].frame_rate, 20.0); |
295 } | 302 } |
296 } | 303 } |
297 | 304 |
298 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { | 305 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { |
299 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 306 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
300 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 307 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); |
301 ASSERT_TRUE(device); | 308 ASSERT_TRUE(device); |
302 | 309 |
303 VideoCaptureParams capture_params; | 310 VideoCaptureParams capture_params; |
304 capture_params.requested_format.frame_size.SetSize(640, 480); | 311 capture_params.requested_format.frame_size.SetSize(640, 480); |
305 capture_params.requested_format.frame_rate = 30.0; | 312 capture_params.requested_format.frame_rate = 30.0; |
306 device->AllocateAndStart(capture_params, std::move(client_)); | 313 device->AllocateAndStart(capture_params, CreateClient()); |
307 | 314 |
308 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( | 315 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( |
309 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, | 316 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, |
310 image_capture_client_), | 317 image_capture_client_), |
311 base::Bind(&ImageCaptureClient::OnGetPhotoCapabilitiesFailure, | 318 base::Bind(&ImageCaptureClient::OnGetPhotoCapabilitiesFailure, |
312 image_capture_client_)); | 319 image_capture_client_)); |
313 | 320 |
314 EXPECT_CALL(*image_capture_client_.get(), OnCorrectGetPhotoCapabilities()) | 321 EXPECT_CALL(*image_capture_client_.get(), OnCorrectGetPhotoCapabilities()) |
315 .Times(1); | 322 .Times(1); |
316 device->GetPhotoCapabilities(std::move(scoped_get_callback)); | 323 device->GetPhotoCapabilities(std::move(scoped_get_callback)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 } | 390 } |
384 | 391 |
385 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { | 392 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { |
386 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 393 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
387 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 394 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); |
388 ASSERT_TRUE(device); | 395 ASSERT_TRUE(device); |
389 | 396 |
390 VideoCaptureParams capture_params; | 397 VideoCaptureParams capture_params; |
391 capture_params.requested_format.frame_size.SetSize(640, 480); | 398 capture_params.requested_format.frame_size.SetSize(640, 480); |
392 capture_params.requested_format.frame_rate = 30.0; | 399 capture_params.requested_format.frame_rate = 30.0; |
393 device->AllocateAndStart(capture_params, std::move(client_)); | 400 device->AllocateAndStart(capture_params, CreateClient()); |
394 | 401 |
395 VideoCaptureDevice::TakePhotoCallback scoped_callback( | 402 VideoCaptureDevice::TakePhotoCallback scoped_callback( |
396 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), | 403 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), |
397 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, | 404 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, |
398 image_capture_client_)); | 405 image_capture_client_)); |
399 | 406 |
400 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); | 407 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); |
401 device->TakePhoto(std::move(scoped_callback)); | 408 device->TakePhoto(std::move(scoped_callback)); |
402 | 409 |
403 run_loop_.reset(new base::RunLoop()); | 410 run_loop_.reset(new base::RunLoop()); |
404 run_loop_->Run(); | 411 run_loop_->Run(); |
405 device->StopAndDeAllocate(); | 412 device->StopAndDeAllocate(); |
406 } | 413 } |
407 | 414 |
408 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { | 415 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { |
409 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 416 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
410 switches::kUseFakeDeviceForMediaStream, GetParam().argument); | 417 switches::kUseFakeDeviceForMediaStream, GetParam().argument); |
411 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 418 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
412 EnumerateDevices()); | 419 EnumerateDevices()); |
413 ASSERT_FALSE(descriptors->empty()); | 420 ASSERT_FALSE(descriptors->empty()); |
414 | 421 |
415 for (const auto& descriptors_iterator : *descriptors) { | 422 for (const auto& descriptors_iterator : *descriptors) { |
416 std::unique_ptr<VideoCaptureDevice> device = | 423 std::unique_ptr<VideoCaptureDevice> device = |
417 video_capture_device_factory_->CreateDevice(descriptors_iterator); | 424 video_capture_device_factory_->CreateDevice(descriptors_iterator); |
418 ASSERT_TRUE(device); | 425 ASSERT_TRUE(device); |
419 | 426 |
420 VideoCaptureParams capture_params; | 427 VideoCaptureParams capture_params; |
421 capture_params.requested_format.frame_size.SetSize(1280, 720); | 428 capture_params.requested_format.frame_size.SetSize(1280, 720); |
422 capture_params.requested_format.frame_rate = GetParam().fps; | 429 capture_params.requested_format.frame_rate = GetParam().fps; |
423 device->AllocateAndStart(capture_params, std::move(client_)); | 430 device->AllocateAndStart(capture_params, CreateClient()); |
424 | 431 |
425 WaitForCapturedFrame(); | 432 WaitForCapturedFrame(); |
426 EXPECT_EQ(last_format().frame_size.width(), 1280); | 433 EXPECT_EQ(last_format().frame_size.width(), 1280); |
427 EXPECT_EQ(last_format().frame_size.height(), 720); | 434 EXPECT_EQ(last_format().frame_size.height(), 720); |
428 EXPECT_EQ(last_format().frame_rate, GetParam().fps); | 435 EXPECT_EQ(last_format().frame_rate, GetParam().fps); |
429 device->StopAndDeAllocate(); | 436 device->StopAndDeAllocate(); |
430 } | 437 } |
431 } | 438 } |
432 | 439 |
433 INSTANTIATE_TEST_CASE_P(, | 440 INSTANTIATE_TEST_CASE_P(, |
434 FakeVideoCaptureDeviceCommandLineTest, | 441 FakeVideoCaptureDeviceCommandLineTest, |
435 Values(CommandLineTestData{"fps=-1", 5}, | 442 Values(CommandLineTestData{"fps=-1", 5}, |
436 CommandLineTestData{"fps=29.97", 29.97f}, | 443 CommandLineTestData{"fps=29.97", 29.97f}, |
437 CommandLineTestData{"fps=60", 60}, | 444 CommandLineTestData{"fps=60", 60}, |
438 CommandLineTestData{"fps=1000", 60})); | 445 CommandLineTestData{"fps=1000", 60})); |
439 }; // namespace media | 446 }; // namespace media |
OLD | NEW |