| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 base::TimeTicks reference_time, | 81 base::TimeTicks reference_time, |
| 82 base::TimeDelta timestamp, | 82 base::TimeDelta timestamp, |
| 83 int frame_feedback_id) override { | 83 int frame_feedback_id) override { |
| 84 frame_cb_.Run(format); | 84 frame_cb_.Run(format); |
| 85 } | 85 } |
| 86 // Virtual methods for capturing using Client's Buffers. | 86 // Virtual methods for capturing using Client's Buffers. |
| 87 std::unique_ptr<Buffer> ReserveOutputBuffer(const gfx::Size& dimensions, | 87 std::unique_ptr<Buffer> ReserveOutputBuffer(const gfx::Size& dimensions, |
| 88 media::VideoPixelFormat format, | 88 media::VideoPixelFormat format, |
| 89 media::VideoPixelStorage storage, | 89 media::VideoPixelStorage storage, |
| 90 int frame_feedback_id) override { | 90 int frame_feedback_id) override { |
| 91 EXPECT_TRUE((format == media::PIXEL_FORMAT_ARGB && | 91 EXPECT_TRUE(storage == media::PIXEL_STORAGE_CPU); |
| 92 storage == media::PIXEL_STORAGE_CPU)); | |
| 93 EXPECT_GT(dimensions.GetArea(), 0); | 92 EXPECT_GT(dimensions.GetArea(), 0); |
| 94 const VideoCaptureFormat frame_format(dimensions, 0.0, format); | 93 const VideoCaptureFormat frame_format(dimensions, 0.0, format); |
| 95 return base::MakeUnique<MockBuffer>(0, frame_feedback_id, | 94 return base::MakeUnique<MockBuffer>(0, frame_feedback_id, |
| 96 frame_format.ImageAllocationSize()); | 95 frame_format.ImageAllocationSize()); |
| 97 } | 96 } |
| 98 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, | 97 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, |
| 99 const VideoCaptureFormat& format, | 98 const VideoCaptureFormat& format, |
| 100 base::TimeTicks reference_time, | 99 base::TimeTicks reference_time, |
| 101 base::TimeDelta timestamp) override { | 100 base::TimeDelta timestamp) override { |
| 102 frame_cb_.Run(format); | 101 frame_cb_.Run(format); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 226 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
| 228 const scoped_refptr<ImageCaptureClient> image_capture_client_; | 227 const scoped_refptr<ImageCaptureClient> image_capture_client_; |
| 229 VideoCaptureFormat last_format_; | 228 VideoCaptureFormat last_format_; |
| 230 const std::unique_ptr<VideoCaptureDeviceFactory> | 229 const std::unique_ptr<VideoCaptureDeviceFactory> |
| 231 video_capture_device_factory_; | 230 video_capture_device_factory_; |
| 232 }; | 231 }; |
| 233 | 232 |
| 234 class FakeVideoCaptureDeviceTest | 233 class FakeVideoCaptureDeviceTest |
| 235 : public FakeVideoCaptureDeviceBase, | 234 : public FakeVideoCaptureDeviceBase, |
| 236 public ::testing::WithParamInterface< | 235 public ::testing::WithParamInterface< |
| 237 ::testing::tuple<FakeVideoCaptureDevice::BufferOwnership, float>> {}; | 236 ::testing::tuple<VideoPixelFormat, |
| 237 FakeVideoCaptureDeviceMaker::DeliveryMode, |
| 238 float>> {}; |
| 238 | 239 |
| 239 struct CommandLineTestData { | 240 // Tests that a frame is delivered with the expected settings. |
| 240 // Command line argument | 241 // Sweeps through a fixed set of requested/expected resolutions. |
| 241 std::string argument; | |
| 242 // Expected values | |
| 243 float fps; | |
| 244 size_t device_count; | |
| 245 }; | |
| 246 | |
| 247 class FakeVideoCaptureDeviceCommandLineTest | |
| 248 : public FakeVideoCaptureDeviceBase, | |
| 249 public ::testing::WithParamInterface<CommandLineTestData> {}; | |
| 250 | |
| 251 TEST_P(FakeVideoCaptureDeviceTest, CaptureUsing) { | 242 TEST_P(FakeVideoCaptureDeviceTest, CaptureUsing) { |
| 252 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 243 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
| 253 EnumerateDevices()); | 244 EnumerateDevices()); |
| 254 ASSERT_FALSE(descriptors->empty()); | 245 ASSERT_FALSE(descriptors->empty()); |
| 255 | 246 |
| 256 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 247 auto device = FakeVideoCaptureDeviceMaker::MakeInstance( |
| 257 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); | 248 testing::get<0>(GetParam()), testing::get<1>(GetParam()), |
| 249 testing::get<2>(GetParam())); |
| 258 ASSERT_TRUE(device); | 250 ASSERT_TRUE(device); |
| 259 | 251 |
| 260 VideoCaptureParams capture_params; | 252 // First: Requested, Second: Expected |
| 261 capture_params.requested_format.frame_size.SetSize(640, 480); | 253 std::vector<std::pair<gfx::Size, gfx::Size>> resolutions_to_test; |
| 262 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); | 254 resolutions_to_test.emplace_back(gfx::Size(640, 480), gfx::Size(640, 480)); |
| 263 device->AllocateAndStart(capture_params, std::move(client_)); | 255 resolutions_to_test.emplace_back(gfx::Size(104, 105), gfx::Size(320, 240)); |
| 256 resolutions_to_test.emplace_back(gfx::Size(0, 0), gfx::Size(96, 96)); |
| 257 resolutions_to_test.emplace_back(gfx::Size(0, 720), gfx::Size(96, 96)); |
| 258 resolutions_to_test.emplace_back(gfx::Size(1920, 1080), |
| 259 gfx::Size(1920, 1080)); |
| 264 | 260 |
| 265 WaitForCapturedFrame(); | 261 for (const auto& resolution : resolutions_to_test) { |
| 266 EXPECT_EQ(last_format().frame_size.width(), 640); | 262 auto client = CreateClient(); |
| 267 EXPECT_EQ(last_format().frame_size.height(), 480); | 263 EXPECT_CALL(*client, OnError(_, _)).Times(0); |
| 268 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); | 264 |
| 269 device->StopAndDeAllocate(); | 265 VideoCaptureParams capture_params; |
| 266 capture_params.requested_format.frame_size = resolution.first; |
| 267 capture_params.requested_format.frame_rate = testing::get<2>(GetParam()); |
| 268 device->AllocateAndStart(capture_params, std::move(client)); |
| 269 |
| 270 WaitForCapturedFrame(); |
| 271 EXPECT_EQ(resolution.second.width(), last_format().frame_size.width()); |
| 272 EXPECT_EQ(resolution.second.height(), last_format().frame_size.height()); |
| 273 EXPECT_EQ(last_format().pixel_format, testing::get<0>(GetParam())); |
| 274 EXPECT_EQ(last_format().frame_rate, testing::get<2>(GetParam())); |
| 275 device->StopAndDeAllocate(); |
| 276 } |
| 270 } | 277 } |
| 271 | 278 |
| 272 INSTANTIATE_TEST_CASE_P( | 279 INSTANTIATE_TEST_CASE_P( |
| 273 , | 280 , |
| 274 FakeVideoCaptureDeviceTest, | 281 FakeVideoCaptureDeviceTest, |
| 275 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, | 282 Combine( |
| 276 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), | 283 Values(PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_ARGB), |
| 277 Values(20, 29.97, 30, 50, 60))); | 284 Values(FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS, |
| 285 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_CLIENT_BUFFERS), |
| 286 Values(20, 29.97, 30, 50, 60))); |
| 278 | 287 |
| 279 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 288 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
| 280 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 289 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 281 switches::kUseFakeDeviceForMediaStream, "device-count=3"); | 290 switches::kUseFakeDeviceForMediaStream, "device-count=3"); |
| 282 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 291 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
| 283 EnumerateDevices()); | 292 EnumerateDevices()); |
| 284 ASSERT_EQ(3u, descriptors->size()); | 293 ASSERT_EQ(3u, descriptors->size()); |
| 285 | 294 |
| 286 for (const auto& descriptors_iterator : *descriptors) { | 295 for (const auto& descriptors_iterator : *descriptors) { |
| 287 VideoCaptureFormats supported_formats; | 296 VideoCaptureFormats supported_formats; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 308 EXPECT_EQ(expected_format, supported_formats[3].pixel_format); | 317 EXPECT_EQ(expected_format, supported_formats[3].pixel_format); |
| 309 EXPECT_GE(supported_formats[3].frame_rate, 20.0); | 318 EXPECT_GE(supported_formats[3].frame_rate, 20.0); |
| 310 EXPECT_EQ(1920, supported_formats[4].frame_size.width()); | 319 EXPECT_EQ(1920, supported_formats[4].frame_size.width()); |
| 311 EXPECT_EQ(1080, supported_formats[4].frame_size.height()); | 320 EXPECT_EQ(1080, supported_formats[4].frame_size.height()); |
| 312 EXPECT_EQ(expected_format, supported_formats[4].pixel_format); | 321 EXPECT_EQ(expected_format, supported_formats[4].pixel_format); |
| 313 EXPECT_GE(supported_formats[4].frame_rate, 20.0); | 322 EXPECT_GE(supported_formats[4].frame_rate, 20.0); |
| 314 } | 323 } |
| 315 } | 324 } |
| 316 | 325 |
| 317 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { | 326 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { |
| 318 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 327 auto device = FakeVideoCaptureDeviceMaker::MakeInstance( |
| 319 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 328 PIXEL_FORMAT_I420, |
| 329 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS, 30.0); |
| 320 ASSERT_TRUE(device); | 330 ASSERT_TRUE(device); |
| 321 | 331 |
| 322 VideoCaptureParams capture_params; | 332 VideoCaptureParams capture_params; |
| 323 capture_params.requested_format.frame_size.SetSize(640, 480); | 333 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 324 capture_params.requested_format.frame_rate = 30.0; | 334 capture_params.requested_format.frame_rate = 30.0; |
| 325 device->AllocateAndStart(capture_params, std::move(client_)); | 335 device->AllocateAndStart(capture_params, std::move(client_)); |
| 326 | 336 |
| 327 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( | 337 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( |
| 328 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, | 338 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, |
| 329 image_capture_client_), | 339 image_capture_client_), |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 device->GetPhotoCapabilities(std::move(scoped_get_callback2)); | 427 device->GetPhotoCapabilities(std::move(scoped_get_callback2)); |
| 418 run_loop_.reset(new base::RunLoop()); | 428 run_loop_.reset(new base::RunLoop()); |
| 419 run_loop_->Run(); | 429 run_loop_->Run(); |
| 420 EXPECT_EQ(max_zoom_value, | 430 EXPECT_EQ(max_zoom_value, |
| 421 image_capture_client_->capabilities()->zoom->current); | 431 image_capture_client_->capabilities()->zoom->current); |
| 422 | 432 |
| 423 device->StopAndDeAllocate(); | 433 device->StopAndDeAllocate(); |
| 424 } | 434 } |
| 425 | 435 |
| 426 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { | 436 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { |
| 427 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 437 auto device = FakeVideoCaptureDeviceMaker::MakeInstance( |
| 428 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 438 PIXEL_FORMAT_I420, |
| 439 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS, 30.0); |
| 429 ASSERT_TRUE(device); | 440 ASSERT_TRUE(device); |
| 430 | 441 |
| 431 VideoCaptureParams capture_params; | 442 VideoCaptureParams capture_params; |
| 432 capture_params.requested_format.frame_size.SetSize(640, 480); | 443 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 433 capture_params.requested_format.frame_rate = 30.0; | 444 capture_params.requested_format.frame_rate = 30.0; |
| 434 device->AllocateAndStart(capture_params, std::move(client_)); | 445 device->AllocateAndStart(capture_params, std::move(client_)); |
| 435 | 446 |
| 436 VideoCaptureDevice::TakePhotoCallback scoped_callback( | 447 VideoCaptureDevice::TakePhotoCallback scoped_callback( |
| 437 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), | 448 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), |
| 438 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, | 449 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, |
| 439 image_capture_client_)); | 450 image_capture_client_)); |
| 440 | 451 |
| 441 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); | 452 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); |
| 442 device->TakePhoto(std::move(scoped_callback)); | 453 device->TakePhoto(std::move(scoped_callback)); |
| 443 | 454 |
| 444 run_loop_.reset(new base::RunLoop()); | 455 run_loop_.reset(new base::RunLoop()); |
| 445 run_loop_->Run(); | 456 run_loop_->Run(); |
| 446 device->StopAndDeAllocate(); | 457 device->StopAndDeAllocate(); |
| 447 } | 458 } |
| 448 | 459 |
| 449 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRateAndDeviceCount) { | 460 struct CommandLineTestData { |
| 461 std::string switch_value_string; |
| 462 float expected_fps; |
| 463 size_t expected_device_count; |
| 464 std::vector<VideoPixelFormat> expected_pixel_formats; |
| 465 }; |
| 466 |
| 467 class FakeVideoCaptureDeviceFactoryTest |
| 468 : public FakeVideoCaptureDeviceBase, |
| 469 public ::testing::WithParamInterface<CommandLineTestData> {}; |
| 470 |
| 471 // Tests that the FakeVideoCaptureDeviceFactory delivers the expected number |
| 472 // of devices and formats when being configured using command-line switches. |
| 473 TEST_P(FakeVideoCaptureDeviceFactoryTest, FrameRateAndDeviceCount) { |
| 450 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 474 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 451 switches::kUseFakeDeviceForMediaStream, GetParam().argument); | 475 switches::kUseFakeDeviceForMediaStream, GetParam().switch_value_string); |
| 452 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 476 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
| 453 EnumerateDevices()); | 477 EnumerateDevices()); |
| 454 EXPECT_EQ(descriptors->size(), GetParam().device_count); | 478 EXPECT_EQ(GetParam().expected_device_count, descriptors->size()); |
| 455 ASSERT_FALSE(descriptors->empty()); | 479 ASSERT_FALSE(descriptors->empty()); |
| 456 | 480 |
| 481 int device_index = 0; |
| 457 for (const auto& descriptors_iterator : *descriptors) { | 482 for (const auto& descriptors_iterator : *descriptors) { |
| 483 media::VideoCaptureFormats supported_formats; |
| 484 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, |
| 485 &supported_formats); |
| 486 for (const auto& supported_formats_entry : supported_formats) { |
| 487 EXPECT_EQ(GetParam().expected_pixel_formats[device_index], |
| 488 supported_formats_entry.pixel_format); |
| 489 } |
| 490 |
| 458 std::unique_ptr<VideoCaptureDevice> device = | 491 std::unique_ptr<VideoCaptureDevice> device = |
| 459 video_capture_device_factory_->CreateDevice(descriptors_iterator); | 492 video_capture_device_factory_->CreateDevice(descriptors_iterator); |
| 460 ASSERT_TRUE(device); | 493 ASSERT_TRUE(device); |
| 461 | 494 |
| 462 VideoCaptureParams capture_params; | 495 VideoCaptureParams capture_params; |
| 463 capture_params.requested_format.frame_size.SetSize(1280, 720); | 496 capture_params.requested_format.frame_size.SetSize(1280, 720); |
| 464 capture_params.requested_format.frame_rate = GetParam().fps; | 497 capture_params.requested_format.frame_rate = GetParam().expected_fps; |
| 498 capture_params.requested_format.pixel_format = |
| 499 GetParam().expected_pixel_formats[device_index]; |
| 465 device->AllocateAndStart(capture_params, CreateClient()); | 500 device->AllocateAndStart(capture_params, CreateClient()); |
| 466 WaitForCapturedFrame(); | 501 WaitForCapturedFrame(); |
| 467 EXPECT_EQ(last_format().frame_size.width(), 1280); | 502 EXPECT_EQ(1280, last_format().frame_size.width()); |
| 468 EXPECT_EQ(last_format().frame_size.height(), 720); | 503 EXPECT_EQ(720, last_format().frame_size.height()); |
| 469 EXPECT_EQ(last_format().frame_rate, GetParam().fps); | 504 EXPECT_EQ(GetParam().expected_fps, last_format().frame_rate); |
| 505 EXPECT_EQ(GetParam().expected_pixel_formats[device_index], |
| 506 last_format().pixel_format); |
| 470 device->StopAndDeAllocate(); | 507 device->StopAndDeAllocate(); |
| 508 |
| 509 device_index++; |
| 471 } | 510 } |
| 472 } | 511 } |
| 473 | 512 |
| 474 INSTANTIATE_TEST_CASE_P( | 513 INSTANTIATE_TEST_CASE_P( |
| 475 , | 514 , |
| 476 FakeVideoCaptureDeviceCommandLineTest, | 515 FakeVideoCaptureDeviceFactoryTest, |
| 477 Values(CommandLineTestData{"fps=-1", 5, 1u}, | 516 Values( |
| 478 CommandLineTestData{"fps=29.97, device-count=1", 29.97f, 1u}, | 517 CommandLineTestData{"fps=-1", 5, 1u, |
| 479 CommandLineTestData{"fps=60, device-count=2", 60, 2u}, | 518 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})}, |
| 480 CommandLineTestData{"fps=1000, device-count=-1", 60, 1u}, | 519 CommandLineTestData{"fps=29.97,device-count=1", 29.97f, 1u, |
| 481 CommandLineTestData{"device-count=2", 20, 2u}, | 520 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})}, |
| 482 CommandLineTestData{"device-count=0", 20, 1u})); | 521 CommandLineTestData{"fps=60,device-count=2", 60, 2u, |
| 522 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420, |
| 523 PIXEL_FORMAT_Y16})}, |
| 524 CommandLineTestData{"fps=1000,device-count=-1", 60, 1u, |
| 525 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})}, |
| 526 CommandLineTestData{ |
| 527 "device-count=3", 20, 3u, |
| 528 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, |
| 529 PIXEL_FORMAT_I420})}, |
| 530 CommandLineTestData{ |
| 531 "device-count=3,ownership=client", 20, 3u, |
| 532 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, |
| 533 PIXEL_FORMAT_I420})}, |
| 534 CommandLineTestData{ |
| 535 "device-count=0", 20, 1u, |
| 536 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})})); |
| 483 }; // namespace media | 537 }; // namespace media |
| OLD | NEW |