| 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 "device/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 "device/capture/video/fake_video_capture_device_factory.h" |
| 21 #include "device/capture/video/video_capture_device.h" |
| 20 #include "media/base/media_switches.h" | 22 #include "media/base/media_switches.h" |
| 21 #include "media/base/video_capture_types.h" | 23 #include "media/base/video_capture_types.h" |
| 22 #include "media/capture/video/fake_video_capture_device_factory.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 | 26 |
| 27 using ::testing::_; | 27 using ::testing::_; |
| 28 using ::testing::Bool; | 28 using ::testing::Bool; |
| 29 using ::testing::Combine; | 29 using ::testing::Combine; |
| 30 using ::testing::SaveArg; | 30 using ::testing::SaveArg; |
| 31 using ::testing::Values; | 31 using ::testing::Values; |
| 32 | 32 |
| 33 namespace media { | 33 namespace device { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // This class is a Client::Buffer that allocates and frees the requested |size|. | 37 // This class is a Client::Buffer that allocates and frees the requested |size|. |
| 38 class MockBuffer : public VideoCaptureDevice::Client::Buffer { | 38 class MockBuffer : public VideoCaptureDevice::Client::Buffer { |
| 39 public: | 39 public: |
| 40 MockBuffer(int buffer_id, size_t mapped_size) | 40 MockBuffer(int buffer_id, size_t mapped_size) |
| 41 : id_(buffer_id), | 41 : id_(buffer_id), |
| 42 mapped_size_(mapped_size), | 42 mapped_size_(mapped_size), |
| 43 data_(new uint8_t[mapped_size]) {} | 43 data_(new uint8_t[mapped_size]) {} |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, | 95 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, |
| 96 const VideoCaptureFormat& frame_format, | 96 const VideoCaptureFormat& frame_format, |
| 97 base::TimeTicks reference_time, | 97 base::TimeTicks reference_time, |
| 98 base::TimeDelta timestamp) { | 98 base::TimeDelta timestamp) { |
| 99 frame_cb_.Run(frame_format); | 99 frame_cb_.Run(frame_format); |
| 100 } | 100 } |
| 101 void OnIncomingCapturedVideoFrame( | 101 void OnIncomingCapturedVideoFrame( |
| 102 std::unique_ptr<Buffer> buffer, | 102 std::unique_ptr<Buffer> buffer, |
| 103 const scoped_refptr<media::VideoFrame>& frame) { | 103 const scoped_refptr<media::VideoFrame>& frame) { |
| 104 VideoCaptureFormat format(frame->natural_size(), 30.0, | 104 VideoCaptureFormat format(frame->natural_size(), 30.0, |
| 105 PIXEL_FORMAT_I420); | 105 media::PIXEL_FORMAT_I420); |
| 106 frame_cb_.Run(format); | 106 frame_cb_.Run(format); |
| 107 } | 107 } |
| 108 std::unique_ptr<Buffer> ResurrectLastOutputBuffer( | 108 std::unique_ptr<Buffer> ResurrectLastOutputBuffer( |
| 109 const gfx::Size& dimensions, | 109 const gfx::Size& dimensions, |
| 110 media::VideoPixelFormat format, | 110 media::VideoPixelFormat format, |
| 111 media::VideoPixelStorage storage) { | 111 media::VideoPixelStorage storage) { |
| 112 return std::unique_ptr<Buffer>(); | 112 return std::unique_ptr<Buffer>(); |
| 113 } | 113 } |
| 114 double GetBufferPoolUtilization() const override { return 0.0; } | 114 double GetBufferPoolUtilization() const override { return 0.0; } |
| 115 | 115 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 129 } | 129 } |
| 130 | 130 |
| 131 private: | 131 private: |
| 132 friend class base::RefCounted<DeviceEnumerationListener>; | 132 friend class base::RefCounted<DeviceEnumerationListener>; |
| 133 virtual ~DeviceEnumerationListener() {} | 133 virtual ~DeviceEnumerationListener() {} |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 class ImageCaptureClient : public base::RefCounted<ImageCaptureClient> { | 136 class ImageCaptureClient : public base::RefCounted<ImageCaptureClient> { |
| 137 public: | 137 public: |
| 138 // GMock doesn't support move-only arguments, so we use this forward method. | 138 // GMock doesn't support move-only arguments, so we use this forward method. |
| 139 void DoOnGetPhotoCapabilities(mojom::PhotoCapabilitiesPtr capabilities) { | 139 void DoOnGetPhotoCapabilities( |
| 140 media::mojom::PhotoCapabilitiesPtr capabilities) { |
| 140 capabilities_ = std::move(capabilities); | 141 capabilities_ = std::move(capabilities); |
| 141 OnCorrectGetPhotoCapabilities(); | 142 OnCorrectGetPhotoCapabilities(); |
| 142 } | 143 } |
| 143 MOCK_METHOD0(OnCorrectGetPhotoCapabilities, void(void)); | 144 MOCK_METHOD0(OnCorrectGetPhotoCapabilities, void(void)); |
| 144 MOCK_METHOD1(OnGetPhotoCapabilitiesFailure, | 145 MOCK_METHOD1( |
| 145 void(const base::Callback<void(mojom::PhotoCapabilitiesPtr)>&)); | 146 OnGetPhotoCapabilitiesFailure, |
| 147 void(const base::Callback<void(media::mojom::PhotoCapabilitiesPtr)>&)); |
| 146 | 148 |
| 147 const mojom::PhotoCapabilities* capabilities() { return capabilities_.get(); } | 149 const media::mojom::PhotoCapabilities* capabilities() { |
| 150 return capabilities_.get(); |
| 151 } |
| 148 | 152 |
| 149 MOCK_METHOD1(OnCorrectSetPhotoOptions, void(bool)); | 153 MOCK_METHOD1(OnCorrectSetPhotoOptions, void(bool)); |
| 150 MOCK_METHOD1(OnSetPhotoOptionsFailure, | 154 MOCK_METHOD1(OnSetPhotoOptionsFailure, |
| 151 void(const base::Callback<void(bool)>&)); | 155 void(const base::Callback<void(bool)>&)); |
| 152 | 156 |
| 153 // GMock doesn't support move-only arguments, so we use this forward method. | 157 // GMock doesn't support move-only arguments, so we use this forward method. |
| 154 void DoOnPhotoTaken(mojom::BlobPtr blob) { | 158 void DoOnPhotoTaken(media::mojom::BlobPtr blob) { |
| 155 // Only PNG images are supported right now. | 159 // Only PNG images are supported right now. |
| 156 EXPECT_STREQ("image/png", blob->mime_type.c_str()); | 160 EXPECT_STREQ("image/png", blob->mime_type.c_str()); |
| 157 // Not worth decoding the incoming data. Just check that the header is PNG. | 161 // Not worth decoding the incoming data. Just check that the header is PNG. |
| 158 // http://www.libpng.org/pub/png/spec/1.2/PNG-Rationale.html#R.PNG-file-sign
ature | 162 // http://www.libpng.org/pub/png/spec/1.2/PNG-Rationale.html#R.PNG-file-sign
ature |
| 159 ASSERT_GT(blob->data.size(), 4u); | 163 ASSERT_GT(blob->data.size(), 4u); |
| 160 EXPECT_EQ('P', blob->data[1]); | 164 EXPECT_EQ('P', blob->data[1]); |
| 161 EXPECT_EQ('N', blob->data[2]); | 165 EXPECT_EQ('N', blob->data[2]); |
| 162 EXPECT_EQ('G', blob->data[3]); | 166 EXPECT_EQ('G', blob->data[3]); |
| 163 OnCorrectPhotoTaken(); | 167 OnCorrectPhotoTaken(); |
| 164 } | 168 } |
| 165 MOCK_METHOD0(OnCorrectPhotoTaken, void(void)); | 169 MOCK_METHOD0(OnCorrectPhotoTaken, void(void)); |
| 166 MOCK_METHOD1(OnTakePhotoFailure, | 170 MOCK_METHOD1(OnTakePhotoFailure, |
| 167 void(const base::Callback<void(mojom::BlobPtr)>&)); | 171 void(const base::Callback<void(media::mojom::BlobPtr)>&)); |
| 168 | 172 |
| 169 private: | 173 private: |
| 170 friend class base::RefCounted<ImageCaptureClient>; | 174 friend class base::RefCounted<ImageCaptureClient>; |
| 171 virtual ~ImageCaptureClient() {} | 175 virtual ~ImageCaptureClient() {} |
| 172 | 176 |
| 173 mojom::PhotoCapabilitiesPtr capabilities_; | 177 media::mojom::PhotoCapabilitiesPtr capabilities_; |
| 174 }; | 178 }; |
| 175 | 179 |
| 176 } // namespace | 180 } // namespace |
| 177 | 181 |
| 178 class FakeVideoCaptureDeviceBase : public ::testing::Test { | 182 class FakeVideoCaptureDeviceBase : public ::testing::Test { |
| 179 protected: | 183 protected: |
| 180 FakeVideoCaptureDeviceBase() | 184 FakeVideoCaptureDeviceBase() |
| 181 : loop_(new base::MessageLoop()), | 185 : loop_(new base::MessageLoop()), |
| 182 client_(new MockClient( | 186 client_(new MockClient( |
| 183 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, | 187 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 276 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
| 273 EnumerateDevices()); | 277 EnumerateDevices()); |
| 274 | 278 |
| 275 for (const auto& descriptors_iterator : *descriptors) { | 279 for (const auto& descriptors_iterator : *descriptors) { |
| 276 VideoCaptureFormats supported_formats; | 280 VideoCaptureFormats supported_formats; |
| 277 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, | 281 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, |
| 278 &supported_formats); | 282 &supported_formats); |
| 279 ASSERT_EQ(supported_formats.size(), 4u); | 283 ASSERT_EQ(supported_formats.size(), 4u); |
| 280 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); | 284 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); |
| 281 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); | 285 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); |
| 282 EXPECT_EQ(supported_formats[0].pixel_format, PIXEL_FORMAT_I420); | 286 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); |
| 283 EXPECT_GE(supported_formats[0].frame_rate, 20.0); | 287 EXPECT_GE(supported_formats[0].frame_rate, 20.0); |
| 284 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); | 288 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); |
| 285 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); | 289 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); |
| 286 EXPECT_EQ(supported_formats[1].pixel_format, PIXEL_FORMAT_I420); | 290 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); |
| 287 EXPECT_GE(supported_formats[1].frame_rate, 20.0); | 291 EXPECT_GE(supported_formats[1].frame_rate, 20.0); |
| 288 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 292 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
| 289 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 293 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
| 290 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); | 294 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); |
| 291 EXPECT_GE(supported_formats[2].frame_rate, 20.0); | 295 EXPECT_GE(supported_formats[2].frame_rate, 20.0); |
| 292 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 296 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); |
| 293 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 297 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); |
| 294 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); | 298 EXPECT_EQ(supported_formats[3].pixel_format, media::PIXEL_FORMAT_I420); |
| 295 EXPECT_GE(supported_formats[3].frame_rate, 20.0); | 299 EXPECT_GE(supported_formats[3].frame_rate, 20.0); |
| 296 } | 300 } |
| 297 } | 301 } |
| 298 | 302 |
| 299 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { | 303 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { |
| 300 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 304 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
| 301 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 305 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); |
| 302 ASSERT_TRUE(device); | 306 ASSERT_TRUE(device); |
| 303 | 307 |
| 304 VideoCaptureParams capture_params; | 308 VideoCaptureParams capture_params; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 328 EXPECT_EQ(240u, capabilities->height->min); | 332 EXPECT_EQ(240u, capabilities->height->min); |
| 329 EXPECT_EQ(1080u, capabilities->height->max); | 333 EXPECT_EQ(1080u, capabilities->height->max); |
| 330 EXPECT_EQ(capture_params.requested_format.frame_size.width(), | 334 EXPECT_EQ(capture_params.requested_format.frame_size.width(), |
| 331 static_cast<int>(capabilities->width->current)); | 335 static_cast<int>(capabilities->width->current)); |
| 332 EXPECT_EQ(320u, capabilities->width->min); | 336 EXPECT_EQ(320u, capabilities->width->min); |
| 333 EXPECT_EQ(1920u, capabilities->width->max); | 337 EXPECT_EQ(1920u, capabilities->width->max); |
| 334 EXPECT_EQ(100u, capabilities->zoom->min); | 338 EXPECT_EQ(100u, capabilities->zoom->min); |
| 335 EXPECT_EQ(400u, capabilities->zoom->max); | 339 EXPECT_EQ(400u, capabilities->zoom->max); |
| 336 EXPECT_GE(capabilities->zoom->current, capabilities->zoom->min); | 340 EXPECT_GE(capabilities->zoom->current, capabilities->zoom->min); |
| 337 EXPECT_GE(capabilities->zoom->max, capabilities->zoom->current); | 341 EXPECT_GE(capabilities->zoom->max, capabilities->zoom->current); |
| 338 EXPECT_EQ(mojom::FocusMode::UNAVAILABLE, capabilities->focus_mode); | 342 EXPECT_EQ(media::mojom::FocusMode::UNAVAILABLE, capabilities->focus_mode); |
| 339 | 343 |
| 340 // Set options: zoom to the maximum value. | 344 // Set options: zoom to the maximum value. |
| 341 const unsigned int max_zoom_value = capabilities->zoom->max; | 345 const unsigned int max_zoom_value = capabilities->zoom->max; |
| 342 VideoCaptureDevice::SetPhotoOptionsCallback scoped_set_callback( | 346 VideoCaptureDevice::SetPhotoOptionsCallback scoped_set_callback( |
| 343 base::Bind(&ImageCaptureClient::OnCorrectSetPhotoOptions, | 347 base::Bind(&ImageCaptureClient::OnCorrectSetPhotoOptions, |
| 344 image_capture_client_), | 348 image_capture_client_), |
| 345 base::Bind(&ImageCaptureClient::OnSetPhotoOptionsFailure, | 349 base::Bind(&ImageCaptureClient::OnSetPhotoOptionsFailure, |
| 346 image_capture_client_)); | 350 image_capture_client_)); |
| 347 | 351 |
| 348 mojom::PhotoSettingsPtr settings = mojom::PhotoSettings::New(); | 352 media::mojom::PhotoSettingsPtr settings = media::mojom::PhotoSettings::New(); |
| 349 settings->zoom = max_zoom_value; | 353 settings->zoom = max_zoom_value; |
| 350 settings->has_zoom = true; | 354 settings->has_zoom = true; |
| 351 | 355 |
| 352 EXPECT_CALL(*image_capture_client_.get(), OnCorrectSetPhotoOptions(true)) | 356 EXPECT_CALL(*image_capture_client_.get(), OnCorrectSetPhotoOptions(true)) |
| 353 .Times(1); | 357 .Times(1); |
| 354 device->SetPhotoOptions(std::move(settings), std::move(scoped_set_callback)); | 358 device->SetPhotoOptions(std::move(settings), std::move(scoped_set_callback)); |
| 355 run_loop_.reset(new base::RunLoop()); | 359 run_loop_.reset(new base::RunLoop()); |
| 356 run_loop_->Run(); | 360 run_loop_->Run(); |
| 357 | 361 |
| 358 // Retrieve Capabilities again and check against the set values. | 362 // Retrieve Capabilities again and check against the set values. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 device->StopAndDeAllocate(); | 424 device->StopAndDeAllocate(); |
| 421 } | 425 } |
| 422 } | 426 } |
| 423 | 427 |
| 424 INSTANTIATE_TEST_CASE_P(, | 428 INSTANTIATE_TEST_CASE_P(, |
| 425 FakeVideoCaptureDeviceCommandLineTest, | 429 FakeVideoCaptureDeviceCommandLineTest, |
| 426 Values(CommandLineTestData{"fps=-1", 5}, | 430 Values(CommandLineTestData{"fps=-1", 5}, |
| 427 CommandLineTestData{"fps=29.97", 29.97f}, | 431 CommandLineTestData{"fps=29.97", 29.97f}, |
| 428 CommandLineTestData{"fps=60", 60}, | 432 CommandLineTestData{"fps=60", 60}, |
| 429 CommandLineTestData{"fps=1000", 60})); | 433 CommandLineTestData{"fps=1000", 60})); |
| 430 }; // namespace media | 434 }; // namespace device |
| OLD | NEW |