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" |
| 25 #include "base/threading/thread_checker.h" |
23 #include "base/timer/elapsed_timer.h" | 26 #include "base/timer/elapsed_timer.h" |
24 #include "content/browser/renderer_host/media/media_stream_provider.h" | 27 #include "content/browser/renderer_host/media/media_stream_provider.h" |
25 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 28 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
26 #include "content/common/content_export.h" | 29 #include "content/common/content_export.h" |
27 #include "content/common/media/media_stream_options.h" | 30 #include "content/common/media/media_stream_options.h" |
28 #include "media/video/capture/video_capture_device.h" | 31 #include "media/video/capture/video_capture_device.h" |
29 #include "media/video/capture/video_capture_device_factory.h" | 32 #include "media/video/capture/video_capture_device_factory.h" |
30 #include "media/video/capture/video_capture_device_info.h" | 33 #include "media/video/capture/video_capture_device_info.h" |
31 #include "media/video/capture/video_capture_types.h" | 34 #include "media/video/capture/video_capture_types.h" |
32 | 35 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 return video_capture_device_factory_.get(); | 134 return video_capture_device_factory_.get(); |
132 } | 135 } |
133 | 136 |
134 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
135 void set_device_task_runner( | 138 void set_device_task_runner( |
136 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { | 139 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { |
137 device_task_runner_ = device_task_runner; | 140 device_task_runner_ = device_task_runner; |
138 } | 141 } |
139 #endif | 142 #endif |
140 | 143 |
| 144 // Returns the SingleThreadTaskRunner where devices are enumerated on and |
| 145 // started. |
| 146 scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner() { |
| 147 return device_task_runner_; |
| 148 } |
141 private: | 149 private: |
142 ~VideoCaptureManager() override; | 150 ~VideoCaptureManager() override; |
143 struct DeviceEntry; | 151 class DeviceEntry; |
144 | 152 |
145 // Checks to see if |entry| has no clients left on its controller. If so, | 153 // Checks to see if |entry| has no clients left on its controller. If so, |
146 // remove it from the list of devices, and delete it asynchronously. |entry| | 154 // remove it from the list of devices, and delete it asynchronously. |entry| |
147 // may be freed by this function. | 155 // may be freed by this function. |
148 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 156 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
149 | 157 |
150 // Helpers to report an event to our Listener. | 158 // Helpers to report an event to our Listener. |
151 void OnOpened(MediaStreamType type, | 159 void OnOpened(MediaStreamType type, |
152 media::VideoCaptureSessionId capture_session_id); | 160 media::VideoCaptureSessionId capture_session_id); |
153 void OnClosed(MediaStreamType type, | 161 void OnClosed(MediaStreamType type, |
(...skipping 21 matching lines...) Expand all Loading... |
175 // Consolidates the cached devices list with the list of currently connected | 183 // Consolidates the cached devices list with the list of currently connected |
176 // devices in the system |names_snapshot|. Retrieves the supported formats of | 184 // devices in the system |names_snapshot|. Retrieves the supported formats of |
177 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 185 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
178 void ConsolidateDevicesInfoOnDeviceThread( | 186 void ConsolidateDevicesInfoOnDeviceThread( |
179 base::Callback<void(const media::VideoCaptureDeviceInfos&)> | 187 base::Callback<void(const media::VideoCaptureDeviceInfos&)> |
180 on_devices_enumerated_callback, | 188 on_devices_enumerated_callback, |
181 MediaStreamType stream_type, | 189 MediaStreamType stream_type, |
182 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 190 const media::VideoCaptureDeviceInfos& old_device_info_cache, |
183 scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot); | 191 scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot); |
184 | 192 |
185 // Creates and Starts a new VideoCaptureDevice, storing the result in | 193 // Starting a capture device can take 1-2 seconds. |
186 // |entry->video_capture_device|. Ownership of |client| passes to | 194 // To avoid multiple unnecessary start/stop commands to the OS, each start |
| 195 // request is queued in |device_start_queue_|. |
| 196 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
| 197 // request to start the device on the device thread unless there is |
| 198 // another request pending start. |
| 199 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
| 200 DeviceEntry* entry, |
| 201 const media::VideoCaptureParams& params); |
| 202 void OnDeviceStarted(int serial_id, |
| 203 scoped_ptr<media::VideoCaptureDevice> device); |
| 204 void DoStopDevice(DeviceEntry* entry); |
| 205 void HandleQueuedStartRequest(); |
| 206 |
| 207 // Creates and Starts a new VideoCaptureDevice. The resulting |
| 208 // VideoCaptureDevice is returned to the IO-thread and stored in |
| 209 // a DeviceEntry in |devices_|. Ownership of |client| passes to |
187 // the device. | 210 // the device. |
188 void DoStartDeviceOnDeviceThread( | 211 scoped_ptr<media::VideoCaptureDevice> DoStartDeviceOnDeviceThread( |
189 media::VideoCaptureSessionId session_id, | 212 media::VideoCaptureSessionId session_id, |
190 DeviceEntry* entry, | 213 const std::string& device_id, |
| 214 MediaStreamType stream_type, |
191 const media::VideoCaptureParams& params, | 215 const media::VideoCaptureParams& params, |
192 scoped_ptr<media::VideoCaptureDevice::Client> client); | 216 scoped_ptr<media::VideoCaptureDevice::Client> client); |
193 | 217 |
194 // Stops and destroys the VideoCaptureDevice held in | 218 // Stops and destroys the VideoCaptureDevice held in |
195 // |entry->video_capture_device|. | 219 // |device|. |
196 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); | 220 void DoStopDeviceOnDeviceThread(scoped_ptr<media::VideoCaptureDevice> device); |
197 | 221 |
198 media::VideoCaptureDeviceInfo* FindDeviceInfoById( | 222 media::VideoCaptureDeviceInfo* FindDeviceInfoById( |
199 const std::string& id, | 223 const std::string& id, |
200 media::VideoCaptureDeviceInfos& device_vector); | 224 media::VideoCaptureDeviceInfos& device_vector); |
201 | 225 |
202 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry, | 226 void SetDesktopCaptureWindowIdOnDeviceThread( |
203 gfx::NativeViewId window_id); | 227 media::VideoCaptureDevice* device, |
| 228 gfx::NativeViewId window_id); |
204 | 229 |
205 void SaveDesktopCaptureWindowIdOnDeviceThread( | 230 void SaveDesktopCaptureWindowIdOnDeviceThread( |
206 media::VideoCaptureSessionId session_id, | 231 media::VideoCaptureSessionId session_id, |
207 gfx::NativeViewId window_id); | 232 gfx::NativeViewId window_id); |
208 | 233 |
209 // The message loop of media stream device thread, where VCD's live. | 234 // The message loop of media stream device thread, where VCD's live. |
210 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; | 235 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; |
211 | 236 |
212 // Only accessed on Browser::IO thread. | 237 // Only accessed on Browser::IO thread. |
213 MediaStreamProviderListener* listener_; | 238 MediaStreamProviderListener* listener_; |
214 media::VideoCaptureSessionId new_capture_session_id_; | 239 media::VideoCaptureSessionId new_capture_session_id_; |
215 | 240 |
216 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap; | 241 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap; |
217 // An entry is kept in this map for every session that has been created via | 242 // An entry is kept in this map for every session that has been created via |
218 // the Open() entry point. The keys are session_id's. This map is used to | 243 // the Open() entry point. The keys are session_id's. This map is used to |
219 // determine which device to use when StartCaptureForClient() occurs. Used | 244 // determine which device to use when StartCaptureForClient() occurs. Used |
220 // only on the IO thread. | 245 // only on the IO thread. |
221 SessionMap sessions_; | 246 SessionMap sessions_; |
222 | 247 |
223 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated | 248 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated |
224 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices | 249 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices |
225 // and VideoCaptureControllers and is responsible for deleting the instances | 250 // and VideoCaptureControllers and is responsible for deleting the instances |
226 // when they are not used any longer. | 251 // when they are not used any longer. |
227 // | 252 // |
228 // The set of currently started VideoCaptureDevice and VideoCaptureController | 253 // The set of currently started VideoCaptureDevice and VideoCaptureController |
229 // objects is only accessed from IO thread, though the DeviceEntry instances | 254 // objects is only accessed from IO thread. |
230 // themselves may visit to the device thread for device creation and | 255 class DeviceEntry { |
231 // destruction. | 256 public: |
232 struct DeviceEntry { | |
233 DeviceEntry(MediaStreamType stream_type, | 257 DeviceEntry(MediaStreamType stream_type, |
234 const std::string& id, | 258 const std::string& id, |
235 scoped_ptr<VideoCaptureController> controller); | 259 scoped_ptr<VideoCaptureController> controller); |
236 ~DeviceEntry(); | 260 ~DeviceEntry(); |
237 | 261 |
| 262 const int serial_id; |
238 const MediaStreamType stream_type; | 263 const MediaStreamType stream_type; |
239 const std::string id; | 264 const std::string id; |
240 | 265 |
241 // The controller. Only used from the IO thread. | 266 VideoCaptureController* video_capture_controller(); |
242 scoped_ptr<VideoCaptureController> video_capture_controller; | 267 media::VideoCaptureDevice* video_capture_device(); |
243 | 268 |
244 // The capture device. Only used from the device thread. | 269 void SetVideoCaptureDevice(scoped_ptr<media::VideoCaptureDevice> device); |
245 scoped_ptr<media::VideoCaptureDevice> video_capture_device; | 270 scoped_ptr<media::VideoCaptureDevice> ReleaseVideoCaptureDevice(); |
| 271 |
| 272 private: |
| 273 // The controller. |
| 274 scoped_ptr<VideoCaptureController> video_capture_controller_; |
| 275 |
| 276 // The capture device. |
| 277 scoped_ptr<media::VideoCaptureDevice> video_capture_device_; |
| 278 |
| 279 base::ThreadChecker thread_checker_; |
246 }; | 280 }; |
247 typedef std::set<DeviceEntry*> DeviceEntries; | 281 |
| 282 typedef ScopedVector<DeviceEntry> DeviceEntries; |
| 283 // Currently opened devices. The device may or may not be started. |
248 DeviceEntries devices_; | 284 DeviceEntries devices_; |
249 | 285 |
| 286 // Class used for queuing request for starting a device. |
| 287 class CaptureDeviceStartRequest { |
| 288 public: |
| 289 CaptureDeviceStartRequest( |
| 290 int serial_id, |
| 291 media::VideoCaptureSessionId session_id, |
| 292 const media::VideoCaptureParams& params); |
| 293 int serial_id() const { return serial_id_;} |
| 294 media::VideoCaptureSessionId session_id() const { return session_id_; } |
| 295 media::VideoCaptureParams params() const { return params_; } |
| 296 |
| 297 // Set to true if the device should be stopped before it has successfully |
| 298 // been started. |
| 299 bool abort_start() const { return abort_start_; } |
| 300 void set_abort_start() { abort_start_ = true; } |
| 301 |
| 302 private: |
| 303 const int serial_id_; |
| 304 const media::VideoCaptureSessionId session_id_; |
| 305 const media::VideoCaptureParams params_; |
| 306 // Set to true if the device should be stopped before it has successfully |
| 307 // been started. |
| 308 bool abort_start_; |
| 309 }; |
| 310 |
| 311 typedef std::list<CaptureDeviceStartRequest> DeviceStartQueue; |
| 312 DeviceStartQueue device_start_queue_; |
| 313 |
250 // Device creation factory injected on construction from MediaStreamManager or | 314 // Device creation factory injected on construction from MediaStreamManager or |
251 // from the test harness. | 315 // from the test harness. |
252 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_; | 316 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_; |
253 | 317 |
254 // Local cache of the enumerated video capture devices' names and capture | 318 // Local cache of the enumerated video capture devices' names and capture |
255 // supported formats. A snapshot of the current devices and their capabilities | 319 // supported formats. A snapshot of the current devices and their capabilities |
256 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 320 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
257 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 321 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
258 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 322 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
259 // use this list if the device is not started, otherwise it will retrieve the | 323 // use this list if the device is not started, otherwise it will retrieve the |
260 // active device capture format from the VideoCaptureController associated. | 324 // active device capture format from the VideoCaptureController associated. |
261 media::VideoCaptureDeviceInfos devices_info_cache_; | 325 media::VideoCaptureDeviceInfos devices_info_cache_; |
262 | 326 |
263 // Accessed on the device thread only. | 327 // Accessed on the device thread only. |
264 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 328 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
265 notification_window_ids_; | 329 notification_window_ids_; |
266 | 330 |
267 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 331 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
268 }; | 332 }; |
269 | 333 |
270 } // namespace content | 334 } // namespace content |
271 | 335 |
272 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 336 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |