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/pepper_video_capture_host.h" |
| 6 |
| 7 #include "content/public/renderer/renderer_ppapi_host.h" |
| 8 #include "ppapi/host/dispatch_host_message.h" |
| 9 #include "ppapi/host/host_message_context.h" |
| 10 #include "ppapi/host/ppapi_host.h" |
| 11 #include "ppapi/proxy/host_dispatcher.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/shared_impl/host_resource.h" |
| 14 #include "ppapi/shared_impl/ppapi_globals.h" |
| 15 #include "ppapi/shared_impl/ppb_device_ref_shared.h" |
| 16 #include "ppapi/thunk/enter.h" |
| 17 #include "ppapi/thunk/ppb_buffer_api.h" |
| 18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 19 #include "webkit/plugins/ppapi/ppb_buffer_impl.h" |
| 20 #include "webkit/plugins/ppapi/resource_helper.h" |
| 21 |
| 22 using ppapi::DeviceRefData; |
| 23 using ppapi::HostResource; |
| 24 using ppapi::PpapiGlobals; |
| 25 using ppapi::TrackedCallback; |
| 26 using ppapi::thunk::EnterResourceNoLock; |
| 27 using ppapi::thunk::PPB_Buffer_API; |
| 28 using ppapi::thunk::PPB_BufferTrusted_API; |
| 29 using webkit::ppapi::ResourceHelper; |
| 30 using webkit::ppapi::PPB_Buffer_Impl; |
| 31 using webkit::ppapi::PluginInstance; |
| 32 |
| 33 namespace { |
| 34 |
| 35 // Maximum number of buffers to actually allocate. |
| 36 const uint32_t kMaxBuffers = 20; |
| 37 |
| 38 } // namespace |
| 39 |
| 40 namespace content { |
| 41 |
| 42 PepperVideoCaptureHost::PepperVideoCaptureHost(RendererPpapiHost* host, |
| 43 PP_Instance instance, |
| 44 PP_Resource resource) |
| 45 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 46 renderer_ppapi_host_(host), |
| 47 buffer_count_hint_(0), |
| 48 status_(PP_VIDEO_CAPTURE_STATUS_STOPPED) { |
| 49 } |
| 50 |
| 51 PepperVideoCaptureHost::~PepperVideoCaptureHost() { |
| 52 Close(); |
| 53 } |
| 54 |
| 55 bool PepperVideoCaptureHost::Init() { |
| 56 PluginInstance* instance = GetPluginInstance(); |
| 57 return !!instance; |
| 58 } |
| 59 |
| 60 int32_t PepperVideoCaptureHost::OnResourceMessageReceived( |
| 61 const IPC::Message& msg, |
| 62 ppapi::host::HostMessageContext* context) { |
| 63 IPC_BEGIN_MESSAGE_MAP(PepperVideoCaptureHost, msg) |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 65 PpapiHostMsg_VideoCapture_EnumerateDevices, |
| 66 OnEnumerateDevices) |
| 67 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 68 PpapiHostMsg_VideoCapture_Open, |
| 69 OnOpen) |
| 70 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 71 PpapiHostMsg_VideoCapture_StartCapture, |
| 72 OnStartCapture) |
| 73 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 74 PpapiHostMsg_VideoCapture_ReuseBuffer, |
| 75 OnReuseBuffer) |
| 76 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 77 PpapiHostMsg_VideoCapture_StopCapture, |
| 78 OnStopCapture) |
| 79 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 80 PpapiHostMsg_VideoCapture_Close, |
| 81 OnClose) |
| 82 IPC_END_MESSAGE_MAP() |
| 83 return PP_ERROR_FAILED; |
| 84 } |
| 85 |
| 86 void PepperVideoCaptureHost::OnInitialized(media::VideoCapture* capture, |
| 87 bool succeeded) { |
| 88 DCHECK(capture == platform_video_capture_.get()); |
| 89 |
| 90 if (succeeded) { |
| 91 open_reply_context_.params.set_result(PP_OK); |
| 92 } else { |
| 93 DetachPlatformVideoCapture(); |
| 94 open_reply_context_.params.set_result(PP_ERROR_FAILED); |
| 95 } |
| 96 |
| 97 host()->SendReply(open_reply_context_, |
| 98 PpapiPluginMsg_VideoCapture_OpenReply()); |
| 99 } |
| 100 |
| 101 void PepperVideoCaptureHost::OnStarted(media::VideoCapture* capture) { |
| 102 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTED, false)) |
| 103 SendStatus(); |
| 104 } |
| 105 |
| 106 void PepperVideoCaptureHost::OnStopped(media::VideoCapture* capture) { |
| 107 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, false)) |
| 108 SendStatus(); |
| 109 } |
| 110 |
| 111 void PepperVideoCaptureHost::OnPaused(media::VideoCapture* capture) { |
| 112 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_PAUSED, false)) |
| 113 SendStatus(); |
| 114 } |
| 115 |
| 116 void PepperVideoCaptureHost::OnError(media::VideoCapture* capture, |
| 117 int error_code) { |
| 118 // Today, the media layer only sends "1" as an error. |
| 119 DCHECK(error_code == 1); |
| 120 // It either comes because some error was detected while starting (e.g. 2 |
| 121 // conflicting "master" resolution), or because the browser failed to start |
| 122 // the capture. |
| 123 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true); |
| 124 host()->SendUnsolicitedReply(pp_resource(), |
| 125 PpapiPluginMsg_VideoCapture_OnError(PP_ERROR_FAILED)); |
| 126 } |
| 127 |
| 128 void PepperVideoCaptureHost::OnRemoved(media::VideoCapture* capture) { |
| 129 } |
| 130 |
| 131 void PepperVideoCaptureHost::OnBufferReady( |
| 132 media::VideoCapture* capture, |
| 133 scoped_refptr<media::VideoCapture::VideoFrameBuffer> buffer) { |
| 134 DCHECK(buffer.get()); |
| 135 for (uint32_t i = 0; i < buffers_.size(); ++i) { |
| 136 if (!buffers_[i].in_use) { |
| 137 // TODO(ihf): Switch to a size calculation based on stride. |
| 138 // Stride is filled out now but not more meaningful than size |
| 139 // until wjia unifies VideoFrameBuffer and media::VideoFrame. |
| 140 size_t size = std::min(static_cast<size_t>(buffers_[i].buffer->size()), |
| 141 buffer->buffer_size); |
| 142 memcpy(buffers_[i].data, buffer->memory_pointer, size); |
| 143 buffers_[i].in_use = true; |
| 144 platform_video_capture_->FeedBuffer(buffer); |
| 145 host()->SendUnsolicitedReply(pp_resource(), |
| 146 PpapiPluginMsg_VideoCapture_OnBufferReady(i)); |
| 147 return; |
| 148 } |
| 149 } |
| 150 |
| 151 // No free slot, just discard the frame and tell the media layer it can |
| 152 // re-use the buffer. |
| 153 platform_video_capture_->FeedBuffer(buffer); |
| 154 } |
| 155 |
| 156 void PepperVideoCaptureHost::OnDeviceInfoReceived( |
| 157 media::VideoCapture* capture, |
| 158 const media::VideoCaptureParams& device_info) { |
| 159 PP_VideoCaptureDeviceInfo_Dev info = { |
| 160 static_cast<uint32_t>(device_info.width), |
| 161 static_cast<uint32_t>(device_info.height), |
| 162 static_cast<uint32_t>(device_info.frame_per_second) |
| 163 }; |
| 164 ReleaseBuffers(); |
| 165 |
| 166 // YUV 4:2:0 |
| 167 int uv_width = info.width / 2; |
| 168 int uv_height = info.height / 2; |
| 169 size_t size = info.width * info.height + 2 * uv_width * uv_height; |
| 170 |
| 171 ppapi::proxy::ResourceMessageReplyParams params(pp_resource(), 0); |
| 172 |
| 173 // Allocate buffers. We keep a reference to them, that is released in |
| 174 // ReleaseBuffers. In the mean time, we prepare the resource and handle here |
| 175 // for sending below. |
| 176 std::vector<HostResource> buffer_host_resources; |
| 177 buffers_.reserve(buffer_count_hint_); |
| 178 ::ppapi::ResourceTracker* tracker = |
| 179 PpapiGlobals::Get()->GetResourceTracker(); |
| 180 ppapi::proxy::HostDispatcher* dispatcher = |
| 181 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance()); |
| 182 for (size_t i = 0; i < buffer_count_hint_; ++i) { |
| 183 PP_Resource res = PPB_Buffer_Impl::Create(pp_instance(), size); |
| 184 if (!res) |
| 185 break; |
| 186 |
| 187 EnterResourceNoLock<PPB_Buffer_API> enter(res, true); |
| 188 DCHECK(enter.succeeded()); |
| 189 |
| 190 BufferInfo buf; |
| 191 buf.buffer = static_cast<PPB_Buffer_Impl*>(enter.object()); |
| 192 buf.data = buf.buffer->Map(); |
| 193 if (!buf.data) { |
| 194 tracker->ReleaseResource(res); |
| 195 break; |
| 196 } |
| 197 buffers_.push_back(buf); |
| 198 |
| 199 // Add to HostResource array to be sent. |
| 200 { |
| 201 HostResource host_resource; |
| 202 host_resource.SetHostResource(pp_instance(), res); |
| 203 buffer_host_resources.push_back(host_resource); |
| 204 |
| 205 // Add a reference for the plugin, which is resposible for releasing it. |
| 206 tracker->AddRefResource(res); |
| 207 } |
| 208 |
| 209 // Add the serialized shared memory handle to params. FileDescriptor is |
| 210 // treated in special case. |
| 211 { |
| 212 EnterResourceNoLock<PPB_BufferTrusted_API> enter(res, true); |
| 213 DCHECK(enter.succeeded()); |
| 214 int handle; |
| 215 int32_t result = enter.object()->GetSharedMemory(&handle); |
| 216 DCHECK(result == PP_OK); |
| 217 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, |
| 218 // those casts are ugly. |
| 219 base::PlatformFile platform_file = |
| 220 #if defined(OS_WIN) |
| 221 reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 222 #elif defined(OS_POSIX) |
| 223 handle; |
| 224 #else |
| 225 #error Not implemented. |
| 226 #endif |
| 227 params.AppendHandle( |
| 228 ppapi::proxy::SerializedHandle( |
| 229 dispatcher->ShareHandleWithRemote(platform_file, false), |
| 230 size)); |
| 231 } |
| 232 } |
| 233 |
| 234 if (buffers_.empty()) { |
| 235 // We couldn't allocate/map buffers at all. Send an error and stop the |
| 236 // capture. |
| 237 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, true); |
| 238 platform_video_capture_->StopCapture(this); |
| 239 OnError(capture, PP_ERROR_NOMEMORY); |
| 240 return; |
| 241 } |
| 242 |
| 243 host()->Send(new PpapiPluginMsg_ResourceReply( |
| 244 params, PpapiPluginMsg_VideoCapture_OnDeviceInfo( |
| 245 info, buffer_host_resources, size))); |
| 246 } |
| 247 |
| 248 PluginInstance* PepperVideoCaptureHost::GetPluginInstance() const { |
| 249 return ResourceHelper::PPInstanceToPluginInstance(pp_instance()); |
| 250 } |
| 251 |
| 252 int32_t PepperVideoCaptureHost::OnEnumerateDevices( |
| 253 ppapi::host::HostMessageContext* context) { |
| 254 PluginInstance* instance = GetPluginInstance(); |
| 255 if (!instance) |
| 256 return PP_ERROR_FAILED; |
| 257 |
| 258 enum_reply_context_ = context->MakeReplyMessageContext(); |
| 259 instance->delegate()->EnumerateDevices( |
| 260 PP_DEVICETYPE_DEV_VIDEOCAPTURE, |
| 261 base::Bind(&PepperVideoCaptureHost::EnumerateDevicesCallbackFunc, |
| 262 AsWeakPtr())); |
| 263 return PP_OK_COMPLETIONPENDING; |
| 264 } |
| 265 |
| 266 int32_t PepperVideoCaptureHost::OnOpen( |
| 267 ppapi::host::HostMessageContext* context, |
| 268 const std::string& device_id, |
| 269 const PP_VideoCaptureDeviceInfo_Dev& requested_info, |
| 270 uint32_t buffer_count) { |
| 271 if (platform_video_capture_.get()) |
| 272 return PP_ERROR_FAILED; |
| 273 |
| 274 PluginInstance* instance = GetPluginInstance(); |
| 275 if (!instance) |
| 276 return PP_ERROR_FAILED; |
| 277 |
| 278 SetRequestedInfo(requested_info, buffer_count); |
| 279 |
| 280 platform_video_capture_ = |
| 281 instance->delegate()->CreateVideoCapture(device_id, this); |
| 282 |
| 283 open_reply_context_ = context->MakeReplyMessageContext(); |
| 284 |
| 285 // It is able to complete synchronously if the default device is used. |
| 286 bool sync_completion = device_id.empty(); |
| 287 if (sync_completion) { |
| 288 // Send OpenACK directly, but still need to return PP_OK_COMPLETIONPENDING |
| 289 // to make PluginResource happy. |
| 290 OnInitialized(platform_video_capture_.get(), true); |
| 291 } |
| 292 |
| 293 return PP_OK_COMPLETIONPENDING; |
| 294 } |
| 295 |
| 296 int32_t PepperVideoCaptureHost::OnStartCapture( |
| 297 ppapi::host::HostMessageContext* context) { |
| 298 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTING, false) || |
| 299 !platform_video_capture_.get()) |
| 300 return PP_ERROR_FAILED; |
| 301 |
| 302 DCHECK(buffers_.empty()); |
| 303 |
| 304 // It's safe to call this regardless it's capturing or not, because |
| 305 // PepperPlatformVideoCaptureImpl maintains the state. |
| 306 platform_video_capture_->StartCapture(this, capability_); |
| 307 return PP_OK; |
| 308 } |
| 309 |
| 310 int32_t PepperVideoCaptureHost::OnReuseBuffer( |
| 311 ppapi::host::HostMessageContext* context, |
| 312 uint32_t buffer) { |
| 313 if (buffer >= buffers_.size() || !buffers_[buffer].in_use) |
| 314 return PP_ERROR_BADARGUMENT; |
| 315 buffers_[buffer].in_use = false; |
| 316 return PP_OK; |
| 317 } |
| 318 |
| 319 int32_t PepperVideoCaptureHost::OnStopCapture( |
| 320 ppapi::host::HostMessageContext* context) { |
| 321 return StopCapture(); |
| 322 } |
| 323 |
| 324 int32_t PepperVideoCaptureHost::OnClose( |
| 325 ppapi::host::HostMessageContext* context) { |
| 326 return Close(); |
| 327 } |
| 328 |
| 329 int32_t PepperVideoCaptureHost::StopCapture() { |
| 330 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, false)) |
| 331 return PP_ERROR_FAILED; |
| 332 |
| 333 DCHECK(platform_video_capture_.get()); |
| 334 |
| 335 ReleaseBuffers(); |
| 336 // It's safe to call this regardless it's capturing or not, because |
| 337 // PepperPlatformVideoCaptureImpl maintains the state. |
| 338 platform_video_capture_->StopCapture(this); |
| 339 return PP_OK; |
| 340 } |
| 341 |
| 342 int32_t PepperVideoCaptureHost::Close() { |
| 343 if (!platform_video_capture_.get()) |
| 344 return PP_OK; |
| 345 |
| 346 StopCapture(); |
| 347 DCHECK(buffers_.empty()); |
| 348 DetachPlatformVideoCapture(); |
| 349 return PP_OK; |
| 350 } |
| 351 |
| 352 void PepperVideoCaptureHost::ReleaseBuffers() { |
| 353 ::ppapi::ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); |
| 354 for (size_t i = 0; i < buffers_.size(); ++i) { |
| 355 buffers_[i].buffer->Unmap(); |
| 356 tracker->ReleaseResource(buffers_[i].buffer->pp_resource()); |
| 357 } |
| 358 buffers_.clear(); |
| 359 } |
| 360 |
| 361 void PepperVideoCaptureHost::SendStatus() { |
| 362 host()->SendUnsolicitedReply(pp_resource(), |
| 363 PpapiPluginMsg_VideoCapture_OnStatus(status_)); |
| 364 } |
| 365 |
| 366 void PepperVideoCaptureHost::SetRequestedInfo( |
| 367 const PP_VideoCaptureDeviceInfo_Dev& device_info, |
| 368 uint32_t buffer_count) { |
| 369 // Clamp the buffer count to between 1 and |kMaxBuffers|. |
| 370 buffer_count_hint_ = std::min(std::max(buffer_count, 1U), kMaxBuffers); |
| 371 |
| 372 capability_.width = device_info.width; |
| 373 capability_.height = device_info.height; |
| 374 capability_.frame_rate = device_info.frames_per_second; |
| 375 capability_.expected_capture_delay = 0; // Ignored. |
| 376 capability_.color = media::VideoCaptureCapability::kI420; |
| 377 capability_.interlaced = false; // Ignored. |
| 378 } |
| 379 |
| 380 void PepperVideoCaptureHost::DetachPlatformVideoCapture() { |
| 381 if (platform_video_capture_.get()) { |
| 382 platform_video_capture_->DetachEventHandler(); |
| 383 platform_video_capture_ = NULL; |
| 384 } |
| 385 } |
| 386 |
| 387 void PepperVideoCaptureHost::EnumerateDevicesCallbackFunc( |
| 388 int request_id, |
| 389 bool succeeded, |
| 390 const std::vector<ppapi::DeviceRefData>& devices) { |
| 391 PluginInstance* instance = GetPluginInstance(); |
| 392 if (instance) |
| 393 instance->delegate()->StopEnumerateDevices(request_id); |
| 394 |
| 395 if (succeeded) { |
| 396 enum_reply_context_.params.set_result(PP_OK); |
| 397 host()->SendReply(enum_reply_context_, |
| 398 PpapiPluginMsg_VideoCapture_EnumerateDevicesReply( |
| 399 devices)); |
| 400 } else { |
| 401 enum_reply_context_.params.set_result(PP_ERROR_FAILED); |
| 402 host()->SendReply(enum_reply_context_, |
| 403 PpapiPluginMsg_VideoCapture_EnumerateDevicesReply( |
| 404 std::vector<DeviceRefData>())); |
| 405 } |
| 406 } |
| 407 |
| 408 bool PepperVideoCaptureHost::SetStatus(PP_VideoCaptureStatus_Dev status, |
| 409 bool forced) { |
| 410 if (!forced) { |
| 411 switch (status) { |
| 412 case PP_VIDEO_CAPTURE_STATUS_STOPPED: |
| 413 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPING) |
| 414 return false; |
| 415 break; |
| 416 case PP_VIDEO_CAPTURE_STATUS_STARTING: |
| 417 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPED) |
| 418 return false; |
| 419 break; |
| 420 case PP_VIDEO_CAPTURE_STATUS_STARTED: |
| 421 switch (status_) { |
| 422 case PP_VIDEO_CAPTURE_STATUS_STARTING: |
| 423 case PP_VIDEO_CAPTURE_STATUS_PAUSED: |
| 424 break; |
| 425 default: |
| 426 return false; |
| 427 } |
| 428 break; |
| 429 case PP_VIDEO_CAPTURE_STATUS_PAUSED: |
| 430 switch (status_) { |
| 431 case PP_VIDEO_CAPTURE_STATUS_STARTING: |
| 432 case PP_VIDEO_CAPTURE_STATUS_STARTED: |
| 433 break; |
| 434 default: |
| 435 return false; |
| 436 } |
| 437 break; |
| 438 case PP_VIDEO_CAPTURE_STATUS_STOPPING: |
| 439 switch (status_) { |
| 440 case PP_VIDEO_CAPTURE_STATUS_STARTING: |
| 441 case PP_VIDEO_CAPTURE_STATUS_STARTED: |
| 442 case PP_VIDEO_CAPTURE_STATUS_PAUSED: |
| 443 break; |
| 444 default: |
| 445 return false; |
| 446 } |
| 447 break; |
| 448 } |
| 449 } |
| 450 |
| 451 status_ = status; |
| 452 return true; |
| 453 } |
| 454 |
| 455 PepperVideoCaptureHost::BufferInfo::BufferInfo() |
| 456 : in_use(false), |
| 457 data(NULL), |
| 458 buffer() { |
| 459 } |
| 460 |
| 461 PepperVideoCaptureHost::BufferInfo::~BufferInfo() { |
| 462 } |
| 463 |
| 464 } // namespace content |
OLD | NEW |