[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "ppapi/proxy/video_source_resource.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "ipc/ipc_message.h" |
| 9 | #include "ppapi/c/pp_errors.h" |
| 10 | #include "ppapi/c/private/pp_video_frame_private.h" |
| 11 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 12 | #include "ppapi/proxy/ppb_image_data_proxy.h" |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 13 | #include "ppapi/shared_impl/ppapi_globals.h" |
| 14 | #include "ppapi/shared_impl/resource_tracker.h" |
| 15 | #include "ppapi/shared_impl/var.h" |
| 16 | #include "ppapi/thunk/enter.h" |
| 17 | |
| 18 | using ppapi::thunk::EnterResourceNoLock; |
| 19 | using ppapi::thunk::PPB_VideoSource_Private_API; |
| 20 | |
| 21 | namespace ppapi { |
| 22 | namespace proxy { |
| 23 | |
| 24 | VideoSourceResource::VideoSourceResource( |
| 25 | Connection connection, |
| 26 | PP_Instance instance) |
| 27 | : PluginResource(connection, instance), |
| 28 | is_open_(false) { |
| 29 | SendCreate(RENDERER, PpapiHostMsg_VideoSource_Create()); |
| 30 | } |
| 31 | |
| 32 | VideoSourceResource::~VideoSourceResource() { |
| 33 | } |
| 34 | |
| 35 | PPB_VideoSource_Private_API* |
| 36 | VideoSourceResource::AsPPB_VideoSource_Private_API() { |
| 37 | return this; |
| 38 | } |
| 39 | |
| 40 | int32_t VideoSourceResource::Open( |
| 41 | const PP_Var& stream_url, |
| 42 | scoped_refptr<TrackedCallback> callback) { |
| 43 | if (TrackedCallback::IsPending(open_callback_)) |
| 44 | return PP_ERROR_INPROGRESS; |
| 45 | |
| 46 | open_callback_ = callback; |
| 47 | |
| 48 | scoped_refptr<StringVar> stream_url_var = StringVar::FromPPVar(stream_url); |
| 49 | const uint32_t kMaxStreamIdSizeInBytes = 16384; |
[email protected] | f0c8624 | 2013-06-02 21:25:43 | [diff] [blame] | 50 | if (!stream_url_var.get() || |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 51 | stream_url_var->value().size() > kMaxStreamIdSizeInBytes) |
| 52 | return PP_ERROR_BADARGUMENT; |
| 53 | Call<PpapiPluginMsg_VideoSource_OpenReply>(RENDERER, |
| 54 | PpapiHostMsg_VideoSource_Open(stream_url_var->value()), |
| 55 | base::Bind(&VideoSourceResource::OnPluginMsgOpenComplete, this)); |
| 56 | return PP_OK_COMPLETIONPENDING; |
| 57 | } |
| 58 | |
| 59 | int32_t VideoSourceResource::GetFrame( |
| 60 | PP_VideoFrame_Private* frame, |
| 61 | scoped_refptr<TrackedCallback> callback) { |
| 62 | if (!is_open_) |
| 63 | return PP_ERROR_FAILED; |
| 64 | |
| 65 | if (TrackedCallback::IsPending(get_frame_callback_)) |
| 66 | return PP_ERROR_INPROGRESS; |
| 67 | |
| 68 | get_frame_callback_ = callback; |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 69 | Call<PpapiPluginMsg_VideoSource_GetFrameReply>(RENDERER, |
| 70 | PpapiHostMsg_VideoSource_GetFrame(), |
| 71 | base::Bind(&VideoSourceResource::OnPluginMsgGetFrameComplete, this, |
| 72 | frame)); |
| 73 | return PP_OK_COMPLETIONPENDING; |
| 74 | } |
| 75 | |
| 76 | void VideoSourceResource::Close() { |
| 77 | Post(RENDERER, PpapiHostMsg_VideoSource_Close()); |
| 78 | |
| 79 | if (TrackedCallback::IsPending(open_callback_)) |
| 80 | open_callback_->PostAbort(); |
| 81 | if (TrackedCallback::IsPending(get_frame_callback_)) |
| 82 | get_frame_callback_->PostAbort(); |
| 83 | } |
| 84 | |
| 85 | void VideoSourceResource::OnPluginMsgOpenComplete( |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 86 | const ResourceMessageReplyParams& reply_params) { |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 87 | if (TrackedCallback::IsPending(open_callback_)) { |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 88 | int32_t result = reply_params.result(); |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 89 | if (result == PP_OK) |
| 90 | is_open_ = true; |
| 91 | open_callback_->Run(result); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void VideoSourceResource::OnPluginMsgGetFrameComplete( |
| 96 | PP_VideoFrame_Private* frame, |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 97 | const ResourceMessageReplyParams& reply_params, |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 98 | const HostResource& image_data, |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 99 | const PP_ImageDataDesc& image_desc, |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 100 | PP_TimeTicks timestamp) { |
| 101 | // The callback may have been aborted by Close(). |
| 102 | if (TrackedCallback::IsPending(get_frame_callback_)) { |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 103 | int32_t result = reply_params.result(); |
[email protected] | ebdbb11 | 2013-05-16 16:56:34 | [diff] [blame] | 104 | if (result == PP_OK && |
| 105 | PPB_ImageData_Shared::IsImageDataDescValid(image_desc)) { |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 106 | frame->timestamp = timestamp; |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 107 | |
[email protected] | 38f61e8f | 2013-05-11 19:23:38 | [diff] [blame] | 108 | base::SharedMemoryHandle handle; |
| 109 | if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &handle)) |
| 110 | frame->image_data = 0; |
| 111 | frame->image_data = |
[email protected] | 558c190 | 2013-06-20 07:59:15 | [diff] [blame] | 112 | (new SimpleImageData( |
[email protected] | 4e1b91e | 2013-06-12 00:18:13 | [diff] [blame] | 113 | image_data, image_desc, handle))->GetReference(); |
[email protected] | b76257d | 2013-05-04 00:11:30 | [diff] [blame] | 114 | } |
| 115 | get_frame_callback_->Run(result); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } // namespace proxy |
| 120 | } // namespace ppapi |