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/gpu_channel_host.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 int command_buffer_route_id
= graphics3d
->GetCommandBufferRouteId();
128 if (!command_buffer_route_id
)
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 through the returned channel.
136 GpuChannelHost
* channel
= graphics3d
->channel();
138 decoder_
= channel
->CreateVideoDecoder(command_buffer_route_id
);
139 if (decoder_
&& decoder_
->Initialize(media_profile
, this)) {
144 if (acceleration
== PP_HARDWAREACCELERATION_ONLY
)
145 return PP_ERROR_NOTSUPPORTED
;
148 #if defined(OS_ANDROID)
149 return PP_ERROR_NOTSUPPORTED
;
151 decoder_
.reset(new VideoDecoderShim(this));
152 initialize_reply_context_
= context
->MakeReplyMessageContext();
153 decoder_
->Initialize(media_profile
, this);
155 return PP_OK_COMPLETIONPENDING
;
159 int32_t PepperVideoDecoderHost::OnHostMsgGetShm(
160 ppapi::host::HostMessageContext
* context
,
164 return PP_ERROR_FAILED
;
166 // Make the buffers larger since we hope to reuse them.
169 static_cast<uint32_t>(ppapi::proxy::kMinimumBitstreamBufferSize
));
170 if (shm_size
> ppapi::proxy::kMaximumBitstreamBufferSize
)
171 return PP_ERROR_FAILED
;
173 if (shm_id
>= ppapi::proxy::kMaximumPendingDecodes
)
174 return PP_ERROR_FAILED
;
175 // The shm_id must be inside or at the end of shm_buffers_.
176 if (shm_id
> shm_buffers_
.size())
177 return PP_ERROR_FAILED
;
178 // Reject an attempt to reallocate a busy shm buffer.
179 if (shm_id
< shm_buffers_
.size() && shm_buffer_busy_
[shm_id
])
180 return PP_ERROR_FAILED
;
182 content::RenderThread
* render_thread
= content::RenderThread::Get();
183 scoped_ptr
<base::SharedMemory
> shm(
184 render_thread
->HostAllocateSharedMemoryBuffer(shm_size
).Pass());
185 if (!shm
|| !shm
->Map(shm_size
))
186 return PP_ERROR_FAILED
;
188 base::SharedMemoryHandle shm_handle
= shm
->handle();
189 if (shm_id
== shm_buffers_
.size()) {
190 shm_buffers_
.push_back(shm
.release());
191 shm_buffer_busy_
.push_back(false);
193 // Remove the old buffer. Delete manually since ScopedVector won't delete
194 // the existing element if we just assign over it.
195 delete shm_buffers_
[shm_id
];
196 shm_buffers_
[shm_id
] = shm
.release();
199 SerializedHandle
handle(
200 renderer_ppapi_host_
->ShareSharedMemoryHandleWithRemote(shm_handle
),
202 ppapi::host::ReplyMessageContext reply_context
=
203 context
->MakeReplyMessageContext();
204 reply_context
.params
.AppendHandle(handle
);
205 host()->SendReply(reply_context
,
206 PpapiPluginMsg_VideoDecoder_GetShmReply(shm_size
));
208 return PP_OK_COMPLETIONPENDING
;
211 int32_t PepperVideoDecoderHost::OnHostMsgDecode(
212 ppapi::host::HostMessageContext
* context
,
217 return PP_ERROR_FAILED
;
219 // |shm_id| is just an index into shm_buffers_. Make sure it's in range.
220 if (static_cast<size_t>(shm_id
) >= shm_buffers_
.size())
221 return PP_ERROR_FAILED
;
222 // Reject an attempt to pass a busy buffer to the decoder again.
223 if (shm_buffer_busy_
[shm_id
])
224 return PP_ERROR_FAILED
;
225 // Reject non-unique decode_id values.
226 if (pending_decodes_
.find(decode_id
) != pending_decodes_
.end())
227 return PP_ERROR_FAILED
;
229 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
230 return PP_ERROR_FAILED
;
232 pending_decodes_
.insert(std::make_pair(
233 decode_id
, PendingDecode(shm_id
, context
->MakeReplyMessageContext())));
235 shm_buffer_busy_
[shm_id
] = true;
237 media::BitstreamBuffer(decode_id
, shm_buffers_
[shm_id
]->handle(), size
));
239 return PP_OK_COMPLETIONPENDING
;
242 int32_t PepperVideoDecoderHost::OnHostMsgAssignTextures(
243 ppapi::host::HostMessageContext
* context
,
245 const std::vector
<uint32_t>& texture_ids
) {
247 return PP_ERROR_FAILED
;
250 std::vector
<media::PictureBuffer
> picture_buffers
;
251 for (uint32 i
= 0; i
< texture_ids
.size(); i
++) {
252 media::PictureBuffer
buffer(
253 texture_ids
[i
], // Use the texture_id to identify the buffer.
254 gfx::Size(size
.width
, size
.height
),
256 picture_buffers
.push_back(buffer
);
258 decoder_
->AssignPictureBuffers(picture_buffers
);
262 int32_t PepperVideoDecoderHost::OnHostMsgRecyclePicture(
263 ppapi::host::HostMessageContext
* context
,
264 uint32_t texture_id
) {
266 return PP_ERROR_FAILED
;
269 TextureSet::iterator it
= pictures_in_use_
.find(texture_id
);
270 if (it
== pictures_in_use_
.end())
271 return PP_ERROR_BADARGUMENT
;
273 pictures_in_use_
.erase(it
);
275 TextureSet::iterator dismissed_texture
=
276 dismissed_pictures_in_use_
.find(texture_id
);
277 if (dismissed_texture
!= dismissed_pictures_in_use_
.end()) {
278 // The texture was already dismissed by the decoder. Notify the plugin.
279 host()->SendUnsolicitedReply(
281 PpapiPluginMsg_VideoDecoder_DismissPicture(texture_id
));
282 dismissed_pictures_in_use_
.erase(dismissed_texture
);
284 decoder_
->ReusePictureBuffer(texture_id
);
290 int32_t PepperVideoDecoderHost::OnHostMsgFlush(
291 ppapi::host::HostMessageContext
* context
) {
293 return PP_ERROR_FAILED
;
295 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
296 return PP_ERROR_FAILED
;
298 flush_reply_context_
= context
->MakeReplyMessageContext();
301 return PP_OK_COMPLETIONPENDING
;
304 int32_t PepperVideoDecoderHost::OnHostMsgReset(
305 ppapi::host::HostMessageContext
* context
) {
307 return PP_ERROR_FAILED
;
309 if (flush_reply_context_
.is_valid() || reset_reply_context_
.is_valid())
310 return PP_ERROR_FAILED
;
312 reset_reply_context_
= context
->MakeReplyMessageContext();
315 return PP_OK_COMPLETIONPENDING
;
318 void PepperVideoDecoderHost::ProvidePictureBuffers(
319 uint32 requested_num_of_buffers
,
320 const gfx::Size
& dimensions
,
321 uint32 texture_target
) {
322 RequestTextures(requested_num_of_buffers
,
325 std::vector
<gpu::Mailbox
>());
328 void PepperVideoDecoderHost::PictureReady(const media::Picture
& picture
) {
329 // Don't bother validating the visible rect, since the plugin process is less
330 // trusted than the gpu process.
331 DCHECK(pictures_in_use_
.find(picture
.picture_buffer_id()) ==
332 pictures_in_use_
.end());
333 pictures_in_use_
.insert(picture
.picture_buffer_id());
335 PP_Rect visible_rect
= PP_FromGfxRect(picture
.visible_rect());
336 host()->SendUnsolicitedReply(pp_resource(),
337 PpapiPluginMsg_VideoDecoder_PictureReady(
338 picture
.bitstream_buffer_id(),
339 picture
.picture_buffer_id(), visible_rect
));
342 void PepperVideoDecoderHost::DismissPictureBuffer(int32 picture_buffer_id
) {
343 // If the texture is still used by the plugin keep it until the plugin
345 if (pictures_in_use_
.find(picture_buffer_id
) != pictures_in_use_
.end()) {
346 dismissed_pictures_in_use_
.insert(picture_buffer_id
);
350 host()->SendUnsolicitedReply(
352 PpapiPluginMsg_VideoDecoder_DismissPicture(picture_buffer_id
));
355 void PepperVideoDecoderHost::NotifyEndOfBitstreamBuffer(
356 int32 bitstream_buffer_id
) {
357 PendingDecodeMap::iterator it
= pending_decodes_
.find(bitstream_buffer_id
);
358 if (it
== pending_decodes_
.end()) {
362 const PendingDecode
& pending_decode
= it
->second
;
364 pending_decode
.reply_context
,
365 PpapiPluginMsg_VideoDecoder_DecodeReply(pending_decode
.shm_id
));
366 shm_buffer_busy_
[pending_decode
.shm_id
] = false;
367 pending_decodes_
.erase(it
);
370 void PepperVideoDecoderHost::NotifyFlushDone() {
371 DCHECK(pending_decodes_
.empty());
372 host()->SendReply(flush_reply_context_
,
373 PpapiPluginMsg_VideoDecoder_FlushReply());
374 flush_reply_context_
= ppapi::host::ReplyMessageContext();
377 void PepperVideoDecoderHost::NotifyResetDone() {
378 DCHECK(pending_decodes_
.empty());
379 host()->SendReply(reset_reply_context_
,
380 PpapiPluginMsg_VideoDecoder_ResetReply());
381 reset_reply_context_
= ppapi::host::ReplyMessageContext();
384 void PepperVideoDecoderHost::NotifyError(
385 media::VideoDecodeAccelerator::Error error
) {
386 int32_t pp_error
= PP_ERROR_FAILED
;
388 case media::VideoDecodeAccelerator::UNREADABLE_INPUT
:
389 pp_error
= PP_ERROR_MALFORMED_INPUT
;
391 case media::VideoDecodeAccelerator::ILLEGAL_STATE
:
392 case media::VideoDecodeAccelerator::INVALID_ARGUMENT
:
393 case media::VideoDecodeAccelerator::PLATFORM_FAILURE
:
394 case media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM
:
395 pp_error
= PP_ERROR_RESOURCE_FAILED
;
397 // No default case, to catch unhandled enum values.
399 host()->SendUnsolicitedReply(
400 pp_resource(), PpapiPluginMsg_VideoDecoder_NotifyError(pp_error
));
403 void PepperVideoDecoderHost::OnInitializeComplete(int32_t result
) {
407 initialize_reply_context_
.params
.set_result(result
);
408 host()->SendReply(initialize_reply_context_
,
409 PpapiPluginMsg_VideoDecoder_InitializeReply());
413 const uint8_t* PepperVideoDecoderHost::DecodeIdToAddress(uint32_t decode_id
) {
414 PendingDecodeMap::const_iterator it
= pending_decodes_
.find(decode_id
);
415 DCHECK(it
!= pending_decodes_
.end());
416 uint32_t shm_id
= it
->second
.shm_id
;
417 return static_cast<uint8_t*>(shm_buffers_
[shm_id
]->memory());
420 void PepperVideoDecoderHost::RequestTextures(
421 uint32 requested_num_of_buffers
,
422 const gfx::Size
& dimensions
,
423 uint32 texture_target
,
424 const std::vector
<gpu::Mailbox
>& mailboxes
) {
425 host()->SendUnsolicitedReply(
427 PpapiPluginMsg_VideoDecoder_RequestTextures(
428 requested_num_of_buffers
,
429 PP_MakeSize(dimensions
.width(), dimensions
.height()),
434 } // namespace content