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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 MediaStreamType type, | 134 MediaStreamType type, |
135 const GURL& security_origin); | 135 const GURL& security_origin); |
136 | 136 |
137 // Implements MediaStreamProviderListener. | 137 // Implements MediaStreamProviderListener. |
138 virtual void Opened(MediaStreamType stream_type, | 138 virtual void Opened(MediaStreamType stream_type, |
139 int capture_session_id) OVERRIDE; | 139 int capture_session_id) OVERRIDE; |
140 virtual void Closed(MediaStreamType stream_type, | 140 virtual void Closed(MediaStreamType stream_type, |
141 int capture_session_id) OVERRIDE; | 141 int capture_session_id) OVERRIDE; |
142 virtual void DevicesEnumerated(MediaStreamType stream_type, | 142 virtual void DevicesEnumerated(MediaStreamType stream_type, |
143 const StreamDeviceInfoArray& devices) OVERRIDE; | 143 const StreamDeviceInfoArray& devices) OVERRIDE; |
144 virtual void Error(MediaStreamType stream_type, | |
145 int capture_session_id, | |
146 MediaStreamProviderError error) OVERRIDE; | |
147 | 144 |
148 // Implements base::SystemMonitor::DevicesChangedObserver. | 145 // Implements base::SystemMonitor::DevicesChangedObserver. |
149 virtual void OnDevicesChanged( | 146 virtual void OnDevicesChanged( |
150 base::SystemMonitor::DeviceType device_type) OVERRIDE; | 147 base::SystemMonitor::DeviceType device_type) OVERRIDE; |
151 | 148 |
152 // Used by unit test to make sure fake devices are used instead of a real | 149 // Used by unit test to make sure fake devices are used instead of a real |
153 // devices, which is needed for server based testing or certain tests (which | 150 // devices, which is needed for server based testing or certain tests (which |
154 // can pass --use-fake-device-for-media-stream). | 151 // can pass --use-fake-device-for-media-stream). |
155 void UseFakeDevice(); | 152 void UseFakeDevice(); |
156 | 153 |
157 // Called by the tests to specify a fake UI that should be used for next | 154 // Called by the tests to specify a fake UI that should be used for next |
158 // generated stream (or when using --use-fake-ui-for-media-stream). | 155 // generated stream (or when using --use-fake-ui-for-media-stream). |
159 void UseFakeUI(scoped_ptr<FakeMediaStreamUIProxy> fake_ui); | 156 void UseFakeUI(scoped_ptr<FakeMediaStreamUIProxy> fake_ui); |
160 | 157 |
161 // This object gets deleted on the UI thread after the IO thread has been | 158 // Used by test to find a StreamDeviceInfo that currently has been |
162 // destroyed. So we need to know when IO thread is being destroyed so that | 159 // requested and match the input arguments. If such a StreamDeviceInfo is |
163 // we can delete VideoCaptureManager and AudioInputDeviceManager. | 160 // found it is copied to |device_info|. |
164 // We also must call this function explicitly in tests which use | 161 bool FindRequestedDeviceInfo(const std::string& source_id, |
165 // TestBrowserThreadBundle, because the notification happens too late in that | 162 int render_process_id, |
166 // case (see http://crbug.com/247525#c14). | 163 int render_view_id, |
167 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 164 MediaStreamRequestType type, |
| 165 StreamDeviceInfo* device_info); |
168 | 166 |
169 protected: | 167 protected: |
170 // Used for testing. | 168 // Used for testing. |
171 MediaStreamManager(); | 169 MediaStreamManager(); |
172 | 170 |
173 private: | 171 private: |
174 friend class MockMediaStreamDispatcherHost; | |
175 | |
176 // Contains all data needed to keep track of requests. | 172 // Contains all data needed to keep track of requests. |
177 class DeviceRequest; | 173 class DeviceRequest; |
178 | 174 |
179 // Cache enumerated device list. | 175 // Cache enumerated device list. |
180 struct EnumerationCache { | 176 struct EnumerationCache { |
181 EnumerationCache(); | 177 EnumerationCache(); |
182 ~EnumerationCache(); | 178 ~EnumerationCache(); |
183 | 179 |
184 bool valid; | 180 bool valid; |
185 StreamDeviceInfoArray devices; | 181 StreamDeviceInfoArray devices; |
186 }; | 182 }; |
187 | 183 |
188 typedef std::map<std::string, DeviceRequest*> DeviceRequests; | 184 typedef std::map<std::string, DeviceRequest*> DeviceRequests; |
189 | 185 |
| 186 // This object gets deleted on the UI thread after the IO thread has been |
| 187 // destroyed. So we need to know when IO thread is being destroyed so that |
| 188 // we can delete VideoCaptureManager and AudioInputDeviceManager. |
| 189 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 190 |
190 // Initializes the device managers on IO thread. Auto-starts the device | 191 // Initializes the device managers on IO thread. Auto-starts the device |
191 // thread and registers this as a listener with the device managers. | 192 // thread and registers this as a listener with the device managers. |
192 void InitializeDeviceManagersOnIOThread(); | 193 void InitializeDeviceManagersOnIOThread(); |
193 | 194 |
194 // Helper for sending up-to-date device lists to media observer when a | 195 // Helper for sending up-to-date device lists to media observer when a |
195 // capture device is plugged in or unplugged. | 196 // capture device is plugged in or unplugged. |
196 void NotifyDevicesChanged(MediaStreamType stream_type, | 197 void NotifyDevicesChanged(MediaStreamType stream_type, |
197 const StreamDeviceInfoArray& devices); | 198 const StreamDeviceInfoArray& devices); |
198 | 199 |
199 | |
200 void HandleAccessRequestResponse(const std::string& label, | 200 void HandleAccessRequestResponse(const std::string& label, |
201 const MediaStreamDevices& devices); | 201 const MediaStreamDevices& devices); |
202 void StopStreamFromUI(const std::string& label); | 202 void StopStreamFromUI(const std::string& label); |
203 | 203 |
| 204 void DoCancelRequest(const std::string& label); |
| 205 |
| 206 void DoStopStreamDevice(int render_process_id, |
| 207 int render_view_id, |
| 208 const std::string& device_id); |
| 209 |
| 210 void DoEnumerateDevices(const std::string& label); |
| 211 |
204 // Helpers. | 212 // Helpers. |
205 // Checks if all devices that was requested in the request identififed by | 213 // Checks if all devices that was requested in the request identififed by |
206 // |label| has been opened and set the request state accordingly. | 214 // |label| has been opened and set the request state accordingly. |
207 void HandleRequestDone(const std::string& label, | 215 void HandleRequestDone(const std::string& label, |
208 DeviceRequest* request); | 216 DeviceRequest* request); |
209 void StopDevice(const StreamDeviceInfo& device_info); | 217 void StopDevice(const StreamDeviceInfo& device_info); |
210 // Returns true if a request for devices has been completed and the devices | 218 // Returns true if a request for devices has been completed and the devices |
211 // has either been opened or an error has occurred. | 219 // has either been opened or an error has occurred. |
212 bool RequestDone(const DeviceRequest& request) const; | 220 bool RequestDone(const DeviceRequest& request) const; |
213 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); | 221 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); |
214 void StartEnumeration(DeviceRequest* request); | 222 void StartEnumeration(DeviceRequest* request); |
215 std::string AddRequest(DeviceRequest* request); | 223 std::string AddRequest(DeviceRequest* request); |
216 void RemoveRequest(DeviceRequests::iterator it); | 224 void DeleteRequest(DeviceRequests::iterator it); |
217 void ClearEnumerationCache(EnumerationCache* cache); | 225 void ClearEnumerationCache(EnumerationCache* cache); |
218 void PostRequestToUI(const std::string& label); | 226 void PostRequestToUI(const std::string& label); |
219 void HandleRequest(const std::string& label); | 227 void HandleRequest(const std::string& label); |
220 // Returns true if a device with |device_id| has already been requested by | 228 // Returns true if a device with |device_id| has already been requested by |
221 // |render_process_id| and |render_view_id| of type |type|. If it has been | 229 // |render_process_id| and |render_view_id| of type |type|. If it has been |
222 // requested, |device_info| contain information about the the device. | 230 // requested, |device_info| contain information about the device. |
223 bool FindExistingRequestedDeviceInfo(int render_process_id, | 231 bool FindExistingRequestedDeviceInfo(int render_process_id, |
224 int render_view_id, | 232 int render_view_id, |
| 233 GURL security_origin, |
225 MediaStreamRequestType type, | 234 MediaStreamRequestType type, |
226 const std::string& device_id, | 235 const std::string& device_id, |
227 StreamDeviceInfo* device_info, | 236 StreamDeviceInfo* device_info, |
228 MediaRequestState* request_state) const; | 237 MediaRequestState* request_state) const; |
229 | 238 |
230 // Sends cached device list to a client corresponding to the request | 239 void FinalizeGenerateStream(const std::string& label, |
231 // identified by |label|. | 240 DeviceRequest* request); |
232 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); | 241 void FinalizeRequestFailed(const std::string& label); |
| 242 void FinalizeOpenDevice(const std::string& label, |
| 243 DeviceRequest* request); |
| 244 void FinalizeMediaAccessRequest(const std::string& label, |
| 245 const MediaStreamDevices& devices); |
| 246 void FinalizeEnumerateDevices(const std::string& label, |
| 247 DeviceRequest* request); |
233 | 248 |
234 // Helpers to start and stop monitoring devices. | 249 // Helpers to start and stop monitoring devices. |
235 void StartMonitoring(); | 250 void StartMonitoring(); |
236 void StopMonitoring(); | 251 void StopMonitoring(); |
237 | 252 |
238 // Finds and returns the raw device id corresponding to the given | 253 bool TransLateRequestedSourceIdToDeviceId(MediaStreamRequest* request); |
239 // |device_guid|. Returns true if there was a raw device id that matched the | 254 // Finds and returns the device id corresponding to the given |
240 // given |device_guid|, false if nothing matched it. | 255 // |source_id|. Returns true if there was a raw device id that matched the |
241 bool TranslateGUIDToRawId( | 256 // given |source_id|, false if nothing matched it. |
| 257 bool TranslateSourceIdToDeviceId( |
242 MediaStreamType stream_type, | 258 MediaStreamType stream_type, |
243 const GURL& security_origin, | 259 const GURL& security_origin, |
244 const std::string& device_guid, | 260 const std::string& source_id, |
245 std::string* raw_device_id); | 261 std::string* device_id); |
246 | 262 |
247 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. | 263 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. |
248 scoped_ptr<base::Thread> device_thread_; | 264 scoped_ptr<base::Thread> device_thread_; |
249 | 265 |
250 media::AudioManager* const audio_manager_; // not owned | 266 media::AudioManager* const audio_manager_; // not owned |
251 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; | 267 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; |
252 scoped_refptr<VideoCaptureManager> video_capture_manager_; | 268 scoped_refptr<VideoCaptureManager> video_capture_manager_; |
253 | 269 |
254 // Indicator of device monitoring state. | 270 // Indicator of device monitoring state. |
255 bool monitoring_started_; | 271 bool monitoring_started_; |
(...skipping 21 matching lines...) Expand all Loading... |
277 | 293 |
278 bool use_fake_ui_; | 294 bool use_fake_ui_; |
279 scoped_ptr<FakeMediaStreamUIProxy> fake_ui_; | 295 scoped_ptr<FakeMediaStreamUIProxy> fake_ui_; |
280 | 296 |
281 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); | 297 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); |
282 }; | 298 }; |
283 | 299 |
284 } // namespace content | 300 } // namespace content |
285 | 301 |
286 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ | 302 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ |
OLD | NEW |