| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/video_capture_device_client.h" | 5 #include "media/capture/video/video_capture_device_client.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 class MockVideoCaptureController : public VideoCaptureController { | 34 class MockVideoCaptureController : public VideoCaptureController { |
| 35 public: | 35 public: |
| 36 explicit MockVideoCaptureController(int max_buffers) | 36 explicit MockVideoCaptureController(int max_buffers) |
| 37 : VideoCaptureController(max_buffers) {} | 37 : VideoCaptureController(max_buffers) {} |
| 38 ~MockVideoCaptureController() override {} | 38 ~MockVideoCaptureController() override {} |
| 39 | 39 |
| 40 MOCK_METHOD1(MockOnIncomingCapturedVideoFrame, void(const gfx::Size&)); | 40 MOCK_METHOD2(MockOnIncomingCapturedVideoFrame, |
| 41 void(const gfx::Size& coded_size, int buffer_id)); |
| 41 MOCK_METHOD0(OnError, void()); | 42 MOCK_METHOD0(OnError, void()); |
| 42 MOCK_METHOD1(OnLog, void(const std::string& message)); | 43 MOCK_METHOD1(OnLog, void(const std::string& message)); |
| 43 MOCK_METHOD1(OnBufferDestroyed, void(int buffer_id_to_drop)); | 44 MOCK_METHOD1(OnBufferDestroyed, void(int buffer_id_to_drop)); |
| 44 | 45 |
| 45 void OnIncomingCapturedVideoFrame( | 46 void OnIncomingCapturedVideoFrame( |
| 46 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 47 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 47 scoped_refptr<media::VideoFrame> frame) override { | 48 scoped_refptr<media::VideoFrame> frame) override { |
| 48 MockOnIncomingCapturedVideoFrame(frame->coded_size()); | 49 MockOnIncomingCapturedVideoFrame(frame->coded_size(), buffer->id()); |
| 49 } | 50 } |
| 50 }; | 51 }; |
| 51 | 52 |
| 53 class MockConsumerLoadObserver : public media::ConsumerLoadObserver { |
| 54 public: |
| 55 MOCK_METHOD2(OnConsumerReportingUtilization, |
| 56 void(int frame_id, double utilization)); |
| 57 }; |
| 58 |
| 52 // Note that this test does not exercise the class VideoCaptureDeviceClient | 59 // Note that this test does not exercise the class VideoCaptureDeviceClient |
| 53 // in isolation. The "unit under test" is an instance of | 60 // in isolation. The "unit under test" is an instance of |
| 54 // VideoCaptureDeviceClient with some context that is specific to | 61 // VideoCaptureDeviceClient with some context that is specific to |
| 55 // renderer_host/media, and therefore this test must live here and not in | 62 // renderer_host/media, and therefore this test must live here and not in |
| 56 // media/capture/video. | 63 // media/capture/video. |
| 57 class VideoCaptureDeviceClientTest : public ::testing::Test { | 64 class VideoCaptureDeviceClientTest : public ::testing::Test { |
| 58 public: | 65 public: |
| 59 VideoCaptureDeviceClientTest() | 66 VideoCaptureDeviceClientTest() |
| 60 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 67 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 61 controller_(new MockVideoCaptureController(1)), | 68 controller_(new MockVideoCaptureController(1)), |
| 62 device_client_(controller_->NewDeviceClient()) {} | 69 device_client_(controller_->NewDeviceClient()) {} |
| 63 ~VideoCaptureDeviceClientTest() override {} | 70 ~VideoCaptureDeviceClientTest() override {} |
| 64 | 71 |
| 65 void TearDown() override { base::RunLoop().RunUntilIdle(); } | 72 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
| 66 | 73 |
| 67 protected: | 74 protected: |
| 68 const content::TestBrowserThreadBundle thread_bundle_; | 75 const content::TestBrowserThreadBundle thread_bundle_; |
| 69 const std::unique_ptr<MockVideoCaptureController> controller_; | 76 const std::unique_ptr<MockVideoCaptureController> controller_; |
| 70 const std::unique_ptr<media::VideoCaptureDevice::Client> device_client_; | 77 const std::unique_ptr<media::VideoCaptureDeviceClient> device_client_; |
| 71 | 78 |
| 72 private: | 79 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceClientTest); | 80 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceClientTest); |
| 74 }; | 81 }; |
| 75 | 82 |
| 76 } // namespace | 83 } // namespace |
| 77 | 84 |
| 78 // A small test for reference and to verify VideoCaptureDeviceClient is | 85 // A small test for reference and to verify VideoCaptureDeviceClient is |
| 79 // minimally functional. | 86 // minimally functional. |
| 80 TEST_F(VideoCaptureDeviceClientTest, Minimal) { | 87 TEST_F(VideoCaptureDeviceClientTest, Minimal) { |
| 81 const size_t kScratchpadSizeInBytes = 400; | 88 const size_t kScratchpadSizeInBytes = 400; |
| 82 unsigned char data[kScratchpadSizeInBytes] = {}; | 89 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 83 const media::VideoCaptureFormat kFrameFormat( | 90 const media::VideoCaptureFormat kFrameFormat( |
| 84 gfx::Size(10, 10), 30.0f /*frame_rate*/, | 91 gfx::Size(10, 10), 30.0f /*frame_rate*/, |
| 85 media::PIXEL_FORMAT_I420, | 92 media::PIXEL_FORMAT_I420, |
| 86 media::PIXEL_STORAGE_CPU); | 93 media::PIXEL_STORAGE_CPU); |
| 87 DCHECK(device_client_.get()); | 94 DCHECK(device_client_.get()); |
| 88 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | 95 EXPECT_CALL(*controller_, OnLog(_)).Times(1); |
| 89 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | 96 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)).Times(1); |
| 90 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | 97 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, |
| 91 kFrameFormat, 0 /*clockwise rotation*/, | 98 kFrameFormat, 0 /*clockwise rotation*/, |
| 92 base::TimeTicks(), base::TimeDelta()); | 99 base::TimeTicks(), base::TimeDelta()); |
| 93 base::RunLoop().RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
| 94 Mock::VerifyAndClearExpectations(controller_.get()); | 101 Mock::VerifyAndClearExpectations(controller_.get()); |
| 95 } | 102 } |
| 96 | 103 |
| 97 // Tests that we don't try to pass on frames with an invalid frame format. | 104 // Tests that we don't try to pass on frames with an invalid frame format. |
| 98 TEST_F(VideoCaptureDeviceClientTest, FailsSilentlyGivenInvalidFrameFormat) { | 105 TEST_F(VideoCaptureDeviceClientTest, FailsSilentlyGivenInvalidFrameFormat) { |
| 99 const size_t kScratchpadSizeInBytes = 400; | 106 const size_t kScratchpadSizeInBytes = 400; |
| 100 unsigned char data[kScratchpadSizeInBytes] = {}; | 107 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 101 // kFrameFormat is invalid in a number of ways. | 108 // kFrameFormat is invalid in a number of ways. |
| 102 const media::VideoCaptureFormat kFrameFormat( | 109 const media::VideoCaptureFormat kFrameFormat( |
| 103 gfx::Size(media::limits::kMaxDimension + 1, media::limits::kMaxDimension), | 110 gfx::Size(media::limits::kMaxDimension + 1, media::limits::kMaxDimension), |
| 104 media::limits::kMaxFramesPerSecond + 1, | 111 media::limits::kMaxFramesPerSecond + 1, |
| 105 media::VideoPixelFormat::PIXEL_FORMAT_I420, | 112 media::VideoPixelFormat::PIXEL_FORMAT_I420, |
| 106 media::VideoPixelStorage::PIXEL_STORAGE_CPU); | 113 media::VideoPixelStorage::PIXEL_STORAGE_CPU); |
| 107 DCHECK(device_client_.get()); | 114 DCHECK(device_client_.get()); |
| 108 // Expect the the call to fail silently inside the VideoCaptureDeviceClient. | 115 // Expect the the call to fail silently inside the VideoCaptureDeviceClient. |
| 109 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | 116 EXPECT_CALL(*controller_, OnLog(_)).Times(1); |
| 110 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(0); | 117 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)).Times(0); |
| 111 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | 118 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, |
| 112 kFrameFormat, 0 /*clockwise rotation*/, | 119 kFrameFormat, 0 /*clockwise rotation*/, |
| 113 base::TimeTicks(), base::TimeDelta()); | 120 base::TimeTicks(), base::TimeDelta()); |
| 114 base::RunLoop().RunUntilIdle(); | 121 base::RunLoop().RunUntilIdle(); |
| 115 Mock::VerifyAndClearExpectations(controller_.get()); | 122 Mock::VerifyAndClearExpectations(controller_.get()); |
| 116 } | 123 } |
| 117 | 124 |
| 118 // Tests that we fail silently if no available buffers to use. | 125 // Tests that we fail silently if no available buffers to use. |
| 119 TEST_F(VideoCaptureDeviceClientTest, DropsFrameIfNoBuffer) { | 126 TEST_F(VideoCaptureDeviceClientTest, DropsFrameIfNoBuffer) { |
| 120 const size_t kScratchpadSizeInBytes = 400; | 127 const size_t kScratchpadSizeInBytes = 400; |
| 121 unsigned char data[kScratchpadSizeInBytes] = {}; | 128 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 122 const media::VideoCaptureFormat kFrameFormat( | 129 const media::VideoCaptureFormat kFrameFormat( |
| 123 gfx::Size(10, 10), 30.0f /*frame_rate*/, | 130 gfx::Size(10, 10), 30.0f /*frame_rate*/, |
| 124 media::PIXEL_FORMAT_I420, | 131 media::PIXEL_FORMAT_I420, |
| 125 media::PIXEL_STORAGE_CPU); | 132 media::PIXEL_STORAGE_CPU); |
| 126 // We expect the second frame to be silently dropped, so these should | 133 // We expect the second frame to be silently dropped, so these should |
| 127 // only be called once despite the two frames. | 134 // only be called once despite the two frames. |
| 128 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | 135 EXPECT_CALL(*controller_, OnLog(_)).Times(1); |
| 129 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | 136 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)).Times(1); |
| 130 // Pass two frames. The second will be dropped. | 137 // Pass two frames. The second will be dropped. |
| 131 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | 138 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, |
| 132 kFrameFormat, 0 /*clockwise rotation*/, | 139 kFrameFormat, 0 /*clockwise rotation*/, |
| 133 base::TimeTicks(), base::TimeDelta()); | 140 base::TimeTicks(), base::TimeDelta()); |
| 134 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, | 141 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, |
| 135 kFrameFormat, 0 /*clockwise rotation*/, | 142 kFrameFormat, 0 /*clockwise rotation*/, |
| 136 base::TimeTicks(), base::TimeDelta()); | 143 base::TimeTicks(), base::TimeDelta()); |
| 137 base::RunLoop().RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
| 138 Mock::VerifyAndClearExpectations(controller_.get()); | 145 Mock::VerifyAndClearExpectations(controller_.get()); |
| 139 } | 146 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 168 #endif | 175 #endif |
| 169 media::PIXEL_FORMAT_RGB32, | 176 media::PIXEL_FORMAT_RGB32, |
| 170 media::PIXEL_FORMAT_ARGB, | 177 media::PIXEL_FORMAT_ARGB, |
| 171 media::PIXEL_FORMAT_Y16, | 178 media::PIXEL_FORMAT_Y16, |
| 172 }; | 179 }; |
| 173 | 180 |
| 174 for (media::VideoPixelFormat format : kSupportedFormats) { | 181 for (media::VideoPixelFormat format : kSupportedFormats) { |
| 175 params.requested_format.pixel_format = format; | 182 params.requested_format.pixel_format = format; |
| 176 | 183 |
| 177 EXPECT_CALL(*controller_, OnLog(_)).Times(1); | 184 EXPECT_CALL(*controller_, OnLog(_)).Times(1); |
| 178 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)).Times(1); | 185 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)).Times(1); |
| 179 device_client_->OnIncomingCapturedData( | 186 device_client_->OnIncomingCapturedData( |
| 180 data, params.requested_format.ImageAllocationSize(), | 187 data, params.requested_format.ImageAllocationSize(), |
| 181 params.requested_format, 0 /* clockwise_rotation */, base::TimeTicks(), | 188 params.requested_format, 0 /* clockwise_rotation */, base::TimeTicks(), |
| 182 base::TimeDelta()); | 189 base::TimeDelta()); |
| 183 base::RunLoop().RunUntilIdle(); | 190 base::RunLoop().RunUntilIdle(); |
| 184 Mock::VerifyAndClearExpectations(controller_.get()); | 191 Mock::VerifyAndClearExpectations(controller_.get()); |
| 185 } | 192 } |
| 186 } | 193 } |
| 187 | 194 |
| 188 // Test that we receive the expected resolution for a given captured frame | 195 // Test that we receive the expected resolution for a given captured frame |
| (...skipping 23 matching lines...) Expand all Loading... |
| 212 | 219 |
| 213 media::VideoCaptureParams params; | 220 media::VideoCaptureParams params; |
| 214 for (const auto& size_and_rotation : kSizeAndRotations) { | 221 for (const auto& size_and_rotation : kSizeAndRotations) { |
| 215 ASSERT_GE(kScratchpadSizeInBytes, | 222 ASSERT_GE(kScratchpadSizeInBytes, |
| 216 size_and_rotation.input_resolution.GetArea() * 4u) | 223 size_and_rotation.input_resolution.GetArea() * 4u) |
| 217 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; | 224 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; |
| 218 params.requested_format = | 225 params.requested_format = |
| 219 media::VideoCaptureFormat(size_and_rotation.input_resolution, 30.0f, | 226 media::VideoCaptureFormat(size_and_rotation.input_resolution, 30.0f, |
| 220 media::PIXEL_FORMAT_ARGB); | 227 media::PIXEL_FORMAT_ARGB); |
| 221 gfx::Size coded_size; | 228 gfx::Size coded_size; |
| 222 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_)) | 229 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)) |
| 223 .Times(1) | 230 .Times(1) |
| 224 .WillOnce(SaveArg<0>(&coded_size)); | 231 .WillOnce(SaveArg<0>(&coded_size)); |
| 225 device_client_->OnIncomingCapturedData( | 232 device_client_->OnIncomingCapturedData( |
| 226 data, params.requested_format.ImageAllocationSize(), | 233 data, params.requested_format.ImageAllocationSize(), |
| 227 params.requested_format, size_and_rotation.rotation, base::TimeTicks(), | 234 params.requested_format, size_and_rotation.rotation, base::TimeTicks(), |
| 228 base::TimeDelta()); | 235 base::TimeDelta()); |
| 229 base::RunLoop().RunUntilIdle(); | 236 base::RunLoop().RunUntilIdle(); |
| 230 | 237 |
| 231 EXPECT_EQ(coded_size.width(), size_and_rotation.output_resolution.width()); | 238 EXPECT_EQ(coded_size.width(), size_and_rotation.output_resolution.width()); |
| 232 EXPECT_EQ(coded_size.height(), | 239 EXPECT_EQ(coded_size.height(), |
| 233 size_and_rotation.output_resolution.height()); | 240 size_and_rotation.output_resolution.height()); |
| 234 | 241 |
| 235 Mock::VerifyAndClearExpectations(controller_.get()); | 242 Mock::VerifyAndClearExpectations(controller_.get()); |
| 236 } | 243 } |
| 237 } | 244 } |
| 238 | 245 |
| 246 // Tests that consumer load reports arriving at VideoCaptureDeviceClient are |
| 247 // correctly forwarded to its ConsumerLoadObserver with the buffer_id being |
| 248 // translated to the frame_id. |
| 249 TEST_F(VideoCaptureDeviceClientTest, ReportConsumerLoad) { |
| 250 const size_t kScratchpadSizeInBytes = 400; |
| 251 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 252 const media::VideoCaptureFormat kFrameFormat( |
| 253 gfx::Size(10, 10), 30.0f /*frame_rate*/, |
| 254 media::PIXEL_FORMAT_I420, |
| 255 media::PIXEL_STORAGE_CPU); |
| 256 const int arbitrary_frame_id = 123; |
| 257 const double arbitrary_utilization = 0.789; |
| 258 DCHECK(device_client_.get()); |
| 259 MockConsumerLoadObserver load_observer; |
| 260 device_client_->SetConsumerLoadObserver(&load_observer); |
| 261 |
| 262 int buffer_id; |
| 263 EXPECT_CALL(*controller_, MockOnIncomingCapturedVideoFrame(_, _)) |
| 264 .Times(1) |
| 265 .WillOnce(SaveArg<1>(&buffer_id)); |
| 266 EXPECT_CALL(load_observer, OnConsumerReportingUtilization( |
| 267 arbitrary_frame_id, arbitrary_utilization)) |
| 268 .Times(1); |
| 269 |
| 270 device_client_->OnIncomingCapturedData(data, kScratchpadSizeInBytes, |
| 271 kFrameFormat, 0 /*clockwise rotation*/, |
| 272 base::TimeTicks(), base::TimeDelta(), |
| 273 arbitrary_frame_id); |
| 274 base::RunLoop().RunUntilIdle(); |
| 275 |
| 276 device_client_->OnReceiverReportingUtilization(buffer_id, |
| 277 arbitrary_utilization); |
| 278 |
| 279 base::RunLoop().RunUntilIdle(); |
| 280 Mock::VerifyAndClearExpectations(controller_.get()); |
| 281 } |
| 282 |
| 239 } // namespace content | 283 } // namespace content |
| OLD | NEW |