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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
6 #define CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/observer_list.h"
15 #include "content/public/browser/media_observer.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/common/media_stream_request.h"
21 class AudioStreamIndicator;
22 class DesktopStreamsRegistry;
23 class MediaStreamCaptureIndicator;
26 namespace extensions {
30 namespace user_prefs {
31 class PrefRegistrySyncable;
34 // This singleton is used to receive updates about media events from the content
36 class MediaCaptureDevicesDispatcher : public content::MediaObserver,
37 public content::NotificationObserver {
41 // Handle an information update consisting of a up-to-date audio capture
42 // device lists. This happens when a microphone is plugged in or unplugged.
43 virtual void OnUpdateAudioDevices(
44 const content::MediaStreamDevices& devices) {}
46 // Handle an information update consisting of a up-to-date video capture
47 // device lists. This happens when a camera is plugged in or unplugged.
48 virtual void OnUpdateVideoDevices(
49 const content::MediaStreamDevices& devices) {}
51 // Handle an information update related to a media stream request.
52 virtual void OnRequestUpdate(
53 int render_process_id,
55 const content::MediaStreamDevice& device,
56 const content::MediaRequestState state) {}
58 // Handle an information update that a new stream is being created.
59 virtual void OnCreatingAudioStream(int render_process_id,
60 int render_view_id) {}
62 virtual ~Observer() {}
65 static MediaCaptureDevicesDispatcher* GetInstance();
67 // Registers the preferences related to Media Stream default devices.
68 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
70 // Methods for observers. Called on UI thread.
71 // Observers should add themselves on construction and remove themselves
73 void AddObserver(Observer* observer);
74 void RemoveObserver(Observer* observer);
75 const content::MediaStreamDevices& GetAudioCaptureDevices();
76 const content::MediaStreamDevices& GetVideoCaptureDevices();
78 // Method called from WebCapturerDelegate implementations to process access
79 // requests. |extension| is set to NULL if request was made from a drive-by
81 void ProcessMediaAccessRequest(
82 content::WebContents* web_contents,
83 const content::MediaStreamRequest& request,
84 const content::MediaResponseCallback& callback,
85 const extensions::Extension* extension);
87 // Helper to get the default devices which can be used by the media request.
88 // Uses the first available devices if the default devices are not available.
89 // If the return list is empty, it means there is no available device on the
91 // Called on the UI thread.
92 void GetDefaultDevicesForProfile(Profile* profile,
95 content::MediaStreamDevices* devices);
97 // Helpers for picking particular requested devices, identified by raw id.
98 // If the device requested is not available it will return NULL.
99 const content::MediaStreamDevice*
100 GetRequestedAudioDevice(const std::string& requested_audio_device_id);
101 const content::MediaStreamDevice*
102 GetRequestedVideoDevice(const std::string& requested_video_device_id);
104 // Returns the first available audio or video device, or NULL if no devices
106 const content::MediaStreamDevice* GetFirstAvailableAudioDevice();
107 const content::MediaStreamDevice* GetFirstAvailableVideoDevice();
109 // Unittests that do not require actual device enumeration should call this
110 // API on the singleton. It is safe to call this multiple times on the
112 void DisableDeviceEnumerationForTesting();
114 // Overridden from content::MediaObserver:
115 virtual void OnAudioCaptureDevicesChanged(
116 const content::MediaStreamDevices& devices) OVERRIDE;
117 virtual void OnVideoCaptureDevicesChanged(
118 const content::MediaStreamDevices& devices) OVERRIDE;
119 virtual void OnMediaRequestStateChanged(
120 int render_process_id,
123 const content::MediaStreamDevice& device,
124 content::MediaRequestState state) OVERRIDE;
125 virtual void OnAudioStreamPlayingChanged(
126 int render_process_id,
131 bool clipped) OVERRIDE;
132 virtual void OnCreatingAudioStream(int render_process_id,
133 int render_view_id) OVERRIDE;
135 scoped_refptr<MediaStreamCaptureIndicator> GetMediaStreamCaptureIndicator();
137 scoped_refptr<AudioStreamIndicator> GetAudioStreamIndicator();
139 DesktopStreamsRegistry* GetDesktopStreamsRegistry();
142 friend struct DefaultSingletonTraits<MediaCaptureDevicesDispatcher>;
144 struct PendingAccessRequest {
145 PendingAccessRequest(const content::MediaStreamRequest& request,
146 const content::MediaResponseCallback& callback);
147 ~PendingAccessRequest();
149 content::MediaStreamRequest request;
150 content::MediaResponseCallback callback;
152 typedef std::deque<PendingAccessRequest> RequestsQueue;
153 typedef std::map<content::WebContents*, RequestsQueue> RequestsQueues;
155 MediaCaptureDevicesDispatcher();
156 virtual ~MediaCaptureDevicesDispatcher();
158 // content::NotificationObserver implementation.
159 virtual void Observe(int type,
160 const content::NotificationSource& source,
161 const content::NotificationDetails& details) OVERRIDE;
163 // Helpers for ProcessMediaAccessRequest().
164 void ProcessDesktopCaptureAccessRequest(
165 content::WebContents* web_contents,
166 const content::MediaStreamRequest& request,
167 const content::MediaResponseCallback& callback,
168 const extensions::Extension* extension);
169 void ProcessScreenCaptureAccessRequest(
170 content::WebContents* web_contents,
171 const content::MediaStreamRequest& request,
172 const content::MediaResponseCallback& callback,
173 const extensions::Extension* extension);
174 void ProcessTabCaptureAccessRequest(
175 content::WebContents* web_contents,
176 const content::MediaStreamRequest& request,
177 const content::MediaResponseCallback& callback,
178 const extensions::Extension* extension);
179 void ProcessMediaAccessRequestFromPlatformAppOrExtension(
180 content::WebContents* web_contents,
181 const content::MediaStreamRequest& request,
182 const content::MediaResponseCallback& callback,
183 const extensions::Extension* extension);
184 void ProcessRegularMediaAccessRequest(
185 content::WebContents* web_contents,
186 const content::MediaStreamRequest& request,
187 const content::MediaResponseCallback& callback);
188 void ProcessQueuedAccessRequest(content::WebContents* web_contents);
189 void OnAccessRequestResponse(content::WebContents* web_contents,
190 const content::MediaStreamDevices& devices,
191 scoped_ptr<content::MediaStreamUI> ui);
193 // Called by the MediaObserver() functions, executed on UI thread.
194 void UpdateAudioDevicesOnUIThread(const content::MediaStreamDevices& devices);
195 void UpdateVideoDevicesOnUIThread(const content::MediaStreamDevices& devices);
196 void UpdateMediaRequestStateOnUIThread(
197 int render_process_id,
200 const content::MediaStreamDevice& device,
201 content::MediaRequestState state);
202 void OnCreatingAudioStreamOnUIThread(int render_process_id,
205 // A list of cached audio capture devices.
206 content::MediaStreamDevices audio_devices_;
208 // A list of cached video capture devices.
209 content::MediaStreamDevices video_devices_;
211 // A list of observers for the device update notifications.
212 ObserverList<Observer> observers_;
214 // Flag to indicate if device enumeration has been done/doing.
215 // Only accessed on UI thread.
216 bool devices_enumerated_;
218 // Flag used by unittests to disable device enumeration.
219 bool is_device_enumeration_disabled_;
221 RequestsQueues pending_requests_;
223 scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_;
225 scoped_refptr<AudioStreamIndicator> audio_stream_indicator_;
227 scoped_ptr<DesktopStreamsRegistry> desktop_streams_registry_;
229 content::NotificationRegistrar notifications_registrar_;
231 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher);
234 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_