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.width = 352; |
343 params.height = 288; | 349 params.height = 288; |
344 params.frame_rate = 30; | 350 params.frame_rate = 30; |
345 params.session_id = opened_session_id_; | 351 params.session_id = opened_session_id_; |
346 host_->OnStartCapture(kDeviceId, params); | 352 host_->OnStartCapture(kDeviceId, params); |
347 run_loop.Run(); | 353 run_loop.Run(); |
(...skipping 11 matching lines...) Expand all Loading... |
359 params.frame_rate = 30; | 365 params.frame_rate = 30; |
360 params.session_id = opened_session_id_; | 366 params.session_id = opened_session_id_; |
361 host_->OnStartCapture(kDeviceId, params); | 367 host_->OnStartCapture(kDeviceId, params); |
362 host_->OnStopCapture(kDeviceId); | 368 host_->OnStopCapture(kDeviceId); |
363 run_loop.RunUntilIdle(); | 369 run_loop.RunUntilIdle(); |
364 } | 370 } |
365 | 371 |
366 #ifdef DUMP_VIDEO | 372 #ifdef DUMP_VIDEO |
367 void CaptureAndDumpVideo(int width, int height, int frame_rate) { | 373 void CaptureAndDumpVideo(int width, int height, int frame_rate) { |
368 InSequence s; | 374 InSequence s; |
369 // 1. First - get info about the new resolution | 375 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) |
370 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); | 376 .Times(AnyNumber()).WillRepeatedly(Return()); |
371 | 377 |
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; | 378 base::RunLoop run_loop; |
377 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 379 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |
378 .Times(AnyNumber()) | 380 .Times(AnyNumber()) |
379 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 381 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |
380 | 382 |
381 media::VideoCaptureParams params; | 383 media::VideoCaptureParams params; |
382 params.width = width; | 384 params.width = width; |
383 params.height = height; | 385 params.height = height; |
384 params.frame_rate = frame_rate; | 386 params.frame_rate = frame_rate; |
385 params.session_id = opened_session_id_; | 387 params.session_id = opened_session_id_; |
386 host_->SetDumpVideo(true); | 388 host_->SetDumpVideo(true); |
387 host_->OnStartCapture(kDeviceId, params); | 389 host_->OnStartCapture(kDeviceId, params); |
388 run_loop.Run(); | 390 run_loop.Run(); |
389 } | 391 } |
390 #endif | 392 #endif |
391 | 393 |
392 void StopCapture() { | 394 void StopCapture() { |
393 base::RunLoop run_loop; | 395 base::RunLoop run_loop; |
394 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) | 396 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |
395 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 397 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |
396 | 398 |
397 host_->OnStopCapture(kDeviceId); | 399 host_->OnStopCapture(kDeviceId); |
398 host_->SetReturnReceviedDibs(true); | 400 host_->SetReturnReceivedDibs(true); |
399 host_->ReturnReceivedDibs(kDeviceId); | 401 host_->ReturnReceivedDibs(kDeviceId); |
400 | 402 |
401 run_loop.Run(); | 403 run_loop.Run(); |
402 | 404 |
403 host_->SetReturnReceviedDibs(false); | 405 host_->SetReturnReceivedDibs(false); |
404 // Expect the VideoCaptureDevice has been stopped | 406 // Expect the VideoCaptureDevice has been stopped |
405 EXPECT_EQ(0u, host_->entries_.size()); | 407 EXPECT_EQ(0u, host_->entries_.size()); |
406 } | 408 } |
407 | 409 |
408 void NotifyPacketReady() { | 410 void NotifyPacketReady() { |
409 base::RunLoop run_loop; | 411 base::RunLoop run_loop; |
410 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 412 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |
411 .Times(AnyNumber()).WillOnce(ExitMessageLoop( | 413 .Times(AnyNumber()).WillOnce(ExitMessageLoop( |
412 message_loop_, run_loop.QuitClosure())) | 414 message_loop_, run_loop.QuitClosure())) |
413 .RetiresOnSaturation(); | 415 .RetiresOnSaturation(); |
414 run_loop.Run(); | 416 run_loop.Run(); |
415 } | 417 } |
416 | 418 |
417 void ReturnReceivedPackets() { | 419 void ReturnReceivedPackets() { |
418 host_->ReturnReceivedDibs(kDeviceId); | 420 host_->ReturnReceivedDibs(kDeviceId); |
419 } | 421 } |
420 | 422 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 #ifdef DUMP_VIDEO | 485 #ifdef DUMP_VIDEO |
484 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 486 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
485 CaptureAndDumpVideo(640, 480, 30); | 487 CaptureAndDumpVideo(640, 480, 30); |
486 } | 488 } |
487 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 489 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
488 CaptureAndDumpVideo(1280, 720, 30); | 490 CaptureAndDumpVideo(1280, 720, 30); |
489 } | 491 } |
490 #endif | 492 #endif |
491 | 493 |
492 } // namespace content | 494 } // namespace content |
OLD | NEW |