| 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/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
| 12 #include "base/location.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/thread_task_runner_handle.h" |
| 17 #include "content/browser/browser_thread_impl.h" | 19 #include "content/browser/browser_thread_impl.h" |
| 18 #include "content/browser/renderer_host/media/media_stream_manager.h" | 20 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 19 #include "content/browser/renderer_host/media/media_stream_requester.h" | 21 #include "content/browser/renderer_host/media/media_stream_requester.h" |
| 20 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 22 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 21 #include "content/browser/renderer_host/media/video_capture_host.h" | 23 #include "content/browser/renderer_host/media/video_capture_host.h" |
| 22 #include "content/browser/renderer_host/media/video_capture_manager.h" | 24 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 23 #include "content/common/media/video_capture_messages.h" | 25 #include "content/common/media/video_capture_messages.h" |
| 24 #include "content/public/common/content_switches.h" | 26 #include "content/public/common/content_switches.h" |
| 25 #include "content/public/test/mock_resource_context.h" | 27 #include "content/public/test/mock_resource_context.h" |
| 26 #include "content/public/test/test_browser_context.h" | 28 #include "content/public/test/test_browser_context.h" |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 OnStateChanged(device_id, state); | 267 OnStateChanged(device_id, state); |
| 266 } | 268 } |
| 267 | 269 |
| 268 std::map<int, base::SharedMemory*> filled_dib_; | 270 std::map<int, base::SharedMemory*> filled_dib_; |
| 269 bool return_buffers_; | 271 bool return_buffers_; |
| 270 bool dump_video_; | 272 bool dump_video_; |
| 271 media::VideoCaptureFormat format_; | 273 media::VideoCaptureFormat format_; |
| 272 DumpVideo dumper_; | 274 DumpVideo dumper_; |
| 273 }; | 275 }; |
| 274 | 276 |
| 275 ACTION_P2(ExitMessageLoop, message_loop, quit_closure) { | 277 ACTION_P2(ExitMessageLoop, task_runner, quit_closure) { |
| 276 message_loop->PostTask(FROM_HERE, quit_closure); | 278 task_runner->PostTask(FROM_HERE, quit_closure); |
| 277 } | 279 } |
| 278 | 280 |
| 279 // This is an integration test of VideoCaptureHost in conjunction with | 281 // This is an integration test of VideoCaptureHost in conjunction with |
| 280 // MediaStreamManager, VideoCaptureManager, VideoCaptureController, and | 282 // MediaStreamManager, VideoCaptureManager, VideoCaptureController, and |
| 281 // VideoCaptureDevice. | 283 // VideoCaptureDevice. |
| 282 class VideoCaptureHostTest : public testing::Test { | 284 class VideoCaptureHostTest : public testing::Test { |
| 283 public: | 285 public: |
| 284 VideoCaptureHostTest() | 286 VideoCaptureHostTest() |
| 285 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 287 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 286 message_loop_(base::MessageLoopProxy::current()), | 288 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 287 opened_session_id_(kInvalidMediaCaptureSessionId) {} | 289 opened_session_id_(kInvalidMediaCaptureSessionId) {} |
| 288 | 290 |
| 289 void SetUp() override { | 291 void SetUp() override { |
| 290 SetBrowserClientForTesting(&browser_client_); | 292 SetBrowserClientForTesting(&browser_client_); |
| 291 | 293 |
| 292 #if defined(OS_CHROMEOS) | 294 #if defined(OS_CHROMEOS) |
| 293 chromeos::CrasAudioHandler::InitializeForTesting(); | 295 chromeos::CrasAudioHandler::InitializeForTesting(); |
| 294 #endif | 296 #endif |
| 295 | 297 |
| 296 // Create our own MediaStreamManager. | 298 // Create our own MediaStreamManager. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 { | 344 { |
| 343 base::RunLoop run_loop; | 345 base::RunLoop run_loop; |
| 344 std::string label = media_stream_manager_->EnumerateDevices( | 346 std::string label = media_stream_manager_->EnumerateDevices( |
| 345 &stream_requester_, | 347 &stream_requester_, |
| 346 render_process_id, | 348 render_process_id, |
| 347 render_frame_id, | 349 render_frame_id, |
| 348 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(), | 350 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(), |
| 349 page_request_id, | 351 page_request_id, |
| 350 MEDIA_DEVICE_VIDEO_CAPTURE, | 352 MEDIA_DEVICE_VIDEO_CAPTURE, |
| 351 security_origin); | 353 security_origin); |
| 352 EXPECT_CALL(stream_requester_, DevicesEnumerated(render_frame_id, | 354 EXPECT_CALL(stream_requester_, |
| 353 page_request_id, | 355 DevicesEnumerated(render_frame_id, page_request_id, label, _)) |
| 354 label, | 356 .Times(1) |
| 355 _)) | 357 .WillOnce(DoAll(ExitMessageLoop(task_runner_, run_loop.QuitClosure()), |
| 356 .Times(1).WillOnce( | 358 SaveArg<3>(&devices))); |
| 357 DoAll(ExitMessageLoop(message_loop_, run_loop.QuitClosure()), | |
| 358 SaveArg<3>(&devices))); | |
| 359 run_loop.Run(); | 359 run_loop.Run(); |
| 360 Mock::VerifyAndClearExpectations(&stream_requester_); | 360 Mock::VerifyAndClearExpectations(&stream_requester_); |
| 361 media_stream_manager_->CancelRequest(label); | 361 media_stream_manager_->CancelRequest(label); |
| 362 } | 362 } |
| 363 ASSERT_FALSE(devices.empty()); | 363 ASSERT_FALSE(devices.empty()); |
| 364 ASSERT_EQ(StreamDeviceInfo::kNoId, devices[0].session_id); | 364 ASSERT_EQ(StreamDeviceInfo::kNoId, devices[0].session_id); |
| 365 | 365 |
| 366 // Open the first device. | 366 // Open the first device. |
| 367 { | 367 { |
| 368 base::RunLoop run_loop; | 368 base::RunLoop run_loop; |
| 369 StreamDeviceInfo opened_device; | 369 StreamDeviceInfo opened_device; |
| 370 media_stream_manager_->OpenDevice( | 370 media_stream_manager_->OpenDevice( |
| 371 &stream_requester_, | 371 &stream_requester_, |
| 372 render_process_id, | 372 render_process_id, |
| 373 render_frame_id, | 373 render_frame_id, |
| 374 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(), | 374 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(), |
| 375 page_request_id, | 375 page_request_id, |
| 376 devices[0].device.id, | 376 devices[0].device.id, |
| 377 MEDIA_DEVICE_VIDEO_CAPTURE, | 377 MEDIA_DEVICE_VIDEO_CAPTURE, |
| 378 security_origin); | 378 security_origin); |
| 379 EXPECT_CALL(stream_requester_, DeviceOpened(render_frame_id, | 379 EXPECT_CALL(stream_requester_, |
| 380 page_request_id, | 380 DeviceOpened(render_frame_id, page_request_id, _, _)) |
| 381 _, | 381 .Times(1) |
| 382 _)) | 382 .WillOnce(DoAll(ExitMessageLoop(task_runner_, run_loop.QuitClosure()), |
| 383 .Times(1).WillOnce( | 383 SaveArg<2>(&opened_device_label_), |
| 384 DoAll(ExitMessageLoop(message_loop_, run_loop.QuitClosure()), | 384 SaveArg<3>(&opened_device))); |
| 385 SaveArg<2>(&opened_device_label_), | |
| 386 SaveArg<3>(&opened_device))); | |
| 387 run_loop.Run(); | 385 run_loop.Run(); |
| 388 Mock::VerifyAndClearExpectations(&stream_requester_); | 386 Mock::VerifyAndClearExpectations(&stream_requester_); |
| 389 ASSERT_NE(StreamDeviceInfo::kNoId, opened_device.session_id); | 387 ASSERT_NE(StreamDeviceInfo::kNoId, opened_device.session_id); |
| 390 opened_session_id_ = opened_device.session_id; | 388 opened_session_id_ = opened_device.session_id; |
| 391 } | 389 } |
| 392 } | 390 } |
| 393 | 391 |
| 394 void CloseSession() { | 392 void CloseSession() { |
| 395 if (opened_device_label_.empty()) | 393 if (opened_device_label_.empty()) |
| 396 return; | 394 return; |
| 397 media_stream_manager_->CancelRequest(opened_device_label_); | 395 media_stream_manager_->CancelRequest(opened_device_label_); |
| 398 opened_device_label_.clear(); | 396 opened_device_label_.clear(); |
| 399 opened_session_id_ = kInvalidMediaCaptureSessionId; | 397 opened_session_id_ = kInvalidMediaCaptureSessionId; |
| 400 } | 398 } |
| 401 | 399 |
| 402 protected: | 400 protected: |
| 403 void StartCapture() { | 401 void StartCapture() { |
| 404 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) | 402 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) |
| 405 .Times(AnyNumber()) | 403 .Times(AnyNumber()) |
| 406 .WillRepeatedly(Return()); | 404 .WillRepeatedly(Return()); |
| 407 | 405 |
| 408 base::RunLoop run_loop; | 406 base::RunLoop run_loop; |
| 409 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _, _, _)) | 407 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _, _, _)) |
| 410 .Times(AnyNumber()) | 408 .Times(AnyNumber()) |
| 411 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 409 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())); |
| 412 | 410 |
| 413 media::VideoCaptureParams params; | 411 media::VideoCaptureParams params; |
| 414 params.requested_format = media::VideoCaptureFormat( | 412 params.requested_format = media::VideoCaptureFormat( |
| 415 gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); | 413 gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); |
| 416 host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 414 host_->OnStartCapture(kDeviceId, opened_session_id_, params); |
| 417 run_loop.Run(); | 415 run_loop.Run(); |
| 418 } | 416 } |
| 419 | 417 |
| 420 void StartStopCapture() { | 418 void StartStopCapture() { |
| 421 // Quickly start and then stop capture, without giving much chance for | 419 // Quickly start and then stop capture, without giving much chance for |
| (...skipping 28 matching lines...) Expand all Loading... |
| 450 host_->SetDumpVideo(true); | 448 host_->SetDumpVideo(true); |
| 451 host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 449 host_->OnStartCapture(kDeviceId, opened_session_id_, params); |
| 452 run_loop.Run(); | 450 run_loop.Run(); |
| 453 } | 451 } |
| 454 #endif | 452 #endif |
| 455 | 453 |
| 456 void StopCapture() { | 454 void StopCapture() { |
| 457 base::RunLoop run_loop; | 455 base::RunLoop run_loop; |
| 458 EXPECT_CALL(*host_.get(), | 456 EXPECT_CALL(*host_.get(), |
| 459 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) | 457 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |
| 460 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 458 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())); |
| 461 | 459 |
| 462 host_->OnStopCapture(kDeviceId); | 460 host_->OnStopCapture(kDeviceId); |
| 463 host_->SetReturnReceivedDibs(true); | 461 host_->SetReturnReceivedDibs(true); |
| 464 host_->ReturnReceivedDibs(kDeviceId); | 462 host_->ReturnReceivedDibs(kDeviceId); |
| 465 | 463 |
| 466 run_loop.Run(); | 464 run_loop.Run(); |
| 467 | 465 |
| 468 host_->SetReturnReceivedDibs(false); | 466 host_->SetReturnReceivedDibs(false); |
| 469 // Expect the VideoCaptureDevice has been stopped | 467 // Expect the VideoCaptureDevice has been stopped |
| 470 EXPECT_EQ(0u, host_->entries_.size()); | 468 EXPECT_EQ(0u, host_->entries_.size()); |
| 471 } | 469 } |
| 472 | 470 |
| 473 void NotifyPacketReady() { | 471 void NotifyPacketReady() { |
| 474 base::RunLoop run_loop; | 472 base::RunLoop run_loop; |
| 475 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _, _, _)) | 473 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _, _, _)) |
| 476 .Times(AnyNumber()) | 474 .Times(AnyNumber()) |
| 477 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())) | 475 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())) |
| 478 .RetiresOnSaturation(); | 476 .RetiresOnSaturation(); |
| 479 run_loop.Run(); | 477 run_loop.Run(); |
| 480 } | 478 } |
| 481 | 479 |
| 482 void ReturnReceivedPackets() { | 480 void ReturnReceivedPackets() { |
| 483 host_->ReturnReceivedDibs(kDeviceId); | 481 host_->ReturnReceivedDibs(kDeviceId); |
| 484 } | 482 } |
| 485 | 483 |
| 486 void SimulateError() { | 484 void SimulateError() { |
| 487 // Expect a change state to error state sent through IPC. | 485 // Expect a change state to error state sent through IPC. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 505 | 503 |
| 506 scoped_refptr<MockVideoCaptureHost> host_; | 504 scoped_refptr<MockVideoCaptureHost> host_; |
| 507 | 505 |
| 508 private: | 506 private: |
| 509 StrictMock<MockMediaStreamRequester> stream_requester_; | 507 StrictMock<MockMediaStreamRequester> stream_requester_; |
| 510 scoped_ptr<media::AudioManager> audio_manager_; | 508 scoped_ptr<media::AudioManager> audio_manager_; |
| 511 scoped_ptr<MediaStreamManager> media_stream_manager_; | 509 scoped_ptr<MediaStreamManager> media_stream_manager_; |
| 512 content::TestBrowserThreadBundle thread_bundle_; | 510 content::TestBrowserThreadBundle thread_bundle_; |
| 513 content::TestBrowserContext browser_context_; | 511 content::TestBrowserContext browser_context_; |
| 514 content::TestContentBrowserClient browser_client_; | 512 content::TestContentBrowserClient browser_client_; |
| 515 scoped_refptr<base::MessageLoopProxy> message_loop_; | 513 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 516 int opened_session_id_; | 514 int opened_session_id_; |
| 517 std::string opened_device_label_; | 515 std::string opened_device_label_; |
| 518 | 516 |
| 519 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 517 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |
| 520 }; | 518 }; |
| 521 | 519 |
| 522 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { | 520 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { |
| 523 StartCapture(); | 521 StartCapture(); |
| 524 | 522 |
| 525 // When the session is closed via the stream without stopping capture, the | 523 // When the session is closed via the stream without stopping capture, the |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 #ifdef DUMP_VIDEO | 558 #ifdef DUMP_VIDEO |
| 561 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 559 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
| 562 CaptureAndDumpVideo(640, 480, 30); | 560 CaptureAndDumpVideo(640, 480, 30); |
| 563 } | 561 } |
| 564 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 562 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
| 565 CaptureAndDumpVideo(1280, 720, 30); | 563 CaptureAndDumpVideo(1280, 720, 30); |
| 566 } | 564 } |
| 567 #endif | 565 #endif |
| 568 | 566 |
| 569 } // namespace content | 567 } // namespace content |
| OLD | NEW |