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

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

Issue 10912004: Begin adding support for tab mirroring via the MediaStream audio/video capturing (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add MediaStreamDispatcher IPC glue (and unittests) for new GenerateStreamForDevice() API. Created 8 years, 3 months 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 MediaStreamDeviceSettings for permission to 9 // 2. MediaStreamManager will ask MediaStreamDeviceSettings for permission to
10 // use devices and for which device to use. 10 // use devices and for which device to use.
11 // 3. MediaStreamManager will request the corresponding media device manager(s) 11 // 3. MediaStreamManager will request the corresponding media device manager(s)
12 // to enumerate available devices. The result will be given to 12 // to enumerate available devices. The result will be given to
13 // MediaStreamDeviceSettings. 13 // MediaStreamDeviceSettings.
14 // 4. MediaStreamDeviceSettings will, by using user settings, pick devices which 14 // 4. MediaStreamDeviceSettings will, by using user settings, pick devices which
15 // devices to use and let MediaStreamManager know the result. 15 // devices to use and let MediaStreamManager know the result.
16 // 5. MediaStreamManager will call the proper media device manager to open the 16 // 5. MediaStreamManager will call the proper media device manager to open the
17 // device and let the MediaStreamRequester know it has been done. 17 // device and let the MediaStreamRequester know it has been done.
18 18
19 // When enumeration and open are done in separate operations, 19 // When enumeration and open are done in separate operations,
20 // MediaStreamDeviceSettings is not involved as in steps. 20 // MediaStreamDeviceSettings is not involved as in steps.
21 21
22 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ 22 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_
23 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ 23 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_
24 24
25 #include <map> 25 #include <map>
26 #include <string> 26 #include <string>
27 #include <vector>
28 27
29 #include "base/basictypes.h" 28 #include "base/basictypes.h"
30 #include "base/memory/scoped_ptr.h" 29 #include "base/memory/scoped_ptr.h"
31 #include "base/memory/ref_counted.h" 30 #include "base/memory/ref_counted.h"
32 #include "base/message_loop.h" 31 #include "base/message_loop.h"
33 #include "base/system_monitor/system_monitor.h" 32 #include "base/system_monitor/system_monitor.h"
34 #include "base/threading/thread.h" 33 #include "base/threading/thread.h"
35 #include "content/browser/renderer_host/media/media_stream_provider.h" 34 #include "content/browser/renderer_host/media/media_stream_provider.h"
36 #include "content/browser/renderer_host/media/media_stream_settings_requester.h" 35 #include "content/browser/renderer_host/media/media_stream_settings_requester.h"
37 #include "content/common/media/media_stream_options.h" 36 #include "content/common/media/media_stream_options.h"
38 #include "content/common/content_export.h" 37 #include "content/common/content_export.h"
39 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
40 39
41 namespace base { 40 namespace base {
42 namespace win { 41 namespace win {
43 class ScopedCOMInitializer; 42 class ScopedCOMInitializer;
44 } 43 }
45 } 44 }
46 45
46 namespace media {
47 class AudioManager;
48 }
49
47 namespace media_stream { 50 namespace media_stream {
48 51
49 class AudioInputDeviceManager; 52 class AudioInputDeviceManager;
50 class MediaStreamDeviceSettings; 53 class MediaStreamDeviceSettings;
51 class MediaStreamRequester; 54 class MediaStreamRequester;
52 class VideoCaptureManager; 55 class VideoCaptureManager;
53 56
54 // Thread that enters MTA on windows, and is base::Thread on linux and mac. 57 // Thread that enters MTA on windows, and is base::Thread on linux and mac.
55 class DeviceThread : public base::Thread { 58 class DeviceThread : public base::Thread {
56 public: 59 public:
(...skipping 12 matching lines...) Expand all
69 // MediaStreamManager is used to generate and close new media devices, not to 72 // MediaStreamManager is used to generate and close new media devices, not to
70 // start the media flow. 73 // start the media flow.
71 // The classes requesting new media streams are answered using 74 // The classes requesting new media streams are answered using
72 // MediaStreamManager::Listener. 75 // MediaStreamManager::Listener.
73 class CONTENT_EXPORT MediaStreamManager 76 class CONTENT_EXPORT MediaStreamManager
74 : public MediaStreamProviderListener, 77 : public MediaStreamProviderListener,
75 public MessageLoop::DestructionObserver, 78 public MessageLoop::DestructionObserver,
76 public SettingsRequester, 79 public SettingsRequester,
77 public base::SystemMonitor::DevicesChangedObserver { 80 public base::SystemMonitor::DevicesChangedObserver {
78 public: 81 public:
79 // This class takes the ownerships of the |audio_input_device_manager| 82 MediaStreamManager();
80 // and |video_capture_manager|.
81 MediaStreamManager(AudioInputDeviceManager* audio_input_device_manager,
82 VideoCaptureManager* video_capture_manager);
83
84 virtual ~MediaStreamManager(); 83 virtual ~MediaStreamManager();
85 84
86 // Used to access VideoCaptureManager. 85 // Used to access the VideoCaptureManager for the given |stream_type|.
87 VideoCaptureManager* video_capture_manager(); 86 VideoCaptureManager* GetVideoCaptureManager(MediaStreamType stream_type);
88 87
89 // Used to access AudioInputDeviceManager. 88 // Used to access the AudioInputDeviceManager for the given |stream_type|.
90 AudioInputDeviceManager* audio_input_device_manager(); 89 AudioInputDeviceManager* GetAudioInputDeviceManager(
90 MediaStreamType stream_type);
91 91
92 // GenerateStream opens new media devices according to |components|. It 92 // GenerateStream opens new media devices according to |components|. It
93 // creates a new request which is identified by a unique |label| that's 93 // creates a new request which is identified by a unique |label| that's
94 // returned to the caller. 94 // returned to the caller. |render_process_id| and |render_view_id| refer to
95 // the view where the infobar will appear to the user.
95 void GenerateStream(MediaStreamRequester* requester, int render_process_id, 96 void GenerateStream(MediaStreamRequester* requester, int render_process_id,
96 int render_view_id, const StreamOptions& options, 97 int render_view_id, const StreamOptions& components,
97 const GURL& security_origin, std::string* label); 98 const GURL& security_origin, std::string* label);
98 99
100 // Like GenerateStream above, except the user is only able to allow/deny the
101 // request for the device specified by |device_id|.
102 void GenerateStreamForDevice(MediaStreamRequester* requester,
103 int render_process_id, int render_view_id,
104 const StreamOptions& components,
105 const std::string& device_id,
106 const GURL& security_origin, std::string* label);
107
99 // Cancel generate stream. 108 // Cancel generate stream.
100 void CancelGenerateStream(const std::string& label); 109 void CancelGenerateStream(const std::string& label);
101 110
102 // Closes generated stream. 111 // Closes generated stream.
103 void StopGeneratedStream(const std::string& label); 112 void StopGeneratedStream(const std::string& label);
104 113
105 // Gets a list of devices of |type|. 114 // Gets a list of devices of |type|, which must be MEDIA_AUDIO_DEVICE_CAPTURE
115 // or MEDIA_VIDEO_DEVICE_CAPTURE.
106 // The request is identified using |label|, which is pointing to a 116 // The request is identified using |label|, which is pointing to a
107 // std::string. 117 // std::string.
108 // The request is persistent, which means the client keeps listening to 118 // The request is persistent, which means the client keeps listening to
109 // device changes, such as plug/unplug, and expects new device list for 119 // device changes, such as plug/unplug, and expects new device list for
110 // such a change, till the client stops the request. 120 // such a change, till the client stops the request.
111 void EnumerateDevices(MediaStreamRequester* requester, 121 void EnumerateDevices(MediaStreamRequester* requester,
112 int render_process_id, 122 int render_process_id,
113 int render_view_id, 123 int render_view_id,
114 MediaStreamType type, 124 MediaStreamType type,
115 const GURL& security_origin, 125 const GURL& security_origin,
116 std::string* label); 126 std::string* label);
117 127
118 // Open a device identified by |device_id|. 128 // Open a device identified by |device_id|. |type| must be either
129 // MEDIA_AUDIO_DEVICE_CAPTURE or MEDIA_VIDEO_DEVICE_CAPTURE.
119 // The request is identified using |label|, which is pointing to a 130 // The request is identified using |label|, which is pointing to a
120 // std::string. 131 // std::string.
121 void OpenDevice(MediaStreamRequester* requester, 132 void OpenDevice(MediaStreamRequester* requester,
122 int render_process_id, 133 int render_process_id,
123 int render_view_id, 134 int render_view_id,
124 const std::string& device_id, 135 const std::string& device_id,
125 MediaStreamType type, 136 MediaStreamType type,
126 const GURL& security_origin, 137 const GURL& security_origin,
127 std::string* label); 138 std::string* label);
128 139
(...skipping 10 matching lines...) Expand all
139 150
140 // Implements SettingsRequester. 151 // Implements SettingsRequester.
141 virtual void DevicesAccepted(const std::string& label, 152 virtual void DevicesAccepted(const std::string& label,
142 const StreamDeviceInfoArray& devices) OVERRIDE; 153 const StreamDeviceInfoArray& devices) OVERRIDE;
143 virtual void SettingsError(const std::string& label) OVERRIDE; 154 virtual void SettingsError(const std::string& label) OVERRIDE;
144 155
145 // Implements base::SystemMonitor::DevicesChangedObserver. 156 // Implements base::SystemMonitor::DevicesChangedObserver.
146 virtual void OnDevicesChanged( 157 virtual void OnDevicesChanged(
147 base::SystemMonitor::DeviceType device_type) OVERRIDE; 158 base::SystemMonitor::DeviceType device_type) OVERRIDE;
148 159
160 // Used by unit tests to provide an alternate AudioManager instance.
161 // Normally, BrowserMainLoop::GetAudioManager() is used to gain access to the
162 // AudioManager. This function must be called before any
163 // AudioInputDeviceManager is accessed or streaming/opening of an audio
164 // device is attempted.
165 void SetAudioManager(media::AudioManager* audio_manager);
166
149 // Used by unit test to make sure fake devices are used instead of a real 167 // Used by unit test to make sure fake devices are used instead of a real
150 // devices, which is needed for server based testing. 168 // devices, which is needed for server based testing.
151 // TODO(xians): Remove this hack since we can create our own 169 // TODO(xians): Remove this hack since we can create our own
152 // MediaStreamManager in our unit tests. 170 // MediaStreamManager in our unit tests.
153 void UseFakeDevice(); 171 void UseFakeDevice();
154 172
155 // This object gets deleted on the UI thread after the IO thread has been 173 // This object gets deleted on the UI thread after the IO thread has been
156 // destroyed. So we need to know when IO thread is being destroyed so that 174 // destroyed. So we need to know when IO thread is being destroyed so that
157 // we can delete VideoCaptureManager and AudioInputDeviceManager. 175 // we can delete VideoCaptureManager and AudioInputDeviceManager.
158 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; 176 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
(...skipping 16 matching lines...) Expand all
175 void NotifyObserverDevicesOpened(DeviceRequest* request); 193 void NotifyObserverDevicesOpened(DeviceRequest* request);
176 void NotifyObserverDevicesClosed(DeviceRequest* request); 194 void NotifyObserverDevicesClosed(DeviceRequest* request);
177 void DevicesFromRequest(DeviceRequest* request, 195 void DevicesFromRequest(DeviceRequest* request,
178 content::MediaStreamDevices* devices); 196 content::MediaStreamDevices* devices);
179 197
180 // Helpers. 198 // Helpers.
181 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const; 199 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const;
182 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); 200 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type);
183 void StartEnumeration(DeviceRequest* new_request, 201 void StartEnumeration(DeviceRequest* new_request,
184 std::string* label); 202 std::string* label);
185 void AddRequest(DeviceRequest* new_request, std::string* label); 203 void AddRequest(const DeviceRequest& new_request, std::string* label);
186 bool HasEnumerationRequest(MediaStreamType type); 204 bool HasEnumerationRequest(MediaStreamType type);
187 bool HasEnumerationRequest(); 205 bool HasEnumerationRequest();
188 void ClearEnumerationCache(EnumerationCache* cache); 206 void ClearEnumerationCache(EnumerationCache* cache);
189 207
190 // Helper to ensure the device thread and pass the message loop to device 208 // Helper to create the device manager for the given stream_type, if needed.
191 // managers, it also register itself as the listener to the device managers. 209 // Auto-starts the device thread and registers this as a listener with the
192 void EnsureDeviceThreadAndListener(); 210 // device managers.
211 void EnsureDeviceManagerStarted(MediaStreamType stream_type);
193 212
194 // Sends cached device list to a client corresponding to the request 213 // Sends cached device list to a client corresponding to the request
195 // identified by |label|. 214 // identified by |label|.
196 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); 215 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label);
197 216
198 // Stop the request of enumerating devices indentified by |label|. 217 // Stop the request of enumerating devices indentified by |label|.
199 void StopEnumerateDevices(const std::string& label); 218 void StopEnumerateDevices(const std::string& label);
200 219
201 // Helpers to start and stop monitoring devices. 220 // Helpers to start and stop monitoring devices.
202 void StartMonitoring(); 221 void StartMonitoring();
203 void StopMonitoring(); 222 void StopMonitoring();
204 223
205 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. 224 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager.
206 scoped_ptr<base::Thread> device_thread_; 225 scoped_ptr<base::Thread> device_thread_;
207 226
208 scoped_ptr<MediaStreamDeviceSettings> device_settings_; 227 scoped_ptr<MediaStreamDeviceSettings> device_settings_;
209 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; 228
210 scoped_refptr<VideoCaptureManager> video_capture_manager_; 229 media::AudioManager* audio_manager_; // not owned
230
231 // Device managers, instantiated on-demand.
232 scoped_refptr<MediaStreamProvider> device_manager_[content::NUM_MEDIA_TYPES];
211 233
212 // Indicator of device monitoring state. 234 // Indicator of device monitoring state.
213 bool monitoring_started_; 235 bool monitoring_started_;
214 236
215 // Stores most recently enumerated device lists. The cache is cleared when 237 // Stores most recently enumerated device lists. The cache is cleared when
216 // monitoring is stopped or there is no request for that type of device. 238 // monitoring is stopped or there is no request for that type of device.
217 EnumerationCache audio_enumeration_cache_; 239 EnumerationCache audio_enumeration_cache_;
218 EnumerationCache video_enumeration_cache_; 240 EnumerationCache video_enumeration_cache_;
219 241
220 // Keeps track of live enumeration commands sent to VideoCaptureManager or 242 // Keeps track of live enumeration commands sent to VideoCaptureManager or
221 // AudioInputDeviceManager, in order to only enumerate when necessary. 243 // AudioInputDeviceManager, in order to only enumerate when necessary.
222 int active_enumeration_ref_count_[content::NUM_MEDIA_STREAM_DEVICE_TYPES]; 244 int active_enumeration_ref_count_[content::NUM_MEDIA_TYPES];
223 245
224 // All non-closed request. 246 // All non-closed request.
225 typedef std::map<std::string, DeviceRequest> DeviceRequests; 247 typedef std::map<std::string, DeviceRequest> DeviceRequests;
226 DeviceRequests requests_; 248 DeviceRequests requests_;
227 249
228 // Hold a pointer to the IO loop to check we delete the device thread and 250 // Hold a pointer to the IO loop to check we delete the device thread and
229 // managers on the right thread. 251 // managers on the right thread.
230 MessageLoop* io_loop_; 252 MessageLoop* io_loop_;
231 253
232 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); 254 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager);
233 }; 255 };
234 256
235 } // namespace media_stream 257 } // namespace media_stream
236 258
237 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ 259 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698