1 // Copyright (c) 2012 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_video_capture_impl.h"
8 #include "base/logging.h"
9 #include "base/message_loop_proxy.h"
10 #include "content/renderer/media/video_capture_impl_manager.h"
11 #include "content/renderer/pepper/pepper_plugin_delegate_impl.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "media/video/capture/video_capture_proxy.h"
17 PepperPlatformVideoCaptureImpl::PepperPlatformVideoCaptureImpl(
18 const base::WeakPtr
<PepperPluginDelegateImpl
>& plugin_delegate
,
19 const std::string
& device_id
,
20 webkit::ppapi::PluginDelegate::PlatformVideoCaptureEventHandler
* handler
)
21 : plugin_delegate_(plugin_delegate
),
22 device_id_(device_id
),
24 handler_proxy_(new media::VideoCaptureHandlerProxy(
25 this, base::MessageLoopProxy::current())),
28 unbalanced_start_(false) {
29 if (device_id
.empty()) {
30 // "1" is the session ID for the default device.
34 // We need to open the device and obtain the label and session ID before
36 if (plugin_delegate_
) {
37 plugin_delegate_
->OpenDevice(
38 PP_DEVICETYPE_DEV_VIDEOCAPTURE
, device_id
,
39 base::Bind(&PepperPlatformVideoCaptureImpl::OnDeviceOpened
, this));
44 void PepperPlatformVideoCaptureImpl::StartCapture(
45 media::VideoCapture::EventHandler
* handler
,
46 const media::VideoCaptureCapability
& capability
) {
47 DCHECK(handler
== handler_
);
49 if (unbalanced_start_
)
53 unbalanced_start_
= true;
54 AddRef(); // Will be balanced in OnRemoved().
55 video_capture_
->StartCapture(handler_proxy_
.get(), capability
);
59 void PepperPlatformVideoCaptureImpl::StopCapture(
60 media::VideoCapture::EventHandler
* handler
) {
61 DCHECK(handler
== handler_
);
62 if (!unbalanced_start_
)
66 unbalanced_start_
= false;
67 video_capture_
->StopCapture(handler_proxy_
.get());
71 void PepperPlatformVideoCaptureImpl::FeedBuffer(
72 scoped_refptr
<VideoFrameBuffer
> buffer
) {
74 video_capture_
->FeedBuffer(buffer
);
77 bool PepperPlatformVideoCaptureImpl::CaptureStarted() {
78 return handler_proxy_
->state().started
;
81 int PepperPlatformVideoCaptureImpl::CaptureWidth() {
82 return handler_proxy_
->state().width
;
85 int PepperPlatformVideoCaptureImpl::CaptureHeight() {
86 return handler_proxy_
->state().height
;
89 int PepperPlatformVideoCaptureImpl::CaptureFrameRate() {
90 return handler_proxy_
->state().frame_rate
;
93 void PepperPlatformVideoCaptureImpl::DetachEventHandler() {
98 void PepperPlatformVideoCaptureImpl::OnStarted(VideoCapture
* capture
) {
100 handler_
->OnStarted(capture
);
103 void PepperPlatformVideoCaptureImpl::OnStopped(VideoCapture
* capture
) {
105 handler_
->OnStopped(capture
);
108 void PepperPlatformVideoCaptureImpl::OnPaused(VideoCapture
* capture
) {
110 handler_
->OnPaused(capture
);
113 void PepperPlatformVideoCaptureImpl::OnError(
114 VideoCapture
* capture
,
117 handler_
->OnError(capture
, error_code
);
120 void PepperPlatformVideoCaptureImpl::OnRemoved(VideoCapture
* capture
) {
122 handler_
->OnRemoved(capture
);
124 Release(); // Balance the AddRef() in StartCapture().
127 void PepperPlatformVideoCaptureImpl::OnBufferReady(
128 VideoCapture
* capture
,
129 scoped_refptr
<VideoFrameBuffer
> buffer
) {
131 handler_
->OnBufferReady(capture
, buffer
);
133 // Even after handler_ is detached, we have to return buffers that are in
134 // flight to us. Otherwise VideoCaptureController will not tear down.
139 void PepperPlatformVideoCaptureImpl::OnDeviceInfoReceived(
140 VideoCapture
* capture
,
141 const media::VideoCaptureParams
& device_info
) {
143 handler_
->OnDeviceInfoReceived(capture
, device_info
);
146 PepperPlatformVideoCaptureImpl::~PepperPlatformVideoCaptureImpl() {
147 if (video_capture_
) {
148 VideoCaptureImplManager
* manager
=
149 RenderThreadImpl::current()->video_capture_impl_manager();
150 manager
->RemoveDevice(session_id_
, handler_proxy_
.get());
153 if (plugin_delegate_
&& !label_
.empty())
154 plugin_delegate_
->CloseDevice(label_
);
157 void PepperPlatformVideoCaptureImpl::Initialize() {
158 VideoCaptureImplManager
* manager
=
159 RenderThreadImpl::current()->video_capture_impl_manager();
160 video_capture_
= manager
->AddDevice(session_id_
, handler_proxy_
.get());
163 void PepperPlatformVideoCaptureImpl::OnDeviceOpened(int request_id
,
165 const std::string
& label
) {
166 succeeded
= succeeded
&& plugin_delegate_
;
169 session_id_
= plugin_delegate_
->GetSessionID(PP_DEVICETYPE_DEV_VIDEOCAPTURE
,
175 handler_
->OnInitialized(this, succeeded
);
178 } // namespace content