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: |
91 friend class VideoCaptureImplManager; | |
Ami GONE FROM CHROMIUM
2014/04/24 21:04:31
Non-test friends are rare in chromium so should ha
Alpha Left Google
2014/04/24 22:50:34
This is not needed any more. Forgot to remove.
| |
82 friend class VideoCaptureImplTest; | 92 friend class VideoCaptureImplTest; |
83 friend class MockVideoCaptureImpl; | 93 friend class MockVideoCaptureImpl; |
84 | 94 |
95 // Carries a shared memory for transferring video frames from browser to | |
96 // renderer. | |
85 class ClientBuffer; | 97 class ClientBuffer; |
86 typedef std::map<media::VideoCapture::EventHandler*, | |
87 media::VideoCaptureParams> ClientInfo; | |
88 | 98 |
89 void InitOnIOThread(); | 99 // Contains information for a video capture client. Including parameters |
90 void DeInitOnIOThread(base::Closure done_cb); | 100 // for capturing and callbacks to the client. |
91 void SuspendCaptureOnIOThread(bool suspend); | 101 struct ClientInfo { |
92 void StartCaptureOnIOThread( | 102 ClientInfo(); |
93 media::VideoCapture::EventHandler* handler, | 103 ~ClientInfo(); |
94 const media::VideoCaptureParams& params); | 104 media::VideoCaptureParams params; |
95 void StopCaptureOnIOThread(media::VideoCapture::EventHandler* handler); | 105 VideoCaptureStateUpdateCB state_update_cb; |
96 void GetDeviceSupportedFormatsOnIOThread( | 106 VideoCaptureDeliverFrameCB deliver_frame_cb; |
97 const DeviceFormatsCallback& callback); | 107 }; |
98 void GetDeviceFormatsInUseOnIOThread( | 108 typedef std::map<int, ClientInfo> ClientInfoMap; |
99 const DeviceFormatsInUseCallback& callback); | |
100 | 109 |
101 // VideoCaptureMessageFilter::Delegate interface. | 110 // VideoCaptureMessageFilter::Delegate interface. |
102 virtual void OnBufferCreated(base::SharedMemoryHandle handle, | 111 virtual void OnBufferCreated(base::SharedMemoryHandle handle, |
103 int length, | 112 int length, |
104 int buffer_id) OVERRIDE; | 113 int buffer_id) OVERRIDE; |
105 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE; | 114 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE; |
106 virtual void OnBufferReceived(int buffer_id, | 115 virtual void OnBufferReceived(int buffer_id, |
107 const media::VideoCaptureFormat& format, | 116 const media::VideoCaptureFormat& format, |
108 base::TimeTicks) OVERRIDE; | 117 base::TimeTicks) OVERRIDE; |
109 virtual void OnMailboxBufferReceived(int buffer_id, | 118 virtual void OnMailboxBufferReceived(int buffer_id, |
(...skipping 13 matching lines...) Expand all Loading... | |
123 const scoped_refptr<ClientBuffer>& buffer, | 132 const scoped_refptr<ClientBuffer>& buffer, |
124 scoped_ptr<gpu::MailboxHolder> mailbox_holder); | 133 scoped_ptr<gpu::MailboxHolder> mailbox_holder); |
125 | 134 |
126 void StopDevice(); | 135 void StopDevice(); |
127 void RestartCapture(); | 136 void RestartCapture(); |
128 void StartCaptureInternal(); | 137 void StartCaptureInternal(); |
129 | 138 |
130 virtual void Send(IPC::Message* message); | 139 virtual void Send(IPC::Message* message); |
131 | 140 |
132 // Helpers. | 141 // Helpers. |
133 bool RemoveClient(media::VideoCapture::EventHandler* handler, | 142 bool RemoveClient(int client_id, ClientInfoMap* clients); |
134 ClientInfo* clients); | |
135 | 143 |
136 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; | 144 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; |
137 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
138 int device_id_; | 145 int device_id_; |
139 const int session_id_; | 146 const int session_id_; |
140 | 147 |
141 // Vector of callbacks to be notified of device format enumerations, used only | 148 // Vector of callbacks to be notified of device format enumerations, used only |
142 // on IO Thread. | 149 // on IO Thread. |
143 std::vector<DeviceFormatsCallback> device_formats_callback_queue_; | 150 std::vector<VideoCaptureDeviceFormatsCB> device_formats_cb_queue_; |
144 // Vector of callbacks to be notified of a device's in use capture format(s), | 151 // Vector of callbacks to be notified of a device's in use capture format(s), |
145 // used only on IO Thread. | 152 // used only on IO Thread. |
146 std::vector<DeviceFormatsInUseCallback> device_formats_in_use_callback_queue_; | 153 std::vector<VideoCaptureDeviceFormatsCB> device_formats_in_use_cb_queue_; |
147 | 154 |
148 // Buffers available for sending to the client. | 155 // Buffers available for sending to the client. |
149 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; | 156 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; |
150 ClientBufferMap client_buffers_; | 157 ClientBufferMap client_buffers_; |
151 | 158 |
152 ClientInfo clients_; | 159 ClientInfoMap clients_; |
153 ClientInfo clients_pending_on_filter_; | 160 ClientInfoMap clients_pending_on_filter_; |
154 ClientInfo clients_pending_on_restart_; | 161 ClientInfoMap clients_pending_on_restart_; |
155 | 162 |
156 // Member params_ represents the video format requested by the | 163 // Member params_ represents the video format requested by the |
157 // client to this class via StartCapture(). | 164 // client to this class via StartCapture(). |
158 media::VideoCaptureParams params_; | 165 media::VideoCaptureParams params_; |
159 | 166 |
160 // The device's video capture format sent from browser process side. | 167 // The device's video capture format sent from browser process side. |
161 media::VideoCaptureFormat last_frame_format_; | 168 media::VideoCaptureFormat last_frame_format_; |
162 | 169 |
163 // The device's first captured frame timestamp sent from browser process side. | 170 // The device's first captured frame timestamp sent from browser process side. |
164 base::TimeTicks first_frame_timestamp_; | 171 base::TimeTicks first_frame_timestamp_; |
165 | 172 |
166 bool suspended_; | 173 bool suspended_; |
167 VideoCaptureState state_; | 174 VideoCaptureState state_; |
168 | 175 |
176 // |weak_factory_| and |thread_checker_| are bound to the IO thread. | |
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 |
184 base::ThreadChecker thread_checker_; | |
Ami GONE FROM CHROMIUM
2014/04/24 21:04:31
You misunderstood my comment; weak_factory_ should
Alpha Left Google
2014/04/24 22:50:34
Sure. I still like the ThreadChecker in this class
| |
185 | |
175 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); | 186 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); |
176 }; | 187 }; |
177 | 188 |
178 } // namespace content | 189 } // namespace content |
179 | 190 |
180 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 191 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
OLD | NEW |