Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_platform_camera_device.cc
blobc3dfff222a6c634d9f526b2006dc8825f1d0eaaa
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"
7 #include "base/bind.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"
18 #include "url/gurl.h"
20 namespace content {
22 PepperPlatformCameraDevice::PepperPlatformCameraDevice(
23 int render_frame_id,
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),
29 session_id_(0),
30 handler_(handler),
31 pending_open_device_(false),
32 pending_open_device_id_(-1),
33 weak_factory_(this) {
34 // We need to open the device and obtain the label and session ID before
35 // initializing.
36 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
37 if (device_manager) {
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(
51 session_id_,
52 media::BindToCurrentLoop(base::Bind(
53 &PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated,
54 weak_factory_.GetWeakPtr())));
57 void PepperPlatformCameraDevice::DetachEventHandler() {
58 DCHECK(thread_checker_.CalledOnValidThread());
59 handler_ = NULL;
60 if (!release_device_cb_.is_null()) {
61 base::ResetAndReturn(&release_device_cb_).Run();
63 if (!label_.empty()) {
64 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
65 if (device_manager)
66 device_manager->CloseDevice(label_);
67 label_.clear();
69 if (pending_open_device_) {
70 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
71 if (device_manager)
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,
86 bool succeeded,
87 const std::string& label) {
88 DCHECK(thread_checker_.CalledOnValidThread());
89 DCHECK(handler_);
91 pending_open_device_ = false;
92 pending_open_device_id_ = -1;
94 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
95 succeeded = succeeded && device_manager;
96 if (succeeded) {
97 label_ = label;
98 session_id_ =
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());
111 DCHECK(handler_);
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_);
126 return render_frame
127 ? PepperMediaDeviceManager::GetForRenderFrame(render_frame).get()
128 : NULL;
131 } // namespace content