Disable ContentSettingBubbleModelTest.RPHAllow which is flaky.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_platform_video_capture_impl.cc
blob39ee60922df0fc0db4db3f166c4f3d572a12c756
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"
7 #include "base/bind.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"
15 namespace content {
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),
23 session_id_(0),
24 handler_proxy_(new media::VideoCaptureHandlerProxy(
25 this, base::MessageLoopProxy::current())),
26 handler_(handler),
27 video_capture_(NULL),
28 unbalanced_start_(false) {
29 if (device_id.empty()) {
30 // "1" is the session ID for the default device.
31 session_id_ = 1;
32 Initialize();
33 } else {
34 // We need to open the device and obtain the label and session ID before
35 // initializing.
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_)
50 return;
52 if (video_capture_) {
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_)
63 return;
65 if (video_capture_) {
66 unbalanced_start_ = false;
67 video_capture_->StopCapture(handler_proxy_.get());
71 void PepperPlatformVideoCaptureImpl::FeedBuffer(
72 scoped_refptr<VideoFrameBuffer> buffer) {
73 if (video_capture_)
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() {
94 handler_ = NULL;
95 StopCapture(NULL);
98 void PepperPlatformVideoCaptureImpl::OnStarted(VideoCapture* capture) {
99 if (handler_)
100 handler_->OnStarted(capture);
103 void PepperPlatformVideoCaptureImpl::OnStopped(VideoCapture* capture) {
104 if (handler_)
105 handler_->OnStopped(capture);
108 void PepperPlatformVideoCaptureImpl::OnPaused(VideoCapture* capture) {
109 if (handler_)
110 handler_->OnPaused(capture);
113 void PepperPlatformVideoCaptureImpl::OnError(
114 VideoCapture* capture,
115 int error_code) {
116 if (handler_)
117 handler_->OnError(capture, error_code);
120 void PepperPlatformVideoCaptureImpl::OnRemoved(VideoCapture* capture) {
121 if (handler_)
122 handler_->OnRemoved(capture);
124 Release(); // Balance the AddRef() in StartCapture().
127 void PepperPlatformVideoCaptureImpl::OnBufferReady(
128 VideoCapture* capture,
129 scoped_refptr<VideoFrameBuffer> buffer) {
130 if (handler_) {
131 handler_->OnBufferReady(capture, buffer);
132 } else {
133 // Even after handler_ is detached, we have to return buffers that are in
134 // flight to us. Otherwise VideoCaptureController will not tear down.
135 FeedBuffer(buffer);
139 void PepperPlatformVideoCaptureImpl::OnDeviceInfoReceived(
140 VideoCapture* capture,
141 const media::VideoCaptureParams& device_info) {
142 if (handler_)
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,
164 bool succeeded,
165 const std::string& label) {
166 succeeded = succeeded && plugin_delegate_;
167 if (succeeded) {
168 label_ = label;
169 session_id_ = plugin_delegate_->GetSessionID(PP_DEVICETYPE_DEV_VIDEOCAPTURE,
170 label);
171 Initialize();
174 if (handler_)
175 handler_->OnInitialized(this, succeeded);
178 } // namespace content