OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // VideoCaptureManager is used to open/close, start/stop, enumerate available | 5 // VideoCaptureManager is used to open/close, start/stop, enumerate available |
6 // video capture devices, and manage VideoCaptureController's. | 6 // video capture devices, and manage VideoCaptureController's. |
7 // All functions are expected to be called from Browser::IO thread. Some helper | 7 // All functions are expected to be called from Browser::IO thread. Some helper |
8 // functions (*OnDeviceThread) will dispatch operations to the device thread. | 8 // functions (*OnDeviceThread) will dispatch operations to the device thread. |
9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. | 9 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. |
10 // A device can only be opened once. | 10 // A device can only be opened once. |
11 | 11 |
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 12 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 13 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
14 | 14 |
15 #include <list> | 15 #include <list> |
16 #include <map> | 16 #include <map> |
17 #include <set> | 17 #include <set> |
18 #include <string> | 18 #include <string> |
19 | 19 |
20 #include "base/macros.h" | 20 #include "base/macros.h" |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
23 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
24 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
25 #include "base/threading/thread_checker.h" | 25 #include "base/threading/thread_checker.h" |
26 #include "base/timer/elapsed_timer.h" | 26 #include "base/timer/elapsed_timer.h" |
27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 28 #include "content/browser/media/video_capture_device_info.h" |
28 #include "content/browser/renderer_host/media/media_stream_provider.h" | 29 #include "content/browser/renderer_host/media/media_stream_provider.h" |
29 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 30 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
30 #include "content/common/content_export.h" | 31 #include "content/common/content_export.h" |
31 #include "content/common/media/media_stream_options.h" | 32 #include "content/common/media/media_stream_options.h" |
32 #include "media/base/video_capture_types.h" | 33 #include "media/base/video_capture_types.h" |
33 #include "media/capture/video/video_capture_device.h" | 34 #include "media/capture/video/video_capture_device.h" |
34 #include "media/capture/video/video_capture_device_factory.h" | 35 #include "media/capture/video/video_capture_device_factory.h" |
35 #include "media/capture/video/video_capture_device_info.h" | |
36 | 36 |
37 #if defined(OS_ANDROID) | 37 #if defined(OS_ANDROID) |
38 #include "base/android/application_status_listener.h" | 38 #include "base/android/application_status_listener.h" |
39 #endif | 39 #endif |
40 | 40 |
41 namespace content { | 41 namespace content { |
42 class VideoCaptureController; | 42 class VideoCaptureController; |
43 class VideoCaptureControllerEventHandler; | 43 class VideoCaptureControllerEventHandler; |
44 | 44 |
45 // VideoCaptureManager opens/closes and start/stops video capture devices. | 45 // VideoCaptureManager opens/closes and start/stops video capture devices. |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 void OnApplicationStateChange(base::android::ApplicationState state); | 173 void OnApplicationStateChange(base::android::ApplicationState state); |
174 #endif | 174 #endif |
175 | 175 |
176 private: | 176 private: |
177 class CaptureDeviceStartRequest; | 177 class CaptureDeviceStartRequest; |
178 class DeviceEntry; | 178 class DeviceEntry; |
179 | 179 |
180 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; | 180 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
181 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; | 181 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; |
182 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; | 182 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
| 183 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; |
| 184 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; |
183 | 185 |
184 ~VideoCaptureManager() override; | 186 ~VideoCaptureManager() override; |
185 | 187 |
186 // Helpers to report an event to our Listener. | 188 // Helpers to report an event to our Listener. |
187 void OnOpened(MediaStreamType type, | 189 void OnOpened(MediaStreamType type, |
188 media::VideoCaptureSessionId capture_session_id); | 190 media::VideoCaptureSessionId capture_session_id); |
189 void OnClosed(MediaStreamType type, | 191 void OnClosed(MediaStreamType type, |
190 media::VideoCaptureSessionId capture_session_id); | 192 media::VideoCaptureSessionId capture_session_id); |
191 void OnDevicesInfoEnumerated( | 193 void OnDevicesInfoEnumerated( |
192 MediaStreamType stream_type, | 194 MediaStreamType stream_type, |
193 base::ElapsedTimer* timer, | 195 base::ElapsedTimer* timer, |
194 const media::VideoCaptureDeviceInfos& new_devices_info_cache); | 196 const VideoCaptureDeviceInfos& new_devices_info_cache); |
195 | 197 |
196 bool IsOnDeviceThread() const; | 198 bool IsOnDeviceThread() const; |
197 | 199 |
198 // Consolidates the cached devices list with the list of currently connected | 200 // Consolidates the cached devices list with the list of currently connected |
199 // devices in the system |names_snapshot|. Retrieves the supported formats of | 201 // devices in the system |names_snapshot|. Retrieves the supported formats of |
200 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 202 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
201 void ConsolidateDevicesInfoOnDeviceThread( | 203 void ConsolidateDevicesInfoOnDeviceThread( |
202 base::Callback<void(const media::VideoCaptureDeviceInfos&)> | 204 base::Callback<void(const VideoCaptureDeviceInfos&)> |
203 on_devices_enumerated_callback, | 205 on_devices_enumerated_callback, |
204 MediaStreamType stream_type, | 206 MediaStreamType stream_type, |
205 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 207 const VideoCaptureDeviceInfos& old_device_info_cache, |
206 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); | 208 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); |
207 | 209 |
208 // Checks to see if |entry| has no clients left on its controller. If so, | 210 // Checks to see if |entry| has no clients left on its controller. If so, |
209 // remove it from the list of devices, and delete it asynchronously. |entry| | 211 // remove it from the list of devices, and delete it asynchronously. |entry| |
210 // may be freed by this function. | 212 // may be freed by this function. |
211 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 213 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
212 | 214 |
213 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr | 215 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr |
214 // if not found. | 216 // if not found. |
215 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); | 217 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); |
216 | 218 |
217 // Finds a DeviceEntry in different ways: by its |device_id| and |type| (if it | 219 // Finds a DeviceEntry in different ways: by its |device_id| and |type| (if it |
218 // is already opened), by its |controller| or by its |serial_id|. In all | 220 // is already opened), by its |controller| or by its |serial_id|. In all |
219 // cases, if not found, nullptr is returned. | 221 // cases, if not found, nullptr is returned. |
220 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, | 222 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, |
221 const std::string& device_id) const; | 223 const std::string& device_id) const; |
222 DeviceEntry* GetDeviceEntryByController( | 224 DeviceEntry* GetDeviceEntryByController( |
223 const VideoCaptureController* controller) const; | 225 const VideoCaptureController* controller) const; |
224 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; | 226 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; |
225 | 227 |
226 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | 228 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
227 media::VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); | 229 VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); |
228 | 230 |
229 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating | 231 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating |
230 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is | 232 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is |
231 // invalid. | 233 // invalid. |
232 DeviceEntry* GetOrCreateDeviceEntry( | 234 DeviceEntry* GetOrCreateDeviceEntry( |
233 media::VideoCaptureSessionId capture_session_id, | 235 media::VideoCaptureSessionId capture_session_id, |
234 const media::VideoCaptureParams& params); | 236 const media::VideoCaptureParams& params); |
235 | 237 |
236 // Starting a capture device can take 1-2 seconds. | 238 // Starting a capture device can take 1-2 seconds. |
237 // To avoid multiple unnecessary start/stop commands to the OS, each start | 239 // To avoid multiple unnecessary start/stop commands to the OS, each start |
238 // request is queued in |device_start_queue_|. | 240 // request is queued in |device_start_queue_|. |
239 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 241 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
240 // request to start the device on the device thread unless there is | 242 // request to start the device on the device thread unless there is |
241 // another request pending start. | 243 // another request pending start. |
242 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 244 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
243 DeviceEntry* entry, | 245 DeviceEntry* entry, |
244 const media::VideoCaptureParams& params); | 246 const media::VideoCaptureParams& params); |
245 void OnDeviceStarted(int serial_id, | 247 void OnDeviceStarted(int serial_id, |
246 std::unique_ptr<VideoCaptureDevice> device); | 248 std::unique_ptr<VideoCaptureDevice> device); |
247 void DoStopDevice(DeviceEntry* entry); | 249 void DoStopDevice(DeviceEntry* entry); |
248 void HandleQueuedStartRequest(); | 250 void HandleQueuedStartRequest(); |
249 | 251 |
250 // Creates and Starts a new VideoCaptureDevice. The resulting | 252 // Creates and Starts a new VideoCaptureDevice. The resulting |
251 // VideoCaptureDevice is returned to the IO-thread and stored in | 253 // VideoCaptureDevice is returned to the IO-thread and stored in |
252 // a DeviceEntry in |devices_|. Ownership of |client| passes to | 254 // a DeviceEntry in |devices_|. Ownership of |client| passes to |
253 // the device. | 255 // the device. |
254 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread( | 256 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread( |
255 const VideoCaptureDevice::Name& name, | 257 const VideoCaptureDeviceDescriptor& descriptor, |
256 const media::VideoCaptureParams& params, | 258 const media::VideoCaptureParams& params, |
257 std::unique_ptr<VideoCaptureDevice::Client> client); | 259 std::unique_ptr<VideoCaptureDevice::Client> client); |
258 | 260 |
259 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread( | 261 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread( |
260 const std::string& device_id, | 262 const std::string& device_id, |
261 const media::VideoCaptureParams& params, | 263 const media::VideoCaptureParams& params, |
262 std::unique_ptr<VideoCaptureDevice::Client> client); | 264 std::unique_ptr<VideoCaptureDevice::Client> client); |
263 | 265 |
264 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( | 266 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( |
265 const std::string& device_id, | 267 const std::string& device_id, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 std::unique_ptr<media::VideoCaptureDeviceFactory> | 330 std::unique_ptr<media::VideoCaptureDeviceFactory> |
329 video_capture_device_factory_; | 331 video_capture_device_factory_; |
330 | 332 |
331 // Local cache of the enumerated video capture devices' names and capture | 333 // Local cache of the enumerated video capture devices' names and capture |
332 // supported formats. A snapshot of the current devices and their capabilities | 334 // supported formats. A snapshot of the current devices and their capabilities |
333 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 335 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
334 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 336 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
335 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 337 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
336 // use this list if the device is not started, otherwise it will retrieve the | 338 // use this list if the device is not started, otherwise it will retrieve the |
337 // active device capture format from the VideoCaptureController associated. | 339 // active device capture format from the VideoCaptureController associated. |
338 media::VideoCaptureDeviceInfos devices_info_cache_; | 340 VideoCaptureDeviceInfos devices_info_cache_; |
339 | 341 |
340 // Map used by DesktopCapture. | 342 // Map used by DesktopCapture. |
341 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 343 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
342 notification_window_ids_; | 344 notification_window_ids_; |
343 | 345 |
344 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 346 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
345 }; | 347 }; |
346 | 348 |
347 } // namespace content | 349 } // namespace content |
348 | 350 |
349 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 351 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |