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
3 // found in the LICENSE file.
5 // VideoCaptureManager is used to open/close, start/stop, enumerate available
6 // video capture devices, and manage VideoCaptureController's.
7 // All functions are expected to be called from Browser::IO thread. Some helper
8 // functions (*OnDeviceThread) will dispatch operations to the device thread.
9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice.
10 // A device can only be opened once.
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/process/process_handle.h"
23 #include "base/timer/elapsed_timer.h"
24 #include "content/browser/renderer_host/media/media_stream_provider.h"
25 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
26 #include "content/common/content_export.h"
27 #include "content/common/media/media_stream_options.h"
28 #include "media/video/capture/video_capture_device.h"
29 #include "media/video/capture/video_capture_device_factory.h"
30 #include "media/video/capture/video_capture_types.h"
33 class VideoCaptureController
;
34 class VideoCaptureControllerEventHandler
;
36 // VideoCaptureManager opens/closes and start/stops video capture devices.
37 class CONTENT_EXPORT VideoCaptureManager
: public MediaStreamProvider
{
39 // Callback used to signal the completion of a controller lookup.
40 typedef base::Callback
<
41 void(const base::WeakPtr
<VideoCaptureController
>&)> DoneCB
;
43 explicit VideoCaptureManager(
44 scoped_ptr
<media::VideoCaptureDeviceFactory
> factory
);
46 // Implements MediaStreamProvider.
47 virtual void Register(MediaStreamProviderListener
* listener
,
48 const scoped_refptr
<base::SingleThreadTaskRunner
>&
49 device_task_runner
) OVERRIDE
;
51 virtual void Unregister() OVERRIDE
;
53 virtual void EnumerateDevices(MediaStreamType stream_type
) OVERRIDE
;
55 virtual int Open(const StreamDeviceInfo
& device
) OVERRIDE
;
57 virtual void Close(int capture_session_id
) OVERRIDE
;
59 // Called by VideoCaptureHost to locate a capture device for |capture_params|,
60 // adding the Host as a client of the device's controller if successful. The
61 // value of |session_id| controls which device is selected;
62 // this value should be a session id previously returned by Open().
64 // If the device is not already started (i.e., no other client is currently
65 // capturing from this device), this call will cause a VideoCaptureController
66 // and VideoCaptureDevice to be created, possibly asynchronously.
68 // On success, the controller is returned via calling |done_cb|, indicating
69 // that the client was successfully added. A NULL controller is passed to
70 // the callback on failure.
71 void StartCaptureForClient(media::VideoCaptureSessionId session_id
,
72 const media::VideoCaptureParams
& capture_params
,
73 base::ProcessHandle client_render_process
,
74 VideoCaptureControllerID client_id
,
75 VideoCaptureControllerEventHandler
* client_handler
,
76 const DoneCB
& done_cb
);
78 // Called by VideoCaptureHost to remove |client_handler|. If this is the last
79 // client of the device, the |controller| and its VideoCaptureDevice may be
80 // destroyed. The client must not access |controller| after calling this
82 void StopCaptureForClient(VideoCaptureController
* controller
,
83 VideoCaptureControllerID client_id
,
84 VideoCaptureControllerEventHandler
* client_handler
,
85 bool aborted_due_to_error
);
87 // Retrieves all capture supported formats for a particular device. Returns
88 // false if the |capture_session_id| is not found. The supported formats are
89 // cached during device(s) enumeration, and depending on the underlying
90 // implementation, could be an empty list.
91 bool GetDeviceSupportedFormats(
92 media::VideoCaptureSessionId capture_session_id
,
93 media::VideoCaptureFormats
* supported_formats
);
95 // Retrieves the format(s) currently in use. Returns false if the
96 // |capture_session_id| is not found. Returns true and |formats_in_use|
97 // otherwise. |formats_in_use| is empty if the device is not in use.
98 bool GetDeviceFormatsInUse(media::VideoCaptureSessionId capture_session_id
,
99 media::VideoCaptureFormats
* formats_in_use
);
101 // Sets the platform-dependent window ID for the desktop capture notification
102 // UI for the given session.
103 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id
,
104 gfx::NativeViewId window_id
);
106 // Gets a weak reference to the device factory, used for tests.
107 media::VideoCaptureDeviceFactory
* video_capture_device_factory() const {
108 return video_capture_device_factory_
.get();
112 virtual ~VideoCaptureManager();
115 // This data structure is a convenient wrap of a devices' name and associated
116 // video capture supported formats.
119 DeviceInfo(const media::VideoCaptureDevice::Name
& name
,
120 const media::VideoCaptureFormats
& supported_formats
);
123 media::VideoCaptureDevice::Name name
;
124 media::VideoCaptureFormats supported_formats
;
126 typedef std::vector
<DeviceInfo
> DeviceInfos
;
128 // Checks to see if |entry| has no clients left on its controller. If so,
129 // remove it from the list of devices, and delete it asynchronously. |entry|
130 // may be freed by this function.
131 void DestroyDeviceEntryIfNoClients(DeviceEntry
* entry
);
133 // Helpers to report an event to our Listener.
134 void OnOpened(MediaStreamType type
,
135 media::VideoCaptureSessionId capture_session_id
);
136 void OnClosed(MediaStreamType type
,
137 media::VideoCaptureSessionId capture_session_id
);
138 void OnDevicesInfoEnumerated(MediaStreamType stream_type
,
139 base::ElapsedTimer
* timer
,
140 const DeviceInfos
& new_devices_info_cache
);
142 // Finds a DeviceEntry by its device ID and type, if it is already opened.
143 DeviceEntry
* GetDeviceEntryForMediaStreamDevice(
144 const MediaStreamDevice
& device_info
);
146 // Finds a DeviceEntry entry for the indicated session, creating a fresh one
147 // if necessary. Returns NULL if the session id is invalid.
148 DeviceEntry
* GetOrCreateDeviceEntry(
149 media::VideoCaptureSessionId capture_session_id
);
151 // Finds the DeviceEntry that owns a particular controller pointer.
152 DeviceEntry
* GetDeviceEntryForController(
153 const VideoCaptureController
* controller
) const;
155 bool IsOnDeviceThread() const;
157 // Consolidates the cached devices list with the list of currently connected
158 // devices in the system |names_snapshot|. Retrieves the supported formats of
159 // the new devices and sends the new cache to OnDevicesInfoEnumerated().
160 void ConsolidateDevicesInfoOnDeviceThread(
161 base::Callback
<void(const DeviceInfos
&)> on_devices_enumerated_callback
,
162 MediaStreamType stream_type
,
163 const DeviceInfos
& old_device_info_cache
,
164 scoped_ptr
<media::VideoCaptureDevice::Names
> names_snapshot
);
166 // Creates and Starts a new VideoCaptureDevice, storing the result in
167 // |entry->video_capture_device|. Ownership of |client| passes to
169 void DoStartDeviceOnDeviceThread(
170 media::VideoCaptureSessionId session_id
,
172 const media::VideoCaptureParams
& params
,
173 scoped_ptr
<media::VideoCaptureDevice::Client
> client
);
175 // Stops and destroys the VideoCaptureDevice held in
176 // |entry->video_capture_device|.
177 void DoStopDeviceOnDeviceThread(DeviceEntry
* entry
);
179 DeviceInfo
* FindDeviceInfoById(const std::string
& id
,
180 DeviceInfos
& device_vector
);
182 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry
* entry
,
183 gfx::NativeViewId window_id
);
185 void SaveDesktopCaptureWindowIdOnDeviceThread(
186 media::VideoCaptureSessionId session_id
,
187 gfx::NativeViewId window_id
);
189 // The message loop of media stream device thread, where VCD's live.
190 scoped_refptr
<base::SingleThreadTaskRunner
> device_task_runner_
;
192 // Only accessed on Browser::IO thread.
193 MediaStreamProviderListener
* listener_
;
194 media::VideoCaptureSessionId new_capture_session_id_
;
196 typedef std::map
<media::VideoCaptureSessionId
, MediaStreamDevice
> SessionMap
;
197 // An entry is kept in this map for every session that has been created via
198 // the Open() entry point. The keys are session_id's. This map is used to
199 // determine which device to use when StartCaptureForClient() occurs. Used
200 // only on the IO thread.
201 SessionMap sessions_
;
203 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated
204 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices
205 // and VideoCaptureControllers and is responsible for deleting the instances
206 // when they are not used any longer.
208 // The set of currently started VideoCaptureDevice and VideoCaptureController
209 // objects is only accessed from IO thread, though the DeviceEntry instances
210 // themselves may visit to the device thread for device creation and
213 DeviceEntry(MediaStreamType stream_type
,
214 const std::string
& id
,
215 scoped_ptr
<VideoCaptureController
> controller
);
218 const MediaStreamType stream_type
;
219 const std::string id
;
221 // The controller. Only used from the IO thread.
222 scoped_ptr
<VideoCaptureController
> video_capture_controller
;
224 // The capture device. Only used from the device thread.
225 scoped_ptr
<media::VideoCaptureDevice
> video_capture_device
;
227 typedef std::set
<DeviceEntry
*> DeviceEntries
;
228 DeviceEntries devices_
;
230 // Device creation factory injected on construction from MediaStreamManager or
231 // from the test harness.
232 scoped_ptr
<media::VideoCaptureDeviceFactory
> video_capture_device_factory_
;
234 // Local cache of the enumerated video capture devices' names and capture
235 // supported formats. A snapshot of the current devices and their capabilities
236 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and
237 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update
238 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will
239 // use this list if the device is not started, otherwise it will retrieve the
240 // active device capture format from the VideoCaptureController associated.
241 DeviceInfos devices_info_cache_
;
243 // Accessed on the device thread only.
244 std::map
<media::VideoCaptureSessionId
, gfx::NativeViewId
>
245 notification_window_ids_
;
247 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager
);
250 } // namespace content
252 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_