Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_manager.h
blob3b0b46676825abebafb481b0a452ae11f8b7150a
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_
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <string>
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/memory/weak_ptr.h"
23 #include "base/message_loop/message_loop.h"
24 #include "base/process/process_handle.h"
25 #include "base/threading/thread_checker.h"
26 #include "base/timer/elapsed_timer.h"
27 #include "content/browser/renderer_host/media/media_stream_provider.h"
28 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
29 #include "content/common/content_export.h"
30 #include "content/common/media/media_stream_options.h"
31 #include "media/base/video_capture_types.h"
32 #include "media/video/capture/video_capture_device.h"
33 #include "media/video/capture/video_capture_device_factory.h"
34 #include "media/video/capture/video_capture_device_info.h"
36 namespace content {
37 class VideoCaptureController;
38 class VideoCaptureControllerEventHandler;
40 // VideoCaptureManager opens/closes and start/stops video capture devices.
41 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider {
42 public:
43 // Callback used to signal the completion of a controller lookup.
44 typedef base::Callback<
45 void(const base::WeakPtr<VideoCaptureController>&)> DoneCB;
47 explicit VideoCaptureManager(
48 scoped_ptr<media::VideoCaptureDeviceFactory> factory);
50 void Unregister();
52 // Implements MediaStreamProvider.
53 void Register(MediaStreamProviderListener* listener,
54 const scoped_refptr<base::SingleThreadTaskRunner>&
55 device_task_runner) override;
57 void EnumerateDevices(MediaStreamType stream_type) override;
59 int Open(const StreamDeviceInfo& device) override;
61 void Close(int capture_session_id) override;
63 // Called by VideoCaptureHost to locate a capture device for |capture_params|,
64 // adding the Host as a client of the device's controller if successful. The
65 // value of |session_id| controls which device is selected;
66 // this value should be a session id previously returned by Open().
68 // If the device is not already started (i.e., no other client is currently
69 // capturing from this device), this call will cause a VideoCaptureController
70 // and VideoCaptureDevice to be created, possibly asynchronously.
72 // On success, the controller is returned via calling |done_cb|, indicating
73 // that the client was successfully added. A NULL controller is passed to
74 // the callback on failure.
75 void StartCaptureForClient(media::VideoCaptureSessionId session_id,
76 const media::VideoCaptureParams& capture_params,
77 base::ProcessHandle client_render_process,
78 VideoCaptureControllerID client_id,
79 VideoCaptureControllerEventHandler* client_handler,
80 const DoneCB& done_cb);
82 // Called by VideoCaptureHost to remove |client_handler|. If this is the last
83 // client of the device, the |controller| and its VideoCaptureDevice may be
84 // destroyed. The client must not access |controller| after calling this
85 // function.
86 void StopCaptureForClient(VideoCaptureController* controller,
87 VideoCaptureControllerID client_id,
88 VideoCaptureControllerEventHandler* client_handler,
89 bool aborted_due_to_error);
91 // Called by VideoCaptureHost to pause to update video buffer specified by
92 // |client_id| and |client_handler|. If all clients of |controller| are
93 // paused, the corresponding device will be closed.
94 void PauseCaptureForClient(
95 VideoCaptureController* controller,
96 VideoCaptureControllerID client_id,
97 VideoCaptureControllerEventHandler* client_handler);
99 // Called by VideoCaptureHost to resume to update video buffer specified by
100 // |client_id| and |client_handler|. The |session_id| and |params| should be
101 // same as those used in StartCaptureForClient().
102 // If this is first active client of |controller|, device will be allocated
103 // and it will take a little time to resume.
104 // Allocating device could failed if other app holds the camera, the error
105 // will be notified through VideoCaptureControllerEventHandler::OnError().
106 void ResumeCaptureForClient(
107 media::VideoCaptureSessionId session_id,
108 const media::VideoCaptureParams& params,
109 VideoCaptureController* controller,
110 VideoCaptureControllerID client_id,
111 VideoCaptureControllerEventHandler* client_handler);
113 // Retrieves all capture supported formats for a particular device. Returns
114 // false if the |capture_session_id| is not found. The supported formats are
115 // cached during device(s) enumeration, and depending on the underlying
116 // implementation, could be an empty list.
117 bool GetDeviceSupportedFormats(
118 media::VideoCaptureSessionId capture_session_id,
119 media::VideoCaptureFormats* supported_formats);
121 // Retrieves the format(s) currently in use. Returns false if the
122 // |capture_session_id| is not found. Returns true and |formats_in_use|
123 // otherwise. |formats_in_use| is empty if the device is not in use.
124 bool GetDeviceFormatsInUse(media::VideoCaptureSessionId capture_session_id,
125 media::VideoCaptureFormats* formats_in_use);
127 // Sets the platform-dependent window ID for the desktop capture notification
128 // UI for the given session.
129 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id,
130 gfx::NativeViewId window_id);
132 // Gets a weak reference to the device factory, used for tests.
133 media::VideoCaptureDeviceFactory* video_capture_device_factory() const {
134 return video_capture_device_factory_.get();
137 #if defined(OS_WIN)
138 void set_device_task_runner(
139 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) {
140 device_task_runner_ = device_task_runner;
142 #endif
144 // Returns the SingleThreadTaskRunner where devices are enumerated on and
145 // started.
146 scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner() {
147 return device_task_runner_;
149 private:
150 ~VideoCaptureManager() override;
151 class DeviceEntry;
153 // Checks to see if |entry| has no clients left on its controller. If so,
154 // remove it from the list of devices, and delete it asynchronously. |entry|
155 // may be freed by this function.
156 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry);
158 // Helpers to report an event to our Listener.
159 void OnOpened(MediaStreamType type,
160 media::VideoCaptureSessionId capture_session_id);
161 void OnClosed(MediaStreamType type,
162 media::VideoCaptureSessionId capture_session_id);
163 void OnDevicesInfoEnumerated(
164 MediaStreamType stream_type,
165 base::ElapsedTimer* timer,
166 const media::VideoCaptureDeviceInfos& new_devices_info_cache);
168 // Finds a DeviceEntry by its device ID and type, if it is already opened.
169 DeviceEntry* GetDeviceEntryForMediaStreamDevice(
170 const MediaStreamDevice& device_info);
172 // Finds a DeviceEntry entry for the indicated session, creating a fresh one
173 // if necessary. Returns NULL if the session id is invalid.
174 DeviceEntry* GetOrCreateDeviceEntry(
175 media::VideoCaptureSessionId capture_session_id);
177 // Finds the DeviceEntry that owns a particular controller pointer.
178 DeviceEntry* GetDeviceEntryForController(
179 const VideoCaptureController* controller) const;
181 bool IsOnDeviceThread() const;
183 // Consolidates the cached devices list with the list of currently connected
184 // devices in the system |names_snapshot|. Retrieves the supported formats of
185 // the new devices and sends the new cache to OnDevicesInfoEnumerated().
186 void ConsolidateDevicesInfoOnDeviceThread(
187 base::Callback<void(const media::VideoCaptureDeviceInfos&)>
188 on_devices_enumerated_callback,
189 MediaStreamType stream_type,
190 const media::VideoCaptureDeviceInfos& old_device_info_cache,
191 scoped_ptr<media::VideoCaptureDevice::Names> names_snapshot);
193 // Starting a capture device can take 1-2 seconds.
194 // To avoid multiple unnecessary start/stop commands to the OS, each start
195 // request is queued in |device_start_queue_|.
196 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a
197 // request to start the device on the device thread unless there is
198 // another request pending start.
199 void QueueStartDevice(media::VideoCaptureSessionId session_id,
200 DeviceEntry* entry,
201 const media::VideoCaptureParams& params);
202 void OnDeviceStarted(int serial_id,
203 scoped_ptr<media::VideoCaptureDevice> device);
204 void DoStopDevice(DeviceEntry* entry);
205 void HandleQueuedStartRequest();
207 // Creates and Starts a new VideoCaptureDevice. The resulting
208 // VideoCaptureDevice is returned to the IO-thread and stored in
209 // a DeviceEntry in |devices_|. Ownership of |client| passes to
210 // the device.
211 scoped_ptr<media::VideoCaptureDevice> DoStartDeviceOnDeviceThread(
212 media::VideoCaptureSessionId session_id,
213 const std::string& device_id,
214 MediaStreamType stream_type,
215 const media::VideoCaptureParams& params,
216 scoped_ptr<media::VideoCaptureDevice::Client> client);
218 // Stops and destroys the VideoCaptureDevice held in
219 // |device|.
220 void DoStopDeviceOnDeviceThread(scoped_ptr<media::VideoCaptureDevice> device);
222 media::VideoCaptureDeviceInfo* FindDeviceInfoById(
223 const std::string& id,
224 media::VideoCaptureDeviceInfos& device_vector);
226 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id);
227 void SetDesktopCaptureWindowIdOnDeviceThread(
228 media::VideoCaptureDevice* device,
229 gfx::NativeViewId window_id);
231 // The message loop of media stream device thread, where VCD's live.
232 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
234 // Only accessed on Browser::IO thread.
235 MediaStreamProviderListener* listener_;
236 media::VideoCaptureSessionId new_capture_session_id_;
238 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap;
239 // An entry is kept in this map for every session that has been created via
240 // the Open() entry point. The keys are session_id's. This map is used to
241 // determine which device to use when StartCaptureForClient() occurs. Used
242 // only on the IO thread.
243 SessionMap sessions_;
245 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated
246 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices
247 // and VideoCaptureControllers and is responsible for deleting the instances
248 // when they are not used any longer.
250 // The set of currently started VideoCaptureDevice and VideoCaptureController
251 // objects is only accessed from IO thread.
252 class DeviceEntry {
253 public:
254 DeviceEntry(MediaStreamType stream_type,
255 const std::string& id,
256 scoped_ptr<VideoCaptureController> controller);
257 ~DeviceEntry();
259 const int serial_id;
260 const MediaStreamType stream_type;
261 const std::string id;
263 VideoCaptureController* video_capture_controller();
264 media::VideoCaptureDevice* video_capture_device();
266 void SetVideoCaptureDevice(scoped_ptr<media::VideoCaptureDevice> device);
267 scoped_ptr<media::VideoCaptureDevice> ReleaseVideoCaptureDevice();
269 private:
270 // The controller.
271 scoped_ptr<VideoCaptureController> video_capture_controller_;
273 // The capture device.
274 scoped_ptr<media::VideoCaptureDevice> video_capture_device_;
276 base::ThreadChecker thread_checker_;
279 typedef ScopedVector<DeviceEntry> DeviceEntries;
280 // Currently opened devices. The device may or may not be started.
281 DeviceEntries devices_;
283 // Class used for queuing request for starting a device.
284 class CaptureDeviceStartRequest {
285 public:
286 CaptureDeviceStartRequest(
287 int serial_id,
288 media::VideoCaptureSessionId session_id,
289 const media::VideoCaptureParams& params);
290 int serial_id() const { return serial_id_;}
291 media::VideoCaptureSessionId session_id() const { return session_id_; }
292 media::VideoCaptureParams params() const { return params_; }
294 // Set to true if the device should be stopped before it has successfully
295 // been started.
296 bool abort_start() const { return abort_start_; }
297 void set_abort_start() { abort_start_ = true; }
299 private:
300 const int serial_id_;
301 const media::VideoCaptureSessionId session_id_;
302 const media::VideoCaptureParams params_;
303 // Set to true if the device should be stopped before it has successfully
304 // been started.
305 bool abort_start_;
308 typedef std::list<CaptureDeviceStartRequest> DeviceStartQueue;
309 DeviceStartQueue device_start_queue_;
311 // Device creation factory injected on construction from MediaStreamManager or
312 // from the test harness.
313 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_;
315 // Local cache of the enumerated video capture devices' names and capture
316 // supported formats. A snapshot of the current devices and their capabilities
317 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and
318 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update
319 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will
320 // use this list if the device is not started, otherwise it will retrieve the
321 // active device capture format from the VideoCaptureController associated.
322 media::VideoCaptureDeviceInfos devices_info_cache_;
324 // Map used by DesktopCapture.
325 std::map<media::VideoCaptureSessionId, gfx::NativeViewId>
326 notification_window_ids_;
328 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager);
331 } // namespace content
333 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_