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 16 matching lines...) Expand all Loading... |
27 #include "base/timer/elapsed_timer.h" | 27 #include "base/timer/elapsed_timer.h" |
28 #include "build/build_config.h" | 28 #include "build/build_config.h" |
29 #include "content/browser/renderer_host/media/buildable_video_capture_device.h" | 29 #include "content/browser/renderer_host/media/buildable_video_capture_device.h" |
30 #include "content/browser/renderer_host/media/media_stream_provider.h" | 30 #include "content/browser/renderer_host/media/media_stream_provider.h" |
31 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 31 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
32 #include "content/common/content_export.h" | 32 #include "content/common/content_export.h" |
33 #include "content/common/media/media_stream_options.h" | 33 #include "content/common/media/media_stream_options.h" |
34 #include "media/base/video_facing.h" | 34 #include "media/base/video_facing.h" |
35 #include "media/capture/video/video_capture_device.h" | 35 #include "media/capture/video/video_capture_device.h" |
36 #include "media/capture/video/video_capture_device_factory.h" | 36 #include "media/capture/video/video_capture_device_factory.h" |
| 37 #include "media/capture/video/video_capture_device_info.h" |
| 38 #include "media/capture/video/video_capture_system.h" |
37 #include "media/capture/video_capture_types.h" | 39 #include "media/capture/video_capture_types.h" |
38 | 40 |
39 #if defined(OS_ANDROID) | 41 #if defined(OS_ANDROID) |
40 #include "base/android/application_status_listener.h" | 42 #include "base/android/application_status_listener.h" |
41 #endif | 43 #endif |
42 | 44 |
43 namespace content { | 45 namespace content { |
44 class VideoCaptureController; | 46 class VideoCaptureController; |
45 class VideoCaptureControllerEventHandler; | 47 class VideoCaptureControllerEventHandler; |
46 | 48 |
47 // VideoCaptureManager opens/closes and start/stops video capture devices. | 49 // VideoCaptureManager opens/closes and start/stops video capture devices. |
48 class CONTENT_EXPORT VideoCaptureManager | 50 class CONTENT_EXPORT VideoCaptureManager |
49 : public MediaStreamProvider, | 51 : public MediaStreamProvider, |
50 public BuildableVideoCaptureDevice::Callbacks { | 52 public BuildableVideoCaptureDevice::Callbacks { |
51 public: | 53 public: |
52 using VideoCaptureDevice = media::VideoCaptureDevice; | 54 using VideoCaptureDevice = media::VideoCaptureDevice; |
53 | 55 |
54 // Callback used to signal the completion of a controller lookup. | 56 // Callback used to signal the completion of a controller lookup. |
55 using DoneCB = | 57 using DoneCB = |
56 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; | 58 base::Callback<void(const base::WeakPtr<VideoCaptureController>&)>; |
57 | 59 |
58 VideoCaptureManager( | 60 VideoCaptureManager( |
59 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, | 61 std::unique_ptr<media::VideoCaptureSystem> capture_system, |
60 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); | 62 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner); |
61 | 63 |
62 // AddVideoCaptureObserver() can be called only before any devices are opened. | 64 // AddVideoCaptureObserver() can be called only before any devices are opened. |
63 // RemoveAllVideoCaptureObservers() can be called only after all devices | 65 // RemoveAllVideoCaptureObservers() can be called only after all devices |
64 // are closed. | 66 // are closed. |
65 // They can be called more than once and it's ok to not call at all if the | 67 // They can be called more than once and it's ok to not call at all if the |
66 // client is not interested in receiving media::VideoCaptureObserver callacks. | 68 // client is not interested in receiving media::VideoCaptureObserver callacks. |
67 // This methods can be called on whatever thread. The callbacks of | 69 // This methods can be called on whatever thread. The callbacks of |
68 // media::VideoCaptureObserver arrive on browser IO thread. | 70 // media::VideoCaptureObserver arrive on browser IO thread. |
69 void AddVideoCaptureObserver(media::VideoCaptureObserver* observer); | 71 void AddVideoCaptureObserver(media::VideoCaptureObserver* observer); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 const std::string& device_id, | 158 const std::string& device_id, |
157 media::VideoCaptureFormats* supported_formats); | 159 media::VideoCaptureFormats* supported_formats); |
158 | 160 |
159 // Sets the platform-dependent window ID for the desktop capture notification | 161 // Sets the platform-dependent window ID for the desktop capture notification |
160 // UI for the given session. | 162 // UI for the given session. |
161 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id, | 163 void SetDesktopCaptureWindowId(media::VideoCaptureSessionId session_id, |
162 gfx::NativeViewId window_id); | 164 gfx::NativeViewId window_id); |
163 | 165 |
164 // Gets a weak reference to the device factory, used for tests. | 166 // Gets a weak reference to the device factory, used for tests. |
165 media::VideoCaptureDeviceFactory* video_capture_device_factory() const { | 167 media::VideoCaptureDeviceFactory* video_capture_device_factory() const { |
166 return video_capture_device_factory_.get(); | 168 return video_capture_system_->video_capture_device_factory(); |
167 } | 169 } |
168 | 170 |
169 #if defined(OS_WIN) | 171 #if defined(OS_WIN) |
170 void set_device_task_runner( | 172 void set_device_task_runner( |
171 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { | 173 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { |
172 device_task_runner_ = device_task_runner; | 174 device_task_runner_ = device_task_runner; |
173 } | 175 } |
174 #endif | 176 #endif |
175 | 177 |
176 // Returns the SingleThreadTaskRunner where devices are enumerated on and | 178 // Returns the SingleThreadTaskRunner where devices are enumerated on and |
(...skipping 13 matching lines...) Expand all Loading... |
190 | 192 |
191 #if defined(OS_ANDROID) | 193 #if defined(OS_ANDROID) |
192 // Some devices had troubles when stopped and restarted quickly, so the device | 194 // Some devices had troubles when stopped and restarted quickly, so the device |
193 // is only stopped when Chrome is sent to background and not when, e.g., a tab | 195 // is only stopped when Chrome is sent to background and not when, e.g., a tab |
194 // is hidden, see http://crbug.com/582295. | 196 // is hidden, see http://crbug.com/582295. |
195 void OnApplicationStateChange(base::android::ApplicationState state); | 197 void OnApplicationStateChange(base::android::ApplicationState state); |
196 #endif | 198 #endif |
197 | 199 |
198 using EnumerationCallback = | 200 using EnumerationCallback = |
199 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; | 201 base::Callback<void(const media::VideoCaptureDeviceDescriptors&)>; |
| 202 // Asynchronously obtains descriptors for the available devices. |
| 203 // As a side-effect, updates |devices_info_cache_|. |
200 void EnumerateDevices(const EnumerationCallback& client_callback); | 204 void EnumerateDevices(const EnumerationCallback& client_callback); |
201 | 205 |
202 // Implementation of BuildableVideoCaptureDevice::Callbacks: | 206 // Implementation of BuildableVideoCaptureDevice::Callbacks: |
203 const media::VideoCaptureDeviceDescriptor* LookupDeviceDescriptor( | 207 void OnDeviceStarted(VideoCaptureController* controller) override; |
204 const std::string& id) override; | |
205 void WillStartDevice(media::VideoFacingMode facing_mode) override; | |
206 void DidStartDevice(VideoCaptureController* controller) override; | |
207 void OnDeviceStartFailed(VideoCaptureController* controller) override; | 208 void OnDeviceStartFailed(VideoCaptureController* controller) override; |
208 void OnDeviceStartAborted() override; | 209 void OnDeviceStartAborted() override; |
209 | 210 |
210 // Retrieves camera calibration information for a particular device. Returns | 211 // Retrieves camera calibration information for a particular device. Returns |
211 // nullopt_t if the |device_id| is not found or camera calibration information | 212 // nullopt_t if the |device_id| is not found or camera calibration information |
212 // is not available for the device. Camera calibration is cached during | 213 // is not available for the device. Camera calibration is cached during |
213 // device(s) enumeration. | 214 // device(s) enumeration. |
214 base::Optional<CameraCalibration> GetCameraCalibration( | 215 base::Optional<CameraCalibration> GetCameraCalibration( |
215 const std::string& device_id); | 216 const std::string& device_id); |
216 | 217 |
217 private: | 218 private: |
218 class CaptureDeviceStartRequest; | 219 class CaptureDeviceStartRequest; |
219 struct DeviceInfo; | |
220 | 220 |
221 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; | 221 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
222 using DeviceInfos = std::vector<DeviceInfo>; | |
223 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; | 222 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
224 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; | 223 using VideoCaptureDeviceDescriptor = media::VideoCaptureDeviceDescriptor; |
225 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; | 224 using VideoCaptureDeviceDescriptors = media::VideoCaptureDeviceDescriptors; |
226 | 225 |
227 ~VideoCaptureManager() override; | 226 ~VideoCaptureManager() override; |
228 | 227 |
| 228 void OnDeviceInfosReceived( |
| 229 base::ElapsedTimer* timer, |
| 230 const EnumerationCallback& client_callback, |
| 231 const std::vector<media::VideoCaptureDeviceInfo>& device_infos); |
| 232 |
229 // Helpers to report an event to our Listener. | 233 // Helpers to report an event to our Listener. |
230 void OnOpened(MediaStreamType type, | 234 void OnOpened(MediaStreamType type, |
231 media::VideoCaptureSessionId capture_session_id); | 235 media::VideoCaptureSessionId capture_session_id); |
232 void OnClosed(MediaStreamType type, | 236 void OnClosed(MediaStreamType type, |
233 media::VideoCaptureSessionId capture_session_id); | 237 media::VideoCaptureSessionId capture_session_id); |
234 void OnDevicesInfoEnumerated(base::ElapsedTimer* timer, | |
235 const EnumerationCallback& client_callback, | |
236 const DeviceInfos& new_devices_info_cache); | |
237 | |
238 // Consolidates the cached devices list with the list of currently connected | |
239 // devices in the system |names_snapshot|. Retrieves the supported formats of | |
240 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | |
241 void ConsolidateDevicesInfoOnDeviceThread( | |
242 base::Callback<void(const DeviceInfos&)> on_devices_enumerated_callback, | |
243 const DeviceInfos& old_device_info_cache, | |
244 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot); | |
245 | 238 |
246 // Checks to see if |controller| has no clients left. If so, remove it from | 239 // Checks to see if |controller| has no clients left. If so, remove it from |
247 // the list of controllers, and delete it asynchronously. |controller| may be | 240 // the list of controllers, and delete it asynchronously. |controller| may be |
248 // freed by this function. | 241 // freed by this function. |
249 void DestroyControllerIfNoClients(VideoCaptureController* controller); | 242 void DestroyControllerIfNoClients(VideoCaptureController* controller); |
250 | 243 |
251 // Finds a VideoCaptureController in different ways: by |session_id|, by its | 244 // Finds a VideoCaptureController in different ways: by |session_id|, by its |
252 // |device_id| and |type| (if it is already opened), by its |controller| or by | 245 // |device_id| and |type| (if it is already opened), by its |controller| or by |
253 // its |serial_id|. In all cases, if not found, nullptr is returned. | 246 // its |serial_id|. In all cases, if not found, nullptr is returned. |
254 VideoCaptureController* LookupControllerBySessionId(int session_id); | 247 VideoCaptureController* LookupControllerBySessionId(int session_id); |
255 VideoCaptureController* LookupControllerByMediaTypeAndDeviceId( | 248 VideoCaptureController* LookupControllerByMediaTypeAndDeviceId( |
256 MediaStreamType type, | 249 MediaStreamType type, |
257 const std::string& device_id) const; | 250 const std::string& device_id) const; |
258 bool IsControllerPointerValid(const VideoCaptureController* controller) const; | 251 bool IsControllerPointerValid(const VideoCaptureController* controller) const; |
259 scoped_refptr<VideoCaptureController> GetControllerSharedRef( | 252 scoped_refptr<VideoCaptureController> GetControllerSharedRef( |
260 VideoCaptureController* controller) const; | 253 VideoCaptureController* controller) const; |
261 | 254 |
262 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | 255 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. |
263 DeviceInfo* GetDeviceInfoById(const std::string& id); | 256 media::VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); |
264 | 257 |
265 // Finds a VideoCaptureController for the indicated |capture_session_id|, | 258 // Finds a VideoCaptureController for the indicated |capture_session_id|, |
266 // creating a fresh one if necessary. Returns nullptr if said | 259 // creating a fresh one if necessary. Returns nullptr if said |
267 // |capture_session_id| is invalid. | 260 // |capture_session_id| is invalid. |
268 VideoCaptureController* GetOrCreateController( | 261 VideoCaptureController* GetOrCreateController( |
269 media::VideoCaptureSessionId capture_session_id, | 262 media::VideoCaptureSessionId capture_session_id, |
270 const media::VideoCaptureParams& params); | 263 const media::VideoCaptureParams& params); |
271 | 264 |
272 // Starting a capture device can take 1-2 seconds. | 265 // Starting a capture device can take 1-2 seconds. |
273 // To avoid multiple unnecessary start/stop commands to the OS, each start | 266 // To avoid multiple unnecessary start/stop commands to the OS, each start |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 // TODO(chfremer): Consider using CancellableTaskTracker, see | 306 // TODO(chfremer): Consider using CancellableTaskTracker, see |
314 // crbug.com/598465. | 307 // crbug.com/598465. |
315 DeviceStartQueue device_start_request_queue_; | 308 DeviceStartQueue device_start_request_queue_; |
316 | 309 |
317 // Queue to keep photo-associated requests waiting for a device to initialize, | 310 // Queue to keep photo-associated requests waiting for a device to initialize, |
318 // bundles a session id integer and an associated photo-related request. | 311 // bundles a session id integer and an associated photo-related request. |
319 std::list<std::pair<int, base::Closure>> photo_request_queue_; | 312 std::list<std::pair<int, base::Closure>> photo_request_queue_; |
320 | 313 |
321 // Device creation factory injected on construction from MediaStreamManager or | 314 // Device creation factory injected on construction from MediaStreamManager or |
322 // from the test harness. | 315 // from the test harness. |
323 std::unique_ptr<media::VideoCaptureDeviceFactory> | 316 std::unique_ptr<media::VideoCaptureSystem> video_capture_system_; |
324 video_capture_device_factory_; | |
325 | 317 |
326 base::ObserverList<media::VideoCaptureObserver> capture_observers_; | 318 base::ObserverList<media::VideoCaptureObserver> capture_observers_; |
327 | 319 |
328 // Local cache of the enumerated video capture devices' names and capture | 320 // Local cache of the enumerated DeviceInfos. GetDeviceSupportedFormats() will |
329 // supported formats. A snapshot of the current devices and their capabilities | |
330 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | |
331 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | |
332 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | |
333 // use this list if the device is not started, otherwise it will retrieve the | 321 // use this list if the device is not started, otherwise it will retrieve the |
334 // active device capture format from the VideoCaptureController associated. | 322 // active device capture format from the VideoCaptureController associated. |
335 DeviceInfos devices_info_cache_; | 323 std::vector<media::VideoCaptureDeviceInfo> devices_info_cache_; |
336 | 324 |
337 // Map used by DesktopCapture. | 325 // Map used by DesktopCapture. |
338 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 326 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
339 notification_window_ids_; | 327 notification_window_ids_; |
340 | 328 |
341 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 329 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
342 }; | 330 }; |
343 | 331 |
344 } // namespace content | 332 } // namespace content |
345 | 333 |
346 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 334 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |