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_device_enumeration_event_handler.h"
7 #include "base/logging.h"
8 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
14 ppapi::DeviceRefData
FromStreamDeviceInfo(const StreamDeviceInfo
& info
) {
15 ppapi::DeviceRefData data
;
16 data
.id
= info
.device
.id
;
17 data
.name
= info
.device
.name
;
18 data
.type
= PepperDeviceEnumerationEventHandler::FromMediaStreamType(
25 PepperDeviceEnumerationEventHandler::PepperDeviceEnumerationEventHandler()
29 PepperDeviceEnumerationEventHandler::~PepperDeviceEnumerationEventHandler() {
30 DCHECK(enumerate_callbacks_
.empty());
31 DCHECK(open_callbacks_
.empty());
34 int PepperDeviceEnumerationEventHandler::RegisterEnumerateDevicesCallback(
35 const webkit::ppapi::PluginDelegate::EnumerateDevicesCallback
& callback
) {
36 enumerate_callbacks_
[next_id_
] = callback
;
40 void PepperDeviceEnumerationEventHandler::UnregisterEnumerateDevicesCallback(
42 enumerate_callbacks_
.erase(request_id
);
45 int PepperDeviceEnumerationEventHandler::RegisterOpenDeviceCallback(
46 const PepperPluginDelegateImpl::OpenDeviceCallback
& callback
) {
47 open_callbacks_
[next_id_
] = callback
;
51 void PepperDeviceEnumerationEventHandler::OnStreamGenerated(
53 const std::string
& label
,
54 const StreamDeviceInfoArray
& audio_device_array
,
55 const StreamDeviceInfoArray
& video_device_array
) {
58 void PepperDeviceEnumerationEventHandler::OnStreamGenerationFailed(
62 void PepperDeviceEnumerationEventHandler::OnDevicesEnumerated(
64 const StreamDeviceInfoArray
& device_array
) {
65 NotifyDevicesEnumerated(request_id
, true, device_array
);
68 void PepperDeviceEnumerationEventHandler::OnDevicesEnumerationFailed(
70 NotifyDevicesEnumerated(request_id
, false, StreamDeviceInfoArray());
73 void PepperDeviceEnumerationEventHandler::OnDeviceOpened(
75 const std::string
& label
,
76 const StreamDeviceInfo
& device_info
) {
77 NotifyDeviceOpened(request_id
, true, label
);
80 void PepperDeviceEnumerationEventHandler::OnDeviceOpenFailed(int request_id
) {
81 NotifyDeviceOpened(request_id
, false, std::string());
85 MediaStreamType
PepperDeviceEnumerationEventHandler::FromPepperDeviceType(
86 PP_DeviceType_Dev type
) {
88 case PP_DEVICETYPE_DEV_INVALID
:
89 return MEDIA_NO_SERVICE
;
90 case PP_DEVICETYPE_DEV_AUDIOCAPTURE
:
91 return MEDIA_DEVICE_AUDIO_CAPTURE
;
92 case PP_DEVICETYPE_DEV_VIDEOCAPTURE
:
93 return MEDIA_DEVICE_VIDEO_CAPTURE
;
96 return MEDIA_NO_SERVICE
;
101 PP_DeviceType_Dev
PepperDeviceEnumerationEventHandler::FromMediaStreamType(
102 MediaStreamType type
) {
104 case MEDIA_NO_SERVICE
:
105 return PP_DEVICETYPE_DEV_INVALID
;
106 case MEDIA_DEVICE_AUDIO_CAPTURE
:
107 return PP_DEVICETYPE_DEV_AUDIOCAPTURE
;
108 case MEDIA_DEVICE_VIDEO_CAPTURE
:
109 return PP_DEVICETYPE_DEV_VIDEOCAPTURE
;
112 return PP_DEVICETYPE_DEV_INVALID
;
116 void PepperDeviceEnumerationEventHandler::NotifyDevicesEnumerated(
119 const StreamDeviceInfoArray
& device_array
) {
120 EnumerateCallbackMap::iterator iter
= enumerate_callbacks_
.find(request_id
);
121 if (iter
== enumerate_callbacks_
.end()) {
122 // This might be enumerated result sent before StopEnumerateDevices is
123 // called since EnumerateDevices is persistent request.
127 webkit::ppapi::PluginDelegate::EnumerateDevicesCallback callback
=
130 std::vector
<ppapi::DeviceRefData
> devices
;
132 devices
.reserve(device_array
.size());
133 for (StreamDeviceInfoArray::const_iterator info
=
134 device_array
.begin(); info
!= device_array
.end(); ++info
) {
135 devices
.push_back(FromStreamDeviceInfo(*info
));
138 callback
.Run(request_id
, succeeded
, devices
);
141 void PepperDeviceEnumerationEventHandler::NotifyDeviceOpened(
144 const std::string
& label
) {
145 OpenCallbackMap::iterator iter
= open_callbacks_
.find(request_id
);
146 if (iter
== open_callbacks_
.end()) {
151 PepperPluginDelegateImpl::OpenDeviceCallback callback
= iter
->second
;
152 open_callbacks_
.erase(iter
);
154 callback
.Run(request_id
, succeeded
, label
);
157 } // namespace content