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_
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "base/observer_list.h"
16 #include "content/public/browser/media_observer.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_contents_delegate.h"
20 #include "content/public/common/media_stream_request.h"
22 class AudioStreamIndicator
;
23 class DesktopStreamsRegistry
;
24 class MediaStreamCaptureIndicator
;
27 namespace extensions
{
31 namespace user_prefs
{
32 class PrefRegistrySyncable
;
35 // This singleton is used to receive updates about media events from the content
37 class MediaCaptureDevicesDispatcher
: public content::MediaObserver
,
38 public content::NotificationObserver
{
42 // Handle an information update consisting of a up-to-date audio capture
43 // device lists. This happens when a microphone is plugged in or unplugged.
44 virtual void OnUpdateAudioDevices(
45 const content::MediaStreamDevices
& devices
) {}
47 // Handle an information update consisting of a up-to-date video capture
48 // device lists. This happens when a camera is plugged in or unplugged.
49 virtual void OnUpdateVideoDevices(
50 const content::MediaStreamDevices
& devices
) {}
52 // Handle an information update related to a media stream request.
53 virtual void OnRequestUpdate(
54 int render_process_id
,
56 const content::MediaStreamDevice
& device
,
57 const content::MediaRequestState state
) {}
59 // Handle an information update that a new stream is being created.
60 virtual void OnCreatingAudioStream(int render_process_id
,
61 int render_view_id
) {}
63 virtual ~Observer() {}
66 static MediaCaptureDevicesDispatcher
* GetInstance();
68 // Registers the preferences related to Media Stream default devices.
69 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
71 // Methods for observers. Called on UI thread.
72 // Observers should add themselves on construction and remove themselves
74 void AddObserver(Observer
* observer
);
75 void RemoveObserver(Observer
* observer
);
76 const content::MediaStreamDevices
& GetAudioCaptureDevices();
77 const content::MediaStreamDevices
& GetVideoCaptureDevices();
79 // Method called from WebCapturerDelegate implementations to process access
80 // requests. |extension| is set to NULL if request was made from a drive-by
82 void ProcessMediaAccessRequest(
83 content::WebContents
* web_contents
,
84 const content::MediaStreamRequest
& request
,
85 const content::MediaResponseCallback
& callback
,
86 const extensions::Extension
* extension
);
88 // Helper to get the default devices which can be used by the media request.
89 // Uses the first available devices if the default devices are not available.
90 // If the return list is empty, it means there is no available device on the
92 // Called on the UI thread.
93 void GetDefaultDevicesForProfile(Profile
* profile
,
96 content::MediaStreamDevices
* devices
);
98 // Helpers for picking particular requested devices, identified by raw id.
99 // If the device requested is not available it will return NULL.
100 const content::MediaStreamDevice
*
101 GetRequestedAudioDevice(const std::string
& requested_audio_device_id
);
102 const content::MediaStreamDevice
*
103 GetRequestedVideoDevice(const std::string
& requested_video_device_id
);
105 // Returns the first available audio or video device, or NULL if no devices
107 const content::MediaStreamDevice
* GetFirstAvailableAudioDevice();
108 const content::MediaStreamDevice
* GetFirstAvailableVideoDevice();
110 // Unittests that do not require actual device enumeration should call this
111 // API on the singleton. It is safe to call this multiple times on the
113 void DisableDeviceEnumerationForTesting();
115 // Overridden from content::MediaObserver:
116 virtual void OnAudioCaptureDevicesChanged(
117 const content::MediaStreamDevices
& devices
) OVERRIDE
;
118 virtual void OnVideoCaptureDevicesChanged(
119 const content::MediaStreamDevices
& devices
) OVERRIDE
;
120 virtual void OnMediaRequestStateChanged(
121 int render_process_id
,
124 const GURL
& security_origin
,
125 const content::MediaStreamDevice
& device
,
126 content::MediaRequestState state
) OVERRIDE
;
127 virtual void OnAudioStreamPlayingChanged(
128 int render_process_id
,
133 bool clipped
) OVERRIDE
;
134 virtual void OnCreatingAudioStream(int render_process_id
,
135 int render_view_id
) OVERRIDE
;
137 scoped_refptr
<MediaStreamCaptureIndicator
> GetMediaStreamCaptureIndicator();
139 scoped_refptr
<AudioStreamIndicator
> GetAudioStreamIndicator();
141 DesktopStreamsRegistry
* GetDesktopStreamsRegistry();
143 bool IsDesktopCaptureInProgress();
146 friend struct DefaultSingletonTraits
<MediaCaptureDevicesDispatcher
>;
148 struct PendingAccessRequest
{
149 PendingAccessRequest(const content::MediaStreamRequest
& request
,
150 const content::MediaResponseCallback
& callback
);
151 ~PendingAccessRequest();
153 content::MediaStreamRequest request
;
154 content::MediaResponseCallback callback
;
156 typedef std::deque
<PendingAccessRequest
> RequestsQueue
;
157 typedef std::map
<content::WebContents
*, RequestsQueue
> RequestsQueues
;
159 MediaCaptureDevicesDispatcher();
160 virtual ~MediaCaptureDevicesDispatcher();
162 // content::NotificationObserver implementation.
163 virtual void Observe(int type
,
164 const content::NotificationSource
& source
,
165 const content::NotificationDetails
& details
) OVERRIDE
;
167 // Helpers for ProcessMediaAccessRequest().
168 void ProcessDesktopCaptureAccessRequest(
169 content::WebContents
* web_contents
,
170 const content::MediaStreamRequest
& request
,
171 const content::MediaResponseCallback
& callback
,
172 const extensions::Extension
* extension
);
173 void ProcessScreenCaptureAccessRequest(
174 content::WebContents
* web_contents
,
175 const content::MediaStreamRequest
& request
,
176 const content::MediaResponseCallback
& callback
,
177 const extensions::Extension
* extension
);
178 void ProcessTabCaptureAccessRequest(
179 content::WebContents
* web_contents
,
180 const content::MediaStreamRequest
& request
,
181 const content::MediaResponseCallback
& callback
,
182 const extensions::Extension
* extension
);
183 void ProcessMediaAccessRequestFromPlatformAppOrExtension(
184 content::WebContents
* web_contents
,
185 const content::MediaStreamRequest
& request
,
186 const content::MediaResponseCallback
& callback
,
187 const extensions::Extension
* extension
);
188 void ProcessRegularMediaAccessRequest(
189 content::WebContents
* web_contents
,
190 const content::MediaStreamRequest
& request
,
191 const content::MediaResponseCallback
& callback
);
192 void ProcessQueuedAccessRequest(content::WebContents
* web_contents
);
193 void OnAccessRequestResponse(content::WebContents
* web_contents
,
194 const content::MediaStreamDevices
& devices
,
195 scoped_ptr
<content::MediaStreamUI
> ui
);
197 // Called by the MediaObserver() functions, executed on UI thread.
198 void UpdateAudioDevicesOnUIThread(const content::MediaStreamDevices
& devices
);
199 void UpdateVideoDevicesOnUIThread(const content::MediaStreamDevices
& devices
);
200 void UpdateMediaRequestStateOnUIThread(
201 int render_process_id
,
204 const GURL
& security_origin
,
205 const content::MediaStreamDevice
& device
,
206 content::MediaRequestState state
);
207 void OnCreatingAudioStreamOnUIThread(int render_process_id
,
210 // A list of cached audio capture devices.
211 content::MediaStreamDevices audio_devices_
;
213 // A list of cached video capture devices.
214 content::MediaStreamDevices video_devices_
;
216 // A list of observers for the device update notifications.
217 ObserverList
<Observer
> observers_
;
219 // Flag to indicate if device enumeration has been done/doing.
220 // Only accessed on UI thread.
221 bool devices_enumerated_
;
223 // Flag used by unittests to disable device enumeration.
224 bool is_device_enumeration_disabled_
;
226 RequestsQueues pending_requests_
;
228 scoped_refptr
<MediaStreamCaptureIndicator
> media_stream_capture_indicator_
;
230 scoped_refptr
<AudioStreamIndicator
> audio_stream_indicator_
;
232 scoped_ptr
<DesktopStreamsRegistry
> desktop_streams_registry_
;
234 content::NotificationRegistrar notifications_registrar_
;
236 // Tracks MEDIA_DESKTOP_VIDEO_CAPTURE sessions which reach the
237 // MEDIA_REQUEST_STATE_DONE state. Sessions are remove when
238 // MEDIA_REQUEST_STATE_CLOSING is encountered.
239 struct DesktopCaptureSession
{
240 int render_process_id
;
244 typedef std::list
<DesktopCaptureSession
> DesktopCaptureSessions
;
245 DesktopCaptureSessions desktop_capture_sessions_
;
247 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher
);
250 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_