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