- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / media_capture_devices_dispatcher.h
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.
4
5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
6 #define CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
7
8 #include <deque>
9 #include <map>
10
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"
20
21 class AudioStreamIndicator;
22 class DesktopStreamsRegistry;
23 class MediaStreamCaptureIndicator;
24 class Profile;
25
26 namespace extensions {
27 class Extension;
28 }
29
30 namespace user_prefs {
31 class PrefRegistrySyncable;
32 }
33
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) {}
45
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) {}
50
51     // Handle an information update related to a media stream request.
52     virtual void OnRequestUpdate(
53         int render_process_id,
54         int render_view_id,
55         const content::MediaStreamDevice& device,
56         const content::MediaRequestState state) {}
57
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) {}
61
62     virtual ~Observer() {}
63   };
64
65   static MediaCaptureDevicesDispatcher* GetInstance();
66
67   // Registers the preferences related to Media Stream default devices.
68   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
69
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();
77
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);
86
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
90   // OS.
91   // Called on the UI thread.
92   void GetDefaultDevicesForProfile(Profile* profile,
93                                    bool audio,
94                                    bool video,
95                                    content::MediaStreamDevices* devices);
96
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);
103
104   // Returns the first available audio or video device, or NULL if no devices
105   // are available.
106   const content::MediaStreamDevice* GetFirstAvailableAudioDevice();
107   const content::MediaStreamDevice* GetFirstAvailableVideoDevice();
108
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
111   // signleton.
112   void DisableDeviceEnumerationForTesting();
113
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,
121       int render_view_id,
122       int page_request_id,
123       const content::MediaStreamDevice& device,
124       content::MediaRequestState state) OVERRIDE;
125   virtual void OnAudioStreamPlayingChanged(
126       int render_process_id,
127       int render_view_id,
128       int stream_id,
129       bool is_playing,
130       float power_dBFS,
131       bool clipped) OVERRIDE;
132   virtual void OnCreatingAudioStream(int render_process_id,
133                                      int render_view_id) OVERRIDE;
134
135   scoped_refptr<MediaStreamCaptureIndicator> GetMediaStreamCaptureIndicator();
136
137   scoped_refptr<AudioStreamIndicator> GetAudioStreamIndicator();
138
139   DesktopStreamsRegistry* GetDesktopStreamsRegistry();
140
141  private:
142   friend struct DefaultSingletonTraits<MediaCaptureDevicesDispatcher>;
143
144   struct PendingAccessRequest {
145     PendingAccessRequest(const content::MediaStreamRequest& request,
146                          const content::MediaResponseCallback& callback);
147     ~PendingAccessRequest();
148
149     content::MediaStreamRequest request;
150     content::MediaResponseCallback callback;
151   };
152   typedef std::deque<PendingAccessRequest> RequestsQueue;
153   typedef std::map<content::WebContents*, RequestsQueue> RequestsQueues;
154
155   MediaCaptureDevicesDispatcher();
156   virtual ~MediaCaptureDevicesDispatcher();
157
158   // content::NotificationObserver implementation.
159   virtual void Observe(int type,
160                        const content::NotificationSource& source,
161                        const content::NotificationDetails& details) OVERRIDE;
162
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);
192
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,
198       int render_view_id,
199       int page_request_id,
200       const content::MediaStreamDevice& device,
201       content::MediaRequestState state);
202   void OnCreatingAudioStreamOnUIThread(int render_process_id,
203                                        int render_view_id);
204
205   // A list of cached audio capture devices.
206   content::MediaStreamDevices audio_devices_;
207
208   // A list of cached video capture devices.
209   content::MediaStreamDevices video_devices_;
210
211   // A list of observers for the device update notifications.
212   ObserverList<Observer> observers_;
213
214   // Flag to indicate if device enumeration has been done/doing.
215   // Only accessed on UI thread.
216   bool devices_enumerated_;
217
218   // Flag used by unittests to disable device enumeration.
219   bool is_device_enumeration_disabled_;
220
221   RequestsQueues pending_requests_;
222
223   scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_;
224
225   scoped_refptr<AudioStreamIndicator> audio_stream_indicator_;
226
227   scoped_ptr<DesktopStreamsRegistry> desktop_streams_registry_;
228
229   content::NotificationRegistrar notifications_registrar_;
230
231   DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher);
232 };
233
234 #endif  // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_