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(int buffer_id, | |
40 media::VideoPixelFormat pixel_format, | |
41 media::VideoCapturePixelFormat capture_pixel_format, | |
42 const gfx::Size& coded_size, | |
43 const gpu::MailboxHolder& mailbox_holder) | |
44 : buffer_id(buffer_id), pixel_format(pixel_format), | |
45 capture_pixel_format(capture_pixel_format), coded_size(coded_size), | |
46 mailbox_holder(mailbox_holder) {} | |
47 | |
48 int buffer_id; | |
49 media::VideoPixelFormat pixel_format; | |
50 media::VideoCapturePixelFormat capture_pixel_format; | |
51 const gfx::Size coded_size; | |
52 const gpu::MailboxHolder mailbox_holder; | |
53 }; | |
mcasas
2015/08/31 17:33:26
Prefer
struct Bla {
// ...
} kBlas[] = {
// .
msu.koo
2015/09/01 05:25:01
Moved |kBufferFormats| to INSTANTIATE_TEST_CASE_P
| |
54 | |
55 static const BufferReceivedTestArg kBufferFormats[] = { | |
56 BufferReceivedTestArg(0, media::PIXEL_FORMAT_I420, | |
mcasas
2015/08/31 17:33:26
|buffer_id| and |coded_size| are common among
the
msu.koo
2015/09/01 05:25:01
I basically agree with your opinion, but these var
mcasas
2015/09/01 18:09:53
Ah, we'll extend it when the moment comes,
no dead
msu.koo
2015/09/02 08:38:10
Done.
| |
57 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420, | |
58 gfx::Size(1280, 720), | |
59 gpu::MailboxHolder()), | |
60 BufferReceivedTestArg(0, media::PIXEL_FORMAT_ARGB, | |
61 media::VIDEO_CAPTURE_PIXEL_FORMAT_ARGB, | |
62 gfx::Size(1280, 720), | |
63 gpu::MailboxHolder(gpu::Mailbox::Generate(), 0, 0))}; | |
64 | |
65 class VideoCaptureImplTest : | |
66 public ::testing::TestWithParam<BufferReceivedTestArg> { | |
38 public: | 67 public: |
39 class MockVideoCaptureImpl : public VideoCaptureImpl { | 68 class MockVideoCaptureImpl : public VideoCaptureImpl { |
40 public: | 69 public: |
41 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 70 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
42 VideoCaptureMessageFilter* filter) | 71 VideoCaptureMessageFilter* filter) |
43 : VideoCaptureImpl(id, filter) { | 72 : VideoCaptureImpl(id, filter), received_buffer_counts_(0) { |
44 } | 73 } |
45 ~MockVideoCaptureImpl() override {} | 74 ~MockVideoCaptureImpl() override {} |
46 | 75 |
47 // Override Send() to mimic device to send events. | 76 // Override Send() to mimic device to send events. |
48 void Send(IPC::Message* message) override { | 77 void Send(IPC::Message* message) override { |
49 CHECK(message); | 78 CHECK(message); |
50 | 79 |
51 // In this method, messages are sent to the according handlers as if | 80 // In this method, messages are sent to the according handlers as if |
52 // we are the device. | 81 // we are the device. |
53 bool handled = true; | 82 bool handled = true; |
(...skipping 23 matching lines...) Expand all Loading... | |
77 | 106 |
78 void DevicePauseCapture(int device_id) {} | 107 void DevicePauseCapture(int device_id) {} |
79 | 108 |
80 void DeviceStopCapture(int device_id) { | 109 void DeviceStopCapture(int device_id) { |
81 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); | 110 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); |
82 } | 111 } |
83 | 112 |
84 void DeviceReceiveEmptyBuffer(int device_id, | 113 void DeviceReceiveEmptyBuffer(int device_id, |
85 int buffer_id, | 114 int buffer_id, |
86 uint32 sync_point, | 115 uint32 sync_point, |
87 double consumer_resource_utilization) {} | 116 double consumer_resource_utilization) { |
117 received_buffer_counts_++; | |
118 } | |
88 | 119 |
89 void DeviceGetSupportedFormats(int device_id, | 120 void DeviceGetSupportedFormats(int device_id, |
90 media::VideoCaptureSessionId session_id) { | 121 media::VideoCaptureSessionId session_id) { |
91 // When the mock message filter receives a request for the device | 122 // When the mock message filter receives a request for the device |
92 // supported formats, replies immediately with an empty format list. | 123 // supported formats, replies immediately with an empty format list. |
93 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); | 124 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); |
94 } | 125 } |
95 | 126 |
96 void DeviceGetFormatsInUse(int device_id, | 127 void DeviceGetFormatsInUse(int device_id, |
97 media::VideoCaptureSessionId session_id) { | 128 media::VideoCaptureSessionId session_id) { |
98 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); | 129 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); |
99 } | 130 } |
100 | 131 |
101 void ReceiveStateChangeMessage(VideoCaptureState state) { | 132 void ReceiveStateChangeMessage(VideoCaptureState state) { |
102 OnStateChanged(state); | 133 OnStateChanged(state); |
103 } | 134 } |
104 | 135 |
105 const media::VideoCaptureParams& capture_params() const { | 136 const media::VideoCaptureParams& capture_params() const { |
mcasas
2015/08/31 17:33:26
unused now? (both method and member variable).
msu.koo
2015/09/01 05:25:01
Done.
| |
106 return capture_params_; | 137 return capture_params_; |
107 } | 138 } |
108 | 139 |
140 int GetReceivedBufferCounts() const { return received_buffer_counts_; } | |
mcasas
2015/08/31 17:33:26
s/GetReceivedBufferCounts()/received_buffer_count(
msu.koo
2015/09/01 05:25:01
Done.
| |
141 | |
109 private: | 142 private: |
110 media::VideoCaptureParams capture_params_; | 143 media::VideoCaptureParams capture_params_; |
144 int received_buffer_counts_; | |
mcasas
2015/08/31 17:33:26
s/received_buffer_counts_/received_buffer_count_/
msu.koo
2015/09/01 05:25:01
Done.
| |
111 }; | 145 }; |
112 | 146 |
113 VideoCaptureImplTest() { | 147 VideoCaptureImplTest() { |
114 params_small_.requested_format = media::VideoCaptureFormat( | 148 params_small_.requested_format = media::VideoCaptureFormat( |
115 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 149 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
116 | 150 |
117 params_large_.requested_format = media::VideoCaptureFormat( | 151 params_large_.requested_format = media::VideoCaptureFormat( |
118 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 152 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
119 | 153 |
120 child_process_.reset(new ChildProcess()); | 154 child_process_.reset(new ChildProcess()); |
(...skipping 26 matching lines...) Expand all Loading... | |
147 void StartCapture(const media::VideoCaptureParams& params) { | 181 void StartCapture(const media::VideoCaptureParams& params) { |
148 video_capture_impl_->StartCapture( | 182 video_capture_impl_->StartCapture( |
149 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, | 183 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, |
150 base::Unretained(this)), | 184 base::Unretained(this)), |
151 base::Bind(&VideoCaptureImplTest::OnFrameReady, | 185 base::Bind(&VideoCaptureImplTest::OnFrameReady, |
152 base::Unretained(this))); | 186 base::Unretained(this))); |
153 } | 187 } |
154 | 188 |
155 void StopCapture() { video_capture_impl_->StopCapture(); } | 189 void StopCapture() { video_capture_impl_->StopCapture(); } |
156 | 190 |
191 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { | |
192 video_capture_impl_->OnBufferCreated( | |
193 base::SharedMemory::DuplicateHandle(shm.handle()), | |
194 shm.mapped_size(), buffer_id); | |
195 } | |
196 | |
197 void BufferReceived(int buffer_id, const gfx::Size& size, | |
198 media::VideoPixelFormat pixel_format, | |
199 const gpu::MailboxHolder& mailbox_holder) { | |
200 video_capture_impl_->OnBufferReceived( | |
201 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(), | |
202 pixel_format, media::VideoFrame::STORAGE_SHMEM, size, | |
203 gfx::Rect(size), mailbox_holder); | |
204 } | |
205 | |
206 void BufferDestroyed(int buffer_id) { | |
207 video_capture_impl_->OnBufferDestroyed(buffer_id); | |
208 } | |
209 | |
157 void DeInit() { | 210 void DeInit() { |
158 video_capture_impl_->DeInit(); | 211 video_capture_impl_->DeInit(); |
159 } | 212 } |
160 | 213 |
161 void GetDeviceSupportedFormats() { | 214 void GetDeviceSupportedFormats() { |
162 const base::Callback<void(const media::VideoCaptureFormats&)> | 215 const base::Callback<void(const media::VideoCaptureFormats&)> |
163 callback = base::Bind( | 216 callback = base::Bind( |
164 &VideoCaptureImplTest::OnDeviceSupportedFormats, | 217 &VideoCaptureImplTest::OnDeviceSupportedFormats, |
165 base::Unretained(this)); | 218 base::Unretained(this)); |
166 video_capture_impl_->GetDeviceSupportedFormats(callback); | 219 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 | 274 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the |
222 // provided callback. | 275 // provided callback. |
223 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { | 276 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { |
224 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); | 277 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); |
225 | 278 |
226 Init(); | 279 Init(); |
227 GetDeviceFormatsInUse(); | 280 GetDeviceFormatsInUse(); |
228 DeInit(); | 281 DeInit(); |
229 } | 282 } |
230 | 283 |
284 TEST_P(VideoCaptureImplTest, BufferReceivedWithFormat) { | |
285 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
286 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
287 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1); | |
288 | |
289 const BufferReceivedTestArg& buffer_arg = GetParam(); | |
290 | |
291 // Create a fake shared memory for buffer. | |
292 base::SharedMemory shm; | |
293 const size_t frame_size = media::VideoFrame::AllocationSize( | |
294 buffer_arg.pixel_format, buffer_arg.coded_size); | |
295 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | |
296 | |
297 media::VideoCaptureParams params; | |
298 params.requested_format = media::VideoCaptureFormat( | |
299 buffer_arg.coded_size, 30, buffer_arg.capture_pixel_format); | |
300 | |
301 Init(); | |
302 StartCapture(params); | |
303 NewBuffer(buffer_arg.buffer_id, shm); | |
304 BufferReceived(buffer_arg.buffer_id, buffer_arg.coded_size, | |
305 buffer_arg.pixel_format, buffer_arg.mailbox_holder); | |
306 StopCapture(); | |
307 BufferDestroyed(buffer_arg.buffer_id); | |
308 DeInit(); | |
309 } | |
310 | |
311 INSTANTIATE_TEST_CASE_P(I420AndARGB, | |
312 VideoCaptureImplTest, | |
313 testing::ValuesIn(kBufferFormats)); | |
mcasas
2015/08/31 17:33:26
You can also bring |kBufferFormats| here .
msu.koo
2015/09/01 05:25:01
Done.
| |
314 | |
315 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | |
316 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
317 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
318 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | |
319 | |
320 // Create a fake shared memory for buffer. | |
321 base::SharedMemory shm; | |
322 size_t i420_frame_size = media::VideoFrame::AllocationSize( | |
mcasas
2015/08/31 17:33:26
const
msu.koo
2015/09/01 05:25:01
Done.
| |
323 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | |
324 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); | |
325 | |
326 Init(); | |
327 StartCapture(params_large_); | |
328 NewBuffer(0, shm); | |
329 StopCapture(); | |
330 BufferReceived(0, params_large_.requested_format.frame_size, | |
331 media::PIXEL_FORMAT_I420, gpu::MailboxHolder()); | |
332 BufferDestroyed(0); | |
333 DeInit(); | |
334 | |
335 EXPECT_EQ(this->video_capture_impl_->GetReceivedBufferCounts(), 1); | |
336 } | |
337 | |
231 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { | 338 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { |
232 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 339 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 340 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
234 | 341 |
235 Init(); | 342 Init(); |
236 StartCapture(params_small_); | 343 StartCapture(params_small_); |
237 | 344 |
238 // Receive state change message from browser. | 345 // Receive state change message from browser. |
239 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); | 346 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); |
240 | 347 |
241 StopCapture(); | 348 StopCapture(); |
242 DeInit(); | 349 DeInit(); |
243 } | 350 } |
244 | 351 |
245 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { | 352 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { |
246 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 353 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
247 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); | 354 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); |
248 | 355 |
249 Init(); | 356 Init(); |
250 StartCapture(params_small_); | 357 StartCapture(params_small_); |
251 | 358 |
252 // Receive state change message from browser. | 359 // Receive state change message from browser. |
253 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); | 360 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); |
254 | 361 |
255 StopCapture(); | 362 StopCapture(); |
256 DeInit(); | 363 DeInit(); |
257 } | 364 } |
258 | 365 |
259 } // namespace content | 366 } // namespace content |
OLD | NEW |