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.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "content/renderer/media/video_capture_impl_manager.h"
11 #include "content/renderer/pepper/pepper_media_device_manager.h"
12 #include "content/renderer/pepper/pepper_video_capture_host.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "media/video/capture/video_capture_proxy.h"
20 PepperPlatformVideoCapture::PepperPlatformVideoCapture(
21 const base::WeakPtr<RenderViewImpl>& render_view,
22 const std::string& device_id,
23 const GURL& document_url,
24 PepperVideoCaptureHost* handler)
25 : render_view_(render_view),
26 device_id_(device_id),
28 handler_proxy_(new media::VideoCaptureHandlerProxy(
29 this, base::MessageLoopProxy::current())),
31 unbalanced_start_(false),
32 pending_open_device_(false),
33 pending_open_device_id_(-1) {
34 // We need to open the device and obtain the label and session ID before
36 if (render_view_.get()) {
37 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice(
38 PP_DEVICETYPE_DEV_VIDEOCAPTURE,
41 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, this));
42 pending_open_device_ = true;
46 void PepperPlatformVideoCapture::StartCapture(
47 media::VideoCapture::EventHandler* handler,
48 const media::VideoCaptureParams& params) {
49 DCHECK(handler == handler_);
51 if (unbalanced_start_)
55 unbalanced_start_ = true;
56 AddRef(); // Will be balanced in OnRemoved().
57 video_capture_->StartCapture(handler_proxy_.get(), params);
61 void PepperPlatformVideoCapture::StopCapture(
62 media::VideoCapture::EventHandler* handler) {
63 DCHECK(handler == handler_);
64 if (!unbalanced_start_)
68 unbalanced_start_ = false;
69 video_capture_->StopCapture(handler_proxy_.get());
73 bool PepperPlatformVideoCapture::CaptureStarted() {
74 return handler_proxy_->state().started;
77 int PepperPlatformVideoCapture::CaptureFrameRate() {
78 return handler_proxy_->state().frame_rate;
81 void PepperPlatformVideoCapture::GetDeviceSupportedFormats(
82 const DeviceFormatsCallback& callback) {
86 void PepperPlatformVideoCapture::DetachEventHandler() {
90 video_capture_.reset();
92 if (render_view_.get()) {
93 if (!label_.empty()) {
94 GetMediaDeviceManager()->CloseDevice(label_);
97 if (pending_open_device_) {
98 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_);
99 pending_open_device_ = false;
100 pending_open_device_id_ = -1;
105 void PepperPlatformVideoCapture::OnStarted(VideoCapture* capture) {
107 handler_->OnStarted(capture);
110 void PepperPlatformVideoCapture::OnStopped(VideoCapture* capture) {
112 handler_->OnStopped(capture);
115 void PepperPlatformVideoCapture::OnPaused(VideoCapture* capture) {
117 handler_->OnPaused(capture);
120 void PepperPlatformVideoCapture::OnError(VideoCapture* capture,
123 handler_->OnError(capture, error_code);
126 void PepperPlatformVideoCapture::OnRemoved(VideoCapture* capture) {
128 handler_->OnRemoved(capture);
130 Release(); // Balance the AddRef() in StartCapture().
133 void PepperPlatformVideoCapture::OnFrameReady(
134 VideoCapture* capture,
135 const scoped_refptr<media::VideoFrame>& frame) {
137 handler_->OnFrameReady(capture, frame);
140 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() {
141 DCHECK(!video_capture_);
142 DCHECK(label_.empty());
143 DCHECK(!pending_open_device_);
146 void PepperPlatformVideoCapture::Initialize() {
147 VideoCaptureImplManager* manager =
148 RenderThreadImpl::current()->video_capture_impl_manager();
149 video_capture_ = manager->UseDevice(session_id_);
152 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id,
154 const std::string& label) {
155 pending_open_device_ = false;
156 pending_open_device_id_ = -1;
158 succeeded = succeeded && render_view_.get();
161 session_id_ = GetMediaDeviceManager()->GetSessionID(
162 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label);
167 handler_->OnInitialized(this, succeeded);
170 PepperMediaDeviceManager*
171 PepperPlatformVideoCapture::GetMediaDeviceManager() {
172 return PepperMediaDeviceManager::GetForRenderView(render_view_.get());
175 } // namespace content