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 "content/renderer/media/video_capture_impl_manager.h"
11 #include "content/renderer/pepper/gfx_conversion.h"
12 #include "content/renderer/pepper/pepper_camera_device_host.h"
13 #include "content/renderer/pepper/pepper_media_device_manager.h"
14 #include "content/renderer/render_frame_impl.h"
15 #include "content/renderer/render_thread_impl.h"
16 #include "media/base/bind_to_current_loop.h"
21 PepperPlatformCameraDevice::PepperPlatformCameraDevice(
23 const std::string
& device_id
,
24 const GURL
& document_url
,
25 PepperCameraDeviceHost
* handler
)
26 : render_frame_id_(render_frame_id
),
27 device_id_(device_id
),
30 pending_open_device_(false),
31 pending_open_device_id_(-1),
33 // We need to open the device and obtain the label and session ID before
35 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
37 pending_open_device_id_
= device_manager
->OpenDevice(
38 PP_DEVICETYPE_DEV_VIDEOCAPTURE
, device_id
, document_url
,
39 base::Bind(&PepperPlatformCameraDevice::OnDeviceOpened
,
40 weak_factory_
.GetWeakPtr()));
41 pending_open_device_
= true;
45 void PepperPlatformCameraDevice::GetSupportedVideoCaptureFormats() {
46 DCHECK(thread_checker_
.CalledOnValidThread());
47 VideoCaptureImplManager
* manager
=
48 RenderThreadImpl::current()->video_capture_impl_manager();
49 manager
->GetDeviceSupportedFormats(
51 media::BindToCurrentLoop(base::Bind(
52 &PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated
,
53 weak_factory_
.GetWeakPtr())));
56 void PepperPlatformCameraDevice::DetachEventHandler() {
57 DCHECK(thread_checker_
.CalledOnValidThread());
59 if (!release_device_cb_
.is_null()) {
60 base::ResetAndReturn(&release_device_cb_
).Run();
62 if (!label_
.empty()) {
63 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
65 device_manager
->CloseDevice(label_
);
68 if (pending_open_device_
) {
69 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
71 device_manager
->CancelOpenDevice(pending_open_device_id_
);
72 pending_open_device_
= false;
73 pending_open_device_id_
= -1;
77 PepperPlatformCameraDevice::~PepperPlatformCameraDevice() {
78 DCHECK(thread_checker_
.CalledOnValidThread());
79 DCHECK(release_device_cb_
.is_null());
80 DCHECK(label_
.empty());
81 DCHECK(!pending_open_device_
);
84 void PepperPlatformCameraDevice::OnDeviceOpened(int request_id
,
86 const std::string
& label
) {
87 DCHECK(thread_checker_
.CalledOnValidThread());
90 pending_open_device_
= false;
91 pending_open_device_id_
= -1;
93 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
94 succeeded
= succeeded
&& device_manager
;
98 device_manager
->GetSessionID(PP_DEVICETYPE_DEV_VIDEOCAPTURE
, label
);
99 VideoCaptureImplManager
* manager
=
100 RenderThreadImpl::current()->video_capture_impl_manager();
101 release_device_cb_
= manager
->UseDevice(session_id_
);
104 handler_
->OnInitialized(succeeded
);
107 void PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated(
108 const media::VideoCaptureFormats
& formats
) {
109 DCHECK(thread_checker_
.CalledOnValidThread());
112 std::vector
<PP_VideoCaptureFormat
> output_formats
;
113 for (const auto& format
: formats
) {
114 PP_VideoCaptureFormat output_format
;
115 output_format
.frame_size
= PP_FromGfxSize(format
.frame_size
);
116 output_format
.frame_rate
= format
.frame_rate
;
117 output_formats
.push_back(output_format
);
119 handler_
->OnVideoCaptureFormatsEnumerated(output_formats
);
122 PepperMediaDeviceManager
* PepperPlatformCameraDevice::GetMediaDeviceManager() {
123 RenderFrameImpl
* const render_frame
=
124 RenderFrameImpl::FromRoutingID(render_frame_id_
);
126 ? PepperMediaDeviceManager::GetForRenderFrame(render_frame
).get()
130 } // namespace content