Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / media / media_capture_devices_dispatcher.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
7 #define XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
8
9 #include <string>
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/web_contents.h"
17 #include "content/public/common/media_stream_request.h"
18
19 // This singleton is used to receive updates about media events from the content
20 // layer. Based on chrome/browser/media/media_capture_devices_dispatcher.[h|cc].
21 class XWalkMediaCaptureDevicesDispatcher : public content::MediaObserver {
22  public:
23   class Observer {
24    public:
25     // Handle an information update consisting of a up-to-date audio capture
26     // device lists. This happens when a microphone is plugged in or unplugged.
27     virtual void OnUpdateAudioDevices(
28         const content::MediaStreamDevices& devices) {}
29
30     // Handle an information update consisting of a up-to-date video capture
31     // device lists. This happens when a camera is plugged in or unplugged.
32     virtual void OnUpdateVideoDevices(
33         const content::MediaStreamDevices& devices) {}
34
35     // Handle an information update related to a media stream request.
36     virtual void OnRequestUpdate(
37         int render_process_id,
38         int render_frame_id,
39         content::MediaStreamType stream_type,
40         const content::MediaRequestState state) {}
41
42     virtual ~Observer() {}
43   };
44
45   static XWalkMediaCaptureDevicesDispatcher* GetInstance();
46
47   static void RunRequestMediaAccessPermission(
48       content::WebContents* web_contents,
49       const content::MediaStreamRequest& request,
50       const content::MediaResponseCallback& callback);
51
52   // Methods for observers. Called on UI thread.
53   // Observers should add themselves on construction and remove themselves
54   // on destruction.
55   void AddObserver(Observer* observer);
56   void RemoveObserver(Observer* observer);
57   const content::MediaStreamDevices& GetAudioCaptureDevices();
58   const content::MediaStreamDevices& GetVideoCaptureDevices();
59
60   // Helper for picking the device that was requested for an OpenDevice request.
61   // If the device requested is not available it will revert to using the first
62   // available one instead or will return an empty list if no devices of the
63   // requested kind are present.
64   void GetRequestedDevice(const std::string& requested_audio_device_id,
65                           const std::string& requested_video_device_id,
66                           bool audio,
67                           bool video,
68                           content::MediaStreamDevices* devices);
69
70   // Overridden from content::MediaObserver:
71   virtual void OnAudioCaptureDevicesChanged() OVERRIDE;
72   virtual void OnVideoCaptureDevicesChanged() OVERRIDE;
73   virtual void OnMediaRequestStateChanged(
74       int render_process_id,
75       int render_frame_id,
76       int page_request_id,
77       const GURL& security_origin,
78       content::MediaStreamType stream_type,
79       content::MediaRequestState state) OVERRIDE;
80   virtual void OnCreatingAudioStream(int render_process_id,
81                                      int render_view_id) OVERRIDE {}
82   virtual void OnAudioStreamPlaying(
83       int render_process_id,
84       int render_frame_id,
85       int stream_id,
86       const ReadPowerAndClipCallback& power_read_callback) OVERRIDE {}
87   virtual void OnAudioStreamStopped(
88       int render_process_id,
89       int render_frame_id,
90       int stream_id) OVERRIDE {}
91
92   // Only for testing.
93   void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices);
94   void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices);
95
96  private:
97   friend struct DefaultSingletonTraits<XWalkMediaCaptureDevicesDispatcher>;
98
99   XWalkMediaCaptureDevicesDispatcher();
100   virtual ~XWalkMediaCaptureDevicesDispatcher();
101
102   // Called by the MediaObserver() functions, executed on UI thread.
103   void NotifyAudioDevicesChangedOnUIThread();
104   void NotifyVideoDevicesChangedOnUIThread();
105   void UpdateMediaReqStateOnUIThread(
106       int render_process_id,
107       int render_frame_id,
108       const GURL& security_origin,
109       content::MediaStreamType stream_type,
110       content::MediaRequestState state);
111
112   // Only for testing, a list of cached audio capture devices.
113   content::MediaStreamDevices test_audio_devices_;
114
115   // Only for testing, a list of video audio capture devices.
116   content::MediaStreamDevices test_video_devices_;
117
118   // A list of observers for the device update notifications.
119   ObserverList<Observer> observers_;
120 };
121
122 #endif  // XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_