1 // Copyright 2015 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_platform_camera_device.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "content/renderer/media/video_capture_impl_manager.h"
12 #include "content/renderer/pepper/gfx_conversion.h"
13 #include "content/renderer/pepper/pepper_camera_device_host.h"
14 #include "content/renderer/pepper/pepper_media_device_manager.h"
15 #include "content/renderer/render_frame_impl.h"
16 #include "content/renderer/render_thread_impl.h"
17 #include "media/base/bind_to_current_loop.h"
22 PepperPlatformCameraDevice::PepperPlatformCameraDevice(
24 const std::string
& device_id
,
25 const GURL
& document_url
,
26 PepperCameraDeviceHost
* handler
)
27 : render_frame_id_(render_frame_id
),
28 device_id_(device_id
),
31 pending_open_device_(false),
32 pending_open_device_id_(-1),
34 // We need to open the device and obtain the label and session ID before
36 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
38 pending_open_device_id_
= device_manager
->OpenDevice(
39 PP_DEVICETYPE_DEV_VIDEOCAPTURE
, device_id
, document_url
,
40 base::Bind(&PepperPlatformCameraDevice::OnDeviceOpened
,
41 weak_factory_
.GetWeakPtr()));
42 pending_open_device_
= true;
46 void PepperPlatformCameraDevice::GetSupportedVideoCaptureFormats() {
47 DCHECK(thread_checker_
.CalledOnValidThread());
48 VideoCaptureImplManager
* manager
=
49 RenderThreadImpl::current()->video_capture_impl_manager();
50 manager
->GetDeviceSupportedFormats(
52 media::BindToCurrentLoop(base::Bind(
53 &PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated
,
54 weak_factory_
.GetWeakPtr())));
57 void PepperPlatformCameraDevice::DetachEventHandler() {
58 DCHECK(thread_checker_
.CalledOnValidThread());
60 if (!release_device_cb_
.is_null()) {
61 base::ResetAndReturn(&release_device_cb_
).Run();
63 if (!label_
.empty()) {
64 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
66 device_manager
->CloseDevice(label_
);
69 if (pending_open_device_
) {
70 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
72 device_manager
->CancelOpenDevice(pending_open_device_id_
);
73 pending_open_device_
= false;
74 pending_open_device_id_
= -1;
78 PepperPlatformCameraDevice::~PepperPlatformCameraDevice() {
79 DCHECK(thread_checker_
.CalledOnValidThread());
80 DCHECK(release_device_cb_
.is_null());
81 DCHECK(label_
.empty());
82 DCHECK(!pending_open_device_
);
85 void PepperPlatformCameraDevice::OnDeviceOpened(int request_id
,
87 const std::string
& label
) {
88 DCHECK(thread_checker_
.CalledOnValidThread());
91 pending_open_device_
= false;
92 pending_open_device_id_
= -1;
94 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
95 succeeded
= succeeded
&& device_manager
;
99 device_manager
->GetSessionID(PP_DEVICETYPE_DEV_VIDEOCAPTURE
, label
);
100 VideoCaptureImplManager
* manager
=
101 RenderThreadImpl::current()->video_capture_impl_manager();
102 release_device_cb_
= manager
->UseDevice(session_id_
);
105 handler_
->OnInitialized(succeeded
);
108 void PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated(
109 const media::VideoCaptureFormats
& formats
) {
110 DCHECK(thread_checker_
.CalledOnValidThread());
113 std::vector
<PP_VideoCaptureFormat
> output_formats
;
114 for (const auto& format
: formats
) {
115 PP_VideoCaptureFormat output_format
;
116 output_format
.frame_size
= PP_FromGfxSize(format
.frame_size
);
117 output_format
.frame_rate
= format
.frame_rate
;
118 output_formats
.push_back(output_format
);
120 handler_
->OnVideoCaptureFormatsEnumerated(output_formats
);
123 PepperMediaDeviceManager
* PepperPlatformCameraDevice::GetMediaDeviceManager() {
124 RenderFrameImpl
* const render_frame
=
125 RenderFrameImpl::FromRoutingID(render_frame_id_
);
127 ? PepperMediaDeviceManager::GetForRenderFrame(render_frame
).get()
131 } // namespace content