blob: 97c23488d3ea56db554103ce59e23a737fd09201 [file] [log] [blame]
[email protected]7ace8ad2011-08-06 03:23:581// Copyright (c) 2011 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/ppb_video_decoder_proxy.h"
6
7#include "base/logging.h"
8#include "gpu/command_buffer/client/gles2_implementation.h"
9#include "ppapi/proxy/enter_proxy.h"
10#include "ppapi/proxy/plugin_dispatcher.h"
11#include "ppapi/proxy/ppapi_messages.h"
12#include "ppapi/proxy/ppb_buffer_proxy.h"
13#include "ppapi/proxy/ppb_context_3d_proxy.h"
14#include "ppapi/thunk/enter.h"
15#include "ppapi/thunk/resource_creation_api.h"
16#include "ppapi/thunk/thunk.h"
17
18using ppapi::thunk::EnterResourceNoLock;
19using ppapi::thunk::PPB_Buffer_API;
20using ppapi::thunk::PPB_Context3D_API;
21using ppapi::thunk::PPB_VideoDecoder_API;
22
23namespace pp {
24namespace proxy {
25
26class VideoDecoder : public PluginResource,
27 public ::ppapi::VideoDecoderImpl {
28 public:
29 virtual ~VideoDecoder();
30
31 static VideoDecoder* Create(const HostResource& resource,
32 PP_Resource context3d_id,
33 const PP_VideoConfigElement* config);
34
35 // ResourceObjectBase overrides.
36 virtual PPB_VideoDecoder_API* AsPPB_VideoDecoder_API() OVERRIDE;
37
38 // PPB_VideoDecoder_API implementation.
39 virtual int32_t Decode(const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
40 PP_CompletionCallback callback) OVERRIDE;
41 virtual void AssignPictureBuffers(
42 uint32_t no_of_buffers, const PP_PictureBuffer_Dev* buffers) OVERRIDE;
43 virtual void ReusePictureBuffer(int32_t picture_buffer_id) OVERRIDE;
44 virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE;
45 virtual int32_t Reset(PP_CompletionCallback callback) OVERRIDE;
46 virtual void Destroy() OVERRIDE;
47
48 protected:
49 virtual void AddRefResource(PP_Resource resource) OVERRIDE;
50 virtual void UnrefResource(PP_Resource resource) OVERRIDE;
51
52 private:
53 friend class PPB_VideoDecoder_Proxy;
54 explicit VideoDecoder(const HostResource& resource);
55
56 // Run the callbacks that were passed into the plugin interface.
57 void FlushACK(int32_t result);
58 void ResetACK(int32_t result);
59 void EndOfBitstreamACK(int32_t buffer_id, int32_t result);
60
61 DISALLOW_COPY_AND_ASSIGN(VideoDecoder);
62};
63
64VideoDecoder::VideoDecoder(const HostResource& decoder)
65 : PluginResource(decoder) {
66}
67
68VideoDecoder* VideoDecoder::Create(const HostResource& resource,
69 PP_Resource context3d_id,
70 const PP_VideoConfigElement* config) {
71 if (!context3d_id)
72 return NULL;
73
74 EnterResourceNoLock<PPB_Context3D_API> enter_context(context3d_id, true);
75 if (enter_context.failed())
76 return NULL;
77
78 scoped_ptr<VideoDecoder> decoder(new VideoDecoder(resource));
79 if (decoder->Init(context3d_id, enter_context.object(), config))
80 return decoder.release();
81 return NULL;
82}
83
84VideoDecoder::~VideoDecoder() {
85}
86
87::ppapi::thunk::PPB_VideoDecoder_API* VideoDecoder::AsPPB_VideoDecoder_API() {
88 return this;
89}
90
91int32_t VideoDecoder::Decode(
92 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
93 PP_CompletionCallback callback) {
94 ppapi::thunk::EnterResourceNoLock<PPB_Buffer_API>
95 enter_buffer(bitstream_buffer->data, true);
96 if (enter_buffer.failed())
97 return PP_ERROR_BADRESOURCE;
98
99 SetBitstreamBufferCallback(bitstream_buffer->id, callback);
100
101 Buffer* ppb_buffer =
102 static_cast<Buffer*>(enter_buffer.object());
103 HostResource host_buffer = ppb_buffer->host_resource();
104
105 FlushCommandBuffer();
106 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Decode(
107 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource(),
108 host_buffer, bitstream_buffer->id,
109 bitstream_buffer->size));
110 return PP_OK_COMPLETIONPENDING;
111}
112
113void VideoDecoder::AssignPictureBuffers(uint32_t no_of_buffers,
114 const PP_PictureBuffer_Dev* buffers) {
115 std::vector<PP_PictureBuffer_Dev> buffer_list(
116 buffers, buffers + no_of_buffers);
117 FlushCommandBuffer();
118 GetDispatcher()->Send(
119 new PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers(
120 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource(), buffer_list));
121}
122
123void VideoDecoder::ReusePictureBuffer(int32_t picture_buffer_id) {
124 FlushCommandBuffer();
125 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer(
126 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource(), picture_buffer_id));
127}
128
129int32_t VideoDecoder::Flush(PP_CompletionCallback callback) {
130 SetFlushCallback(callback);
131
132 FlushCommandBuffer();
133 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Flush(
134 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
135 return PP_OK_COMPLETIONPENDING;
136}
137
138int32_t VideoDecoder::Reset(PP_CompletionCallback callback) {
139 SetResetCallback(callback);
140
141 FlushCommandBuffer();
142 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Reset(
143 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
144 return PP_OK_COMPLETIONPENDING;
145}
146
147void VideoDecoder::Destroy() {
148 FlushCommandBuffer();
149 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Destroy(
150 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
151 ::ppapi::VideoDecoderImpl::Destroy();
152}
153
154void VideoDecoder::AddRefResource(PP_Resource resource) {
155 PluginResourceTracker::GetInstance()->AddRefResource(resource);
156}
157
158void VideoDecoder::UnrefResource(PP_Resource resource) {
159 PluginResourceTracker::GetInstance()->ReleaseResource(resource);
160}
161
162void VideoDecoder::ResetACK(int32_t result) {
163 RunResetCallback(result);
164}
165
166void VideoDecoder::FlushACK(int32_t result) {
167 RunFlushCallback(result);
168}
169
170void VideoDecoder::EndOfBitstreamACK(
171 int32_t bitstream_buffer_id, int32_t result) {
172 RunBitstreamBufferCallback(bitstream_buffer_id, result);
173}
174
175namespace {
176
177InterfaceProxy* CreateVideoDecoderProxy(Dispatcher* dispatcher,
178 const void* target_interface) {
179 return new PPB_VideoDecoder_Proxy(dispatcher, target_interface);
180}
181
182} // namespace
183
184PPB_VideoDecoder_Proxy::PPB_VideoDecoder_Proxy(Dispatcher* dispatcher,
185 const void* target_interface)
186 : InterfaceProxy(dispatcher, target_interface),
187 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
188}
189
190PPB_VideoDecoder_Proxy::~PPB_VideoDecoder_Proxy() {
191}
192
193// static
194const InterfaceProxy::Info* PPB_VideoDecoder_Proxy::GetInfo() {
195 static const Info info = {
196 ::ppapi::thunk::GetPPB_VideoDecoder_Thunk(),
197 PPB_VIDEODECODER_DEV_INTERFACE,
198 INTERFACE_ID_PPB_VIDEO_DECODER_DEV,
199 false,
200 &CreateVideoDecoderProxy,
201 };
202 return &info;
203}
204
205bool PPB_VideoDecoder_Proxy::OnMessageReceived(const IPC::Message& msg) {
206 bool handled = true;
207 IPC_BEGIN_MESSAGE_MAP(PPB_VideoDecoder_Proxy, msg)
208 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Create,
209 OnMsgCreate)
210 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Decode, OnMsgDecode)
211 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers,
212 OnMsgAssignPictureBuffers)
213 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer,
214 OnMsgReusePictureBuffer)
215 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Flush, OnMsgFlush)
216 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Reset, OnMsgReset)
217 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Destroy, OnMsgDestroy)
218 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_ResetACK, OnMsgResetACK)
219 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK,
220 OnMsgEndOfBitstreamACK)
221 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_FlushACK, OnMsgFlushACK)
222 IPC_MESSAGE_UNHANDLED(handled = false)
223 IPC_END_MESSAGE_MAP()
224 DCHECK(handled);
225 return handled;
226}
227
228PP_Resource PPB_VideoDecoder_Proxy::CreateProxyResource(
229 PP_Instance instance, PP_Resource context3d_id,
230 const PP_VideoConfigElement* config) {
231 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
232 // Dispatcher is null if it cannot find the instance passed to it (i.e. if the
233 // client passes in an invalid instance).
234 if (!dispatcher)
235 return 0;
236
237 std::vector<PP_VideoConfigElement> copied;
238 if (!ppapi::VideoDecoderImpl::CopyConfigsToVector(config, &copied))
239 return 0;
240
241 ppapi::thunk::EnterResourceNoLock<PPB_Context3D_API>
242 enter_context(context3d_id, true);
243 if (enter_context.failed())
244 return 0;
245 Context3D* ppb_context =
246 static_cast<Context3D*>(enter_context.object());
247 HostResource host_context = ppb_context->host_resource();
248
249 HostResource result;
250 dispatcher->Send(new PpapiHostMsg_PPBVideoDecoder_Create(
251 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, instance,
252 host_context, copied, &result));
253 if (result.is_null())
254 return 0;
255
256 linked_ptr<VideoDecoder> video_decoder(
257 VideoDecoder::Create(result, context3d_id, config));
258
259 return PluginResourceTracker::GetInstance()->AddResource(video_decoder);
260}
261
262void PPB_VideoDecoder_Proxy::OnMsgCreate(
263 PP_Instance instance, const HostResource& context3d_id,
264 const std::vector<PP_VideoConfigElement>& config,
265 HostResource* result) {
266 ::ppapi::thunk::EnterFunction< ::ppapi::thunk::ResourceCreationAPI>
267 resource_creation(instance, true);
268 if (resource_creation.failed())
269 return;
270
271 std::vector<PP_VideoConfigElement> copied = config;
272 copied.push_back(PP_VIDEOATTR_DICTIONARY_TERMINATOR);
273
274 // Make the resource and get the API pointer to its interface.
275 result->SetHostResource(
276 instance, resource_creation.functions()->CreateVideoDecoder(
277 instance, context3d_id.host_resource(), &copied.front()));
278}
279
280void PPB_VideoDecoder_Proxy::OnMsgDecode(
281 const HostResource& decoder,
282 const HostResource& buffer, int32 id, int32 size) {
283 CompletionCallback callback = callback_factory_.NewRequiredCallback(
284 &PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin, decoder, id);
285
286 PP_VideoBitstreamBuffer_Dev bitstream = { id, buffer.host_resource(), size };
287 ppb_video_decoder_target()->Decode(
288 decoder.host_resource(), &bitstream, callback.pp_completion_callback());
289}
290
291void PPB_VideoDecoder_Proxy::OnMsgAssignPictureBuffers(
292 const HostResource& decoder,
293 const std::vector<PP_PictureBuffer_Dev>& buffers) {
294 DCHECK(!buffers.empty());
295 const PP_PictureBuffer_Dev* buffer_array = &buffers.front();
296
297 ppb_video_decoder_target()->AssignPictureBuffers(
298 decoder.host_resource(), buffers.size(), buffer_array);
299}
300
301void PPB_VideoDecoder_Proxy::OnMsgReusePictureBuffer(
302 const HostResource& decoder, int32 picture_buffer_id) {
303 ppb_video_decoder_target()->ReusePictureBuffer(
304 decoder.host_resource(), picture_buffer_id);
305}
306
307void PPB_VideoDecoder_Proxy::OnMsgFlush(const HostResource& decoder) {
308 CompletionCallback callback = callback_factory_.NewRequiredCallback(
309 &PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin, decoder);
310 ppb_video_decoder_target()->Flush(
311 decoder.host_resource(), callback.pp_completion_callback());
312}
313
314void PPB_VideoDecoder_Proxy::OnMsgReset(const HostResource& decoder) {
315 CompletionCallback callback = callback_factory_.NewRequiredCallback(
316 &PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin, decoder);
317 ppb_video_decoder_target()->Reset(
318 decoder.host_resource(), callback.pp_completion_callback());
319}
320
321void PPB_VideoDecoder_Proxy::OnMsgDestroy(const HostResource& decoder) {
322 ppb_video_decoder_target()->Destroy(decoder.host_resource());
323}
324
325void PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin(
326 int32_t result, const HostResource& decoder, int32 id) {
327 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK(
328 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, decoder, id, result));
329}
330
331void PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin(
332 int32_t result, const HostResource& decoder) {
333 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_FlushACK(
334 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
335}
336
337void PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin(
338 int32_t result, const HostResource& decoder) {
339 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_ResetACK(
340 INTERFACE_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
341}
342
343void PPB_VideoDecoder_Proxy::OnMsgEndOfBitstreamACK(
344 const HostResource& decoder, int32_t id, int32_t result) {
345 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
346 if (enter.succeeded())
347 static_cast<VideoDecoder*>(enter.object())->EndOfBitstreamACK(id, result);
348}
349
350void PPB_VideoDecoder_Proxy::OnMsgFlushACK(
351 const HostResource& decoder, int32_t result) {
352 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
353 if (enter.succeeded())
354 static_cast<VideoDecoder*>(enter.object())->FlushACK(result);
355}
356
357void PPB_VideoDecoder_Proxy::OnMsgResetACK(
358 const HostResource& decoder, int32_t result) {
359 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
360 if (enter.succeeded())
361 static_cast<VideoDecoder*>(enter.object())->ResetACK(result);
362}
363
364} // namespace proxy
365} // namespace pp