OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/renderer/pepper/pepper_video_decoder_host.h" | |
6 | |
7 #include "base/bind.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/render_thread_impl.h" | |
14 #include "content/renderer/render_view_impl.h" | |
15 #include "media/video/picture.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" | |
25 | |
26 using ppapi::proxy::SerializedHandle; | |
27 using ppapi::thunk::EnterResourceNoLock; | |
28 using ppapi::thunk::PPB_Graphics3D_API; | |
29 | |
30 namespace content { | |
31 | |
32 namespace { | |
33 | |
34 media::VideoCodecProfile PepperToMediaVideoProfile(PP_VideoProfile profile) { | |
35 switch (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_VP8MAIN: | |
59 return media::VP8PROFILE_MAIN; | |
60 // No default case, to catch unhandled PP_VideoProfile values. | |
61 } | |
62 | |
63 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | |
64 } | |
65 | |
66 } // namespace | |
67 | |
68 PepperVideoDecoderHost::PendingDecode::PendingDecode( | |
69 uint32_t shm_id, | |
70 const ppapi::host::ReplyMessageContext& reply_context) | |
71 : shm_id(shm_id), reply_context(reply_context) { | |
72 } | |
73 | |
74 PepperVideoDecoderHost::PendingDecode::~PendingDecode() { | |
75 } | |
76 | |
77 PepperVideoDecoderHost::PepperVideoDecoderHost(RendererPpapiHost* host, | |
78 PP_Instance instance, | |
79 PP_Resource resource) | |
80 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
81 renderer_ppapi_host_(host), | |
82 num_decodes_(0), | |
83 initialized_(false) { | |
84 } | |
85 | |
86 PepperVideoDecoderHost::~PepperVideoDecoderHost() { | |
87 if (decoder_) | |
88 decoder_.release()->Destroy(); | |
89 } | |
90 | |
91 int32_t PepperVideoDecoderHost::OnResourceMessageReceived( | |
92 const IPC::Message& msg, | |
93 ppapi::host::HostMessageContext* context) { | |
94 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDecoderHost, msg) | |
95 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Initialize, | |
96 OnHostMsgInitialize) | |
97 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_GetShm, | |
98 OnHostMsgGetShm) | |
99 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_Decode, | |
100 OnHostMsgDecode) | |
101 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_AssignTextures, | |
102 OnHostMsgAssignTextures) | |
103 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDecoder_RecyclePicture, | |
104 OnHostMsgRecyclePicture) | |
105 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Flush, | |
106 OnHostMsgFlush) | |
107 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDecoder_Reset, | |
108 OnHostMsgReset) | |
109 PPAPI_END_MESSAGE_MAP() | |
110 return PP_ERROR_FAILED; | |
111 } | |
112 | |
113 int32_t PepperVideoDecoderHost::OnHostMsgInitialize( | |
114 ppapi::host::HostMessageContext* context, | |
115 const ppapi::HostResource& graphics_context, | |
116 PP_VideoProfile profile, | |
117 bool allow_software_fallback) { | |
118 if (initialized_) | |
119 return PP_ERROR_FAILED; | |
120 | |
121 EnterResourceNoLock<PPB_Graphics3D_API> enter_graphics( | |
122 graphics_context.host_resource(), true); | |
123 if (enter_graphics.failed()) | |
124 return PP_ERROR_FAILED; | |
125 graphics3d_ = static_cast<PPB_Graphics3D_Impl*>(enter_graphics.object()); | |
126 | |
127 int command_buffer_route_id = graphics3d_->GetCommandBufferRouteId(); | |
128 if (!command_buffer_route_id) | |
129 return PP_ERROR_FAILED; | |
130 | |
131 media::VideoCodecProfile media_profile = PepperToMediaVideoProfile(profile); | |
132 | |
133 // This is not synchronous, but subsequent IPC messages will be buffered, so | |
134 // it is okay to immediately send IPC messages through the returned channel. | |
135 GpuChannelHost* channel = graphics3d_->channel(); | |
136 DCHECK(channel); | |
137 decoder_ = channel->CreateVideoDecoder(command_buffer_route_id); | |
138 if (decoder_ && decoder_->Initialize(media_profile, this)) { | |
139 initialized_ = true; | |
140 return PP_OK; | |
141 } | |
142 decoder_.reset(); | |
143 | |
144 // TODO(bbudge) Implement software fallback. | |
145 return PP_ERROR_NOTSUPPORTED; | |
146 } | |
147 | |
148 int32_t PepperVideoDecoderHost::OnHostMsgGetShm( | |
149 ppapi::host::HostMessageContext* context, | |
150 uint32_t shm_id, | |
151 uint32_t shm_size) { | |
152 if (!initialized_) | |
153 return PP_ERROR_FAILED; | |
154 | |
155 // Make the buffers larger since we hope to reuse them. | |
156 shm_size = std::max(shm_size, ppapi::proxy::kMinimumBitstreamBufferSize); | |
157 if (shm_size > ppapi::proxy::kMaximumBitstreamBufferSize) | |
158 return PP_ERROR_FAILED; | |
piman
2014/05/23 20:07:27
Would it be useful to add some granularity, say, a
bbudge
2014/05/23 20:57:05
The std::max above does that for smaller buffers.
| |
159 | |
160 if (shm_id >= ppapi::proxy::kMaximumPendingDecodes) | |
161 return PP_ERROR_FAILED; | |
162 // The shm_id must be inside or at the end of shm_buffers_. | |
163 if (shm_id > shm_buffers_.size()) | |
164 return PP_ERROR_FAILED; | |
165 // Reject an attempt to reallocate a busy shm buffer. | |
166 if (shm_id < shm_buffers_.size() && shm_buffer_busy_[shm_id]) | |
167 return PP_ERROR_FAILED; | |
168 | |
169 content::RenderThread* render_thread = content::RenderThread::Get(); | |
170 scoped_ptr<base::SharedMemory> shm( | |
171 render_thread->HostAllocateSharedMemoryBuffer(shm_size).Pass()); | |
172 if (!shm || !shm->Map(shm_size)) | |
173 return PP_ERROR_FAILED; | |
174 | |
175 base::SharedMemoryHandle shm_handle = shm->handle(); | |
176 if (shm_id == shm_buffers_.size()) { | |
177 shm_buffers_.push_back(shm.release()); | |
178 shm_buffer_busy_.push_back(false); | |
179 } else { | |
180 // Fill in the new resized buffer. Delete it manually since ScopedVector | |
181 // won't delete the existing element if we just assign it. | |
182 delete shm_buffers_[shm_id]; | |
183 shm_buffers_[shm_id] = shm.release(); | |
184 } | |
185 | |
186 #if defined(OS_WIN) | |
187 base::PlatformFile platform_file = shm_handle; | |
188 #elif defined(OS_POSIX) | |
189 base::PlatformFile platform_file = shm_handle.fd; | |
190 #else | |
191 #error Not implemented. | |
192 #endif | |
193 SerializedHandle handle( | |
194 renderer_ppapi_host_->ShareHandleWithRemote(platform_file, false), | |
195 shm_size); | |
196 ppapi::host::ReplyMessageContext reply_context = | |
197 context->MakeReplyMessageContext(); | |
198 reply_context.params.AppendHandle(handle); | |
199 host()->SendReply(reply_context, | |
200 PpapiPluginMsg_VideoDecoder_GetShmReply(shm_size)); | |
201 | |
202 return PP_OK_COMPLETIONPENDING; | |
203 } | |
204 | |
205 int32_t PepperVideoDecoderHost::OnHostMsgDecode( | |
206 ppapi::host::HostMessageContext* context, | |
207 uint32_t shm_id, | |
208 uint32_t size) { | |
209 if (!initialized_) | |
210 return PP_ERROR_FAILED; | |
211 DCHECK(decoder_); | |
212 // |shm_id| is just an index into shm_buffers_. Make sure it's in range. | |
213 if (static_cast<size_t>(shm_id) >= shm_buffers_.size()) | |
214 return PP_ERROR_FAILED; | |
215 // Reject an attempt to pass a busy buffer to the decoder again. | |
216 if (shm_buffer_busy_[shm_id]) | |
217 return PP_ERROR_FAILED; | |
218 | |
219 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
220 return PP_ERROR_FAILED; | |
221 | |
222 DoDecode(context, shm_id, size); | |
223 return PP_OK_COMPLETIONPENDING; | |
224 } | |
225 | |
226 int32_t PepperVideoDecoderHost::OnHostMsgAssignTextures( | |
227 ppapi::host::HostMessageContext* context, | |
228 const PP_Size& size, | |
229 const std::vector<uint32_t>& texture_ids) { | |
230 if (!initialized_) | |
231 return PP_ERROR_FAILED; | |
232 DCHECK(decoder_); | |
233 | |
234 std::vector<media::PictureBuffer> picture_buffers; | |
235 for (uint32 i = 0; i < texture_ids.size(); i++) { | |
236 media::PictureBuffer buffer( | |
237 texture_ids[i], // Use the texture_id to identify the buffer. | |
piman
2014/05/23 20:07:27
Would anything bad happen if texture_ids are not a
bbudge
2014/05/23 20:57:05
The ownership of textures would be confused, so th
piman
2014/05/23 21:19:25
It's not exactly benign, if another texture got re
bbudge
2014/05/23 22:18:44
That's a good point. I think in this case the 'id'
| |
238 gfx::Size(size.width, size.height), | |
239 texture_ids[i]); | |
240 picture_buffers.push_back(buffer); | |
241 } | |
242 decoder_->AssignPictureBuffers(picture_buffers); | |
243 return PP_OK; | |
244 } | |
245 | |
246 int32_t PepperVideoDecoderHost::OnHostMsgRecyclePicture( | |
247 ppapi::host::HostMessageContext* context, | |
248 uint32_t texture_id) { | |
249 if (!initialized_) | |
250 return PP_ERROR_FAILED; | |
251 DCHECK(decoder_); | |
252 if (reset_reply_context_.is_valid()) | |
253 return PP_ERROR_FAILED; | |
254 | |
255 decoder_->ReusePictureBuffer(texture_id); | |
256 | |
257 return PP_OK; | |
258 } | |
259 | |
260 int32_t PepperVideoDecoderHost::OnHostMsgFlush( | |
261 ppapi::host::HostMessageContext* context) { | |
262 if (!initialized_) | |
263 return PP_ERROR_FAILED; | |
264 DCHECK(decoder_); | |
265 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
266 return PP_ERROR_FAILED; | |
267 | |
268 flush_reply_context_ = context->MakeReplyMessageContext(); | |
269 decoder_->Flush(); | |
270 | |
271 return PP_OK_COMPLETIONPENDING; | |
272 } | |
273 | |
274 int32_t PepperVideoDecoderHost::OnHostMsgReset( | |
275 ppapi::host::HostMessageContext* context) { | |
276 if (!initialized_) | |
277 return PP_ERROR_FAILED; | |
278 DCHECK(decoder_); | |
279 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
280 return PP_ERROR_FAILED; | |
281 | |
282 reset_reply_context_ = context->MakeReplyMessageContext(); | |
283 decoder_->Reset(); | |
284 | |
285 return PP_OK_COMPLETIONPENDING; | |
286 } | |
287 | |
288 void PepperVideoDecoderHost::ProvidePictureBuffers( | |
289 uint32 requested_num_of_buffers, | |
290 const gfx::Size& dimensions, | |
291 uint32 texture_target) { | |
292 RequestTextures(requested_num_of_buffers, dimensions, texture_target); | |
293 } | |
294 | |
295 void PepperVideoDecoderHost::DoDecode(ppapi::host::HostMessageContext* context, | |
296 uint32_t shm_id, | |
297 uint32_t size) { | |
298 int32_t uid = num_decodes_++; | |
299 pending_decodes_.insert(std::make_pair( | |
300 uid, PendingDecode(shm_id, context->MakeReplyMessageContext()))); | |
301 | |
302 shm_buffer_busy_[shm_id] = true; | |
303 decoder_->Decode( | |
304 media::BitstreamBuffer(uid, shm_buffers_[shm_id]->handle(), size)); | |
305 } | |
306 | |
307 void PepperVideoDecoderHost::RequestTextures(uint32 requested_num_of_buffers, | |
308 const gfx::Size& dimensions, | |
309 uint32 texture_target) { | |
310 DCHECK(RenderThreadImpl::current()); | |
311 host()->SendUnsolicitedReply( | |
312 pp_resource(), | |
313 PpapiPluginMsg_VideoDecoder_RequestTextures( | |
314 requested_num_of_buffers, | |
315 PP_MakeSize(dimensions.width(), dimensions.height()), | |
316 texture_target)); | |
317 } | |
318 | |
319 void PepperVideoDecoderHost::PictureReady(const media::Picture& picture) { | |
320 DCHECK(RenderThreadImpl::current()); | |
321 host()->SendUnsolicitedReply( | |
322 pp_resource(), | |
323 PpapiPluginMsg_VideoDecoder_PictureReady(picture.bitstream_buffer_id(), | |
324 picture.picture_buffer_id())); | |
325 } | |
326 | |
327 void PepperVideoDecoderHost::DismissPictureBuffer(int32 picture_buffer_id) { | |
328 DCHECK(RenderThreadImpl::current()); | |
329 host()->SendUnsolicitedReply( | |
330 pp_resource(), | |
331 PpapiPluginMsg_VideoDecoder_DismissPicture(picture_buffer_id)); | |
332 } | |
333 | |
334 void PepperVideoDecoderHost::NotifyError( | |
335 media::VideoDecodeAccelerator::Error error) { | |
336 DCHECK(RenderThreadImpl::current()); | |
337 int32_t pp_error = PP_ERROR_FAILED; | |
338 switch (error) { | |
339 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
340 pp_error = PP_ERROR_MALFORMED_INPUT; | |
341 break; | |
342 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
343 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
344 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
345 pp_error = PP_ERROR_RESOURCE_FAILED; | |
346 break; | |
347 default: | |
348 NOTREACHED(); | |
349 break; | |
350 } | |
351 host()->SendUnsolicitedReply( | |
352 pp_resource(), PpapiPluginMsg_VideoDecoder_NotifyError(pp_error)); | |
353 } | |
354 | |
355 void PepperVideoDecoderHost::NotifyResetDone() { | |
356 DCHECK(RenderThreadImpl::current()); | |
357 host()->SendReply(reset_reply_context_, | |
358 PpapiPluginMsg_VideoDecoder_ResetReply()); | |
359 reset_reply_context_ = ppapi::host::ReplyMessageContext(); | |
360 } | |
361 | |
362 void PepperVideoDecoderHost::NotifyEndOfBitstreamBuffer( | |
363 int32 bitstream_buffer_id) { | |
364 DCHECK(RenderThreadImpl::current()); | |
365 PendingDecodeMap::iterator it = pending_decodes_.find(bitstream_buffer_id); | |
366 if (it == pending_decodes_.end()) { | |
367 NOTREACHED(); | |
368 return; | |
369 } | |
370 const PendingDecode& pending_decode = it->second; | |
371 host()->SendReply( | |
372 pending_decode.reply_context, | |
373 PpapiPluginMsg_VideoDecoder_DecodeReply(pending_decode.shm_id)); | |
374 shm_buffer_busy_[pending_decode.shm_id] = false; | |
375 pending_decodes_.erase(it); | |
376 } | |
377 | |
378 void PepperVideoDecoderHost::NotifyFlushDone() { | |
379 DCHECK(RenderThreadImpl::current()); | |
380 host()->SendReply(flush_reply_context_, | |
381 PpapiPluginMsg_VideoDecoder_FlushReply()); | |
382 flush_reply_context_ = ppapi::host::ReplyMessageContext(); | |
383 } | |
384 | |
385 } // namespace content | |
OLD | NEW |