1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_media_device_manager.h"
7 #include "base/logging.h"
8 #include "content/renderer/media/media_stream_dispatcher.h"
9 #include "content/renderer/render_frame_impl.h"
10 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
16 ppapi::DeviceRefData
FromStreamDeviceInfo(const StreamDeviceInfo
& info
) {
17 ppapi::DeviceRefData data
;
18 data
.id
= info
.device
.id
;
19 data
.name
= info
.device
.name
;
20 data
.type
= PepperMediaDeviceManager::FromMediaStreamType(info
.device
.type
);
26 PepperMediaDeviceManager
* PepperMediaDeviceManager::GetForRenderFrame(
27 RenderFrame
* render_frame
) {
28 PepperMediaDeviceManager
* handler
=
29 PepperMediaDeviceManager::Get(render_frame
);
31 handler
= new PepperMediaDeviceManager(render_frame
);
35 PepperMediaDeviceManager::PepperMediaDeviceManager(RenderFrame
* render_frame
)
36 : RenderFrameObserver(render_frame
),
37 RenderFrameObserverTracker
<PepperMediaDeviceManager
>(render_frame
),
40 PepperMediaDeviceManager::~PepperMediaDeviceManager() {
41 DCHECK(enumerate_callbacks_
.empty());
42 DCHECK(open_callbacks_
.empty());
45 int PepperMediaDeviceManager::EnumerateDevices(
46 PP_DeviceType_Dev type
,
47 const GURL
& document_url
,
48 const EnumerateDevicesCallback
& callback
) {
49 enumerate_callbacks_
[next_id_
] = callback
;
50 int request_id
= next_id_
++;
52 #if defined(ENABLE_WEBRTC)
53 GetMediaStreamDispatcher()->EnumerateDevices(
56 PepperMediaDeviceManager::FromPepperDeviceType(type
),
57 document_url
.GetOrigin());
59 base::MessageLoop::current()->PostTask(
61 base::Bind(&PepperMediaDeviceManager::OnDevicesEnumerated
,
64 StreamDeviceInfoArray()));
70 void PepperMediaDeviceManager::StopEnumerateDevices(int request_id
) {
71 enumerate_callbacks_
.erase(request_id
);
73 #if defined(ENABLE_WEBRTC)
74 // Need to post task since this function might be called inside the callback
75 // of EnumerateDevices.
76 base::MessageLoop::current()->PostTask(
78 base::Bind(&PepperMediaDeviceManager::StopEnumerateDevicesDelayed
,
84 void PepperMediaDeviceManager::StopEnumerateDevicesDelayed(int request_id
) {
85 #if defined(ENABLE_WEBRTC)
86 // This method is being invoked by the message loop at some unknown
87 // point-in-time after StopEnumerateDevices(). Therefore, check that
88 // render_frame() is not NULL, in order to guarantee
89 // GetMediaStreamDispatcher() won't return NULL.
91 GetMediaStreamDispatcher()->StopEnumerateDevices(request_id
, AsWeakPtr());
95 int PepperMediaDeviceManager::OpenDevice(PP_DeviceType_Dev type
,
96 const std::string
& device_id
,
97 const GURL
& document_url
,
98 const OpenDeviceCallback
& callback
) {
99 open_callbacks_
[next_id_
] = callback
;
100 int request_id
= next_id_
++;
102 #if defined(ENABLE_WEBRTC)
103 GetMediaStreamDispatcher()->OpenDevice(
107 PepperMediaDeviceManager::FromPepperDeviceType(type
),
108 document_url
.GetOrigin());
110 base::MessageLoop::current()->PostTask(
112 base::Bind(&PepperMediaDeviceManager::OnDeviceOpenFailed
,
120 void PepperMediaDeviceManager::CancelOpenDevice(int request_id
) {
121 open_callbacks_
.erase(request_id
);
123 #if defined(ENABLE_WEBRTC)
124 GetMediaStreamDispatcher()->CancelOpenDevice(request_id
, AsWeakPtr());
128 void PepperMediaDeviceManager::CloseDevice(const std::string
& label
) {
129 #if defined(ENABLE_WEBRTC)
130 GetMediaStreamDispatcher()->CloseDevice(label
);
134 int PepperMediaDeviceManager::GetSessionID(PP_DeviceType_Dev type
,
135 const std::string
& label
) {
136 #if defined(ENABLE_WEBRTC)
138 case PP_DEVICETYPE_DEV_AUDIOCAPTURE
:
139 return GetMediaStreamDispatcher()->audio_session_id(label
, 0);
140 case PP_DEVICETYPE_DEV_VIDEOCAPTURE
:
141 return GetMediaStreamDispatcher()->video_session_id(label
, 0);
151 void PepperMediaDeviceManager::OnStreamGenerated(
153 const std::string
& label
,
154 const StreamDeviceInfoArray
& audio_device_array
,
155 const StreamDeviceInfoArray
& video_device_array
) {}
157 void PepperMediaDeviceManager::OnStreamGenerationFailed(
159 content::MediaStreamRequestResult result
) {}
161 void PepperMediaDeviceManager::OnDeviceStopped(
162 const std::string
& label
,
163 const StreamDeviceInfo
& device_info
) {}
165 void PepperMediaDeviceManager::OnDevicesEnumerated(
167 const StreamDeviceInfoArray
& device_array
) {
168 EnumerateCallbackMap::iterator iter
= enumerate_callbacks_
.find(request_id
);
169 if (iter
== enumerate_callbacks_
.end()) {
170 // This might be enumerated result sent before StopEnumerateDevices is
171 // called since EnumerateDevices is persistent request.
175 EnumerateDevicesCallback callback
= iter
->second
;
177 std::vector
<ppapi::DeviceRefData
> devices
;
178 devices
.reserve(device_array
.size());
179 for (StreamDeviceInfoArray::const_iterator info
= device_array
.begin();
180 info
!= device_array
.end();
182 devices
.push_back(FromStreamDeviceInfo(*info
));
184 callback
.Run(request_id
, devices
);
187 void PepperMediaDeviceManager::OnDeviceOpened(
189 const std::string
& label
,
190 const StreamDeviceInfo
& device_info
) {
191 NotifyDeviceOpened(request_id
, true, label
);
194 void PepperMediaDeviceManager::OnDeviceOpenFailed(int request_id
) {
195 NotifyDeviceOpened(request_id
, false, std::string());
199 MediaStreamType
PepperMediaDeviceManager::FromPepperDeviceType(
200 PP_DeviceType_Dev type
) {
202 case PP_DEVICETYPE_DEV_INVALID
:
203 return MEDIA_NO_SERVICE
;
204 case PP_DEVICETYPE_DEV_AUDIOCAPTURE
:
205 return MEDIA_DEVICE_AUDIO_CAPTURE
;
206 case PP_DEVICETYPE_DEV_VIDEOCAPTURE
:
207 return MEDIA_DEVICE_VIDEO_CAPTURE
;
210 return MEDIA_NO_SERVICE
;
215 PP_DeviceType_Dev
PepperMediaDeviceManager::FromMediaStreamType(
216 MediaStreamType type
) {
218 case MEDIA_NO_SERVICE
:
219 return PP_DEVICETYPE_DEV_INVALID
;
220 case MEDIA_DEVICE_AUDIO_CAPTURE
:
221 return PP_DEVICETYPE_DEV_AUDIOCAPTURE
;
222 case MEDIA_DEVICE_VIDEO_CAPTURE
:
223 return PP_DEVICETYPE_DEV_VIDEOCAPTURE
;
226 return PP_DEVICETYPE_DEV_INVALID
;
230 void PepperMediaDeviceManager::NotifyDeviceOpened(int request_id
,
232 const std::string
& label
) {
233 OpenCallbackMap::iterator iter
= open_callbacks_
.find(request_id
);
234 if (iter
== open_callbacks_
.end()) {
235 // The callback may have been unregistered.
239 OpenDeviceCallback callback
= iter
->second
;
240 open_callbacks_
.erase(iter
);
242 callback
.Run(request_id
, succeeded
, label
);
245 MediaStreamDispatcher
* PepperMediaDeviceManager::GetMediaStreamDispatcher()
247 DCHECK(render_frame());
248 MediaStreamDispatcher
* const dispatcher
=
249 static_cast<RenderFrameImpl
*>(render_frame())->GetMediaStreamDispatcher();
254 } // namespace content