Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / media / media_capture_devices_dispatcher.cc
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 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
7
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"
11
12 using content::BrowserThread;
13 using content::MediaCaptureDevices;
14 using content::MediaStreamDevices;
15
16 namespace {
17
18 const content::MediaStreamDevice* FindDefaultDeviceWithId(
19     const content::MediaStreamDevices& devices,
20     const std::string& device_id) {
21   if (devices.empty()) {
22     return NULL;
23   }
24   content::MediaStreamDevices::const_iterator iter = devices.begin();
25   for (; iter != devices.end(); ++iter) {
26     if (iter->id == device_id) {
27       return &(*iter);
28     }
29   }
30
31   return &(*devices.begin());
32 };
33
34 }  // namespace
35
36
37 XWalkMediaCaptureDevicesDispatcher*
38     XWalkMediaCaptureDevicesDispatcher::GetInstance() {
39   return Singleton<XWalkMediaCaptureDevicesDispatcher>::get();
40 }
41
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,
63             microphone_requested,
64             webcam_requested,
65             &devices);
66         break;
67     }
68   }
69   callback.Run(devices,
70                content::MEDIA_DEVICE_OK,
71                scoped_ptr<content::MediaStreamUI>());
72 }
73
74 XWalkMediaCaptureDevicesDispatcher::XWalkMediaCaptureDevicesDispatcher() {}
75
76 XWalkMediaCaptureDevicesDispatcher::~XWalkMediaCaptureDevicesDispatcher() {}
77
78 void XWalkMediaCaptureDevicesDispatcher::AddObserver(Observer* observer) {
79   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80   if (!observers_.HasObserver(observer))
81     observers_.AddObserver(observer);
82 }
83
84 void XWalkMediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) {
85   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86   observers_.RemoveObserver(observer);
87 }
88
89 const MediaStreamDevices&
90 XWalkMediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
91   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92   if (!test_audio_devices_.empty())
93     return test_audio_devices_;
94
95   return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
96 }
97
98 const MediaStreamDevices&
99 XWalkMediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
100   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101   if (!test_video_devices_.empty())
102     return test_video_devices_;
103
104   return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
105 }
106
107 void XWalkMediaCaptureDevicesDispatcher::GetRequestedDevice(
108     const std::string& requested_audio_device_id,
109     const std::string& requested_video_device_id,
110     bool audio,
111     bool video,
112     content::MediaStreamDevices* devices) {
113   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
114   DCHECK(audio || video);
115
116   if (audio) {
117     const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
118     const content::MediaStreamDevice* const device =
119         FindDefaultDeviceWithId(audio_devices, requested_audio_device_id);
120     if (device)
121       devices->push_back(*device);
122   }
123   if (video) {
124     const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
125     const content::MediaStreamDevice* const device =
126         FindDefaultDeviceWithId(video_devices, requested_video_device_id);
127     if (device)
128       devices->push_back(*device);
129   }
130 }
131
132 void XWalkMediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
133   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
134   BrowserThread::PostTask(
135       BrowserThread::UI, FROM_HERE,
136       base::Bind(
137           &XWalkMediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread, // NOLINT
138           base::Unretained(this)));
139 }
140
141 void XWalkMediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
142   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
143   BrowserThread::PostTask(
144       BrowserThread::UI, FROM_HERE,
145       base::Bind(
146           &XWalkMediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread, // NOLINT
147           base::Unretained(this)));
148 }
149
150 void XWalkMediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
151     int render_process_id,
152     int render_view_id,
153     int page_request_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,
160       base::Bind(
161           &XWalkMediaCaptureDevicesDispatcher::UpdateMediaReqStateOnUIThread,
162           base::Unretained(this), render_process_id, render_view_id,
163           security_origin, device, state));
164 }
165
166 void XWalkMediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() {
167   MediaStreamDevices devices = GetAudioCaptureDevices();
168   FOR_EACH_OBSERVER(Observer, observers_,
169                     OnUpdateAudioDevices(devices));
170 }
171
172 void XWalkMediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() {
173   MediaStreamDevices devices = GetVideoCaptureDevices();
174   FOR_EACH_OBSERVER(Observer, observers_,
175                     OnUpdateVideoDevices(devices));
176 }
177
178 void XWalkMediaCaptureDevicesDispatcher::UpdateMediaReqStateOnUIThread(
179     int render_process_id,
180     int render_view_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,
186                                     render_view_id,
187                                     device,
188                                     state));
189 }
190
191
192 void XWalkMediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices(
193     const MediaStreamDevices& devices) {
194   test_audio_devices_ = devices;
195 }
196
197 void XWalkMediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices(
198     const MediaStreamDevices& devices) {
199   test_video_devices_ = devices;
200 }