Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: content/browser/renderer_host/media/media_stream_manager.h

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

Powered by Google App Engine
This is Rietveld 408576698