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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 public: | 98 public: |
99 MockVideoCaptureHost(MediaStreamManager* manager) | 99 MockVideoCaptureHost(MediaStreamManager* manager) |
100 : VideoCaptureHost(manager), | 100 : VideoCaptureHost(manager), |
101 return_buffers_(false), | 101 return_buffers_(false), |
102 dump_video_(false) {} | 102 dump_video_(false) {} |
103 | 103 |
104 // A list of mock methods. | 104 // A list of mock methods. |
105 MOCK_METHOD4(OnNewBufferCreated, | 105 MOCK_METHOD4(OnNewBufferCreated, |
106 void(int device_id, base::SharedMemoryHandle handle, | 106 void(int device_id, base::SharedMemoryHandle handle, |
107 int length, int buffer_id)); | 107 int length, int buffer_id)); |
108 MOCK_METHOD3(OnBufferFilled, | 108 MOCK_METHOD2(OnBufferFreed, |
109 void(int device_id, int buffer_id, base::Time timestamp)); | 109 void(int device_id, int buffer_id)); |
| 110 MOCK_METHOD4(OnBufferFilled, |
| 111 void(int device_id, int buffer_id, base::Time timestamp, |
| 112 const media::VideoCaptureFormat& format)); |
110 MOCK_METHOD2(OnStateChanged, void(int device_id, VideoCaptureState state)); | 113 MOCK_METHOD2(OnStateChanged, void(int device_id, VideoCaptureState state)); |
111 MOCK_METHOD1(OnDeviceInfo, void(int device_id)); | |
112 | 114 |
113 // Use class DumpVideo to write I420 video to file. | 115 // Use class DumpVideo to write I420 video to file. |
114 void SetDumpVideo(bool enable) { | 116 void SetDumpVideo(bool enable) { |
115 dump_video_ = enable; | 117 dump_video_ = enable; |
116 } | 118 } |
117 | 119 |
118 void SetReturnReceviedDibs(bool enable) { | 120 void SetReturnReceivedDibs(bool enable) { |
119 return_buffers_ = enable; | 121 return_buffers_ = enable; |
120 } | 122 } |
121 | 123 |
122 // Return Dibs we currently have received. | 124 // Return Dibs we currently have received. |
123 void ReturnReceivedDibs(int device_id) { | 125 void ReturnReceivedDibs(int device_id) { |
124 int handle = GetReceivedDib(); | 126 int handle = GetReceivedDib(); |
125 while (handle) { | 127 while (handle) { |
126 this->OnReceiveEmptyBuffer(device_id, handle); | 128 this->OnReceiveEmptyBuffer(device_id, handle); |
127 handle = GetReceivedDib(); | 129 handle = GetReceivedDib(); |
128 } | 130 } |
(...skipping 20 matching lines...) Expand all Loading... |
149 // these messages here and dispatch to our mock methods to verify the | 151 // these messages here and dispatch to our mock methods to verify the |
150 // conversation between this object and the renderer. | 152 // conversation between this object and the renderer. |
151 virtual bool Send(IPC::Message* message) OVERRIDE { | 153 virtual bool Send(IPC::Message* message) OVERRIDE { |
152 CHECK(message); | 154 CHECK(message); |
153 | 155 |
154 // In this method we dispatch the messages to the according handlers as if | 156 // In this method we dispatch the messages to the according handlers as if |
155 // we are the renderer. | 157 // we are the renderer. |
156 bool handled = true; | 158 bool handled = true; |
157 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureHost, *message) | 159 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureHost, *message) |
158 IPC_MESSAGE_HANDLER(VideoCaptureMsg_NewBuffer, OnNewBufferCreatedDispatch) | 160 IPC_MESSAGE_HANDLER(VideoCaptureMsg_NewBuffer, OnNewBufferCreatedDispatch) |
| 161 IPC_MESSAGE_HANDLER(VideoCaptureMsg_FreeBuffer, OnBufferFreedDispatch) |
159 IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferFilledDispatch) | 162 IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferFilledDispatch) |
160 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch) | 163 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch) |
161 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfoDispatch) | |
162 IPC_MESSAGE_UNHANDLED(handled = false) | 164 IPC_MESSAGE_UNHANDLED(handled = false) |
163 IPC_END_MESSAGE_MAP() | 165 IPC_END_MESSAGE_MAP() |
164 EXPECT_TRUE(handled); | 166 EXPECT_TRUE(handled); |
165 | 167 |
166 delete message; | 168 delete message; |
167 return true; | 169 return true; |
168 } | 170 } |
169 | 171 |
170 // These handler methods do minimal things and delegate to the mock methods. | 172 // These handler methods do minimal things and delegate to the mock methods. |
171 void OnNewBufferCreatedDispatch(int device_id, | 173 void OnNewBufferCreatedDispatch(int device_id, |
172 base::SharedMemoryHandle handle, | 174 base::SharedMemoryHandle handle, |
173 uint32 length, | 175 uint32 length, |
174 int buffer_id) { | 176 int buffer_id) { |
175 OnNewBufferCreated(device_id, handle, length, buffer_id); | 177 OnNewBufferCreated(device_id, handle, length, buffer_id); |
176 base::SharedMemory* dib = new base::SharedMemory(handle, false); | 178 base::SharedMemory* dib = new base::SharedMemory(handle, false); |
177 dib->Map(length); | 179 dib->Map(length); |
178 filled_dib_[buffer_id] = dib; | 180 filled_dib_[buffer_id] = dib; |
179 } | 181 } |
180 | 182 |
181 void OnBufferFilledDispatch(int device_id, int buffer_id, | 183 void OnBufferFreedDispatch(int device_id, int buffer_id) { |
182 base::Time timestamp) { | 184 OnBufferFreed(device_id, buffer_id); |
| 185 |
| 186 std::map<int, base::SharedMemory*>::iterator it = |
| 187 filled_dib_.find(buffer_id); |
| 188 ASSERT_TRUE(it != filled_dib_.end()); |
| 189 delete it->second; |
| 190 filled_dib_.erase(it); |
| 191 } |
| 192 |
| 193 void OnBufferFilledDispatch(int device_id, |
| 194 int buffer_id, |
| 195 base::Time timestamp, |
| 196 const media::VideoCaptureFormat& frame_format) { |
| 197 base::SharedMemory* dib = filled_dib_[buffer_id]; |
| 198 ASSERT_TRUE(dib != NULL); |
183 if (dump_video_) { | 199 if (dump_video_) { |
184 base::SharedMemory* dib = filled_dib_[buffer_id]; | 200 if (!format_.IsValid()) { |
185 ASSERT_TRUE(dib != NULL); | 201 dumper_.StartDump(frame_format.width, frame_format.height); |
| 202 format_ = frame_format; |
| 203 } |
| 204 ASSERT_EQ(format_.width, frame_format.width) |
| 205 << "Dump format does not handle variable resolution."; |
| 206 ASSERT_EQ(format_.height, frame_format.height) |
| 207 << "Dump format does not handle variable resolution.";; |
186 dumper_.NewVideoFrame(dib->memory()); | 208 dumper_.NewVideoFrame(dib->memory()); |
187 } | 209 } |
188 | 210 |
189 OnBufferFilled(device_id, buffer_id, timestamp); | 211 OnBufferFilled(device_id, buffer_id, timestamp, frame_format); |
190 if (return_buffers_) { | 212 if (return_buffers_) { |
191 VideoCaptureHost::OnReceiveEmptyBuffer(device_id, buffer_id); | 213 VideoCaptureHost::OnReceiveEmptyBuffer(device_id, buffer_id); |
192 } | 214 } |
193 } | 215 } |
194 | 216 |
195 void OnStateChangedDispatch(int device_id, VideoCaptureState state) { | 217 void OnStateChangedDispatch(int device_id, VideoCaptureState state) { |
196 OnStateChanged(device_id, state); | 218 OnStateChanged(device_id, state); |
197 } | 219 } |
198 | 220 |
199 void OnDeviceInfoDispatch(int device_id, | |
200 media::VideoCaptureParams params) { | |
201 if (dump_video_) { | |
202 dumper_.StartDump(params.width, params.height); | |
203 } | |
204 OnDeviceInfo(device_id); | |
205 } | |
206 | |
207 std::map<int, base::SharedMemory*> filled_dib_; | 221 std::map<int, base::SharedMemory*> filled_dib_; |
208 bool return_buffers_; | 222 bool return_buffers_; |
209 bool dump_video_; | 223 bool dump_video_; |
| 224 media::VideoCaptureFormat format_; |
210 DumpVideo dumper_; | 225 DumpVideo dumper_; |
211 }; | 226 }; |
212 | 227 |
213 ACTION_P2(ExitMessageLoop, message_loop, quit_closure) { | 228 ACTION_P2(ExitMessageLoop, message_loop, quit_closure) { |
214 message_loop->PostTask(FROM_HERE, quit_closure); | 229 message_loop->PostTask(FROM_HERE, quit_closure); |
215 } | 230 } |
216 | 231 |
217 // This is an integration test of VideoCaptureHost in conjunction with | 232 // This is an integration test of VideoCaptureHost in conjunction with |
218 // MediaStreamManager, VideoCaptureManager, VideoCaptureController, and | 233 // MediaStreamManager, VideoCaptureManager, VideoCaptureController, and |
219 // VideoCaptureDevice. | 234 // VideoCaptureDevice. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 void CloseSession() { | 329 void CloseSession() { |
315 if (opened_device_label_.empty()) | 330 if (opened_device_label_.empty()) |
316 return; | 331 return; |
317 media_stream_manager_->StopGeneratedStream(opened_device_label_); | 332 media_stream_manager_->StopGeneratedStream(opened_device_label_); |
318 opened_device_label_.clear(); | 333 opened_device_label_.clear(); |
319 opened_session_id_ = kInvalidMediaCaptureSessionId; | 334 opened_session_id_ = kInvalidMediaCaptureSessionId; |
320 } | 335 } |
321 | 336 |
322 protected: | 337 protected: |
323 void StartCapture() { | 338 void StartCapture() { |
324 InSequence s; | |
325 // 1. First - get info about the new resolution | |
326 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); | |
327 | |
328 // 2. Change state to started | |
329 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STARTED)); | |
330 | |
331 // 3. Newly created buffers will arrive. | |
332 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) | 339 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) |
333 .Times(AnyNumber()).WillRepeatedly(Return()); | 340 .Times(AnyNumber()).WillRepeatedly(Return()); |
334 | 341 |
335 // 4. First filled buffer will arrive. | |
336 base::RunLoop run_loop; | 342 base::RunLoop run_loop; |
337 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 343 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |
338 .Times(AnyNumber()).WillOnce(ExitMessageLoop( | 344 .Times(AnyNumber()).WillOnce(ExitMessageLoop( |
339 message_loop_, run_loop.QuitClosure())); | 345 message_loop_, run_loop.QuitClosure())); |
340 | 346 |
341 media::VideoCaptureParams params; | 347 media::VideoCaptureParams params; |
342 params.width = 352; | 348 params.requested_format = media::VideoCaptureFormat( |
343 params.height = 288; | 349 352, 288, 30, media::ConstantResolutionVideoCaptureDevice); |
344 params.frame_rate = 30; | |
345 params.session_id = opened_session_id_; | 350 params.session_id = opened_session_id_; |
346 host_->OnStartCapture(kDeviceId, params); | 351 host_->OnStartCapture(kDeviceId, params); |
347 run_loop.Run(); | 352 run_loop.Run(); |
348 } | 353 } |
349 | 354 |
350 void StartStopCapture() { | 355 void StartStopCapture() { |
351 // Quickly start and then stop capture, without giving much chance for | 356 // Quickly start and then stop capture, without giving much chance for |
352 // asynchronous start operations to complete. | 357 // asynchronous start operations to complete. |
353 InSequence s; | 358 InSequence s; |
354 base::RunLoop run_loop; | 359 base::RunLoop run_loop; |
355 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); | 360 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); |
356 media::VideoCaptureParams params; | 361 media::VideoCaptureParams params; |
357 params.width = 352; | 362 params.requested_format = media::VideoCaptureFormat( |
358 params.height = 288; | 363 352, 288, 30, media::ConstantResolutionVideoCaptureDevice); |
359 params.frame_rate = 30; | |
360 params.session_id = opened_session_id_; | 364 params.session_id = opened_session_id_; |
361 host_->OnStartCapture(kDeviceId, params); | 365 host_->OnStartCapture(kDeviceId, params); |
362 host_->OnStopCapture(kDeviceId); | 366 host_->OnStopCapture(kDeviceId); |
363 run_loop.RunUntilIdle(); | 367 run_loop.RunUntilIdle(); |
364 } | 368 } |
365 | 369 |
366 #ifdef DUMP_VIDEO | 370 #ifdef DUMP_VIDEO |
367 void CaptureAndDumpVideo(int width, int height, int frame_rate) { | 371 void CaptureAndDumpVideo(int width, int height, int frame_rate) { |
368 InSequence s; | 372 InSequence s; |
369 // 1. First - get info about the new resolution | 373 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) |
370 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); | 374 .Times(AnyNumber()).WillRepeatedly(Return()); |
371 | 375 |
372 // 2. Change state to started | |
373 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STARTED)); | |
374 | |
375 // 3. First filled buffer will arrive. | |
376 base::RunLoop run_loop; | 376 base::RunLoop run_loop; |
377 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 377 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |
378 .Times(AnyNumber()) | 378 .Times(AnyNumber()) |
379 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 379 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |
380 | 380 |
381 media::VideoCaptureParams params; | 381 media::VideoCaptureParams params; |
382 params.width = width; | 382 params.requested_format = media::VideoCaptureFormat( |
383 params.height = height; | 383 width, height, frame_rate, media::ConstantResolutionVideoCaptureDevice); |
384 params.frame_rate = frame_rate; | |
385 params.session_id = opened_session_id_; | 384 params.session_id = opened_session_id_; |
386 host_->SetDumpVideo(true); | 385 host_->SetDumpVideo(true); |
387 host_->OnStartCapture(kDeviceId, params); | 386 host_->OnStartCapture(kDeviceId, params); |
388 run_loop.Run(); | 387 run_loop.Run(); |
389 } | 388 } |
390 #endif | 389 #endif |
391 | 390 |
392 void StopCapture() { | 391 void StopCapture() { |
393 base::RunLoop run_loop; | 392 base::RunLoop run_loop; |
394 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) | 393 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |
395 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 394 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |
396 | 395 |
397 host_->OnStopCapture(kDeviceId); | 396 host_->OnStopCapture(kDeviceId); |
398 host_->SetReturnReceviedDibs(true); | 397 host_->SetReturnReceivedDibs(true); |
399 host_->ReturnReceivedDibs(kDeviceId); | 398 host_->ReturnReceivedDibs(kDeviceId); |
400 | 399 |
401 run_loop.Run(); | 400 run_loop.Run(); |
402 | 401 |
403 host_->SetReturnReceviedDibs(false); | 402 host_->SetReturnReceivedDibs(false); |
404 // Expect the VideoCaptureDevice has been stopped | 403 // Expect the VideoCaptureDevice has been stopped |
405 EXPECT_EQ(0u, host_->entries_.size()); | 404 EXPECT_EQ(0u, host_->entries_.size()); |
406 } | 405 } |
407 | 406 |
408 void NotifyPacketReady() { | 407 void NotifyPacketReady() { |
409 base::RunLoop run_loop; | 408 base::RunLoop run_loop; |
410 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 409 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |
411 .Times(AnyNumber()).WillOnce(ExitMessageLoop( | 410 .Times(AnyNumber()).WillOnce(ExitMessageLoop( |
412 message_loop_, run_loop.QuitClosure())) | 411 message_loop_, run_loop.QuitClosure())) |
413 .RetiresOnSaturation(); | 412 .RetiresOnSaturation(); |
414 run_loop.Run(); | 413 run_loop.Run(); |
415 } | 414 } |
416 | 415 |
417 void ReturnReceivedPackets() { | 416 void ReturnReceivedPackets() { |
418 host_->ReturnReceivedDibs(kDeviceId); | 417 host_->ReturnReceivedDibs(kDeviceId); |
419 } | 418 } |
420 | 419 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 #ifdef DUMP_VIDEO | 482 #ifdef DUMP_VIDEO |
484 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 483 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
485 CaptureAndDumpVideo(640, 480, 30); | 484 CaptureAndDumpVideo(640, 480, 30); |
486 } | 485 } |
487 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 486 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
488 CaptureAndDumpVideo(1280, 720, 30); | 487 CaptureAndDumpVideo(1280, 720, 30); |
489 } | 488 } |
490 #endif | 489 #endif |
491 | 490 |
492 } // namespace content | 491 } // namespace content |
OLD | NEW |