OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/renderer/pepper_platform_video_capture_impl.h" |
| 6 |
| 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_plugin_delegate_impl.h" |
| 12 #include "content/renderer/render_thread_impl.h" |
| 13 #include "media/video/capture/video_capture_proxy.h" |
| 14 |
| 15 PepperPlatformVideoCaptureImpl::PepperPlatformVideoCaptureImpl( |
| 16 const base::WeakPtr<PepperPluginDelegateImpl>& plugin_delegate, |
| 17 const std::string& device_id, |
| 18 webkit::ppapi::PluginDelegate::PlatformVideoCaptureEventHandler* handler) |
| 19 : plugin_delegate_(plugin_delegate), |
| 20 device_id_(device_id), |
| 21 session_id_(0), |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 23 handler_proxy_(new media::VideoCaptureHandlerProxy( |
| 24 this, base::MessageLoopProxy::current()))), |
| 25 handler_(handler), |
| 26 video_capture_(NULL), |
| 27 unbalanced_start_(false) { |
| 28 if (device_id.empty()) { |
| 29 // "1" is the session ID for the default device. |
| 30 session_id_ = 1; |
| 31 Initialize(); |
| 32 } else { |
| 33 // We need to open the device and obtain the label and session ID before |
| 34 // initializing. |
| 35 if (plugin_delegate_) { |
| 36 plugin_delegate_->OpenDevice( |
| 37 PP_DEVICETYPE_DEV_VIDEOCAPTURE, device_id, |
| 38 base::Bind(&PepperPlatformVideoCaptureImpl::OnDeviceOpened, this)); |
| 39 } |
| 40 } |
| 41 } |
| 42 |
| 43 PepperPlatformVideoCaptureImpl::~PepperPlatformVideoCaptureImpl() { |
| 44 if (video_capture_) { |
| 45 VideoCaptureImplManager* manager = |
| 46 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 47 manager->RemoveDevice(session_id_, handler_proxy_.get()); |
| 48 } |
| 49 |
| 50 if (plugin_delegate_ && !label_.empty()) |
| 51 plugin_delegate_->CloseDevice(label_); |
| 52 } |
| 53 |
| 54 void PepperPlatformVideoCaptureImpl::StartCapture( |
| 55 media::VideoCapture::EventHandler* handler, |
| 56 const VideoCaptureCapability& capability) { |
| 57 DCHECK(handler == handler_); |
| 58 |
| 59 if (unbalanced_start_) |
| 60 return; |
| 61 |
| 62 if (video_capture_) { |
| 63 unbalanced_start_ = true; |
| 64 AddRef(); // Will be balanced in OnRemoved(). |
| 65 video_capture_->StartCapture(handler_proxy_.get(), capability); |
| 66 } |
| 67 } |
| 68 |
| 69 void PepperPlatformVideoCaptureImpl::StopCapture( |
| 70 media::VideoCapture::EventHandler* handler) { |
| 71 DCHECK(handler == handler_); |
| 72 if (!unbalanced_start_) |
| 73 return; |
| 74 |
| 75 if (video_capture_) { |
| 76 unbalanced_start_ = false; |
| 77 video_capture_->StopCapture(handler_proxy_.get()); |
| 78 } |
| 79 } |
| 80 |
| 81 void PepperPlatformVideoCaptureImpl::FeedBuffer( |
| 82 scoped_refptr<VideoFrameBuffer> buffer) { |
| 83 if (video_capture_) |
| 84 video_capture_->FeedBuffer(buffer); |
| 85 } |
| 86 |
| 87 bool PepperPlatformVideoCaptureImpl::CaptureStarted() { |
| 88 return handler_proxy_->state().started; |
| 89 } |
| 90 |
| 91 int PepperPlatformVideoCaptureImpl::CaptureWidth() { |
| 92 return handler_proxy_->state().width; |
| 93 } |
| 94 |
| 95 int PepperPlatformVideoCaptureImpl::CaptureHeight() { |
| 96 return handler_proxy_->state().height; |
| 97 } |
| 98 |
| 99 int PepperPlatformVideoCaptureImpl::CaptureFrameRate() { |
| 100 return handler_proxy_->state().frame_rate; |
| 101 } |
| 102 |
| 103 void PepperPlatformVideoCaptureImpl::DetachEventHandler() { |
| 104 handler_ = NULL; |
| 105 StopCapture(NULL); |
| 106 } |
| 107 |
| 108 void PepperPlatformVideoCaptureImpl::OnStarted(VideoCapture* capture) { |
| 109 if (handler_) |
| 110 handler_->OnStarted(capture); |
| 111 } |
| 112 |
| 113 void PepperPlatformVideoCaptureImpl::OnStopped(VideoCapture* capture) { |
| 114 if (handler_) |
| 115 handler_->OnStopped(capture); |
| 116 } |
| 117 |
| 118 void PepperPlatformVideoCaptureImpl::OnPaused(VideoCapture* capture) { |
| 119 if (handler_) |
| 120 handler_->OnPaused(capture); |
| 121 } |
| 122 |
| 123 void PepperPlatformVideoCaptureImpl::OnError( |
| 124 VideoCapture* capture, |
| 125 int error_code) { |
| 126 if (handler_) |
| 127 handler_->OnError(capture, error_code); |
| 128 } |
| 129 |
| 130 void PepperPlatformVideoCaptureImpl::OnRemoved(VideoCapture* capture) { |
| 131 if (handler_) |
| 132 handler_->OnRemoved(capture); |
| 133 |
| 134 Release(); // Balance the AddRef() in StartCapture(). |
| 135 } |
| 136 |
| 137 void PepperPlatformVideoCaptureImpl::OnBufferReady( |
| 138 VideoCapture* capture, |
| 139 scoped_refptr<VideoFrameBuffer> buffer) { |
| 140 if (handler_) { |
| 141 handler_->OnBufferReady(capture, buffer); |
| 142 } else { |
| 143 // Even after handler_ is detached, we have to return buffers that are in |
| 144 // flight to us. Otherwise VideoCaptureController will not tear down. |
| 145 FeedBuffer(buffer); |
| 146 } |
| 147 } |
| 148 |
| 149 void PepperPlatformVideoCaptureImpl::OnDeviceInfoReceived( |
| 150 VideoCapture* capture, |
| 151 const media::VideoCaptureParams& device_info) { |
| 152 if (handler_) |
| 153 handler_->OnDeviceInfoReceived(capture, device_info); |
| 154 } |
| 155 |
| 156 void PepperPlatformVideoCaptureImpl::Initialize() { |
| 157 VideoCaptureImplManager* manager = |
| 158 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 159 video_capture_ = manager->AddDevice(session_id_, handler_proxy_.get()); |
| 160 } |
| 161 |
| 162 void PepperPlatformVideoCaptureImpl::OnDeviceOpened(int request_id, |
| 163 bool succeeded, |
| 164 const std::string& label) { |
| 165 succeeded = succeeded && plugin_delegate_; |
| 166 if (succeeded) { |
| 167 label_ = label; |
| 168 session_id_ = plugin_delegate_->GetSessionID(PP_DEVICETYPE_DEV_VIDEOCAPTURE, |
| 169 label); |
| 170 Initialize(); |
| 171 } |
| 172 |
| 173 if (handler_) |
| 174 handler_->OnInitialized(this, succeeded); |
| 175 } |
OLD | NEW |