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 <list> |
15 #include <map> | 16 #include <map> |
16 #include <set> | 17 #include <set> |
17 #include <string> | 18 #include <string> |
18 | 19 |
19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/scoped_vector.h" |
20 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
21 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
22 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
23 #include "base/timer/elapsed_timer.h" | 25 #include "base/timer/elapsed_timer.h" |
24 #include "content/browser/renderer_host/media/media_stream_provider.h" | 26 #include "content/browser/renderer_host/media/media_stream_provider.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" |
26 #include "content/common/content_export.h" | 28 #include "content/common/content_export.h" |
27 #include "content/common/media/media_stream_options.h" | 29 #include "content/common/media/media_stream_options.h" |
28 #include "media/video/capture/video_capture_device.h" | 30 #include "media/video/capture/video_capture_device.h" |
29 #include "media/video/capture/video_capture_device_factory.h" | 31 #include "media/video/capture/video_capture_device_factory.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // Consolidates the cached devices list with the list of currently connected | 177 // Consolidates the cached devices list with the list of currently connected |
176 // devices in the system |names_snapshot|. Retrieves the supported formats of | 178 // devices in the system |names_snapshot|. Retrieves the supported formats of |
177 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 179 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
178 void ConsolidateDevicesInfoOnDeviceThread( | 180 void ConsolidateDevicesInfoOnDeviceThread( |
179 base::Callback<void(const media::VideoCaptureDeviceInfos&)> | 181 base::Callback<void(const media::VideoCaptureDeviceInfos&)> |
180 on_devices_enumerated_callback, | 182 on_devices_enumerated_callback, |
181 MediaStreamType stream_type, | 183 MediaStreamType stream_type, |
182 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 184 const media::VideoCaptureDeviceInfos& old_device_info_cache, |
183 scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot); | 185 scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot); |
184 | 186 |
| 187 // On the OS level, starting a capture device can take 1-2 seconds. |
| 188 // To avoid multiple unnecessary start/stop commands to the OS, each start |
| 189 // request is queued in |device_start_queue_|. |
| 190 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
| 191 // request to start the device with |id| on the device thread unless there is |
| 192 // another request pending start. |
| 193 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
| 194 DeviceEntry* entry, |
| 195 const media::VideoCaptureParams& params); |
| 196 void OnDeviceStarted(DeviceEntry* device_entry, |
| 197 scoped_ptr<media::VideoCaptureDevice> device); |
| 198 void DoStopDevice(DeviceEntry* entry); |
| 199 void HandleQueuedStartRequest(); |
| 200 |
185 // Creates and Starts a new VideoCaptureDevice, storing the result in | 201 // Creates and Starts a new VideoCaptureDevice, storing the result in |
186 // |entry->video_capture_device|. Ownership of |client| passes to | 202 // |entry->video_capture_device|. Ownership of |client| passes to |
187 // the device. | 203 // the device. |
188 void DoStartDeviceOnDeviceThread( | 204 scoped_ptr<media::VideoCaptureDevice> DoStartDeviceOnDeviceThread( |
189 media::VideoCaptureSessionId session_id, | 205 media::VideoCaptureSessionId session_id, |
190 DeviceEntry* entry, | 206 const std::string& device_id, |
| 207 MediaStreamType stream_type, |
191 const media::VideoCaptureParams& params, | 208 const media::VideoCaptureParams& params, |
192 scoped_ptr<media::VideoCaptureDevice::Client> client); | 209 scoped_ptr<media::VideoCaptureDevice::Client> client); |
193 | 210 |
194 // Stops and destroys the VideoCaptureDevice held in | 211 // Stops and destroys the VideoCaptureDevice held in |
195 // |entry->video_capture_device|. | 212 // |entry->video_capture_device|. |
196 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); | 213 void DoStopDeviceOnDeviceThread(scoped_ptr<media::VideoCaptureDevice> device); |
197 | 214 |
198 media::VideoCaptureDeviceInfo* FindDeviceInfoById( | 215 media::VideoCaptureDeviceInfo* FindDeviceInfoById( |
199 const std::string& id, | 216 const std::string& id, |
200 media::VideoCaptureDeviceInfos& device_vector); | 217 media::VideoCaptureDeviceInfos& device_vector); |
201 | 218 |
202 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry, | 219 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry, |
203 gfx::NativeViewId window_id); | 220 gfx::NativeViewId window_id); |
204 | 221 |
205 void SaveDesktopCaptureWindowIdOnDeviceThread( | 222 void SaveDesktopCaptureWindowIdOnDeviceThread( |
206 media::VideoCaptureSessionId session_id, | 223 media::VideoCaptureSessionId session_id, |
(...skipping 12 matching lines...) Expand all Loading... |
219 // determine which device to use when StartCaptureForClient() occurs. Used | 236 // determine which device to use when StartCaptureForClient() occurs. Used |
220 // only on the IO thread. | 237 // only on the IO thread. |
221 SessionMap sessions_; | 238 SessionMap sessions_; |
222 | 239 |
223 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated | 240 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated |
224 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices | 241 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices |
225 // and VideoCaptureControllers and is responsible for deleting the instances | 242 // and VideoCaptureControllers and is responsible for deleting the instances |
226 // when they are not used any longer. | 243 // when they are not used any longer. |
227 // | 244 // |
228 // The set of currently started VideoCaptureDevice and VideoCaptureController | 245 // The set of currently started VideoCaptureDevice and VideoCaptureController |
229 // objects is only accessed from IO thread, though the DeviceEntry instances | 246 // objects is only accessed from IO thread. |
230 // themselves may visit to the device thread for device creation and | |
231 // destruction. | |
232 struct DeviceEntry { | 247 struct DeviceEntry { |
233 DeviceEntry(MediaStreamType stream_type, | 248 DeviceEntry(MediaStreamType stream_type, |
234 const std::string& id, | 249 const std::string& id, |
235 scoped_ptr<VideoCaptureController> controller); | 250 scoped_ptr<VideoCaptureController> controller); |
236 ~DeviceEntry(); | 251 ~DeviceEntry(); |
237 | 252 |
238 const MediaStreamType stream_type; | 253 const MediaStreamType stream_type; |
239 const std::string id; | 254 const std::string id; |
240 | 255 |
241 // The controller. Only used from the IO thread. | 256 // The controller. Only used from the IO thread. |
242 scoped_ptr<VideoCaptureController> video_capture_controller; | 257 scoped_ptr<VideoCaptureController> video_capture_controller; |
243 | 258 |
244 // The capture device. Only used from the device thread. | 259 // The capture device. Only used from the device thread. |
245 scoped_ptr<media::VideoCaptureDevice> video_capture_device; | 260 scoped_ptr<media::VideoCaptureDevice> video_capture_device; |
246 }; | 261 }; |
247 typedef std::set<DeviceEntry*> DeviceEntries; | 262 typedef ScopedVector<DeviceEntry> DeviceEntries; |
| 263 // Currently opened devices. The device may or may not be started. |
248 DeviceEntries devices_; | 264 DeviceEntries devices_; |
249 | 265 |
| 266 // On the OS level, starting a capture device can take 1-2 seconds. |
| 267 // To avoid multiple unnecessary start/stop commands to the OS, each start |
| 268 // request is queued in a CaptureDeviceStartRequest. |
| 269 struct CaptureDeviceStartRequest { |
| 270 CaptureDeviceStartRequest( |
| 271 DeviceEntry* entry, |
| 272 media::VideoCaptureSessionId session_id, |
| 273 const media::VideoCaptureParams& params); |
| 274 |
| 275 DeviceEntry* entry; // Owned by |devices_| |
| 276 media::VideoCaptureSessionId session_id; |
| 277 media::VideoCaptureParams params; |
| 278 }; |
| 279 |
| 280 typedef std::list<CaptureDeviceStartRequest> DeviceStartQueue; |
| 281 DeviceStartQueue device_start_queue_; |
| 282 |
250 // Device creation factory injected on construction from MediaStreamManager or | 283 // Device creation factory injected on construction from MediaStreamManager or |
251 // from the test harness. | 284 // from the test harness. |
252 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_; | 285 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_; |
253 | 286 |
254 // Local cache of the enumerated video capture devices' names and capture | 287 // Local cache of the enumerated video capture devices' names and capture |
255 // supported formats. A snapshot of the current devices and their capabilities | 288 // supported formats. A snapshot of the current devices and their capabilities |
256 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 289 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
257 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 290 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
258 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 291 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
259 // use this list if the device is not started, otherwise it will retrieve the | 292 // use this list if the device is not started, otherwise it will retrieve the |
260 // active device capture format from the VideoCaptureController associated. | 293 // active device capture format from the VideoCaptureController associated. |
261 media::VideoCaptureDeviceInfos devices_info_cache_; | 294 media::VideoCaptureDeviceInfos devices_info_cache_; |
262 | 295 |
263 // Accessed on the device thread only. | 296 // Accessed on the device thread only. |
264 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 297 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
265 notification_window_ids_; | 298 notification_window_ids_; |
266 | 299 |
267 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 300 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
268 }; | 301 }; |
269 | 302 |
270 } // namespace content | 303 } // namespace content |
271 | 304 |
272 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 305 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |