Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / video_capture_manager.h
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.
4
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.
11
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
14
15 #include <map>
16 #include <set>
17 #include <string>
18
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"
29
30 namespace content {
31 class VideoCaptureController;
32 class VideoCaptureControllerEventHandler;
33
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;
40
41   explicit VideoCaptureManager(
42       scoped_ptr<media::VideoCaptureDeviceFactory> factory);
43
44   // Implements MediaStreamProvider.
45   virtual void Register(MediaStreamProviderListener* listener,
46                         const scoped_refptr<base::SingleThreadTaskRunner>&
47                             device_task_runner) OVERRIDE;
48
49   virtual void Unregister() OVERRIDE;
50
51   virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE;
52
53   virtual int Open(const StreamDeviceInfo& device) OVERRIDE;
54
55   virtual void Close(int capture_session_id) OVERRIDE;
56
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().
61   //
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.
65   //
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);
75
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);
84
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);
92
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);
98
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);
103
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();
107   }
108
109  private:
110   virtual ~VideoCaptureManager();
111   struct DeviceEntry;
112
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();
120
121     media::VideoCaptureDevice::Name name;
122     media::VideoCaptureFormats supported_formats;
123   };
124   typedef std::vector<DeviceInfo> DeviceInfos;
125
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);
130
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);
139
140   // Find a DeviceEntry by its device ID and type, if it is already opened.
141   DeviceEntry* GetDeviceEntryForMediaStreamDevice(
142       const MediaStreamDevice& device_info);
143
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);
148
149   // Find the DeviceEntry that owns a particular controller pointer.
150   DeviceEntry* GetDeviceEntryForController(
151       const VideoCaptureController* controller) const;
152
153   bool IsOnDeviceThread() const;
154
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);
162
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);
171
172   // Stop and destroy the VideoCaptureDevice held in
173   // |entry->video_capture_device|.
174   void DoStopDeviceOnDeviceThread(DeviceEntry* entry);
175
176   DeviceInfo* FindDeviceInfoById(const std::string& id,
177                                  DeviceInfos& device_vector);
178
179   void SetDesktopCaptureWindowIdOnDeviceThread(DeviceEntry* entry,
180                                                gfx::NativeViewId window_id);
181
182   void SaveDesktopCaptureWindowIdOnDeviceThread(
183       media::VideoCaptureSessionId session_id,
184       gfx::NativeViewId window_id);
185
186   // The message loop of media stream device thread, where VCD's live.
187   scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
188
189   // Only accessed on Browser::IO thread.
190   MediaStreamProviderListener* listener_;
191   media::VideoCaptureSessionId new_capture_session_id_;
192
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_;
199
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.
204   //
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();
214
215     const MediaStreamType stream_type;
216     const std::string id;
217
218     // The controller. Only used from the IO thread.
219     scoped_ptr<VideoCaptureController> video_capture_controller;
220
221     // The capture device. Only used from the device thread.
222     scoped_ptr<media::VideoCaptureDevice> video_capture_device;
223   };
224   typedef std::set<DeviceEntry*> DeviceEntries;
225   DeviceEntries devices_;
226
227   // Device creation factory injected on construction from MediaStreamManager or
228   // from the test harness.
229   scoped_ptr<media::VideoCaptureDeviceFactory> video_capture_device_factory_;
230
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_;
239
240   // Accessed on the device thread only.
241   std::map<media::VideoCaptureSessionId, gfx::NativeViewId>
242       notification_window_ids_;
243
244   DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager);
245 };
246
247 }  // namespace content
248
249 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_