1 // Copyright (c) 2014 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.
5 #include "content/renderer/pepper/pepper_video_decoder_host.h"
8 #include "base/memory/shared_memory.h"
9 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
10 #include "content/common/pepper_file_util.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "content/public/renderer/renderer_ppapi_host.h"
13 #include "content/renderer/pepper/gfx_conversion.h"
14 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
15 #include "content/renderer/pepper/video_decoder_shim.h"
16 #include "media/video/video_decode_accelerator.h"
17 #include "ppapi/c/pp_completion_callback.h"
18 #include "ppapi/c/pp_errors.h"
19 #include "ppapi/host/dispatch_host_message.h"
20 #include "ppapi/host/ppapi_host.h"
21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/proxy/video_decoder_constants.h"
23 #include "ppapi/thunk/enter.h"
24 #include "ppapi/thunk/ppb_graphics_3d_api.h"
26 using ppapi::proxy::SerializedHandle
;
27 using ppapi::thunk::EnterResourceNoLock
;
28 using ppapi::thunk::PPB_Graphics3D_API
;
34 media::VideoCodecProfile
PepperToMediaVideoProfile(PP_VideoProfile profile
) {
36 case PP_VIDEOPROFILE_H264BASELINE
:
37 return media::H264PROFILE_BASELINE
;
38 case PP_VIDEOPROFILE_H264MAIN
:
39 return media::H264PROFILE_MAIN
;
40 case PP_VIDEOPROFILE_H264EXTENDED
:
41 return media::H264PROFILE_EXTENDED
;
42 case PP_VIDEOPROFILE_H264HIGH
:
43 return media::H264PROFILE_HIGH
;
44 case PP_VIDEOPROFILE_H264HIGH10PROFILE
:
45 return media::H264PROFILE_HIGH10PROFILE
;
46 case PP_VIDEOPROFILE_H264HIGH422PROFILE
:
47 return media::H264PROFILE_HIGH422PROFILE
;
48 case PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE
:
49 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE
;
50 case PP_VIDEOPROFILE_H264SCALABLEBASELINE
:
51 return media::H264PROFILE_SCALABLEBASELINE
;
52 case PP_VIDEOPROFILE_H264SCALABLEHIGH
:
53 return media::H264PROFILE_SCALABLEHIGH
;
54 case PP_VIDEOPROFILE_H264STEREOHIGH
:
55 return media::H264PROFILE_STEREOHIGH
;
56 case PP_VIDEOPROFILE_H264MULTIVIEWHIGH
:
57 return media::H264PROFILE_MULTIVIEWHIGH
;
58 case PP_VIDEOPROFILE_VP8_ANY
:
59 return media::VP8PROFILE_ANY
;
60 case PP_VIDEOPROFILE_VP9_ANY
:
61 return media::VP9PROFILE_ANY
;
62 // No default case, to catch unhandled PP_VideoProfile values.
65 return media::VIDEO_CODEC_PROFILE_UNKNOWN
;
70 PepperVideoDecoderHost::PendingDecode::PendingDecode(
72 const ppapi::host::ReplyMessageContext
& reply_context
)
73 : shm_id(shm_id
), reply_context(reply_context
) {
76 PepperVideoDecoderHost::PendingDecode::~PendingDecode() {
79 PepperVideoDecoderHost::PepperVideoDecoderHost(RendererPpapiHost
* host
,
82 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
83 renderer_ppapi_host_(host
),
87 PepperVideoDecoderHost::~PepperVideoDecoderHost() {
90 int32_t PepperVideoDecoderHost::OnResourceMessageReceived(
91 const IPC::Message
& msg
,
92 ppapi::host::HostMessageContext
* context
) {
93 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDecoderHost
, msg
)
94 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Initialize
,
96 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_GetShm
,
98 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Decode
,
100 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_AssignTextures
,
101 OnHostMsgAssignTextures
)
102 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_RecyclePicture
,
103 OnHostMsgRecyclePicture
)
104 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Flush
,
106 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Reset
,
108 PPAPI_END_MESSAGE_MAP()
109 return PP_ERROR_FAILED
;
112 int32_t PepperVideoDecoderHost::OnHostMsgInitialize(
113 ppapi::host::HostMessageContext
* context
,
114 const ppapi::HostResource
& graphics_context
,
115 PP_VideoProfile profile
,
116 PP_HardwareAcceleration acceleration
) {
118 return PP_ERROR_FAILED
;
120 EnterResourceNoLock
<PPB_Graphics3D_API
> enter_graphics(
121 graphics_context
.host_resource(), true);
122 if (enter_graphics
.failed())
123 return PP_ERROR_FAILED
;
124 PPB_Graphics3D_Impl
* graphics3d
=
125 static_cast<PPB_Graphics3D_Impl
*>(enter_graphics
.object());
127 CommandBufferProxyImpl
* command_buffer
= graphics3d
->GetCommandBufferProxy();
129 return PP_ERROR_FAILED
;
131 media::VideoCodecProfile media_profile
= PepperToMediaVideoProfile(profile
);
133 if (acceleration
!= PP_HARDWAREACCELERATION_NONE
) {
134 // This is not synchronous, but subsequent IPC messages will be buffered, so
135 // it is okay to immediately send IPC messages.
136 decoder_
= command_buffer
->CreateVideoDecoder();
137 if (decoder_
&& decoder_
->Initialize(media_profile
, this)) {
142 if (acceleration
== PP_HARDWAREACCELERATION_ONLY
)
143 return PP_ERROR_NOTSUPPORTED
;
146 #if defined(OS_ANDROID)
147 return PP_ERROR_NOTSUPPORTED
;
149 decoder_
.reset(new VideoDecoderShim(this));
150 initialize_reply_context_
= context
->MakeReplyMessageContext();
151 decoder_
->Initialize(media_profile
, this);
153 return PP_OK_COMPLETIONPENDING
;
157 int32_t PepperVideoDecoderHost::OnHostMsgGetShm(
158 ppapi::host::HostMessageContext
* context
,
162 return PP_ERROR_FAILED
;
164 // Make the buffers larger since we hope to reuse them.
167 static_cast<uint32_t>(ppapi::proxy::kMinimumBitstreamBufferSize
));
168 if (shm_size
> ppapi::proxy::kMaximumBitstreamBufferSize
)
169 return PP_ERROR_FAILED
;
171 if (shm_id
>= ppapi::proxy::kMaximumPendingDecodes
)
172 return PP_ERROR_FAILED
;
173 // The shm_id must be inside or at the end of shm_buffers_.
174 if (shm_id
> shm_buffers_
.size())
175 return PP_ERROR_FAILED
;
176 // Reject an attempt to reallocate a busy shm buffer.
177 if (shm_id
< shm_buffers_
.size() && shm_buffer_busy_
[shm_id
])
178 return PP_ERROR_FAILED
;
180 content::RenderThread
* render_thread
= content::RenderThread::Get();
181 scoped_ptr
<base::SharedMemory
> shm(
182 render_thread
->HostAllocateSharedMemoryBuffer(shm_size
).Pass());
183 if (!shm
|| !shm
->Map(shm_size
))
184 return PP_ERROR_FAILED
;
186 base::SharedMemoryHandle shm_handle
= shm
->handle();
187 if (shm_id
== shm_buffers_
.size()) {
188 shm_buffers_
.push_back(shm
.release());
189 shm_buffer_busy_
.push_back(false);
191 // Remove the old buffer. Delete manually since ScopedVector won't delete
192 // the existing element if we just assign over it.
193 delete shm_buffers_
[shm_id
];
194 shm_buffers_
[shm_id
] = shm
.release();
197 SerializedHandle
handle(
198 renderer_ppapi_host_
->ShareSharedMemoryHandleWithRemote(shm_handle
),
200 ppapi::host::ReplyMessageContext reply_context
=
201 context
->MakeReplyMessageContext();
202 reply_context
.params
.AppendHandle(handle
);
203 host()->SendReply(reply_context
,
204 PpapiPluginMsg_VideoDecoder_GetShmReply(shm_size
));
206 return PP_OK_COMPLETIONPENDING
;
209 int32_t PepperVideoDecoderHost::OnHostMsgDecode(
210 ppapi::host::HostMessageContext
* context
,
215 return PP_ERROR_FAILED
;
217 // |shm_id| is just an index into shm_buffers_. Make sure it's in range.
218 if (static_cast<size_t>(shm_id
) >= shm_buffers_
.size())
219 return PP_ERROR_FAILED
;
220 // Reject an attempt to pass a busy buffer to the decoder again.
221 if (shm_buffer_busy_
[shm_id
])
222 return PP_ERROR_FAILED
;
223 // Reject non-unique decode_id values.
224 if (pending_decodes_
.find(decode_id
) != pending_decodes_
.end())
225 return PP_ERROR_FAILED
;
227 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
228 return PP_ERROR_FAILED
;
230 pending_decodes_
.insert(std::make_pair(
231 decode_id
, PendingDecode(shm_id
, context
->MakeReplyMessageContext())));
233 shm_buffer_busy_
[shm_id
] = true;
235 media::BitstreamBuffer(decode_id
, shm_buffers_
[shm_id
]->handle(), size
));
237 return PP_OK_COMPLETIONPENDING
;
240 int32_t PepperVideoDecoderHost::OnHostMsgAssignTextures(
241 ppapi::host::HostMessageContext
* context
,
243 const std::vector
<uint32_t>& texture_ids
) {
245 return PP_ERROR_FAILED
;
248 std::vector
<media::PictureBuffer
> picture_buffers
;
249 for (uint32 i
= 0; i
< texture_ids
.size(); i
++) {
250 media::PictureBuffer
buffer(
251 texture_ids
[i
], // Use the texture_id to identify the buffer.
252 gfx::Size(size
.width
, size
.height
),
254 picture_buffers
.push_back(buffer
);
256 decoder_
->AssignPictureBuffers(picture_buffers
);
260 int32_t PepperVideoDecoderHost::OnHostMsgRecyclePicture(
261 ppapi::host::HostMessageContext
* context
,
262 uint32_t texture_id
) {
264 return PP_ERROR_FAILED
;
267 TextureSet::iterator it
= pictures_in_use_
.find(texture_id
);
268 if (it
== pictures_in_use_
.end())
269 return PP_ERROR_BADARGUMENT
;
271 pictures_in_use_
.erase(it
);
273 TextureSet::iterator dismissed_texture
=
274 dismissed_pictures_in_use_
.find(texture_id
);
275 if (dismissed_texture
!= dismissed_pictures_in_use_
.end()) {
276 // The texture was already dismissed by the decoder. Notify the plugin.
277 host()->SendUnsolicitedReply(
279 PpapiPluginMsg_VideoDecoder_DismissPicture(texture_id
));
280 dismissed_pictures_in_use_
.erase(dismissed_texture
);
282 decoder_
->ReusePictureBuffer(texture_id
);
288 int32_t PepperVideoDecoderHost::OnHostMsgFlush(
289 ppapi::host::HostMessageContext
* context
) {
291 return PP_ERROR_FAILED
;
293 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
294 return PP_ERROR_FAILED
;
296 flush_reply_context_
= context
->MakeReplyMessageContext();
299 return PP_OK_COMPLETIONPENDING
;
302 int32_t PepperVideoDecoderHost::OnHostMsgReset(
303 ppapi::host::HostMessageContext
* context
) {
305 return PP_ERROR_FAILED
;
307 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
308 return PP_ERROR_FAILED
;
310 reset_reply_context_
= context
->MakeReplyMessageContext();
313 return PP_OK_COMPLETIONPENDING
;
316 void PepperVideoDecoderHost::ProvidePictureBuffers(
317 uint32 requested_num_of_buffers
,
318 const gfx::Size
& dimensions
,
319 uint32 texture_target
) {
320 RequestTextures(requested_num_of_buffers
,
323 std::vector
<gpu::Mailbox
>());
326 void PepperVideoDecoderHost::PictureReady(const media::Picture
& picture
) {
327 // Don't bother validating the visible rect, since the plugin process is less
328 // trusted than the gpu process.
329 DCHECK(pictures_in_use_
.find(picture
.picture_buffer_id()) ==
330 pictures_in_use_
.end());
331 pictures_in_use_
.insert(picture
.picture_buffer_id());
333 PP_Rect visible_rect
= PP_FromGfxRect(picture
.visible_rect());
334 host()->SendUnsolicitedReply(pp_resource(),
335 PpapiPluginMsg_VideoDecoder_PictureReady(
336 picture
.bitstream_buffer_id(),
337 picture
.picture_buffer_id(), visible_rect
));
340 void PepperVideoDecoderHost::DismissPictureBuffer(int32 picture_buffer_id
) {
341 // If the texture is still used by the plugin keep it until the plugin
343 if (pictures_in_use_
.find(picture_buffer_id
) != pictures_in_use_
.end()) {
344 dismissed_pictures_in_use_
.insert(picture_buffer_id
);
348 host()->SendUnsolicitedReply(
350 PpapiPluginMsg_VideoDecoder_DismissPicture(picture_buffer_id
));
353 void PepperVideoDecoderHost::NotifyEndOfBitstreamBuffer(
354 int32 bitstream_buffer_id
) {
355 PendingDecodeMap::iterator it
= pending_decodes_
.find(bitstream_buffer_id
);
356 if (it
== pending_decodes_
.end()) {
360 const PendingDecode
& pending_decode
= it
->second
;
362 pending_decode
.reply_context
,
363 PpapiPluginMsg_VideoDecoder_DecodeReply(pending_decode
.shm_id
));
364 shm_buffer_busy_
[pending_decode
.shm_id
] = false;
365 pending_decodes_
.erase(it
);
368 void PepperVideoDecoderHost::NotifyFlushDone() {
369 DCHECK(pending_decodes_
.empty());
370 host()->SendReply(flush_reply_context_
,
371 PpapiPluginMsg_VideoDecoder_FlushReply());
372 flush_reply_context_
= ppapi::host::ReplyMessageContext();
375 void PepperVideoDecoderHost::NotifyResetDone() {
376 DCHECK(pending_decodes_
.empty());
377 host()->SendReply(reset_reply_context_
,
378 PpapiPluginMsg_VideoDecoder_ResetReply());
379 reset_reply_context_
= ppapi::host::ReplyMessageContext();
382 void PepperVideoDecoderHost::NotifyError(
383 media::VideoDecodeAccelerator::Error error
) {
384 int32_t pp_error
= PP_ERROR_FAILED
;
386 case media::VideoDecodeAccelerator::UNREADABLE_INPUT
:
387 pp_error
= PP_ERROR_MALFORMED_INPUT
;
389 case media::VideoDecodeAccelerator::ILLEGAL_STATE
:
390 case media::VideoDecodeAccelerator::INVALID_ARGUMENT
:
391 case media::VideoDecodeAccelerator::PLATFORM_FAILURE
:
392 case media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM
:
393 pp_error
= PP_ERROR_RESOURCE_FAILED
;
395 // No default case, to catch unhandled enum values.
397 host()->SendUnsolicitedReply(
398 pp_resource(), PpapiPluginMsg_VideoDecoder_NotifyError(pp_error
));
401 void PepperVideoDecoderHost::OnInitializeComplete(int32_t result
) {
405 initialize_reply_context_
.params
.set_result(result
);
406 host()->SendReply(initialize_reply_context_
,
407 PpapiPluginMsg_VideoDecoder_InitializeReply());
411 const uint8_t* PepperVideoDecoderHost::DecodeIdToAddress(uint32_t decode_id
) {
412 PendingDecodeMap::const_iterator it
= pending_decodes_
.find(decode_id
);
413 DCHECK(it
!= pending_decodes_
.end());
414 uint32_t shm_id
= it
->second
.shm_id
;
415 return static_cast<uint8_t*>(shm_buffers_
[shm_id
]->memory());
418 void PepperVideoDecoderHost::RequestTextures(
419 uint32 requested_num_of_buffers
,
420 const gfx::Size
& dimensions
,
421 uint32 texture_target
,
422 const std::vector
<gpu::Mailbox
>& mailboxes
) {
423 host()->SendUnsolicitedReply(
425 PpapiPluginMsg_VideoDecoder_RequestTextures(
426 requested_num_of_buffers
,
427 PP_MakeSize(dimensions
.width(), dimensions
.height()),
432 } // namespace content