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