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/scoped_vector.h"
15 #include "base/memory/singleton.h"
16 #include "base/observer_list.h"
17 #include "content/public/browser/media_observer.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/common/media_stream_request.h"
21 class DesktopStreamsRegistry
;
22 class MediaAccessHandler
;
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
{
40 // Handle an information update consisting of a up-to-date audio capture
41 // device lists. This happens when a microphone is plugged in or unplugged.
42 virtual void OnUpdateAudioDevices(
43 const content::MediaStreamDevices
& devices
) {}
45 // Handle an information update consisting of a up-to-date video capture
46 // device lists. This happens when a camera is plugged in or unplugged.
47 virtual void OnUpdateVideoDevices(
48 const content::MediaStreamDevices
& devices
) {}
50 // Handle an information update related to a media stream request.
51 virtual void OnRequestUpdate(
52 int render_process_id
,
54 content::MediaStreamType stream_type
,
55 const content::MediaRequestState state
) {}
57 // Handle an information update that a new stream is being created.
58 virtual void OnCreatingAudioStream(int render_process_id
,
59 int render_frame_id
) {}
61 virtual ~Observer() {}
64 static MediaCaptureDevicesDispatcher
* GetInstance();
66 // Registers the preferences related to Media Stream default devices.
67 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
69 // Returns true if the security origin is associated with casting.
70 static bool IsOriginForCasting(const GURL
& origin
);
72 // Methods for observers. Called on UI thread.
73 // Observers should add themselves on construction and remove themselves
75 void AddObserver(Observer
* observer
);
76 void RemoveObserver(Observer
* observer
);
77 const content::MediaStreamDevices
& GetAudioCaptureDevices();
78 const content::MediaStreamDevices
& GetVideoCaptureDevices();
80 // Method called from WebCapturerDelegate implementations to process access
81 // requests. |extension| is set to NULL if request was made from a drive-by
83 void ProcessMediaAccessRequest(
84 content::WebContents
* web_contents
,
85 const content::MediaStreamRequest
& request
,
86 const content::MediaResponseCallback
& callback
,
87 const extensions::Extension
* extension
);
89 // Method called from WebCapturerDelegate implementations to check media
90 // access permission. Note that this does not query the user.
91 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
92 const GURL
& security_origin
,
93 content::MediaStreamType type
);
95 // Same as above but for an |extension|, which may not be NULL.
96 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
97 const GURL
& security_origin
,
98 content::MediaStreamType type
,
99 const extensions::Extension
* extension
);
101 // Helper to get the default devices which can be used by the media request.
102 // Uses the first available devices if the default devices are not available.
103 // If the return list is empty, it means there is no available device on the
105 // Called on the UI thread.
106 void GetDefaultDevicesForProfile(Profile
* profile
,
109 content::MediaStreamDevices
* devices
);
111 // Helpers for picking particular requested devices, identified by raw id.
112 // If the device requested is not available it will return NULL.
113 const content::MediaStreamDevice
*
114 GetRequestedAudioDevice(const std::string
& requested_audio_device_id
);
115 const content::MediaStreamDevice
*
116 GetRequestedVideoDevice(const std::string
& requested_video_device_id
);
118 // Returns the first available audio or video device, or NULL if no devices
120 const content::MediaStreamDevice
* GetFirstAvailableAudioDevice();
121 const content::MediaStreamDevice
* GetFirstAvailableVideoDevice();
123 // Unittests that do not require actual device enumeration should call this
124 // API on the singleton. It is safe to call this multiple times on the
126 void DisableDeviceEnumerationForTesting();
128 // Overridden from content::MediaObserver:
129 void OnAudioCaptureDevicesChanged() override
;
130 void OnVideoCaptureDevicesChanged() override
;
131 void OnMediaRequestStateChanged(int render_process_id
,
134 const GURL
& security_origin
,
135 content::MediaStreamType stream_type
,
136 content::MediaRequestState state
) override
;
137 void OnCreatingAudioStream(int render_process_id
,
138 int render_frame_id
) override
;
140 scoped_refptr
<MediaStreamCaptureIndicator
> GetMediaStreamCaptureIndicator();
142 DesktopStreamsRegistry
* GetDesktopStreamsRegistry();
144 bool IsDesktopCaptureInProgress();
147 void SetTestAudioCaptureDevices(const content::MediaStreamDevices
& devices
);
148 void SetTestVideoCaptureDevices(const content::MediaStreamDevices
& devices
);
151 friend struct DefaultSingletonTraits
<MediaCaptureDevicesDispatcher
>;
153 MediaCaptureDevicesDispatcher();
154 ~MediaCaptureDevicesDispatcher() override
;
156 // Called by the MediaObserver() functions, executed on UI thread.
157 void NotifyAudioDevicesChangedOnUIThread();
158 void NotifyVideoDevicesChangedOnUIThread();
159 void UpdateMediaRequestStateOnUIThread(
160 int render_process_id
,
163 const GURL
& security_origin
,
164 content::MediaStreamType stream_type
,
165 content::MediaRequestState state
);
166 void OnCreatingAudioStreamOnUIThread(int render_process_id
,
167 int render_frame_id
);
169 // Only for testing, a list of cached audio capture devices.
170 content::MediaStreamDevices test_audio_devices_
;
172 // Only for testing, a list of cached video capture devices.
173 content::MediaStreamDevices test_video_devices_
;
175 // A list of observers for the device update notifications.
176 base::ObserverList
<Observer
> observers_
;
178 // Flag used by unittests to disable device enumeration.
179 bool is_device_enumeration_disabled_
;
181 scoped_refptr
<MediaStreamCaptureIndicator
> media_stream_capture_indicator_
;
183 scoped_ptr
<DesktopStreamsRegistry
> desktop_streams_registry_
;
185 // Handlers for processing media access requests.
186 ScopedVector
<MediaAccessHandler
> media_access_handlers_
;
188 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher
);
191 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_