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::GetDeviceFormatsInUse(
87 const DeviceFormatsInUseCallback& callback) {
91 void PepperPlatformVideoCapture::DetachEventHandler() {
95 video_capture_.reset();
97 if (render_view_.get()) {
98 if (!label_.empty()) {
99 GetMediaDeviceManager()->CloseDevice(label_);
102 if (pending_open_device_) {
103 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_);
104 pending_open_device_ = false;
105 pending_open_device_id_ = -1;
110 void PepperPlatformVideoCapture::OnStarted(VideoCapture* capture) {
112 handler_->OnStarted(capture);
115 void PepperPlatformVideoCapture::OnStopped(VideoCapture* capture) {
117 handler_->OnStopped(capture);
120 void PepperPlatformVideoCapture::OnPaused(VideoCapture* capture) {
122 handler_->OnPaused(capture);
125 void PepperPlatformVideoCapture::OnError(VideoCapture* capture,
128 handler_->OnError(capture, error_code);
131 void PepperPlatformVideoCapture::OnRemoved(VideoCapture* capture) {
133 handler_->OnRemoved(capture);
135 Release(); // Balance the AddRef() in StartCapture().
138 void PepperPlatformVideoCapture::OnFrameReady(
139 VideoCapture* capture,
140 const scoped_refptr<media::VideoFrame>& frame) {
142 handler_->OnFrameReady(capture, frame);
145 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() {
146 DCHECK(!video_capture_);
147 DCHECK(label_.empty());
148 DCHECK(!pending_open_device_);
151 void PepperPlatformVideoCapture::Initialize() {
152 VideoCaptureImplManager* manager =
153 RenderThreadImpl::current()->video_capture_impl_manager();
154 video_capture_ = manager->UseDevice(session_id_);
157 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id,
159 const std::string& label) {
160 pending_open_device_ = false;
161 pending_open_device_id_ = -1;
163 succeeded = succeeded && render_view_.get();
166 session_id_ = GetMediaDeviceManager()->GetSessionID(
167 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label);
172 handler_->OnInitialized(this, succeeded);
175 PepperMediaDeviceManager*
176 PepperPlatformVideoCapture::GetMediaDeviceManager() {
177 return PepperMediaDeviceManager::GetForRenderView(render_view_.get());
180 } // namespace content