blob: 3f5cdc418530b6f915656d7511799c2f8bfc8ef9 [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"
[email protected]1b2ec22e2011-08-30 00:48:3313#include "ppapi/proxy/ppb_graphics_3d_proxy.h"
[email protected]7ace8ad2011-08-06 03:23:5814#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;
[email protected]1b2ec22e2011-08-30 00:48:3320using ppapi::thunk::PPB_Graphics3D_API;
[email protected]7ace8ad2011-08-06 03:23:5821using ppapi::thunk::PPB_VideoDecoder_API;
22
[email protected]4d2efd22011-08-18 21:58:0223namespace ppapi {
[email protected]7ace8ad2011-08-06 03:23:5824namespace proxy {
25
[email protected]614888b2012-01-05 06:18:1226class VideoDecoder : public PPB_VideoDecoder_Shared {
[email protected]7ace8ad2011-08-06 03:23:5827 public:
[email protected]7f8b26b2011-08-18 15:41:0128 // You must call Init() before using this class.
29 explicit VideoDecoder(const HostResource& resource);
[email protected]7ace8ad2011-08-06 03:23:5830 virtual ~VideoDecoder();
31
32 static VideoDecoder* Create(const HostResource& resource,
[email protected]1b2ec22e2011-08-30 00:48:3333 PP_Resource graphics_context,
[email protected]2ffc31a2011-09-01 03:18:2834 PP_VideoDecoder_Profile profile);
[email protected]7ace8ad2011-08-06 03:23:5835
[email protected]7ace8ad2011-08-06 03:23:5836 // PPB_VideoDecoder_API implementation.
37 virtual int32_t Decode(const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
38 PP_CompletionCallback callback) OVERRIDE;
39 virtual void AssignPictureBuffers(
40 uint32_t no_of_buffers, const PP_PictureBuffer_Dev* buffers) OVERRIDE;
41 virtual void ReusePictureBuffer(int32_t picture_buffer_id) OVERRIDE;
42 virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE;
43 virtual int32_t Reset(PP_CompletionCallback callback) OVERRIDE;
44 virtual void Destroy() OVERRIDE;
45
[email protected]7ace8ad2011-08-06 03:23:5846 private:
47 friend class PPB_VideoDecoder_Proxy;
[email protected]7f8b26b2011-08-18 15:41:0148
49 PluginDispatcher* GetDispatcher() const;
[email protected]7ace8ad2011-08-06 03:23:5850
51 // Run the callbacks that were passed into the plugin interface.
52 void FlushACK(int32_t result);
53 void ResetACK(int32_t result);
54 void EndOfBitstreamACK(int32_t buffer_id, int32_t result);
55
56 DISALLOW_COPY_AND_ASSIGN(VideoDecoder);
57};
58
[email protected]614888b2012-01-05 06:18:1259VideoDecoder::VideoDecoder(const HostResource& decoder)
60 : PPB_VideoDecoder_Shared(decoder) {
[email protected]7ace8ad2011-08-06 03:23:5861}
62
63VideoDecoder::~VideoDecoder() {
64}
65
[email protected]7ace8ad2011-08-06 03:23:5866int32_t VideoDecoder::Decode(
67 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
68 PP_CompletionCallback callback) {
[email protected]4d2efd22011-08-18 21:58:0269 EnterResourceNoLock<PPB_Buffer_API>
[email protected]7ace8ad2011-08-06 03:23:5870 enter_buffer(bitstream_buffer->data, true);
71 if (enter_buffer.failed())
72 return PP_ERROR_BADRESOURCE;
73
[email protected]d527cdd82011-08-12 22:50:1974 if (!SetBitstreamBufferCallback(bitstream_buffer->id, callback))
75 return PP_ERROR_BADARGUMENT;
[email protected]7ace8ad2011-08-06 03:23:5876
77 Buffer* ppb_buffer =
78 static_cast<Buffer*>(enter_buffer.object());
79 HostResource host_buffer = ppb_buffer->host_resource();
80
81 FlushCommandBuffer();
82 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Decode(
[email protected]ac4b54d2011-10-20 23:09:2883 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(),
[email protected]7ace8ad2011-08-06 03:23:5884 host_buffer, bitstream_buffer->id,
85 bitstream_buffer->size));
86 return PP_OK_COMPLETIONPENDING;
87}
88
89void VideoDecoder::AssignPictureBuffers(uint32_t no_of_buffers,
90 const PP_PictureBuffer_Dev* buffers) {
91 std::vector<PP_PictureBuffer_Dev> buffer_list(
92 buffers, buffers + no_of_buffers);
93 FlushCommandBuffer();
94 GetDispatcher()->Send(
95 new PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers(
[email protected]ac4b54d2011-10-20 23:09:2896 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), buffer_list));
[email protected]7ace8ad2011-08-06 03:23:5897}
98
99void VideoDecoder::ReusePictureBuffer(int32_t picture_buffer_id) {
100 FlushCommandBuffer();
101 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer(
[email protected]ac4b54d2011-10-20 23:09:28102 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), picture_buffer_id));
[email protected]7ace8ad2011-08-06 03:23:58103}
104
105int32_t VideoDecoder::Flush(PP_CompletionCallback callback) {
[email protected]d527cdd82011-08-12 22:50:19106 if (!SetFlushCallback(callback))
107 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58108
109 FlushCommandBuffer();
110 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Flush(
[email protected]ac4b54d2011-10-20 23:09:28111 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58112 return PP_OK_COMPLETIONPENDING;
113}
114
115int32_t VideoDecoder::Reset(PP_CompletionCallback callback) {
[email protected]d527cdd82011-08-12 22:50:19116 if (!SetResetCallback(callback))
117 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58118
119 FlushCommandBuffer();
120 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Reset(
[email protected]ac4b54d2011-10-20 23:09:28121 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58122 return PP_OK_COMPLETIONPENDING;
123}
124
125void VideoDecoder::Destroy() {
126 FlushCommandBuffer();
127 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Destroy(
[email protected]ac4b54d2011-10-20 23:09:28128 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]9a578392011-12-07 18:59:27129 PPB_VideoDecoder_Shared::Destroy();
[email protected]7ace8ad2011-08-06 03:23:58130}
131
[email protected]7f8b26b2011-08-18 15:41:01132PluginDispatcher* VideoDecoder::GetDispatcher() const {
133 return PluginDispatcher::GetForResource(this);
[email protected]7ace8ad2011-08-06 03:23:58134}
135
136void VideoDecoder::ResetACK(int32_t result) {
137 RunResetCallback(result);
138}
139
140void VideoDecoder::FlushACK(int32_t result) {
141 RunFlushCallback(result);
142}
143
144void VideoDecoder::EndOfBitstreamACK(
145 int32_t bitstream_buffer_id, int32_t result) {
146 RunBitstreamBufferCallback(bitstream_buffer_id, result);
147}
148
[email protected]5c966022011-09-13 18:09:37149PPB_VideoDecoder_Proxy::PPB_VideoDecoder_Proxy(Dispatcher* dispatcher)
150 : InterfaceProxy(dispatcher),
[email protected]7ace8ad2011-08-06 03:23:58151 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
152}
153
154PPB_VideoDecoder_Proxy::~PPB_VideoDecoder_Proxy() {
155}
156
[email protected]7ace8ad2011-08-06 03:23:58157bool PPB_VideoDecoder_Proxy::OnMessageReceived(const IPC::Message& msg) {
158 bool handled = true;
159 IPC_BEGIN_MESSAGE_MAP(PPB_VideoDecoder_Proxy, msg)
160 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Create,
161 OnMsgCreate)
162 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Decode, OnMsgDecode)
163 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers,
164 OnMsgAssignPictureBuffers)
165 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer,
166 OnMsgReusePictureBuffer)
167 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Flush, OnMsgFlush)
168 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Reset, OnMsgReset)
169 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Destroy, OnMsgDestroy)
170 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_ResetACK, OnMsgResetACK)
171 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK,
172 OnMsgEndOfBitstreamACK)
173 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_FlushACK, OnMsgFlushACK)
174 IPC_MESSAGE_UNHANDLED(handled = false)
175 IPC_END_MESSAGE_MAP()
176 DCHECK(handled);
177 return handled;
178}
179
180PP_Resource PPB_VideoDecoder_Proxy::CreateProxyResource(
[email protected]1b2ec22e2011-08-30 00:48:33181 PP_Instance instance,
182 PP_Resource graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28183 PP_VideoDecoder_Profile profile) {
[email protected]7ace8ad2011-08-06 03:23:58184 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
185 // Dispatcher is null if it cannot find the instance passed to it (i.e. if the
186 // client passes in an invalid instance).
187 if (!dispatcher)
188 return 0;
189
[email protected]88cc78182011-12-01 06:47:09190 EnterResourceNoLock<PPB_Graphics3D_API> enter_context(graphics_context,
191 true);
192 if (enter_context.failed())
193 return 0;
[email protected]1b2ec22e2011-08-30 00:48:33194
[email protected]88cc78182011-12-01 06:47:09195 Graphics3D* context = static_cast<Graphics3D*>(enter_context.object());
[email protected]7ace8ad2011-08-06 03:23:58196
197 HostResource result;
198 dispatcher->Send(new PpapiHostMsg_PPBVideoDecoder_Create(
[email protected]ac4b54d2011-10-20 23:09:28199 API_ID_PPB_VIDEO_DECODER_DEV, instance,
[email protected]88cc78182011-12-01 06:47:09200 context->host_resource(), profile, &result));
[email protected]7ace8ad2011-08-06 03:23:58201 if (result.is_null())
202 return 0;
203
[email protected]7f8b26b2011-08-18 15:41:01204 // Need a scoped_refptr to keep the object alive during the Init call.
205 scoped_refptr<VideoDecoder> decoder(new VideoDecoder(result));
[email protected]88cc78182011-12-01 06:47:09206 decoder->InitCommon(graphics_context, context->gles2_impl());
[email protected]7f8b26b2011-08-18 15:41:01207 return decoder->GetReference();
[email protected]7ace8ad2011-08-06 03:23:58208}
209
210void PPB_VideoDecoder_Proxy::OnMsgCreate(
[email protected]1b2ec22e2011-08-30 00:48:33211 PP_Instance instance, const HostResource& graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28212 PP_VideoDecoder_Profile profile,
[email protected]7ace8ad2011-08-06 03:23:58213 HostResource* result) {
[email protected]5c966022011-09-13 18:09:37214 thunk::EnterResourceCreation resource_creation(instance);
[email protected]7ace8ad2011-08-06 03:23:58215 if (resource_creation.failed())
216 return;
217
[email protected]7ace8ad2011-08-06 03:23:58218 // Make the resource and get the API pointer to its interface.
219 result->SetHostResource(
220 instance, resource_creation.functions()->CreateVideoDecoder(
[email protected]2ffc31a2011-09-01 03:18:28221 instance, graphics_context.host_resource(), profile));
[email protected]7ace8ad2011-08-06 03:23:58222}
223
224void PPB_VideoDecoder_Proxy::OnMsgDecode(
225 const HostResource& decoder,
226 const HostResource& buffer, int32 id, int32 size) {
[email protected]5c966022011-09-13 18:09:37227 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
228 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58229 &PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin, decoder, id);
[email protected]5c966022011-09-13 18:09:37230 if (enter.failed())
231 return;
[email protected]7ace8ad2011-08-06 03:23:58232 PP_VideoBitstreamBuffer_Dev bitstream = { id, buffer.host_resource(), size };
[email protected]5c966022011-09-13 18:09:37233 enter.SetResult(enter.object()->Decode(&bitstream, enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58234}
235
236void PPB_VideoDecoder_Proxy::OnMsgAssignPictureBuffers(
237 const HostResource& decoder,
238 const std::vector<PP_PictureBuffer_Dev>& buffers) {
[email protected]5c966022011-09-13 18:09:37239 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
240 if (enter.succeeded() && !buffers.empty()) {
241 const PP_PictureBuffer_Dev* buffer_array = &buffers.front();
242 enter.object()->AssignPictureBuffers(buffers.size(), buffer_array);
243 }
[email protected]7ace8ad2011-08-06 03:23:58244}
245
246void PPB_VideoDecoder_Proxy::OnMsgReusePictureBuffer(
247 const HostResource& decoder, int32 picture_buffer_id) {
[email protected]5c966022011-09-13 18:09:37248 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
249 if (enter.succeeded())
250 enter.object()->ReusePictureBuffer(picture_buffer_id);
[email protected]7ace8ad2011-08-06 03:23:58251}
252
253void PPB_VideoDecoder_Proxy::OnMsgFlush(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37254 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
255 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58256 &PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37257 if (enter.succeeded())
258 enter.SetResult(enter.object()->Flush(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58259}
260
261void PPB_VideoDecoder_Proxy::OnMsgReset(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37262 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
263 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58264 &PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37265 if (enter.succeeded())
266 enter.SetResult(enter.object()->Reset(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58267}
268
269void PPB_VideoDecoder_Proxy::OnMsgDestroy(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37270 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
271 if (enter.succeeded())
272 enter.object()->Destroy();
[email protected]7ace8ad2011-08-06 03:23:58273}
274
275void PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin(
276 int32_t result, const HostResource& decoder, int32 id) {
277 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK(
[email protected]ac4b54d2011-10-20 23:09:28278 API_ID_PPB_VIDEO_DECODER_DEV, decoder, id, result));
[email protected]7ace8ad2011-08-06 03:23:58279}
280
281void PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin(
282 int32_t result, const HostResource& decoder) {
283 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_FlushACK(
[email protected]ac4b54d2011-10-20 23:09:28284 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58285}
286
287void PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin(
288 int32_t result, const HostResource& decoder) {
289 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_ResetACK(
[email protected]ac4b54d2011-10-20 23:09:28290 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58291}
292
293void PPB_VideoDecoder_Proxy::OnMsgEndOfBitstreamACK(
294 const HostResource& decoder, int32_t id, int32_t result) {
295 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
296 if (enter.succeeded())
297 static_cast<VideoDecoder*>(enter.object())->EndOfBitstreamACK(id, result);
298}
299
300void PPB_VideoDecoder_Proxy::OnMsgFlushACK(
301 const HostResource& decoder, int32_t result) {
302 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
303 if (enter.succeeded())
304 static_cast<VideoDecoder*>(enter.object())->FlushACK(result);
305}
306
307void PPB_VideoDecoder_Proxy::OnMsgResetACK(
308 const HostResource& decoder, int32_t result) {
309 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
310 if (enter.succeeded())
311 static_cast<VideoDecoder*>(enter.object())->ResetACK(result);
312}
313
314} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02315} // namespace ppapi