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())),
32 unbalanced_start_(false),
33 pending_open_device_(false),
34 pending_open_device_id_(-1) {
35 // We need to open the device and obtain the label and session ID before
37 if (render_view_.get()) {
38 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice(
39 PP_DEVICETYPE_DEV_VIDEOCAPTURE,
42 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, this));
43 pending_open_device_ = true;
47 void PepperPlatformVideoCapture::StartCapture(
48 media::VideoCapture::EventHandler* handler,
49 const media::VideoCaptureParams& params) {
50 DCHECK(handler == handler_);
52 if (unbalanced_start_)
56 unbalanced_start_ = true;
57 AddRef(); // Will be balanced in OnRemoved().
58 video_capture_->StartCapture(handler_proxy_.get(), params);
62 void PepperPlatformVideoCapture::StopCapture(
63 media::VideoCapture::EventHandler* handler) {
64 DCHECK(handler == handler_);
65 if (!unbalanced_start_)
69 unbalanced_start_ = false;
70 video_capture_->StopCapture(handler_proxy_.get());
74 bool PepperPlatformVideoCapture::CaptureStarted() {
75 return handler_proxy_->state().started;
78 int PepperPlatformVideoCapture::CaptureFrameRate() {
79 return handler_proxy_->state().frame_rate;
82 void PepperPlatformVideoCapture::DetachEventHandler() {
87 VideoCaptureImplManager* manager =
88 RenderThreadImpl::current()->video_capture_impl_manager();
89 manager->RemoveDevice(session_id_, handler_proxy_.get());
90 video_capture_ = NULL;
93 if (render_view_.get()) {
94 if (!label_.empty()) {
95 GetMediaDeviceManager()->CloseDevice(label_);
98 if (pending_open_device_) {
99 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_);
100 pending_open_device_ = false;
101 pending_open_device_id_ = -1;
106 void PepperPlatformVideoCapture::OnStarted(VideoCapture* capture) {
108 handler_->OnStarted(capture);
111 void PepperPlatformVideoCapture::OnStopped(VideoCapture* capture) {
113 handler_->OnStopped(capture);
116 void PepperPlatformVideoCapture::OnPaused(VideoCapture* capture) {
118 handler_->OnPaused(capture);
121 void PepperPlatformVideoCapture::OnError(VideoCapture* capture,
124 handler_->OnError(capture, error_code);
127 void PepperPlatformVideoCapture::OnRemoved(VideoCapture* capture) {
129 handler_->OnRemoved(capture);
131 Release(); // Balance the AddRef() in StartCapture().
134 void PepperPlatformVideoCapture::OnFrameReady(
135 VideoCapture* capture,
136 const scoped_refptr<media::VideoFrame>& frame) {
138 handler_->OnFrameReady(capture, frame);
141 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() {
142 DCHECK(!video_capture_);
143 DCHECK(label_.empty());
144 DCHECK(!pending_open_device_);
147 void PepperPlatformVideoCapture::Initialize() {
148 VideoCaptureImplManager* manager =
149 RenderThreadImpl::current()->video_capture_impl_manager();
150 video_capture_ = manager->AddDevice(session_id_, handler_proxy_.get());
153 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id,
155 const std::string& label) {
156 pending_open_device_ = false;
157 pending_open_device_id_ = -1;
159 succeeded = succeeded && render_view_.get();
162 session_id_ = GetMediaDeviceManager()->GetSessionID(
163 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label);
168 handler_->OnInitialized(this, succeeded);
171 PepperMediaDeviceManager*
172 PepperPlatformVideoCapture::GetMediaDeviceManager() {
173 return PepperMediaDeviceManager::GetForRenderView(render_view_.get());
176 } // namespace content