Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_manager.h
blob64589a974a33050050099d8c093eea3e35b5cf1a
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 <map>
16 #include <set>
17 #include <string>
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/process/process_handle.h"
22 #include "content/browser/renderer_host/media/media_stream_provider.h"
23 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
24 #include "content/common/content_export.h"
25 #include "content/common/media/media_stream_options.h"
26 #include "media/video/capture/video_capture_device.h"
27 #include "media/video/capture/video_capture_device_factory.h"
28 #include "media/video/capture/video_capture_types.h"
30 namespace content {
31 class VideoCaptureController;
32 class VideoCaptureControllerEventHandler;
34 // VideoCaptureManager opens/closes and start/stops video capture devices.
35 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider {
36 public:
37 // Callback used to signal the completion of a controller lookup.
38 typedef base::Callback<
39 void(const base::WeakPtr<VideoCaptureController>&)> DoneCB;
41 explicit VideoCaptureManager(
42 scoped_ptr<media::VideoCaptureDeviceFactory> factory);
44 // Implements MediaStreamProvider.
45 virtual void Register(MediaStreamProviderListener* listener,
46 const scoped_refptr<base::SingleThreadTaskRunner>&
47 device_task_runner) OVERRIDE;
49 virtual void Unregister() OVERRIDE;
51 virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE;
53 virtual int Open(const StreamDeviceInfo& device) OVERRIDE;
55 virtual void Close(int capture_session_id) OVERRIDE;
57 // Called by VideoCaptureHost to locate a capture device for |capture_params|,
58 // adding the Host as a client of the device's controller if successful. The
59 // value of |session_id| controls which device is selected;
60 // this value should be a session id previously returned by Open().
62 // If the device is not already started (i.e., no other client is currently
63 // capturing from this device), this call will cause a VideoCaptureController
64 // and VideoCaptureDevice to be created, possibly asynchronously.
66 // On success, the controller is returned via calling |done_cb|, indicating
67 // that the client was successfully added. A NULL controller is passed to
68 // the callback on failure.
69 void StartCaptureForClient(media::VideoCaptureSessionId session_id,
70 const media::VideoCaptureParams& capture_params,
71 base::ProcessHandle client_render_process,
72 VideoCaptureControllerID client_id,
73 VideoCaptureControllerEventHandler* client_handler,
74 const DoneCB& done_cb);
76 // Called by VideoCaptureHost to remove |client_handler|. If this is the last
77 // client of the device, the |controller| and its VideoCaptureDevice may be
78 // destroyed. The client must not access |controller| after calling this
79 // function.
80 void StopCaptureForClient(VideoCaptureController* controller,
81 VideoCaptureControllerID client_id,
82 VideoCaptureControllerEventHandler* client_handler,
83 bool aborted_due_to_error);
85 // Retrieves all capture supported formats for a particular device. Returns
86 // false if the |capture_session_id| is not found. The supported formats are
87 // cached during device(s) enumeration, and depending on the underlying
88 // implementation, could be an empty list.
89 bool GetDeviceSupportedFormats(
90 media::VideoCaptureSessionId capture_session_id,
91 media::VideoCaptureFormats* supported_formats);
93 // Retrieves the format(s) currently in use. Returns false if the
94 // |capture_session_id| is not found. Returns true and |formats_in_use|
95 // otherwise. |formats_in_use| is empty if the device is not in use.
96 bool GetDeviceFormatsInUse(media::VideoCaptureSessionId capture_session_id,
97 media::VideoCaptureFormats* formats_in_use);
99 // Sets the platform-dependent window ID for the desktop capture notification
100 // UI for the given session.
101 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id,
102 gfx::NativeViewId window_id);
104 // Gets a weak reference to the device factory, used for tests.
105 media::VideoCaptureDeviceFactory* video_capture_device_factory() const {
106 return video_capture_device_factory_.get();
109 private:
110 virtual ~VideoCaptureManager();
111 struct DeviceEntry;
113 // This data structure is a convenient wrap of a devices' name and associated
114 // video capture supported formats.
115 struct DeviceInfo {
116 DeviceInfo();
117 DeviceInfo(const media::VideoCaptureDevice::Name& name,
118 const media::VideoCaptureFormats& supported_formats);
119 ~DeviceInfo();
121 media::VideoCaptureDevice::Name name;
122 media::VideoCaptureFormats supported_formats;
124 typedef std::vector<DeviceInfo> DeviceInfos;
126 // Check to see if |entry| has no clients left on its controller. If so,
127 // remove it from the list of devices, and delete it asynchronously. |entry|
128 // may be freed by this function.
129 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry);
131 // Helpers to report an event to our Listener.
132 void OnOpened(MediaStreamType type,
133 media::VideoCaptureSessionId capture_session_id);
134 void OnClosed(MediaStreamType type,
135 media::VideoCaptureSessionId capture_session_id);
136 void OnDevicesInfoEnumerated(
137 MediaStreamType stream_type,
138 const DeviceInfos& new_devices_info_cache);
140 // Find a DeviceEntry by its device ID and type, if it is already opened.
141 DeviceEntry* GetDeviceEntryForMediaStreamDevice(
142 const MediaStreamDevice& device_info);
144 // Find a DeviceEntry entry for the indicated session, creating a fresh one
145 // if necessary. Returns NULL if the session id is invalid.
146 DeviceEntry* GetOrCreateDeviceEntry(
147 media::VideoCaptureSessionId capture_session_id);
149 // Find the DeviceEntry that owns a particular controller pointer.
150 DeviceEntry* GetDeviceEntryForController(
151 const VideoCaptureController* controller) const;
153 bool IsOnDeviceThread() const;
155 // Queries the Names of the devices in the system; the formats supported by
156 // the new devices are also queried, and consolidated with the copy of the
157 // local device info cache passed. The consolidated list of devices and
158 // supported formats is returned.
159 DeviceInfos GetAvailableDevicesInfoOnDeviceThread(
160 MediaStreamType stream_type,
161 const DeviceInfos& old_device_info_cache);
163 // Create and Start a new VideoCaptureDevice, storing the result in
164 // |entry->video_capture_device|. Ownership of |client| passes to
165 // the device.
166 void DoStartDeviceOnDeviceThread(
167 media::VideoCaptureSessionId session_id,
168 DeviceEntry* entry,
169 const media::VideoCaptureParams& params,
170 scoped_ptr<media::VideoCaptureDevice::Client> client);
172 // Stop and destroy the VideoCaptureDevice held in
173 // |entry->video_capture_device|.
174 void DoStopDeviceOnDeviceThread(DeviceEntry* entry);
176 DeviceInfo* FindDeviceInfoById(const std::string& id,
177 DeviceInfos& device_vector);
179 void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry,
180 gfx::NativeViewId window_id);
182 void SaveDesktopCaptureWindowIdOnDeviceThread(
183 media::VideoCaptureSessionId session_id,
184 gfx::NativeViewId window_id);
186 // The message loop of media stream device thread, where VCD's live.
187 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
189 // Only accessed on Browser::IO thread.
190 MediaStreamProviderListener* listener_;
191 media::VideoCaptureSessionId new_capture_session_id_;
193 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap;
194 // An entry is kept in this map for every session that has been created via
195 // the Open() entry point. The keys are session_id's. This map is used to
196 // determine which device to use when StartCaptureForClient() occurs. Used
197 // only on the IO thread.
198 SessionMap sessions_;
200 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated
201 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices
202 // and VideoCaptureControllers and is responsible for deleting the instances
203 // when they are not used any longer.
205 // The set of currently started VideoCaptureDevice and VideoCaptureController
206 // objects is only accessed from IO thread, though the DeviceEntry instances
207 // themselves may visit to the device thread for device creation and
208 // destruction.
209 struct DeviceEntry {
210 DeviceEntry(MediaStreamType stream_type,
211 const std::string& id,
212 scoped_ptr<VideoCaptureController> controller);
213 ~DeviceEntry();
215 const MediaStreamType stream_type;
216 const std::string id;
218 // The controller. Only used from the IO thread.
219 scoped_ptr<VideoCaptureController> video_capture_controller;
221 // The capture device. Only used from the device thread.
222 scoped_ptr<media::VideoCaptureDevice> video_capture_device;
224 typedef std::set<DeviceEntry*> DeviceEntries;
225 DeviceEntries devices_;
227 // Device creation factory injected on construction from MediaStreamManager or
228 // from the test harness.
229 scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_;
231 // Local cache of the enumerated video capture devices' names and capture
232 // supported formats. A snapshot of the current devices and their capabilities
233 // is composed in GetAvailableDevicesInfoOnDeviceThread() --coming
234 // from EnumerateDevices()--, and this snapshot is used to update this list in
235 // OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will
236 // use this list if the device is not started, otherwise it will retrieve the
237 // active device capture format from the VideoCaptureController associated.
238 DeviceInfos devices_info_cache_;
240 // Accessed on the device thread only.
241 std::map<media::VideoCaptureSessionId, gfx::NativeViewId>
242 notification_window_ids_;
244 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager);
247 } // namespace content
249 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_