Chromium Code Reviews| 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/scoped_vector.h" | |
| 23 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
| 24 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
| 25 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
| 26 #include "base/threading/thread_checker.h" | 25 #include "base/threading/thread_checker.h" |
| 27 #include "base/timer/elapsed_timer.h" | 26 #include "base/timer/elapsed_timer.h" |
| 28 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 29 #include "content/browser/renderer_host/media/media_stream_provider.h" | 28 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 30 #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" |
| 31 #include "content/common/content_export.h" | 30 #include "content/common/content_export.h" |
| 32 #include "content/common/media/media_stream_options.h" | 31 #include "content/common/media/media_stream_options.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 VideoCaptureDevice::TakePhotoCallback> callback); | 167 VideoCaptureDevice::TakePhotoCallback> callback); |
| 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 DeviceEntry; | 176 class DeviceEntry; |
| 177 using DeviceEntries = std::vector<std::unique_ptr<DeviceEntry>>; | |
| 178 | 178 |
| 179 ~VideoCaptureManager() override; | 179 ~VideoCaptureManager() override; |
| 180 | 180 |
| 181 // Checks to see if |entry| has no clients left on its controller. If so, | |
| 182 // remove it from the list of devices, and delete it asynchronously. |entry| | |
| 183 // may be freed by this function. | |
| 184 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | |
| 185 | |
| 186 // Helpers to report an event to our Listener. | 181 // Helpers to report an event to our Listener. |
| 187 void OnOpened(MediaStreamType type, | 182 void OnOpened(MediaStreamType type, |
| 188 media::VideoCaptureSessionId capture_session_id); | 183 media::VideoCaptureSessionId capture_session_id); |
| 189 void OnClosed(MediaStreamType type, | 184 void OnClosed(MediaStreamType type, |
| 190 media::VideoCaptureSessionId capture_session_id); | 185 media::VideoCaptureSessionId capture_session_id); |
| 191 void OnDevicesInfoEnumerated( | 186 void OnDevicesInfoEnumerated( |
| 192 MediaStreamType stream_type, | 187 MediaStreamType stream_type, |
| 193 base::ElapsedTimer* timer, | 188 base::ElapsedTimer* timer, |
| 194 const media::VideoCaptureDeviceInfos& new_devices_info_cache); | 189 const media::VideoCaptureDeviceInfos& new_devices_info_cache); |
| 195 | 190 |
| 196 // Finds a DeviceEntry by its device ID and type, if it is already opened. | |
| 197 DeviceEntry* GetDeviceEntryForMediaStreamDevice( | |
| 198 const MediaStreamDevice& device_info); | |
| 199 | |
| 200 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr | |
| 201 // if not found. | |
| 202 VideoCaptureDevice* GetVideoCaptureDeviceFromSessionId(int session_id); | |
| 203 | |
| 204 // Finds a DeviceEntry entry for the indicated session, creating a fresh one | |
| 205 // if necessary. Returns NULL if the session id is invalid. | |
| 206 DeviceEntry* GetOrCreateDeviceEntry( | |
| 207 media::VideoCaptureSessionId capture_session_id, | |
| 208 const media::VideoCaptureParams& params); | |
| 209 | |
| 210 // Finds the DeviceEntry that owns a particular controller pointer. | |
| 211 DeviceEntry* GetDeviceEntryForController( | |
| 212 const VideoCaptureController* controller) const; | |
| 213 | |
| 214 bool IsOnDeviceThread() const; | 191 bool IsOnDeviceThread() const; |
| 215 | 192 |
| 216 // Consolidates the cached devices list with the list of currently connected | 193 // Consolidates the cached devices list with the list of currently connected |
| 217 // devices in the system |names_snapshot|. Retrieves the supported formats of | 194 // devices in the system |names_snapshot|. Retrieves the supported formats of |
| 218 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). | 195 // the new devices and sends the new cache to OnDevicesInfoEnumerated(). |
| 219 void ConsolidateDevicesInfoOnDeviceThread( | 196 void ConsolidateDevicesInfoOnDeviceThread( |
| 220 base::Callback<void(const media::VideoCaptureDeviceInfos&)> | 197 base::Callback<void(const media::VideoCaptureDeviceInfos&)> |
| 221 on_devices_enumerated_callback, | 198 on_devices_enumerated_callback, |
| 222 MediaStreamType stream_type, | 199 MediaStreamType stream_type, |
| 223 const media::VideoCaptureDeviceInfos& old_device_info_cache, | 200 const media::VideoCaptureDeviceInfos& old_device_info_cache, |
| 224 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); | 201 std::unique_ptr<VideoCaptureDevice::Names> names_snapshot); |
| 225 | 202 |
| 203 // Checks to see if |entry| has no clients left on its controller. If so, | |
| 204 // remove it from the list of devices, and delete it asynchronously. |entry| | |
| 205 // may be freed by this function. | |
| 206 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | |
| 207 | |
| 208 // Retrieve the VideoCaptureDevice associated to |session_id|, or nullptr | |
| 209 // if not found. | |
| 210 VideoCaptureDevice* GetVideoCaptureDeviceBySessionId(int session_id); | |
| 211 | |
| 212 // Finds a DeviceEntry in different ways: by its device ID and type (if it is | |
|
emircan
2016/06/30 18:24:05
For consistency; s/device ID/|device_id| (also cha
mcasas
2016/06/30 21:51:24
Done.
| |
| 213 // already opened), by its |controller| or by its |serial_id|. In all cases, | |
| 214 // if not found, nullptr is returned. | |
| 215 DeviceEntry* GetDeviceEntryByTypeAndId(MediaStreamType type, | |
| 216 const std::string& id) const; | |
| 217 DeviceEntry* GetDeviceEntryByController( | |
| 218 const VideoCaptureController* controller) const; | |
| 219 DeviceEntry* GetDeviceEntryBySerialId(int serial_id) const; | |
| 220 | |
| 221 // Finds the device info by |id| in |devices_info_cache_|, or nullptr. | |
| 222 media::VideoCaptureDeviceInfo* GetDeviceInfoById(const std::string& id); | |
| 223 | |
| 224 // Finds a DeviceEntry entry for the indicated session, creating a fresh one | |
| 225 // if necessary. Returns NULL if the session id is invalid. | |
|
emircan
2016/06/30 18:24:04
s/session id/|capture_session_id|
mcasas
2016/06/30 21:51:24
Done.
| |
| 226 DeviceEntry* GetOrCreateDeviceEntry( | |
| 227 media::VideoCaptureSessionId capture_session_id, | |
| 228 const media::VideoCaptureParams& params); | |
| 229 | |
| 226 // Starting a capture device can take 1-2 seconds. | 230 // Starting a capture device can take 1-2 seconds. |
| 227 // To avoid multiple unnecessary start/stop commands to the OS, each start | 231 // To avoid multiple unnecessary start/stop commands to the OS, each start |
| 228 // request is queued in |device_start_queue_|. | 232 // request is queued in |device_start_queue_|. |
| 229 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a | 233 // QueueStartDevice creates a new entry in |device_start_queue_| and posts a |
| 230 // request to start the device on the device thread unless there is | 234 // request to start the device on the device thread unless there is |
| 231 // another request pending start. | 235 // another request pending start. |
| 232 void QueueStartDevice(media::VideoCaptureSessionId session_id, | 236 void QueueStartDevice(media::VideoCaptureSessionId session_id, |
| 233 DeviceEntry* entry, | 237 DeviceEntry* entry, |
| 234 const media::VideoCaptureParams& params); | 238 const media::VideoCaptureParams& params); |
| 235 void OnDeviceStarted(int serial_id, | 239 void OnDeviceStarted(int serial_id, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 252 std::unique_ptr<VideoCaptureDevice::Client> client); | 256 std::unique_ptr<VideoCaptureDevice::Client> client); |
| 253 | 257 |
| 254 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( | 258 std::unique_ptr<VideoCaptureDevice> DoStartDesktopCaptureOnDeviceThread( |
| 255 const std::string& device_id, | 259 const std::string& device_id, |
| 256 const media::VideoCaptureParams& params, | 260 const media::VideoCaptureParams& params, |
| 257 std::unique_ptr<VideoCaptureDevice::Client> client); | 261 std::unique_ptr<VideoCaptureDevice::Client> client); |
| 258 | 262 |
| 259 // Stops and destroys the VideoCaptureDevice held in |device|. | 263 // Stops and destroys the VideoCaptureDevice held in |device|. |
| 260 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device); | 264 void DoStopDeviceOnDeviceThread(std::unique_ptr<VideoCaptureDevice> device); |
| 261 | 265 |
| 262 media::VideoCaptureDeviceInfo* FindDeviceInfoById( | |
| 263 const std::string& id, | |
| 264 media::VideoCaptureDeviceInfos& device_vector); | |
| 265 | |
| 266 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); | 266 void MaybePostDesktopCaptureWindowId(media::VideoCaptureSessionId session_id); |
| 267 void SetDesktopCaptureWindowIdOnDeviceThread( | 267 void SetDesktopCaptureWindowIdOnDeviceThread( |
| 268 media::VideoCaptureDevice* device, | 268 media::VideoCaptureDevice* device, |
| 269 gfx::NativeViewId window_id); | 269 gfx::NativeViewId window_id); |
| 270 | 270 |
| 271 #if defined(OS_MACOSX) | 271 #if defined(OS_MACOSX) |
| 272 // Called on the IO thread after the device layer has been initialized on Mac. | 272 // Called on the IO thread after the device layer has been initialized on Mac. |
| 273 // Sets |capture_device_api_initialized_| to true and then executes and_then. | 273 // Sets |capture_device_api_initialized_| to true and then executes and_then. |
| 274 void OnDeviceLayerInitialized(const base::Closure& and_then); | 274 void OnDeviceLayerInitialized(const base::Closure& and_then); |
| 275 | 275 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 301 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; | 301 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; |
| 302 | 302 |
| 303 // Only accessed on Browser::IO thread. | 303 // Only accessed on Browser::IO thread. |
| 304 MediaStreamProviderListener* listener_; | 304 MediaStreamProviderListener* listener_; |
| 305 media::VideoCaptureSessionId new_capture_session_id_; | 305 media::VideoCaptureSessionId new_capture_session_id_; |
| 306 | 306 |
| 307 // An entry is kept in this map for every session that has been created via | 307 // An entry is kept in this map for every session that has been created via |
| 308 // the Open() entry point. The keys are session_id's. This map is used to | 308 // the Open() entry point. The keys are session_id's. This map is used to |
| 309 // determine which device to use when StartCaptureForClient() occurs. Used | 309 // determine which device to use when StartCaptureForClient() occurs. Used |
| 310 // only on the IO thread. | 310 // only on the IO thread. |
| 311 typedef std::map<media::VideoCaptureSessionId, MediaStreamDevice> SessionMap; | 311 using SessionMap = std::map<media::VideoCaptureSessionId, MediaStreamDevice>; |
| 312 SessionMap sessions_; | 312 SessionMap sessions_; |
|
emircan
2016/06/30 18:24:05
Using-declarations should be in the beginning of t
mcasas
2016/06/30 21:51:24
Done.
| |
| 313 | 313 |
| 314 // Currently opened devices. The device may or may not be started. This member | 314 // Currently opened device-controller couples. The device may or may not be |
|
emircan
2016/06/30 18:24:04
It sounds like this is a vector of pairs which is
mcasas
2016/06/30 21:51:24
Done.
| |
| 315 // is only accessed on IO tbhread. | 315 // started. This member is only accessed on IO thread. |
| 316 typedef ScopedVector<DeviceEntry> DeviceEntries; | |
| 317 DeviceEntries devices_; | 316 DeviceEntries devices_; |
| 318 | 317 |
| 319 class CaptureDeviceStartRequest; | 318 class CaptureDeviceStartRequest; |
| 320 typedef std::list<CaptureDeviceStartRequest> DeviceStartQueue; | 319 using DeviceStartQueue = std::list<CaptureDeviceStartRequest>; |
|
emircan
2016/06/30 18:24:04
Ditto.
mcasas
2016/06/30 21:51:24
Done.
| |
| 321 DeviceStartQueue device_start_queue_; | 320 DeviceStartQueue device_start_queue_; |
| 322 | 321 |
| 323 // Device creation factory injected on construction from MediaStreamManager or | 322 // Device creation factory injected on construction from MediaStreamManager or |
| 324 // from the test harness. | 323 // from the test harness. |
| 325 std::unique_ptr<media::VideoCaptureDeviceFactory> | 324 std::unique_ptr<media::VideoCaptureDeviceFactory> |
| 326 video_capture_device_factory_; | 325 video_capture_device_factory_; |
| 327 | 326 |
| 328 // Local cache of the enumerated video capture devices' names and capture | 327 // Local cache of the enumerated video capture devices' names and capture |
| 329 // supported formats. A snapshot of the current devices and their capabilities | 328 // supported formats. A snapshot of the current devices and their capabilities |
| 330 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and | 329 // is composed in VideoCaptureDeviceFactory::EnumerateDeviceNames() and |
| 331 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update | 330 // ConsolidateDevicesInfoOnDeviceThread(), and this snapshot is used to update |
| 332 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will | 331 // this list in OnDevicesInfoEnumerated(). GetDeviceSupportedFormats() will |
| 333 // use this list if the device is not started, otherwise it will retrieve the | 332 // use this list if the device is not started, otherwise it will retrieve the |
| 334 // active device capture format from the VideoCaptureController associated. | 333 // active device capture format from the VideoCaptureController associated. |
| 335 media::VideoCaptureDeviceInfos devices_info_cache_; | 334 media::VideoCaptureDeviceInfos devices_info_cache_; |
| 336 | 335 |
| 337 // Map used by DesktopCapture. | 336 // Map used by DesktopCapture. |
| 338 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> | 337 std::map<media::VideoCaptureSessionId, gfx::NativeViewId> |
| 339 notification_window_ids_; | 338 notification_window_ids_; |
| 340 | 339 |
| 341 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 340 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
| 342 }; | 341 }; |
| 343 | 342 |
| 344 } // namespace content | 343 } // namespace content |
| 345 | 344 |
| 346 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 345 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
| OLD | NEW |