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/video_capture.mojom.h" | 11 #include "content/common/video_capture.mojom.h" |
12 #include "content/renderer/media/video_capture_impl.h" | 12 #include "content/renderer/media/video_capture_impl.h" |
| 13 #include "mojo/public/cpp/system/platform_handle.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 using ::testing::_; | 17 using ::testing::_; |
17 using ::testing::Invoke; | 18 using ::testing::Invoke; |
18 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
19 using ::testing::SaveArg; | 20 using ::testing::SaveArg; |
20 using ::testing::WithArgs; | 21 using ::testing::WithArgs; |
21 | 22 |
22 namespace content { | 23 namespace content { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 70 |
70 private: | 71 private: |
71 int released_buffer_count_; | 72 int released_buffer_count_; |
72 | 73 |
73 DISALLOW_COPY_AND_ASSIGN(MockMojoVideoCaptureHost); | 74 DISALLOW_COPY_AND_ASSIGN(MockMojoVideoCaptureHost); |
74 }; | 75 }; |
75 | 76 |
76 // This class encapsulates a VideoCaptureImpl under test and the necessary | 77 // This class encapsulates a VideoCaptureImpl under test and the necessary |
77 // accessory classes, namely: | 78 // accessory classes, namely: |
78 // - a VideoCaptureMessageFilter; | 79 // - a VideoCaptureMessageFilter; |
79 // - a MockVideoCaptureHost, mimicking the RendererHost; | 80 // - a MockMojoVideoCaptureHost, mimicking the RendererHost; |
80 // - a few callbacks that are bound when calling operations of VideoCaptureImpl | 81 // - a few callbacks that are bound when calling operations of VideoCaptureImpl |
81 // and on which we set expectations. | 82 // and on which we set expectations. |
82 class VideoCaptureImplTest : public ::testing::Test { | 83 class VideoCaptureImplTest : public ::testing::Test { |
83 public: | 84 public: |
84 VideoCaptureImplTest() | 85 VideoCaptureImplTest() |
85 : message_filter_(new VideoCaptureMessageFilter), | 86 : message_filter_(new VideoCaptureMessageFilter), |
86 video_capture_impl_( | 87 video_capture_impl_( |
87 new VideoCaptureImpl(kSessionId, | 88 new VideoCaptureImpl(kSessionId, |
88 message_filter_.get(), | 89 message_filter_.get(), |
89 base::ThreadTaskRunnerHandle::Get())) { | 90 base::ThreadTaskRunnerHandle::Get())) { |
(...skipping 23 matching lines...) Expand all Loading... |
113 base::Bind(&VideoCaptureImplTest::OnFrameReady, base::Unretained(this)); | 114 base::Bind(&VideoCaptureImplTest::OnFrameReady, base::Unretained(this)); |
114 | 115 |
115 video_capture_impl_->StartCapture(client_id, params, state_update_callback, | 116 video_capture_impl_->StartCapture(client_id, params, state_update_callback, |
116 frame_ready_callback); | 117 frame_ready_callback); |
117 } | 118 } |
118 | 119 |
119 void StopCapture(int client_id) { | 120 void StopCapture(int client_id) { |
120 video_capture_impl_->StopCapture(client_id); | 121 video_capture_impl_->StopCapture(client_id); |
121 } | 122 } |
122 | 123 |
123 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { | 124 void SimulateOnBufferCreated(int buffer_id, const base::SharedMemory& shm) { |
| 125 auto handle = base::SharedMemory::DuplicateHandle(shm.handle()); |
124 video_capture_impl_->OnBufferCreated( | 126 video_capture_impl_->OnBufferCreated( |
125 base::SharedMemory::DuplicateHandle(shm.handle()), | 127 buffer_id, mojo::WrapSharedMemoryHandle(handle, shm.mapped_size(), |
126 shm.mapped_size(), buffer_id); | 128 true /* read_only */)); |
127 } | 129 } |
128 | 130 |
129 void BufferReceived(int buffer_id, const gfx::Size& size) { | 131 void SimulateBufferReceived(int buffer_id, const gfx::Size& size) { |
130 mojom::VideoFrameInfoPtr info = mojom::VideoFrameInfo::New(); | 132 mojom::VideoFrameInfoPtr info = mojom::VideoFrameInfo::New(); |
131 | 133 |
132 const base::TimeTicks now = base::TimeTicks::Now(); | 134 const base::TimeTicks now = base::TimeTicks::Now(); |
133 media::VideoFrameMetadata frame_metadata; | 135 media::VideoFrameMetadata frame_metadata; |
134 frame_metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, now); | 136 frame_metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, now); |
135 frame_metadata.MergeInternalValuesInto(&info->metadata); | 137 frame_metadata.MergeInternalValuesInto(&info->metadata); |
136 | 138 |
137 info->timestamp = now - base::TimeTicks(); | 139 info->timestamp = now - base::TimeTicks(); |
138 info->pixel_format = media::PIXEL_FORMAT_I420; | 140 info->pixel_format = media::PIXEL_FORMAT_I420; |
139 info->storage_type = media::PIXEL_STORAGE_CPU; | 141 info->storage_type = media::PIXEL_STORAGE_CPU; |
140 info->coded_size = size; | 142 info->coded_size = size; |
141 info->visible_rect = gfx::Rect(size); | 143 info->visible_rect = gfx::Rect(size); |
142 | 144 |
143 video_capture_impl_->OnBufferReady(buffer_id, std::move(info)); | 145 video_capture_impl_->OnBufferReady(buffer_id, std::move(info)); |
144 } | 146 } |
145 | 147 |
146 void BufferDestroyed(int buffer_id) { | 148 void SimulateBufferDestroyed(int buffer_id) { |
147 video_capture_impl_->OnBufferDestroyed(buffer_id); | 149 video_capture_impl_->OnBufferDestroyed(buffer_id); |
148 } | 150 } |
149 | 151 |
150 void GetDeviceSupportedFormats() { | 152 void GetDeviceSupportedFormats() { |
151 const base::Callback<void(const media::VideoCaptureFormats&)> | 153 const base::Callback<void(const media::VideoCaptureFormats&)> |
152 callback = base::Bind( | 154 callback = base::Bind( |
153 &VideoCaptureImplTest::OnDeviceSupportedFormats, | 155 &VideoCaptureImplTest::OnDeviceSupportedFormats, |
154 base::Unretained(this)); | 156 base::Unretained(this)); |
155 video_capture_impl_->GetDeviceSupportedFormats(callback); | 157 video_capture_impl_->GetDeviceSupportedFormats(callback); |
156 } | 158 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 269 |
268 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 270 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
269 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 271 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
270 EXPECT_CALL(*this, OnFrameReady(_, _)); | 272 EXPECT_CALL(*this, OnFrameReady(_, _)); |
271 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 273 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
272 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 274 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
273 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)) | 275 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)) |
274 .Times(0); | 276 .Times(0); |
275 | 277 |
276 StartCapture(0, params_small_); | 278 StartCapture(0, params_small_); |
277 NewBuffer(kBufferId, shm); | 279 SimulateOnBufferCreated(kBufferId, shm); |
278 BufferReceived(kBufferId, params_small_.requested_format.frame_size); | 280 SimulateBufferReceived(kBufferId, params_small_.requested_format.frame_size); |
279 StopCapture(0); | 281 StopCapture(0); |
280 BufferDestroyed(kBufferId); | 282 SimulateBufferDestroyed(kBufferId); |
281 | 283 |
282 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0); | 284 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0); |
283 } | 285 } |
284 | 286 |
285 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | 287 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { |
286 const int kBufferId = 12; | 288 const int kBufferId = 12; |
287 | 289 |
288 base::SharedMemory shm; | 290 base::SharedMemory shm; |
289 const size_t frame_size = media::VideoFrame::AllocationSize( | 291 const size_t frame_size = media::VideoFrame::AllocationSize( |
290 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | 292 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); |
291 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | 293 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); |
292 | 294 |
293 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 295 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
294 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 296 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
295 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | 297 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); |
296 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); | 298 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); |
297 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 299 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
298 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)); | 300 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _, _)); |
299 | 301 |
300 StartCapture(0, params_large_); | 302 StartCapture(0, params_large_); |
301 NewBuffer(kBufferId, shm); | 303 SimulateOnBufferCreated(kBufferId, shm); |
302 StopCapture(0); | 304 StopCapture(0); |
303 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). | 305 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). |
304 BufferReceived(kBufferId, params_large_.requested_format.frame_size); | 306 SimulateBufferReceived(kBufferId, params_large_.requested_format.frame_size); |
305 BufferDestroyed(kBufferId); | 307 SimulateBufferDestroyed(kBufferId); |
306 | 308 |
307 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); | 309 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); |
308 } | 310 } |
309 | 311 |
310 TEST_F(VideoCaptureImplTest, AlreadyStarted) { | 312 TEST_F(VideoCaptureImplTest, AlreadyStarted) { |
311 media::VideoCaptureParams params = {}; | 313 media::VideoCaptureParams params = {}; |
312 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 314 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
313 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 315 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
314 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) | 316 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) |
315 .WillOnce(SaveArg<2>(¶ms)); | 317 .WillOnce(SaveArg<2>(¶ms)); |
(...skipping 24 matching lines...) Expand all Loading... |
340 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 342 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
341 | 343 |
342 StartCapture(0, params_small_); | 344 StartCapture(0, params_small_); |
343 | 345 |
344 OnStateChanged(mojom::VideoCaptureState::FAILED); | 346 OnStateChanged(mojom::VideoCaptureState::FAILED); |
345 | 347 |
346 StopCapture(0); | 348 StopCapture(0); |
347 } | 349 } |
348 | 350 |
349 } // namespace content | 351 } // namespace content |
OLD | NEW |