| 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 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 Loading... |
| 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. |
| 95 void GenerateStream(MediaStreamRequester* requester, int render_process_id, | 95 void GenerateStream(MediaStreamRequester* requester, int render_process_id, |
| 96 int render_view_id, const StreamOptions& options, | 96 int render_view_id, const StreamOptions& components, |
| 97 const GURL& security_origin, std::string* label); | 97 const GURL& security_origin, std::string* label); |
| 98 | 98 |
| 99 // Cancel generate stream. | 99 // Cancel generate stream. |
| 100 void CancelGenerateStream(const std::string& label); | 100 void CancelGenerateStream(const std::string& label); |
| 101 | 101 |
| 102 // Closes generated stream. | 102 // Closes generated stream. |
| 103 void StopGeneratedStream(const std::string& label); | 103 void StopGeneratedStream(const std::string& label); |
| 104 | 104 |
| 105 // Gets a list of devices of |type|. | 105 // Gets a list of devices of |type|. |
| 106 // The request is identified using |label|, which is pointing to a | 106 // The request is identified using |label|, which is pointing to a |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 139 |
| 140 // Implements SettingsRequester. | 140 // Implements SettingsRequester. |
| 141 virtual void DevicesAccepted(const std::string& label, | 141 virtual void DevicesAccepted(const std::string& label, |
| 142 const StreamDeviceInfoArray& devices) OVERRIDE; | 142 const StreamDeviceInfoArray& devices) OVERRIDE; |
| 143 virtual void SettingsError(const std::string& label) OVERRIDE; | 143 virtual void SettingsError(const std::string& label) OVERRIDE; |
| 144 | 144 |
| 145 // Implements base::SystemMonitor::DevicesChangedObserver. | 145 // Implements base::SystemMonitor::DevicesChangedObserver. |
| 146 virtual void OnDevicesChanged( | 146 virtual void OnDevicesChanged( |
| 147 base::SystemMonitor::DeviceType device_type) OVERRIDE; | 147 base::SystemMonitor::DeviceType device_type) OVERRIDE; |
| 148 | 148 |
| 149 // Used by unit tests to provide an alternate AudioManager instance. |
| 150 // Normally, BrowserMainLoop::GetAudioManager() is used to gain access to the |
| 151 // AudioManager. |
| 152 void UseAudioManager(media::AudioManager* audio_manager); |
| 153 |
| 149 // Used by unit test to make sure fake devices are used instead of a real | 154 // Used by unit test to make sure fake devices are used instead of a real |
| 150 // devices, which is needed for server based testing. | 155 // devices, which is needed for server based testing. |
| 151 // TODO(xians): Remove this hack since we can create our own | 156 // TODO(xians): Remove this hack since we can create our own |
| 152 // MediaStreamManager in our unit tests. | 157 // MediaStreamManager in our unit tests. |
| 153 void UseFakeDevice(); | 158 void UseFakeDevice(); |
| 154 | 159 |
| 155 // This object gets deleted on the UI thread after the IO thread has been | 160 // 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 | 161 // destroyed. So we need to know when IO thread is being destroyed so that |
| 157 // we can delete VideoCaptureManager and AudioInputDeviceManager. | 162 // we can delete VideoCaptureManager and AudioInputDeviceManager. |
| 158 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 163 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 180 // Helpers. | 185 // Helpers. |
| 181 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const; | 186 bool RequestDone(const MediaStreamManager::DeviceRequest& request) const; |
| 182 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); | 187 MediaStreamProvider* GetDeviceManager(MediaStreamType stream_type); |
| 183 void StartEnumeration(DeviceRequest* new_request, | 188 void StartEnumeration(DeviceRequest* new_request, |
| 184 std::string* label); | 189 std::string* label); |
| 185 void AddRequest(DeviceRequest* new_request, std::string* label); | 190 void AddRequest(DeviceRequest* new_request, std::string* label); |
| 186 bool HasEnumerationRequest(MediaStreamType type); | 191 bool HasEnumerationRequest(MediaStreamType type); |
| 187 bool HasEnumerationRequest(); | 192 bool HasEnumerationRequest(); |
| 188 void ClearEnumerationCache(EnumerationCache* cache); | 193 void ClearEnumerationCache(EnumerationCache* cache); |
| 189 | 194 |
| 190 // Helper to ensure the device thread and pass the message loop to device | 195 // 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. | 196 // Auto-starts the device thread and registers this as a listener with the |
| 192 void EnsureDeviceThreadAndListener(); | 197 // device managers. |
| 198 void EnsureDeviceManagerStarted(MediaStreamType stream_type); |
| 193 | 199 |
| 194 // Sends cached device list to a client corresponding to the request | 200 // Sends cached device list to a client corresponding to the request |
| 195 // identified by |label|. | 201 // identified by |label|. |
| 196 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); | 202 void SendCachedDeviceList(EnumerationCache* cache, const std::string& label); |
| 197 | 203 |
| 198 // Stop the request of enumerating devices indentified by |label|. | 204 // Stop the request of enumerating devices indentified by |label|. |
| 199 void StopEnumerateDevices(const std::string& label); | 205 void StopEnumerateDevices(const std::string& label); |
| 200 | 206 |
| 201 // Helpers to start and stop monitoring devices. | 207 // Helpers to start and stop monitoring devices. |
| 202 void StartMonitoring(); | 208 void StartMonitoring(); |
| 203 void StopMonitoring(); | 209 void StopMonitoring(); |
| 204 | 210 |
| 205 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. | 211 // Device thread shared by VideoCaptureManager and AudioInputDeviceManager. |
| 206 scoped_ptr<base::Thread> device_thread_; | 212 scoped_ptr<base::Thread> device_thread_; |
| 207 | 213 |
| 208 scoped_ptr<MediaStreamDeviceSettings> device_settings_; | 214 scoped_ptr<MediaStreamDeviceSettings> device_settings_; |
| 209 scoped_refptr<AudioInputDeviceManager> audio_input_device_manager_; | 215 |
| 210 scoped_refptr<VideoCaptureManager> video_capture_manager_; | 216 media::AudioManager* audio_manager_; // not owned |
| 217 |
| 218 // Device managers, instantiated on-demand. |
| 219 scoped_refptr<MediaStreamProvider> device_manager_[ |
| 220 content::NUM_MEDIA_STREAM_DEVICE_TYPES]; |
| 211 | 221 |
| 212 // Indicator of device monitoring state. | 222 // Indicator of device monitoring state. |
| 213 bool monitoring_started_; | 223 bool monitoring_started_; |
| 214 | 224 |
| 215 // Stores most recently enumerated device lists. The cache is cleared when | 225 // 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. | 226 // monitoring is stopped or there is no request for that type of device. |
| 217 EnumerationCache audio_enumeration_cache_; | 227 EnumerationCache user_audio_enumeration_cache_; |
| 218 EnumerationCache video_enumeration_cache_; | 228 EnumerationCache user_video_enumeration_cache_; |
| 219 | 229 |
| 220 // Keeps track of live enumeration commands sent to VideoCaptureManager or | 230 // Keeps track of live enumeration commands sent to VideoCaptureManager or |
| 221 // AudioInputDeviceManager, in order to only enumerate when necessary. | 231 // AudioInputDeviceManager, in order to only enumerate when necessary. |
| 222 int active_enumeration_ref_count_[content::NUM_MEDIA_STREAM_DEVICE_TYPES]; | 232 int active_enumeration_ref_count_[content::NUM_MEDIA_STREAM_DEVICE_TYPES]; |
| 223 | 233 |
| 224 // All non-closed request. | 234 // All non-closed request. |
| 225 typedef std::map<std::string, DeviceRequest> DeviceRequests; | 235 typedef std::map<std::string, DeviceRequest> DeviceRequests; |
| 226 DeviceRequests requests_; | 236 DeviceRequests requests_; |
| 227 | 237 |
| 228 // Hold a pointer to the IO loop to check we delete the device thread and | 238 // Hold a pointer to the IO loop to check we delete the device thread and |
| 229 // managers on the right thread. | 239 // managers on the right thread. |
| 230 MessageLoop* io_loop_; | 240 MessageLoop* io_loop_; |
| 231 | 241 |
| 232 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); | 242 DISALLOW_COPY_AND_ASSIGN(MediaStreamManager); |
| 233 }; | 243 }; |
| 234 | 244 |
| 235 } // namespace media_stream | 245 } // namespace media_stream |
| 236 | 246 |
| 237 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ | 247 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_MANAGER_H_ |
| OLD | NEW |