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

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

Issue 10830063: refactor EnumerateDevices to make it a persistent request. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: code review Created 8 years, 4 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.
(...skipping 12 matching lines...) Expand all
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> 27 #include <vector>
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/threading/thread.h" 34 #include "base/threading/thread.h"
34 #include "content/browser/renderer_host/media/media_stream_provider.h" 35 #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/browser/renderer_host/media/media_stream_settings_requester.h"
36 #include "content/common/media/media_stream_options.h" 37 #include "content/common/media/media_stream_options.h"
37 #include "content/common/content_export.h" 38 #include "content/common/content_export.h"
38 #include "content/public/browser/browser_thread.h" 39 #include "content/public/browser/browser_thread.h"
39 40
40 namespace base { 41 namespace base {
41 namespace win { 42 namespace win {
42 class ScopedCOMInitializer; 43 class ScopedCOMInitializer;
(...skipping 22 matching lines...) Expand all
65 DISALLOW_COPY_AND_ASSIGN(DeviceThread); 66 DISALLOW_COPY_AND_ASSIGN(DeviceThread);
66 }; 67 };
67 68
68 // MediaStreamManager is used to generate and close new media devices, not to 69 // MediaStreamManager is used to generate and close new media devices, not to
69 // start the media flow. 70 // start the media flow.
70 // The classes requesting new media streams are answered using 71 // The classes requesting new media streams are answered using
71 // MediaStreamManager::Listener. 72 // MediaStreamManager::Listener.
72 class CONTENT_EXPORT MediaStreamManager 73 class CONTENT_EXPORT MediaStreamManager
73 : public MediaStreamProviderListener, 74 : public MediaStreamProviderListener,
74 public MessageLoop::DestructionObserver, 75 public MessageLoop::DestructionObserver,
75 public SettingsRequester { 76 public SettingsRequester,
77 public base::SystemMonitor::DevicesChangedObserver {
76 public: 78 public:
77 // This class takes the ownerships of the |audio_input_device_manager| 79 // This class takes the ownerships of the |audio_input_device_manager|
78 // and |video_capture_manager|. 80 // and |video_capture_manager|.
79 MediaStreamManager(AudioInputDeviceManager* audio_input_device_manager, 81 MediaStreamManager(AudioInputDeviceManager* audio_input_device_manager,
80 VideoCaptureManager* video_capture_manager); 82 VideoCaptureManager* video_capture_manager);
81 83
82 virtual ~MediaStreamManager(); 84 virtual ~MediaStreamManager();
83 85
84 // Used to access VideoCaptureManager. 86 // Used to access VideoCaptureManager.
85 VideoCaptureManager* video_capture_manager(); 87 VideoCaptureManager* video_capture_manager();
(...skipping 14 matching lines...) Expand all
100 102
101 // Cancel generate stream. 103 // Cancel generate stream.
102 void CancelGenerateStream(const std::string& label); 104 void CancelGenerateStream(const std::string& label);
103 105
104 // Closes generated stream. 106 // Closes generated stream.
105 void StopGeneratedStream(const std::string& label); 107 void StopGeneratedStream(const std::string& label);
106 108
107 // Gets a list of devices of |type|. 109 // Gets a list of devices of |type|.
108 // The request is identified using |label|, which is pointing to a 110 // The request is identified using |label|, which is pointing to a
109 // std::string. 111 // std::string.
112 // The request is persistent, which means the client keeps listening to
113 // device changes, such as plug/unplug, and expects new device list for
114 // such a change, till the client stops the request.
110 void EnumerateDevices(MediaStreamRequester* requester, 115 void EnumerateDevices(MediaStreamRequester* requester,
111 int render_process_id, 116 int render_process_id,
112 int render_view_id, 117 int render_view_id,
113 MediaStreamType type, 118 MediaStreamType type,
114 const GURL& security_origin, 119 const GURL& security_origin,
115 std::string* label); 120 std::string* label);
116 121
117 // Open a device identified by |device_id|. 122 // Open a device identified by |device_id|.
118 // The request is identified using |label|, which is pointing to a 123 // The request is identified using |label|, which is pointing to a
119 // std::string. 124 // std::string.
(...skipping 14 matching lines...) Expand all
134 const StreamDeviceInfoArray& devices) OVERRIDE; 139 const StreamDeviceInfoArray& devices) OVERRIDE;
135 virtual void Error(MediaStreamType stream_type, 140 virtual void Error(MediaStreamType stream_type,
136 int capture_session_id, 141 int capture_session_id,
137 MediaStreamProviderError error) OVERRIDE; 142 MediaStreamProviderError error) OVERRIDE;
138 143
139 // Implements SettingsRequester. 144 // Implements SettingsRequester.
140 virtual void DevicesAccepted(const std::string& label, 145 virtual void DevicesAccepted(const std::string& label,
141 const StreamDeviceInfoArray& devices) OVERRIDE; 146 const StreamDeviceInfoArray& devices) OVERRIDE;
142 virtual void SettingsError(const std::string& label) OVERRIDE; 147 virtual void SettingsError(const std::string& label) OVERRIDE;
143 148
149 // Implements base::SystemMonitor::DevicesChangedObserver.
150 virtual void OnDevicesChanged(base::SystemMonitor::DeviceType device_type);
151
144 // Used by unit test to make sure fake devices are used instead of a real 152 // Used by unit test to make sure fake devices are used instead of a real
145 // devices, which is needed for server based testing. 153 // devices, which is needed for server based testing.
146 // TODO(xians): Remove this hack since we can create our own 154 // TODO(xians): Remove this hack since we can create our own
147 // MediaStreamManager in our unit tests. 155 // MediaStreamManager in our unit tests.
148 void UseFakeDevice(); 156 void UseFakeDevice();
149 157
150 // This object gets deleted on the UI thread after the IO thread has been 158 // This object gets deleted on the UI thread after the IO thread has been
151 // destroyed. So we need to know when IO thread is being destroyed so that 159 // destroyed. So we need to know when IO thread is being destroyed so that
152 // we can delete VideoCaptureManager and AudioInputDeviceManager. 160 // we can delete VideoCaptureManager and AudioInputDeviceManager.
153 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; 161 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
154 162
155 private: 163 private:
156 // Contains all data needed to keep track of requests. 164 // Contains all data needed to keep track of requests.
157 struct DeviceRequest; 165 struct DeviceRequest;
158 166
167 // Cache enumerated device list.
168 struct EnumerationCache {
169 EnumerationCache();
170
171 bool valid;
172 StreamDeviceInfoArray devices;
173 };
174
159 // Helpers for signaling the media observer that new capture devices are 175 // Helpers for signaling the media observer that new capture devices are
160 // opened/closed. 176 // opened/closed.
161 void NotifyObserverDevicesOpened(DeviceRequest* request); 177 void NotifyObserverDevicesOpened(DeviceRequest* request);
162 void NotifyObserverDevicesClosed(DeviceRequest* request); 178 void NotifyObserverDevicesClosed(DeviceRequest* request);
163 void DevicesFromRequest(DeviceRequest* request, 179 void DevicesFromRequest(DeviceRequest* request,
164 content::MediaStreamDevices* devices); 180 content::MediaStreamDevices* devices);
165 181
166 // Helpers. 182 // Helpers.
167 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const; 183 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const;
168 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); 184 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type);
169 void StartEnumeration(DeviceRequest* new_request, 185 void StartEnumeration(DeviceRequest* new_request,
170 std::string* label); 186 std::string* label);
187 void AddRequest(DeviceRequest* new_request, std::string* label);
188 bool HasEnumerationRequest(MediaStreamType type);
189 bool HasEnumerationRequest();
190 void ClearEnumerationCache(EnumerationCache* cache);
171 191
172 // Helper to ensure the device thread and pass the message loop to device 192 // Helper to ensure the device thread and pass the message loop to device
173 // managers, it also register itself as the listener to the device managers. 193 // managers, it also register itself as the listener to the device managers.
174 void EnsureDeviceThreadAndListener(); 194 void EnsureDeviceThreadAndListener();
175 195
196 // Sends cached device list to a client corresponding to the request
197 // identified by |label|.
198 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label);
199
200 // Stop the request of enumerating devices indentified by |label|.
201 void StopEnumerateDevices(const std::string& label);
202
203 // Helpers to start and stop monitoring devices.
204 void StartMonitoring();
205 void StopMonitoring();
206
176 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. 207 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager.
177 scoped_ptr<base::Thread> device_thread_; 208 scoped_ptr<base::Thread> device_thread_;
178 209
179 scoped_ptr<MediaStreamDeviceSettings> device_settings_; 210 scoped_ptr<MediaStreamDeviceSettings> device_settings_;
180 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; 211 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_;
181 scoped_refptr<VideoCaptureManager> video_capture_manager_; 212 scoped_refptr<VideoCaptureManager> video_capture_manager_;
182 213
183 // Keeps track of device types currently being enumerated to not enumerate 214 // Indicator of device monitoring state.
184 // when not necessary. 215 bool monitoring_started_;
185 std::vector<bool> enumeration_in_progress_; 216
217 // Stores most recently enumerated device lists. The cache is cleared when
218 // monitoring is stopped or there is no request for that type of devices.
tommi (sloooow) - chröme 2012/08/01 10:21:38 s/devices/device.
wjia(left Chromium) 2012/08/01 14:24:03 Done.
219 EnumerationCache audio_enumeration_cache_;
220 EnumerationCache video_enumeration_cache_;
221
222 // Keeps track of live enumeration commands sent to VideoCaptureManager or
223 // AudioInputDeviceManager, in order to not enumerate when not necessary.
tommi (sloooow) - chröme 2012/08/01 10:21:38 nit: ...in order to only enumerate when necessary.
wjia(left Chromium) 2012/08/01 14:24:03 Agree and done. I didn't change it since it was th
224 std::vector<int> enumeration_in_progress_;
186 225
187 // All non-closed request. 226 // All non-closed request.
188 typedef std::map<std::string, DeviceRequest> DeviceRequests; 227 typedef std::map<std::string, DeviceRequest> DeviceRequests;
189 DeviceRequests requests_; 228 DeviceRequests requests_;
190 229
191 // 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
192 // managers on the right thread. 231 // managers on the right thread.
193 MessageLoop* io_loop_; 232 MessageLoop* io_loop_;
194 233
195 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); 234 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager);
196 }; 235 };
197 236
198 } // namespace media_stream 237 } // namespace media_stream
199 238
200 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ 239 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698