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/public/renderer/render_thread.h"
11 #include "content/public/renderer/renderer_ppapi_host.h"
12 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
13 #include "content/renderer/pepper/video_decoder_shim.h"
14 #include "media/video/video_decode_accelerator.h"
15 #include "ppapi/c/pp_completion_callback.h"
16 #include "ppapi/c/pp_errors.h"
17 #include "ppapi/host/dispatch_host_message.h"
18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "ppapi/proxy/video_decoder_constants.h"
21 #include "ppapi/thunk/enter.h"
22 #include "ppapi/thunk/ppb_graphics_3d_api.h"
24 using ppapi::proxy::SerializedHandle;
25 using ppapi::thunk::EnterResourceNoLock;
26 using ppapi::thunk::PPB_Graphics3D_API;
32 media::VideoCodecProfile PepperToMediaVideoProfile(PP_VideoProfile profile) {
34 case PP_VIDEOPROFILE_H264BASELINE:
35 return media::H264PROFILE_BASELINE;
36 case PP_VIDEOPROFILE_H264MAIN:
37 return media::H264PROFILE_MAIN;
38 case PP_VIDEOPROFILE_H264EXTENDED:
39 return media::H264PROFILE_EXTENDED;
40 case PP_VIDEOPROFILE_H264HIGH:
41 return media::H264PROFILE_HIGH;
42 case PP_VIDEOPROFILE_H264HIGH10PROFILE:
43 return media::H264PROFILE_HIGH10PROFILE;
44 case PP_VIDEOPROFILE_H264HIGH422PROFILE:
45 return media::H264PROFILE_HIGH422PROFILE;
46 case PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE:
47 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE;
48 case PP_VIDEOPROFILE_H264SCALABLEBASELINE:
49 return media::H264PROFILE_SCALABLEBASELINE;
50 case PP_VIDEOPROFILE_H264SCALABLEHIGH:
51 return media::H264PROFILE_SCALABLEHIGH;
52 case PP_VIDEOPROFILE_H264STEREOHIGH:
53 return media::H264PROFILE_STEREOHIGH;
54 case PP_VIDEOPROFILE_H264MULTIVIEWHIGH:
55 return media::H264PROFILE_MULTIVIEWHIGH;
56 case PP_VIDEOPROFILE_VP8MAIN:
57 return media::VP8PROFILE_MAIN;
58 case PP_VIDEOPROFILE_VP9MAIN:
59 return media::VP9PROFILE_MAIN;
60 // No default case, to catch unhandled PP_VideoProfile values.
63 return media::VIDEO_CODEC_PROFILE_UNKNOWN;
68 PepperVideoDecoderHost::PendingDecode::PendingDecode(
70 const ppapi::host::ReplyMessageContext& reply_context)
71 : shm_id(shm_id), reply_context(reply_context) {
74 PepperVideoDecoderHost::PendingDecode::~PendingDecode() {
77 PepperVideoDecoderHost::PepperVideoDecoderHost(RendererPpapiHost* host,
80 : ResourceHost(host->GetPpapiHost(), instance, resource),
81 renderer_ppapi_host_(host),
85 PepperVideoDecoderHost::~PepperVideoDecoderHost() {
88 int32_t PepperVideoDecoderHost::OnResourceMessageReceived(
89 const IPC::Message& msg,
90 ppapi::host::HostMessageContext* context) {
91 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDecoderHost, msg)
92 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Initialize,
94 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_GetShm,
96 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Decode,
98 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_AssignTextures,
99 OnHostMsgAssignTextures)
100 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_RecyclePicture,
101 OnHostMsgRecyclePicture)
102 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Flush,
104 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Reset,
106 PPAPI_END_MESSAGE_MAP()
107 return PP_ERROR_FAILED;
110 int32_t PepperVideoDecoderHost::OnHostMsgInitialize(
111 ppapi::host::HostMessageContext* context,
112 const ppapi::HostResource& graphics_context,
113 PP_VideoProfile profile,
114 bool allow_software_fallback) {
116 return PP_ERROR_FAILED;
118 EnterResourceNoLock<PPB_Graphics3D_API> enter_graphics(
119 graphics_context.host_resource(), true);
120 if (enter_graphics.failed())
121 return PP_ERROR_FAILED;
122 PPB_Graphics3D_Impl* graphics3d =
123 static_cast<PPB_Graphics3D_Impl*>(enter_graphics.object());
125 int command_buffer_route_id = graphics3d->GetCommandBufferRouteId();
126 if (!command_buffer_route_id)
127 return PP_ERROR_FAILED;
129 media::VideoCodecProfile media_profile = PepperToMediaVideoProfile(profile);
131 // This is not synchronous, but subsequent IPC messages will be buffered, so
132 // it is okay to immediately send IPC messages through the returned channel.
133 GpuChannelHost* channel = graphics3d->channel();
135 decoder_ = channel->CreateVideoDecoder(command_buffer_route_id);
136 if (decoder_ && decoder_->Initialize(media_profile, this)) {
142 if (!allow_software_fallback)
143 return PP_ERROR_NOTSUPPORTED;
145 decoder_.reset(new VideoDecoderShim(this));
146 initialize_reply_context_ = context->MakeReplyMessageContext();
147 decoder_->Initialize(media_profile, this);
149 return PP_OK_COMPLETIONPENDING;
152 int32_t PepperVideoDecoderHost::OnHostMsgGetShm(
153 ppapi::host::HostMessageContext* context,
157 return PP_ERROR_FAILED;
159 // Make the buffers larger since we hope to reuse them.
162 static_cast<uint32_t>(ppapi::proxy::kMinimumBitstreamBufferSize));
163 if (shm_size > ppapi::proxy::kMaximumBitstreamBufferSize)
164 return PP_ERROR_FAILED;
166 if (shm_id >= ppapi::proxy::kMaximumPendingDecodes)
167 return PP_ERROR_FAILED;
168 // The shm_id must be inside or at the end of shm_buffers_.
169 if (shm_id > shm_buffers_.size())
170 return PP_ERROR_FAILED;
171 // Reject an attempt to reallocate a busy shm buffer.
172 if (shm_id < shm_buffers_.size() && shm_buffer_busy_[shm_id])
173 return PP_ERROR_FAILED;
175 content::RenderThread* render_thread = content::RenderThread::Get();
176 scoped_ptr<base::SharedMemory> shm(
177 render_thread->HostAllocateSharedMemoryBuffer(shm_size).Pass());
178 if (!shm || !shm->Map(shm_size))
179 return PP_ERROR_FAILED;
181 base::SharedMemoryHandle shm_handle = shm->handle();
182 if (shm_id == shm_buffers_.size()) {
183 shm_buffers_.push_back(shm.release());
184 shm_buffer_busy_.push_back(false);
186 // Remove the old buffer. Delete manually since ScopedVector won't delete
187 // the existing element if we just assign over it.
188 delete shm_buffers_[shm_id];
189 shm_buffers_[shm_id] = shm.release();
193 base::PlatformFile platform_file = shm_handle;
194 #elif defined(OS_POSIX)
195 base::PlatformFile platform_file = shm_handle.fd;
197 #error Not implemented.
199 SerializedHandle handle(
200 renderer_ppapi_host_->ShareHandleWithRemote(platform_file, false),
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 decoder_->ReusePictureBuffer(texture_id);
273 int32_t PepperVideoDecoderHost::OnHostMsgFlush(
274 ppapi::host::HostMessageContext* context) {
276 return PP_ERROR_FAILED;
278 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid())
279 return PP_ERROR_FAILED;
281 flush_reply_context_ = context->MakeReplyMessageContext();
284 return PP_OK_COMPLETIONPENDING;
287 int32_t PepperVideoDecoderHost::OnHostMsgReset(
288 ppapi::host::HostMessageContext* context) {
290 return PP_ERROR_FAILED;
292 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid())
293 return PP_ERROR_FAILED;
295 reset_reply_context_ = context->MakeReplyMessageContext();
298 return PP_OK_COMPLETIONPENDING;
301 void PepperVideoDecoderHost::ProvidePictureBuffers(
302 uint32 requested_num_of_buffers,
303 const gfx::Size& dimensions,
304 uint32 texture_target) {
305 RequestTextures(requested_num_of_buffers,
308 std::vector<gpu::Mailbox>());
311 void PepperVideoDecoderHost::PictureReady(const media::Picture& picture) {
312 host()->SendUnsolicitedReply(
314 PpapiPluginMsg_VideoDecoder_PictureReady(picture.bitstream_buffer_id(),
315 picture.picture_buffer_id()));
318 void PepperVideoDecoderHost::DismissPictureBuffer(int32 picture_buffer_id) {
319 host()->SendUnsolicitedReply(
321 PpapiPluginMsg_VideoDecoder_DismissPicture(picture_buffer_id));
324 void PepperVideoDecoderHost::NotifyEndOfBitstreamBuffer(
325 int32 bitstream_buffer_id) {
326 PendingDecodeMap::iterator it = pending_decodes_.find(bitstream_buffer_id);
327 if (it == pending_decodes_.end()) {
331 const PendingDecode& pending_decode = it->second;
333 pending_decode.reply_context,
334 PpapiPluginMsg_VideoDecoder_DecodeReply(pending_decode.shm_id));
335 shm_buffer_busy_[pending_decode.shm_id] = false;
336 pending_decodes_.erase(it);
339 void PepperVideoDecoderHost::NotifyFlushDone() {
340 DCHECK(pending_decodes_.empty());
341 host()->SendReply(flush_reply_context_,
342 PpapiPluginMsg_VideoDecoder_FlushReply());
343 flush_reply_context_ = ppapi::host::ReplyMessageContext();
346 void PepperVideoDecoderHost::NotifyResetDone() {
347 DCHECK(pending_decodes_.empty());
348 host()->SendReply(reset_reply_context_,
349 PpapiPluginMsg_VideoDecoder_ResetReply());
350 reset_reply_context_ = ppapi::host::ReplyMessageContext();
353 void PepperVideoDecoderHost::NotifyError(
354 media::VideoDecodeAccelerator::Error error) {
355 int32_t pp_error = PP_ERROR_FAILED;
357 case media::VideoDecodeAccelerator::UNREADABLE_INPUT:
358 pp_error = PP_ERROR_MALFORMED_INPUT;
360 case media::VideoDecodeAccelerator::ILLEGAL_STATE:
361 case media::VideoDecodeAccelerator::INVALID_ARGUMENT:
362 case media::VideoDecodeAccelerator::PLATFORM_FAILURE:
363 case media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM:
364 pp_error = PP_ERROR_RESOURCE_FAILED;
366 // No default case, to catch unhandled enum values.
368 host()->SendUnsolicitedReply(
369 pp_resource(), PpapiPluginMsg_VideoDecoder_NotifyError(pp_error));
372 void PepperVideoDecoderHost::OnInitializeComplete(int32_t result) {
376 initialize_reply_context_.params.set_result(result);
377 host()->SendReply(initialize_reply_context_,
378 PpapiPluginMsg_VideoDecoder_InitializeReply());
382 const uint8_t* PepperVideoDecoderHost::DecodeIdToAddress(uint32_t decode_id) {
383 PendingDecodeMap::const_iterator it = pending_decodes_.find(decode_id);
384 DCHECK(it != pending_decodes_.end());
385 uint32_t shm_id = it->second.shm_id;
386 return static_cast<uint8_t*>(shm_buffers_[shm_id]->memory());
389 void PepperVideoDecoderHost::RequestTextures(
390 uint32 requested_num_of_buffers,
391 const gfx::Size& dimensions,
392 uint32 texture_target,
393 const std::vector<gpu::Mailbox>& mailboxes) {
394 host()->SendUnsolicitedReply(
396 PpapiPluginMsg_VideoDecoder_RequestTextures(
397 requested_num_of_buffers,
398 PP_MakeSize(dimensions.width(), dimensions.height()),
403 } // namespace content