| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "media/capture/video/video_capture_device_client.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "content/browser/renderer_host/media/video_capture_controller.h" | |
| 19 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" | |
| 20 #include "content/browser/renderer_host/media/video_frame_receiver_on_io_thread.
h" | |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | |
| 22 #include "media/base/limits.h" | |
| 23 #include "media/capture/video/video_capture_buffer_pool_impl.h" | |
| 24 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | |
| 25 #include "testing/gmock/include/gmock/gmock.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | |
| 27 | |
| 28 using ::testing::_; | |
| 29 using ::testing::Mock; | |
| 30 using ::testing::InSequence; | |
| 31 using ::testing::SaveArg; | |
| 32 | |
| 33 namespace content { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 class MockVideoCaptureController : public VideoCaptureController { | |
| 38 public: | |
| 39 explicit MockVideoCaptureController() : VideoCaptureController() {} | |
| 40 ~MockVideoCaptureController() override {} | |
| 41 | |
| 42 MOCK_METHOD1(MockOnIncomingCapturedVideoFrame, void(const gfx::Size&)); | |
| 43 MOCK_METHOD0(OnError, void()); | |
| 44 MOCK_METHOD1(OnLog, void(const std::string& message)); | |
| 45 MOCK_METHOD1(OnBufferRetired, void(int buffer_id)); | |
| 46 | |
| 47 void OnIncomingCapturedVideoFrame( | |
| 48 media::VideoCaptureDevice::Client::Buffer buffer, | |
| 49 scoped_refptr<media::VideoFrame> frame) override { | |
| 50 MockOnIncomingCapturedVideoFrame(frame->coded_size()); | |
| 51 } | |
| 52 }; | |
| 53 | |
| 54 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( | |
| 55 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { | |
| 56 return base::MakeUnique<content::VideoCaptureGpuJpegDecoder>(decode_done_cb); | |
| 57 } | |
| 58 | |
| 59 // Test fixture for testing a unit consisting of an instance of | |
| 60 // VideoCaptureDeviceClient connected to a partly-mocked instance of | |
| 61 // VideoCaptureController, and an instance of VideoCaptureBufferPoolImpl | |
| 62 // as well as related threading glue that replicates how it is used in | |
| 63 // production. | |
| 64 class VideoCaptureDeviceClientTest : public ::testing::Test { | |
| 65 public: | |
| 66 VideoCaptureDeviceClientTest() | |
| 67 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
| 68 scoped_refptr<media::VideoCaptureBufferPoolImpl> buffer_pool( | |
| 69 new media::VideoCaptureBufferPoolImpl( | |
| 70 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | |
| 71 1)); | |
| 72 controller_ = base::MakeUnique<MockVideoCaptureController>(); | |
| 73 device_client_ = base::MakeUnique<media::VideoCaptureDeviceClient>( | |
| 74 base::MakeUnique<VideoFrameReceiverOnIOThread>( | |
| 75 controller_->GetWeakPtrForIOThread()), | |
| 76 buffer_pool, | |
| 77 base::Bind( | |
| 78 &CreateGpuJpegDecoder, | |
| 79 base::Bind(&media::VideoFrameReceiver::OnIncomingCapturedVideoFrame, | |
| 80 controller_->GetWeakPtrForIOThread()))); | |
| 81 } | |
| 82 ~VideoCaptureDeviceClientTest() override {} | |
| 83 | |
| 84 void TearDown() override { base::RunLoop().RunUntilIdle(); } | |
| 85 | |
| 86 protected: | |
| 87 const content::TestBrowserThreadBundle thread_bundle_; | |
| 88 std::unique_ptr<MockVideoCaptureController> controller_; | |
| 89 std::unique_ptr<media::VideoCaptureDeviceClient> device_client_; | |
| 90 | |
| 91 private: | |
| 92 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceClientTest); | |
| 93 }; | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 // A small test for reference and to verify VideoCaptureDeviceClient is | |
| 98 // minimally functional. | |
| 99 TEST_F(VideoCaptureDeviceClientTest, Minimal) { | |
| 100 const size_t kScratchpadSizeInBytes = 400; | |
| 101 unsigned char data[kScratchpadSizeInBytes] = {}; | |
| 102 const media::VideoCaptureFormat kFrameFormat( | |
| 103 gfx::Size(10, 10), 30.0f /*frame_rate*/, | |
| 104 media::PIXEL_FORMAT_I420, | |
| 105 media::PIXEL_STORAGE_CPU); | |
| 106 DCHECK(device_client_.get()); | |
| 107 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | |
| 108 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | |
| 109 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | |
| 110 kFrameFormat, 0 /*clockwise rotation*/, | |
| 111 base::TimeTicks(), base::TimeDelta()); | |
| 112 base::RunLoop().RunUntilIdle(); | |
| 113 Mock::VerifyAndClearExpectations(controller_.get()); | |
| 114 } | |
| 115 | |
| 116 // Tests that we don't try to pass on frames with an invalid frame format. | |
| 117 TEST_F(VideoCaptureDeviceClientTest, FailsSilentlyGivenInvalidFrameFormat) { | |
| 118 const size_t kScratchpadSizeInBytes = 400; | |
| 119 unsigned char data[kScratchpadSizeInBytes] = {}; | |
| 120 // kFrameFormat is invalid in a number of ways. | |
| 121 const media::VideoCaptureFormat kFrameFormat( | |
| 122 gfx::Size(media::limits::kMaxDimension + 1, media::limits::kMaxDimension), | |
| 123 media::limits::kMaxFramesPerSecond + 1, | |
| 124 media::VideoPixelFormat::PIXEL_FORMAT_I420, | |
| 125 media::VideoPixelStorage::PIXEL_STORAGE_CPU); | |
| 126 DCHECK(device_client_.get()); | |
| 127 // Expect the the call to fail silently inside the VideoCaptureDeviceClient. | |
| 128 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | |
| 129 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(0); | |
| 130 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | |
| 131 kFrameFormat, 0 /*clockwise rotation*/, | |
| 132 base::TimeTicks(), base::TimeDelta()); | |
| 133 base::RunLoop().RunUntilIdle(); | |
| 134 Mock::VerifyAndClearExpectations(controller_.get()); | |
| 135 } | |
| 136 | |
| 137 // Tests that we fail silently if no available buffers to use. | |
| 138 TEST_F(VideoCaptureDeviceClientTest, DropsFrameIfNoBuffer) { | |
| 139 const size_t kScratchpadSizeInBytes = 400; | |
| 140 unsigned char data[kScratchpadSizeInBytes] = {}; | |
| 141 const media::VideoCaptureFormat kFrameFormat( | |
| 142 gfx::Size(10, 10), 30.0f /*frame_rate*/, | |
| 143 media::PIXEL_FORMAT_I420, | |
| 144 media::PIXEL_STORAGE_CPU); | |
| 145 // We expect the second frame to be silently dropped, so these should | |
| 146 // only be called once despite the two frames. | |
| 147 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | |
| 148 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | |
| 149 // Pass two frames. The second will be dropped. | |
| 150 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | |
| 151 kFrameFormat, 0 /*clockwise rotation*/, | |
| 152 base::TimeTicks(), base::TimeDelta()); | |
| 153 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | |
| 154 kFrameFormat, 0 /*clockwise rotation*/, | |
| 155 base::TimeTicks(), base::TimeDelta()); | |
| 156 base::RunLoop().RunUntilIdle(); | |
| 157 Mock::VerifyAndClearExpectations(controller_.get()); | |
| 158 } | |
| 159 | |
| 160 // Tests that buffer-based capture API accepts some memory-backed pixel formats. | |
| 161 TEST_F(VideoCaptureDeviceClientTest, DataCaptureGoodPixelFormats) { | |
| 162 // The usual ReserveOutputBuffer() -> OnIncomingCapturedVideoFrame() cannot | |
| 163 // be used since it does not accept all pixel formats. The memory backed | |
| 164 // buffer OnIncomingCapturedData() is used instead, with a dummy scratchpad | |
| 165 // buffer. | |
| 166 const size_t kScratchpadSizeInBytes = 400; | |
| 167 unsigned char data[kScratchpadSizeInBytes] = {}; | |
| 168 const gfx::Size kCaptureResolution(10, 10); | |
| 169 ASSERT_GE(kScratchpadSizeInBytes, kCaptureResolution.GetArea() * 4u) | |
| 170 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; | |
| 171 | |
| 172 media::VideoCaptureParams params; | |
| 173 params.requested_format = media::VideoCaptureFormat( | |
| 174 kCaptureResolution, 30.0f, media::PIXEL_FORMAT_UNKNOWN); | |
| 175 | |
| 176 // Only use the VideoPixelFormats that we know supported. Do not add | |
| 177 // PIXEL_FORMAT_MJPEG since it would need a real JPEG header. | |
| 178 const media::VideoPixelFormat kSupportedFormats[] = { | |
| 179 media::PIXEL_FORMAT_I420, | |
| 180 media::PIXEL_FORMAT_YV12, | |
| 181 media::PIXEL_FORMAT_NV12, | |
| 182 media::PIXEL_FORMAT_NV21, | |
| 183 media::PIXEL_FORMAT_YUY2, | |
| 184 media::PIXEL_FORMAT_UYVY, | |
| 185 #if defined(OS_WIN) || defined(OS_LINUX) | |
| 186 media::PIXEL_FORMAT_RGB24, | |
| 187 #endif | |
| 188 media::PIXEL_FORMAT_RGB32, | |
| 189 media::PIXEL_FORMAT_ARGB, | |
| 190 media::PIXEL_FORMAT_Y16, | |
| 191 }; | |
| 192 | |
| 193 for (media::VideoPixelFormat format : kSupportedFormats) { | |
| 194 params.requested_format.pixel_format = format; | |
| 195 | |
| 196 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | |
| 197 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | |
| 198 device_client_->OnIncomingCapturedData( | |
| 199 data, params.requested_format.ImageAllocationSize(), | |
| 200 params.requested_format, 0 /* clockwise_rotation */, base::TimeTicks(), | |
| 201 base::TimeDelta()); | |
| 202 base::RunLoop().RunUntilIdle(); | |
| 203 Mock::VerifyAndClearExpectations(controller_.get()); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // Test that we receive the expected resolution for a given captured frame | |
| 208 // resolution and rotation. Odd resolutions are also cropped. | |
| 209 TEST_F(VideoCaptureDeviceClientTest, CheckRotationsAndCrops) { | |
| 210 const struct SizeAndRotation { | |
| 211 gfx::Size input_resolution; | |
| 212 int rotation; | |
| 213 gfx::Size output_resolution; | |
| 214 } kSizeAndRotations[] = {{{6, 4}, 0, {6, 4}}, | |
| 215 {{6, 4}, 90, {4, 6}}, | |
| 216 {{6, 4}, 180, {6, 4}}, | |
| 217 {{6, 4}, 270, {4, 6}}, | |
| 218 {{7, 4}, 0, {6, 4}}, | |
| 219 {{7, 4}, 90, {4, 6}}, | |
| 220 {{7, 4}, 180, {6, 4}}, | |
| 221 {{7, 4}, 270, {4, 6}}}; | |
| 222 | |
| 223 // The usual ReserveOutputBuffer() -> OnIncomingCapturedVideoFrame() cannot | |
| 224 // be used since it does not resolve rotations or crops. The memory backed | |
| 225 // buffer OnIncomingCapturedData() is used instead, with a dummy scratchpad | |
| 226 // buffer. | |
| 227 const size_t kScratchpadSizeInBytes = 400; | |
| 228 unsigned char data[kScratchpadSizeInBytes] = {}; | |
| 229 | |
| 230 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | |
| 231 | |
| 232 media::VideoCaptureParams params; | |
| 233 for (const auto& size_and_rotation : kSizeAndRotations) { | |
| 234 ASSERT_GE(kScratchpadSizeInBytes, | |
| 235 size_and_rotation.input_resolution.GetArea() * 4u) | |
| 236 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; | |
| 237 params.requested_format = | |
| 238 media::VideoCaptureFormat(size_and_rotation.input_resolution, 30.0f, | |
| 239 media::PIXEL_FORMAT_ARGB); | |
| 240 gfx::Size coded_size; | |
| 241 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)) | |
| 242 .Times(1) | |
| 243 .WillOnce(SaveArg<0>(&coded_size)); | |
| 244 device_client_->OnIncomingCapturedData( | |
| 245 data, params.requested_format.ImageAllocationSize(), | |
| 246 params.requested_format, size_and_rotation.rotation, base::TimeTicks(), | |
| 247 base::TimeDelta()); | |
| 248 base::RunLoop().RunUntilIdle(); | |
| 249 | |
| 250 EXPECT_EQ(coded_size.width(), size_and_rotation.output_resolution.width()); | |
| 251 EXPECT_EQ(coded_size.height(), | |
| 252 size_and_rotation.output_resolution.height()); | |
| 253 | |
| 254 Mock::VerifyAndClearExpectations(controller_.get()); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 } // namespace content | |
| OLD | NEW |