| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 11 #include "content/common/media/video_capture_messages.h" | |
| 12 #include "content/common/video_capture.mojom.h" | 11 #include "content/common/video_capture.mojom.h" |
| 13 #include "content/renderer/media/video_capture_impl.h" | 12 #include "content/renderer/media/video_capture_impl.h" |
| 14 #include "media/base/bind_to_current_loop.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 15 |
| 18 using ::testing::_; | 16 using ::testing::_; |
| 19 using ::testing::Invoke; | 17 using ::testing::Invoke; |
| 20 using ::testing::InvokeWithoutArgs; | 18 using ::testing::InvokeWithoutArgs; |
| 21 using ::testing::SaveArg; | 19 using ::testing::SaveArg; |
| 22 using ::testing::WithArgs; | 20 using ::testing::WithArgs; |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 | 23 |
| 26 const int kSessionId = 1; | 24 const int kSessionId = 1; |
| 27 | 25 |
| 28 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) { | 26 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) { |
| 29 media::VideoCaptureFormats formats; | 27 media::VideoCaptureFormats formats; |
| 30 callback.Run(formats); | 28 callback.Run(formats); |
| 31 } | 29 } |
| 32 | 30 |
| 33 // Mock implementation of the Mojo service. TODO(mcasas): Replace completely | 31 // Mock implementation of the Mojo Host service. |
| 34 // MockVideoCaptureMessageFilter, https://crbug.com/651897 | |
| 35 class MockMojoVideoCaptureHost : public mojom::VideoCaptureHost { | 32 class MockMojoVideoCaptureHost : public mojom::VideoCaptureHost { |
| 36 public: | 33 public: |
| 37 MockMojoVideoCaptureHost() : released_buffer_count_(0) { | 34 MockMojoVideoCaptureHost() : released_buffer_count_(0) { |
| 38 ON_CALL(*this, GetDeviceSupportedFormats(_, _, _)) | 35 ON_CALL(*this, GetDeviceSupportedFormats(_, _, _)) |
| 39 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); | 36 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); |
| 40 ON_CALL(*this, GetDeviceFormatsInUse(_, _, _)) | 37 ON_CALL(*this, GetDeviceFormatsInUse(_, _, _)) |
| 41 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); | 38 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); |
| 42 ON_CALL(*this, ReleaseBuffer(_, _, _, _)) | 39 ON_CALL(*this, ReleaseBuffer(_, _, _, _)) |
| 43 .WillByDefault(InvokeWithoutArgs( | 40 .WillByDefault(InvokeWithoutArgs( |
| 44 this, &MockMojoVideoCaptureHost::increase_released_buffer_count)); | 41 this, &MockMojoVideoCaptureHost::increase_released_buffer_count)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 69 | 66 |
| 70 int released_buffer_count() const { return released_buffer_count_; } | 67 int released_buffer_count() const { return released_buffer_count_; } |
| 71 void increase_released_buffer_count() { released_buffer_count_++; } | 68 void increase_released_buffer_count() { released_buffer_count_++; } |
| 72 | 69 |
| 73 private: | 70 private: |
| 74 int released_buffer_count_; | 71 int released_buffer_count_; |
| 75 | 72 |
| 76 DISALLOW_COPY_AND_ASSIGN(MockMojoVideoCaptureHost); | 73 DISALLOW_COPY_AND_ASSIGN(MockMojoVideoCaptureHost); |
| 77 }; | 74 }; |
| 78 | 75 |
| 79 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { | 76 // This class encapsulates a VideoCaptureImpl under test and the necessary |
| 80 public: | 77 // accessory classes, namely: |
| 81 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} | 78 // - a VideoCaptureMessageFilter; |
| 82 | 79 // - a MockVideoCaptureHost, mimicking the RendererHost; |
| 83 // Filter implementation. | 80 // - a few callbacks that are bound when calling operations of VideoCaptureImpl |
| 84 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 81 // and on which we set expectations. |
| 85 | |
| 86 protected: | |
| 87 virtual ~MockVideoCaptureMessageFilter() {} | |
| 88 | |
| 89 private: | |
| 90 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); | |
| 91 }; | |
| 92 | |
| 93 class VideoCaptureImplTest : public ::testing::Test { | 82 class VideoCaptureImplTest : public ::testing::Test { |
| 94 public: | 83 public: |
| 95 VideoCaptureImplTest() | 84 VideoCaptureImplTest() |
| 96 : message_filter_(new MockVideoCaptureMessageFilter), | 85 : message_filter_(new VideoCaptureMessageFilter), |
| 97 video_capture_impl_( | 86 video_capture_impl_( |
| 98 new VideoCaptureImpl(kSessionId, | 87 new VideoCaptureImpl(kSessionId, |
| 99 message_filter_.get(), | 88 message_filter_.get(), |
| 100 base::ThreadTaskRunnerHandle::Get())) { | 89 base::ThreadTaskRunnerHandle::Get())) { |
| 101 params_small_.requested_format = media::VideoCaptureFormat( | 90 params_small_.requested_format = media::VideoCaptureFormat( |
| 102 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 91 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
| 103 params_large_.requested_format = media::VideoCaptureFormat( | 92 params_large_.requested_format = media::VideoCaptureFormat( |
| 104 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 93 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
| 105 | 94 |
| 106 video_capture_impl_->SetVideoCaptureHostForTesting( | 95 video_capture_impl_->SetVideoCaptureHostForTesting( |
| 107 &mock_video_capture_host_); | 96 &mock_video_capture_host_); |
| 108 video_capture_impl_->device_id_ = 2; | 97 video_capture_impl_->device_id_ = 2; |
| 109 } | 98 } |
| 110 | 99 |
| 111 protected: | 100 protected: |
| 112 // These four mocks are used to create callbacks for the different oeprations. | 101 // These four mocks are used to create callbacks for the different oeprations. |
| 113 MOCK_METHOD2(OnFrameReady, | 102 MOCK_METHOD2(OnFrameReady, |
| 114 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); | 103 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); |
| 115 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); | 104 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); |
| 116 MOCK_METHOD1(OnDeviceFormatsInUse, void(const media::VideoCaptureFormats&)); | 105 MOCK_METHOD1(OnDeviceFormatsInUse, void(const media::VideoCaptureFormats&)); |
| 117 MOCK_METHOD1(OnDeviceSupportedFormats, | 106 MOCK_METHOD1(OnDeviceSupportedFormats, |
| 118 void(const media::VideoCaptureFormats&)); | 107 void(const media::VideoCaptureFormats&)); |
| 119 | 108 |
| 120 void StartCapture(int client_id, const media::VideoCaptureParams& params) { | 109 void StartCapture(int client_id, const media::VideoCaptureParams& params) { |
| 121 video_capture_impl_->StartCapture( | 110 const auto state_update_callback = base::Bind( |
| 122 client_id, params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, | 111 &VideoCaptureImplTest::OnStateUpdate, base::Unretained(this)); |
| 123 base::Unretained(this)), | 112 const auto frame_ready_callback = |
| 124 base::Bind(&VideoCaptureImplTest::OnFrameReady, | 113 base::Bind(&VideoCaptureImplTest::OnFrameReady, base::Unretained(this)); |
| 125 base::Unretained(this))); | 114 |
| 115 video_capture_impl_->StartCapture(client_id, params, state_update_callback, |
| 116 frame_ready_callback); |
| 126 } | 117 } |
| 127 | 118 |
| 128 void StopCapture(int client_id) { | 119 void StopCapture(int client_id) { |
| 129 video_capture_impl_->StopCapture(client_id); | 120 video_capture_impl_->StopCapture(client_id); |
| 130 } | 121 } |
| 131 | 122 |
| 132 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { | 123 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { |
| 133 video_capture_impl_->OnBufferCreated( | 124 video_capture_impl_->OnBufferCreated( |
| 134 base::SharedMemory::DuplicateHandle(shm.handle()), | 125 base::SharedMemory::DuplicateHandle(shm.handle()), |
| 135 shm.mapped_size(), buffer_id); | 126 shm.mapped_size(), buffer_id); |
| 136 } | 127 } |
| 137 | 128 |
| 138 void BufferReceived(int buffer_id, const gfx::Size& size) { | 129 void BufferReceived(int buffer_id, const gfx::Size& size) { |
| 139 base::TimeTicks now = base::TimeTicks::Now(); | 130 mojom::VideoFrameInfoPtr info = mojom::VideoFrameInfo::New(); |
| 140 base::TimeDelta timestamp = now - base::TimeTicks(); | |
| 141 | 131 |
| 132 const base::TimeTicks now = base::TimeTicks::Now(); |
| 142 media::VideoFrameMetadata frame_metadata; | 133 media::VideoFrameMetadata frame_metadata; |
| 143 frame_metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, now); | 134 frame_metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, now); |
| 144 base::DictionaryValue metadata; | 135 frame_metadata.MergeInternalValuesInto(&info->metadata); |
| 145 frame_metadata.MergeInternalValuesInto(&metadata); | |
| 146 | 136 |
| 147 video_capture_impl_->OnBufferReceived( | 137 info->timestamp = now - base::TimeTicks(); |
| 148 buffer_id, timestamp, metadata, media::PIXEL_FORMAT_I420, | 138 info->pixel_format = media::mojom::VideoFormat::I420; |
| 149 media::VideoFrame::STORAGE_SHMEM, size, gfx::Rect(size)); | 139 info->storage_type = media::PIXEL_STORAGE_CPU; |
| 140 info->coded_size = size; |
| 141 info->visible_rect = gfx::Rect(size); |
| 142 |
| 143 video_capture_impl_->OnBufferReady(buffer_id, std::move(info)); |
| 150 } | 144 } |
| 151 | 145 |
| 152 void BufferDestroyed(int buffer_id) { | 146 void BufferDestroyed(int buffer_id) { |
| 153 video_capture_impl_->OnBufferDestroyed(buffer_id); | 147 video_capture_impl_->OnBufferDestroyed(buffer_id); |
| 154 } | 148 } |
| 155 | 149 |
| 156 void GetDeviceSupportedFormats() { | 150 void GetDeviceSupportedFormats() { |
| 157 const base::Callback<void(const media::VideoCaptureFormats&)> | 151 const base::Callback<void(const media::VideoCaptureFormats&)> |
| 158 callback = base::Bind( | 152 callback = base::Bind( |
| 159 &VideoCaptureImplTest::OnDeviceSupportedFormats, | 153 &VideoCaptureImplTest::OnDeviceSupportedFormats, |
| 160 base::Unretained(this)); | 154 base::Unretained(this)); |
| 161 video_capture_impl_->GetDeviceSupportedFormats(callback); | 155 video_capture_impl_->GetDeviceSupportedFormats(callback); |
| 162 } | 156 } |
| 163 | 157 |
| 164 void GetDeviceFormatsInUse() { | 158 void GetDeviceFormatsInUse() { |
| 165 const base::Callback<void(const media::VideoCaptureFormats&)> | 159 const base::Callback<void(const media::VideoCaptureFormats&)> |
| 166 callback = base::Bind( | 160 callback = base::Bind( |
| 167 &VideoCaptureImplTest::OnDeviceFormatsInUse, | 161 &VideoCaptureImplTest::OnDeviceFormatsInUse, |
| 168 base::Unretained(this)); | 162 base::Unretained(this)); |
| 169 video_capture_impl_->GetDeviceFormatsInUse(callback); | 163 video_capture_impl_->GetDeviceFormatsInUse(callback); |
| 170 } | 164 } |
| 171 | 165 |
| 172 void OnStateChanged(mojom::VideoCaptureState state) { | 166 void OnStateChanged(mojom::VideoCaptureState state) { |
| 173 video_capture_impl_->OnStateChanged(state); | 167 video_capture_impl_->OnStateChanged(state); |
| 174 } | 168 } |
| 175 | 169 |
| 176 const base::MessageLoop message_loop_; | 170 const base::MessageLoop message_loop_; |
| 177 const ChildProcess child_process_; | 171 const ChildProcess child_process_; |
| 178 const scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; | 172 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; |
| 179 const std::unique_ptr<VideoCaptureImpl> video_capture_impl_; | 173 const std::unique_ptr<VideoCaptureImpl> video_capture_impl_; |
| 180 MockMojoVideoCaptureHost mock_video_capture_host_; | 174 MockMojoVideoCaptureHost mock_video_capture_host_; |
| 181 media::VideoCaptureParams params_small_; | 175 media::VideoCaptureParams params_small_; |
| 182 media::VideoCaptureParams params_large_; | 176 media::VideoCaptureParams params_large_; |
| 183 | 177 |
| 184 private: | 178 private: |
| 185 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 179 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
| 186 }; | 180 }; |
| 187 | 181 |
| 188 TEST_F(VideoCaptureImplTest, Simple) { | 182 TEST_F(VideoCaptureImplTest, Simple) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // provided callback. | 251 // provided callback. |
| 258 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { | 252 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { |
| 259 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); | 253 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); |
| 260 EXPECT_CALL(mock_video_capture_host_, | 254 EXPECT_CALL(mock_video_capture_host_, |
| 261 GetDeviceFormatsInUse(_, kSessionId, _)); | 255 GetDeviceFormatsInUse(_, kSessionId, _)); |
| 262 | 256 |
| 263 GetDeviceFormatsInUse(); | 257 GetDeviceFormatsInUse(); |
| 264 } | 258 } |
| 265 | 259 |
| 266 TEST_F(VideoCaptureImplTest, BufferReceived) { | 260 TEST_F(VideoCaptureImplTest, BufferReceived) { |
| 261 const int kBufferId = 11; |
| 262 |
| 267 base::SharedMemory shm; | 263 base::SharedMemory shm; |
| 268 const size_t frame_size = media::VideoFrame::AllocationSize( | 264 const size_t frame_size = media::VideoFrame::AllocationSize( |
| 269 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size); | 265 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size); |
| 270 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | 266 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); |
| 271 | 267 |
| 272 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 268 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 273 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 269 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
| 274 EXPECT_CALL(*this, OnFrameReady(_, _)); | 270 EXPECT_CALL(*this, OnFrameReady(_, _)); |
| 275 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 271 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 276 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 272 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 277 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, _, _, _)).Times(0); | 273 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)) |
| 274 .Times(0); |
| 278 | 275 |
| 279 StartCapture(0, params_small_); | 276 StartCapture(0, params_small_); |
| 280 NewBuffer(0, shm); | 277 NewBuffer(kBufferId, shm); |
| 281 BufferReceived(0, params_small_.requested_format.frame_size); | 278 BufferReceived(kBufferId, params_small_.requested_format.frame_size); |
| 282 StopCapture(0); | 279 StopCapture(0); |
| 283 BufferDestroyed(0); | 280 BufferDestroyed(kBufferId); |
| 284 | 281 |
| 285 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0); | 282 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0); |
| 286 } | 283 } |
| 287 | 284 |
| 288 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | 285 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { |
| 286 const int kBufferId = 12; |
| 287 |
| 289 base::SharedMemory shm; | 288 base::SharedMemory shm; |
| 290 const size_t frame_size = media::VideoFrame::AllocationSize( | 289 const size_t frame_size = media::VideoFrame::AllocationSize( |
| 291 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | 290 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); |
| 292 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | 291 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); |
| 293 | 292 |
| 294 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 293 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 295 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 294 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
| 296 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | 295 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); |
| 297 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); | 296 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); |
| 298 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 297 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 299 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, _, _, _)); | 298 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)); |
| 300 | 299 |
| 301 StartCapture(0, params_large_); | 300 StartCapture(0, params_large_); |
| 302 NewBuffer(0, shm); | 301 NewBuffer(kBufferId, shm); |
| 303 StopCapture(0); | 302 StopCapture(0); |
| 304 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). | 303 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). |
| 305 BufferReceived(0, params_large_.requested_format.frame_size); | 304 BufferReceived(kBufferId, params_large_.requested_format.frame_size); |
| 306 BufferDestroyed(0); | 305 BufferDestroyed(kBufferId); |
| 307 | 306 |
| 308 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); | 307 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); |
| 309 } | 308 } |
| 310 | 309 |
| 311 TEST_F(VideoCaptureImplTest, AlreadyStarted) { | 310 TEST_F(VideoCaptureImplTest, AlreadyStarted) { |
| 312 media::VideoCaptureParams params = {}; | 311 media::VideoCaptureParams params = {}; |
| 313 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 312 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
| 314 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 313 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 315 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) | 314 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) |
| 316 .WillOnce(SaveArg<2>(¶ms)); | 315 .WillOnce(SaveArg<2>(¶ms)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 341 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 340 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 342 | 341 |
| 343 StartCapture(0, params_small_); | 342 StartCapture(0, params_small_); |
| 344 | 343 |
| 345 OnStateChanged(mojom::VideoCaptureState::FAILED); | 344 OnStateChanged(mojom::VideoCaptureState::FAILED); |
| 346 | 345 |
| 347 StopCapture(0); | 346 StopCapture(0); |
| 348 } | 347 } |
| 349 | 348 |
| 350 } // namespace content | 349 } // namespace content |
| OLD | NEW |