Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / pepper_platform_camera_device.cc
blob2be016f951acdc102d8cc6625556fe94d3a2af22
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 "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"
17 #include "url/gurl.h"
19 namespace content {
21 PepperPlatformCameraDevice::PepperPlatformCameraDevice(
22 int render_frame_id,
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),
28 session_id_(0),
29 handler_(handler),
30 pending_open_device_(false),
31 pending_open_device_id_(-1),
32 weak_factory_(this) {
33 // We need to open the device and obtain the label and session ID before
34 // initializing.
35 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
36 if (device_manager) {
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(
50 session_id_,
51 media::BindToCurrentLoop(base::Bind(
52 &PepperPlatformCameraDevice::OnDeviceSupportedFormatsEnumerated,
53 weak_factory_.GetWeakPtr())));
56 void PepperPlatformCameraDevice::DetachEventHandler() {
57 DCHECK(thread_checker_.CalledOnValidThread());
58 handler_ = NULL;
59 if (!release_device_cb_.is_null()) {
60 base::ResetAndReturn(&release_device_cb_).Run();
62 if (!label_.empty()) {
63 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
64 if (device_manager)
65 device_manager->CloseDevice(label_);
66 label_.clear();
68 if (pending_open_device_) {
69 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
70 if (device_manager)
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,
85 bool succeeded,
86 const std::string& label) {
87 DCHECK(thread_checker_.CalledOnValidThread());
88 DCHECK(handler_);
90 pending_open_device_ = false;
91 pending_open_device_id_ = -1;
93 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
94 succeeded = succeeded && device_manager;
95 if (succeeded) {
96 label_ = label;
97 session_id_ =
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());
110 DCHECK(handler_);
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_);
125 return render_frame
126 ? PepperMediaDeviceManager::GetForRenderFrame(render_frame).get()
127 : NULL;
130 } // namespace content