Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: ppapi/proxy/video_capture_resource.cc

Issue 11274036: Refactor video capture to new design (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "ppapi/proxy/video_capture_resource.h"
6
7 #include "ppapi/c/dev/ppp_video_capture_dev.h"
8 #include "ppapi/proxy/dispatch_reply_message.h"
9 #include "ppapi/proxy/plugin_dispatcher.h"
10 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/proxy/ppb_buffer_proxy.h"
12 #include "ppapi/proxy/resource_message_params.h"
13 #include "ppapi/shared_impl/ppb_video_capture_shared.h"
14 #include "ppapi/shared_impl/tracked_callback.h"
15
16 namespace ppapi {
17 namespace proxy {
18
19 VideoCaptureResource::VideoCaptureResource(
20 Connection connection,
21 PP_Instance instance,
22 PluginDispatcher* dispatcher)
23 : PluginResource(connection, instance),
24 PPB_VideoCapture_Shared(),
25 devices_(NULL) {
26 if (!sent_create_to_renderer())
27 SendCreate(RENDERER, PpapiHostMsg_VideoCapture_Create());
28
29 ppp_video_capture_impl_ = static_cast<const PPP_VideoCapture_Dev*>(
30 dispatcher->local_get_interface()(PPP_VIDEO_CAPTURE_DEV_INTERFACE));
31 }
32
33 VideoCaptureResource::~VideoCaptureResource() {
34 }
35
36 void VideoCaptureResource::OnReplyReceived(
37 const ResourceMessageReplyParams& params,
38 const IPC::Message& msg) {
39 if (params.sequence()) {
40 PluginResource::OnReplyReceived(params, msg);
41 return;
42 }
43
44 IPC_BEGIN_MESSAGE_MAP(VideoCaptureResource, msg)
45 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
46 PpapiPluginMsg_VideoCapture_OnDeviceInfo,
47 OnDeviceInfo)
48 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
49 PpapiPluginMsg_VideoCapture_OnStatus,
50 OnStatus)
51 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
52 PpapiPluginMsg_VideoCapture_OnError,
53 OnError)
54 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
55 PpapiPluginMsg_VideoCapture_OnBufferReady,
56 OnBufferReady)
57 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(NOTREACHED())
58 IPC_END_MESSAGE_MAP()
59 }
60
61 int32_t VideoCaptureResource::EnumerateDevices(
62 PP_Resource* devices,
63 scoped_refptr<TrackedCallback> callback) {
64 if (TrackedCallback::IsPending(enumerate_devices_callback_))
65 return PP_ERROR_INPROGRESS;
66
67 devices_ = devices;
68 enumerate_devices_callback_ = callback;
69
70 Call<PpapiPluginMsg_VideoCapture_EnumerateDevicesResponse>(
71 RENDERER,
72 PpapiHostMsg_VideoCapture_EnumerateDevices(),
73 base::Bind(&VideoCaptureResource::OnEnumerateDevicesComplete, this));
74 return PP_OK_COMPLETIONPENDING;
75 }
76
77 int32_t VideoCaptureResource::Open(
78 const std::string& device_id,
79 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
80 uint32_t buffer_count,
81 scoped_refptr<TrackedCallback> callback) {
82 if (open_state_ != BEFORE_OPEN)
83 return PP_ERROR_FAILED;
84
85 if (TrackedCallback::IsPending(open_callback_))
86 return PP_ERROR_INPROGRESS;
87
88 open_callback_ = callback;
89
90 Call<PpapiPluginMsg_VideoCapture_OpenACK>(
91 RENDERER,
92 PpapiHostMsg_VideoCapture_Open(device_id, requested_info, buffer_count),
93 base::Bind(&VideoCaptureResource::OnOpenComplete, this));
94 return PP_OK_COMPLETIONPENDING;
95 }
96
97 int32_t VideoCaptureResource::StartCapture() {
98 return PPB_VideoCapture_Shared::StartCapture();
99 }
100
101 int32_t VideoCaptureResource::ReuseBuffer(uint32_t buffer) {
102 if (buffer >= buffer_in_use_.size() || !buffer_in_use_[buffer])
103 return PP_ERROR_BADARGUMENT;
104 Post(RENDERER, PpapiHostMsg_VideoCapture_ReuseBuffer(buffer));
105 return PP_OK;
106 }
107
108 int32_t VideoCaptureResource::StopCapture() {
109 return PPB_VideoCapture_Shared::StopCapture();
110 }
111
112 void VideoCaptureResource::Close() {
113 Post(RENDERER, PpapiHostMsg_VideoCapture_Close());
114
115 if (TrackedCallback::IsPending(open_callback_))
116 open_callback_->PostAbort();
117 }
118
119 int32_t VideoCaptureResource::StartCapture0_1(
120 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
121 uint32_t buffer_count) {
122 return PPB_VideoCapture_Shared::StartCapture0_1(requested_info,
123 buffer_count);
124 }
125
126 void VideoCaptureResource::OnDeviceInfo(
127 const ResourceMessageReplyParams& params,
128 const struct PP_VideoCaptureDeviceInfo_Dev& info,
129 const std::vector<HostResource>& buffers,
130 uint32_t buffer_size) {
131 if (!ppp_video_capture_impl_)
132 return;
133
134 std::vector<base::SharedMemoryHandle> handles;
135 params.GetAllSharedMemoryHandles(&handles);
136 DCHECK(handles.size() == buffers.size());
137
138 scoped_array<PP_Resource> resources(new PP_Resource[buffers.size()]);
139 for (size_t i = 0; i < buffers.size(); ++i) {
140 resources[i] = ppapi::proxy::PPB_Buffer_Proxy::AddProxyResource(
141 buffers[i], handles[i], buffer_size);
142 }
143
144 buffer_in_use_ = std::vector<bool>(buffers.size());
145
146 ppp_video_capture_impl_->OnDeviceInfo(
147 pp_instance(),
148 pp_resource(),
149 &info,
150 buffers.size(),
151 resources.get());
152 }
153
154 void VideoCaptureResource::OnStatus(
155 const ResourceMessageReplyParams& params,
156 uint32_t status) {
157 if (ppp_video_capture_impl_)
158 ppp_video_capture_impl_->OnStatus(pp_instance(), pp_resource(), status);
159 }
160
161 void VideoCaptureResource::OnError(
162 const ResourceMessageReplyParams& params,
163 uint32_t error_code) {
164 if (ppp_video_capture_impl_)
165 ppp_video_capture_impl_->OnError(pp_instance(), pp_resource(), error_code);
166 }
167
168 void VideoCaptureResource::OnBufferReady(
169 const ResourceMessageReplyParams& params,
170 uint32_t buffer) {
171 SetBufferInUse(buffer);
172 if (ppp_video_capture_impl_) {
173 ppp_video_capture_impl_->OnBufferReady(pp_instance(), pp_resource(),
174 buffer);
175 }
176 }
177
178 int32_t VideoCaptureResource::InternalStartCapture() {
179 buffer_in_use_.clear();
180 Post(RENDERER, PpapiHostMsg_VideoCapture_StartCapture());
181 return PP_OK;
182 }
183
184 int32_t VideoCaptureResource::InternalStopCapture() {
185 Post(RENDERER, PpapiHostMsg_VideoCapture_StopCapture());
186 return PP_OK;
187 }
188
189 int32_t VideoCaptureResource::InternalStartCapture0_1(
190 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
191 uint32_t buffer_count) {
192 if (TrackedCallback::IsPending(open_callback_))
193 return PP_ERROR_FAILED;
194
195 buffer_in_use_.clear();
196 Post(RENDERER, PpapiHostMsg_VideoCapture_StartCapture0_1(requested_info,
197 buffer_count));
198 return PP_OK;
199 }
200
201 void VideoCaptureResource::OnOpenComplete(
202 const ResourceMessageReplyParams& params,
203 int32_t result) {
204 if (open_state_ == BEFORE_OPEN && result == PP_OK)
205 open_state_ = OPENED;
206
207 // The callback may have been aborted by Close(), or the open operation is
208 // completed synchronously.
209 if (TrackedCallback::IsPending(open_callback_))
210 TrackedCallback::ClearAndRun(&open_callback_, result);
211 }
212
213 void VideoCaptureResource::OnEnumerateDevicesComplete(
214 const ResourceMessageReplyParams& params,
215 int32_t result,
216 const std::vector<DeviceRefData>& devices) {
217 DCHECK(TrackedCallback::IsPending(enumerate_devices_callback_));
218
219 if (result == PP_OK && devices_) {
220 devices_data_ = devices;
221
222 // devices_ points to the resource array of PP_ArrayOutput allocated in
223 // VideoCapture_Dev.
224 *devices_ = PPB_DeviceRef_Shared::CreateResourceArray(
225 OBJECT_IS_PROXY, pp_instance(), devices);
226 }
227 devices_ = NULL;
228
229 TrackedCallback::ClearAndRun(&enumerate_devices_callback_, result);
230 }
231
232 void VideoCaptureResource::SetBufferInUse(uint32_t buffer_index) {
233 DCHECK(buffer_index < buffer_in_use_.size());
234 buffer_in_use_[buffer_index] = true;
235 }
236
237 } // namespace proxy
238 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698