OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "base/bind.h" | |
6 #include "base/memory/shared_memory.h" | |
7 #include "base/numerics/safe_math.h" | |
8 #include "content/common/gpu/client/command_buffer_proxy_impl.h" | |
9 #include "content/public/renderer/renderer_ppapi_host.h" | |
10 #include "content/renderer/pepper/gfx_conversion.h" | |
11 #include "content/renderer/pepper/host_globals.h" | |
12 #include "content/renderer/pepper/pepper_video_encoder_host.h" | |
13 #include "content/renderer/render_thread_impl.h" | |
14 #include "media/base/bind_to_current_loop.h" | |
15 #include "media/base/video_frame.h" | |
16 #include "media/filters/gpu_video_accelerator_factories.h" | |
17 #include "media/video/video_encode_accelerator.h" | |
18 #include "ppapi/c/pp_codecs.h" | |
19 #include "ppapi/c/pp_errors.h" | |
20 #include "ppapi/c/pp_graphics_3d.h" | |
21 #include "ppapi/host/dispatch_host_message.h" | |
22 #include "ppapi/host/ppapi_host.h" | |
23 #include "ppapi/proxy/ppapi_messages.h" | |
24 #include "ppapi/shared_impl/media_stream_buffer.h" | |
25 | |
26 using ppapi::proxy::SerializedHandle; | |
bbudge
2015/02/11 23:27:45
nit: you only use this once, and use the fully qua
llandwerlin-old
2015/02/12 15:59:39
Done.
| |
27 | |
28 namespace content { | |
29 | |
30 namespace { | |
31 | |
32 const int32_t kDefaultNumberOfBitstreamBuffers = 4; | |
33 | |
34 base::PlatformFile ConvertSharedMemoryHandle( | |
35 const base::SharedMemory& shared_memory) { | |
36 #if defined(OS_POSIX) | |
37 return shared_memory.handle().fd; | |
38 #elif defined(OS_WIN) | |
39 return shared_memory.handle(); | |
40 #else | |
41 #error "Platform not supported." | |
42 #endif | |
43 } | |
44 | |
45 int32_t PP_FromMediaEncodeAcceleratorError( | |
46 media::VideoEncodeAccelerator::Error error) { | |
47 switch (error) { | |
48 case media::VideoEncodeAccelerator::kInvalidArgumentError: | |
49 return PP_ERROR_MALFORMED_INPUT; | |
50 case media::VideoEncodeAccelerator::kIllegalStateError: | |
51 case media::VideoEncodeAccelerator::kPlatformFailureError: | |
52 return PP_ERROR_RESOURCE_FAILED; | |
53 // No default case, to catch unhandled enum values. | |
54 } | |
55 return PP_ERROR_FAILED; | |
56 } | |
57 | |
58 // TODO(llandwerlin): move following to media_conversion.cc/h? | |
59 media::VideoCodecProfile PP_ToMediaVideoProfile(PP_VideoProfile profile) { | |
60 switch (profile) { | |
61 case PP_VIDEOPROFILE_H264BASELINE: | |
62 return media::H264PROFILE_BASELINE; | |
63 case PP_VIDEOPROFILE_H264MAIN: | |
64 return media::H264PROFILE_MAIN; | |
65 case PP_VIDEOPROFILE_H264EXTENDED: | |
66 return media::H264PROFILE_EXTENDED; | |
67 case PP_VIDEOPROFILE_H264HIGH: | |
68 return media::H264PROFILE_HIGH; | |
69 case PP_VIDEOPROFILE_H264HIGH10PROFILE: | |
70 return media::H264PROFILE_HIGH10PROFILE; | |
71 case PP_VIDEOPROFILE_H264HIGH422PROFILE: | |
72 return media::H264PROFILE_HIGH422PROFILE; | |
73 case PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE: | |
74 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE; | |
75 case PP_VIDEOPROFILE_H264SCALABLEBASELINE: | |
76 return media::H264PROFILE_SCALABLEBASELINE; | |
77 case PP_VIDEOPROFILE_H264SCALABLEHIGH: | |
78 return media::H264PROFILE_SCALABLEHIGH; | |
79 case PP_VIDEOPROFILE_H264STEREOHIGH: | |
80 return media::H264PROFILE_STEREOHIGH; | |
81 case PP_VIDEOPROFILE_H264MULTIVIEWHIGH: | |
82 return media::H264PROFILE_MULTIVIEWHIGH; | |
83 case PP_VIDEOPROFILE_VP8_ANY: | |
84 return media::VP8PROFILE_ANY; | |
85 case PP_VIDEOPROFILE_VP9_ANY: | |
86 return media::VP9PROFILE_ANY; | |
87 // No default case, to catch unhandled PP_VideoProfile values. | |
88 } | |
89 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | |
90 } | |
91 | |
92 PP_VideoProfile PP_FromMediaVideoProfile(media::VideoCodecProfile profile) { | |
93 switch (profile) { | |
94 case media::H264PROFILE_BASELINE: | |
95 return PP_VIDEOPROFILE_H264BASELINE; | |
96 case media::H264PROFILE_MAIN: | |
97 return PP_VIDEOPROFILE_H264MAIN; | |
98 case media::H264PROFILE_EXTENDED: | |
99 return PP_VIDEOPROFILE_H264EXTENDED; | |
100 case media::H264PROFILE_HIGH: | |
101 return PP_VIDEOPROFILE_H264HIGH; | |
102 case media::H264PROFILE_HIGH10PROFILE: | |
103 return PP_VIDEOPROFILE_H264HIGH10PROFILE; | |
104 case media::H264PROFILE_HIGH422PROFILE: | |
105 return PP_VIDEOPROFILE_H264HIGH422PROFILE; | |
106 case media::H264PROFILE_HIGH444PREDICTIVEPROFILE: | |
107 return PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE; | |
108 case media::H264PROFILE_SCALABLEBASELINE: | |
109 return PP_VIDEOPROFILE_H264SCALABLEBASELINE; | |
110 case media::H264PROFILE_SCALABLEHIGH: | |
111 return PP_VIDEOPROFILE_H264SCALABLEHIGH; | |
112 case media::H264PROFILE_STEREOHIGH: | |
113 return PP_VIDEOPROFILE_H264STEREOHIGH; | |
114 case media::H264PROFILE_MULTIVIEWHIGH: | |
115 return PP_VIDEOPROFILE_H264MULTIVIEWHIGH; | |
116 case media::VP8PROFILE_ANY: | |
117 return PP_VIDEOPROFILE_VP8_ANY; | |
118 case media::VP9PROFILE_ANY: | |
119 return PP_VIDEOPROFILE_VP9_ANY; | |
120 default: | |
121 NOTREACHED(); | |
122 return static_cast<PP_VideoProfile>(-1); | |
123 } | |
124 } | |
125 | |
126 media::VideoFrame::Format PP_ToMediaVideoFormat(PP_VideoFrame_Format format) { | |
127 switch (format) { | |
128 case PP_VIDEOFRAME_FORMAT_UNKNOWN: | |
129 return media::VideoFrame::UNKNOWN; | |
130 case PP_VIDEOFRAME_FORMAT_YV12: | |
131 return media::VideoFrame::YV12; | |
132 case PP_VIDEOFRAME_FORMAT_I420: | |
133 return media::VideoFrame::I420; | |
134 case PP_VIDEOFRAME_FORMAT_BGRA: | |
135 return media::VideoFrame::UNKNOWN; | |
136 // No default case, to catch unhandled PP_VideoFrame_Format values. | |
bbudge
2015/02/11 23:27:45
indent like above
llandwerlin-old
2015/02/12 15:59:39
Sorry for that, cl format does the wrong thing.
| |
137 } | |
138 return media::VideoFrame::UNKNOWN; | |
139 } | |
140 | |
141 PP_VideoFrame_Format PP_FromMediaVideoFormat(media::VideoFrame::Format format) { | |
142 switch (format) { | |
143 case media::VideoFrame::UNKNOWN: | |
144 return PP_VIDEOFRAME_FORMAT_UNKNOWN; | |
145 case media::VideoFrame::YV12: | |
146 return PP_VIDEOFRAME_FORMAT_YV12; | |
147 case media::VideoFrame::I420: | |
148 return PP_VIDEOFRAME_FORMAT_I420; | |
149 default: | |
150 return PP_VIDEOFRAME_FORMAT_UNKNOWN; | |
151 } | |
152 } | |
153 | |
154 PP_VideoProfileDescription PP_FromVideoEncodeAcceleratorSupportedProfile( | |
155 media::VideoEncodeAccelerator::SupportedProfile profile, | |
156 PP_HardwareAcceleration acceleration) { | |
157 PP_VideoProfileDescription pp_profile; | |
158 pp_profile.profile = PP_FromMediaVideoProfile(profile.profile); | |
159 pp_profile.max_resolution = PP_FromGfxSize(profile.max_resolution); | |
160 pp_profile.max_framerate_numerator = profile.max_framerate_numerator; | |
161 pp_profile.max_framerate_denominator = profile.max_framerate_denominator; | |
162 pp_profile.acceleration = acceleration; | |
163 return pp_profile; | |
164 } | |
165 | |
166 bool PP_HardwareAccelerationCompatible(PP_HardwareAcceleration supply, | |
167 PP_HardwareAcceleration demand) { | |
168 switch (supply) { | |
169 case PP_HARDWAREACCELERATION_ONLY: | |
170 return (demand == PP_HARDWAREACCELERATION_ONLY || | |
171 demand == PP_HARDWAREACCELERATION_WITHFALLBACK); | |
172 case PP_HARDWAREACCELERATION_WITHFALLBACK: | |
173 return true; | |
174 case PP_HARDWAREACCELERATION_NONE: | |
175 return (demand == PP_HARDWAREACCELERATION_WITHFALLBACK || | |
176 demand == PP_HARDWAREACCELERATION_NONE); | |
177 // No default case, to catch unhandled PP_HardwareAcceleration values. | |
178 } | |
179 return false; | |
180 } | |
181 | |
182 } // namespace | |
183 | |
184 PepperVideoEncoderHost::ShmBuffer::ShmBuffer( | |
185 int32_t id, | |
186 scoped_ptr<base::SharedMemory> memory, | |
187 size_t size) | |
188 : id(id), shm(memory.Pass()), in_use(true) { | |
189 if (shm) | |
190 shm->Map(size); | |
191 } | |
192 | |
193 PepperVideoEncoderHost::ShmBuffer::~ShmBuffer() { | |
194 } | |
195 | |
196 media::BitstreamBuffer PepperVideoEncoderHost::ShmBuffer::toBitstreamBuffer() { | |
197 return media::BitstreamBuffer(id, shm->handle(), shm->mapped_size()); | |
198 } | |
199 | |
200 PepperVideoEncoderHost::PepperVideoEncoderHost(RendererPpapiHost* host, | |
201 PP_Instance instance, | |
202 PP_Resource resource) | |
203 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
204 renderer_ppapi_host_(host), | |
205 buffer_manager_(this), | |
206 command_buffer_(nullptr), | |
207 initialized_(false), | |
208 encoder_last_error_(PP_ERROR_FAILED), | |
209 frame_count_(0), | |
210 media_input_format_(media::VideoFrame::UNKNOWN), | |
211 weak_ptr_factory_(this) { | |
212 } | |
213 | |
214 PepperVideoEncoderHost::~PepperVideoEncoderHost() { | |
215 encoder_ = nullptr; | |
216 if (command_buffer_) { | |
217 DCHECK(channel_.get()); | |
bbudge
2015/02/11 23:27:45
nit: .get() not needed.
llandwerlin-old
2015/02/12 15:59:39
Done.
| |
218 channel_->DestroyCommandBuffer(command_buffer_); | |
219 command_buffer_ = nullptr; | |
220 } | |
221 channel_ = nullptr; | |
bbudge
2015/02/11 23:27:45
nit: this destructor is about to do this anyway.
llandwerlin-old
2015/02/12 15:59:39
Done.
| |
222 } | |
223 | |
224 int32_t PepperVideoEncoderHost::OnResourceMessageReceived( | |
225 const IPC::Message& msg, | |
226 ppapi::host::HostMessageContext* context) { | |
227 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoEncoderHost, msg) | |
bbudge
2015/02/11 23:27:45
handlers in the map should be indented 2 spaces.
c
llandwerlin-old
2015/02/12 15:59:39
Done.
| |
228 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
229 PpapiHostMsg_VideoEncoder_GetSupportedProfiles, | |
230 OnHostMsgGetSupportedProfiles) | |
231 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Initialize, | |
232 OnHostMsgInitialize) | |
233 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoEncoder_GetVideoFrames, | |
234 OnHostMsgGetVideoFrames) | |
235 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Encode, | |
236 OnHostMsgEncode) | |
237 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
238 PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer, | |
239 OnHostMsgRecycleBitstreamBuffer) | |
240 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
241 PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange, | |
242 OnHostMsgRequestEncodingParametersChange) | |
243 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoEncoder_Close, | |
244 OnHostMsgClose) | |
245 PPAPI_END_MESSAGE_MAP() | |
246 return PP_ERROR_FAILED; | |
247 } | |
248 | |
249 int32_t PepperVideoEncoderHost::OnHostMsgGetSupportedProfiles( | |
250 ppapi::host::HostMessageContext* context) { | |
251 std::vector<PP_VideoProfileDescription> pp_profiles; | |
252 GetSupportedProfiles(&pp_profiles); | |
253 | |
254 host()->SendReply( | |
255 context->MakeReplyMessageContext(), | |
256 PpapiPluginMsg_VideoEncoder_GetSupportedProfilesReply(pp_profiles)); | |
257 | |
258 return PP_OK_COMPLETIONPENDING; | |
259 } | |
260 | |
261 int32_t PepperVideoEncoderHost::OnHostMsgInitialize( | |
262 ppapi::host::HostMessageContext* context, | |
263 PP_VideoFrame_Format input_format, | |
264 const PP_Size& input_visible_size, | |
265 PP_VideoProfile output_profile, | |
266 uint32_t initial_bitrate, | |
267 PP_HardwareAcceleration acceleration) { | |
268 if (initialized_) | |
269 return PP_ERROR_FAILED; | |
270 | |
271 media_input_format_ = PP_ToMediaVideoFormat(input_format); | |
272 if (media_input_format_ == media::VideoFrame::UNKNOWN) | |
273 return PP_ERROR_BADARGUMENT; | |
274 | |
275 media::VideoCodecProfile media_profile = | |
276 PP_ToMediaVideoProfile(output_profile); | |
bbudge
2015/02/11 23:27:44
Check that media_profile is valid, and if not, ret
llandwerlin-old
2015/02/12 15:59:40
Thanks, adding a check on the input_size too (it s
| |
277 gfx::Size input_size(input_visible_size.width, input_visible_size.height); | |
278 | |
279 if (!IsInitializationValid(input_visible_size, output_profile, acceleration)) | |
280 return PP_ERROR_NOTSUPPORTED; | |
281 | |
282 initialize_reply_context_ = context->MakeReplyMessageContext(); | |
283 | |
284 if (acceleration == PP_HARDWAREACCELERATION_ONLY || | |
285 acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK) { | |
286 // There is no guarantee that we have a 3D context to work with. So | |
287 // we create a dummy command buffer to communicate with the gpu process. | |
288 channel_ = RenderThreadImpl::current()->EstablishGpuChannelSync( | |
289 CAUSE_FOR_GPU_LAUNCH_PEPPERVIDEOENCODERACCELERATOR_INITIALIZE); | |
290 if (!channel_.get()) | |
291 return PP_ERROR_FAILED; | |
292 | |
293 std::vector<int32> attribs; | |
294 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE); | |
bbudge
2015/02/11 23:27:45
nit: Could be:
std::vector<int32> attribs(1, PP_GR
llandwerlin-old
2015/02/12 15:59:40
Done.
| |
295 | |
296 command_buffer_ = channel_->CreateOffscreenCommandBuffer( | |
297 gfx::Size(), nullptr, attribs, GURL::EmptyGURL(), | |
298 gfx::PreferIntegratedGpu); | |
299 if (!command_buffer_) | |
300 return PP_ERROR_FAILED; | |
301 | |
302 command_buffer_->SetChannelErrorCallback(media::BindToCurrentLoop( | |
303 base::Bind(&PepperVideoEncoderHost::NotifyPepperError, | |
304 weak_ptr_factory_.GetWeakPtr(), PP_ERROR_FAILED))); | |
305 | |
306 if (!command_buffer_->Initialize()) | |
307 return PP_ERROR_FAILED; | |
308 | |
309 encoder_ = command_buffer_->CreateVideoEncoder(); | |
310 | |
311 if (encoder_ && | |
312 encoder_->Initialize(media_input_format_, input_size, media_profile, | |
313 initial_bitrate, this)) { | |
314 return PP_OK_COMPLETIONPENDING; | |
bbudge
2015/02/11 23:27:45
If Initialize() fails encoder_ is in a weird state
| |
315 } | |
316 } | |
317 | |
318 // TODO(llandwerlin): Software encoder. | |
319 return PP_ERROR_NOTSUPPORTED; | |
320 } | |
bbudge
2015/02/11 23:27:45
It would be best if initialized_ was true before e
| |
321 | |
322 int32_t PepperVideoEncoderHost::OnHostMsgGetVideoFrames( | |
323 ppapi::host::HostMessageContext* context) { | |
324 if (encoder_last_error_) | |
325 return encoder_last_error_; | |
326 | |
327 uint32_t frame_length = | |
328 media::VideoFrame::AllocationSize(media_input_format_, input_coded_size_); | |
329 uint32_t buffer_size = frame_length + | |
330 sizeof(ppapi::MediaStreamBuffer::Video) - | |
331 sizeof(ppapi::MediaStreamBuffer::Video::data); | |
332 | |
333 // Make each buffer 4 byte aligned. | |
334 base::CheckedNumeric<int32_t> buffer_size_aligned = buffer_size; | |
335 buffer_size_aligned += (4 - buffer_size % 4); | |
336 | |
337 base::CheckedNumeric<int32_t> total_size = frame_count_ * buffer_size_aligned; | |
338 if (!total_size.IsValid()) | |
339 return PP_ERROR_FAILED; | |
340 | |
341 scoped_ptr<base::SharedMemory> shm( | |
342 RenderThreadImpl::current() | |
343 ->HostAllocateSharedMemoryBuffer(total_size.ValueOrDie()) | |
344 .Pass()); | |
345 if (!shm) | |
346 return PP_ERROR_FAILED; | |
347 | |
348 VLOG(4) << " frame_count=" << frame_count_ << " frame_length=" << frame_length | |
349 << " buffer_size=" << buffer_size_aligned.ValueOrDie(); | |
350 | |
351 if (!buffer_manager_.SetBuffers( | |
352 frame_count_, buffer_size_aligned.ValueOrDie(), shm.Pass(), true)) | |
353 return PP_ERROR_FAILED; | |
354 | |
355 for (int32_t i = 0; i < buffer_manager_.number_of_buffers(); ++i) { | |
356 ppapi::MediaStreamBuffer::Video* buffer = | |
357 &(buffer_manager_.GetBufferPointer(i)->video); | |
358 buffer->header.size = buffer_manager_.buffer_size(); | |
359 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_VIDEO; | |
360 buffer->format = PP_FromMediaVideoFormat(media_input_format_); | |
361 buffer->size.width = input_coded_size_.width(); | |
362 buffer->size.height = input_coded_size_.height(); | |
363 buffer->data_size = frame_length; | |
364 } | |
365 | |
366 ppapi::host::ReplyMessageContext reply_context = | |
367 context->MakeReplyMessageContext(); | |
368 reply_context.params.AppendHandle(SerializedHandle( | |
369 renderer_ppapi_host_->ShareHandleWithRemote( | |
370 ConvertSharedMemoryHandle(*buffer_manager_.shm()), false), | |
371 total_size.ValueOrDie())); | |
372 | |
373 host()->SendReply(reply_context, | |
374 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply( | |
375 frame_count_, buffer_size_aligned.ValueOrDie(), | |
376 PP_FromGfxSize(input_coded_size_))); | |
377 | |
378 return PP_OK_COMPLETIONPENDING; | |
379 } | |
380 | |
381 int32_t PepperVideoEncoderHost::OnHostMsgEncode( | |
382 ppapi::host::HostMessageContext* context, | |
383 uint32_t frame_id, | |
384 bool force_keyframe) { | |
385 if (encoder_last_error_) | |
386 return encoder_last_error_; | |
387 | |
388 if (frame_id >= static_cast<uint32_t>(buffer_manager_.number_of_buffers())) | |
389 return PP_ERROR_FAILED; | |
390 | |
391 encoder_->Encode( | |
392 CreateVideoFrame(frame_id, context->MakeReplyMessageContext()), | |
393 force_keyframe); | |
394 | |
395 return PP_OK_COMPLETIONPENDING; | |
396 } | |
397 | |
398 int32_t PepperVideoEncoderHost::OnHostMsgRecycleBitstreamBuffer( | |
399 ppapi::host::HostMessageContext* context, | |
400 uint32_t buffer_id) { | |
401 if (encoder_last_error_) | |
402 return encoder_last_error_; | |
403 | |
404 if (buffer_id < 0 || | |
405 buffer_id >= shm_buffers_.size() || | |
406 shm_buffers_[buffer_id]->in_use) | |
407 return PP_ERROR_FAILED; | |
408 | |
409 shm_buffers_[buffer_id]->in_use = true; | |
410 encoder_->UseOutputBitstreamBuffer( | |
411 shm_buffers_[buffer_id]->toBitstreamBuffer()); | |
412 | |
413 return PP_OK; | |
414 } | |
415 | |
416 int32_t PepperVideoEncoderHost::OnHostMsgRequestEncodingParametersChange( | |
417 ppapi::host::HostMessageContext* context, | |
418 uint32_t bitrate, | |
419 uint32_t framerate) { | |
420 if (encoder_last_error_) | |
421 return encoder_last_error_; | |
422 | |
423 encoder_->RequestEncodingParametersChange(bitrate, framerate); | |
424 | |
425 return PP_OK; | |
426 } | |
427 | |
428 int32_t PepperVideoEncoderHost::OnHostMsgClose( | |
429 ppapi::host::HostMessageContext* context) { | |
430 encoder_last_error_ = PP_ERROR_ABORTED; | |
431 encoder_->Destroy(); | |
432 base::Unretained(encoder_.release()); | |
bbudge
2015/02/11 23:27:45
if (encoder_)
encoder_.release()->Destroy();
I
llandwerlin-old
2015/02/12 15:59:40
Yeah, looking again at the receiving end of the VE
| |
433 | |
434 return PP_OK; | |
435 } | |
436 | |
437 void PepperVideoEncoderHost::RequireBitstreamBuffers( | |
438 unsigned int frame_count, | |
439 const gfx::Size& input_coded_size, | |
440 size_t output_buffer_size) { | |
441 DCHECK(RenderThreadImpl::current()); | |
bbudge
2015/02/11 23:27:45
Rereading the comments on VEA, it implies that thi
bbudge
2015/02/12 02:25:36
As discussed on email, with the current VEA code,
llandwerlin-old
2015/02/12 15:59:39
Ok.
But from the Resource point of view, the initi
bbudge
2015/02/12 18:18:11
I don't see why it has to be that way. The plugin
| |
442 | |
443 for (int32_t i = 0; i < kDefaultNumberOfBitstreamBuffers; ++i) { | |
444 scoped_ptr<base::SharedMemory> shm( | |
445 RenderThread::Get() | |
446 ->HostAllocateSharedMemoryBuffer(output_buffer_size) | |
447 .Pass()); | |
448 | |
449 if (!shm || !shm->Map(output_buffer_size)) { | |
450 shm_buffers_.clear(); | |
451 break; | |
452 } | |
453 | |
454 shm_buffers_.push_back(new ShmBuffer(i, shm.Pass(), output_buffer_size)); | |
455 | |
456 initialize_reply_context_.params.AppendHandle( | |
457 ppapi::proxy::SerializedHandle( | |
458 renderer_ppapi_host_->ShareHandleWithRemote( | |
459 ConvertSharedMemoryHandle(*shm_buffers_.back()->shm), false), | |
460 output_buffer_size)); | |
461 } | |
462 | |
463 if (shm_buffers_.empty()) { | |
464 initialize_reply_context_.params.set_result(PP_ERROR_FAILED); | |
465 host()->SendReply( | |
466 initialize_reply_context_, | |
467 PpapiPluginMsg_VideoEncoder_InitializeReply(0, 0, PP_MakeSize(0, 0))); | |
468 return; | |
469 } | |
470 | |
471 // Feed buffers to the encoder. | |
472 for (size_t i = 0; i < shm_buffers_.size(); ++i) { | |
473 encoder_->UseOutputBitstreamBuffer(shm_buffers_[i]->toBitstreamBuffer()); | |
474 } | |
475 | |
476 // Notify the plugin of the buffers and the input size. | |
477 PP_Size size = | |
478 PP_MakeSize(input_coded_size.width(), input_coded_size.height()); | |
479 input_coded_size_ = input_coded_size; | |
480 frame_count_ = frame_count; | |
481 initialized_ = true; | |
482 encoder_last_error_ = PP_OK; | |
483 | |
484 host()->SendReply(initialize_reply_context_, | |
485 PpapiPluginMsg_VideoEncoder_InitializeReply( | |
486 output_buffer_size, frame_count, size)); | |
487 } | |
488 | |
489 void PepperVideoEncoderHost::BitstreamBufferReady(int32 buffer_id, | |
490 size_t payload_size, | |
491 bool key_frame) { | |
492 DCHECK(RenderThreadImpl::current()); | |
493 DCHECK(shm_buffers_[buffer_id]->in_use); | |
494 | |
495 shm_buffers_[buffer_id]->in_use = false; | |
496 host()->SendUnsolicitedReply(pp_resource(), | |
497 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady( | |
498 buffer_id, payload_size, key_frame)); | |
499 } | |
500 | |
501 void PepperVideoEncoderHost::NotifyError( | |
502 media::VideoEncodeAccelerator::Error error) { | |
503 DCHECK(RenderThreadImpl::current()); | |
504 NotifyPepperError(PP_FromMediaEncodeAcceleratorError(error)); | |
505 } | |
506 | |
507 void PepperVideoEncoderHost::GetSupportedProfiles( | |
508 std::vector<PP_VideoProfileDescription>* pp_profiles) { | |
509 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles = | |
510 RenderThreadImpl::current() | |
511 ->GetGpuFactories() | |
512 ->GetVideoEncodeAcceleratorSupportedProfiles(); | |
513 for (media::VideoEncodeAccelerator::SupportedProfile profile : profiles) | |
514 pp_profiles->push_back(PP_FromVideoEncodeAcceleratorSupportedProfile( | |
515 profile, PP_HARDWAREACCELERATION_ONLY)); | |
516 | |
517 // TODO(llandwerlin): merge software supported profiles. | |
518 } | |
519 | |
520 bool PepperVideoEncoderHost::IsInitializationValid( | |
521 const PP_Size& input_size, | |
522 PP_VideoProfile output_profile, | |
523 PP_HardwareAcceleration acceleration) { | |
524 std::vector<PP_VideoProfileDescription> profiles; | |
525 GetSupportedProfiles(&profiles); | |
526 | |
527 for (const PP_VideoProfileDescription& profile : profiles) { | |
528 if (output_profile == profile.profile && | |
529 input_size.width <= profile.max_resolution.width && | |
530 input_size.height <= profile.max_resolution.height && | |
531 PP_HardwareAccelerationCompatible(profile.acceleration, acceleration)) | |
532 return true; | |
533 } | |
534 | |
535 return false; | |
536 } | |
537 | |
538 scoped_refptr<media::VideoFrame> PepperVideoEncoderHost::CreateVideoFrame( | |
539 uint32_t frame_id, | |
540 const ppapi::host::ReplyMessageContext& reply_context) { | |
541 DCHECK_LT(frame_id, | |
542 static_cast<uint32_t>(buffer_manager_.number_of_buffers())); | |
543 | |
544 ppapi::MediaStreamBuffer* buffer = buffer_manager_.GetBufferPointer(frame_id); | |
545 uint32_t shm_offset = frame_id * buffer_manager_.buffer_size() + | |
546 sizeof(ppapi::MediaStreamBuffer::Video) - | |
547 sizeof(ppapi::MediaStreamBuffer::Video::data); | |
548 | |
549 return media::VideoFrame::WrapExternalPackedMemory( | |
550 media_input_format_, input_coded_size_, gfx::Rect(input_coded_size_), | |
551 input_coded_size_, static_cast<uint8*>(buffer->video.data), | |
552 buffer->video.data_size, buffer_manager_.shm()->handle(), shm_offset, | |
553 base::TimeDelta(), base::Bind( | |
554 &PepperVideoEncoderHost::EncodeDone, | |
555 weak_ptr_factory_.GetWeakPtr(), reply_context, frame_id)); | |
556 } | |
557 | |
558 void PepperVideoEncoderHost::EncodeDone( | |
559 const ppapi::host::ReplyMessageContext& reply_context, | |
560 uint32_t frame_id) { | |
561 DCHECK(RenderThreadImpl::current()); | |
562 | |
563 ppapi::host::ReplyMessageContext context = reply_context; | |
564 context.params.set_result(encoder_last_error_); | |
565 host()->SendReply(reply_context, | |
566 PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id)); | |
567 } | |
568 | |
569 void PepperVideoEncoderHost::NotifyPepperError(int32_t error) { | |
570 DCHECK(RenderThreadImpl::current()); | |
571 | |
572 encoder_last_error_ = error; | |
573 base::Unretained(encoder_.release()); | |
574 host()->SendUnsolicitedReply( | |
575 pp_resource(), | |
576 PpapiPluginMsg_VideoEncoder_NotifyError(encoder_last_error_)); | |
577 } | |
578 | |
579 } // namespace content | |
OLD | NEW |