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_device_info.h"
31 #include "media/video/capture/video_capture_types.h"
34 class VideoCaptureController
;
35 class VideoCaptureControllerEventHandler
;
37 // VideoCaptureManager opens/closes and start/stops video capture devices.
38 class CONTENT_EXPORT VideoCaptureManager
: public MediaStreamProvider
{
40 // Callback used to signal the completion of a controller lookup.
41 typedef base::Callback
<
42 void(const base::WeakPtr
<VideoCaptureController
>&)> DoneCB
;
44 explicit VideoCaptureManager(
45 scoped_ptr
<media::VideoCaptureDeviceFactory
> factory
);
47 // Implements MediaStreamProvider.
48 void Register(MediaStreamProviderListener
* listener
,
49 const scoped_refptr
<base::SingleThreadTaskRunner
>&
50 device_task_runner
) override
;
52 void Unregister() override
;
54 void EnumerateDevices(MediaStreamType stream_type
) override
;
56 int Open(const StreamDeviceInfo
& device
) override
;
58 void Close(int capture_session_id
) override
;
60 // Called by VideoCaptureHost to locate a capture device for |capture_params|,
61 // adding the Host as a client of the device's controller if successful. The
62 // value of |session_id| controls which device is selected;
63 // this value should be a session id previously returned by Open().
65 // If the device is not already started (i.e., no other client is currently
66 // capturing from this device), this call will cause a VideoCaptureController
67 // and VideoCaptureDevice to be created, possibly asynchronously.
69 // On success, the controller is returned via calling |done_cb|, indicating
70 // that the client was successfully added. A NULL controller is passed to
71 // the callback on failure.
72 void StartCaptureForClient(media::VideoCaptureSessionId session_id
,
73 const media::VideoCaptureParams
& capture_params
,
74 base::ProcessHandle client_render_process
,
75 VideoCaptureControllerID client_id
,
76 VideoCaptureControllerEventHandler
* client_handler
,
77 const DoneCB
& done_cb
);
79 // Called by VideoCaptureHost to remove |client_handler|. If this is the last
80 // client of the device, the |controller| and its VideoCaptureDevice may be
81 // destroyed. The client must not access |controller| after calling this
83 void StopCaptureForClient(VideoCaptureController
* controller
,
84 VideoCaptureControllerID client_id
,
85 VideoCaptureControllerEventHandler
* client_handler
,
86 bool aborted_due_to_error
);
88 // Called by VideoCaptureHost to pause to update video buffer specified by
89 // |client_id| and |client_handler|. If all clients of |controller| are
90 // paused, the corresponding device will be closed.
91 void PauseCaptureForClient(
92 VideoCaptureController
* controller
,
93 VideoCaptureControllerID client_id
,
94 VideoCaptureControllerEventHandler
* client_handler
);
96 // Called by VideoCaptureHost to resume to update video buffer specified by
97 // |client_id| and |client_handler|. The |session_id| and |params| should be
98 // same as those used in StartCaptureForClient().
99 // If this is first active client of |controller|, device will be allocated
100 // and it will take a little time to resume.
101 // Allocating device could failed if other app holds the camera, the error
102 // will be notified through VideoCaptureControllerEventHandler::OnError().
103 void ResumeCaptureForClient(
104 media::VideoCaptureSessionId session_id
,
105 const media::VideoCaptureParams
& params
,
106 VideoCaptureController
* controller
,
107 VideoCaptureControllerID client_id
,
108 VideoCaptureControllerEventHandler
* client_handler
);
110 // Retrieves all capture supported formats for a particular device. Returns
111 // false if the |capture_session_id| is not found. The supported formats are
112 // cached during device(s) enumeration, and depending on the underlying
113 // implementation, could be an empty list.
114 bool GetDeviceSupportedFormats(
115 media::VideoCaptureSessionId capture_session_id
,
116 media::VideoCaptureFormats
* supported_formats
);
118 // Retrieves the format(s) currently in use. Returns false if the
119 // |capture_session_id| is not found. Returns true and |formats_in_use|
120 // otherwise. |formats_in_use| is empty if the device is not in use.
121 bool GetDeviceFormatsInUse(media::VideoCaptureSessionId capture_session_id
,
122 media::VideoCaptureFormats
* formats_in_use
);
124 // Sets the platform-dependent window ID for the desktop capture notification
125 // UI for the given session.
126 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id
,
127 gfx::NativeViewId window_id
);
129 // Gets a weak reference to the device factory, used for tests.
130 media::VideoCaptureDeviceFactory
* video_capture_device_factory() const {
131 return video_capture_device_factory_
.get();
135 void set_device_task_runner(
136 const scoped_refptr
<base::SingleThreadTaskRunner
>& device_task_runner
) {
137 device_task_runner_
= device_task_runner
;
142 ~VideoCaptureManager() override
;
145 // 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|
147 // may be freed by this function.
148 void DestroyDeviceEntryIfNoClients(DeviceEntry
* entry
);
150 // Helpers to report an event to our Listener.
151 void OnOpened(MediaStreamType type
,
152 media::VideoCaptureSessionId capture_session_id
);
153 void OnClosed(MediaStreamType type
,
154 media::VideoCaptureSessionId capture_session_id
);
155 void OnDevicesInfoEnumerated(
156 MediaStreamType stream_type
,
157 base::ElapsedTimer
* timer
,
158 const media::VideoCaptureDeviceInfos
& new_devices_info_cache
);
160 // Finds a DeviceEntry by its device ID and type, if it is already opened.
161 DeviceEntry
* GetDeviceEntryForMediaStreamDevice(
162 const MediaStreamDevice
& device_info
);
164 // Finds a DeviceEntry entry for the indicated session, creating a fresh one
165 // if necessary. Returns NULL if the session id is invalid.
166 DeviceEntry
* GetOrCreateDeviceEntry(
167 media::VideoCaptureSessionId capture_session_id
);
169 // Finds the DeviceEntry that owns a particular controller pointer.
170 DeviceEntry
* GetDeviceEntryForController(
171 const VideoCaptureController
* controller
) const;
173 bool IsOnDeviceThread() const;
175 // Consolidates the cached devices list with the list of currently connected
176 // devices in the system |names_snapshot|. Retrieves the supported formats of
177 // the new devices and sends the new cache to OnDevicesInfoEnumerated().
178 void ConsolidateDevicesInfoOnDeviceThread(
179 base::Callback
<void(const media::VideoCaptureDeviceInfos
&)>
180 on_devices_enumerated_callback
,
181 MediaStreamType stream_type
,
182 const media::VideoCaptureDeviceInfos
& old_device_info_cache
,
183 scoped_ptr
<media::VideoCaptureDevice::Names
> names_snapshot
);
185 // Creates and Starts a new VideoCaptureDevice, storing the result in
186 // |entry->video_capture_device|. Ownership of |client| passes to
188 void DoStartDeviceOnDeviceThread(
189 media::VideoCaptureSessionId session_id
,
191 const media::VideoCaptureParams
& params
,
192 scoped_ptr
<media::VideoCaptureDevice::Client
> client
);
194 // Stops and destroys the VideoCaptureDevice held in
195 // |entry->video_capture_device|.
196 void DoStopDeviceOnDeviceThread(DeviceEntry
* entry
);
198 media::VideoCaptureDeviceInfo
* FindDeviceInfoById(
199 const std::string
& id
,
200 media::VideoCaptureDeviceInfos
& device_vector
);
202 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry
* entry
,
203 gfx::NativeViewId window_id
);
205 void SaveDesktopCaptureWindowIdOnDeviceThread(
206 media::VideoCaptureSessionId session_id
,
207 gfx::NativeViewId window_id
);
209 // The message loop of media stream device thread, where VCD's live.
210 scoped_refptr
<base::SingleThreadTaskRunner
> device_task_runner_
;
212 // Only accessed on Browser::IO thread.
213 MediaStreamProviderListener
* listener_
;
214 media::VideoCaptureSessionId new_capture_session_id_
;
216 typedef std::map
<media::VideoCaptureSessionId
, MediaStreamDevice
> SessionMap
;
217 // 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
219 // determine which device to use when StartCaptureForClient() occurs. Used
220 // only on the IO thread.
221 SessionMap sessions_
;
223 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated
224 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices
225 // and VideoCaptureControllers and is responsible for deleting the instances
226 // when they are not used any longer.
228 // The set of currently started VideoCaptureDevice and VideoCaptureController
229 // objects is only accessed from IO thread, though the DeviceEntry instances
230 // themselves may visit to the device thread for device creation and
233 DeviceEntry(MediaStreamType stream_type
,
234 const std::string
& id
,
235 scoped_ptr
<VideoCaptureController
> controller
);
238 const MediaStreamType stream_type
;
239 const std::string id
;
241 // The controller. Only used from the IO thread.
242 scoped_ptr
<VideoCaptureController
> video_capture_controller
;
244 // The capture device. Only used from the device thread.
245 scoped_ptr
<media::VideoCaptureDevice
> video_capture_device
;
247 typedef std::set
<DeviceEntry
*> DeviceEntries
;
248 DeviceEntries devices_
;
250 // Device creation factory injected on construction from MediaStreamManager or
251 // from the test harness.
252 scoped_ptr
<media::VideoCaptureDeviceFactory
> video_capture_device_factory_
;
254 // Local cache of the enumerated video capture devices' names and capture
255 // supported formats. A snapshot of the current devices and their capabilities
256 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and
257 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update
258 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will
259 // use this list if the device is not started, otherwise it will retrieve the
260 // active device capture format from the VideoCaptureController associated.
261 media::VideoCaptureDeviceInfos devices_info_cache_
;
263 // Accessed on the device thread only.
264 std::map
<media::VideoCaptureSessionId
, gfx::NativeViewId
>
265 notification_window_ids_
;
267 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager
);
270 } // namespace content
272 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_