| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // VideoCaptureController is the glue between VideoCaptureHost, | 5 // VideoCaptureController is the glue between VideoCaptureHost, |
| 6 // VideoCaptureManager and VideoCaptureDevice. | 6 // VideoCaptureManager and VideoCaptureDevice. |
| 7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and | 7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and |
| 8 // is responsible for keeping track of TransportDIBs and filling them with I420 | 8 // is responsible for keeping track of shared DIBs and filling them with I420 |
| 9 // video frames for IPC communication between VideoCaptureHost and | 9 // video frames for IPC communication between VideoCaptureHost and |
| 10 // VideoCaptureMessageFilter. | 10 // VideoCaptureMessageFilter. |
| 11 // It implements media::VideoCaptureDevice::EventHandler to get video frames | 11 // It implements media::VideoCaptureDevice::EventHandler to get video frames |
| 12 // from a VideoCaptureDevice object and do color conversion straight into the | 12 // from a VideoCaptureDevice object and do color conversion straight into the |
| 13 // TransportDIBs to avoid a memory copy. | 13 // shared DIBs to avoid a memory copy. |
| 14 // It serves multiple VideoCaptureControllerEventHandlers. |
| 14 | 15 |
| 15 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 16 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| 16 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 17 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| 17 | 18 |
| 18 #include <list> | 19 #include <list> |
| 19 #include <map> | 20 #include <map> |
| 20 | 21 |
| 22 #include "base/compiler_specific.h" |
| 21 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 22 #include "base/process.h" | 24 #include "base/process.h" |
| 23 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 24 #include "base/task.h" | 26 #include "base/task.h" |
| 25 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 27 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
| 28 #include "media/video/capture/video_capture.h" |
| 26 #include "media/video/capture/video_capture_device.h" | 29 #include "media/video/capture/video_capture_device.h" |
| 27 #include "media/video/capture/video_capture_types.h" | 30 #include "media/video/capture/video_capture_types.h" |
| 28 #include "ui/gfx/surface/transport_dib.h" | |
| 29 | 31 |
| 30 namespace media_stream { | 32 namespace media_stream { |
| 31 class VideoCaptureManager; | 33 class VideoCaptureManager; |
| 32 } // namespace media_stream | 34 } // namespace media_stream |
| 33 | 35 |
| 34 class VideoCaptureController | 36 class VideoCaptureController |
| 35 : public base::RefCountedThreadSafe<VideoCaptureController>, | 37 : public base::RefCountedThreadSafe<VideoCaptureController>, |
| 36 public media::VideoCaptureDevice::EventHandler { | 38 public media::VideoCaptureDevice::EventHandler { |
| 37 public: | 39 public: |
| 38 VideoCaptureController( | 40 VideoCaptureController( |
| 39 const VideoCaptureControllerID& id, | |
| 40 base::ProcessHandle render_process, | |
| 41 VideoCaptureControllerEventHandler* event_handler, | |
| 42 media_stream::VideoCaptureManager* video_capture_manager); | 41 media_stream::VideoCaptureManager* video_capture_manager); |
| 43 virtual ~VideoCaptureController(); | 42 virtual ~VideoCaptureController(); |
| 44 | 43 |
| 45 // Starts video capturing and tries to use the resolution specified in | 44 // Start video capturing and try to use the resolution specified in |
| 46 // params. | 45 // |params|. |
| 47 // When capturing has started VideoCaptureControllerEventHandler::OnFrameInfo | 46 // When capturing has started, the |event_handler| receives a call OnFrameInfo |
| 48 // is called with resolution that best matches the requested that the video | 47 // with resolution that best matches the requested that the video |
| 49 // capture device support. | 48 // capture device support. |
| 50 void StartCapture(const media::VideoCaptureParams& params); | 49 void StartCapture(const VideoCaptureControllerID& id, |
| 50 VideoCaptureControllerEventHandler* event_handler, |
| 51 base::ProcessHandle render_process, |
| 52 const media::VideoCaptureParams& params); |
| 51 | 53 |
| 52 // Stop video capture. | 54 // Stop video capture. |
| 53 // When the capture is stopped and all TransportDIBS have been returned | 55 // When the capture is stopped and all DIBs have been returned, |
| 54 // VideoCaptureControllerEventHandler::OnReadyToDelete will be called. | 56 // VideoCaptureControllerEventHandler::OnReadyToDelete will be called. |
| 55 // |stopped_cb| may be NULL. But a non-NULL Closure can be used to get | 57 // |force_buffer_return| allows controller to take back all buffers used |
| 56 // a notification when the device is stopped, regardless of | 58 // by |event_handler|. |
| 57 // VideoCaptureController's state. | 59 void StopCapture(const VideoCaptureControllerID& id, |
| 58 void StopCapture(base::Closure stopped_cb); | 60 VideoCaptureControllerEventHandler* event_handler, |
| 61 bool force_buffer_return); |
| 59 | 62 |
| 60 // Return a buffer previously given in | 63 // Return a buffer previously given in |
| 61 // VideoCaptureControllerEventHandler::OnBufferReady. | 64 // VideoCaptureControllerEventHandler::OnBufferReady. |
| 62 void ReturnBuffer(int buffer_id); | 65 void ReturnBuffer(const VideoCaptureControllerID& id, |
| 66 VideoCaptureControllerEventHandler* event_handler, |
| 67 int buffer_id); |
| 63 | 68 |
| 64 // Implement media::VideoCaptureDevice::EventHandler. | 69 // Implement media::VideoCaptureDevice::EventHandler. |
| 65 virtual void OnIncomingCapturedFrame(const uint8* data, | 70 virtual void OnIncomingCapturedFrame(const uint8* data, |
| 66 int length, | 71 int length, |
| 67 base::Time timestamp); | 72 base::Time timestamp) OVERRIDE; |
| 68 virtual void OnError(); | 73 virtual void OnError() OVERRIDE; |
| 69 virtual void OnFrameInfo(const media::VideoCaptureDevice::Capability& info); | 74 virtual void OnFrameInfo( |
| 75 const media::VideoCaptureDevice::Capability& info) OVERRIDE; |
| 76 virtual void OnDeviceState(bool in_use) OVERRIDE; |
| 70 | 77 |
| 71 private: | 78 private: |
| 72 // Called by VideoCaptureManager when a device have been stopped. | 79 struct ControllerClient; |
| 73 void OnDeviceStopped(base::Closure stopped_cb); | 80 typedef std::list<ControllerClient*> ControllerClients; |
| 81 |
| 82 // Worker functions on IO thread. |
| 83 void DoIncomingCapturedFrameOnIOThread(int buffer_id, base::Time timestamp); |
| 84 void DoFrameInfoOnIOThread(const media::VideoCaptureDevice::Capability info); |
| 85 void DoErrorOnIOThread(); |
| 86 void DoDeviceStateOnIOThread(bool in_use); |
| 87 |
| 88 // Send frame info and init buffers to |client|. |
| 89 void SendFrameInfoAndBuffers(ControllerClient* client); |
| 90 |
| 91 // Helpers. |
| 92 // Find a client of |id| and |handler| in |clients|. |
| 93 ControllerClients::iterator FindClient( |
| 94 const VideoCaptureControllerID& id, |
| 95 VideoCaptureControllerEventHandler* handler, |
| 96 ControllerClients& clients); |
| 97 // Decide what to do after kStopping state. Dependent on events, controller |
| 98 // can stay in kStopping state, or go to kStopped, or restart capture. |
| 99 void PostStopping(); |
| 100 // Check if any DIB is used by client. |
| 101 bool ClientHasDIB(); |
| 102 void SendFrameInfoAndBuffersWithLock( |
| 103 ControllerClient* client, int needed_size); |
| 74 | 104 |
| 75 // Lock to protect free_dibs_ and owned_dibs_. | 105 // Lock to protect free_dibs_ and owned_dibs_. |
| 76 base::Lock lock_; | 106 base::Lock lock_; |
| 77 // Handle to the render process that will receive the DIBs. | |
| 78 base::ProcessHandle render_handle_; | |
| 79 bool report_ready_to_delete_; | |
| 80 typedef std::list<int> DIBHandleList; | |
| 81 typedef std::map<int, base::SharedMemory*> DIBMap; | |
| 82 | 107 |
| 83 // Free DIBS that can be filled with video frames. | 108 typedef std::list<int /*buffer_id*/> DIBHandleList; |
| 109 // Free DIBs that can be filled with video frames. |
| 110 // It can be modified on both IO and device threads. |
| 84 DIBHandleList free_dibs_; | 111 DIBHandleList free_dibs_; |
| 85 | 112 |
| 86 // All DIBS created by this object. | 113 typedef std::map<int /*buffer_id*/, base::SharedMemory*> DIBMap; |
| 114 // All DIBs created by this object. |
| 115 // It's modified only on IO thread. |
| 87 DIBMap owned_dibs_; | 116 DIBMap owned_dibs_; |
| 88 VideoCaptureControllerEventHandler* event_handler_; | |
| 89 | 117 |
| 90 // The parameter that was requested when starting the capture device. | 118 typedef std::map<int /*buffer_id*/, int /*count*/> ClientSideDIBCount; |
| 91 media::VideoCaptureParams params_; | 119 // Keep count of clients to which a DIB has been sent. |
| 120 // It's modified only on IO thread. |
| 121 ClientSideDIBCount client_side_dib_count_; |
| 92 | 122 |
| 93 // ID used for identifying this object. | 123 // All clients served by this controller. |
| 94 VideoCaptureControllerID id_; | 124 ControllerClients controller_clients_; |
| 125 |
| 126 // All clients waiting for service. |
| 127 ControllerClients pending_clients_; |
| 128 |
| 129 // The parameter that currently used for the capturing. |
| 130 media::VideoCaptureParams current_params_; |
| 131 |
| 95 media::VideoCaptureDevice::Capability frame_info_; | 132 media::VideoCaptureDevice::Capability frame_info_; |
| 133 bool frame_info_available_; |
| 96 | 134 |
| 97 media_stream::VideoCaptureManager* video_capture_manager_; | 135 media_stream::VideoCaptureManager* video_capture_manager_; |
| 98 | 136 |
| 137 bool device_in_use_; |
| 138 media::VideoCapture::State state_; |
| 139 |
| 99 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); | 140 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); |
| 100 }; | 141 }; |
| 101 | 142 |
| 102 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 143 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| OLD | NEW |