Chromium Code Reviews| 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; | |
| 70 | 76 |
| 71 private: | 77 private: |
| 72 // Called by VideoCaptureManager when a device have been stopped. | 78 struct ControllerClient; |
| 73 void OnDeviceStopped(base::Closure stopped_cb); | 79 typedef std::list<ControllerClient*> ControllerClients; |
| 80 | |
| 81 // Callback when manager has stopped device. | |
| 82 void OnDeviceStopped(); | |
| 83 | |
| 84 // Worker functions on IO thread. | |
| 85 void DoIncomingCapturedFrameOnIOThread(int buffer_id, base::Time timestamp); | |
| 86 void DoFrameInfoOnIOThread(const media::VideoCaptureDevice::Capability info); | |
| 87 void DoErrorOnIOThread(); | |
| 88 void DoDeviceStateOnIOThread(bool in_use); | |
| 89 void DoDeviceStoppedOnIOThread(); | |
| 90 | |
| 91 // Send frame info and init buffers to |client|. | |
| 92 void SendFrameInfoAndBuffers(ControllerClient* client); | |
| 93 | |
| 94 // Helpers. | |
| 95 // Find a client of |id| and |handler| in |clients|. | |
| 96 ControllerClients::iterator FindClient( | |
| 97 const VideoCaptureControllerID& id, | |
| 98 VideoCaptureControllerEventHandler* handler, | |
| 99 ControllerClients& clients); | |
| 100 // Decide what to do after kStopping state. Dependent on events, controller | |
| 101 // can stay in kStopping state, or go to kStopped, or restart capture. | |
| 102 void PostStopping(); | |
| 103 // Check if any DIB is used by client. | |
| 104 bool ClientHasDIB(); | |
| 105 void SendFrameInfoAndBuffersWithLock( | |
| 106 ControllerClient* client, int needed_size); | |
| 74 | 107 |
| 75 // Lock to protect free_dibs_ and owned_dibs_. | 108 // Lock to protect free_dibs_ and owned_dibs_. |
| 76 base::Lock lock_; | 109 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 | 110 |
| 83 // Free DIBS that can be filled with video frames. | 111 typedef std::list<int /*buffer_id*/> DIBHandleList; |
| 112 // Free DIBs that can be filled with video frames. | |
| 113 // It can be modified on both IO and device threads. | |
| 84 DIBHandleList free_dibs_; | 114 DIBHandleList free_dibs_; |
| 85 | 115 |
| 86 // All DIBS created by this object. | 116 typedef std::map<int /*buffer_id*/, base::SharedMemory*> DIBMap; |
| 117 // All DIBs created by this object. | |
| 118 // It's modified only on IO thread. | |
| 87 DIBMap owned_dibs_; | 119 DIBMap owned_dibs_; |
| 88 VideoCaptureControllerEventHandler* event_handler_; | |
| 89 | 120 |
| 90 // The parameter that was requested when starting the capture device. | 121 typedef std::map<int /*buffer_id*/, int /*count*/> ClientSideDIBCount; |
| 91 media::VideoCaptureParams params_; | 122 // Keep count of clients to which a DIB has been sent. |
| 123 // It's modified only on IO thread. | |
| 124 ClientSideDIBCount client_side_dib_count_; | |
|
scherkus (not reviewing)
2011/10/31 03:20:18
I find the buffer_id system awfully confusing
why
wjia(left Chromium)
2011/10/31 22:02:44
Added SharedDIB and removed ClientSideDIBCount.
U
scherkus (not reviewing)
2011/11/01 02:18:38
I guess I don't fully understand. What is the flex
wjia(left Chromium)
2011/11/01 21:34:23
By "flexibility", I mean we can improve the captur
| |
| 92 | 125 |
| 93 // ID used for identifying this object. | 126 // All clients served by this controller. |
| 94 VideoCaptureControllerID id_; | 127 ControllerClients controller_clients_; |
| 128 | |
| 129 // All clients waiting for service. | |
| 130 ControllerClients pending_clients_; | |
| 131 | |
| 132 // The parameter that currently used for the capturing. | |
| 133 media::VideoCaptureParams current_params_; | |
| 134 | |
| 95 media::VideoCaptureDevice::Capability frame_info_; | 135 media::VideoCaptureDevice::Capability frame_info_; |
| 136 bool frame_info_available_; | |
| 96 | 137 |
| 97 media_stream::VideoCaptureManager* video_capture_manager_; | 138 media_stream::VideoCaptureManager* video_capture_manager_; |
| 98 | 139 |
| 140 bool device_in_use_; | |
| 141 media::VideoCapture::State state_; | |
| 142 | |
| 99 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); | 143 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); |
| 100 }; | 144 }; |
| 101 | 145 |
| 102 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 146 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| OLD | NEW |