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. |
(...skipping 14 matching lines...) Expand all Loading... |
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/renderer_host/media/media_stream_provider.h" | 28 #include "content/browser/renderer_host/media/media_stream_provider.h" |
29 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 29 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
30 #include "content/common/content_export.h" | 30 #include "content/common/content_export.h" |
31 #include "content/common/media/media_stream_options.h" | 31 #include "content/common/media/media_stream_options.h" |
32 #include "media/base/video_capture_types.h" | 32 #include "media/base/video_capture_types.h" |
33 #include "media/capture/video/video_capture_device.h" | 33 #include "media/capture/video/video_capture_device.h" |
34 #include "media/capture/video/video_capture_device_factory.h" | 34 #include "media/capture/video/video_capture_device_factory.h" |
35 #include "media/capture/video/video_capture_device_info.h" | |
36 | 35 |
37 #if defined(OS_ANDROID) | 36 #if defined(OS_ANDROID) |
38 #include "base/android/application_status_listener.h" | 37 #include "base/android/application_status_listener.h" |
39 #endif | 38 #endif |
40 | 39 |
41 namespace content { | 40 namespace content { |
42 class VideoCaptureController; | 41 class VideoCaptureController; |
43 class VideoCaptureControllerEventHandler; | 42 class VideoCaptureControllerEventHandler; |
44 | 43 |
45 // VideoCaptureManager opens/closes and start/stops video capture devices. | 44 // VideoCaptureManager opens/closes and start/stops video capture devices. |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 #if defined(OS_ANDROID) | 168 #if defined(OS_ANDROID) |
170 // Some devices had troubles when stopped and restarted quickly, so the device | 169 // Some devices had troubles when stopped and restarted quickly, so the device |
171 // is only stopped when Chrome is sent to background and not when, e.g., a tab | 170 // is only stopped when Chrome is sent to background and not when, e.g., a tab |
172 // is hidden, see http://crbug.com/582295. | 171 // is hidden, see http://crbug.com/582295. |
173 void OnApplicationStateChange(base::android::ApplicationState state); | 172 void OnApplicationStateChange(base::android::ApplicationState state); |
174 #endif | 173 #endif |
175 | 174 |
176 private: | 175 private: |
177 class CaptureDeviceStartRequest; | 176 class CaptureDeviceStartRequest; |
178 class DeviceEntry; | 177 class DeviceEntry; |
| 178 struct DeviceInfo; |
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 DeviceInfos = std::vector<DeviceInfo>; |
182 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; | 183 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
| 184 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; |
| 185 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; |
183 | 186 |
184 ~VideoCaptureManager() override; | 187 ~VideoCaptureManager() override; |
185 | 188 |
186 // Helpers to report an event to our Listener. | 189 // Helpers to report an event to our Listener. |
187 void OnOpened(MediaStreamType type, | 190 void OnOpened(MediaStreamType type, |
188 media::VideoCaptureSessionId capture_session_id); | 191 media::VideoCaptureSessionId capture_session_id); |
189 void OnClosed(MediaStreamType type, | 192 void OnClosed(MediaStreamType type, |
190 media::VideoCaptureSessionId capture_session_id); | 193 media::VideoCaptureSessionId capture_session_id); |
191 void OnDevicesInfoEnumerated( | 194 void OnDevicesInfoEnumerated(MediaStreamType stream_type, |
192 MediaStreamType stream_type, | 195 base::ElapsedTimer* timer, |
193 base::ElapsedTimer* timer, | 196 const DeviceInfos& new_devices_info_cache); |
194 const media::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 DeviceInfos&)> on_devices_enumerated_callback, |
203 on_devices_enumerated_callback, | |
204 MediaStreamType stream_type, | 205 MediaStreamType stream_type, |
205 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 206 const DeviceInfos& old_device_info_cache, |
206 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); | 207 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); |
207 | 208 |
208 // Checks to see if |entry| has no clients left on its controller. If so, | 209 // 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| | 210 // remove it from the list of devices, and delete it asynchronously. |entry| |
210 // may be freed by this function. | 211 // may be freed by this function. |
211 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 212 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
212 | 213 |
213 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr | 214 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr |
214 // if not found. | 215 // if not found. |
215 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); | 216 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); |
216 | 217 |
217 // Finds a DeviceEntry in different ways: by its |device_id| and |type| (if it | 218 // 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 | 219 // is already opened), by its |controller| or by its |serial_id|. In all |
219 // cases, if not found, nullptr is returned. | 220 // cases, if not found, nullptr is returned. |
220 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, | 221 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, |
221 const std::string& device_id) const; | 222 const std::string& device_id) const; |
222 DeviceEntry* GetDeviceEntryByController( | 223 DeviceEntry* GetDeviceEntryByController( |
223 const VideoCaptureController* controller) const; | 224 const VideoCaptureController* controller) const; |
224 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; | 225 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; |
225 | 226 |
226 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | 227 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
227 media::VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); | 228 DeviceInfo* GetDeviceInfoById(const std::string& id); |
228 | 229 |
229 // Finds a DeviceEntry entry for the indicated |capture_session_id|, creating | 230 // 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 | 231 // a fresh one if necessary. Returns nullptr if said |capture_session_id| is |
231 // invalid. | 232 // invalid. |
232 DeviceEntry* GetOrCreateDeviceEntry( | 233 DeviceEntry* GetOrCreateDeviceEntry( |
233 media::VideoCaptureSessionId capture_session_id, | 234 media::VideoCaptureSessionId capture_session_id, |
234 const media::VideoCaptureParams& params); | 235 const media::VideoCaptureParams& params); |
235 | 236 |
236 // Starting a capture device can take 1-2 seconds. | 237 // Starting a capture device can take 1-2 seconds. |
237 // To avoid multiple unnecessary start/stop commands to the OS, each start | 238 // To avoid multiple unnecessary start/stop commands to the OS, each start |
238 // request is queued in |device_start_queue_|. | 239 // request is queued in |device_start_queue_|. |
239 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 240 // 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 | 241 // request to start the device on the device thread unless there is |
241 // another request pending start. | 242 // another request pending start. |
242 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 243 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
243 DeviceEntry* entry, | 244 DeviceEntry* entry, |
244 const media::VideoCaptureParams& params); | 245 const media::VideoCaptureParams& params); |
245 void OnDeviceStarted(int serial_id, | 246 void OnDeviceStarted(int serial_id, |
246 std::unique_ptr<VideoCaptureDevice> device); | 247 std::unique_ptr<VideoCaptureDevice> device); |
247 void DoStopDevice(DeviceEntry* entry); | 248 void DoStopDevice(DeviceEntry* entry); |
248 void HandleQueuedStartRequest(); | 249 void HandleQueuedStartRequest(); |
249 | 250 |
250 // Creates and Starts a new VideoCaptureDevice. The resulting | 251 // Creates and Starts a new VideoCaptureDevice. The resulting |
251 // VideoCaptureDevice is returned to the IO-thread and stored in | 252 // VideoCaptureDevice is returned to the IO-thread and stored in |
252 // a DeviceEntry in |devices_|. Ownership of |client| passes to | 253 // a DeviceEntry in |devices_|. Ownership of |client| passes to |
253 // the device. | 254 // the device. |
254 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread( | 255 std::unique_ptr<VideoCaptureDevice> DoStartDeviceCaptureOnDeviceThread( |
255 const VideoCaptureDevice::Name& name, | 256 const VideoCaptureDeviceDescriptor& descriptor, |
256 const media::VideoCaptureParams& params, | 257 const media::VideoCaptureParams& params, |
257 std::unique_ptr<VideoCaptureDevice::Client> client); | 258 std::unique_ptr<VideoCaptureDevice::Client> client); |
258 | 259 |
259 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread( | 260 std::unique_ptr<VideoCaptureDevice> DoStartTabCaptureOnDeviceThread( |
260 const std::string& device_id, | 261 const std::string& device_id, |
261 const media::VideoCaptureParams& params, | 262 const media::VideoCaptureParams& params, |
262 std::unique_ptr<VideoCaptureDevice::Client> client); | 263 std::unique_ptr<VideoCaptureDevice::Client> client); |
263 | 264 |
264 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( | 265 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( |
265 const std::string& device_id, | 266 const std::string& device_id, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 std::unique_ptr<media::VideoCaptureDeviceFactory> | 329 std::unique_ptr<media::VideoCaptureDeviceFactory> |
329 video_capture_device_factory_; | 330 video_capture_device_factory_; |
330 | 331 |
331 // Local cache of the enumerated video capture devices' names and capture | 332 // Local cache of the enumerated video capture devices' names and capture |
332 // supported formats. A snapshot of the current devices and their capabilities | 333 // supported formats. A snapshot of the current devices and their capabilities |
333 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 334 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
334 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 335 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
335 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 336 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
336 // use this list if the device is not started, otherwise it will retrieve the | 337 // use this list if the device is not started, otherwise it will retrieve the |
337 // active device capture format from the VideoCaptureController associated. | 338 // active device capture format from the VideoCaptureController associated. |
338 media::VideoCaptureDeviceInfos devices_info_cache_; | 339 DeviceInfos devices_info_cache_; |
339 | 340 |
340 // Map used by DesktopCapture. | 341 // Map used by DesktopCapture. |
341 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 342 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
342 notification_window_ids_; | 343 notification_window_ids_; |
343 | 344 |
344 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 345 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
345 }; | 346 }; |
346 | 347 |
347 } // namespace content | 348 } // namespace content |
348 | 349 |
349 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 350 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |