video_source_resource.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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"
12#include "ppapi/proxy/ppb_image_data_proxy.h"
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
18using ppapi::thunk::EnterResourceNoLock;
19using ppapi::thunk::PPB_VideoSource_Private_API;
20
21namespace ppapi {
22namespace proxy {
23
24VideoSourceResource::VideoSourceResource(
25    Connection connection,
26    PP_Instance instance)
27    : PluginResource(connection, instance),
28      is_open_(false) {
29  SendCreate(RENDERER, PpapiHostMsg_VideoSource_Create());
30}
31
32VideoSourceResource::~VideoSourceResource() {
33}
34
35PPB_VideoSource_Private_API*
36    VideoSourceResource::AsPPB_VideoSource_Private_API() {
37  return this;
38}
39
40int32_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;
50  if (!stream_url_var.get() ||
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
59int32_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;
69
70  Call<PpapiPluginMsg_VideoSource_GetFrameReply>(RENDERER,
71      PpapiHostMsg_VideoSource_GetFrame(),
72      base::Bind(&VideoSourceResource::OnPluginMsgGetFrameComplete, this,
73                 frame));
74  return PP_OK_COMPLETIONPENDING;
75}
76
77void VideoSourceResource::Close() {
78  Post(RENDERER, PpapiHostMsg_VideoSource_Close());
79
80  if (TrackedCallback::IsPending(open_callback_))
81    open_callback_->PostAbort();
82  if (TrackedCallback::IsPending(get_frame_callback_))
83    get_frame_callback_->PostAbort();
84}
85
86void VideoSourceResource::OnPluginMsgOpenComplete(
87    const ResourceMessageReplyParams& reply_params) {
88  if (TrackedCallback::IsPending(open_callback_)) {
89    int32_t result = reply_params.result();
90    if (result == PP_OK)
91      is_open_ = true;
92    open_callback_->Run(result);
93  }
94}
95
96void VideoSourceResource::OnPluginMsgGetFrameComplete(
97    PP_VideoFrame_Private* frame,
98    const ResourceMessageReplyParams& reply_params,
99    const HostResource& image_data,
100    const PP_ImageDataDesc& image_desc,
101    int fd,
102    PP_TimeTicks timestamp) {
103  // The callback may have been aborted by Close().
104  if (TrackedCallback::IsPending(get_frame_callback_)) {
105    int32_t result = reply_params.result();
106    if (result == PP_OK &&
107        PPB_ImageData_Shared::IsImageDataDescValid(image_desc)) {
108      frame->timestamp = timestamp;
109
110#if defined(OS_ANDROID)
111      frame->image_data = 0;
112#elif defined(TOOLKIT_GTK)
113      frame->image_data =
114          (new PlatformImageData(image_data, image_desc, fd))->GetReference();
115#elif defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
116      base::SharedMemoryHandle handle;
117      if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &handle))
118        frame->image_data = 0;
119      frame->image_data =
120          (new PlatformImageData(
121              image_data, image_desc, handle))->GetReference();
122#else
123#error Not implemented.
124#endif
125    }
126    get_frame_callback_->Run(result);
127  }
128}
129
130}  // namespace proxy
131}  // namespace ppapi
132