| 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/file_util.h" |   10 #include "base/file_util.h" | 
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  381   void CloseSession() { |  381   void CloseSession() { | 
|  382     if (opened_device_label_.empty()) |  382     if (opened_device_label_.empty()) | 
|  383       return; |  383       return; | 
|  384     media_stream_manager_->CancelRequest(opened_device_label_); |  384     media_stream_manager_->CancelRequest(opened_device_label_); | 
|  385     opened_device_label_.clear(); |  385     opened_device_label_.clear(); | 
|  386     opened_session_id_ = kInvalidMediaCaptureSessionId; |  386     opened_session_id_ = kInvalidMediaCaptureSessionId; | 
|  387   } |  387   } | 
|  388  |  388  | 
|  389  protected: |  389  protected: | 
|  390   void StartCapture() { |  390   void StartCapture() { | 
|  391     EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) |  391     EXPECT_CALL(*host_.get(), OnNewBufferCreated(kDeviceId, _, _, _)) | 
|  392         .Times(AnyNumber()).WillRepeatedly(Return()); |  392         .Times(AnyNumber()) | 
 |  393         .WillRepeatedly(Return()); | 
|  393  |  394  | 
|  394     base::RunLoop run_loop; |  395     base::RunLoop run_loop; | 
|  395     EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |  396     EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _)) | 
|  396         .Times(AnyNumber()).WillOnce(ExitMessageLoop( |  397         .Times(AnyNumber()) | 
|  397             message_loop_, run_loop.QuitClosure())); |  398         .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 
|  398  |  399  | 
|  399     media::VideoCaptureParams params; |  400     media::VideoCaptureParams params; | 
|  400     params.requested_format = media::VideoCaptureFormat( |  401     params.requested_format = media::VideoCaptureFormat( | 
|  401         gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); |  402         gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); | 
|  402     host_->OnStartCapture(kDeviceId, opened_session_id_, params); |  403     host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 
|  403     run_loop.Run(); |  404     run_loop.Run(); | 
|  404   } |  405   } | 
|  405  |  406  | 
|  406   void StartStopCapture() { |  407   void StartStopCapture() { | 
|  407     // Quickly start and then stop capture, without giving much chance for |  408     // Quickly start and then stop capture, without giving much chance for | 
|  408     // asynchronous start operations to complete. |  409     // asynchronous start operations to complete. | 
|  409     InSequence s; |  410     InSequence s; | 
|  410     base::RunLoop run_loop; |  411     base::RunLoop run_loop; | 
|  411     EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); |  412     EXPECT_CALL(*host_.get(), | 
 |  413                 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)); | 
|  412     media::VideoCaptureParams params; |  414     media::VideoCaptureParams params; | 
|  413     params.requested_format = media::VideoCaptureFormat( |  415     params.requested_format = media::VideoCaptureFormat( | 
|  414         gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); |  416         gfx::Size(352, 288), 30, media::PIXEL_FORMAT_I420); | 
|  415     host_->OnStartCapture(kDeviceId, opened_session_id_, params); |  417     host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 
|  416     host_->OnStopCapture(kDeviceId); |  418     host_->OnStopCapture(kDeviceId); | 
|  417     run_loop.RunUntilIdle(); |  419     run_loop.RunUntilIdle(); | 
|  418   } |  420   } | 
|  419  |  421  | 
|  420 #ifdef DUMP_VIDEO |  422 #ifdef DUMP_VIDEO | 
|  421   void CaptureAndDumpVideo(int width, int height, int frame_rate) { |  423   void CaptureAndDumpVideo(int width, int height, int frame_rate) { | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  432     params.requested_format = |  434     params.requested_format = | 
|  433         media::VideoCaptureFormat(gfx::Size(width, height), frame_rate); |  435         media::VideoCaptureFormat(gfx::Size(width, height), frame_rate); | 
|  434     host_->SetDumpVideo(true); |  436     host_->SetDumpVideo(true); | 
|  435     host_->OnStartCapture(kDeviceId, opened_session_id_, params); |  437     host_->OnStartCapture(kDeviceId, opened_session_id_, params); | 
|  436     run_loop.Run(); |  438     run_loop.Run(); | 
|  437   } |  439   } | 
|  438 #endif |  440 #endif | 
|  439  |  441  | 
|  440   void StopCapture() { |  442   void StopCapture() { | 
|  441     base::RunLoop run_loop; |  443     base::RunLoop run_loop; | 
|  442     EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |  444     EXPECT_CALL(*host_.get(), | 
 |  445                 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) | 
|  443         .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); |  446         .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())); | 
|  444  |  447  | 
|  445     host_->OnStopCapture(kDeviceId); |  448     host_->OnStopCapture(kDeviceId); | 
|  446     host_->SetReturnReceivedDibs(true); |  449     host_->SetReturnReceivedDibs(true); | 
|  447     host_->ReturnReceivedDibs(kDeviceId); |  450     host_->ReturnReceivedDibs(kDeviceId); | 
|  448  |  451  | 
|  449     run_loop.Run(); |  452     run_loop.Run(); | 
|  450  |  453  | 
|  451     host_->SetReturnReceivedDibs(false); |  454     host_->SetReturnReceivedDibs(false); | 
|  452     // Expect the VideoCaptureDevice has been stopped |  455     // Expect the VideoCaptureDevice has been stopped | 
|  453     EXPECT_EQ(0u, host_->entries_.size()); |  456     EXPECT_EQ(0u, host_->entries_.size()); | 
|  454   } |  457   } | 
|  455  |  458  | 
|  456   void NotifyPacketReady() { |  459   void NotifyPacketReady() { | 
|  457     base::RunLoop run_loop; |  460     base::RunLoop run_loop; | 
|  458     EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _, _)) |  461     EXPECT_CALL(*host_.get(), OnBufferFilled(kDeviceId, _, _, _)) | 
|  459         .Times(AnyNumber()).WillOnce(ExitMessageLoop( |  462         .Times(AnyNumber()) | 
|  460             message_loop_, run_loop.QuitClosure())) |  463         .WillOnce(ExitMessageLoop(message_loop_, run_loop.QuitClosure())) | 
|  461         .RetiresOnSaturation(); |  464         .RetiresOnSaturation(); | 
|  462     run_loop.Run(); |  465     run_loop.Run(); | 
|  463   } |  466   } | 
|  464  |  467  | 
|  465   void ReturnReceivedPackets() { |  468   void ReturnReceivedPackets() { | 
|  466     host_->ReturnReceivedDibs(kDeviceId); |  469     host_->ReturnReceivedDibs(kDeviceId); | 
|  467   } |  470   } | 
|  468  |  471  | 
|  469   void SimulateError() { |  472   void SimulateError() { | 
|  470     // Expect a change state to error state sent through IPC. |  473     // Expect a change state to error state sent through IPC. | 
|  471     EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_ERROR)) |  474     EXPECT_CALL(*host_.get(), | 
|  472         .Times(1); |  475                 OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_ERROR)).Times(1); | 
|  473     VideoCaptureControllerID id(kDeviceId); |  476     VideoCaptureControllerID id(kDeviceId); | 
|  474     host_->OnError(id); |  477     host_->OnError(id); | 
|  475     // Wait for the error callback. |  478     // Wait for the error callback. | 
|  476     base::RunLoop().RunUntilIdle(); |  479     base::RunLoop().RunUntilIdle(); | 
|  477   } |  480   } | 
|  478  |  481  | 
|  479   scoped_refptr<MockVideoCaptureHost> host_; |  482   scoped_refptr<MockVideoCaptureHost> host_; | 
|  480  |  483  | 
|  481  private: |  484  private: | 
|  482   StrictMock<MockMediaStreamRequester> stream_requester_; |  485   StrictMock<MockMediaStreamRequester> stream_requester_; | 
|  483   scoped_ptr<media::AudioManager> audio_manager_; |  486   scoped_ptr<media::AudioManager> audio_manager_; | 
|  484   scoped_ptr<MediaStreamManager> media_stream_manager_; |  487   scoped_ptr<MediaStreamManager> media_stream_manager_; | 
|  485   content::TestBrowserThreadBundle thread_bundle_; |  488   content::TestBrowserThreadBundle thread_bundle_; | 
|  486   content::TestBrowserContext browser_context_; |  489   content::TestBrowserContext browser_context_; | 
|  487   content::TestContentBrowserClient browser_client_; |  490   content::TestContentBrowserClient browser_client_; | 
|  488   scoped_refptr<base::MessageLoopProxy> message_loop_; |  491   scoped_refptr<base::MessageLoopProxy> message_loop_; | 
|  489   int opened_session_id_; |  492   int opened_session_id_; | 
|  490   std::string opened_device_label_; |  493   std::string opened_device_label_; | 
|  491  |  494  | 
|  492   DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |  495   DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 
|  493 }; |  496 }; | 
|  494  |  497  | 
|  495 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { |  498 TEST_F(VideoCaptureHostTest, CloseSessionWithoutStopping) { | 
|  496   StartCapture(); |  499   StartCapture(); | 
|  497  |  500  | 
|  498   // When the session is closed via the stream without stopping capture, the |  501   // When the session is closed via the stream without stopping capture, the | 
|  499   // ENDED event is sent. |  502   // ENDED event is sent. | 
|  500   EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_ENDED)) |  503   EXPECT_CALL(*host_.get(), | 
|  501       .Times(1); |  504               OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_ENDED)).Times(1); | 
|  502   CloseSession(); |  505   CloseSession(); | 
|  503   base::RunLoop().RunUntilIdle(); |  506   base::RunLoop().RunUntilIdle(); | 
|  504 } |  507 } | 
|  505  |  508  | 
|  506 TEST_F(VideoCaptureHostTest, StopWhileStartPending) { |  509 TEST_F(VideoCaptureHostTest, StopWhileStartPending) { | 
|  507   StartStopCapture(); |  510   StartStopCapture(); | 
|  508 } |  511 } | 
|  509  |  512  | 
|  510 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { |  513 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { | 
|  511   StartCapture(); |  514   StartCapture(); | 
|  512   NotifyPacketReady(); |  515   NotifyPacketReady(); | 
|  513   NotifyPacketReady(); |  516   NotifyPacketReady(); | 
|  514   ReturnReceivedPackets(); |  517   ReturnReceivedPackets(); | 
|  515   StopCapture(); |  518   StopCapture(); | 
|  516 } |  519 } | 
|  517  |  520  | 
|  518 TEST_F(VideoCaptureHostTest, StartCaptureErrorStop) { |  521 TEST_F(VideoCaptureHostTest, StartCaptureErrorStop) { | 
|  519   StartCapture(); |  522   StartCapture(); | 
|  520   SimulateError(); |  523   SimulateError(); | 
|  521   StopCapture(); |  524   StopCapture(); | 
|  522 } |  525 } | 
|  523  |  526  | 
|  524 TEST_F(VideoCaptureHostTest, StartCaptureError) { |  527 TEST_F(VideoCaptureHostTest, StartCaptureError) { | 
|  525   EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)) |  528   EXPECT_CALL(*host_.get(), | 
|  526       .Times(0); |  529               OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STOPPED)).Times(0); | 
|  527   StartCapture(); |  530   StartCapture(); | 
|  528   NotifyPacketReady(); |  531   NotifyPacketReady(); | 
|  529   SimulateError(); |  532   SimulateError(); | 
|  530   base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); |  533   base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); | 
|  531 } |  534 } | 
|  532  |  535  | 
|  533 #ifdef DUMP_VIDEO |  536 #ifdef DUMP_VIDEO | 
|  534 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |  537 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 
|  535   CaptureAndDumpVideo(640, 480, 30); |  538   CaptureAndDumpVideo(640, 480, 30); | 
|  536 } |  539 } | 
|  537 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |  540 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 
|  538   CaptureAndDumpVideo(1280, 720, 30); |  541   CaptureAndDumpVideo(1280, 720, 30); | 
|  539 } |  542 } | 
|  540 #endif |  543 #endif | 
|  541  |  544  | 
|  542 }  // namespace content |  545 }  // namespace content | 
| OLD | NEW |