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 "base/memory/shared_memory.h" | |
5 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
6 #include "content/child/child_process.h" | 7 #include "content/child/child_process.h" |
7 #include "content/common/media/video_capture_messages.h" | 8 #include "content/common/media/video_capture_messages.h" |
8 #include "content/renderer/media/video_capture_impl.h" | 9 #include "content/renderer/media/video_capture_impl.h" |
9 #include "media/base/bind_to_current_loop.h" | 10 #include "media/base/bind_to_current_loop.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 using ::testing::_; | 14 using ::testing::_; |
14 using ::testing::AtLeast; | 15 using ::testing::AtLeast; |
(...skipping 12 matching lines...) Expand all Loading... | |
27 // Filter implementation. | 28 // Filter implementation. |
28 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 29 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
29 | 30 |
30 protected: | 31 protected: |
31 virtual ~MockVideoCaptureMessageFilter() {} | 32 virtual ~MockVideoCaptureMessageFilter() {} |
32 | 33 |
33 private: | 34 private: |
34 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); | 35 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); |
35 }; | 36 }; |
36 | 37 |
37 class VideoCaptureImplTest : public ::testing::Test { | 38 struct BufferReceivedTestArg { |
39 BufferReceivedTestArg(media::VideoPixelFormat pixel_format, | |
40 media::VideoCapturePixelFormat capture_pixel_format, | |
41 const gpu::MailboxHolder& mailbox_holder) | |
42 : pixel_format(pixel_format), capture_pixel_format(capture_pixel_format), | |
43 mailbox_holder(mailbox_holder) {} | |
44 | |
45 media::VideoPixelFormat pixel_format; | |
46 media::VideoCapturePixelFormat capture_pixel_format; | |
47 const gpu::MailboxHolder mailbox_holder; | |
48 }; | |
49 | |
50 static const BufferReceivedTestArg kBufferFormats[] = { | |
51 BufferReceivedTestArg(media::PIXEL_FORMAT_I420, | |
52 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420, | |
mcasas
2015/09/02 16:51:26
Heh incidentally there has been another CL
unifyin
| |
53 gpu::MailboxHolder()), | |
54 BufferReceivedTestArg(media::PIXEL_FORMAT_ARGB, | |
55 media::VIDEO_CAPTURE_PIXEL_FORMAT_ARGB, | |
56 gpu::MailboxHolder(gpu::Mailbox::Generate(), 0, 0))}; | |
57 | |
58 class VideoCaptureImplTest : | |
59 public ::testing::TestWithParam<BufferReceivedTestArg> { | |
38 public: | 60 public: |
39 class MockVideoCaptureImpl : public VideoCaptureImpl { | 61 class MockVideoCaptureImpl : public VideoCaptureImpl { |
40 public: | 62 public: |
41 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 63 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
42 VideoCaptureMessageFilter* filter) | 64 VideoCaptureMessageFilter* filter) |
43 : VideoCaptureImpl(id, filter) { | 65 : VideoCaptureImpl(id, filter), received_buffer_count_(0) { |
44 } | 66 } |
45 ~MockVideoCaptureImpl() override {} | 67 ~MockVideoCaptureImpl() override {} |
46 | 68 |
47 // Override Send() to mimic device to send events. | 69 // Override Send() to mimic device to send events. |
48 void Send(IPC::Message* message) override { | 70 void Send(IPC::Message* message) override { |
49 CHECK(message); | 71 CHECK(message); |
50 | 72 |
51 // In this method, messages are sent to the according handlers as if | 73 // In this method, messages are sent to the according handlers as if |
52 // we are the device. | 74 // we are the device. |
53 bool handled = true; | 75 bool handled = true; |
(...skipping 11 matching lines...) Expand all Loading... | |
65 IPC_END_MESSAGE_MAP() | 87 IPC_END_MESSAGE_MAP() |
66 EXPECT_TRUE(handled); | 88 EXPECT_TRUE(handled); |
67 delete message; | 89 delete message; |
68 } | 90 } |
69 | 91 |
70 void DeviceStartCapture(int device_id, | 92 void DeviceStartCapture(int device_id, |
71 media::VideoCaptureSessionId session_id, | 93 media::VideoCaptureSessionId session_id, |
72 const media::VideoCaptureParams& params) { | 94 const media::VideoCaptureParams& params) { |
73 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this | 95 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this |
74 // does not accurately reflect the behavior of the VideoCaptureHost. | 96 // does not accurately reflect the behavior of the VideoCaptureHost. |
75 capture_params_ = params; | |
76 } | 97 } |
77 | 98 |
78 void DevicePauseCapture(int device_id) {} | 99 void DevicePauseCapture(int device_id) {} |
79 | 100 |
80 void DeviceStopCapture(int device_id) { | 101 void DeviceStopCapture(int device_id) { |
81 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); | 102 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); |
82 } | 103 } |
83 | 104 |
84 void DeviceReceiveEmptyBuffer(int device_id, | 105 void DeviceReceiveEmptyBuffer(int device_id, |
85 int buffer_id, | 106 int buffer_id, |
86 uint32 sync_point, | 107 uint32 sync_point, |
87 double consumer_resource_utilization) {} | 108 double consumer_resource_utilization) { |
109 received_buffer_count_++; | |
110 } | |
88 | 111 |
89 void DeviceGetSupportedFormats(int device_id, | 112 void DeviceGetSupportedFormats(int device_id, |
90 media::VideoCaptureSessionId session_id) { | 113 media::VideoCaptureSessionId session_id) { |
91 // When the mock message filter receives a request for the device | 114 // When the mock message filter receives a request for the device |
92 // supported formats, replies immediately with an empty format list. | 115 // supported formats, replies immediately with an empty format list. |
93 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); | 116 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); |
94 } | 117 } |
95 | 118 |
96 void DeviceGetFormatsInUse(int device_id, | 119 void DeviceGetFormatsInUse(int device_id, |
97 media::VideoCaptureSessionId session_id) { | 120 media::VideoCaptureSessionId session_id) { |
98 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); | 121 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); |
99 } | 122 } |
100 | 123 |
101 void ReceiveStateChangeMessage(VideoCaptureState state) { | 124 void ReceiveStateChangeMessage(VideoCaptureState state) { |
102 OnStateChanged(state); | 125 OnStateChanged(state); |
103 } | 126 } |
104 | 127 |
105 const media::VideoCaptureParams& capture_params() const { | 128 int received_buffer_count() const { return received_buffer_count_; } |
106 return capture_params_; | |
107 } | |
108 | 129 |
109 private: | 130 private: |
110 media::VideoCaptureParams capture_params_; | 131 int received_buffer_count_; |
111 }; | 132 }; |
112 | 133 |
113 VideoCaptureImplTest() { | 134 VideoCaptureImplTest() { |
114 params_small_.requested_format = media::VideoCaptureFormat( | 135 params_small_.requested_format = media::VideoCaptureFormat( |
115 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 136 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
116 | 137 |
117 params_large_.requested_format = media::VideoCaptureFormat( | 138 params_large_.requested_format = media::VideoCaptureFormat( |
118 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 139 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
119 | 140 |
120 child_process_.reset(new ChildProcess()); | 141 child_process_.reset(new ChildProcess()); |
(...skipping 26 matching lines...) Expand all Loading... | |
147 void StartCapture(const media::VideoCaptureParams& params) { | 168 void StartCapture(const media::VideoCaptureParams& params) { |
148 video_capture_impl_->StartCapture( | 169 video_capture_impl_->StartCapture( |
149 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, | 170 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, |
150 base::Unretained(this)), | 171 base::Unretained(this)), |
151 base::Bind(&VideoCaptureImplTest::OnFrameReady, | 172 base::Bind(&VideoCaptureImplTest::OnFrameReady, |
152 base::Unretained(this))); | 173 base::Unretained(this))); |
153 } | 174 } |
154 | 175 |
155 void StopCapture() { video_capture_impl_->StopCapture(); } | 176 void StopCapture() { video_capture_impl_->StopCapture(); } |
156 | 177 |
178 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { | |
179 video_capture_impl_->OnBufferCreated( | |
180 base::SharedMemory::DuplicateHandle(shm.handle()), | |
181 shm.mapped_size(), buffer_id); | |
182 } | |
183 | |
184 void BufferReceived(int buffer_id, const gfx::Size& size, | |
185 media::VideoPixelFormat pixel_format, | |
186 const gpu::MailboxHolder& mailbox_holder) { | |
187 video_capture_impl_->OnBufferReceived( | |
188 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(), | |
189 pixel_format, media::VideoFrame::STORAGE_SHMEM, size, | |
190 gfx::Rect(size), mailbox_holder); | |
191 } | |
192 | |
193 void BufferDestroyed(int buffer_id) { | |
194 video_capture_impl_->OnBufferDestroyed(buffer_id); | |
195 } | |
196 | |
157 void DeInit() { | 197 void DeInit() { |
158 video_capture_impl_->DeInit(); | 198 video_capture_impl_->DeInit(); |
159 } | 199 } |
160 | 200 |
161 void GetDeviceSupportedFormats() { | 201 void GetDeviceSupportedFormats() { |
162 const base::Callback<void(const media::VideoCaptureFormats&)> | 202 const base::Callback<void(const media::VideoCaptureFormats&)> |
163 callback = base::Bind( | 203 callback = base::Bind( |
164 &VideoCaptureImplTest::OnDeviceSupportedFormats, | 204 &VideoCaptureImplTest::OnDeviceSupportedFormats, |
165 base::Unretained(this)); | 205 base::Unretained(this)); |
166 video_capture_impl_->GetDeviceSupportedFormats(callback); | 206 video_capture_impl_->GetDeviceSupportedFormats(callback); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the | 261 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the |
222 // provided callback. | 262 // provided callback. |
223 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { | 263 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { |
224 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); | 264 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); |
225 | 265 |
226 Init(); | 266 Init(); |
227 GetDeviceFormatsInUse(); | 267 GetDeviceFormatsInUse(); |
228 DeInit(); | 268 DeInit(); |
229 } | 269 } |
230 | 270 |
271 TEST_P(VideoCaptureImplTest, BufferReceivedWithFormat) { | |
272 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
273 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
274 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1); | |
275 | |
276 const BufferReceivedTestArg& buffer_arg = GetParam(); | |
277 const gfx::Size size(1280, 720); | |
278 | |
279 // Create a fake shared memory for buffer. | |
280 base::SharedMemory shm; | |
281 const size_t frame_size = media::VideoFrame::AllocationSize( | |
282 buffer_arg.pixel_format, size); | |
283 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | |
284 | |
285 media::VideoCaptureParams params; | |
286 params.requested_format = media::VideoCaptureFormat( | |
287 size, 30, buffer_arg.capture_pixel_format); | |
288 | |
289 Init(); | |
290 StartCapture(params); | |
291 NewBuffer(0, shm); | |
292 BufferReceived(0, size, buffer_arg.pixel_format, buffer_arg.mailbox_holder); | |
293 StopCapture(); | |
294 BufferDestroyed(0); | |
295 DeInit(); | |
296 } | |
297 | |
298 INSTANTIATE_TEST_CASE_P(I420AndARGB, | |
299 VideoCaptureImplTest, | |
300 testing::ValuesIn(kBufferFormats)); | |
301 | |
302 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | |
303 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
304 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
305 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | |
306 | |
307 // Create a fake shared memory for buffer. | |
308 base::SharedMemory shm; | |
309 const size_t i420_frame_size = media::VideoFrame::AllocationSize( | |
310 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | |
311 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); | |
312 | |
313 Init(); | |
314 StartCapture(params_large_); | |
315 NewBuffer(0, shm); | |
316 StopCapture(); | |
317 BufferReceived(0, params_large_.requested_format.frame_size, | |
318 media::PIXEL_FORMAT_I420, gpu::MailboxHolder()); | |
319 BufferDestroyed(0); | |
320 DeInit(); | |
321 | |
322 EXPECT_EQ(this->video_capture_impl_->received_buffer_count(), 1); | |
323 } | |
324 | |
231 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { | 325 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { |
232 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 326 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 327 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
234 | 328 |
235 Init(); | 329 Init(); |
236 StartCapture(params_small_); | 330 StartCapture(params_small_); |
237 | 331 |
238 // Receive state change message from browser. | 332 // Receive state change message from browser. |
239 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); | 333 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); |
240 | 334 |
241 StopCapture(); | 335 StopCapture(); |
242 DeInit(); | 336 DeInit(); |
243 } | 337 } |
244 | 338 |
245 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { | 339 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { |
246 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 340 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
247 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); | 341 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); |
248 | 342 |
249 Init(); | 343 Init(); |
250 StartCapture(params_small_); | 344 StartCapture(params_small_); |
251 | 345 |
252 // Receive state change message from browser. | 346 // Receive state change message from browser. |
253 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); | 347 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); |
254 | 348 |
255 StopCapture(); | 349 StopCapture(); |
256 DeInit(); | 350 DeInit(); |
257 } | 351 } |
258 | 352 |
259 } // namespace content | 353 } // namespace content |
OLD | NEW |