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 <list> |
16 #include <map> | 16 #include <map> |
17 #include <set> | 17 #include <set> |
18 #include <string> | 18 #include <string> |
19 | 19 |
20 #include "base/macros.h" | 20 #include "base/macros.h" |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/memory/scoped_vector.h" | |
23 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
24 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
25 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
26 #include "base/threading/thread_checker.h" | 25 #include "base/threading/thread_checker.h" |
27 #include "base/timer/elapsed_timer.h" | 26 #include "base/timer/elapsed_timer.h" |
28 #include "build/build_config.h" | 27 #include "build/build_config.h" |
29 #include "content/browser/renderer_host/media/media_stream_provider.h" | 28 #include "content/browser/renderer_host/media/media_stream_provider.h" |
30 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 29 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
31 #include "content/common/content_export.h" | 30 #include "content/common/content_export.h" |
32 #include "content/common/media/media_stream_options.h" | 31 #include "content/common/media/media_stream_options.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 media::ScopedResultCallback< | 166 media::ScopedResultCallback< |
168 VideoCaptureDevice::TakePhotoCallback> callback); | 167 VideoCaptureDevice::TakePhotoCallback> callback); |
169 #if defined(OS_ANDROID) | 168 #if defined(OS_ANDROID) |
170 // Some devices had troubles when stopped and restarted quickly, so the device | 169 // Some devices had troubles when stopped and restarted quickly, so the device |
171 // is only stopped when Chrome is sent to background and not when, e.g., a tab | 170 // is only stopped when Chrome is sent to background and not when, e.g., a tab |
172 // is hidden, see http://crbug.com/582295. | 171 // is hidden, see http://crbug.com/582295. |
173 void OnApplicationStateChange(base::android::ApplicationState state); | 172 void OnApplicationStateChange(base::android::ApplicationState state); |
174 #endif | 173 #endif |
175 | 174 |
176 private: | 175 private: |
| 176 class CaptureDeviceStartRequest; |
177 class DeviceEntry; | 177 class DeviceEntry; |
178 | 178 |
| 179 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
| 180 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; |
| 181 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
| 182 |
179 ~VideoCaptureManager() override; | 183 ~VideoCaptureManager() override; |
180 | 184 |
181 // Checks to see if |entry| has no clients left on its controller. If so, | |
182 // remove it from the list of devices, and delete it asynchronously. |entry| | |
183 // may be freed by this function. | |
184 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | |
185 | |
186 // Helpers to report an event to our Listener. | 185 // Helpers to report an event to our Listener. |
187 void OnOpened(MediaStreamType type, | 186 void OnOpened(MediaStreamType type, |
188 media::VideoCaptureSessionId capture_session_id); | 187 media::VideoCaptureSessionId capture_session_id); |
189 void OnClosed(MediaStreamType type, | 188 void OnClosed(MediaStreamType type, |
190 media::VideoCaptureSessionId capture_session_id); | 189 media::VideoCaptureSessionId capture_session_id); |
191 void OnDevicesInfoEnumerated( | 190 void OnDevicesInfoEnumerated( |
192 MediaStreamType stream_type, | 191 MediaStreamType stream_type, |
193 base::ElapsedTimer* timer, | 192 base::ElapsedTimer* timer, |
194 const media::VideoCaptureDeviceInfos& new_devices_info_cache); | 193 const media::VideoCaptureDeviceInfos& new_devices_info_cache); |
195 | 194 |
196 // Finds a DeviceEntry by its device ID and type, if it is already opened. | |
197 DeviceEntry* GetDeviceEntryForMediaStreamDevice( | |
198 const MediaStreamDevice& device_info); | |
199 | |
200 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr | |
201 // if not found. | |
202 VideoCaptureDevice* GetVideoCaptureDeviceFromSessionId(int session_id); | |
203 | |
204 // Finds a DeviceEntry entry for the indicated session, creating a fresh one | |
205 // if necessary. Returns NULL if the session id is invalid. | |
206 DeviceEntry* GetOrCreateDeviceEntry( | |
207 media::VideoCaptureSessionId capture_session_id, | |
208 const media::VideoCaptureParams& params); | |
209 | |
210 // Finds the DeviceEntry that owns a particular controller pointer. | |
211 DeviceEntry* GetDeviceEntryForController( | |
212 const VideoCaptureController* controller) const; | |
213 | |
214 bool IsOnDeviceThread() const; | 195 bool IsOnDeviceThread() const; |
215 | 196 |
216 // Consolidates the cached devices list with the list of currently connected | 197 // Consolidates the cached devices list with the list of currently connected |
217 // devices in the system |names_snapshot|. Retrieves the supported formats of | 198 // devices in the system |names_snapshot|. Retrieves the supported formats of |
218 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 199 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
219 void ConsolidateDevicesInfoOnDeviceThread( | 200 void ConsolidateDevicesInfoOnDeviceThread( |
220 base::Callback<void(const media::VideoCaptureDeviceInfos&)> | 201 base::Callback<void(const media::VideoCaptureDeviceInfos&)> |
221 on_devices_enumerated_callback, | 202 on_devices_enumerated_callback, |
222 MediaStreamType stream_type, | 203 MediaStreamType stream_type, |
223 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 204 const media::VideoCaptureDeviceInfos& old_device_info_cache, |
224 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); | 205 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); |
225 | 206 |
| 207 // Checks to see if |entry| has no clients left on its controller. If so, |
| 208 // remove it from the list of devices, and delete it asynchronously. |entry| |
| 209 // may be freed by this function. |
| 210 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
| 211 |
| 212 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr |
| 213 // if not found. |
| 214 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); |
| 215 |
| 216 // Finds a DeviceEntry in different ways: by its |device_id| and |type| (if it |
| 217 // is already opened), by its |controller| or by its |serial_id|. In all |
| 218 // cases, if not found, nullptr is returned. |
| 219 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, |
| 220 const std::string& device_id) const; |
| 221 DeviceEntry* GetDeviceEntryByController( |
| 222 const VideoCaptureController* controller) const; |
| 223 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; |
| 224 |
| 225 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
| 226 media::VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); |
| 227 |
| 228 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating |
| 229 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is |
| 230 // invalid. |
| 231 DeviceEntry* GetOrCreateDeviceEntry( |
| 232 media::VideoCaptureSessionId capture_session_id, |
| 233 const media::VideoCaptureParams& params); |
| 234 |
226 // Starting a capture device can take 1-2 seconds. | 235 // Starting a capture device can take 1-2 seconds. |
227 // To avoid multiple unnecessary start/stop commands to the OS, each start | 236 // To avoid multiple unnecessary start/stop commands to the OS, each start |
228 // request is queued in |device_start_queue_|. | 237 // request is queued in |device_start_queue_|. |
229 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 238 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
230 // request to start the device on the device thread unless there is | 239 // request to start the device on the device thread unless there is |
231 // another request pending start. | 240 // another request pending start. |
232 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 241 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
233 DeviceEntry* entry, | 242 DeviceEntry* entry, |
234 const media::VideoCaptureParams& params); | 243 const media::VideoCaptureParams& params); |
235 void OnDeviceStarted(int serial_id, | 244 void OnDeviceStarted(int serial_id, |
(...skipping 16 matching lines...) Expand all Loading... |
252 std::unique_ptr<VideoCaptureDevice::Client> client); | 261 std::unique_ptr<VideoCaptureDevice::Client> client); |
253 | 262 |
254 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( | 263 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( |
255 const std::string& device_id, | 264 const std::string& device_id, |
256 const media::VideoCaptureParams& params, | 265 const media::VideoCaptureParams& params, |
257 std::unique_ptr<VideoCaptureDevice::Client> client); | 266 std::unique_ptr<VideoCaptureDevice::Client> client); |
258 | 267 |
259 // Stops and destroys the VideoCaptureDevice held in |device|. | 268 // Stops and destroys the VideoCaptureDevice held in |device|. |
260 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device); | 269 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device); |
261 | 270 |
262 media::VideoCaptureDeviceInfo* FindDeviceInfoById( | |
263 const std::string& id, | |
264 media::VideoCaptureDeviceInfos& device_vector); | |
265 | |
266 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); | 271 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); |
267 void SetDesktopCaptureWindowIdOnDeviceThread( | 272 void SetDesktopCaptureWindowIdOnDeviceThread( |
268 media::VideoCaptureDevice* device, | 273 media::VideoCaptureDevice* device, |
269 gfx::NativeViewId window_id); | 274 gfx::NativeViewId window_id); |
270 | 275 |
271 #if defined(OS_MACOSX) | 276 #if defined(OS_MACOSX) |
272 // Called on the IO thread after the device layer has been initialized on Mac. | 277 // Called on the IO thread after the device layer has been initialized on Mac. |
273 // Sets |capture_device_api_initialized_| to true and then executes and_then. | 278 // Sets |capture_device_api_initialized_| to true and then executes and_then. |
274 void OnDeviceLayerInitialized(const base::Closure& and_then); | 279 void OnDeviceLayerInitialized(const base::Closure& and_then); |
275 | 280 |
(...skipping 25 matching lines...) Expand all Loading... |
301 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; | 306 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; |
302 | 307 |
303 // Only accessed on Browser::IO thread. | 308 // Only accessed on Browser::IO thread. |
304 MediaStreamProviderListener* listener_; | 309 MediaStreamProviderListener* listener_; |
305 media::VideoCaptureSessionId new_capture_session_id_; | 310 media::VideoCaptureSessionId new_capture_session_id_; |
306 | 311 |
307 // An entry is kept in this map for every session that has been created via | 312 // An entry is kept in this map for every session that has been created via |
308 // the Open() entry point. The keys are session_id's. This map is used to | 313 // the Open() entry point. The keys are session_id's. This map is used to |
309 // determine which device to use when StartCaptureForClient() occurs. Used | 314 // determine which device to use when StartCaptureForClient() occurs. Used |
310 // only on the IO thread. | 315 // only on the IO thread. |
311 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap; | |
312 SessionMap sessions_; | 316 SessionMap sessions_; |
313 | 317 |
314 // Currently opened devices. The device may or may not be started. This member | 318 // Currently opened DeviceEntry instances (each owning a VideoCaptureDevice - |
315 // is only accessed on IO tbhread. | 319 // VideoCaptureController pair). The device may or may not be started. This |
316 typedef ScopedVector<DeviceEntry> DeviceEntries; | 320 // member is only accessed on IO thread. |
317 DeviceEntries devices_; | 321 DeviceEntries devices_; |
318 | 322 |
319 class CaptureDeviceStartRequest; | |
320 typedef std::list<CaptureDeviceStartRequest> DeviceStartQueue; | |
321 DeviceStartQueue device_start_queue_; | 323 DeviceStartQueue device_start_queue_; |
322 | 324 |
323 // Device creation factory injected on construction from MediaStreamManager or | 325 // Device creation factory injected on construction from MediaStreamManager or |
324 // from the test harness. | 326 // from the test harness. |
325 std::unique_ptr<media::VideoCaptureDeviceFactory> | 327 std::unique_ptr<media::VideoCaptureDeviceFactory> |
326 video_capture_device_factory_; | 328 video_capture_device_factory_; |
327 | 329 |
328 // Local cache of the enumerated video capture devices' names and capture | 330 // Local cache of the enumerated video capture devices' names and capture |
329 // supported formats. A snapshot of the current devices and their capabilities | 331 // supported formats. A snapshot of the current devices and their capabilities |
330 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 332 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
331 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 333 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
332 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 334 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
333 // use this list if the device is not started, otherwise it will retrieve the | 335 // use this list if the device is not started, otherwise it will retrieve the |
334 // active device capture format from the VideoCaptureController associated. | 336 // active device capture format from the VideoCaptureController associated. |
335 media::VideoCaptureDeviceInfos devices_info_cache_; | 337 media::VideoCaptureDeviceInfos devices_info_cache_; |
336 | 338 |
337 // Map used by DesktopCapture. | 339 // Map used by DesktopCapture. |
338 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 340 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
339 notification_window_ids_; | 341 notification_window_ids_; |
340 | 342 |
341 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 343 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
342 }; | 344 }; |
343 | 345 |
344 } // namespace content | 346 } // namespace content |
345 | 347 |
346 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 348 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |