summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/video_source_resource.cc
blob: f6c5fdc41db100166635c0e09ebcb810f2b0f011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/video_source_resource.h"

#include "base/bind.h"
#include "ipc/ipc_message.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/private/pp_video_frame_private.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppb_image_data_proxy.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"

using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_VideoSource_Private_API;

namespace ppapi {
namespace proxy {

VideoSourceResource::VideoSourceResource(
    Connection connection,
    PP_Instance instance)
    : PluginResource(connection, instance),
      is_open_(false) {
  SendCreate(RENDERER, PpapiHostMsg_VideoSource_Create());
}

VideoSourceResource::~VideoSourceResource() {
}

PPB_VideoSource_Private_API*
    VideoSourceResource::AsPPB_VideoSource_Private_API() {
  return this;
}

int32_t VideoSourceResource::Open(
    const PP_Var& stream_url,
    scoped_refptr<TrackedCallback> callback) {
  if (TrackedCallback::IsPending(open_callback_))
    return PP_ERROR_INPROGRESS;

  open_callback_ = callback;

  scoped_refptr<StringVar> stream_url_var = StringVar::FromPPVar(stream_url);
  const uint32_t kMaxStreamIdSizeInBytes = 16384;
  if (!stream_url_var.get() ||
      stream_url_var->value().size() > kMaxStreamIdSizeInBytes)
    return PP_ERROR_BADARGUMENT;
  Call<PpapiPluginMsg_VideoSource_OpenReply>(RENDERER,
      PpapiHostMsg_VideoSource_Open(stream_url_var->value()),
      base::Bind(&VideoSourceResource::OnPluginMsgOpenComplete, this));
  return PP_OK_COMPLETIONPENDING;
}

int32_t VideoSourceResource::GetFrame(
    PP_VideoFrame_Private* frame,
    scoped_refptr<TrackedCallback> callback) {
  if (!is_open_)
    return PP_ERROR_FAILED;

  if (TrackedCallback::IsPending(get_frame_callback_))
    return PP_ERROR_INPROGRESS;

  get_frame_callback_ = callback;

  Call<PpapiPluginMsg_VideoSource_GetFrameReply>(RENDERER,
      PpapiHostMsg_VideoSource_GetFrame(),
      base::Bind(&VideoSourceResource::OnPluginMsgGetFrameComplete, this,
                 frame));
  return PP_OK_COMPLETIONPENDING;
}

void VideoSourceResource::Close() {
  Post(RENDERER, PpapiHostMsg_VideoSource_Close());

  if (TrackedCallback::IsPending(open_callback_))
    open_callback_->PostAbort();
  if (TrackedCallback::IsPending(get_frame_callback_))
    get_frame_callback_->PostAbort();
}

void VideoSourceResource::OnPluginMsgOpenComplete(
    const ResourceMessageReplyParams& reply_params) {
  if (TrackedCallback::IsPending(open_callback_)) {
    int32_t result = reply_params.result();
    if (result == PP_OK)
      is_open_ = true;
    open_callback_->Run(result);
  }
}

void VideoSourceResource::OnPluginMsgGetFrameComplete(
    PP_VideoFrame_Private* frame,
    const ResourceMessageReplyParams& reply_params,
    const HostResource& image_data,
    const PP_ImageDataDesc& image_desc,
    int fd,
    PP_TimeTicks timestamp) {
  // The callback may have been aborted by Close().
  if (TrackedCallback::IsPending(get_frame_callback_)) {
    int32_t result = reply_params.result();
    if (result == PP_OK &&
        PPB_ImageData_Shared::IsImageDataDescValid(image_desc)) {
      frame->timestamp = timestamp;

#if defined(OS_ANDROID)
      frame->image_data = 0;
#elif defined(TOOLKIT_GTK)
      frame->image_data =
          (new PlatformImageData(image_data, image_desc, fd))->GetReference();
#elif defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
      base::SharedMemoryHandle handle;
      if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &handle))
        frame->image_data = 0;
      frame->image_data =
          (new PlatformImageData(
              image_data, image_desc, handle))->GetReference();
#else
#error Not implemented.
#endif
    }
    get_frame_callback_->Run(result);
  }
}

}  // namespace proxy
}  // namespace ppapi