| 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 <map> | 15 #include <map> |
| 16 #include <set> | 16 #include <set> |
| 17 #include <string> | 17 #include <string> |
| 18 | 18 |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_vector.h" |
| 20 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 21 #include "base/process/process_handle.h" | 22 #include "base/process/process_handle.h" |
| 22 #include "content/browser/renderer_host/media/media_stream_provider.h" | 23 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 23 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 24 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
| 24 #include "content/common/content_export.h" | 25 #include "content/common/content_export.h" |
| 25 #include "content/common/media/media_stream_options.h" | 26 #include "content/common/media/media_stream_options.h" |
| 26 #include "media/video/capture/video_capture_device.h" | 27 #include "media/video/capture/video_capture_device.h" |
| 27 #include "media/video/capture/video_capture_types.h" | 28 #include "media/video/capture/video_capture_types.h" |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 virtual void Close(int capture_session_id) OVERRIDE; | 53 virtual void Close(int capture_session_id) OVERRIDE; |
| 53 | 54 |
| 54 // Used by unit test to make sure a fake device is used instead of a real | 55 // Used by unit test to make sure a fake device is used instead of a real |
| 55 // video capture device. Due to timing requirements, the function must be | 56 // video capture device. Due to timing requirements, the function must be |
| 56 // called before EnumerateDevices and Open. | 57 // called before EnumerateDevices and Open. |
| 57 void UseFakeDevice(); | 58 void UseFakeDevice(); |
| 58 | 59 |
| 59 // Called by VideoCaptureHost to locate a capture device for |capture_params|, | 60 // Called by VideoCaptureHost to locate a capture device for |capture_params|, |
| 60 // adding the Host as a client of the device's controller if successful. The | 61 // adding the Host as a client of the device's controller if successful. The |
| 61 // value of |capture_params.session_id| controls which device is selected; | 62 // value of |capture_params.session_id| controls which device is selected; |
| 62 // this value should be a session id previously returned by Open(). | 63 // this value should be a session id previously returned by Open(). The device |
| 64 // capabilities are reduced to |capture_params|. |
| 63 // | 65 // |
| 64 // If the device is not already started (i.e., no other client is currently | 66 // If the device is not already started (i.e., no other client is currently |
| 65 // capturing from this device), this call will cause a VideoCaptureController | 67 // capturing from this device), this call will cause a VideoCaptureController |
| 66 // and VideoCaptureDevice to be created, possibly asynchronously. | 68 // and VideoCaptureDevice to be created, possibly asynchronously. |
| 67 // | 69 // |
| 68 // On success, the controller is returned via calling |done_cb|, indicating | 70 // On success, the controller is returned via calling |done_cb|, indicating |
| 69 // that the client was successfully added. A NULL controller is passed to | 71 // that the client was successfully added. A NULL controller is passed to |
| 70 // the callback on failure. | 72 // the callback on failure. |
| 71 void StartCaptureForClient(const media::VideoCaptureParams& capture_params, | 73 void StartCaptureForClient(const media::VideoCaptureParams& capture_params, |
| 72 base::ProcessHandle client_render_process, | 74 base::ProcessHandle client_render_process, |
| 73 VideoCaptureControllerID client_id, | 75 VideoCaptureControllerID client_id, |
| 74 VideoCaptureControllerEventHandler* client_handler, | 76 VideoCaptureControllerEventHandler* client_handler, |
| 75 const DoneCB& done_cb); | 77 const DoneCB& done_cb); |
| 76 | 78 |
| 77 // Called by VideoCaptureHost to remove |client_handler|. If this is the last | 79 // Called by VideoCaptureHost to remove |client_handler|. If this is the last |
| 78 // client of the device, the |controller| and its VideoCaptureDevice may be | 80 // client of the device, the |controller| and its VideoCaptureDevice may be |
| 79 // destroyed. The client must not access |controller| after calling this | 81 // destroyed. The client must not access |controller| after calling this |
| 80 // function. | 82 // function. |
| 81 void StopCaptureForClient(VideoCaptureController* controller, | 83 void StopCaptureForClient(VideoCaptureController* controller, |
| 82 VideoCaptureControllerID client_id, | 84 VideoCaptureControllerID client_id, |
| 83 VideoCaptureControllerEventHandler* client_handler); | 85 VideoCaptureControllerEventHandler* client_handler); |
| 84 | 86 |
| 87 // Retrieve the available capture capabilities for a particular device. The |
| 88 // capabilities are cached during device(s) enumeration and updated as clients |
| 89 // open/close the device(s). |
| 90 void GetDeviceCapabilities(int capture_session_id, |
| 91 media::VideoCaptureCapabilities* capabilities); |
| 92 |
| 85 private: | 93 private: |
| 86 virtual ~VideoCaptureManager(); | 94 virtual ~VideoCaptureManager(); |
| 87 struct DeviceEntry; | 95 struct DeviceEntry; |
| 96 struct DeviceInfo; |
| 88 | 97 |
| 89 // Check to see if |entry| has no clients left on its controller. If so, | 98 // Check to see if |entry| has no clients left on its controller. If so, |
| 90 // remove it from the list of devices, and delete it asynchronously. |entry| | 99 // remove it from the list of devices, and delete it asynchronously. |entry| |
| 91 // may be freed by this function. | 100 // may be freed by this function. |
| 92 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 101 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
| 93 | 102 |
| 94 // Helpers to report an event to our Listener. | 103 // Helpers to report an event to our Listener. |
| 95 void OnOpened(MediaStreamType type, int capture_session_id); | 104 void OnOpened(MediaStreamType type, int capture_session_id); |
| 96 void OnClosed(MediaStreamType type, int capture_session_id); | 105 void OnClosed(MediaStreamType type, int capture_session_id); |
| 97 void OnDevicesEnumerated(MediaStreamType stream_type, | 106 void OnDeviceNamesEnumerated( |
| 98 const media::VideoCaptureDevice::Names& names); | 107 MediaStreamType stream_type, |
| 108 const media::VideoCaptureDevice::Names& names_snapshot); |
| 109 void OnDeviceNameAndCapabilitiesEnumerated( |
| 110 MediaStreamType stream_type, |
| 111 const media::VideoCaptureDevice::Names& names_snapshot, |
| 112 ScopedVector<DeviceInfo> new_devices_info); |
| 99 | 113 |
| 100 // Find a DeviceEntry by its device ID and type, if it is already opened. | 114 // Find a DeviceEntry by its device ID and type, if it is already opened. |
| 101 DeviceEntry* GetDeviceEntryForMediaStreamDevice( | 115 DeviceEntry* GetDeviceEntryForMediaStreamDevice( |
| 102 const MediaStreamDevice& device_info); | 116 const MediaStreamDevice& device_info); |
| 103 | 117 |
| 104 // Find a DeviceEntry entry for the indicated session, creating a fresh one | 118 // Find a DeviceEntry entry for the indicated session, creating a fresh one |
| 105 // if necessary. Returns NULL if the session id is invalid. | 119 // if necessary. Returns NULL if the session id is invalid. |
| 106 DeviceEntry* GetOrCreateDeviceEntry(int capture_session_id); | 120 DeviceEntry* GetOrCreateDeviceEntry(int capture_session_id); |
| 107 | 121 |
| 108 // Find the DeviceEntry that owns a particular controller pointer. | 122 // Find the DeviceEntry that owns a particular controller pointer. |
| 109 DeviceEntry* GetDeviceEntryForController( | 123 DeviceEntry* GetDeviceEntryForController( |
| 110 const VideoCaptureController* controller); | 124 const VideoCaptureController* controller); |
| 111 | 125 |
| 112 bool IsOnDeviceThread() const; | 126 bool IsOnDeviceThread() const; |
| 113 | 127 |
| 114 // Queries and returns the available device IDs. | 128 // Queries and returns the available device IDs. |
| 115 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( | 129 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( |
| 116 MediaStreamType stream_type); | 130 MediaStreamType stream_type); |
| 117 | 131 |
| 132 // Queries and returns the capabilities of all the devices in |device_names|. |
| 133 ScopedVector<DeviceInfo> GetDevicesCapabilitiesOnDeviceThread( |
| 134 const media::VideoCaptureDevice::Names& device_names); |
| 135 |
| 118 // Create and Start a new VideoCaptureDevice, storing the result in | 136 // Create and Start a new VideoCaptureDevice, storing the result in |
| 119 // |entry->video_capture_device|. Ownership of |client| passes to | 137 // |entry->video_capture_device|. Ownership of |client| passes to |
| 120 // the device. | 138 // the device. |
| 121 void DoStartDeviceOnDeviceThread( | 139 void DoStartDeviceOnDeviceThread( |
| 122 DeviceEntry* entry, | 140 DeviceEntry* entry, |
| 141 const media::VideoCaptureDevice::Name* device_name, |
| 123 const media::VideoCaptureCapability& capture_params, | 142 const media::VideoCaptureCapability& capture_params, |
| 124 scoped_ptr<media::VideoCaptureDevice::Client> client); | 143 scoped_ptr<media::VideoCaptureDevice::Client> client); |
| 125 | 144 |
| 126 // Stop and destroy the VideoCaptureDevice held in | 145 // Stop and destroy the VideoCaptureDevice held in |
| 127 // |entry->video_capture_device|. | 146 // |entry->video_capture_device|. |
| 128 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); | 147 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); |
| 129 | 148 |
| 149 DeviceInfo* FindDeviceInfoById(const std::string& id); |
| 150 |
| 130 // The message loop of media stream device thread, where VCD's live. | 151 // The message loop of media stream device thread, where VCD's live. |
| 131 scoped_refptr<base::MessageLoopProxy> device_loop_; | 152 scoped_refptr<base::MessageLoopProxy> device_loop_; |
| 132 | 153 |
| 133 // Only accessed on Browser::IO thread. | 154 // Only accessed on Browser::IO thread. |
| 134 MediaStreamProviderListener* listener_; | 155 MediaStreamProviderListener* listener_; |
| 135 int new_capture_session_id_; | 156 int new_capture_session_id_; |
| 136 | 157 |
| 137 // An entry is kept in this map for every session that has been created via | 158 // An entry is kept in this map for every session that has been created via |
| 138 // the Open() entry point. The keys are session_id's. This map is used to | 159 // the Open() entry point. The keys are session_id's. This map is used to |
| 139 // determine which device to use when StartCaptureForClient() occurs. Used | 160 // determine which device to use when StartCaptureForClient() occurs. Used |
| (...skipping 24 matching lines...) Expand all Loading... |
| 164 // The capture device. Only used from the device thread. | 185 // The capture device. Only used from the device thread. |
| 165 scoped_ptr<media::VideoCaptureDevice> video_capture_device; | 186 scoped_ptr<media::VideoCaptureDevice> video_capture_device; |
| 166 }; | 187 }; |
| 167 typedef std::set<DeviceEntry*> DeviceEntries; | 188 typedef std::set<DeviceEntry*> DeviceEntries; |
| 168 DeviceEntries devices_; | 189 DeviceEntries devices_; |
| 169 | 190 |
| 170 // Set to true if using fake video capture devices for testing, false by | 191 // Set to true if using fake video capture devices for testing, false by |
| 171 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. | 192 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. |
| 172 bool use_fake_device_; | 193 bool use_fake_device_; |
| 173 | 194 |
| 174 // We cache the enumerated video capture devices in | 195 // This struct is a convenient wrap of a devices' name and associated video |
| 175 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID | 196 // capture capabilities. |
| 176 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the | 197 struct DeviceInfo { |
| 177 // device thread. | 198 DeviceInfo(); |
| 178 media::VideoCaptureDevice::Names video_capture_devices_; | 199 DeviceInfo(const media::VideoCaptureDevice::Name& name, |
| 200 const media::VideoCaptureCapabilities& capabilities); |
| 201 ~DeviceInfo(); |
| 202 |
| 203 bool device_in_use_; |
| 204 media::VideoCaptureCapability capability_in_use_; |
| 205 media::VideoCaptureDevice::Name name_; |
| 206 media::VideoCaptureCapabilities capabilities_; |
| 207 }; |
| 208 // Local cache of the enumerated video capture devices' names and capture |
| 209 // capabilities. The creation/update works as following: EnumeratedDevices() |
| 210 // jumps to GetAvailableDevicesOnDeviceThread() and OnDeviceNamesEnumerated(). |
| 211 // From there, a posttask-reply GetDevicesCapabilitiesOnDeviceThread() and |
| 212 // OnDeviceNameAndCapabilitiesEnumerated() is carried out. From the last one, |
| 213 // the |listener_| OnDevicesEnumerated is called. The cache is only used in |
| 214 // Browser::IO thread. |
| 215 ScopedVector<DeviceInfo> devices_info_cache_; |
| 179 | 216 |
| 180 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 217 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
| 181 }; | 218 }; |
| 182 | 219 |
| 183 } // namespace content | 220 } // namespace content |
| 184 | 221 |
| 185 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 222 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
| OLD | NEW |