| 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 // VideoCaptureImpl represents a capture device in renderer process. It provides | 5 // VideoCaptureImpl represents a capture device in renderer process. It provides |
| 6 // interfaces for clients to Start/Stop capture. It also communicates to clients | 6 // interfaces for clients to Start/Stop capture. It also communicates to clients |
| 7 // when buffer is ready, state of capture device is changed. | 7 // when buffer is ready, state of capture device is changed. |
| 8 | 8 |
| 9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays | 9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays |
| 10 // operation of a capture device to the browser process and receives responses | 10 // operation of a capture device to the browser process and receives responses |
| 11 // from browser process. | 11 // from browser process. |
| 12 // | 12 // |
| 13 // All public methods of VideoCaptureImpl can be called on any thread. | 13 // VideoCaptureImpl is an IO thread only object. See the comments in |
| 14 // Internally it runs on the IO thread. Clients of this class implement | 14 // video_capture_impl_manager.cc for the lifetime of this object. |
| 15 // interface media::VideoCapture::EventHandler which is called only on the IO | 15 // All methods must be called on the IO thread. |
| 16 // thread. | |
| 17 // | 16 // |
| 18 // Implementation note: tasks are posted bound to Unretained(this) to the I/O | 17 // This is an internal class used by VideoCaptureImplManager only. Do not access |
| 19 // thread and this is safe (even though the I/O thread is scoped to the renderer | 18 // this directly. |
| 20 // process) because VideoCaptureImplManager only triggers deletion of its | |
| 21 // VideoCaptureImpl's by calling DeInit which detours through the I/O thread, so | |
| 22 // as long as nobody posts tasks after the DeInit() call is made, it is | |
| 23 // guaranteed none of these Unretained posted tasks will dangle after the delete | |
| 24 // goes through. The "as long as" is guaranteed by clients of | |
| 25 // VideoCaptureImplManager not using devices after they've released | |
| 26 // VideoCaptureHandle, which is a wrapper of this object. | |
| 27 | 19 |
| 28 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 20 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
| 29 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 21 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
| 30 | 22 |
| 31 #include <list> | 23 #include <list> |
| 32 #include <map> | 24 #include <map> |
| 33 | 25 |
| 34 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
| 27 #include "base/threading/thread_checker.h" |
| 35 #include "content/common/content_export.h" | 28 #include "content/common/content_export.h" |
| 36 #include "content/common/media/video_capture.h" | 29 #include "content/common/media/video_capture.h" |
| 37 #include "content/renderer/media/video_capture_message_filter.h" | 30 #include "content/renderer/media/video_capture_message_filter.h" |
| 38 #include "media/video/capture/video_capture.h" | |
| 39 #include "media/video/capture/video_capture_types.h" | 31 #include "media/video/capture/video_capture_types.h" |
| 40 | 32 |
| 41 namespace base { | 33 namespace base { |
| 42 class MessageLoopProxy; | 34 class MessageLoopProxy; |
| 43 } // namespace base | 35 } // namespace base |
| 44 | 36 |
| 45 namespace gpu { | 37 namespace gpu { |
| 46 struct MailboxHolder; | 38 struct MailboxHolder; |
| 47 } // namespace gpu | 39 } // namespace gpu |
| 48 | 40 |
| 41 namespace media { |
| 42 class VideoFrame; |
| 43 } // namespace media |
| 44 |
| 49 namespace content { | 45 namespace content { |
| 50 | 46 |
| 51 class CONTENT_EXPORT VideoCaptureImpl | 47 class CONTENT_EXPORT VideoCaptureImpl |
| 52 : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate { | 48 : public VideoCaptureMessageFilter::Delegate { |
| 53 public: | 49 public: |
| 50 virtual ~VideoCaptureImpl(); |
| 51 |
| 54 VideoCaptureImpl(media::VideoCaptureSessionId session_id, | 52 VideoCaptureImpl(media::VideoCaptureSessionId session_id, |
| 55 VideoCaptureMessageFilter* filter); | 53 VideoCaptureMessageFilter* filter); |
| 56 virtual ~VideoCaptureImpl(); | |
| 57 | 54 |
| 58 // Start listening to IPC messages. | 55 // Start listening to IPC messages. |
| 59 void Init(); | 56 void Init(); |
| 60 | 57 |
| 61 // Stop listening to IPC messages. Call |done_cb| when done. | 58 // Stop listening to IPC messages. |
| 62 void DeInit(base::Closure done_cb); | 59 void DeInit(); |
| 63 | 60 |
| 64 // Stop/resume delivering video frames to clients, based on flag |suspend|. | 61 // Stop/resume delivering video frames to clients, based on flag |suspend|. |
| 65 void SuspendCapture(bool suspend); | 62 void SuspendCapture(bool suspend); |
| 66 | 63 |
| 67 // media::VideoCapture interface. | 64 // Start capturing using the provided parameters. |
| 68 virtual void StartCapture( | 65 // |client_id| must be unique to this object in the render process. It is |
| 69 media::VideoCapture::EventHandler* handler, | 66 // used later to stop receiving video frames. |
| 70 const media::VideoCaptureParams& params) OVERRIDE; | 67 // |state_update_cb| will be called when state changes. |
| 71 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE; | 68 // |deliver_frame_cb| will be called when a frame is ready. |
| 72 virtual bool CaptureStarted() OVERRIDE; | 69 void StartCapture( |
| 73 virtual int CaptureFrameRate() OVERRIDE; | 70 int client_id, |
| 74 virtual void GetDeviceSupportedFormats( | 71 const media::VideoCaptureParams& params, |
| 75 const DeviceFormatsCallback& callback) OVERRIDE; | 72 const VideoCaptureStateUpdateCB& state_update_cb, |
| 76 virtual void GetDeviceFormatsInUse( | 73 const VideoCaptureDeliverFrameCB& deliver_frame_cb); |
| 77 const DeviceFormatsInUseCallback& callback) OVERRIDE; | 74 |
| 75 // Stop capturing. |client_id| is the identifier used to call StartCapture. |
| 76 void StopCapture(int client_id); |
| 77 |
| 78 // Get capturing formats supported by this device. |
| 79 // |callback| will be invoked with the results. |
| 80 void GetDeviceSupportedFormats( |
| 81 const VideoCaptureDeviceFormatsCB& callback); |
| 82 |
| 83 // Get capturing formats currently in use by this device. |
| 84 // |callback| will be invoked with the results. |
| 85 void GetDeviceFormatsInUse( |
| 86 const VideoCaptureDeviceFormatsCB& callback); |
| 78 | 87 |
| 79 media::VideoCaptureSessionId session_id() const { return session_id_; } | 88 media::VideoCaptureSessionId session_id() const { return session_id_; } |
| 80 | 89 |
| 81 private: | 90 private: |
| 82 friend class VideoCaptureImplTest; | 91 friend class VideoCaptureImplTest; |
| 83 friend class MockVideoCaptureImpl; | 92 friend class MockVideoCaptureImpl; |
| 84 | 93 |
| 94 // Carries a shared memory for transferring video frames from browser to |
| 95 // renderer. |
| 85 class ClientBuffer; | 96 class ClientBuffer; |
| 86 typedef std::map<media::VideoCapture::EventHandler*, | |
| 87 media::VideoCaptureParams> ClientInfo; | |
| 88 | 97 |
| 89 void InitOnIOThread(); | 98 // Contains information for a video capture client. Including parameters |
| 90 void DeInitOnIOThread(base::Closure done_cb); | 99 // for capturing and callbacks to the client. |
| 91 void SuspendCaptureOnIOThread(bool suspend); | 100 struct ClientInfo { |
| 92 void StartCaptureOnIOThread( | 101 ClientInfo(); |
| 93 media::VideoCapture::EventHandler* handler, | 102 ~ClientInfo(); |
| 94 const media::VideoCaptureParams& params); | 103 media::VideoCaptureParams params; |
| 95 void StopCaptureOnIOThread(media::VideoCapture::EventHandler* handler); | 104 VideoCaptureStateUpdateCB state_update_cb; |
| 96 void GetDeviceSupportedFormatsOnIOThread( | 105 VideoCaptureDeliverFrameCB deliver_frame_cb; |
| 97 const DeviceFormatsCallback& callback); | 106 }; |
| 98 void GetDeviceFormatsInUseOnIOThread( | 107 typedef std::map<int, ClientInfo> ClientInfoMap; |
| 99 const DeviceFormatsInUseCallback& callback); | |
| 100 | 108 |
| 101 // VideoCaptureMessageFilter::Delegate interface. | 109 // VideoCaptureMessageFilter::Delegate interface. |
| 102 virtual void OnBufferCreated(base::SharedMemoryHandle handle, | 110 virtual void OnBufferCreated(base::SharedMemoryHandle handle, |
| 103 int length, | 111 int length, |
| 104 int buffer_id) OVERRIDE; | 112 int buffer_id) OVERRIDE; |
| 105 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE; | 113 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE; |
| 106 virtual void OnBufferReceived(int buffer_id, | 114 virtual void OnBufferReceived(int buffer_id, |
| 107 const media::VideoCaptureFormat& format, | 115 const media::VideoCaptureFormat& format, |
| 108 base::TimeTicks) OVERRIDE; | 116 base::TimeTicks) OVERRIDE; |
| 109 virtual void OnMailboxBufferReceived(int buffer_id, | 117 virtual void OnMailboxBufferReceived(int buffer_id, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 123 const scoped_refptr<ClientBuffer>& buffer, | 131 const scoped_refptr<ClientBuffer>& buffer, |
| 124 scoped_ptr<gpu::MailboxHolder> mailbox_holder); | 132 scoped_ptr<gpu::MailboxHolder> mailbox_holder); |
| 125 | 133 |
| 126 void StopDevice(); | 134 void StopDevice(); |
| 127 void RestartCapture(); | 135 void RestartCapture(); |
| 128 void StartCaptureInternal(); | 136 void StartCaptureInternal(); |
| 129 | 137 |
| 130 virtual void Send(IPC::Message* message); | 138 virtual void Send(IPC::Message* message); |
| 131 | 139 |
| 132 // Helpers. | 140 // Helpers. |
| 133 bool RemoveClient(media::VideoCapture::EventHandler* handler, | 141 bool RemoveClient(int client_id, ClientInfoMap* clients); |
| 134 ClientInfo* clients); | |
| 135 | 142 |
| 136 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; | 143 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; |
| 137 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 138 int device_id_; | 144 int device_id_; |
| 139 const int session_id_; | 145 const int session_id_; |
| 140 | 146 |
| 141 // Vector of callbacks to be notified of device format enumerations, used only | 147 // Vector of callbacks to be notified of device format enumerations, used only |
| 142 // on IO Thread. | 148 // on IO Thread. |
| 143 std::vector<DeviceFormatsCallback> device_formats_callback_queue_; | 149 std::vector<VideoCaptureDeviceFormatsCB> device_formats_cb_queue_; |
| 144 // Vector of callbacks to be notified of a device's in use capture format(s), | 150 // Vector of callbacks to be notified of a device's in use capture format(s), |
| 145 // used only on IO Thread. | 151 // used only on IO Thread. |
| 146 std::vector<DeviceFormatsInUseCallback> device_formats_in_use_callback_queue_; | 152 std::vector<VideoCaptureDeviceFormatsCB> device_formats_in_use_cb_queue_; |
| 147 | 153 |
| 148 // Buffers available for sending to the client. | 154 // Buffers available for sending to the client. |
| 149 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; | 155 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; |
| 150 ClientBufferMap client_buffers_; | 156 ClientBufferMap client_buffers_; |
| 151 | 157 |
| 152 ClientInfo clients_; | 158 ClientInfoMap clients_; |
| 153 ClientInfo clients_pending_on_filter_; | 159 ClientInfoMap clients_pending_on_filter_; |
| 154 ClientInfo clients_pending_on_restart_; | 160 ClientInfoMap clients_pending_on_restart_; |
| 155 | 161 |
| 156 // Member params_ represents the video format requested by the | 162 // Member params_ represents the video format requested by the |
| 157 // client to this class via StartCapture(). | 163 // client to this class via StartCapture(). |
| 158 media::VideoCaptureParams params_; | 164 media::VideoCaptureParams params_; |
| 159 | 165 |
| 160 // The device's video capture format sent from browser process side. | 166 // The device's video capture format sent from browser process side. |
| 161 media::VideoCaptureFormat last_frame_format_; | 167 media::VideoCaptureFormat last_frame_format_; |
| 162 | 168 |
| 163 // The device's first captured frame timestamp sent from browser process side. | 169 // The device's first captured frame timestamp sent from browser process side. |
| 164 base::TimeTicks first_frame_timestamp_; | 170 base::TimeTicks first_frame_timestamp_; |
| 165 | 171 |
| 166 bool suspended_; | 172 bool suspended_; |
| 167 VideoCaptureState state_; | 173 VideoCaptureState state_; |
| 168 | 174 |
| 175 // |weak_factory_| and |thread_checker_| are bound to the IO thread. |
| 176 base::ThreadChecker thread_checker_; |
| 177 |
| 169 // WeakPtrFactory pointing back to |this| object, for use with | 178 // WeakPtrFactory pointing back to |this| object, for use with |
| 170 // media::VideoFrames constructed in OnBufferReceived() from buffers cached | 179 // media::VideoFrames constructed in OnBufferReceived() from buffers cached |
| 171 // in |client_buffers_|. | 180 // in |client_buffers_|. |
| 172 // NOTE: Weak pointers must be invalidated before all other member variables. | 181 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 173 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_; | 182 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_; |
| 174 | 183 |
| 175 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); | 184 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); |
| 176 }; | 185 }; |
| 177 | 186 |
| 178 } // namespace content | 187 } // namespace content |
| 179 | 188 |
| 180 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 189 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
| OLD | NEW |