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.
6 #ifndef XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
7 #define XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
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"
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 {
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) {}
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) {}
35 // Handle an information update related to a media stream request.
36 virtual void OnRequestUpdate(
37 int render_process_id,
39 content::MediaStreamType stream_type,
40 const content::MediaRequestState state) {}
42 virtual ~Observer() {}
45 static XWalkMediaCaptureDevicesDispatcher* GetInstance();
47 static void RunRequestMediaAccessPermission(
48 content::WebContents* web_contents,
49 const content::MediaStreamRequest& request,
50 const content::MediaResponseCallback& callback);
52 // Methods for observers. Called on UI thread.
53 // Observers should add themselves on construction and remove themselves
55 void AddObserver(Observer* observer);
56 void RemoveObserver(Observer* observer);
57 const content::MediaStreamDevices& GetAudioCaptureDevices();
58 const content::MediaStreamDevices& GetVideoCaptureDevices();
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,
68 content::MediaStreamDevices* devices);
70 // Overridden from content::MediaObserver:
71 virtual void OnAudioCaptureDevicesChanged() OVERRIDE;
72 virtual void OnVideoCaptureDevicesChanged() OVERRIDE;
73 virtual void OnMediaRequestStateChanged(
74 int render_process_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,
86 const ReadPowerAndClipCallback& power_read_callback) OVERRIDE {}
87 virtual void OnAudioStreamStopped(
88 int render_process_id,
90 int stream_id) OVERRIDE {}
93 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices);
94 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices);
97 friend struct DefaultSingletonTraits<XWalkMediaCaptureDevicesDispatcher>;
99 XWalkMediaCaptureDevicesDispatcher();
100 virtual ~XWalkMediaCaptureDevicesDispatcher();
102 // Called by the MediaObserver() functions, executed on UI thread.
103 void NotifyAudioDevicesChangedOnUIThread();
104 void NotifyVideoDevicesChangedOnUIThread();
105 void UpdateMediaReqStateOnUIThread(
106 int render_process_id,
108 const GURL& security_origin,
109 content::MediaStreamType stream_type,
110 content::MediaRequestState state);
112 // Only for testing, a list of cached audio capture devices.
113 content::MediaStreamDevices test_audio_devices_;
115 // Only for testing, a list of video audio capture devices.
116 content::MediaStreamDevices test_video_devices_;
118 // A list of observers for the device update notifications.
119 ObserverList<Observer> observers_;
122 #endif // XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_