[go: nahoru, domu]

blob: 0091e160c3f1cdd9897ccacceb9a020577fd1aa8 [file] [log] [blame]
victorhsieh@chromium.orgcc123872012-11-16 07:53:081// 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 "ppapi/proxy/video_capture_resource.h"
6
avie029c4132015-12-23 06:45:227#include <stddef.h>
8
victorhsieh@chromium.orgcc123872012-11-16 07:53:089#include "ppapi/c/dev/ppp_video_capture_dev.h"
10#include "ppapi/proxy/dispatch_reply_message.h"
11#include "ppapi/proxy/plugin_dispatcher.h"
12#include "ppapi/proxy/plugin_globals.h"
13#include "ppapi/proxy/plugin_resource_tracker.h"
14#include "ppapi/proxy/ppapi_messages.h"
15#include "ppapi/proxy/ppb_buffer_proxy.h"
16#include "ppapi/proxy/resource_message_params.h"
victorhsieh@chromium.orgcc123872012-11-16 07:53:0817#include "ppapi/shared_impl/proxy_lock.h"
18#include "ppapi/shared_impl/tracked_callback.h"
19
20namespace ppapi {
21namespace proxy {
22
23VideoCaptureResource::VideoCaptureResource(
24 Connection connection,
25 PP_Instance instance,
26 PluginDispatcher* dispatcher)
27 : PluginResource(connection, instance),
28 open_state_(BEFORE_OPEN),
scherkus@chromium.orga2f53dc2013-04-30 01:06:3529 enumeration_helper_(this) {
victorhsieh@chromium.orgcc123872012-11-16 07:53:0830 SendCreate(RENDERER, PpapiHostMsg_VideoCapture_Create());
31
32 ppp_video_capture_impl_ = static_cast<const PPP_VideoCapture_Dev*>(
33 dispatcher->local_get_interface()(PPP_VIDEO_CAPTURE_DEV_INTERFACE));
34}
35
36VideoCaptureResource::~VideoCaptureResource() {
37}
38
39void VideoCaptureResource::OnReplyReceived(
40 const ResourceMessageReplyParams& params,
41 const IPC::Message& msg) {
yzshen@chromium.org33eccce2012-12-10 22:15:1042 if (enumeration_helper_.HandleReply(params, msg))
43 return;
44
victorhsieh@chromium.orgcc123872012-11-16 07:53:0845 if (params.sequence()) {
46 PluginResource::OnReplyReceived(params, msg);
47 return;
48 }
49
jam@chromium.orgdade5f82014-05-13 21:59:2150 PPAPI_BEGIN_MESSAGE_MAP(VideoCaptureResource, msg)
victorhsieh@chromium.orgcc123872012-11-16 07:53:0851 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
52 PpapiPluginMsg_VideoCapture_OnDeviceInfo,
53 OnPluginMsgOnDeviceInfo)
54 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
55 PpapiPluginMsg_VideoCapture_OnStatus,
56 OnPluginMsgOnStatus)
57 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
58 PpapiPluginMsg_VideoCapture_OnError,
59 OnPluginMsgOnError)
60 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
61 PpapiPluginMsg_VideoCapture_OnBufferReady,
62 OnPluginMsgOnBufferReady)
63 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(NOTREACHED())
jam@chromium.orgdade5f82014-05-13 21:59:2164 PPAPI_END_MESSAGE_MAP()
victorhsieh@chromium.orgcc123872012-11-16 07:53:0865}
66
yzshen@chromium.org33eccce2012-12-10 22:15:1067int32_t VideoCaptureResource::EnumerateDevices(
68 const PP_ArrayOutput& output,
69 scoped_refptr<TrackedCallback> callback) {
70 return enumeration_helper_.EnumerateDevices(output, callback);
71}
victorhsieh@chromium.orgcc123872012-11-16 07:53:0872
yzshen@chromium.org33eccce2012-12-10 22:15:1073int32_t VideoCaptureResource::MonitorDeviceChange(
74 PP_MonitorDeviceChangeCallback callback,
75 void* user_data) {
76 return enumeration_helper_.MonitorDeviceChange(callback, user_data);
victorhsieh@chromium.orgcc123872012-11-16 07:53:0877}
78
79int32_t VideoCaptureResource::Open(
80 const std::string& device_id,
81 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
82 uint32_t buffer_count,
83 scoped_refptr<TrackedCallback> callback) {
84 if (open_state_ != BEFORE_OPEN)
85 return PP_ERROR_FAILED;
86
87 if (TrackedCallback::IsPending(open_callback_))
88 return PP_ERROR_INPROGRESS;
89
90 open_callback_ = callback;
91
92 Call<PpapiPluginMsg_VideoCapture_OpenReply>(
93 RENDERER,
94 PpapiHostMsg_VideoCapture_Open(device_id, requested_info, buffer_count),
95 base::Bind(&VideoCaptureResource::OnPluginMsgOpenReply, this));
96 return PP_OK_COMPLETIONPENDING;
97}
98
99int32_t VideoCaptureResource::StartCapture() {
100 if (open_state_ != OPENED)
101 return PP_ERROR_FAILED;
102
victorhsieh@chromium.orgcc123872012-11-16 07:53:08103 Post(RENDERER, PpapiHostMsg_VideoCapture_StartCapture());
104 return PP_OK;
105}
106
107int32_t VideoCaptureResource::ReuseBuffer(uint32_t buffer) {
108 if (buffer >= buffer_in_use_.size() || !buffer_in_use_[buffer])
109 return PP_ERROR_BADARGUMENT;
110 Post(RENDERER, PpapiHostMsg_VideoCapture_ReuseBuffer(buffer));
111 return PP_OK;
112}
113
114int32_t VideoCaptureResource::StopCapture() {
115 if (open_state_ != OPENED)
116 return PP_ERROR_FAILED;
117
118 Post(RENDERER, PpapiHostMsg_VideoCapture_StopCapture());
119 return PP_OK;
120}
121
122void VideoCaptureResource::Close() {
123 if (open_state_ == CLOSED)
124 return;
125
126 Post(RENDERER, PpapiHostMsg_VideoCapture_Close());
127
128 open_state_ = CLOSED;
129
130 if (TrackedCallback::IsPending(open_callback_))
131 open_callback_->PostAbort();
132}
133
134int32_t VideoCaptureResource::EnumerateDevicesSync(
135 const PP_ArrayOutput& devices) {
yzshen@chromium.org33eccce2012-12-10 22:15:10136 return enumeration_helper_.EnumerateDevicesSync(devices);
137}
victorhsieh@chromium.orgcc123872012-11-16 07:53:08138
yzshen@chromium.org33eccce2012-12-10 22:15:10139void VideoCaptureResource::LastPluginRefWasDeleted() {
140 enumeration_helper_.LastPluginRefWasDeleted();
victorhsieh@chromium.orgcc123872012-11-16 07:53:08141}
142
143void VideoCaptureResource::OnPluginMsgOnDeviceInfo(
144 const ResourceMessageReplyParams& params,
145 const struct PP_VideoCaptureDeviceInfo_Dev& info,
146 const std::vector<HostResource>& buffers,
147 uint32_t buffer_size) {
148 if (!ppp_video_capture_impl_)
149 return;
150
151 std::vector<base::SharedMemoryHandle> handles;
152 params.TakeAllSharedMemoryHandles(&handles);
153 CHECK(handles.size() == buffers.size());
154
155 PluginResourceTracker* tracker =
156 PluginGlobals::Get()->plugin_resource_tracker();
dchengced92242016-04-07 00:00:12157 std::unique_ptr<PP_Resource[]> resources(new PP_Resource[buffers.size()]);
victorhsieh@chromium.orgcc123872012-11-16 07:53:08158 for (size_t i = 0; i < buffers.size(); ++i) {
159 // We assume that the browser created a new set of resources.
160 DCHECK(!tracker->PluginResourceForHostResource(buffers[i]));
161 resources[i] = ppapi::proxy::PPB_Buffer_Proxy::AddProxyResource(
162 buffers[i], handles[i], buffer_size);
163 }
164
165 buffer_in_use_ = std::vector<bool>(buffers.size());
166
167 CallWhileUnlocked(ppp_video_capture_impl_->OnDeviceInfo,
168 pp_instance(),
169 pp_resource(),
170 &info,
brettw669d47b12015-02-13 21:17:38171 static_cast<uint32_t>(buffers.size()),
dmichael36e91cd2014-09-11 16:57:30172 resources.get());
victorhsieh@chromium.orgcc123872012-11-16 07:53:08173
174 for (size_t i = 0; i < buffers.size(); ++i)
175 tracker->ReleaseResource(resources[i]);
176}
177
178void VideoCaptureResource::OnPluginMsgOnStatus(
179 const ResourceMessageReplyParams& params,
180 uint32_t status) {
181 switch (status) {
182 case PP_VIDEO_CAPTURE_STATUS_STARTING:
183 case PP_VIDEO_CAPTURE_STATUS_STOPPING:
184 // Those states are not sent by the browser.
185 NOTREACHED();
186 break;
187 }
188 if (ppp_video_capture_impl_) {
189 CallWhileUnlocked(ppp_video_capture_impl_->OnStatus,
190 pp_instance(),
191 pp_resource(),
192 status);
193 }
194}
195
196void VideoCaptureResource::OnPluginMsgOnError(
197 const ResourceMessageReplyParams& params,
198 uint32_t error_code) {
199 open_state_ = CLOSED;
200 if (ppp_video_capture_impl_) {
201 CallWhileUnlocked(ppp_video_capture_impl_->OnError,
202 pp_instance(),
203 pp_resource(),
204 error_code);
205 }
206}
207
208void VideoCaptureResource::OnPluginMsgOnBufferReady(
209 const ResourceMessageReplyParams& params,
210 uint32_t buffer) {
211 SetBufferInUse(buffer);
212 if (ppp_video_capture_impl_) {
213 CallWhileUnlocked(ppp_video_capture_impl_->OnBufferReady,
214 pp_instance(),
215 pp_resource(),
216 buffer);
217 }
218}
219
220void VideoCaptureResource::OnPluginMsgOpenReply(
221 const ResourceMessageReplyParams& params) {
222 if (open_state_ == BEFORE_OPEN && params.result() == PP_OK)
223 open_state_ = OPENED;
224
225 // The callback may have been aborted by Close().
226 if (TrackedCallback::IsPending(open_callback_))
227 open_callback_->Run(params.result());
228}
229
victorhsieh@chromium.orgcc123872012-11-16 07:53:08230void VideoCaptureResource::SetBufferInUse(uint32_t buffer_index) {
yzshen@chromium.org33eccce2012-12-10 22:15:10231 CHECK(buffer_index < buffer_in_use_.size());
victorhsieh@chromium.orgcc123872012-11-16 07:53:08232 buffer_in_use_[buffer_index] = true;
233}
234
235} // namespace proxy
236} // namespace ppapi