Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / media / media_capture_devices_dispatcher.h
blob3523d46e5b80e831c4c529322cbe387fb5630788
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_
8 #include <deque>
9 #include <list>
10 #include <map>
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 DesktopStreamsRegistry;
23 class MediaStreamCaptureIndicator;
24 class Profile;
26 namespace extensions {
27 class Extension;
30 namespace user_prefs {
31 class PrefRegistrySyncable;
34 // This singleton is used to receive updates about media events from the content
35 // layer.
36 class MediaCaptureDevicesDispatcher : public content::MediaObserver,
37 public content::NotificationObserver {
38 public:
39 class Observer {
40 public:
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,
54 int render_frame_id,
55 content::MediaStreamType stream_type,
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_frame_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
72 // on destruction.
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
80 // page.
81 void ProcessMediaAccessRequest(
82 content::WebContents* web_contents,
83 const content::MediaStreamRequest& request,
84 const content::MediaResponseCallback& callback,
85 const extensions::Extension* extension);
87 // Checks if we have media access permission. Note that this only checks the
88 // settings and does not query the user.
89 bool CheckMediaAccessPermission(content::BrowserContext* browser_context,
90 const GURL& security_origin,
91 content::MediaStreamType type);
93 // Method called from WebCapturerDelegate implementations to check media
94 // access permission. Note that this does not query the user.
95 bool CheckMediaAccessPermission(content::WebContents* web_contents,
96 const GURL& security_origin,
97 content::MediaStreamType type);
99 // Same as above but for an |extension|, which may not be NULL.
100 #if defined(ENABLE_EXTENSIONS)
101 bool CheckMediaAccessPermission(content::WebContents* web_contents,
102 const GURL& security_origin,
103 content::MediaStreamType type,
104 const extensions::Extension* extension);
105 #endif
107 // Helper to get the default devices which can be used by the media request.
108 // Uses the first available devices if the default devices are not available.
109 // If the return list is empty, it means there is no available device on the
110 // OS.
111 // Called on the UI thread.
112 void GetDefaultDevicesForProfile(Profile* profile,
113 bool audio,
114 bool video,
115 content::MediaStreamDevices* devices);
117 // Helpers for picking particular requested devices, identified by raw id.
118 // If the device requested is not available it will return NULL.
119 const content::MediaStreamDevice*
120 GetRequestedAudioDevice(const std::string& requested_audio_device_id);
121 const content::MediaStreamDevice*
122 GetRequestedVideoDevice(const std::string& requested_video_device_id);
124 // Returns the first available audio or video device, or NULL if no devices
125 // are available.
126 const content::MediaStreamDevice* GetFirstAvailableAudioDevice();
127 const content::MediaStreamDevice* GetFirstAvailableVideoDevice();
129 // Unittests that do not require actual device enumeration should call this
130 // API on the singleton. It is safe to call this multiple times on the
131 // signleton.
132 void DisableDeviceEnumerationForTesting();
134 // Overridden from content::MediaObserver:
135 void OnAudioCaptureDevicesChanged() override;
136 void OnVideoCaptureDevicesChanged() override;
137 void OnMediaRequestStateChanged(int render_process_id,
138 int render_frame_id,
139 int page_request_id,
140 const GURL& security_origin,
141 content::MediaStreamType stream_type,
142 content::MediaRequestState state) override;
143 void OnCreatingAudioStream(int render_process_id,
144 int render_frame_id) override;
146 scoped_refptr<MediaStreamCaptureIndicator> GetMediaStreamCaptureIndicator();
148 DesktopStreamsRegistry* GetDesktopStreamsRegistry();
150 bool IsDesktopCaptureInProgress();
152 // Only for testing.
153 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices);
154 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices);
156 private:
157 friend struct DefaultSingletonTraits<MediaCaptureDevicesDispatcher>;
159 struct PendingAccessRequest {
160 PendingAccessRequest(const content::MediaStreamRequest& request,
161 const content::MediaResponseCallback& callback);
162 ~PendingAccessRequest();
164 // TODO(gbillock): make the MediaStreamDevicesController owned by
165 // this object when we're using bubbles.
166 content::MediaStreamRequest request;
167 content::MediaResponseCallback callback;
169 typedef std::deque<PendingAccessRequest> RequestsQueue;
170 typedef std::map<content::WebContents*, RequestsQueue> RequestsQueues;
172 MediaCaptureDevicesDispatcher();
173 ~MediaCaptureDevicesDispatcher() override;
175 // content::NotificationObserver implementation.
176 void Observe(int type,
177 const content::NotificationSource& source,
178 const content::NotificationDetails& details) override;
180 // Helpers for ProcessMediaAccessRequest().
181 void ProcessDesktopCaptureAccessRequest(
182 content::WebContents* web_contents,
183 const content::MediaStreamRequest& request,
184 const content::MediaResponseCallback& callback,
185 const extensions::Extension* extension);
186 void ProcessScreenCaptureAccessRequest(
187 content::WebContents* web_contents,
188 const content::MediaStreamRequest& request,
189 const content::MediaResponseCallback& callback,
190 const extensions::Extension* extension);
191 void ProcessTabCaptureAccessRequest(
192 content::WebContents* web_contents,
193 const content::MediaStreamRequest& request,
194 const content::MediaResponseCallback& callback,
195 const extensions::Extension* extension);
196 #if defined(ENABLE_EXTENSIONS)
197 void ProcessMediaAccessRequestFromPlatformAppOrExtension(
198 content::WebContents* web_contents,
199 const content::MediaStreamRequest& request,
200 const content::MediaResponseCallback& callback,
201 const extensions::Extension* extension);
202 #endif
203 void ProcessRegularMediaAccessRequest(
204 content::WebContents* web_contents,
205 const content::MediaStreamRequest& request,
206 const content::MediaResponseCallback& callback);
207 void ProcessQueuedAccessRequest(content::WebContents* web_contents);
208 void OnAccessRequestResponse(content::WebContents* web_contents,
209 const content::MediaStreamDevices& devices,
210 content::MediaStreamRequestResult result,
211 scoped_ptr<content::MediaStreamUI> ui);
213 // Called by the MediaObserver() functions, executed on UI thread.
214 void NotifyAudioDevicesChangedOnUIThread();
215 void NotifyVideoDevicesChangedOnUIThread();
216 void UpdateMediaRequestStateOnUIThread(
217 int render_process_id,
218 int render_frame_id,
219 int page_request_id,
220 const GURL& security_origin,
221 content::MediaStreamType stream_type,
222 content::MediaRequestState state);
223 void OnCreatingAudioStreamOnUIThread(int render_process_id,
224 int render_frame_id);
226 // Only for testing, a list of cached audio capture devices.
227 content::MediaStreamDevices test_audio_devices_;
229 // Only for testing, a list of cached video capture devices.
230 content::MediaStreamDevices test_video_devices_;
232 // A list of observers for the device update notifications.
233 ObserverList<Observer> observers_;
235 // Flag used by unittests to disable device enumeration.
236 bool is_device_enumeration_disabled_;
238 RequestsQueues pending_requests_;
240 scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_;
242 scoped_ptr<DesktopStreamsRegistry> desktop_streams_registry_;
244 content::NotificationRegistrar notifications_registrar_;
246 // Tracks MEDIA_DESKTOP_VIDEO_CAPTURE sessions which reach the
247 // MEDIA_REQUEST_STATE_DONE state. Sessions are remove when
248 // MEDIA_REQUEST_STATE_CLOSING is encountered.
249 struct DesktopCaptureSession {
250 int render_process_id;
251 int render_frame_id;
252 int page_request_id;
254 typedef std::list<DesktopCaptureSession> DesktopCaptureSessions;
255 DesktopCaptureSessions desktop_capture_sessions_;
257 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher);
260 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_