summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_media_device_manager.cc
blob: f460118e8f0e9a94314d828ca6262b622864b029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/pepper/pepper_media_device_manager.h"

#include "base/feature_list.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/child/child_thread_impl.h"
#include "content/public/common/console_message_level.h"
#include "content/public/common/content_features.h"
#include "content/public/common/service_names.mojom.h"
#include "content/renderer/media/media_devices_event_dispatcher.h"
#include "content/renderer/pepper/renderer_ppapi_host_impl.h"
#include "content/renderer/render_frame_impl.h"
#include "media/media_features.h"
#include "ppapi/shared_impl/ppb_device_ref_shared.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/cpp/interface_provider.h"

#if BUILDFLAG(ENABLE_WEBRTC)
#include "content/renderer/media/media_stream_device_observer.h"
#endif

namespace content {

namespace {

const char kPepperInsecureOriginMessage[] =
    "Microphone and Camera access no longer works on insecure origins. To use "
    "this feature, you should consider switching your application to a "
    "secure origin, such as HTTPS. See https://goo.gl/rStTGz for more "
    "details.";

PP_DeviceType_Dev FromMediaDeviceType(MediaDeviceType type) {
  switch (type) {
    case MEDIA_DEVICE_TYPE_AUDIO_INPUT:
      return PP_DEVICETYPE_DEV_AUDIOCAPTURE;
    case MEDIA_DEVICE_TYPE_VIDEO_INPUT:
      return PP_DEVICETYPE_DEV_VIDEOCAPTURE;
    case MEDIA_DEVICE_TYPE_AUDIO_OUTPUT:
      return PP_DEVICETYPE_DEV_AUDIOOUTPUT;
    default:
      NOTREACHED();
      return PP_DEVICETYPE_DEV_INVALID;
  }
}

MediaDeviceType ToMediaDeviceType(PP_DeviceType_Dev type) {
  switch (type) {
    case PP_DEVICETYPE_DEV_AUDIOCAPTURE:
      return MEDIA_DEVICE_TYPE_AUDIO_INPUT;
    case PP_DEVICETYPE_DEV_VIDEOCAPTURE:
      return MEDIA_DEVICE_TYPE_VIDEO_INPUT;
    case PP_DEVICETYPE_DEV_AUDIOOUTPUT:
      return MEDIA_DEVICE_TYPE_AUDIO_OUTPUT;
    default:
      NOTREACHED();
      return MEDIA_DEVICE_TYPE_AUDIO_OUTPUT;
  }
}

ppapi::DeviceRefData FromMediaDeviceInfo(MediaDeviceType type,
                                         const MediaDeviceInfo& info) {
  ppapi::DeviceRefData data;
  data.id = info.device_id;
  // Some Flash content can't handle an empty string, so stick a space in to
  // make them happy. See crbug.com/408404.
  data.name = info.label.empty() ? std::string(" ") : info.label;
  data.type = FromMediaDeviceType(type);
  return data;
}

}  // namespace

base::WeakPtr<PepperMediaDeviceManager>
PepperMediaDeviceManager::GetForRenderFrame(
    RenderFrame* render_frame) {
  PepperMediaDeviceManager* handler =
      PepperMediaDeviceManager::Get(render_frame);
  if (!handler)
    handler = new PepperMediaDeviceManager(render_frame);
  return handler->AsWeakPtr();
}

PepperMediaDeviceManager::PepperMediaDeviceManager(RenderFrame* render_frame)
    : RenderFrameObserver(render_frame),
      RenderFrameObserverTracker<PepperMediaDeviceManager>(render_frame),
      next_id_(1) {}

PepperMediaDeviceManager::~PepperMediaDeviceManager() {
  DCHECK(open_callbacks_.empty());
}

void PepperMediaDeviceManager::EnumerateDevices(
    PP_DeviceType_Dev type,
    const DevicesCallback& callback) {
#if BUILDFLAG(ENABLE_WEBRTC)
  bool request_audio_input = type == PP_DEVICETYPE_DEV_AUDIOCAPTURE;
  bool request_video_input = type == PP_DEVICETYPE_DEV_VIDEOCAPTURE;
  bool request_audio_output = type == PP_DEVICETYPE_DEV_AUDIOOUTPUT;
  CHECK(request_audio_input || request_video_input || request_audio_output);
  GetMediaDevicesDispatcher()->EnumerateDevices(
      request_audio_input, request_video_input, request_audio_output,
      base::BindOnce(&PepperMediaDeviceManager::DevicesEnumerated, AsWeakPtr(),
                     callback, ToMediaDeviceType(type)));
#else
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(&PepperMediaDeviceManager::DevicesEnumerated,
                            AsWeakPtr(), callback, ToMediaDeviceType(type),
                            std::vector<MediaDeviceInfoArray>()));
#endif
}

uint32_t PepperMediaDeviceManager::StartMonitoringDevices(
    PP_DeviceType_Dev type,
    const DevicesCallback& callback) {
#if BUILDFLAG(ENABLE_WEBRTC)
  base::WeakPtr<MediaDevicesEventDispatcher> event_dispatcher =
      MediaDevicesEventDispatcher::GetForRenderFrame(render_frame());
  return event_dispatcher->SubscribeDeviceChangeNotifications(
      ToMediaDeviceType(type),
      base::Bind(&PepperMediaDeviceManager::DevicesChanged, AsWeakPtr(),
                 callback));
#else
  return 0;
#endif
}

void PepperMediaDeviceManager::StopMonitoringDevices(PP_DeviceType_Dev type,
                                                     uint32_t subscription_id) {
#if BUILDFLAG(ENABLE_WEBRTC)
  base::WeakPtr<MediaDevicesEventDispatcher> event_dispatcher =
      MediaDevicesEventDispatcher::GetForRenderFrame(render_frame());
  event_dispatcher->UnsubscribeDeviceChangeNotifications(
      ToMediaDeviceType(type), subscription_id);
#endif
}

int PepperMediaDeviceManager::OpenDevice(PP_DeviceType_Dev type,
                                         const std::string& device_id,
                                         PP_Instance pp_instance,
                                         const OpenDeviceCallback& callback) {
  open_callbacks_[next_id_] = callback;
  int request_id = next_id_++;

  RendererPpapiHostImpl* host =
      RendererPpapiHostImpl::GetForPPInstance(pp_instance);
  if (!host->IsSecureContext(pp_instance)) {
    RenderFrame* render_frame = host->GetRenderFrameForInstance(pp_instance);
    if (render_frame) {
      render_frame->AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_WARNING,
                                        kPepperInsecureOriginMessage);
    }
    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE,
        base::BindOnce(&PepperMediaDeviceManager::OnDeviceOpened, AsWeakPtr(),
                       request_id, false, std::string(), MediaStreamDevice()));
    return request_id;
  }

#if BUILDFLAG(ENABLE_WEBRTC)
  GetMediaStreamDispatcherHost()->OpenDevice(
      routing_id(), request_id, device_id,
      PepperMediaDeviceManager::FromPepperDeviceType(type),
      base::BindOnce(&PepperMediaDeviceManager::OnDeviceOpened, AsWeakPtr(),
                     request_id));
#else
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE,
      base::Bind(&PepperMediaDeviceManager::OnDeviceOpened, AsWeakPtr(),
                 request_id, false, std::string(), MediaStreamDevice()));
#endif

  return request_id;
}

void PepperMediaDeviceManager::CancelOpenDevice(int request_id) {
  open_callbacks_.erase(request_id);

#if BUILDFLAG(ENABLE_WEBRTC)
  GetMediaStreamDispatcherHost()->CancelRequest(routing_id(), request_id);
#endif
}

void PepperMediaDeviceManager::CloseDevice(const std::string& label) {
#if BUILDFLAG(ENABLE_WEBRTC)
  if (!GetMediaStreamDeviceObserver()->RemoveStream(label))
    return;

  GetMediaStreamDispatcherHost()->CloseDevice(label);
#endif
}

int PepperMediaDeviceManager::GetSessionID(PP_DeviceType_Dev type,
                                           const std::string& label) {
#if BUILDFLAG(ENABLE_WEBRTC)
  switch (type) {
    case PP_DEVICETYPE_DEV_AUDIOCAPTURE:
      return GetMediaStreamDeviceObserver()->audio_session_id(label);
    case PP_DEVICETYPE_DEV_VIDEOCAPTURE:
      return GetMediaStreamDeviceObserver()->video_session_id(label);
    default:
      NOTREACHED();
      return 0;
  }
#else
  return 0;
#endif
}

// static
MediaStreamType PepperMediaDeviceManager::FromPepperDeviceType(
    PP_DeviceType_Dev type) {
  switch (type) {
    case PP_DEVICETYPE_DEV_INVALID:
      return MEDIA_NO_SERVICE;
    case PP_DEVICETYPE_DEV_AUDIOCAPTURE:
      return MEDIA_DEVICE_AUDIO_CAPTURE;
    case PP_DEVICETYPE_DEV_VIDEOCAPTURE:
      return MEDIA_DEVICE_VIDEO_CAPTURE;
    default:
      NOTREACHED();
      return MEDIA_NO_SERVICE;
  }
}

void PepperMediaDeviceManager::OnDeviceOpened(int request_id,
                                              bool success,
                                              const std::string& label,
                                              const MediaStreamDevice& device) {
  OpenCallbackMap::iterator iter = open_callbacks_.find(request_id);
  if (iter == open_callbacks_.end()) {
    // The callback may have been unregistered.
    return;
  }

#if BUILDFLAG(ENABLE_WEBRTC)
  if (success)
    GetMediaStreamDeviceObserver()->AddStream(label, device);
#endif

  OpenDeviceCallback callback = iter->second;
  open_callbacks_.erase(iter);

  callback.Run(request_id, success, success ? label : std::string());
}

void PepperMediaDeviceManager::DevicesEnumerated(
    const DevicesCallback& client_callback,
    MediaDeviceType type,
    const std::vector<MediaDeviceInfoArray>& enumeration) {
  DevicesChanged(client_callback, type, enumeration[type]);
}

void PepperMediaDeviceManager::DevicesChanged(
    const DevicesCallback& client_callback,
    MediaDeviceType type,
    const MediaDeviceInfoArray& device_infos) {
  std::vector<ppapi::DeviceRefData> devices;
  devices.reserve(device_infos.size());
  for (const auto& device_info : device_infos)
    devices.push_back(FromMediaDeviceInfo(type, device_info));

  client_callback.Run(devices);
}

const mojom::MediaStreamDispatcherHostPtr&
PepperMediaDeviceManager::GetMediaStreamDispatcherHost() {
  if (!dispatcher_host_) {
    ChildThreadImpl::current()->GetConnector()->BindInterface(
        mojom::kBrowserServiceName, &dispatcher_host_);
  }
  return dispatcher_host_;
}

MediaStreamDeviceObserver*
PepperMediaDeviceManager::GetMediaStreamDeviceObserver() const {
  DCHECK(render_frame());
  MediaStreamDeviceObserver* const observer =
      static_cast<RenderFrameImpl*>(render_frame())
          ->GetMediaStreamDeviceObserver();
  DCHECK(observer);
  return observer;
}

const blink::mojom::MediaDevicesDispatcherHostPtr&
PepperMediaDeviceManager::GetMediaDevicesDispatcher() {
  if (!media_devices_dispatcher_) {
    CHECK(render_frame());
    CHECK(render_frame()->GetRemoteInterfaces());
    render_frame()->GetRemoteInterfaces()->GetInterface(
        mojo::MakeRequest(&media_devices_dispatcher_));
  }

  return media_devices_dispatcher_;
}

void PepperMediaDeviceManager::OnDestruct() {
  delete this;
}

}  // namespace content