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( | |
157 shm_size, | |
158 static_cast<uint32_t>(ppapi::proxy::kMinimumBitstreamBufferSize)); | |
159 if (shm_size > ppapi::proxy::kMaximumBitstreamBufferSize) | |
160 return PP_ERROR_FAILED; | |
161 | |
162 if (shm_id >= ppapi::proxy::kMaximumPendingDecodes) | |
163 return PP_ERROR_FAILED; | |
164 // The shm_id must be inside or at the end of shm_buffers_. | |
165 if (shm_id > shm_buffers_.size()) | |
166 return PP_ERROR_FAILED; | |
167 // Reject an attempt to reallocate a busy shm buffer. | |
168 if (shm_id < shm_buffers_.size() && shm_buffer_busy_[shm_id]) | |
169 return PP_ERROR_FAILED; | |
170 | |
171 content::RenderThread* render_thread = content::RenderThread::Get(); | |
172 scoped_ptr<base::SharedMemory> shm( | |
173 render_thread->HostAllocateSharedMemoryBuffer(shm_size).Pass()); | |
174 if (!shm || !shm->Map(shm_size)) | |
175 return PP_ERROR_FAILED; | |
176 | |
177 base::SharedMemoryHandle shm_handle = shm->handle(); | |
178 if (shm_id == shm_buffers_.size()) { | |
179 shm_buffers_.push_back(shm.release()); | |
180 shm_buffer_busy_.push_back(false); | |
181 } else { | |
182 // Fill in the new resized buffer. Delete it manually since ScopedVector | |
183 // won't delete the existing element if we just assign it. | |
184 delete shm_buffers_[shm_id]; | |
185 shm_buffers_[shm_id] = shm.release(); | |
186 } | |
187 | |
188 #if defined(OS_WIN) | |
189 base::PlatformFile platform_file = shm_handle; | |
190 #elif defined(OS_POSIX) | |
191 base::PlatformFile platform_file = shm_handle.fd; | |
192 #else | |
193 #error Not implemented. | |
194 #endif | |
195 SerializedHandle handle( | |
196 renderer_ppapi_host_->ShareHandleWithRemote(platform_file, false), | |
197 shm_size); | |
198 ppapi::host::ReplyMessageContext reply_context = | |
199 context->MakeReplyMessageContext(); | |
200 reply_context.params.AppendHandle(handle); | |
201 host()->SendReply(reply_context, | |
202 PpapiPluginMsg_VideoDecoder_GetShmReply(shm_size)); | |
203 | |
204 return PP_OK_COMPLETIONPENDING; | |
205 } | |
206 | |
207 int32_t PepperVideoDecoderHost::OnHostMsgDecode( | |
208 ppapi::host::HostMessageContext* context, | |
209 uint32_t shm_id, | |
210 uint32_t size) { | |
211 if (!initialized_) | |
212 return PP_ERROR_FAILED; | |
213 DCHECK(decoder_); | |
214 // |shm_id| is just an index into shm_buffers_. Make sure it's in range. | |
215 if (static_cast<size_t>(shm_id) >= shm_buffers_.size()) | |
216 return PP_ERROR_FAILED; | |
217 // Reject an attempt to pass a busy buffer to the decoder again. | |
218 if (shm_buffer_busy_[shm_id]) | |
219 return PP_ERROR_FAILED; | |
220 | |
221 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
222 return PP_ERROR_FAILED; | |
223 | |
224 int32_t uid = num_decodes_++; | |
225 // Wrap to 0 if num_decodes_ overflowed. | |
Ami GONE FROM CHROMIUM
2014/05/29 02:31:36
signed integer overflow is undefined behavior. Yo
bbudge
2014/05/29 12:07:38
Thanks! Done.
| |
226 if (num_decodes_ < 0) | |
227 num_decodes_ = 0; | |
228 | |
229 pending_decodes_.insert(std::make_pair( | |
230 uid, PendingDecode(shm_id, context->MakeReplyMessageContext()))); | |
231 | |
232 shm_buffer_busy_[shm_id] = true; | |
233 decoder_->Decode( | |
234 media::BitstreamBuffer(uid, shm_buffers_[shm_id]->handle(), size)); | |
235 | |
236 return PP_OK_COMPLETIONPENDING; | |
237 } | |
238 | |
239 int32_t PepperVideoDecoderHost::OnHostMsgAssignTextures( | |
240 ppapi::host::HostMessageContext* context, | |
241 const PP_Size& size, | |
242 const std::vector<uint32_t>& texture_ids) { | |
243 if (!initialized_) | |
244 return PP_ERROR_FAILED; | |
245 DCHECK(decoder_); | |
246 | |
247 std::vector<media::PictureBuffer> picture_buffers; | |
248 for (uint32 i = 0; i < texture_ids.size(); i++) { | |
249 media::PictureBuffer buffer( | |
250 texture_ids[i], // Use the texture_id to identify the buffer. | |
251 gfx::Size(size.width, size.height), | |
252 texture_ids[i]); | |
253 picture_buffers.push_back(buffer); | |
254 } | |
255 decoder_->AssignPictureBuffers(picture_buffers); | |
256 return PP_OK; | |
257 } | |
258 | |
259 int32_t PepperVideoDecoderHost::OnHostMsgRecyclePicture( | |
260 ppapi::host::HostMessageContext* context, | |
261 uint32_t texture_id) { | |
262 if (!initialized_) | |
263 return PP_ERROR_FAILED; | |
264 DCHECK(decoder_); | |
265 if (reset_reply_context_.is_valid()) | |
266 return PP_ERROR_FAILED; | |
267 | |
268 decoder_->ReusePictureBuffer(texture_id); | |
269 | |
270 return PP_OK; | |
271 } | |
272 | |
273 int32_t PepperVideoDecoderHost::OnHostMsgFlush( | |
274 ppapi::host::HostMessageContext* context) { | |
275 if (!initialized_) | |
276 return PP_ERROR_FAILED; | |
277 DCHECK(decoder_); | |
278 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
279 return PP_ERROR_FAILED; | |
280 | |
281 flush_reply_context_ = context->MakeReplyMessageContext(); | |
282 decoder_->Flush(); | |
283 | |
284 return PP_OK_COMPLETIONPENDING; | |
285 } | |
286 | |
287 int32_t PepperVideoDecoderHost::OnHostMsgReset( | |
288 ppapi::host::HostMessageContext* context) { | |
289 if (!initialized_) | |
290 return PP_ERROR_FAILED; | |
291 DCHECK(decoder_); | |
292 if (flush_reply_context_.is_valid() || reset_reply_context_.is_valid()) | |
293 return PP_ERROR_FAILED; | |
294 | |
295 reset_reply_context_ = context->MakeReplyMessageContext(); | |
296 decoder_->Reset(); | |
297 | |
298 return PP_OK_COMPLETIONPENDING; | |
299 } | |
300 | |
301 void PepperVideoDecoderHost::ProvidePictureBuffers( | |
302 uint32 requested_num_of_buffers, | |
303 const gfx::Size& dimensions, | |
304 uint32 texture_target) { | |
305 DCHECK(RenderThreadImpl::current()); | |
306 host()->SendUnsolicitedReply( | |
307 pp_resource(), | |
308 PpapiPluginMsg_VideoDecoder_RequestTextures( | |
309 requested_num_of_buffers, | |
310 PP_MakeSize(dimensions.width(), dimensions.height()), | |
311 texture_target)); | |
312 } | |
313 | |
314 void PepperVideoDecoderHost::PictureReady(const media::Picture& picture) { | |
315 DCHECK(RenderThreadImpl::current()); | |
316 host()->SendUnsolicitedReply( | |
317 pp_resource(), | |
318 PpapiPluginMsg_VideoDecoder_PictureReady(picture.bitstream_buffer_id(), | |
319 picture.picture_buffer_id())); | |
320 } | |
321 | |
322 void PepperVideoDecoderHost::DismissPictureBuffer(int32 picture_buffer_id) { | |
323 DCHECK(RenderThreadImpl::current()); | |
324 host()->SendUnsolicitedReply( | |
325 pp_resource(), | |
326 PpapiPluginMsg_VideoDecoder_DismissPicture(picture_buffer_id)); | |
327 } | |
328 | |
329 void PepperVideoDecoderHost::NotifyError( | |
330 media::VideoDecodeAccelerator::Error error) { | |
331 DCHECK(RenderThreadImpl::current()); | |
332 int32_t pp_error = PP_ERROR_FAILED; | |
333 switch (error) { | |
334 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
335 pp_error = PP_ERROR_MALFORMED_INPUT; | |
336 break; | |
337 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
338 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
339 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
340 case media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM: | |
341 pp_error = PP_ERROR_RESOURCE_FAILED; | |
342 break; | |
343 // No default case, to catch unhandled enum values. | |
344 } | |
345 host()->SendUnsolicitedReply( | |
346 pp_resource(), PpapiPluginMsg_VideoDecoder_NotifyError(pp_error)); | |
347 } | |
348 | |
349 void PepperVideoDecoderHost::NotifyResetDone() { | |
350 DCHECK(RenderThreadImpl::current()); | |
351 host()->SendReply(reset_reply_context_, | |
352 PpapiPluginMsg_VideoDecoder_ResetReply()); | |
353 reset_reply_context_ = ppapi::host::ReplyMessageContext(); | |
354 } | |
355 | |
356 void PepperVideoDecoderHost::NotifyEndOfBitstreamBuffer( | |
357 int32 bitstream_buffer_id) { | |
358 DCHECK(RenderThreadImpl::current()); | |
359 PendingDecodeMap::iterator it = pending_decodes_.find(bitstream_buffer_id); | |
360 if (it == pending_decodes_.end()) { | |
361 NOTREACHED(); | |
362 return; | |
363 } | |
364 const PendingDecode& pending_decode = it->second; | |
365 host()->SendReply( | |
366 pending_decode.reply_context, | |
367 PpapiPluginMsg_VideoDecoder_DecodeReply(pending_decode.shm_id)); | |
368 shm_buffer_busy_[pending_decode.shm_id] = false; | |
369 pending_decodes_.erase(it); | |
370 } | |
371 | |
372 void PepperVideoDecoderHost::NotifyFlushDone() { | |
373 DCHECK(RenderThreadImpl::current()); | |
374 host()->SendReply(flush_reply_context_, | |
375 PpapiPluginMsg_VideoDecoder_FlushReply()); | |
376 flush_reply_context_ = ppapi::host::ReplyMessageContext(); | |
377 } | |
378 | |
379 } // namespace content | |
OLD | NEW |