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 // MediaStreamManager is used to open/enumerate media capture devices (video | 5 // MediaStreamManager is used to open/enumerate media capture devices (video |
6 // supported now). Call flow: | 6 // supported now). Call flow: |
7 // 1. GenerateStream is called when a render process wants to use a capture | 7 // 1. GenerateStream is called when a render process wants to use a capture |
8 // device. | 8 // device. |
9 // 2. MediaStreamManager will ask MediaStreamUIController for permission to | 9 // 2. MediaStreamManager will ask MediaStreamUIController for permission to |
10 // use devices and for which device to use. | 10 // use devices and for which device to use. |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 #include <map> | 26 #include <map> |
27 #include <string> | 27 #include <string> |
28 | 28 |
29 #include "base/basictypes.h" | 29 #include "base/basictypes.h" |
30 #include "base/memory/scoped_ptr.h" | 30 #include "base/memory/scoped_ptr.h" |
31 #include "base/memory/ref_counted.h" | 31 #include "base/memory/ref_counted.h" |
32 #include "base/message_loop.h" | 32 #include "base/message_loop.h" |
33 #include "base/system_monitor/system_monitor.h" | 33 #include "base/system_monitor/system_monitor.h" |
34 #include "content/browser/renderer_host/media/media_stream_provider.h" | 34 #include "content/browser/renderer_host/media/media_stream_provider.h" |
35 #include "content/browser/renderer_host/media/media_stream_settings_requester.h" | |
36 #include "content/common/media/media_stream_options.h" | 35 #include "content/common/media/media_stream_options.h" |
37 #include "content/common/content_export.h" | 36 #include "content/common/content_export.h" |
38 | 37 |
39 namespace base { | 38 namespace base { |
40 class Thread; | 39 class Thread; |
41 } | 40 } |
42 | 41 |
43 namespace media { | 42 namespace media { |
44 class AudioManager; | 43 class AudioManager; |
45 } | 44 } |
46 | 45 |
47 namespace content { | 46 namespace content { |
48 | 47 |
49 class AudioInputDeviceManager; | 48 class AudioInputDeviceManager; |
| 49 class FakeMediaStreamUIProxy; |
50 class MediaStreamDeviceSettings; | 50 class MediaStreamDeviceSettings; |
51 class MediaStreamRequester; | 51 class MediaStreamRequester; |
52 class MediaStreamUIController; | 52 class MediaStreamUIProxy; |
53 class VideoCaptureManager; | 53 class VideoCaptureManager; |
54 | 54 |
55 // MediaStreamManager is used to generate and close new media devices, not to | 55 // MediaStreamManager is used to generate and close new media devices, not to |
56 // start the media flow. | 56 // start the media flow. |
57 // The classes requesting new media streams are answered using | 57 // The classes requesting new media streams are answered using |
58 // MediaStreamManager::Listener. | 58 // MediaStreamManager::Listener. |
59 class CONTENT_EXPORT MediaStreamManager | 59 class CONTENT_EXPORT MediaStreamManager |
60 : public MediaStreamProviderListener, | 60 : public MediaStreamProviderListener, |
61 public base::MessageLoop::DestructionObserver, | 61 public base::MessageLoop::DestructionObserver, |
62 public SettingsRequester, | |
63 public base::SystemMonitor::DevicesChangedObserver { | 62 public base::SystemMonitor::DevicesChangedObserver { |
64 public: | 63 public: |
| 64 // Callback to deliver the result of a media request. |label| is the string |
| 65 // to identify the request, |
| 66 typedef base::Callback<void(const MediaStreamDevices& devices, |
| 67 scoped_ptr<MediaStreamUIProxy> ui)> |
| 68 MediaRequestResponseCallback; |
| 69 |
65 explicit MediaStreamManager(media::AudioManager* audio_manager); | 70 explicit MediaStreamManager(media::AudioManager* audio_manager); |
66 virtual ~MediaStreamManager(); | 71 virtual ~MediaStreamManager(); |
67 | 72 |
68 // Used to access VideoCaptureManager. | 73 // Used to access VideoCaptureManager. |
69 VideoCaptureManager* video_capture_manager(); | 74 VideoCaptureManager* video_capture_manager(); |
70 | 75 |
71 // Used to access AudioInputDeviceManager. | 76 // Used to access AudioInputDeviceManager. |
72 AudioInputDeviceManager* audio_input_device_manager(); | 77 AudioInputDeviceManager* audio_input_device_manager(); |
73 | 78 |
74 // Creates a new media access request which is identified by a unique string | 79 // Creates a new media access request which is identified by a unique string |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // Open a device identified by |device_id|. |type| must be either | 120 // Open a device identified by |device_id|. |type| must be either |
116 // MEDIA_DEVICE_AUDIO_CAPTURE or MEDIA_DEVICE_VIDEO_CAPTURE. | 121 // MEDIA_DEVICE_AUDIO_CAPTURE or MEDIA_DEVICE_VIDEO_CAPTURE. |
117 // The request is identified using string returned to the caller. | 122 // The request is identified using string returned to the caller. |
118 std::string OpenDevice(MediaStreamRequester* requester, | 123 std::string OpenDevice(MediaStreamRequester* requester, |
119 int render_process_id, | 124 int render_process_id, |
120 int render_view_id, | 125 int render_view_id, |
121 const std::string& device_id, | 126 const std::string& device_id, |
122 MediaStreamType type, | 127 MediaStreamType type, |
123 const GURL& security_origin); | 128 const GURL& security_origin); |
124 | 129 |
125 // Signals the UI that the devices are opened. | |
126 // Users are responsible for calling NotifyUIDevicesClosed when the devices | |
127 // are not used anymore, otherwise UI will leak. | |
128 void NotifyUIDevicesOpened(const std::string& label); | |
129 | |
130 // Signals the UI that the devices are being closed. | |
131 void NotifyUIDevicesClosed(const std::string& label); | |
132 | |
133 // Implements MediaStreamProviderListener. | 130 // Implements MediaStreamProviderListener. |
134 virtual void Opened(MediaStreamType stream_type, | 131 virtual void Opened(MediaStreamType stream_type, |
135 int capture_session_id) OVERRIDE; | 132 int capture_session_id) OVERRIDE; |
136 virtual void Closed(MediaStreamType stream_type, | 133 virtual void Closed(MediaStreamType stream_type, |
137 int capture_session_id) OVERRIDE; | 134 int capture_session_id) OVERRIDE; |
138 virtual void DevicesEnumerated(MediaStreamType stream_type, | 135 virtual void DevicesEnumerated(MediaStreamType stream_type, |
139 const StreamDeviceInfoArray& devices) OVERRIDE; | 136 const StreamDeviceInfoArray& devices) OVERRIDE; |
140 virtual void Error(MediaStreamType stream_type, | 137 virtual void Error(MediaStreamType stream_type, |
141 int capture_session_id, | 138 int capture_session_id, |
142 MediaStreamProviderError error) OVERRIDE; | 139 MediaStreamProviderError error) OVERRIDE; |
143 | 140 |
144 // Implements SettingsRequester. | |
145 virtual void DevicesAccepted(const std::string& label, | |
146 const StreamDeviceInfoArray& devices) OVERRIDE; | |
147 virtual void SettingsError(const std::string& label) OVERRIDE; | |
148 virtual void StopStreamFromUI(const std::string& label) OVERRIDE; | |
149 virtual void GetAvailableDevices(MediaStreamDevices* devices) OVERRIDE; | |
150 | |
151 // Implements base::SystemMonitor::DevicesChangedObserver. | 141 // Implements base::SystemMonitor::DevicesChangedObserver. |
152 virtual void OnDevicesChanged( | 142 virtual void OnDevicesChanged( |
153 base::SystemMonitor::DeviceType device_type) OVERRIDE; | 143 base::SystemMonitor::DeviceType device_type) OVERRIDE; |
154 | 144 |
155 // Used by unit test to make sure fake devices are used instead of a real | 145 // Used by unit test to make sure fake devices are used instead of a real |
156 // devices, which is needed for server based testing. | 146 // devices, which is needed for server based testing. |
157 void UseFakeDevice(); | 147 void UseFakeDevice(); |
158 | 148 |
159 // Called by the unittests to specify fake UI that should be used for next | 149 // Called by the unittests to specify fake UI that should be used for next |
160 // generated stream. | 150 // generated stream. |
161 void UseFakeUI(scoped_ptr<MediaStreamUI> fake_ui); | 151 void UseFakeUI(scoped_ptr<FakeMediaStreamUIProxy> fake_ui); |
162 | 152 |
163 // This object gets deleted on the UI thread after the IO thread has been | 153 // This object gets deleted on the UI thread after the IO thread has been |
164 // destroyed. So we need to know when IO thread is being destroyed so that | 154 // destroyed. So we need to know when IO thread is being destroyed so that |
165 // we can delete VideoCaptureManager and AudioInputDeviceManager. | 155 // we can delete VideoCaptureManager and AudioInputDeviceManager. |
166 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 156 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
167 | 157 |
168 private: | 158 private: |
169 // Contains all data needed to keep track of requests. | 159 // Contains all data needed to keep track of requests. |
170 class DeviceRequest; | 160 class DeviceRequest; |
171 | 161 |
(...skipping 10 matching lines...) Expand all Loading... |
182 | 172 |
183 // Initializes the device managers on IO thread. Auto-starts the device | 173 // Initializes the device managers on IO thread. Auto-starts the device |
184 // thread and registers this as a listener with the device managers. | 174 // thread and registers this as a listener with the device managers. |
185 void InitializeDeviceManagersOnIOThread(); | 175 void InitializeDeviceManagersOnIOThread(); |
186 | 176 |
187 // Helper for sending up-to-date device lists to media observer when a | 177 // Helper for sending up-to-date device lists to media observer when a |
188 // capture device is plugged in or unplugged. | 178 // capture device is plugged in or unplugged. |
189 void NotifyDevicesChanged(MediaStreamType stream_type, | 179 void NotifyDevicesChanged(MediaStreamType stream_type, |
190 const StreamDeviceInfoArray& devices); | 180 const StreamDeviceInfoArray& devices); |
191 | 181 |
| 182 |
| 183 void HandleAccessRequestResponse(const std::string& label, |
| 184 const MediaStreamDevices& devices); |
| 185 void StopStreamFromUI(const std::string& label); |
| 186 |
192 // Helpers. | 187 // Helpers. |
193 bool RequestDone(const DeviceRequest& request) const; | 188 bool RequestDone(const DeviceRequest& request) const; |
194 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); | 189 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); |
195 void StartEnumeration(DeviceRequest* request); | 190 void StartEnumeration(DeviceRequest* request); |
196 std::string AddRequest(DeviceRequest* request); | 191 std::string AddRequest(DeviceRequest* request); |
197 void RemoveRequest(DeviceRequests::iterator it); | 192 void RemoveRequest(DeviceRequests::iterator it); |
198 void ClearEnumerationCache(EnumerationCache* cache); | 193 void ClearEnumerationCache(EnumerationCache* cache); |
199 void PostRequestToUI(const std::string& label); | 194 void PostRequestToUI(const std::string& label); |
200 void HandleRequest(const std::string& label); | 195 void HandleRequest(const std::string& label); |
201 | 196 |
202 // Sends cached device list to a client corresponding to the request | 197 // Sends cached device list to a client corresponding to the request |
203 // identified by |label|. | 198 // identified by |label|. |
204 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); | 199 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); |
205 | 200 |
206 // Stop the request of enumerating devices indentified by |label|. | 201 // Stop the request of enumerating devices indentified by |label|. |
207 void StopEnumerateDevices(const std::string& label); | 202 void StopEnumerateDevices(const std::string& label); |
208 | 203 |
209 // Helpers to start and stop monitoring devices. | 204 // Helpers to start and stop monitoring devices. |
210 void StartMonitoring(); | 205 void StartMonitoring(); |
211 void StopMonitoring(); | 206 void StopMonitoring(); |
212 | 207 |
213 // Callback for UI called when the user requests stream with the specified | |
214 // |label| to be stopped. | |
215 void OnStopStreamRequested(const std::string& label); | |
216 | |
217 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. | 208 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. |
218 scoped_ptr<base::Thread> device_thread_; | 209 scoped_ptr<base::Thread> device_thread_; |
219 | 210 |
220 scoped_ptr<MediaStreamUIController> ui_controller_; | |
221 | |
222 media::AudioManager* const audio_manager_; // not owned | 211 media::AudioManager* const audio_manager_; // not owned |
223 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; | 212 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; |
224 scoped_refptr<VideoCaptureManager> video_capture_manager_; | 213 scoped_refptr<VideoCaptureManager> video_capture_manager_; |
225 | 214 |
226 // Indicator of device monitoring state. | 215 // Indicator of device monitoring state. |
227 bool monitoring_started_; | 216 bool monitoring_started_; |
228 | 217 |
229 // Stores most recently enumerated device lists. The cache is cleared when | 218 // Stores most recently enumerated device lists. The cache is cleared when |
230 // monitoring is stopped or there is no request for that type of device. | 219 // monitoring is stopped or there is no request for that type of device. |
231 EnumerationCache audio_enumeration_cache_; | 220 EnumerationCache audio_enumeration_cache_; |
232 EnumerationCache video_enumeration_cache_; | 221 EnumerationCache video_enumeration_cache_; |
233 | 222 |
234 // Keeps track of live enumeration commands sent to VideoCaptureManager or | 223 // Keeps track of live enumeration commands sent to VideoCaptureManager or |
235 // AudioInputDeviceManager, in order to only enumerate when necessary. | 224 // AudioInputDeviceManager, in order to only enumerate when necessary. |
236 int active_enumeration_ref_count_[NUM_MEDIA_TYPES]; | 225 int active_enumeration_ref_count_[NUM_MEDIA_TYPES]; |
237 | 226 |
238 // All non-closed request. | 227 // All non-closed request. |
239 DeviceRequests requests_; | 228 DeviceRequests requests_; |
240 | 229 |
241 // Hold a pointer to the IO loop to check we delete the device thread and | 230 // Hold a pointer to the IO loop to check we delete the device thread and |
242 // managers on the right thread. | 231 // managers on the right thread. |
243 base::MessageLoop* io_loop_; | 232 base::MessageLoop* io_loop_; |
244 | 233 |
245 bool screen_capture_active_; | 234 bool screen_capture_active_; |
246 | 235 |
| 236 bool use_fake_ui_; |
| 237 scoped_ptr<FakeMediaStreamUIProxy> fake_ui_; |
| 238 |
247 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); | 239 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); |
248 }; | 240 }; |
249 | 241 |
250 } // namespace content | 242 } // namespace content |
251 | 243 |
252 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ | 244 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ |
OLD | NEW |