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; |
45 | 46 |
46 // VideoCaptureManager opens/closes and start/stops video capture devices. | 47 // VideoCaptureManager opens/closes and start/stops video capture devices. |
47 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { | 48 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider, |
49 public BuildableDeviceCallbacks { | |
48 public: | 50 public: |
49 using VideoCaptureDevice = media::VideoCaptureDevice; | 51 using VideoCaptureDevice = media::VideoCaptureDevice; |
50 | 52 |
51 // Callback used to signal the completion of a controller lookup. | 53 // Callback used to signal the completion of a controller lookup. |
52 using DoneCB = | 54 using DoneCB = |
53 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; | 55 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; |
54 | 56 |
55 VideoCaptureManager( | 57 VideoCaptureManager( |
56 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, | 58 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, |
57 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); | 59 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 | 191 // 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 | 192 // is only stopped when Chrome is sent to background and not when, e.g., a tab |
191 // is hidden, see http://crbug.com/582295. | 193 // is hidden, see http://crbug.com/582295. |
192 void OnApplicationStateChange(base::android::ApplicationState state); | 194 void OnApplicationStateChange(base::android::ApplicationState state); |
193 #endif | 195 #endif |
194 | 196 |
195 using EnumerationCallback = | 197 using EnumerationCallback = |
196 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; | 198 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; |
197 void EnumerateDevices(const EnumerationCallback& client_callback); | 199 void EnumerateDevices(const EnumerationCallback& client_callback); |
198 | 200 |
201 // BuildableDeviceCallbacks implementation: | |
202 const media::VideoCaptureDeviceDescriptor* LookupDeviceDescriptor( | |
203 const std::string& id) override; | |
204 void WillStartDevice(media::VideoFacingMode facing_mode) override; | |
205 void DidStartDevice(VideoCaptureController* controller) override; | |
206 void OnDeviceStartFailed(VideoCaptureController* controller) override; | |
207 | |
199 // Retrieves camera calibration information for a particular device. Returns | 208 // Retrieves camera calibration information for a particular device. Returns |
200 // nullopt_t if the |device_id| is not found or camera calibration information | 209 // 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 | 210 // is not available for the device. Camera calibration is cached during |
202 // device(s) enumeration. | 211 // device(s) enumeration. |
203 base::Optional<CameraCalibration> GetCameraCalibration( | 212 base::Optional<CameraCalibration> GetCameraCalibration( |
204 const std::string& device_id); | 213 const std::string& device_id); |
205 | 214 |
206 private: | 215 private: |
207 class CaptureDeviceStartRequest; | 216 class CaptureDeviceStartRequest; |
208 struct DeviceEntry; | |
209 struct DeviceInfo; | 217 struct DeviceInfo; |
210 | 218 |
211 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; | 219 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
212 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; | |
213 using DeviceInfos = std::vector<DeviceInfo>; | 220 using DeviceInfos = std::vector<DeviceInfo>; |
214 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; | 221 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
215 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; | 222 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; |
216 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; | 223 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; |
217 | 224 |
218 ~VideoCaptureManager() override; | 225 ~VideoCaptureManager() override; |
219 | 226 |
220 // Helpers to report an event to our Listener. | 227 // Helpers to report an event to our Listener. |
221 void OnOpened(MediaStreamType type, | 228 void OnOpened(MediaStreamType type, |
222 media::VideoCaptureSessionId capture_session_id); | 229 media::VideoCaptureSessionId capture_session_id); |
223 void OnClosed(MediaStreamType type, | 230 void OnClosed(MediaStreamType type, |
224 media::VideoCaptureSessionId capture_session_id); | 231 media::VideoCaptureSessionId capture_session_id); |
225 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, | 232 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, |
226 const EnumerationCallback& client_callback, | 233 const EnumerationCallback& client_callback, |
227 const DeviceInfos& new_devices_info_cache); | 234 const DeviceInfos& new_devices_info_cache); |
228 | 235 |
229 bool IsOnDeviceThread() const; | |
230 | |
231 // Consolidates the cached devices list with the list of currently connected | 236 // Consolidates the cached devices list with the list of currently connected |
232 // devices in the system |names_snapshot|. Retrieves the supported formats of | 237 // devices in the system |names_snapshot|. Retrieves the supported formats of |
233 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 238 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
234 void ConsolidateDevicesInfoOnDeviceThread( | 239 void ConsolidateDevicesInfoOnDeviceThread( |
235 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, | 240 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, |
236 const DeviceInfos& old_device_info_cache, | 241 const DeviceInfos& old_device_info_cache, |
237 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); | 242 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); |
238 | 243 |
239 // Checks to see if |entry| has no clients left on its controller. If so, | 244 // Checks to see if |controller| has no clients left on its controller. If so, |
240 // remove it from the list of devices, and delete it asynchronously. |entry| | 245 // remove it from the list of devices, and delete it asynchronously. |
241 // may be freed by this function. | 246 // |controller| may be freed by this function. |
242 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 247 void DestroyDeviceEntryIfNoClients(VideoCaptureController* controller); |
miu
2017/03/16 00:01:36
Looks like you might have wanted to also rename th
chfremer
2017/03/16 17:39:39
Done.
| |
243 | 248 |
244 // Finds a DeviceEntry in different ways: by |session_id|, by its |device_id| | 249 // Finds a VideoCaptureController in different ways: by |session_id|, by its |
245 // and |type| (if it is already opened), by its |controller| or by its | 250 // |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. | 251 // its |serial_id|. In all cases, if not found, nullptr is returned. |
247 DeviceEntry* GetDeviceEntryBySessionId(int session_id); | 252 VideoCaptureController* LookupControllerBySessionId(int session_id); |
248 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, | 253 VideoCaptureController* LookupControllerByTypeAndId( |
249 const std::string& device_id) const; | 254 MediaStreamType type, |
250 DeviceEntry* GetDeviceEntryByController( | 255 const std::string& device_id) const; |
251 const VideoCaptureController* controller) const; | 256 bool IsControllerPointerValid(const VideoCaptureController* controller) const; |
252 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; | 257 VideoCaptureController* LookupControllerBySerialId(int serial_id) const; |
258 scoped_refptr<VideoCaptureController> GetControllerSharedRefFromSerialId( | |
259 int serial_id) const; | |
253 | 260 |
254 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | 261 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
255 DeviceInfo* GetDeviceInfoById(const std::string& id); | 262 DeviceInfo* GetDeviceInfoById(const std::string& id); |
256 | 263 |
257 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating | 264 // Finds a VideoCaptureController for the indicated |capture_session_id|, |
258 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is | 265 // creating a fresh one if necessary. Returns nullptr if said |
259 // invalid. | 266 // |capture_session_id| is invalid. |
260 DeviceEntry* GetOrCreateDeviceEntry( | 267 VideoCaptureController* GetOrCreateController( |
261 media::VideoCaptureSessionId capture_session_id, | 268 media::VideoCaptureSessionId capture_session_id, |
262 const media::VideoCaptureParams& params); | 269 const media::VideoCaptureParams& params); |
263 | 270 |
264 // Starting a capture device can take 1-2 seconds. | 271 // Starting a capture device can take 1-2 seconds. |
265 // To avoid multiple unnecessary start/stop commands to the OS, each start | 272 // To avoid multiple unnecessary start/stop commands to the OS, each start |
266 // request is queued in |device_start_queue_|. | 273 // request is queued in |device_start_queue_|. |
267 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 274 // 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 | 275 // request to start the device on the device thread unless there is |
269 // another request pending start. | 276 // another request pending start. |
270 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 277 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
271 DeviceEntry* entry, | 278 VideoCaptureController* controller, |
272 const media::VideoCaptureParams& params); | 279 const media::VideoCaptureParams& params); |
273 void OnDeviceStarted( | 280 void DoStopDevice(VideoCaptureController* controller); |
274 int serial_id, | |
275 std::unique_ptr<VideoCaptureDevice> device); | |
276 void DoStopDevice(DeviceEntry* entry); | |
277 void HandleQueuedStartRequest(); | 281 void HandleQueuedStartRequest(); |
278 | 282 |
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); | 283 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 | 284 |
317 #if defined(OS_ANDROID) | 285 #if defined(OS_ANDROID) |
318 void ReleaseDevices(); | 286 void ReleaseDevices(); |
319 void ResumeDevices(); | 287 void ResumeDevices(); |
320 | 288 |
321 std::unique_ptr<base::android::ApplicationStatusListener> | 289 std::unique_ptr<base::android::ApplicationStatusListener> |
322 app_status_listener_; | 290 app_status_listener_; |
323 bool application_state_has_running_activities_; | 291 bool application_state_has_running_activities_; |
324 #endif | 292 #endif |
325 | 293 |
326 // The message loop of media stream device thread, where VCD's live. | 294 // The message loop of media stream device thread, where VCD's live. |
327 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; | 295 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; |
328 | 296 |
329 // Only accessed on Browser::IO thread. | 297 // Only accessed on Browser::IO thread. |
330 base::ObserverList<MediaStreamProviderListener> listeners_; | 298 base::ObserverList<MediaStreamProviderListener> listeners_; |
331 media::VideoCaptureSessionId new_capture_session_id_; | 299 media::VideoCaptureSessionId new_capture_session_id_; |
332 | 300 |
333 // An entry is kept in this map for every session that has been created via | 301 // 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 | 302 // 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 | 303 // determine which device to use when StartCaptureForClient() occurs. Used |
336 // only on the IO thread. | 304 // only on the IO thread. |
337 SessionMap sessions_; | 305 SessionMap sessions_; |
338 | 306 |
339 // Currently opened DeviceEntry instances (each owning a VideoCaptureDevice - | 307 // Currently opened VideoCaptureController instances. The device may or may |
340 // VideoCaptureController pair). The device may or may not be started. This | 308 // not be started. This member is only accessed on IO thread. |
341 // member is only accessed on IO thread. | 309 std::vector<scoped_refptr<VideoCaptureController>> controllers_; |
342 DeviceEntries devices_; | |
343 | 310 |
344 DeviceStartQueue device_start_queue_; | 311 DeviceStartQueue device_start_queue_; |
345 | 312 |
346 // Queue to keep photo-associated requests waiting for a device to initialize, | 313 // Queue to keep photo-associated requests waiting for a device to initialize, |
347 // bundles a session id integer and an associated photo-related request. | 314 // bundles a session id integer and an associated photo-related request. |
348 std::list<std::pair<int, base::Callback<void(media::VideoCaptureDevice*)>>> | 315 std::list<std::pair<int, base::Closure>> photo_request_queue_; |
349 photo_request_queue_; | |
350 | 316 |
351 // Device creation factory injected on construction from MediaStreamManager or | 317 // Device creation factory injected on construction from MediaStreamManager or |
352 // from the test harness. | 318 // from the test harness. |
353 std::unique_ptr<media::VideoCaptureDeviceFactory> | 319 std::unique_ptr<media::VideoCaptureDeviceFactory> |
354 video_capture_device_factory_; | 320 video_capture_device_factory_; |
355 | 321 |
356 base::ObserverList<media::VideoCaptureObserver> capture_observers_; | 322 base::ObserverList<media::VideoCaptureObserver> capture_observers_; |
357 | 323 |
358 // Local cache of the enumerated video capture devices' names and capture | 324 // Local cache of the enumerated video capture devices' names and capture |
359 // supported formats. A snapshot of the current devices and their capabilities | 325 // supported formats. A snapshot of the current devices and their capabilities |
360 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 326 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
361 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 327 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
362 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 328 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
363 // use this list if the device is not started, otherwise it will retrieve the | 329 // use this list if the device is not started, otherwise it will retrieve the |
364 // active device capture format from the VideoCaptureController associated. | 330 // active device capture format from the VideoCaptureController associated. |
365 DeviceInfos devices_info_cache_; | 331 DeviceInfos devices_info_cache_; |
366 | 332 |
367 // Map used by DesktopCapture. | 333 // Map used by DesktopCapture. |
368 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 334 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
369 notification_window_ids_; | 335 notification_window_ids_; |
370 | 336 |
371 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 337 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
372 }; | 338 }; |
373 | 339 |
374 } // namespace content | 340 } // namespace content |
375 | 341 |
376 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 342 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |