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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 | 148 |
149 // 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 |
150 // 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 |
151 // can pass --use-fake-device-for-media-stream). | 151 // can pass --use-fake-device-for-media-stream). |
152 void UseFakeDevice(); | 152 void UseFakeDevice(); |
153 | 153 |
154 // 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 |
155 // generated stream (or when using --use-fake-ui-for-media-stream). | 155 // generated stream (or when using --use-fake-ui-for-media-stream). |
156 void UseFakeUI(scoped_ptr<FakeMediaStreamUIProxy> fake_ui); | 156 void UseFakeUI(scoped_ptr<FakeMediaStreamUIProxy> fake_ui); |
157 | 157 |
158 // Used by test to find a StreamDeviceInfo that currently has been | |
159 // requested and match the input arguments. If such a StreamDeviceInfo is | |
160 // found it is copied to |device_info|. | |
161 const StreamDeviceInfo* FindRequestedDeviceInfoForTest( | |
tommi (sloooow) - chröme
2013/11/26 14:44:21
just checking... is there no way this can be achie
perkj_chrome
2013/11/27 13:41:34
Done.
| |
162 const std::string& source_id, | |
163 int render_process_id, | |
164 int render_view_id, | |
165 MediaStreamRequestType type); | |
166 | |
158 // This object gets deleted on the UI thread after the IO thread has been | 167 // This object gets deleted on the UI thread after the IO thread has been |
159 // destroyed. So we need to know when IO thread is being destroyed so that | 168 // destroyed. So we need to know when IO thread is being destroyed so that |
160 // we can delete VideoCaptureManager and AudioInputDeviceManager. | 169 // we can delete VideoCaptureManager and AudioInputDeviceManager. Normally |
161 // We also must call this function explicitly in tests which use | 170 // this is handled by |
162 // TestBrowserThreadBundle, because the notification happens too late in that | 171 // base::MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop. |
163 // case (see http://crbug.com/247525#c14). | 172 // But for some tests which use TestBrowserThreadBundle, we need to call |
173 // WillDestroyCurrentMessageLoop explicitly because the notification happens | |
174 // too late. (see http://crbug.com/247525#c14). | |
164 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 175 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
165 | 176 |
166 protected: | 177 protected: |
167 // Used for testing. | 178 // Used for testing. |
168 MediaStreamManager(); | 179 MediaStreamManager(); |
169 | 180 |
170 private: | 181 private: |
171 friend class MockMediaStreamDispatcherHost; | |
172 | |
173 // Contains all data needed to keep track of requests. | 182 // Contains all data needed to keep track of requests. |
174 class DeviceRequest; | 183 class DeviceRequest; |
175 | 184 |
176 // Cache enumerated device list. | 185 // Cache enumerated device list. |
177 struct EnumerationCache { | 186 struct EnumerationCache { |
178 EnumerationCache(); | 187 EnumerationCache(); |
179 ~EnumerationCache(); | 188 ~EnumerationCache(); |
180 | 189 |
181 bool valid; | 190 bool valid; |
182 StreamDeviceInfoArray devices; | 191 StreamDeviceInfoArray devices; |
183 }; | 192 }; |
184 | 193 |
185 typedef std::map<std::string, DeviceRequest*> DeviceRequests; | 194 typedef std::map<std::string, DeviceRequest*> DeviceRequests; |
186 | 195 |
187 // Initializes the device managers on IO thread. Auto-starts the device | 196 // Initializes the device managers on IO thread. Auto-starts the device |
188 // thread and registers this as a listener with the device managers. | 197 // thread and registers this as a listener with the device managers. |
189 void InitializeDeviceManagersOnIOThread(); | 198 void InitializeDeviceManagersOnIOThread(); |
190 | 199 |
191 // Helper for sending up-to-date device lists to media observer when a | 200 // Helper for sending up-to-date device lists to media observer when a |
192 // capture device is plugged in or unplugged. | 201 // capture device is plugged in or unplugged. |
193 void NotifyDevicesChanged(MediaStreamType stream_type, | 202 void NotifyDevicesChanged(MediaStreamType stream_type, |
194 const StreamDeviceInfoArray& devices); | 203 const StreamDeviceInfoArray& devices); |
195 | 204 |
196 | |
197 void HandleAccessRequestResponse(const std::string& label, | 205 void HandleAccessRequestResponse(const std::string& label, |
198 const MediaStreamDevices& devices); | 206 const MediaStreamDevices& devices); |
199 void StopMediaStreamFromBrowser(const std::string& label); | 207 void StopMediaStreamFromBrowser(const std::string& label); |
200 | 208 |
209 void DoEnumerateDevices(const std::string& label); | |
210 | |
201 // Helpers. | 211 // Helpers. |
202 // Checks if all devices that was requested in the request identififed by | 212 // Checks if all devices that was requested in the request identififed by |
203 // |label| has been opened and set the request state accordingly. | 213 // |label| has been opened and set the request state accordingly. |
204 void HandleRequestDone(const std::string& label, | 214 void HandleRequestDone(const std::string& label, |
205 DeviceRequest* request); | 215 DeviceRequest* request); |
206 // Stop the use of the device associated with |session_id| of type |type| in | 216 // Stop the use of the device associated with |session_id| of type |type| in |
207 // all |requests_|. The device is removed from the request. If a request | 217 // all |requests_|. The device is removed from the request. If a request |
208 /// doesn't use any devices as a consequence, the request is deleted. | 218 /// doesn't use any devices as a consequence, the request is deleted. |
209 void StopDevice(MediaStreamType type, int session_id); | 219 void StopDevice(MediaStreamType type, int session_id); |
210 // Calls the correct capture manager and close the device with |session_id|. | 220 // Calls the correct capture manager and close the device with |session_id|. |
211 // All requests that uses the device are updated. | 221 // All requests that uses the device are updated. |
212 void CloseDevice(MediaStreamType type, int session_id); | 222 void CloseDevice(MediaStreamType type, int session_id); |
213 // Returns true if a request for devices has been completed and the devices | 223 // Returns true if a request for devices has been completed and the devices |
214 // has either been opened or an error has occurred. | 224 // has either been opened or an error has occurred. |
215 bool RequestDone(const DeviceRequest& request) const; | 225 bool RequestDone(const DeviceRequest& request) const; |
216 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); | 226 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); |
217 void StartEnumeration(DeviceRequest* request); | 227 void StartEnumeration(DeviceRequest* request); |
218 std::string AddRequest(DeviceRequest* request); | 228 std::string AddRequest(DeviceRequest* request); |
219 void RemoveRequest(DeviceRequests::iterator it); | 229 DeviceRequest* FindRequest(const std::string& label); |
230 void DeleteRequest(const std::string& label); | |
220 void ClearEnumerationCache(EnumerationCache* cache); | 231 void ClearEnumerationCache(EnumerationCache* cache); |
221 void PostRequestToUI(const std::string& label); | 232 // Prepare the request with label |label| by starting device enumeration if |
222 void HandleRequest(const std::string& label); | 233 // needed. |
234 void SetupRequest(const std::string& label); | |
235 bool SetupTabCaptureRequest(DeviceRequest* request); | |
236 bool SetupScreenCaptureRequest(DeviceRequest* request); | |
237 // Called when a request has been setup and devices have been enumerated if | |
238 // needed. If a certain source id has been requested, the source id is | |
239 // translated to a real device id before the request is posted to UI. | |
240 void PostRequestToUI(const std::string& label, DeviceRequest* request); | |
223 // Returns true if a device with |device_id| has already been requested by | 241 // Returns true if a device with |device_id| has already been requested by |
224 // |render_process_id| and |render_view_id| of type |type|. If it has been | 242 // |render_process_id| and |render_view_id| of type |type|. If it has been |
225 // requested, |device_info| contain information about the the device. | 243 // requested, |device_info| contain information about the device. |
226 bool FindExistingRequestedDeviceInfo(int render_process_id, | 244 bool FindExistingRequestedDeviceInfo(int render_process_id, |
227 int render_view_id, | 245 int render_view_id, |
246 const GURL& security_origin, | |
228 MediaStreamRequestType type, | 247 MediaStreamRequestType type, |
229 const std::string& device_id, | 248 const std::string& device_id, |
249 MediaStreamType device_type, | |
230 StreamDeviceInfo* device_info, | 250 StreamDeviceInfo* device_info, |
231 MediaRequestState* request_state) const; | 251 MediaRequestState* request_state) const; |
232 | 252 |
233 // Sends cached device list to a client corresponding to the request | 253 void FinalizeGenerateStream(const std::string& label, |
234 // identified by |label|. | 254 DeviceRequest* request); |
235 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); | 255 void FinalizeRequestFailed(const std::string& label, |
256 DeviceRequest* request); | |
257 void FinalizeOpenDevice(const std::string& label, | |
258 DeviceRequest* request); | |
259 void FinalizeMediaAccessRequest(const std::string& label, | |
260 DeviceRequest* request, | |
261 const MediaStreamDevices& devices); | |
262 void FinalizeEnumerateDevices(const std::string& label, | |
263 DeviceRequest* request); | |
236 | 264 |
237 // This method is called when an audio or video device is plugged in or | 265 // This method is called when an audio or video device is plugged in or |
238 // removed. It make sure all MediaStreams that use a removed device is | 266 // removed. It make sure all MediaStreams that use a removed device is |
239 // stopped and that the render process is notified. |old_devices| is the list | 267 // stopped and that the render process is notified. |old_devices| is the list |
240 // of previously available devices. |new_devices| is the new | 268 // of previously available devices. |new_devices| is the new |
241 // list of currently available devices. | 269 // list of currently available devices. |
242 void StopRemovedDevices(const StreamDeviceInfoArray& old_devices, | 270 void StopRemovedDevices(const StreamDeviceInfoArray& old_devices, |
243 const StreamDeviceInfoArray& new_devices); | 271 const StreamDeviceInfoArray& new_devices); |
244 // Helper method used by StopRemovedDevices to stop the use of a certain | 272 // Helper method used by StopRemovedDevices to stop the use of a certain |
245 // device. | 273 // device. |
246 void StopRemovedDevice(const MediaStreamDevice& device); | 274 void StopRemovedDevice(const MediaStreamDevice& device); |
247 | 275 |
248 // Helpers to start and stop monitoring devices. | 276 // Helpers to start and stop monitoring devices. |
249 void StartMonitoring(); | 277 void StartMonitoring(); |
250 void StopMonitoring(); | 278 void StopMonitoring(); |
251 | 279 |
252 // Finds and returns the raw device id corresponding to the given | 280 bool TranslateRequestedSourceIdToDeviceId(MediaStreamRequest* request); |
253 // |device_guid|. Returns true if there was a raw device id that matched the | 281 void TranslateDeviceIdToSourceId(const MediaStreamRequest& request, |
254 // given |device_guid|, false if nothing matched it. | 282 MediaStreamDevice* device); |
255 bool TranslateGUIDToRawId( | 283 |
284 // Finds and returns the device id corresponding to the given | |
285 // |source_id|. Returns true if there was a raw device id that matched the | |
286 // given |source_id|, false if nothing matched it. | |
287 bool TranslateSourceIdToDeviceId( | |
256 MediaStreamType stream_type, | 288 MediaStreamType stream_type, |
257 const GURL& security_origin, | 289 const GURL& security_origin, |
258 const std::string& device_guid, | 290 const std::string& source_id, |
259 std::string* raw_device_id); | 291 std::string* device_id); |
260 | 292 |
261 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. | 293 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. |
262 scoped_ptr<base::Thread> device_thread_; | 294 scoped_ptr<base::Thread> device_thread_; |
263 | 295 |
264 media::AudioManager* const audio_manager_; // not owned | 296 media::AudioManager* const audio_manager_; // not owned |
265 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; | 297 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; |
266 scoped_refptr<VideoCaptureManager> video_capture_manager_; | 298 scoped_refptr<VideoCaptureManager> video_capture_manager_; |
267 | 299 |
268 // Indicator of device monitoring state. | 300 // Indicator of device monitoring state. |
269 bool monitoring_started_; | 301 bool monitoring_started_; |
270 | 302 |
271 // Stores most recently enumerated device lists. The cache is cleared when | 303 // Stores most recently enumerated device lists. The cache is cleared when |
272 // monitoring is stopped or there is no request for that type of device. | 304 // monitoring is stopped or there is no request for that type of device. |
273 EnumerationCache audio_enumeration_cache_; | 305 EnumerationCache audio_enumeration_cache_; |
274 EnumerationCache video_enumeration_cache_; | 306 EnumerationCache video_enumeration_cache_; |
275 | 307 |
276 // Keeps track of live enumeration commands sent to VideoCaptureManager or | 308 // Keeps track of live enumeration commands sent to VideoCaptureManager or |
277 // AudioInputDeviceManager, in order to only enumerate when necessary. | 309 // AudioInputDeviceManager, in order to only enumerate when necessary. |
278 int active_enumeration_ref_count_[NUM_MEDIA_TYPES]; | 310 int active_enumeration_ref_count_[NUM_MEDIA_TYPES]; |
279 | 311 |
280 // All non-closed request. | 312 // All non-closed request. |
281 DeviceRequests requests_; | 313 DeviceRequests requests_; |
282 | 314 |
283 std::vector<int> opened_audio_session_ids_; | |
284 std::vector<int> opened_video_session_ids_; | |
285 | |
286 // Hold a pointer to the IO loop to check we delete the device thread and | 315 // Hold a pointer to the IO loop to check we delete the device thread and |
287 // managers on the right thread. | 316 // managers on the right thread. |
288 base::MessageLoop* io_loop_; | 317 base::MessageLoop* io_loop_; |
289 | 318 |
290 bool screen_capture_active_; | |
291 | |
292 bool use_fake_ui_; | 319 bool use_fake_ui_; |
293 scoped_ptr<FakeMediaStreamUIProxy> fake_ui_; | 320 scoped_ptr<FakeMediaStreamUIProxy> fake_ui_; |
294 | 321 |
295 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); | 322 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); |
296 }; | 323 }; |
297 | 324 |
298 } // namespace content | 325 } // namespace content |
299 | 326 |
300 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ | 327 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ |
OLD | NEW |