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 // VideoCaptureManager is used to open/close, start/stop, enumerate available | 5 // VideoCaptureManager is used to open/close, start/stop, enumerate available |
6 // video capture devices, and manage VideoCaptureController's. | 6 // video capture devices, and manage VideoCaptureController's. |
7 // All functions are expected to be called from Browser::IO thread. Some helper | 7 // All functions are expected to be called from Browser::IO thread. Some helper |
8 // functions (*OnDeviceThread) will dispatch operations to the device thread. | 8 // functions (*OnDeviceThread) will dispatch operations to the device thread. |
9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. | 9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. |
10 // A device can only be opened once. | 10 // A device can only be opened once. |
11 | 11 |
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
14 | 14 |
15 #include <map> | 15 #include <map> |
16 #include <set> | 16 #include <set> |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/memory/weak_ptr.h" | |
20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
21 #include "content/browser/renderer_host/media/media_stream_provider.h" | 22 #include "content/browser/renderer_host/media/media_stream_provider.h" |
22 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 23 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
23 #include "content/common/content_export.h" | 24 #include "content/common/content_export.h" |
24 #include "content/common/media/media_stream_options.h" | 25 #include "content/common/media/media_stream_options.h" |
25 #include "media/video/capture/video_capture_device.h" | 26 #include "media/video/capture/video_capture_device.h" |
26 #include "media/video/capture/video_capture_types.h" | 27 #include "media/video/capture/video_capture_types.h" |
27 | 28 |
28 namespace content { | 29 namespace content { |
29 class MockVideoCaptureManager; | 30 class MockVideoCaptureManager; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // and VideoCaptureDevice to be created, possibly asynchronously. | 68 // and VideoCaptureDevice to be created, possibly asynchronously. |
68 // | 69 // |
69 // On success, the controller is returned via calling |done_cb|, indicating | 70 // On success, the controller is returned via calling |done_cb|, indicating |
70 // that the client was successfully added. A NULL controller is passed to | 71 // that the client was successfully added. A NULL controller is passed to |
71 // the callback on failure. | 72 // the callback on failure. |
72 void StartCaptureForClient( | 73 void StartCaptureForClient( |
73 const media::VideoCaptureParams& capture_params, | 74 const media::VideoCaptureParams& capture_params, |
74 base::ProcessHandle client_render_process, | 75 base::ProcessHandle client_render_process, |
75 VideoCaptureControllerID client_id, | 76 VideoCaptureControllerID client_id, |
76 VideoCaptureControllerEventHandler* client_handler, | 77 VideoCaptureControllerEventHandler* client_handler, |
77 base::Callback<void(VideoCaptureController*)> done_cb); | 78 base::Callback<void(base::WeakPtr<VideoCaptureController>)> done_cb); |
78 | 79 |
79 // Called by VideoCaptureHost to remove |client_handler|. If this is the last | 80 // Called by VideoCaptureHost to remove |client_handler|. If this is the last |
80 // client of the device, the |controller| and its VideoCaptureDevice may be | 81 // client of the device, the |controller| and its VideoCaptureDevice may be |
81 // destroyed. The client must not access |controller| after calling this | 82 // destroyed. The client must not access |controller| after calling this |
82 // function. | 83 // function. |
83 void StopCaptureForClient(VideoCaptureController* controller, | 84 void StopCaptureForClient(VideoCaptureController* controller, |
84 VideoCaptureControllerID client_id, | 85 VideoCaptureControllerID client_id, |
85 VideoCaptureControllerEventHandler* client_handler); | 86 VideoCaptureControllerEventHandler* client_handler); |
86 | 87 |
87 private: | 88 private: |
88 virtual ~VideoCaptureManager(); | 89 virtual ~VideoCaptureManager(); |
89 struct DeviceEntry; | 90 struct DeviceEntry; |
90 | 91 |
91 // Helper for the kStartOpenSessionId case. | 92 // Helper for the kStartOpenSessionId case. |
92 void OpenAndStartDefaultSession( | 93 void OpenAndStartDefaultSession( |
93 const media::VideoCaptureParams& capture_params, | 94 const media::VideoCaptureParams& capture_params, |
94 base::ProcessHandle client_render_process, | 95 base::ProcessHandle client_render_process, |
95 VideoCaptureControllerID client_id, | 96 VideoCaptureControllerID client_id, |
96 VideoCaptureControllerEventHandler* client_handler, | 97 VideoCaptureControllerEventHandler* client_handler, |
97 base::Callback<void(VideoCaptureController*)> done_cb, | 98 base::Callback<void(base::WeakPtr<VideoCaptureController>)> done_cb, |
98 const media::VideoCaptureDevice::Names& device_names); | 99 const media::VideoCaptureDevice::Names& device_names); |
99 | 100 |
100 // Helper routine implementing StartCaptureForClient(). | 101 // Helper routine implementing StartCaptureForClient(). |
101 void DoStartCaptureForClient( | 102 void DoStartCaptureForClient( |
102 const media::VideoCaptureParams& capture_params, | 103 const media::VideoCaptureParams& capture_params, |
103 base::ProcessHandle client_render_process, | 104 base::ProcessHandle client_render_process, |
104 VideoCaptureControllerID client_id, | 105 VideoCaptureControllerID client_id, |
105 VideoCaptureControllerEventHandler* client_handler, | 106 VideoCaptureControllerEventHandler* client_handler, |
106 base::Callback<void(VideoCaptureController*)> done_cb); | 107 base::Callback<void(base::WeakPtr<VideoCaptureController>)> done_cb); |
107 | 108 |
108 // Check to see if |entry| has no clients left on its controller. If so, | 109 // Check to see if |entry| has no clients left on its controller. If so, |
109 // remove it from the list of devices, and delete it asynchronously. |entry| | 110 // remove it from the list of devices, and delete it asynchronously. |entry| |
110 // may be freed by this function. | 111 // may be freed by this function. |
111 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 112 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
112 | 113 |
113 // Helpers to report an event to our Listener. | 114 // Helpers to report an event to our Listener. |
114 void OnOpened(MediaStreamType type, int capture_session_id); | 115 void OnOpened(MediaStreamType type, int capture_session_id); |
115 void OnClosed(MediaStreamType type, int capture_session_id); | 116 void OnClosed(MediaStreamType type, int capture_session_id); |
116 void OnDevicesEnumerated(MediaStreamType stream_type, | 117 void OnDevicesEnumerated(MediaStreamType stream_type, |
(...skipping 11 matching lines...) Expand all Loading... | |
128 DeviceEntry* GetDeviceEntryForController( | 129 DeviceEntry* GetDeviceEntryForController( |
129 const VideoCaptureController* controller); | 130 const VideoCaptureController* controller); |
130 | 131 |
131 bool IsOnDeviceThread() const; | 132 bool IsOnDeviceThread() const; |
132 | 133 |
133 // Queries and returns the available device IDs. | 134 // Queries and returns the available device IDs. |
134 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( | 135 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( |
135 MediaStreamType stream_type); | 136 MediaStreamType stream_type); |
136 | 137 |
137 // Create and Start a new VideoCaptureDevice, storing the result in | 138 // Create and Start a new VideoCaptureDevice, storing the result in |
138 // |entry->video_capture_device|. | 139 // |entry->video_capture_device|. Ownership of |handler| passes to |
140 // the device. | |
Ami GONE FROM CHROMIUM
2013/09/13 21:17:59
s/device/device pointed to by |entry|/ ?
ncarter (slow)
2013/09/14 00:07:24
I meant the device mentioned in the previous sente
| |
139 void DoStartDeviceOnDeviceThread( | 141 void DoStartDeviceOnDeviceThread( |
140 DeviceEntry* entry, | 142 DeviceEntry* entry, |
141 const media::VideoCaptureCapability& capture_params, | 143 const media::VideoCaptureCapability& capture_params, |
142 media::VideoCaptureDevice::EventHandler* controller_as_handler); | 144 scoped_ptr<media::VideoCaptureDevice::EventHandler> handler); |
Ami GONE FROM CHROMIUM
2013/09/13 21:17:59
Amen.
ncarter (slow)
2013/09/14 00:07:24
Done.
| |
143 | 145 |
144 // Stop and destroy the VideoCaptureDevice held in | 146 // Stop and destroy the VideoCaptureDevice held in |
145 // |entry->video_capture_device|. | 147 // |entry->video_capture_device|. |
146 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); | 148 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); |
147 | 149 |
148 // Helper to clean up the DeviceEntry* instance, and the | |
149 // VideoCaptureController, on the IO thread. Must happen after | |
150 // DoStopDeviceOnDeviceThread() destroys the VideoCaptureDevice. It is assumed | |
151 // that |dead_device| has already been removed from the |devices_| map. | |
152 void FreeDeviceEntryOnIOThread(scoped_ptr<DeviceEntry> dead_device); | |
153 | |
154 // The message loop of media stream device thread, where VCD's live. | 150 // The message loop of media stream device thread, where VCD's live. |
155 scoped_refptr<base::MessageLoopProxy> device_loop_; | 151 scoped_refptr<base::MessageLoopProxy> device_loop_; |
156 | 152 |
157 // Only accessed on Browser::IO thread. | 153 // Only accessed on Browser::IO thread. |
158 MediaStreamProviderListener* listener_; | 154 MediaStreamProviderListener* listener_; |
159 int new_capture_session_id_; | 155 int new_capture_session_id_; |
160 | 156 |
161 // An entry is kept in this map for every session that has been created via | 157 // An entry is kept in this map for every session that has been created via |
162 // the Open() entry point. The keys are session_id's. This map is used to | 158 // the Open() entry point. The keys are session_id's. This map is used to |
163 // determine which device to use when StartCaptureForClient() occurs. Used | 159 // determine which device to use when StartCaptureForClient() occurs. Used |
164 // only on the IO thread. | 160 // only on the IO thread. |
165 std::map<int, MediaStreamDevice> sessions_; | 161 std::map<int, MediaStreamDevice> sessions_; |
166 | 162 |
167 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated | 163 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated |
168 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices | 164 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices |
169 // and VideoCaptureControllers and is responsible for deleting the instances | 165 // and VideoCaptureControllers and is responsible for deleting the instances |
170 // when they are not used any longer. | 166 // when they are not used any longer. |
171 // | 167 // |
172 // The set of currently started VideoCaptureDevice and VideoCaptureController | 168 // The set of currently started VideoCaptureDevice and VideoCaptureController |
173 // objects is only accessed from IO thread, though the DeviceEntry instances | 169 // objects is only accessed from IO thread, though the DeviceEntry instances |
174 // themselves may visit to the device thread for device creation and | 170 // themselves may visit to the device thread for device creation and |
175 // destruction. | 171 // destruction. |
176 struct DeviceEntry { | 172 struct DeviceEntry { |
177 DeviceEntry(MediaStreamType stream_type, | 173 DeviceEntry(MediaStreamType stream_type, |
178 const std::string& id, | 174 const std::string& id, |
179 scoped_refptr<VideoCaptureController> controller); | 175 scoped_ptr<VideoCaptureController> controller); |
180 ~DeviceEntry(); | 176 ~DeviceEntry(); |
181 | 177 |
182 const MediaStreamType stream_type; | 178 const MediaStreamType stream_type; |
183 const std::string id; | 179 const std::string id; |
184 | 180 |
185 // The controller. Only used from the IO thread. | 181 // The controller. Only used from the IO thread. |
186 scoped_refptr<VideoCaptureController> video_capture_controller; | 182 scoped_ptr<VideoCaptureController> video_capture_controller; |
187 | 183 |
188 // The capture device. Only used from the device thread. | 184 // The capture device. Only used from the device thread. |
189 scoped_ptr<media::VideoCaptureDevice> video_capture_device; | 185 scoped_ptr<media::VideoCaptureDevice> video_capture_device; |
190 }; | 186 }; |
191 typedef std::set<DeviceEntry*> DeviceEntries; | 187 typedef std::set<DeviceEntry*> DeviceEntries; |
192 DeviceEntries devices_; | 188 DeviceEntries devices_; |
193 | 189 |
194 // Set to true if using fake video capture devices for testing, false by | 190 // Set to true if using fake video capture devices for testing, false by |
195 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. | 191 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. |
196 bool use_fake_device_; | 192 bool use_fake_device_; |
197 | 193 |
198 // We cache the enumerated video capture devices in | 194 // We cache the enumerated video capture devices in |
199 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID | 195 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID |
200 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the | 196 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the |
201 // device thread. | 197 // device thread. |
202 media::VideoCaptureDevice::Names video_capture_devices_; | 198 media::VideoCaptureDevice::Names video_capture_devices_; |
203 | 199 |
204 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 200 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
205 }; | 201 }; |
206 | 202 |
207 } // namespace content | 203 } // namespace content |
208 | 204 |
209 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 205 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |