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/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
23 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
24 #include "base/observer_list.h" | 24 #include "base/observer_list.h" |
25 #include "base/process/process_handle.h" | 25 #include "base/process/process_handle.h" |
26 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
27 #include "base/timer/elapsed_timer.h" | 27 #include "base/timer/elapsed_timer.h" |
28 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 29 #include "content/browser/renderer_host/media/buildable_video_capture_device.h" |
29 #include "content/browser/renderer_host/media/media_stream_provider.h" | 30 #include "content/browser/renderer_host/media/media_stream_provider.h" |
30 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 31 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
31 #include "content/common/content_export.h" | 32 #include "content/common/content_export.h" |
32 #include "content/common/media/media_stream_options.h" | 33 #include "content/common/media/media_stream_options.h" |
33 #include "media/base/video_facing.h" | 34 #include "media/base/video_facing.h" |
34 #include "media/capture/video/video_capture_device.h" | 35 #include "media/capture/video/video_capture_device.h" |
35 #include "media/capture/video/video_capture_device_factory.h" | 36 #include "media/capture/video/video_capture_device_factory.h" |
36 #include "media/capture/video_capture_types.h" | 37 #include "media/capture/video_capture_types.h" |
37 | 38 |
38 #if defined(OS_ANDROID) | 39 #if defined(OS_ANDROID) |
39 #include "base/android/application_status_listener.h" | 40 #include "base/android/application_status_listener.h" |
40 #endif | 41 #endif |
41 | 42 |
42 namespace content { | 43 namespace content { |
43 class VideoCaptureController; | 44 class VideoCaptureController; |
44 class VideoCaptureControllerEventHandler; | 45 class VideoCaptureControllerEventHandler; |
| 46 class VideoCaptureDeviceEntry; |
45 | 47 |
46 // VideoCaptureManager opens/closes and start/stops video capture devices. | 48 // VideoCaptureManager opens/closes and start/stops video capture devices. |
47 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { | 49 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider, |
| 50 public BuildableDeviceCallbacks { |
48 public: | 51 public: |
49 using VideoCaptureDevice = media::VideoCaptureDevice; | 52 using VideoCaptureDevice = media::VideoCaptureDevice; |
50 | 53 |
51 // Callback used to signal the completion of a controller lookup. | 54 // Callback used to signal the completion of a controller lookup. |
52 using DoneCB = | 55 using DoneCB = |
53 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; | 56 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; |
54 | 57 |
55 VideoCaptureManager( | 58 VideoCaptureManager( |
56 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, | 59 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, |
57 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); | 60 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // Some devices had troubles when stopped and restarted quickly, so the device | 192 // Some devices had troubles when stopped and restarted quickly, so the device |
190 // is only stopped when Chrome is sent to background and not when, e.g., a tab | 193 // is only stopped when Chrome is sent to background and not when, e.g., a tab |
191 // is hidden, see http://crbug.com/582295. | 194 // is hidden, see http://crbug.com/582295. |
192 void OnApplicationStateChange(base::android::ApplicationState state); | 195 void OnApplicationStateChange(base::android::ApplicationState state); |
193 #endif | 196 #endif |
194 | 197 |
195 using EnumerationCallback = | 198 using EnumerationCallback = |
196 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; | 199 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; |
197 void EnumerateDevices(const EnumerationCallback& client_callback); | 200 void EnumerateDevices(const EnumerationCallback& client_callback); |
198 | 201 |
| 202 // BuildableDeviceCallbacks implementation: |
| 203 bool LookupDeviceDescriptor( |
| 204 const std::string& id, |
| 205 media::VideoCaptureDeviceDescriptor* descriptor) override; |
| 206 void OnDeviceAboutToStart(media::VideoFacingMode facing_mode) override; |
| 207 void OnDeviceStarted(VideoCaptureDeviceEntry* entry) override; |
| 208 void OnDeviceStartFailed(VideoCaptureDeviceEntry* entry) override; |
| 209 |
199 // Retrieves camera calibration information for a particular device. Returns | 210 // Retrieves camera calibration information for a particular device. Returns |
200 // nullopt_t if the |device_id| is not found or camera calibration information | 211 // nullopt_t if the |device_id| is not found or camera calibration information |
201 // is not available for the device. Camera calibration is cached during | 212 // is not available for the device. Camera calibration is cached during |
202 // device(s) enumeration. | 213 // device(s) enumeration. |
203 base::Optional<CameraCalibration> GetCameraCalibration( | 214 base::Optional<CameraCalibration> GetCameraCalibration( |
204 const std::string& device_id); | 215 const std::string& device_id); |
205 | 216 |
206 private: | 217 private: |
207 class CaptureDeviceStartRequest; | 218 class CaptureDeviceStartRequest; |
208 struct DeviceEntry; | |
209 struct DeviceInfo; | 219 struct DeviceInfo; |
210 | 220 |
211 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; | 221 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
212 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; | 222 using DeviceEntries = std::vector<scoped_refptr<VideoCaptureDeviceEntry>>; |
213 using DeviceInfos = std::vector<DeviceInfo>; | 223 using DeviceInfos = std::vector<DeviceInfo>; |
214 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; | 224 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
215 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; | 225 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; |
216 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; | 226 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; |
217 | 227 |
218 ~VideoCaptureManager() override; | 228 ~VideoCaptureManager() override; |
219 | 229 |
220 // Helpers to report an event to our Listener. | 230 // Helpers to report an event to our Listener. |
221 void OnOpened(MediaStreamType type, | 231 void OnOpened(MediaStreamType type, |
222 media::VideoCaptureSessionId capture_session_id); | 232 media::VideoCaptureSessionId capture_session_id); |
223 void OnClosed(MediaStreamType type, | 233 void OnClosed(MediaStreamType type, |
224 media::VideoCaptureSessionId capture_session_id); | 234 media::VideoCaptureSessionId capture_session_id); |
225 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, | 235 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, |
226 const EnumerationCallback& client_callback, | 236 const EnumerationCallback& client_callback, |
227 const DeviceInfos& new_devices_info_cache); | 237 const DeviceInfos& new_devices_info_cache); |
228 | 238 |
229 bool IsOnDeviceThread() const; | 239 bool IsOnDeviceThread() const; |
230 | 240 |
231 // Consolidates the cached devices list with the list of currently connected | 241 // Consolidates the cached devices list with the list of currently connected |
232 // devices in the system |names_snapshot|. Retrieves the supported formats of | 242 // devices in the system |names_snapshot|. Retrieves the supported formats of |
233 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 243 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
234 void ConsolidateDevicesInfoOnDeviceThread( | 244 void ConsolidateDevicesInfoOnDeviceThread( |
235 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, | 245 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, |
236 const DeviceInfos& old_device_info_cache, | 246 const DeviceInfos& old_device_info_cache, |
237 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); | 247 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); |
238 | 248 |
239 // Checks to see if |entry| has no clients left on its controller. If so, | 249 // Checks to see if |entry| has no clients left on its controller. If so, |
240 // remove it from the list of devices, and delete it asynchronously. |entry| | 250 // remove it from the list of devices, and delete it asynchronously. |entry| |
241 // may be freed by this function. | 251 // may be freed by this function. |
242 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 252 void DestroyDeviceEntryIfNoClients(VideoCaptureDeviceEntry* entry); |
243 | 253 |
244 // Finds a DeviceEntry in different ways: by |session_id|, by its |device_id| | 254 // Finds a VideoCaptureDeviceEntry in different ways: by |session_id|, by its |
245 // and |type| (if it is already opened), by its |controller| or by its | 255 // |device_id| and |type| (if it is already opened), by its |controller| or by |
246 // |serial_id|. In all cases, if not found, nullptr is returned. | 256 // its |serial_id|. In all cases, if not found, nullptr is returned. |
247 DeviceEntry* GetDeviceEntryBySessionId(int session_id); | 257 VideoCaptureDeviceEntry* GetDeviceEntryBySessionId(int session_id); |
248 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, | 258 VideoCaptureDeviceEntry* GetDeviceEntryByTypeAndId( |
249 const std::string& device_id) const; | 259 MediaStreamType type, |
250 DeviceEntry* GetDeviceEntryByController( | 260 const std::string& device_id) const; |
| 261 VideoCaptureDeviceEntry* GetDeviceEntryByController( |
251 const VideoCaptureController* controller) const; | 262 const VideoCaptureController* controller) const; |
252 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; | 263 VideoCaptureDeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; |
| 264 scoped_refptr<VideoCaptureDeviceEntry> GetDeviceEntrySharedRefBySerialId( |
| 265 int serial_id) const; |
253 | 266 |
254 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | 267 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
255 DeviceInfo* GetDeviceInfoById(const std::string& id); | 268 DeviceInfo* GetDeviceInfoById(const std::string& id); |
256 | 269 |
257 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating | 270 // Finds a VideoCaptureDeviceEntry entry for the indicated |
| 271 // |capture_session_id|, creating |
258 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is | 272 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is |
259 // invalid. | 273 // invalid. |
260 DeviceEntry* GetOrCreateDeviceEntry( | 274 VideoCaptureDeviceEntry* GetOrCreateDeviceEntry( |
261 media::VideoCaptureSessionId capture_session_id, | 275 media::VideoCaptureSessionId capture_session_id, |
262 const media::VideoCaptureParams& params); | 276 const media::VideoCaptureParams& params); |
263 | 277 |
264 // Starting a capture device can take 1-2 seconds. | 278 // Starting a capture device can take 1-2 seconds. |
265 // To avoid multiple unnecessary start/stop commands to the OS, each start | 279 // To avoid multiple unnecessary start/stop commands to the OS, each start |
266 // request is queued in |device_start_queue_|. | 280 // request is queued in |device_start_queue_|. |
267 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 281 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
268 // request to start the device on the device thread unless there is | 282 // request to start the device on the device thread unless there is |
269 // another request pending start. | 283 // another request pending start. |
270 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 284 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
271 DeviceEntry* entry, | 285 VideoCaptureDeviceEntry* entry, |
272 const media::VideoCaptureParams& params); | 286 const media::VideoCaptureParams& params); |
273 void OnDeviceStarted( | 287 void DoStopDevice(VideoCaptureDeviceEntry* entry); |
274 int serial_id, | |
275 std::unique_ptr<VideoCaptureDevice> device); | |
276 void DoStopDevice(DeviceEntry* entry); | |
277 void HandleQueuedStartRequest(); | 288 void HandleQueuedStartRequest(); |
278 | 289 |
279 // Creates and Starts a new VideoCaptureDevice. The resulting | |
280 // VideoCaptureDevice is returned to the IO-thread and stored in | |
281 // a DeviceEntry in |devices_|. Ownership of |client| passes to | |
282 // the device. | |
283 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread( | |
284 const VideoCaptureDeviceDescriptor& descriptor, | |
285 const media::VideoCaptureParams& params, | |
286 std::unique_ptr<VideoCaptureDevice::Client> client); | |
287 | |
288 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread( | |
289 const std::string& device_id, | |
290 const media::VideoCaptureParams& params, | |
291 std::unique_ptr<VideoCaptureDevice::Client> client); | |
292 | |
293 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( | |
294 const std::string& device_id, | |
295 const media::VideoCaptureParams& params, | |
296 std::unique_ptr<VideoCaptureDevice::Client> client); | |
297 | |
298 // Stops and destroys the VideoCaptureDevice held in |device|. | |
299 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device); | |
300 | |
301 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); | 290 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); |
302 void SetDesktopCaptureWindowIdOnDeviceThread( | |
303 media::VideoCaptureDevice* device, | |
304 gfx::NativeViewId window_id); | |
305 | |
306 // Internal versions of the Image Capture public ones, for delayed execution. | |
307 void DoGetPhotoCapabilities( | |
308 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback, | |
309 VideoCaptureDevice* device); | |
310 void DoSetPhotoOptions( | |
311 VideoCaptureDevice::SetPhotoOptionsCallback callback, | |
312 media::mojom::PhotoSettingsPtr settings, | |
313 VideoCaptureDevice* device); | |
314 void DoTakePhoto(VideoCaptureDevice::TakePhotoCallback callback, | |
315 VideoCaptureDevice* device); | |
316 | 291 |
317 #if defined(OS_ANDROID) | 292 #if defined(OS_ANDROID) |
318 void ReleaseDevices(); | 293 void ReleaseDevices(); |
319 void ResumeDevices(); | 294 void ResumeDevices(); |
320 | 295 |
321 std::unique_ptr<base::android::ApplicationStatusListener> | 296 std::unique_ptr<base::android::ApplicationStatusListener> |
322 app_status_listener_; | 297 app_status_listener_; |
323 bool application_state_has_running_activities_; | 298 bool application_state_has_running_activities_; |
324 #endif | 299 #endif |
325 | 300 |
326 // The message loop of media stream device thread, where VCD's live. | 301 // The message loop of media stream device thread, where VCD's live. |
327 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; | 302 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; |
328 | 303 |
329 // Only accessed on Browser::IO thread. | 304 // Only accessed on Browser::IO thread. |
330 base::ObserverList<MediaStreamProviderListener> listeners_; | 305 base::ObserverList<MediaStreamProviderListener> listeners_; |
331 media::VideoCaptureSessionId new_capture_session_id_; | 306 media::VideoCaptureSessionId new_capture_session_id_; |
332 | 307 |
333 // An entry is kept in this map for every session that has been created via | 308 // An entry is kept in this map for every session that has been created via |
334 // the Open() entry point. The keys are session_id's. This map is used to | 309 // the Open() entry point. The keys are session_id's. This map is used to |
335 // determine which device to use when StartCaptureForClient() occurs. Used | 310 // determine which device to use when StartCaptureForClient() occurs. Used |
336 // only on the IO thread. | 311 // only on the IO thread. |
337 SessionMap sessions_; | 312 SessionMap sessions_; |
338 | 313 |
339 // Currently opened DeviceEntry instances (each owning a VideoCaptureDevice - | 314 // Currently opened VideoCaptureDeviceEntry instances. The device may or may |
340 // VideoCaptureController pair). The device may or may not be started. This | 315 // not be started. This member is only accessed on IO thread. |
341 // member is only accessed on IO thread. | |
342 DeviceEntries devices_; | 316 DeviceEntries devices_; |
343 | 317 |
344 DeviceStartQueue device_start_queue_; | 318 DeviceStartQueue device_start_queue_; |
345 | 319 |
346 // Queue to keep photo-associated requests waiting for a device to initialize, | 320 // Queue to keep photo-associated requests waiting for a device to initialize, |
347 // bundles a session id integer and an associated photo-related request. | 321 // bundles a session id integer and an associated photo-related request. |
348 std::list<std::pair<int, base::Callback<void(media::VideoCaptureDevice*)>>> | 322 std::list<std::pair<int, base::Closure>> photo_request_queue_; |
349 photo_request_queue_; | |
350 | 323 |
351 // Device creation factory injected on construction from MediaStreamManager or | 324 // Device creation factory injected on construction from MediaStreamManager or |
352 // from the test harness. | 325 // from the test harness. |
353 std::unique_ptr<media::VideoCaptureDeviceFactory> | 326 std::unique_ptr<media::VideoCaptureDeviceFactory> |
354 video_capture_device_factory_; | 327 video_capture_device_factory_; |
355 | 328 |
356 base::ObserverList<media::VideoCaptureObserver> capture_observers_; | 329 base::ObserverList<media::VideoCaptureObserver> capture_observers_; |
357 | 330 |
358 // Local cache of the enumerated video capture devices' names and capture | 331 // Local cache of the enumerated video capture devices' names and capture |
359 // supported formats. A snapshot of the current devices and their capabilities | 332 // supported formats. A snapshot of the current devices and their capabilities |
360 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 333 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
361 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 334 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
362 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 335 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
363 // use this list if the device is not started, otherwise it will retrieve the | 336 // use this list if the device is not started, otherwise it will retrieve the |
364 // active device capture format from the VideoCaptureController associated. | 337 // active device capture format from the VideoCaptureController associated. |
365 DeviceInfos devices_info_cache_; | 338 DeviceInfos devices_info_cache_; |
366 | 339 |
367 // Map used by DesktopCapture. | 340 // Map used by DesktopCapture. |
368 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 341 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
369 notification_window_ids_; | 342 notification_window_ids_; |
370 | 343 |
371 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 344 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
372 }; | 345 }; |
373 | 346 |
374 } // namespace content | 347 } // namespace content |
375 | 348 |
376 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 349 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |