1 // Copyright (c) 2012 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/ppb_video_decoder_impl.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "content/common/gpu/client/gpu_channel_host.h"
13 #include "content/renderer/pepper/common.h"
14 #include "content/renderer/pepper/host_globals.h"
15 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
16 #include "content/renderer/pepper/plugin_module.h"
17 #include "content/renderer/pepper/ppb_buffer_impl.h"
18 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "gpu/command_buffer/client/gles2_implementation.h"
21 #include "media/video/picture.h"
22 #include "media/video/video_decode_accelerator.h"
23 #include "ppapi/c/dev/pp_video_dev.h"
24 #include "ppapi/c/dev/ppb_video_decoder_dev.h"
25 #include "ppapi/c/dev/ppp_video_decoder_dev.h"
26 #include "ppapi/c/pp_completion_callback.h"
27 #include "ppapi/c/pp_errors.h"
28 #include "ppapi/shared_impl/resource_tracker.h"
29 #include "ppapi/thunk/enter.h"
30
31 using ppapi::TrackedCallback;
32 using ppapi::thunk::EnterResourceNoLock;
33 using ppapi::thunk::PPB_Buffer_API;
34 using ppapi::thunk::PPB_Graphics3D_API;
35
36 namespace {
37
38 // Convert PP_VideoDecoder_Profile to media::VideoCodecProfile.
PPToMediaProfile(const PP_VideoDecoder_Profile pp_profile)39 media::VideoCodecProfile PPToMediaProfile(
40 const PP_VideoDecoder_Profile pp_profile) {
41 switch (pp_profile) {
42 case PP_VIDEODECODER_H264PROFILE_NONE:
43 // HACK: PPAPI contains a bogus "none" h264 profile that doesn't
44 // correspond to anything in h.264; but a number of released chromium
45 // versions silently promoted this to Baseline profile, so we retain that
46 // behavior here. Fall through.
47 case PP_VIDEODECODER_H264PROFILE_BASELINE:
48 return media::H264PROFILE_BASELINE;
49 case PP_VIDEODECODER_H264PROFILE_MAIN:
50 return media::H264PROFILE_MAIN;
51 case PP_VIDEODECODER_H264PROFILE_EXTENDED:
52 return media::H264PROFILE_EXTENDED;
53 case PP_VIDEODECODER_H264PROFILE_HIGH:
54 return media::H264PROFILE_HIGH;
55 case PP_VIDEODECODER_H264PROFILE_HIGH10PROFILE:
56 return media::H264PROFILE_HIGH10PROFILE;
57 case PP_VIDEODECODER_H264PROFILE_HIGH422PROFILE:
58 return media::H264PROFILE_HIGH422PROFILE;
59 case PP_VIDEODECODER_H264PROFILE_HIGH444PREDICTIVEPROFILE:
60 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE;
61 case PP_VIDEODECODER_H264PROFILE_SCALABLEBASELINE:
62 return media::H264PROFILE_SCALABLEBASELINE;
63 case PP_VIDEODECODER_H264PROFILE_SCALABLEHIGH:
64 return media::H264PROFILE_SCALABLEHIGH;
65 case PP_VIDEODECODER_H264PROFILE_STEREOHIGH:
66 return media::H264PROFILE_STEREOHIGH;
67 case PP_VIDEODECODER_H264PROFILE_MULTIVIEWHIGH:
68 return media::H264PROFILE_MULTIVIEWHIGH;
69 case PP_VIDEODECODER_VP8PROFILE_MAIN:
70 return media::VP8PROFILE_MAIN;
71 default:
72 return media::VIDEO_CODEC_PROFILE_UNKNOWN;
73 }
74 }
75
MediaToPPError(media::VideoDecodeAccelerator::Error error)76 PP_VideoDecodeError_Dev MediaToPPError(
77 media::VideoDecodeAccelerator::Error error) {
78 switch (error) {
79 case media::VideoDecodeAccelerator::ILLEGAL_STATE:
80 return PP_VIDEODECODERERROR_ILLEGAL_STATE;
81 case media::VideoDecodeAccelerator::INVALID_ARGUMENT:
82 return PP_VIDEODECODERERROR_INVALID_ARGUMENT;
83 case media::VideoDecodeAccelerator::UNREADABLE_INPUT:
84 return PP_VIDEODECODERERROR_UNREADABLE_INPUT;
85 case media::VideoDecodeAccelerator::PLATFORM_FAILURE:
86 return PP_VIDEODECODERERROR_PLATFORM_FAILURE;
87 default:
88 NOTREACHED();
89 return PP_VIDEODECODERERROR_ILLEGAL_STATE;
90 }
91 }
92
93 } // namespace
94
95 namespace content {
96
PPB_VideoDecoder_Impl(PP_Instance instance)97 PPB_VideoDecoder_Impl::PPB_VideoDecoder_Impl(PP_Instance instance)
98 : PPB_VideoDecoder_Shared(instance), ppp_videodecoder_(NULL) {
99 PluginModule* plugin_module =
100 HostGlobals::Get()->GetInstance(pp_instance())->module();
101 if (plugin_module) {
102 ppp_videodecoder_ = static_cast<const PPP_VideoDecoder_Dev*>(
103 plugin_module->GetPluginInterface(PPP_VIDEODECODER_DEV_INTERFACE));
104 }
105 }
106
~PPB_VideoDecoder_Impl()107 PPB_VideoDecoder_Impl::~PPB_VideoDecoder_Impl() { Destroy(); }
108
109 // static
Create(PP_Instance instance,PP_Resource graphics_context,PP_VideoDecoder_Profile profile)110 PP_Resource PPB_VideoDecoder_Impl::Create(PP_Instance instance,
111 PP_Resource graphics_context,
112 PP_VideoDecoder_Profile profile) {
113 scoped_refptr<PPB_VideoDecoder_Impl> decoder(
114 new PPB_VideoDecoder_Impl(instance));
115 if (decoder->Init(graphics_context, profile))
116 return decoder->GetReference();
117 return 0;
118 }
119
Init(PP_Resource graphics_context,PP_VideoDecoder_Profile profile)120 bool PPB_VideoDecoder_Impl::Init(PP_Resource graphics_context,
121 PP_VideoDecoder_Profile profile) {
122 EnterResourceNoLock<PPB_Graphics3D_API> enter_context(graphics_context, true);
123 if (enter_context.failed())
124 return false;
125
126 PPB_Graphics3D_Impl* graphics_3d =
127 static_cast<PPB_Graphics3D_Impl*>(enter_context.object());
128
129 int command_buffer_route_id = graphics_3d->GetCommandBufferRouteId();
130 if (command_buffer_route_id == 0)
131 return false;
132
133 InitCommon(graphics_context, graphics_3d->gles2_impl());
134 FlushCommandBuffer();
135
136 // This is not synchronous, but subsequent IPC messages will be buffered, so
137 // it is okay to immediately send IPC messages through the returned channel.
138 GpuChannelHost* channel = graphics_3d->channel();
139 DCHECK(channel);
140 decoder_ = channel->CreateVideoDecoder(command_buffer_route_id);
141 return (decoder_ && decoder_->Initialize(PPToMediaProfile(profile), this));
142 }
143
Decode(const PP_VideoBitstreamBuffer_Dev * bitstream_buffer,scoped_refptr<TrackedCallback> callback)144 int32_t PPB_VideoDecoder_Impl::Decode(
145 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
146 scoped_refptr<TrackedCallback> callback) {
147 if (!decoder_)
148 return PP_ERROR_BADRESOURCE;
149
150 EnterResourceNoLock<PPB_Buffer_API> enter(bitstream_buffer->data, true);
151 if (enter.failed())
152 return PP_ERROR_FAILED;
153
154 PPB_Buffer_Impl* buffer = static_cast<PPB_Buffer_Impl*>(enter.object());
155 DCHECK_GE(bitstream_buffer->id, 0);
156 media::BitstreamBuffer decode_buffer(bitstream_buffer->id,
157 buffer->shared_memory()->handle(),
158 bitstream_buffer->size);
159 if (!SetBitstreamBufferCallback(bitstream_buffer->id, callback))
160 return PP_ERROR_BADARGUMENT;
161
162 FlushCommandBuffer();
163 decoder_->Decode(decode_buffer);
164 return PP_OK_COMPLETIONPENDING;
165 }
166
AssignPictureBuffers(uint32_t no_of_buffers,const PP_PictureBuffer_Dev * buffers)167 void PPB_VideoDecoder_Impl::AssignPictureBuffers(
168 uint32_t no_of_buffers,
169 const PP_PictureBuffer_Dev* buffers) {
170 if (!decoder_)
171 return;
172 UMA_HISTOGRAM_COUNTS_100("Media.PepperVideoDecoderPictureCount",
173 no_of_buffers);
174
175 std::vector<media::PictureBuffer> wrapped_buffers;
176 for (uint32 i = 0; i < no_of_buffers; i++) {
177 PP_PictureBuffer_Dev in_buf = buffers[i];
178 DCHECK_GE(in_buf.id, 0);
179 media::PictureBuffer buffer(
180 in_buf.id,
181 gfx::Size(in_buf.size.width, in_buf.size.height),
182 in_buf.texture_id);
183 wrapped_buffers.push_back(buffer);
184 UMA_HISTOGRAM_COUNTS_10000("Media.PepperVideoDecoderPictureHeight",
185 in_buf.size.height);
186 }
187
188 FlushCommandBuffer();
189 decoder_->AssignPictureBuffers(wrapped_buffers);
190 }
191
ReusePictureBuffer(int32_t picture_buffer_id)192 void PPB_VideoDecoder_Impl::ReusePictureBuffer(int32_t picture_buffer_id) {
193 if (!decoder_)
194 return;
195
196 FlushCommandBuffer();
197 decoder_->ReusePictureBuffer(picture_buffer_id);
198 }
199
Flush(scoped_refptr<TrackedCallback> callback)200 int32_t PPB_VideoDecoder_Impl::Flush(scoped_refptr<TrackedCallback> callback) {
201 if (!decoder_)
202 return PP_ERROR_BADRESOURCE;
203
204 if (!SetFlushCallback(callback))
205 return PP_ERROR_INPROGRESS;
206
207 FlushCommandBuffer();
208 decoder_->Flush();
209 return PP_OK_COMPLETIONPENDING;
210 }
211
Reset(scoped_refptr<TrackedCallback> callback)212 int32_t PPB_VideoDecoder_Impl::Reset(scoped_refptr<TrackedCallback> callback) {
213 if (!decoder_)
214 return PP_ERROR_BADRESOURCE;
215
216 if (!SetResetCallback(callback))
217 return PP_ERROR_INPROGRESS;
218
219 FlushCommandBuffer();
220 decoder_->Reset();
221 return PP_OK_COMPLETIONPENDING;
222 }
223
Destroy()224 void PPB_VideoDecoder_Impl::Destroy() {
225 FlushCommandBuffer();
226
227 decoder_.reset();
228 ppp_videodecoder_ = NULL;
229
230 ::ppapi::PPB_VideoDecoder_Shared::Destroy();
231 }
232
ProvidePictureBuffers(uint32 requested_num_of_buffers,const gfx::Size & dimensions,uint32 texture_target)233 void PPB_VideoDecoder_Impl::ProvidePictureBuffers(
234 uint32 requested_num_of_buffers,
235 const gfx::Size& dimensions,
236 uint32 texture_target) {
237 DCHECK(RenderThreadImpl::current());
238 if (!ppp_videodecoder_)
239 return;
240
241 PP_Size out_dim = PP_MakeSize(dimensions.width(), dimensions.height());
242 ppp_videodecoder_->ProvidePictureBuffers(pp_instance(),
243 pp_resource(),
244 requested_num_of_buffers,
245 &out_dim,
246 texture_target);
247 }
248
PictureReady(const media::Picture & picture)249 void PPB_VideoDecoder_Impl::PictureReady(const media::Picture& picture) {
250 DCHECK(RenderThreadImpl::current());
251 if (!ppp_videodecoder_)
252 return;
253
254 PP_Picture_Dev output;
255 output.picture_buffer_id = picture.picture_buffer_id();
256 output.bitstream_buffer_id = picture.bitstream_buffer_id();
257 ppp_videodecoder_->PictureReady(pp_instance(), pp_resource(), &output);
258 }
259
DismissPictureBuffer(int32 picture_buffer_id)260 void PPB_VideoDecoder_Impl::DismissPictureBuffer(int32 picture_buffer_id) {
261 DCHECK(RenderThreadImpl::current());
262 if (!ppp_videodecoder_)
263 return;
264 ppp_videodecoder_->DismissPictureBuffer(
265 pp_instance(), pp_resource(), picture_buffer_id);
266 }
267
NotifyError(media::VideoDecodeAccelerator::Error error)268 void PPB_VideoDecoder_Impl::NotifyError(
269 media::VideoDecodeAccelerator::Error error) {
270 DCHECK(RenderThreadImpl::current());
271 if (!ppp_videodecoder_)
272 return;
273
274 PP_VideoDecodeError_Dev pp_error = MediaToPPError(error);
275 ppp_videodecoder_->NotifyError(pp_instance(), pp_resource(), pp_error);
276 UMA_HISTOGRAM_ENUMERATION("Media.PepperVideoDecoderError",
277 error,
278 media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM);
279 }
280
NotifyResetDone()281 void PPB_VideoDecoder_Impl::NotifyResetDone() {
282 DCHECK(RenderThreadImpl::current());
283 RunResetCallback(PP_OK);
284 }
285
NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id)286 void PPB_VideoDecoder_Impl::NotifyEndOfBitstreamBuffer(
287 int32 bitstream_buffer_id) {
288 DCHECK(RenderThreadImpl::current());
289 RunBitstreamBufferCallback(bitstream_buffer_id, PP_OK);
290 }
291
NotifyFlushDone()292 void PPB_VideoDecoder_Impl::NotifyFlushDone() {
293 DCHECK(RenderThreadImpl::current());
294 RunFlushCallback(PP_OK);
295 }
296
297 } // namespace content
298