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 void OnAudioCaptureDevicesChanged() override;
72 void OnVideoCaptureDevicesChanged() override;
73 void OnMediaRequestStateChanged(
74 int render_process_id,
77 const GURL& security_origin,
78 content::MediaStreamType stream_type,
79 content::MediaRequestState state) override;
80 void OnCreatingAudioStream(int render_process_id,
81 int render_view_id) override {}
84 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices);
85 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices);
88 friend struct DefaultSingletonTraits<XWalkMediaCaptureDevicesDispatcher>;
90 XWalkMediaCaptureDevicesDispatcher();
91 virtual ~XWalkMediaCaptureDevicesDispatcher();
93 // Called by the MediaObserver() functions, executed on UI thread.
94 void NotifyAudioDevicesChangedOnUIThread();
95 void NotifyVideoDevicesChangedOnUIThread();
96 void UpdateMediaReqStateOnUIThread(
97 int render_process_id,
99 const GURL& security_origin,
100 content::MediaStreamType stream_type,
101 content::MediaRequestState state);
103 // Only for testing, a list of cached audio capture devices.
104 content::MediaStreamDevices test_audio_devices_;
106 // Only for testing, a list of video audio capture devices.
107 content::MediaStreamDevices test_video_devices_;
109 // A list of observers for the device update notifications.
110 ObserverList<Observer> observers_;
113 #endif // XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_