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 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
8 #include "content/public/browser/media_capture_devices.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/common/media_stream_request.h"
12 using content::BrowserThread;
13 using content::MediaCaptureDevices;
14 using content::MediaStreamDevices;
18 const content::MediaStreamDevice* FindDefaultDeviceWithId(
19 const content::MediaStreamDevices& devices,
20 const std::string& device_id) {
21 if (devices.empty()) {
24 content::MediaStreamDevices::const_iterator iter = devices.begin();
25 for (; iter != devices.end(); ++iter) {
26 if (iter->id == device_id) {
31 return &(*devices.begin());
37 XWalkMediaCaptureDevicesDispatcher*
38 XWalkMediaCaptureDevicesDispatcher::GetInstance() {
39 return Singleton<XWalkMediaCaptureDevicesDispatcher>::get();
42 void XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
43 content::WebContents* web_contents,
44 const content::MediaStreamRequest& request,
45 const content::MediaResponseCallback& callback) {
46 content::MediaStreamDevices devices;
47 // Based on chrome/browser/media/media_stream_devices_controller.cc.
48 bool microphone_requested =
49 (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE);
50 bool webcam_requested =
51 (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE);
52 if (microphone_requested || webcam_requested) {
53 switch (request.request_type) {
54 case content::MEDIA_OPEN_DEVICE:
55 case content::MEDIA_DEVICE_ACCESS:
56 case content::MEDIA_GENERATE_STREAM:
57 case content::MEDIA_ENUMERATE_DEVICES:
58 // Get the exact audio and video devices if id is specified.
59 // Or get the default devices when requested device id is empty.
60 XWalkMediaCaptureDevicesDispatcher::GetInstance()->GetRequestedDevice(
61 request.requested_audio_device_id,
62 request.requested_video_device_id,
70 content::MEDIA_DEVICE_OK,
71 scoped_ptr<content::MediaStreamUI>());
74 XWalkMediaCaptureDevicesDispatcher::XWalkMediaCaptureDevicesDispatcher() {}
76 XWalkMediaCaptureDevicesDispatcher::~XWalkMediaCaptureDevicesDispatcher() {}
78 void XWalkMediaCaptureDevicesDispatcher::AddObserver(Observer* observer) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 if (!observers_.HasObserver(observer))
81 observers_.AddObserver(observer);
84 void XWalkMediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 observers_.RemoveObserver(observer);
89 const MediaStreamDevices&
90 XWalkMediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 if (!test_audio_devices_.empty())
93 return test_audio_devices_;
95 return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
98 const MediaStreamDevices&
99 XWalkMediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 if (!test_video_devices_.empty())
102 return test_video_devices_;
104 return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
107 void XWalkMediaCaptureDevicesDispatcher::GetRequestedDevice(
108 const std::string& requested_audio_device_id,
109 const std::string& requested_video_device_id,
112 content::MediaStreamDevices* devices) {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
114 DCHECK(audio || video);
117 const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
118 const content::MediaStreamDevice* const device =
119 FindDefaultDeviceWithId(audio_devices, requested_audio_device_id);
121 devices->push_back(*device);
124 const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
125 const content::MediaStreamDevice* const device =
126 FindDefaultDeviceWithId(video_devices, requested_video_device_id);
128 devices->push_back(*device);
132 void XWalkMediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
134 BrowserThread::PostTask(
135 BrowserThread::UI, FROM_HERE,
137 &XWalkMediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread, // NOLINT
138 base::Unretained(this)));
141 void XWalkMediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
143 BrowserThread::PostTask(
144 BrowserThread::UI, FROM_HERE,
146 &XWalkMediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread, // NOLINT
147 base::Unretained(this)));
150 void XWalkMediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
151 int render_process_id,
154 const GURL& security_origin,
155 const content::MediaStreamDevice& device,
156 content::MediaRequestState state) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
158 BrowserThread::PostTask(
159 BrowserThread::UI, FROM_HERE,
161 &XWalkMediaCaptureDevicesDispatcher::UpdateMediaReqStateOnUIThread,
162 base::Unretained(this), render_process_id, render_view_id,
163 security_origin, device, state));
166 void XWalkMediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() {
167 MediaStreamDevices devices = GetAudioCaptureDevices();
168 FOR_EACH_OBSERVER(Observer, observers_,
169 OnUpdateAudioDevices(devices));
172 void XWalkMediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() {
173 MediaStreamDevices devices = GetVideoCaptureDevices();
174 FOR_EACH_OBSERVER(Observer, observers_,
175 OnUpdateVideoDevices(devices));
178 void XWalkMediaCaptureDevicesDispatcher::UpdateMediaReqStateOnUIThread(
179 int render_process_id,
181 const GURL& security_origin,
182 const content::MediaStreamDevice& device,
183 content::MediaRequestState state) {
184 FOR_EACH_OBSERVER(Observer, observers_,
185 OnRequestUpdate(render_process_id,
192 void XWalkMediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices(
193 const MediaStreamDevices& devices) {
194 test_audio_devices_ = devices;
197 void XWalkMediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices(
198 const MediaStreamDevices& devices) {
199 test_video_devices_ = devices;