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