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 "content/browser/renderer_host/media/video_capture_host.h" | 5 #include "content/browser/renderer_host/media/video_capture_host.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 // Id used to identify the capture session between renderer and | 60 // Id used to identify the capture session between renderer and |
61 // video_capture_host. This is an arbitrary value. | 61 // video_capture_host. This is an arbitrary value. |
62 static const int kDeviceId = 555; | 62 static const int kDeviceId = 555; |
63 | 63 |
64 // Define to enable test where video is dumped to file. | 64 // Define to enable test where video is dumped to file. |
65 // #define DUMP_VIDEO | 65 // #define DUMP_VIDEO |
66 | 66 |
67 // Define to use a real video capture device. | 67 // Define to use a real video capture device. |
68 // #define TEST_REAL_CAPTURE_DEVICE | 68 // #define TEST_REAL_CAPTURE_DEVICE |
69 | 69 |
70 // Simple class used for dumping video to a file. This can be used for | 70 // Simple class used for dumping video to a file. This can be used for verifying |
71 // verifying the output. | 71 // the output. |
72 class DumpVideo { | 72 class DumpVideo { |
73 public: | 73 public: |
74 DumpVideo() {} | 74 DumpVideo() {} |
75 const gfx::Size& coded_size() const { return coded_size_; } | 75 const gfx::Size& coded_size() const { return coded_size_; } |
76 void StartDump(const gfx::Size& coded_size) { | 76 void StartDump(const gfx::Size& coded_size) { |
77 base::FilePath file_name = base::FilePath(base::StringPrintf( | 77 base::FilePath file_name = base::FilePath(base::StringPrintf( |
78 FILE_PATH_LITERAL("dump_w%d_h%d.yuv"), | 78 FILE_PATH_LITERAL("dump_w%d_h%d.yuv"), |
79 coded_size.width(), | 79 coded_size.width(), |
80 coded_size.height())); | 80 coded_size.height())); |
81 file_.reset(base::OpenFile(file_name, "wb")); | 81 file_.reset(base::OpenFile(file_name, "wb")); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 MOCK_METHOD4(DeviceOpened, void(int render_frame_id, | 120 MOCK_METHOD4(DeviceOpened, void(int render_frame_id, |
121 int page_request_id, | 121 int page_request_id, |
122 const std::string& label, | 122 const std::string& label, |
123 const StreamDeviceInfo& device_info)); | 123 const StreamDeviceInfo& device_info)); |
124 MOCK_METHOD1(DevicesChanged, void(MediaStreamType type)); | 124 MOCK_METHOD1(DevicesChanged, void(MediaStreamType type)); |
125 | 125 |
126 private: | 126 private: |
127 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamRequester); | 127 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamRequester); |
128 }; | 128 }; |
129 | 129 |
130 class MockVideoCaptureHost : public VideoCaptureHost { | 130 class MockVideoCaptureHost : public VideoCaptureHost { |
mcasas
2016/10/05 23:42:02
After the renaming in this PS, the linker
started
| |
131 public: | 131 public: |
132 MockVideoCaptureHost(MediaStreamManager* manager) | 132 MockVideoCaptureHost(MediaStreamManager* manager) |
133 : VideoCaptureHost(manager), | 133 : VideoCaptureHost(manager), |
134 return_buffers_(false), | 134 return_buffers_(false), |
135 dump_video_(false) {} | 135 dump_video_(false) {} |
136 | 136 |
137 // A list of mock methods. | 137 // A list of mock methods. |
138 MOCK_METHOD4(OnNewBufferCreated, | 138 MOCK_METHOD4(OnNewBufferCreated, |
139 void(int device_id, | 139 void(int device_id, |
140 base::SharedMemoryHandle handle, | 140 base::SharedMemoryHandle handle, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 host_ = new MockVideoCaptureHost(media_stream_manager_.get()); | 284 host_ = new MockVideoCaptureHost(media_stream_manager_.get()); |
285 host_->OnChannelConnected(base::GetCurrentProcId()); | 285 host_->OnChannelConnected(base::GetCurrentProcId()); |
286 | 286 |
287 OpenSession(); | 287 OpenSession(); |
288 } | 288 } |
289 | 289 |
290 void TearDown() override { | 290 void TearDown() override { |
291 // Verifies and removes the expectations on host_ and | 291 // Verifies and removes the expectations on host_ and |
292 // returns true iff successful. | 292 // returns true iff successful. |
293 Mock::VerifyAndClearExpectations(host_.get()); | 293 Mock::VerifyAndClearExpectations(host_.get()); |
294 EXPECT_EQ(0u, host_->entries_.size()); | 294 EXPECT_TRUE(host_->controllers_.empty()); |
295 | 295 |
296 CloseSession(); | 296 CloseSession(); |
297 | 297 |
298 // Simulate closing the IPC sender. | 298 // Simulate closing the IPC sender. |
299 host_->OnChannelClosing(); | 299 host_->OnChannelClosing(); |
300 | 300 |
301 // Release the reference to the mock object. The object will be destructed | 301 // Release the reference to the mock object. The object will be destructed |
302 // on the current message loop. | 302 // on the current message loop. |
303 host_ = NULL; | 303 host_ = NULL; |
304 | 304 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 // Quickly start and then stop capture, without giving much chance for | 395 // Quickly start and then stop capture, without giving much chance for |
396 // asynchronous start operations to complete. | 396 // asynchronous start operations to complete. |
397 InSequence s; | 397 InSequence s; |
398 base::RunLoop run_loop; | 398 base::RunLoop run_loop; |
399 EXPECT_CALL(*host_.get(), | 399 EXPECT_CALL(*host_.get(), |
400 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); | 400 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); |
401 media::VideoCaptureParams params; | 401 media::VideoCaptureParams params; |
402 params.requested_format = media::VideoCaptureFormat( | 402 params.requested_format = media::VideoCaptureFormat( |
403 gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); | 403 gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); |
404 host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 404 host_->OnStartCapture(kDeviceId, opened_session_id_, params); |
405 host_->OnStopCapture(kDeviceId); | 405 host_->Stop(kDeviceId); |
406 run_loop.RunUntilIdle(); | 406 run_loop.RunUntilIdle(); |
407 WaitForVideoDeviceThread(); | 407 WaitForVideoDeviceThread(); |
408 } | 408 } |
409 | |
410 void PauseResumeCapture() { | |
411 InSequence s; | |
412 base::RunLoop run_loop; | |
413 EXPECT_CALL(*host_.get(), | |
414 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_PAUSED)); | |
415 host_->Pause(kDeviceId); | |
416 | |
417 EXPECT_CALL(*host_.get(), | |
418 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_RESUMED)); | |
419 media::VideoCaptureParams params; | |
420 params.requested_format = media::VideoCaptureFormat( | |
421 gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); | |
422 host_->OnResumeCapture(kDeviceId, opened_session_id_, params); | |
423 run_loop.RunUntilIdle(); | |
424 WaitForVideoDeviceThread(); | |
425 } | |
409 | 426 |
410 #ifdef DUMP_VIDEO | 427 #ifdef DUMP_VIDEO |
411 void CaptureAndDumpVideo(int width, int height, int frame_rate) { | 428 void CaptureAndDumpVideo(int width, int height, int frame_rate) { |
412 InSequence s; | 429 InSequence s; |
413 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) | 430 EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) |
414 .Times(AnyNumber()).WillRepeatedly(Return()); | 431 .Times(AnyNumber()).WillRepeatedly(Return()); |
415 | 432 |
416 base::RunLoop run_loop; | 433 base::RunLoop run_loop; |
417 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _, _, _, _, _, _, _)) | 434 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _, _, _, _, _, _, _)) |
418 .Times(AnyNumber()) | 435 .Times(AnyNumber()) |
419 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 436 .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |
420 | 437 |
421 media::VideoCaptureParams params; | 438 media::VideoCaptureParams params; |
422 params.requested_format = | 439 params.requested_format = |
423 media::VideoCaptureFormat(gfx::Size(width, height), frame_rate); | 440 media::VideoCaptureFormat(gfx::Size(width, height), frame_rate); |
424 host_->SetDumpVideo(true); | 441 host_->SetDumpVideo(true); |
425 host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 442 host_->OnStartCapture(kDeviceId, opened_session_id_, params); |
426 run_loop.Run(); | 443 run_loop.Run(); |
427 } | 444 } |
428 #endif | 445 #endif |
429 | 446 |
430 void StopCapture() { | 447 void StopCapture() { |
431 base::RunLoop run_loop; | 448 base::RunLoop run_loop; |
432 EXPECT_CALL(*host_.get(), | 449 EXPECT_CALL(*host_.get(), |
433 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) | 450 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |
434 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())); | 451 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())); |
435 | 452 |
436 host_->OnStopCapture(kDeviceId); | 453 host_->Stop(kDeviceId); |
437 host_->SetReturnReceivedDibs(true); | 454 host_->SetReturnReceivedDibs(true); |
438 host_->ReturnReceivedDibs(kDeviceId); | 455 host_->ReturnReceivedDibs(kDeviceId); |
439 | 456 |
440 run_loop.Run(); | 457 run_loop.Run(); |
441 | 458 |
442 host_->SetReturnReceivedDibs(false); | 459 host_->SetReturnReceivedDibs(false); |
443 // Expect the VideoCaptureDevice has been stopped | 460 // Expect the VideoCaptureDevice has been stopped |
444 EXPECT_EQ(0u, host_->entries_.size()); | 461 EXPECT_TRUE(host_->controllers_.empty()); |
445 } | 462 } |
446 | 463 |
447 void NotifyPacketReady() { | 464 void NotifyPacketReady() { |
448 base::RunLoop run_loop; | 465 base::RunLoop run_loop; |
449 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId)) | 466 EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId)) |
450 .Times(AnyNumber()) | 467 .Times(AnyNumber()) |
451 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())) | 468 .WillOnce(ExitMessageLoop(task_runner_, run_loop.QuitClosure())) |
452 .RetiresOnSaturation(); | 469 .RetiresOnSaturation(); |
453 run_loop.Run(); | 470 run_loop.Run(); |
454 } | 471 } |
(...skipping 18 matching lines...) Expand all Loading... | |
473 ->PostTaskAndReply( | 490 ->PostTaskAndReply( |
474 FROM_HERE, | 491 FROM_HERE, |
475 base::Bind(&base::DoNothing), | 492 base::Bind(&base::DoNothing), |
476 run_loop.QuitClosure()); | 493 run_loop.QuitClosure()); |
477 run_loop.Run(); | 494 run_loop.Run(); |
478 } | 495 } |
479 | 496 |
480 scoped_refptr<MockVideoCaptureHost> host_; | 497 scoped_refptr<MockVideoCaptureHost> host_; |
481 | 498 |
482 private: | 499 private: |
483 // media_stream_manager_ needs to outlive thread_bundle_ because it is a | 500 // |media_stream_manager_| needs to outlive |thread_bundle_| because it is a |
484 // MessageLoop::DestructionObserver. audio_manager_ needs to outlive | 501 // MessageLoop::DestructionObserver. |audio_manager_| needs to outlive |
485 // thread_bundle_ because it uses the underlying message loop. | 502 // |thread_bundle_| because it uses the underlying message loop. |
486 StrictMock<MockMediaStreamRequester> stream_requester_; | 503 StrictMock<MockMediaStreamRequester> stream_requester_; |
487 std::unique_ptr<MediaStreamManager> media_stream_manager_; | 504 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
488 content::TestBrowserThreadBundle thread_bundle_; | 505 const content::TestBrowserThreadBundle thread_bundle_; |
489 media::ScopedAudioManagerPtr audio_manager_; | 506 media::ScopedAudioManagerPtr audio_manager_; |
490 content::TestBrowserContext browser_context_; | 507 content::TestBrowserContext browser_context_; |
491 content::TestContentBrowserClient browser_client_; | 508 content::TestContentBrowserClient browser_client_; |
492 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 509 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
493 int opened_session_id_; | 510 int opened_session_id_; |
494 std::string opened_device_label_; | 511 std::string opened_device_label_; |
495 | 512 |
496 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 513 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |
497 }; | 514 }; |
498 | 515 |
499 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { | 516 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { |
500 StartCapture(); | 517 StartCapture(); |
501 | 518 |
502 // When the session is closed via the stream without stopping capture, the | 519 // When the session is closed via the stream without stopping capture, the |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
536 | 553 |
537 #ifdef DUMP_VIDEO | 554 #ifdef DUMP_VIDEO |
538 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 555 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
539 CaptureAndDumpVideo(640, 480, 30); | 556 CaptureAndDumpVideo(640, 480, 30); |
540 } | 557 } |
541 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 558 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
542 CaptureAndDumpVideo(1280, 720, 30); | 559 CaptureAndDumpVideo(1280, 720, 30); |
543 } | 560 } |
544 #endif | 561 #endif |
545 | 562 |
563 TEST_F(VideoCaptureHostTest, PauseResumeCapture) { | |
564 StartCapture(); | |
565 PauseResumeCapture(); | |
566 StopCapture(); | |
567 } | |
568 | |
546 } // namespace content | 569 } // namespace content |
OLD | NEW |