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 <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/weak_ptr.h" | |
| 20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
| 21 #include "content/browser/renderer_host/media/media_stream_provider.h" | 22 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 22 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 23 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
| 23 #include "content/common/content_export.h" | 24 #include "content/common/content_export.h" |
| 24 #include "content/common/media/media_stream_options.h" | 25 #include "content/common/media/media_stream_options.h" |
| 25 #include "media/video/capture/video_capture_device.h" | 26 #include "media/video/capture/video_capture_device.h" |
| 26 #include "media/video/capture/video_capture_types.h" | 27 #include "media/video/capture/video_capture_types.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 class MockVideoCaptureManager; | |
| 30 class VideoCaptureController; | 30 class VideoCaptureController; |
| 31 class VideoCaptureControllerEventHandler; | 31 class VideoCaptureControllerEventHandler; |
| 32 | 32 |
| 33 // VideoCaptureManager opens/closes and start/stops video capture devices. | 33 // VideoCaptureManager opens/closes and start/stops video capture devices. |
| 34 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { | 34 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { |
| 35 public: | 35 public: |
| 36 // Callback used to signal the completion of a controller lookup. | |
| 37 typedef base::Callback< | |
| 38 void(const base::WeakPtr<VideoCaptureController>&)> DoneCB; | |
|
Ami GONE FROM CHROMIUM
2013/09/14 00:32:24
"DoneCB"? Oh, so your "callback" signifies being
ncarter (slow)
2013/09/14 01:29:31
One must pick one's battles.
| |
| 39 | |
| 36 // Calling |Start| of this id will open the first device, even though open has | 40 // Calling |Start| of this id will open the first device, even though open has |
| 37 // not been called. This is used to be able to use video capture devices | 41 // not been called. This is used to be able to use video capture devices |
| 38 // before MediaStream is implemented in Chrome and WebKit. | 42 // before MediaStream is implemented in Chrome and WebKit. |
| 39 enum { kStartOpenSessionId = 1 }; | 43 enum { kStartOpenSessionId = 1 }; |
| 40 | 44 |
| 41 VideoCaptureManager(); | 45 VideoCaptureManager(); |
| 42 | 46 |
| 43 // Implements MediaStreamProvider. | 47 // Implements MediaStreamProvider. |
| 44 virtual void Register(MediaStreamProviderListener* listener, | 48 virtual void Register(MediaStreamProviderListener* listener, |
| 45 base::MessageLoopProxy* device_thread_loop) OVERRIDE; | 49 base::MessageLoopProxy* device_thread_loop) OVERRIDE; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 62 // value of |capture_params.session_id| controls which device is selected; | 66 // value of |capture_params.session_id| controls which device is selected; |
| 63 // this value should be a session id previously returned by Open(). | 67 // this value should be a session id previously returned by Open(). |
| 64 // | 68 // |
| 65 // If the device is not already started (i.e., no other client is currently | 69 // If the device is not already started (i.e., no other client is currently |
| 66 // capturing from this device), this call will cause a VideoCaptureController | 70 // capturing from this device), this call will cause a VideoCaptureController |
| 67 // and VideoCaptureDevice to be created, possibly asynchronously. | 71 // and VideoCaptureDevice to be created, possibly asynchronously. |
| 68 // | 72 // |
| 69 // On success, the controller is returned via calling |done_cb|, indicating | 73 // On success, the controller is returned via calling |done_cb|, indicating |
| 70 // that the client was successfully added. A NULL controller is passed to | 74 // that the client was successfully added. A NULL controller is passed to |
| 71 // the callback on failure. | 75 // the callback on failure. |
| 72 void StartCaptureForClient( | 76 void StartCaptureForClient(const media::VideoCaptureParams& capture_params, |
| 73 const media::VideoCaptureParams& capture_params, | 77 base::ProcessHandle client_render_process, |
| 74 base::ProcessHandle client_render_process, | 78 VideoCaptureControllerID client_id, |
| 75 VideoCaptureControllerID client_id, | 79 VideoCaptureControllerEventHandler* client_handler, |
| 76 VideoCaptureControllerEventHandler* client_handler, | 80 DoneCB done_cb); |
|
Ami GONE FROM CHROMIUM
2013/09/14 00:32:24
Shouldn't callbacks be passed by const&?
ncarter (slow)
2013/09/14 01:29:31
Done.
| |
| 77 base::Callback<void(VideoCaptureController*)> done_cb); | |
| 78 | 81 |
| 79 // Called by VideoCaptureHost to remove |client_handler|. If this is the last | 82 // Called by VideoCaptureHost to remove |client_handler|. If this is the last |
| 80 // client of the device, the |controller| and its VideoCaptureDevice may be | 83 // client of the device, the |controller| and its VideoCaptureDevice may be |
| 81 // destroyed. The client must not access |controller| after calling this | 84 // destroyed. The client must not access |controller| after calling this |
| 82 // function. | 85 // function. |
| 83 void StopCaptureForClient(VideoCaptureController* controller, | 86 void StopCaptureForClient(VideoCaptureController* controller, |
| 84 VideoCaptureControllerID client_id, | 87 VideoCaptureControllerID client_id, |
| 85 VideoCaptureControllerEventHandler* client_handler); | 88 VideoCaptureControllerEventHandler* client_handler); |
| 86 | 89 |
| 87 private: | 90 private: |
| 88 virtual ~VideoCaptureManager(); | 91 virtual ~VideoCaptureManager(); |
| 89 struct DeviceEntry; | 92 struct DeviceEntry; |
| 90 | 93 |
| 91 // Helper for the kStartOpenSessionId case. | 94 // Helper for the kStartOpenSessionId case. |
| 92 void OpenAndStartDefaultSession( | 95 void OpenAndStartDefaultSession( |
| 93 const media::VideoCaptureParams& capture_params, | 96 const media::VideoCaptureParams& capture_params, |
| 94 base::ProcessHandle client_render_process, | 97 base::ProcessHandle client_render_process, |
| 95 VideoCaptureControllerID client_id, | 98 VideoCaptureControllerID client_id, |
| 96 VideoCaptureControllerEventHandler* client_handler, | 99 VideoCaptureControllerEventHandler* client_handler, |
| 97 base::Callback<void(VideoCaptureController*)> done_cb, | 100 DoneCB done_cb, |
| 98 const media::VideoCaptureDevice::Names& device_names); | 101 const media::VideoCaptureDevice::Names& device_names); |
| 99 | 102 |
| 100 // Helper routine implementing StartCaptureForClient(). | 103 // Helper routine implementing StartCaptureForClient(). |
| 101 void DoStartCaptureForClient( | 104 void DoStartCaptureForClient( |
| 102 const media::VideoCaptureParams& capture_params, | 105 const media::VideoCaptureParams& capture_params, |
| 103 base::ProcessHandle client_render_process, | 106 base::ProcessHandle client_render_process, |
| 104 VideoCaptureControllerID client_id, | 107 VideoCaptureControllerID client_id, |
| 105 VideoCaptureControllerEventHandler* client_handler, | 108 VideoCaptureControllerEventHandler* client_handler, |
| 106 base::Callback<void(VideoCaptureController*)> done_cb); | 109 DoneCB done_cb); |
| 107 | 110 |
| 108 // Check to see if |entry| has no clients left on its controller. If so, | 111 // Check to see if |entry| has no clients left on its controller. If so, |
| 109 // remove it from the list of devices, and delete it asynchronously. |entry| | 112 // remove it from the list of devices, and delete it asynchronously. |entry| |
| 110 // may be freed by this function. | 113 // may be freed by this function. |
| 111 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); | 114 void DestroyDeviceEntryIfNoClients(DeviceEntry* entry); |
| 112 | 115 |
| 113 // Helpers to report an event to our Listener. | 116 // Helpers to report an event to our Listener. |
| 114 void OnOpened(MediaStreamType type, int capture_session_id); | 117 void OnOpened(MediaStreamType type, int capture_session_id); |
| 115 void OnClosed(MediaStreamType type, int capture_session_id); | 118 void OnClosed(MediaStreamType type, int capture_session_id); |
| 116 void OnDevicesEnumerated(MediaStreamType stream_type, | 119 void OnDevicesEnumerated(MediaStreamType stream_type, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 128 DeviceEntry* GetDeviceEntryForController( | 131 DeviceEntry* GetDeviceEntryForController( |
| 129 const VideoCaptureController* controller); | 132 const VideoCaptureController* controller); |
| 130 | 133 |
| 131 bool IsOnDeviceThread() const; | 134 bool IsOnDeviceThread() const; |
| 132 | 135 |
| 133 // Queries and returns the available device IDs. | 136 // Queries and returns the available device IDs. |
| 134 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( | 137 media::VideoCaptureDevice::Names GetAvailableDevicesOnDeviceThread( |
| 135 MediaStreamType stream_type); | 138 MediaStreamType stream_type); |
| 136 | 139 |
| 137 // Create and Start a new VideoCaptureDevice, storing the result in | 140 // Create and Start a new VideoCaptureDevice, storing the result in |
| 138 // |entry->video_capture_device|. | 141 // |entry->video_capture_device|. Ownership of |handler| passes to |
| 142 // the device. | |
| 139 void DoStartDeviceOnDeviceThread( | 143 void DoStartDeviceOnDeviceThread( |
| 140 DeviceEntry* entry, | 144 DeviceEntry* entry, |
| 141 const media::VideoCaptureCapability& capture_params, | 145 const media::VideoCaptureCapability& capture_params, |
| 142 media::VideoCaptureDevice::EventHandler* controller_as_handler); | 146 scoped_ptr<media::VideoCaptureDevice::EventHandler> handler); |
| 143 | 147 |
| 144 // Stop and destroy the VideoCaptureDevice held in | 148 // Stop and destroy the VideoCaptureDevice held in |
| 145 // |entry->video_capture_device|. | 149 // |entry->video_capture_device|. |
| 146 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); | 150 void DoStopDeviceOnDeviceThread(DeviceEntry* entry); |
| 147 | 151 |
| 148 // Helper to clean up the DeviceEntry* instance, and the | |
| 149 // VideoCaptureController, on the IO thread. Must happen after | |
| 150 // DoStopDeviceOnDeviceThread() destroys the VideoCaptureDevice. It is assumed | |
| 151 // that |dead_device| has already been removed from the |devices_| map. | |
| 152 void FreeDeviceEntryOnIOThread(scoped_ptr<DeviceEntry> dead_device); | |
| 153 | |
| 154 // The message loop of media stream device thread, where VCD's live. | 152 // The message loop of media stream device thread, where VCD's live. |
| 155 scoped_refptr<base::MessageLoopProxy> device_loop_; | 153 scoped_refptr<base::MessageLoopProxy> device_loop_; |
| 156 | 154 |
| 157 // Only accessed on Browser::IO thread. | 155 // Only accessed on Browser::IO thread. |
| 158 MediaStreamProviderListener* listener_; | 156 MediaStreamProviderListener* listener_; |
| 159 int new_capture_session_id_; | 157 int new_capture_session_id_; |
| 160 | 158 |
| 161 // An entry is kept in this map for every session that has been created via | 159 // An entry is kept in this map for every session that has been created via |
| 162 // the Open() entry point. The keys are session_id's. This map is used to | 160 // the Open() entry point. The keys are session_id's. This map is used to |
| 163 // determine which device to use when StartCaptureForClient() occurs. Used | 161 // determine which device to use when StartCaptureForClient() occurs. Used |
| 164 // only on the IO thread. | 162 // only on the IO thread. |
| 165 std::map<int, MediaStreamDevice> sessions_; | 163 std::map<int, MediaStreamDevice> sessions_; |
| 166 | 164 |
| 167 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated | 165 // An entry, kept in a map, that owns a VideoCaptureDevice and its associated |
| 168 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices | 166 // VideoCaptureController. VideoCaptureManager owns all VideoCaptureDevices |
| 169 // and VideoCaptureControllers and is responsible for deleting the instances | 167 // and VideoCaptureControllers and is responsible for deleting the instances |
| 170 // when they are not used any longer. | 168 // when they are not used any longer. |
| 171 // | 169 // |
| 172 // The set of currently started VideoCaptureDevice and VideoCaptureController | 170 // The set of currently started VideoCaptureDevice and VideoCaptureController |
| 173 // objects is only accessed from IO thread, though the DeviceEntry instances | 171 // objects is only accessed from IO thread, though the DeviceEntry instances |
| 174 // themselves may visit to the device thread for device creation and | 172 // themselves may visit to the device thread for device creation and |
| 175 // destruction. | 173 // destruction. |
| 176 struct DeviceEntry { | 174 struct DeviceEntry { |
| 177 DeviceEntry(MediaStreamType stream_type, | 175 DeviceEntry(MediaStreamType stream_type, |
| 178 const std::string& id, | 176 const std::string& id, |
| 179 scoped_refptr<VideoCaptureController> controller); | 177 scoped_ptr<VideoCaptureController> controller); |
| 180 ~DeviceEntry(); | 178 ~DeviceEntry(); |
| 181 | 179 |
| 182 const MediaStreamType stream_type; | 180 const MediaStreamType stream_type; |
| 183 const std::string id; | 181 const std::string id; |
| 184 | 182 |
| 185 // The controller. Only used from the IO thread. | 183 // The controller. Only used from the IO thread. |
| 186 scoped_refptr<VideoCaptureController> video_capture_controller; | 184 scoped_ptr<VideoCaptureController> video_capture_controller; |
| 187 | 185 |
| 188 // The capture device. Only used from the device thread. | 186 // The capture device. Only used from the device thread. |
| 189 scoped_ptr<media::VideoCaptureDevice> video_capture_device; | 187 scoped_ptr<media::VideoCaptureDevice> video_capture_device; |
| 190 }; | 188 }; |
| 191 typedef std::set<DeviceEntry*> DeviceEntries; | 189 typedef std::set<DeviceEntry*> DeviceEntries; |
| 192 DeviceEntries devices_; | 190 DeviceEntries devices_; |
| 193 | 191 |
| 194 // Set to true if using fake video capture devices for testing, false by | 192 // Set to true if using fake video capture devices for testing, false by |
| 195 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. | 193 // default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE device type. |
| 196 bool use_fake_device_; | 194 bool use_fake_device_; |
| 197 | 195 |
| 198 // We cache the enumerated video capture devices in | 196 // We cache the enumerated video capture devices in |
| 199 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID | 197 // GetAvailableDevicesOnDeviceThread() and then later look up the requested ID |
| 200 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the | 198 // when a device is created in DoStartDeviceOnDeviceThread(). Used only on the |
| 201 // device thread. | 199 // device thread. |
| 202 media::VideoCaptureDevice::Names video_capture_devices_; | 200 media::VideoCaptureDevice::Names video_capture_devices_; |
| 203 | 201 |
| 204 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 202 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
| 205 }; | 203 }; |
| 206 | 204 |
| 207 } // namespace content | 205 } // namespace content |
| 208 | 206 |
| 209 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 207 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
| OLD | NEW |