blob: f6eabd433d6947106d7b9bd49671f01ada80a250 [file] [log] [blame]
[email protected]aed96532012-06-23 14:27:421// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7ace8ad2011-08-06 03:23:582// 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,
[email protected]aed96532012-06-23 14:27:4238 scoped_refptr<TrackedCallback> callback) OVERRIDE;
[email protected]7ace8ad2011-08-06 03:23:5839 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;
[email protected]aed96532012-06-23 14:27:4242 virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) OVERRIDE;
43 virtual int32_t Reset(scoped_refptr<TrackedCallback> callback) OVERRIDE;
[email protected]7ace8ad2011-08-06 03:23:5844 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() {
[email protected]c1e77f1a2013-03-28 18:12:0464 FlushCommandBuffer();
65 PPB_VideoDecoder_Shared::Destroy();
[email protected]7ace8ad2011-08-06 03:23:5866}
67
[email protected]7ace8ad2011-08-06 03:23:5868int32_t VideoDecoder::Decode(
69 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
[email protected]aed96532012-06-23 14:27:4270 scoped_refptr<TrackedCallback> callback) {
[email protected]4d2efd22011-08-18 21:58:0271 EnterResourceNoLock<PPB_Buffer_API>
[email protected]7ace8ad2011-08-06 03:23:5872 enter_buffer(bitstream_buffer->data, true);
73 if (enter_buffer.failed())
74 return PP_ERROR_BADRESOURCE;
75
[email protected]d527cdd82011-08-12 22:50:1976 if (!SetBitstreamBufferCallback(bitstream_buffer->id, callback))
77 return PP_ERROR_BADARGUMENT;
[email protected]7ace8ad2011-08-06 03:23:5878
79 Buffer* ppb_buffer =
80 static_cast<Buffer*>(enter_buffer.object());
81 HostResource host_buffer = ppb_buffer->host_resource();
82
83 FlushCommandBuffer();
84 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Decode(
[email protected]ac4b54d2011-10-20 23:09:2885 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(),
[email protected]7ace8ad2011-08-06 03:23:5886 host_buffer, bitstream_buffer->id,
87 bitstream_buffer->size));
88 return PP_OK_COMPLETIONPENDING;
89}
90
91void VideoDecoder::AssignPictureBuffers(uint32_t no_of_buffers,
92 const PP_PictureBuffer_Dev* buffers) {
93 std::vector<PP_PictureBuffer_Dev> buffer_list(
94 buffers, buffers + no_of_buffers);
95 FlushCommandBuffer();
96 GetDispatcher()->Send(
97 new PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers(
[email protected]ac4b54d2011-10-20 23:09:2898 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), buffer_list));
[email protected]7ace8ad2011-08-06 03:23:5899}
100
101void VideoDecoder::ReusePictureBuffer(int32_t picture_buffer_id) {
102 FlushCommandBuffer();
103 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer(
[email protected]ac4b54d2011-10-20 23:09:28104 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), picture_buffer_id));
[email protected]7ace8ad2011-08-06 03:23:58105}
106
[email protected]aed96532012-06-23 14:27:42107int32_t VideoDecoder::Flush(scoped_refptr<TrackedCallback> callback) {
[email protected]d527cdd82011-08-12 22:50:19108 if (!SetFlushCallback(callback))
109 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58110
111 FlushCommandBuffer();
112 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Flush(
[email protected]ac4b54d2011-10-20 23:09:28113 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58114 return PP_OK_COMPLETIONPENDING;
115}
116
[email protected]aed96532012-06-23 14:27:42117int32_t VideoDecoder::Reset(scoped_refptr<TrackedCallback> callback) {
[email protected]d527cdd82011-08-12 22:50:19118 if (!SetResetCallback(callback))
119 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58120
121 FlushCommandBuffer();
122 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Reset(
[email protected]ac4b54d2011-10-20 23:09:28123 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58124 return PP_OK_COMPLETIONPENDING;
125}
126
127void VideoDecoder::Destroy() {
128 FlushCommandBuffer();
129 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Destroy(
[email protected]ac4b54d2011-10-20 23:09:28130 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]9a578392011-12-07 18:59:27131 PPB_VideoDecoder_Shared::Destroy();
[email protected]7ace8ad2011-08-06 03:23:58132}
133
[email protected]7f8b26b2011-08-18 15:41:01134PluginDispatcher* VideoDecoder::GetDispatcher() const {
135 return PluginDispatcher::GetForResource(this);
[email protected]7ace8ad2011-08-06 03:23:58136}
137
138void VideoDecoder::ResetACK(int32_t result) {
139 RunResetCallback(result);
140}
141
142void VideoDecoder::FlushACK(int32_t result) {
143 RunFlushCallback(result);
144}
145
146void VideoDecoder::EndOfBitstreamACK(
147 int32_t bitstream_buffer_id, int32_t result) {
148 RunBitstreamBufferCallback(bitstream_buffer_id, result);
149}
150
[email protected]5c966022011-09-13 18:09:37151PPB_VideoDecoder_Proxy::PPB_VideoDecoder_Proxy(Dispatcher* dispatcher)
152 : InterfaceProxy(dispatcher),
[email protected]7ace8ad2011-08-06 03:23:58153 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
154}
155
156PPB_VideoDecoder_Proxy::~PPB_VideoDecoder_Proxy() {
157}
158
[email protected]7ace8ad2011-08-06 03:23:58159bool PPB_VideoDecoder_Proxy::OnMessageReceived(const IPC::Message& msg) {
[email protected]17cd29a2012-11-15 20:43:23160 if (!dispatcher()->permissions().HasPermission(PERMISSION_DEV))
161 return false;
162
[email protected]7ace8ad2011-08-06 03:23:58163 bool handled = true;
164 IPC_BEGIN_MESSAGE_MAP(PPB_VideoDecoder_Proxy, msg)
165 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Create,
166 OnMsgCreate)
167 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Decode, OnMsgDecode)
168 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers,
169 OnMsgAssignPictureBuffers)
170 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer,
171 OnMsgReusePictureBuffer)
172 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Flush, OnMsgFlush)
173 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Reset, OnMsgReset)
174 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Destroy, OnMsgDestroy)
175 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_ResetACK, OnMsgResetACK)
176 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK,
177 OnMsgEndOfBitstreamACK)
178 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_FlushACK, OnMsgFlushACK)
179 IPC_MESSAGE_UNHANDLED(handled = false)
180 IPC_END_MESSAGE_MAP()
181 DCHECK(handled);
182 return handled;
183}
184
185PP_Resource PPB_VideoDecoder_Proxy::CreateProxyResource(
[email protected]1b2ec22e2011-08-30 00:48:33186 PP_Instance instance,
187 PP_Resource graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28188 PP_VideoDecoder_Profile profile) {
[email protected]7ace8ad2011-08-06 03:23:58189 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
190 // Dispatcher is null if it cannot find the instance passed to it (i.e. if the
191 // client passes in an invalid instance).
192 if (!dispatcher)
193 return 0;
194
[email protected]88cc78182011-12-01 06:47:09195 EnterResourceNoLock<PPB_Graphics3D_API> enter_context(graphics_context,
196 true);
197 if (enter_context.failed())
198 return 0;
[email protected]1b2ec22e2011-08-30 00:48:33199
[email protected]88cc78182011-12-01 06:47:09200 Graphics3D* context = static_cast<Graphics3D*>(enter_context.object());
[email protected]7ace8ad2011-08-06 03:23:58201
202 HostResource result;
203 dispatcher->Send(new PpapiHostMsg_PPBVideoDecoder_Create(
[email protected]ac4b54d2011-10-20 23:09:28204 API_ID_PPB_VIDEO_DECODER_DEV, instance,
[email protected]88cc78182011-12-01 06:47:09205 context->host_resource(), profile, &result));
[email protected]7ace8ad2011-08-06 03:23:58206 if (result.is_null())
207 return 0;
208
[email protected]7f8b26b2011-08-18 15:41:01209 // Need a scoped_refptr to keep the object alive during the Init call.
210 scoped_refptr<VideoDecoder> decoder(new VideoDecoder(result));
[email protected]88cc78182011-12-01 06:47:09211 decoder->InitCommon(graphics_context, context->gles2_impl());
[email protected]7f8b26b2011-08-18 15:41:01212 return decoder->GetReference();
[email protected]7ace8ad2011-08-06 03:23:58213}
214
215void PPB_VideoDecoder_Proxy::OnMsgCreate(
[email protected]1b2ec22e2011-08-30 00:48:33216 PP_Instance instance, const HostResource& graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28217 PP_VideoDecoder_Profile profile,
[email protected]7ace8ad2011-08-06 03:23:58218 HostResource* result) {
[email protected]5c966022011-09-13 18:09:37219 thunk::EnterResourceCreation resource_creation(instance);
[email protected]7ace8ad2011-08-06 03:23:58220 if (resource_creation.failed())
221 return;
222
[email protected]7ace8ad2011-08-06 03:23:58223 // Make the resource and get the API pointer to its interface.
224 result->SetHostResource(
225 instance, resource_creation.functions()->CreateVideoDecoder(
[email protected]2ffc31a2011-09-01 03:18:28226 instance, graphics_context.host_resource(), profile));
[email protected]7ace8ad2011-08-06 03:23:58227}
228
229void PPB_VideoDecoder_Proxy::OnMsgDecode(
230 const HostResource& decoder,
[email protected]b31199e2012-12-12 00:48:29231 const HostResource& buffer, int32 id, uint32 size) {
[email protected]5c966022011-09-13 18:09:37232 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
233 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58234 &PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin, decoder, id);
[email protected]5c966022011-09-13 18:09:37235 if (enter.failed())
236 return;
[email protected]7ace8ad2011-08-06 03:23:58237 PP_VideoBitstreamBuffer_Dev bitstream = { id, buffer.host_resource(), size };
[email protected]5c966022011-09-13 18:09:37238 enter.SetResult(enter.object()->Decode(&bitstream, enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58239}
240
241void PPB_VideoDecoder_Proxy::OnMsgAssignPictureBuffers(
242 const HostResource& decoder,
243 const std::vector<PP_PictureBuffer_Dev>& buffers) {
[email protected]5c966022011-09-13 18:09:37244 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
245 if (enter.succeeded() && !buffers.empty()) {
246 const PP_PictureBuffer_Dev* buffer_array = &buffers.front();
247 enter.object()->AssignPictureBuffers(buffers.size(), buffer_array);
248 }
[email protected]7ace8ad2011-08-06 03:23:58249}
250
251void PPB_VideoDecoder_Proxy::OnMsgReusePictureBuffer(
252 const HostResource& decoder, int32 picture_buffer_id) {
[email protected]5c966022011-09-13 18:09:37253 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
254 if (enter.succeeded())
255 enter.object()->ReusePictureBuffer(picture_buffer_id);
[email protected]7ace8ad2011-08-06 03:23:58256}
257
258void PPB_VideoDecoder_Proxy::OnMsgFlush(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37259 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
260 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58261 &PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37262 if (enter.succeeded())
263 enter.SetResult(enter.object()->Flush(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58264}
265
266void PPB_VideoDecoder_Proxy::OnMsgReset(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37267 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_API> enter(
268 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58269 &PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37270 if (enter.succeeded())
271 enter.SetResult(enter.object()->Reset(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58272}
273
274void PPB_VideoDecoder_Proxy::OnMsgDestroy(const HostResource& decoder) {
[email protected]5c966022011-09-13 18:09:37275 EnterHostFromHostResource<PPB_VideoDecoder_API> enter(decoder);
276 if (enter.succeeded())
277 enter.object()->Destroy();
[email protected]7ace8ad2011-08-06 03:23:58278}
279
280void PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin(
281 int32_t result, const HostResource& decoder, int32 id) {
282 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK(
[email protected]ac4b54d2011-10-20 23:09:28283 API_ID_PPB_VIDEO_DECODER_DEV, decoder, id, result));
[email protected]7ace8ad2011-08-06 03:23:58284}
285
286void PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin(
287 int32_t result, const HostResource& decoder) {
288 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_FlushACK(
[email protected]ac4b54d2011-10-20 23:09:28289 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58290}
291
292void PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin(
293 int32_t result, const HostResource& decoder) {
294 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_ResetACK(
[email protected]ac4b54d2011-10-20 23:09:28295 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58296}
297
298void PPB_VideoDecoder_Proxy::OnMsgEndOfBitstreamACK(
299 const HostResource& decoder, int32_t id, int32_t result) {
300 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
301 if (enter.succeeded())
302 static_cast<VideoDecoder*>(enter.object())->EndOfBitstreamACK(id, result);
303}
304
305void PPB_VideoDecoder_Proxy::OnMsgFlushACK(
306 const HostResource& decoder, int32_t result) {
307 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
308 if (enter.succeeded())
309 static_cast<VideoDecoder*>(enter.object())->FlushACK(result);
310}
311
312void PPB_VideoDecoder_Proxy::OnMsgResetACK(
313 const HostResource& decoder, int32_t result) {
314 EnterPluginFromHostResource<PPB_VideoDecoder_API> enter(decoder);
315 if (enter.succeeded())
316 static_cast<VideoDecoder*>(enter.object())->ResetACK(result);
317}
318
319} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02320} // namespace ppapi