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; | |
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. | |
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(int32_t id, | |
185 scoped_ptr<base::SharedMemory> shm) | |
186 : id(id), shm(shm.Pass()), in_use(true) { | |
187 DCHECK(this->shm); | |
188 } | |
189 | |
190 PepperVideoEncoderHost::ShmBuffer::~ShmBuffer() { | |
191 } | |
192 | |
193 media::BitstreamBuffer PepperVideoEncoderHost::ShmBuffer::ToBitstreamBuffer() { | |
194 return media::BitstreamBuffer(id, shm->handle(), shm->mapped_size()); | |
195 } | |
196 | |
197 PepperVideoEncoderHost::PepperVideoEncoderHost(RendererPpapiHost* host, | |
198 PP_Instance instance, | |
199 PP_Resource resource) | |
200 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
201 renderer_ppapi_host_(host), | |
202 buffer_manager_(this), | |
203 command_buffer_(nullptr), | |
204 initialized_(false), | |
205 encoder_last_error_(PP_ERROR_FAILED), | |
206 frame_count_(0), | |
207 media_input_format_(media::VideoFrame::UNKNOWN), | |
208 weak_ptr_factory_(this) { | |
209 } | |
210 | |
211 PepperVideoEncoderHost::~PepperVideoEncoderHost() { | |
212 Close(); | |
213 } | |
214 | |
215 int32_t PepperVideoEncoderHost::OnResourceMessageReceived( | |
216 const IPC::Message& msg, | |
217 ppapi::host::HostMessageContext* context) { | |
218 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoEncoderHost, msg) | |
219 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
220 PpapiHostMsg_VideoEncoder_GetSupportedProfiles, | |
221 OnHostMsgGetSupportedProfiles) | |
222 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Initialize, | |
223 OnHostMsgInitialize) | |
224 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
225 PpapiHostMsg_VideoEncoder_GetVideoFrames, | |
226 OnHostMsgGetVideoFrames) | |
227 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Encode, | |
228 OnHostMsgEncode) | |
229 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
230 PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer, | |
231 OnHostMsgRecycleBitstreamBuffer) | |
232 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
233 PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange, | |
234 OnHostMsgRequestEncodingParametersChange) | |
235 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoEncoder_Close, | |
236 OnHostMsgClose) | |
237 PPAPI_END_MESSAGE_MAP() | |
238 return PP_ERROR_FAILED; | |
239 } | |
240 | |
241 int32_t PepperVideoEncoderHost::OnHostMsgGetSupportedProfiles( | |
242 ppapi::host::HostMessageContext* context) { | |
243 std::vector<PP_VideoProfileDescription> pp_profiles; | |
244 GetSupportedProfiles(&pp_profiles); | |
245 | |
246 host()->SendReply( | |
247 context->MakeReplyMessageContext(), | |
248 PpapiPluginMsg_VideoEncoder_GetSupportedProfilesReply(pp_profiles)); | |
249 | |
250 return PP_OK_COMPLETIONPENDING; | |
251 } | |
252 | |
253 int32_t PepperVideoEncoderHost::OnHostMsgInitialize( | |
254 ppapi::host::HostMessageContext* context, | |
255 PP_VideoFrame_Format input_format, | |
256 const PP_Size& input_visible_size, | |
257 PP_VideoProfile output_profile, | |
258 uint32_t initial_bitrate, | |
259 PP_HardwareAcceleration acceleration) { | |
260 if (initialized_) | |
261 return PP_ERROR_FAILED; | |
262 | |
263 media_input_format_ = PP_ToMediaVideoFormat(input_format); | |
264 if (media_input_format_ == media::VideoFrame::UNKNOWN) | |
265 return PP_ERROR_BADARGUMENT; | |
266 | |
267 media::VideoCodecProfile media_profile = | |
268 PP_ToMediaVideoProfile(output_profile); | |
269 if (media_profile == media::VIDEO_CODEC_PROFILE_UNKNOWN) | |
270 return PP_ERROR_BADARGUMENT; | |
271 | |
272 gfx::Size input_size(input_visible_size.width, input_visible_size.height); | |
273 if (input_size.IsEmpty()) | |
274 return PP_ERROR_BADARGUMENT; | |
275 | |
276 if (!IsInitializationValid(input_visible_size, output_profile, acceleration)) | |
277 return PP_ERROR_NOTSUPPORTED; | |
278 | |
279 int32_t error = PP_ERROR_NOTSUPPORTED; | |
280 initialize_reply_context_ = context->MakeReplyMessageContext(); | |
281 | |
282 if (acceleration == PP_HARDWAREACCELERATION_ONLY || | |
283 acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK) { | |
284 if (InitializeHardware(media_input_format_, input_size, media_profile, | |
285 initial_bitrate)) | |
286 return PP_OK_COMPLETIONPENDING; | |
287 | |
288 if (acceleration == PP_HARDWAREACCELERATION_ONLY) | |
289 error = PP_ERROR_FAILED; | |
290 } | |
291 | |
292 // TODO(llandwerlin): Software encoder. | |
293 initialize_reply_context_ = ppapi::host::ReplyMessageContext(); | |
294 Close(); | |
295 return error; | |
296 } | |
297 | |
298 int32_t PepperVideoEncoderHost::OnHostMsgGetVideoFrames( | |
299 ppapi::host::HostMessageContext* context) { | |
300 if (encoder_last_error_) | |
301 return encoder_last_error_; | |
302 | |
303 get_video_frames_reply_context_ = context->MakeReplyMessageContext(); | |
304 AllocateVideoFrames(); | |
305 | |
306 return PP_OK_COMPLETIONPENDING; | |
307 } | |
308 | |
309 int32_t PepperVideoEncoderHost::OnHostMsgEncode( | |
310 ppapi::host::HostMessageContext* context, | |
311 uint32_t frame_id, | |
312 bool force_keyframe) { | |
313 if (encoder_last_error_) | |
314 return encoder_last_error_; | |
315 | |
316 if (frame_id >= frame_count_) | |
bbudge
2015/02/20 15:56:13
In theory a malicious plugin could try to get us t
| |
317 return PP_ERROR_FAILED; | |
318 | |
319 encoder_->Encode( | |
320 CreateVideoFrame(frame_id, context->MakeReplyMessageContext()), | |
321 force_keyframe); | |
322 | |
323 return PP_OK_COMPLETIONPENDING; | |
324 } | |
325 | |
326 int32_t PepperVideoEncoderHost::OnHostMsgRecycleBitstreamBuffer( | |
327 ppapi::host::HostMessageContext* context, | |
328 uint32_t buffer_id) { | |
329 if (encoder_last_error_) | |
330 return encoder_last_error_; | |
331 | |
332 if (buffer_id >= shm_buffers_.size() || shm_buffers_[buffer_id]->in_use) | |
333 return PP_ERROR_FAILED; | |
334 | |
335 shm_buffers_[buffer_id]->in_use = true; | |
336 encoder_->UseOutputBitstreamBuffer( | |
337 shm_buffers_[buffer_id]->ToBitstreamBuffer()); | |
338 | |
339 return PP_OK; | |
340 } | |
341 | |
342 int32_t PepperVideoEncoderHost::OnHostMsgRequestEncodingParametersChange( | |
343 ppapi::host::HostMessageContext* context, | |
344 uint32_t bitrate, | |
345 uint32_t framerate) { | |
346 if (encoder_last_error_) | |
347 return encoder_last_error_; | |
348 | |
349 encoder_->RequestEncodingParametersChange(bitrate, framerate); | |
350 | |
351 return PP_OK; | |
352 } | |
353 | |
354 int32_t PepperVideoEncoderHost::OnHostMsgClose( | |
355 ppapi::host::HostMessageContext* context) { | |
356 encoder_last_error_ = PP_ERROR_FAILED; | |
357 Close(); | |
358 | |
359 return PP_OK; | |
360 } | |
361 | |
362 void PepperVideoEncoderHost::RequireBitstreamBuffers( | |
363 unsigned int frame_count, | |
364 const gfx::Size& input_coded_size, | |
365 size_t output_buffer_size) { | |
366 DCHECK(RenderThreadImpl::current()); | |
367 // We assume RequireBitstreamBuffers is only called once. | |
368 DCHECK(!initialized_); | |
369 | |
370 input_coded_size_ = input_coded_size; | |
371 frame_count_ = frame_count; | |
372 | |
373 for (int32_t i = 0; i < kDefaultNumberOfBitstreamBuffers; ++i) { | |
374 scoped_ptr<base::SharedMemory> shm( | |
375 RenderThread::Get() | |
376 ->HostAllocateSharedMemoryBuffer(output_buffer_size) | |
377 .Pass()); | |
378 | |
379 if (!shm || !shm->Map(output_buffer_size)) { | |
380 shm_buffers_.clear(); | |
381 break; | |
382 } | |
383 | |
384 shm_buffers_.push_back(new ShmBuffer(i, shm.Pass())); | |
385 } | |
386 | |
387 // Feed buffers to the encoder. | |
388 std::vector<SerializedHandle> handles; | |
389 for (size_t i = 0; i < shm_buffers_.size(); ++i) { | |
390 encoder_->UseOutputBitstreamBuffer(shm_buffers_[i]->ToBitstreamBuffer()); | |
391 handles.push_back(SerializedHandle( | |
392 renderer_ppapi_host_->ShareHandleWithRemote( | |
393 ConvertSharedMemoryHandle(*shm_buffers_[i]->shm), false), | |
394 output_buffer_size)); | |
395 } | |
396 | |
397 host()->SendUnsolicitedReplyWithHandles( | |
398 pp_resource(), PpapiPluginMsg_VideoEncoder_BitstreamBuffers( | |
399 static_cast<uint32_t>(output_buffer_size)), | |
400 handles); | |
401 | |
402 if (!initialized_) { | |
403 // Tell the plugin that initialization has been successful if we | |
404 // haven't already. | |
405 initialized_ = true; | |
406 encoder_last_error_ = PP_OK; | |
407 host()->SendReply(initialize_reply_context_, | |
408 PpapiPluginMsg_VideoEncoder_InitializeReply( | |
409 frame_count, PP_FromGfxSize(input_coded_size))); | |
410 } | |
411 | |
412 if (shm_buffers_.empty()) { | |
413 NotifyPepperError(PP_ERROR_NOMEMORY); | |
414 return; | |
415 } | |
416 | |
417 // If the plugin already requested video frames, we can now answer | |
418 // that request. | |
419 if (get_video_frames_reply_context_.is_valid()) | |
420 AllocateVideoFrames(); | |
421 } | |
422 | |
423 void PepperVideoEncoderHost::BitstreamBufferReady(int32 buffer_id, | |
424 size_t payload_size, | |
425 bool key_frame) { | |
426 DCHECK(RenderThreadImpl::current()); | |
427 DCHECK(shm_buffers_[buffer_id]->in_use); | |
428 | |
429 shm_buffers_[buffer_id]->in_use = false; | |
430 host()->SendUnsolicitedReply( | |
431 pp_resource(), | |
432 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady( | |
433 buffer_id, static_cast<uint32_t>(payload_size), key_frame)); | |
434 } | |
435 | |
436 void PepperVideoEncoderHost::NotifyError( | |
437 media::VideoEncodeAccelerator::Error error) { | |
438 DCHECK(RenderThreadImpl::current()); | |
439 NotifyPepperError(PP_FromMediaEncodeAcceleratorError(error)); | |
440 } | |
441 | |
442 void PepperVideoEncoderHost::GetSupportedProfiles( | |
443 std::vector<PP_VideoProfileDescription>* pp_profiles) { | |
444 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles = | |
445 RenderThreadImpl::current() | |
446 ->GetGpuFactories() | |
447 ->GetVideoEncodeAcceleratorSupportedProfiles(); | |
448 for (media::VideoEncodeAccelerator::SupportedProfile profile : profiles) | |
449 pp_profiles->push_back(PP_FromVideoEncodeAcceleratorSupportedProfile( | |
450 profile, PP_HARDWAREACCELERATION_ONLY)); | |
451 | |
452 // TODO(llandwerlin): add software supported profiles. | |
453 } | |
454 | |
455 bool PepperVideoEncoderHost::IsInitializationValid( | |
456 const PP_Size& input_size, | |
457 PP_VideoProfile output_profile, | |
458 PP_HardwareAcceleration acceleration) { | |
459 std::vector<PP_VideoProfileDescription> profiles; | |
460 GetSupportedProfiles(&profiles); | |
461 | |
462 for (const PP_VideoProfileDescription& profile : profiles) { | |
463 if (output_profile == profile.profile && | |
464 input_size.width <= profile.max_resolution.width && | |
465 input_size.height <= profile.max_resolution.height && | |
466 PP_HardwareAccelerationCompatible(profile.acceleration, acceleration)) | |
467 return true; | |
468 } | |
469 | |
470 return false; | |
471 } | |
472 | |
473 bool PepperVideoEncoderHost::InitializeHardware( | |
474 media::VideoFrame::Format input_format, | |
475 const gfx::Size& input_visible_size, | |
476 media::VideoCodecProfile output_profile, | |
477 uint32_t initial_bitrate) { | |
478 // There is no guarantee that we have a 3D context to work with. So | |
479 // we create a dummy command buffer to communicate with the gpu process. | |
480 channel_ = RenderThreadImpl::current()->EstablishGpuChannelSync( | |
481 CAUSE_FOR_GPU_LAUNCH_PEPPERVIDEOENCODERACCELERATOR_INITIALIZE); | |
482 if (!channel_) | |
483 return false; | |
484 | |
485 std::vector<int32> attribs(1, PP_GRAPHICS3DATTRIB_NONE); | |
486 | |
487 command_buffer_ = channel_->CreateOffscreenCommandBuffer( | |
488 gfx::Size(), nullptr, attribs, GURL::EmptyGURL(), | |
489 gfx::PreferIntegratedGpu); | |
490 if (!command_buffer_) | |
491 return false; | |
492 | |
493 command_buffer_->SetChannelErrorCallback(media::BindToCurrentLoop( | |
494 base::Bind(&PepperVideoEncoderHost::NotifyPepperError, | |
495 weak_ptr_factory_.GetWeakPtr(), PP_ERROR_RESOURCE_FAILED))); | |
496 if (!command_buffer_->Initialize()) | |
497 return false; | |
498 | |
499 encoder_ = command_buffer_->CreateVideoEncoder(); | |
500 if (!encoder_ || | |
501 !encoder_->Initialize(input_format, input_visible_size, output_profile, | |
502 initial_bitrate, this)) | |
503 return false; | |
504 | |
505 return true; | |
506 } | |
507 | |
508 void PepperVideoEncoderHost::Close() { | |
509 DCHECK(RenderThreadImpl::current()); | |
510 | |
511 encoder_ = nullptr; | |
512 if (command_buffer_) { | |
513 DCHECK(channel_); | |
514 channel_->DestroyCommandBuffer(command_buffer_); | |
515 command_buffer_ = nullptr; | |
516 } | |
517 } | |
518 | |
519 void PepperVideoEncoderHost::AllocateVideoFrames() { | |
520 // Frames have already been allocated. | |
521 if (buffer_manager_.number_of_buffers() > 0) { | |
522 SendGetFramesErrorReply(PP_ERROR_FAILED); | |
523 NOTREACHED(); | |
524 return; | |
525 } | |
526 | |
527 base::CheckedNumeric<uint32_t> size = | |
528 media::VideoFrame::AllocationSize(media_input_format_, input_coded_size_); | |
529 uint32_t frame_size = size.ValueOrDie(); | |
530 size += sizeof(ppapi::MediaStreamBuffer::Video) - | |
531 sizeof(ppapi::MediaStreamBuffer::Video::data); | |
532 uint32_t buffer_size = size.ValueOrDie(); | |
533 // Make each buffer 4 byte aligned. | |
534 size += (4 - buffer_size % 4); | |
535 uint32_t buffer_size_aligned = size.ValueOrDie(); | |
536 size *= frame_count_; | |
537 uint32_t total_size = size.ValueOrDie(); | |
538 | |
539 if (!size.IsValid()) { | |
bbudge
2015/02/20 15:56:13
Since we didn't 'Die' above, size must be valid, s
llandwerlin-old
2015/02/20 16:10:52
Done.
| |
540 SendGetFramesErrorReply(PP_ERROR_FAILED); | |
541 return; | |
542 } | |
543 | |
544 scoped_ptr<base::SharedMemory> shm( | |
545 RenderThreadImpl::current() | |
546 ->HostAllocateSharedMemoryBuffer(total_size) | |
547 .Pass()); | |
548 if (!shm) { | |
549 SendGetFramesErrorReply(PP_ERROR_NOMEMORY); | |
550 return; | |
551 } | |
552 | |
553 VLOG(4) << " frame_count=" << frame_count_ << " frame_size=" << frame_size | |
554 << " buffer_size=" << buffer_size_aligned; | |
555 | |
556 if (!buffer_manager_.SetBuffers(frame_count_, buffer_size_aligned, shm.Pass(), | |
557 true)) { | |
558 SendGetFramesErrorReply(PP_ERROR_FAILED); | |
559 return; | |
560 } | |
bbudge
2015/02/20 15:56:13
If you remove the first SendGetFramesErrorReply, t
llandwerlin-old
2015/02/20 16:10:52
Should I drop the very first SendGetFramesErrorRep
| |
561 | |
562 for (int32_t i = 0; i < buffer_manager_.number_of_buffers(); ++i) { | |
563 ppapi::MediaStreamBuffer::Video* buffer = | |
564 &(buffer_manager_.GetBufferPointer(i)->video); | |
565 buffer->header.size = buffer_manager_.buffer_size(); | |
566 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_VIDEO; | |
567 buffer->format = PP_FromMediaVideoFormat(media_input_format_); | |
568 buffer->size.width = input_coded_size_.width(); | |
569 buffer->size.height = input_coded_size_.height(); | |
570 buffer->data_size = frame_size; | |
571 } | |
572 | |
573 DCHECK(get_video_frames_reply_context_.is_valid()); | |
574 get_video_frames_reply_context_.params.AppendHandle(SerializedHandle( | |
575 renderer_ppapi_host_->ShareHandleWithRemote( | |
576 ConvertSharedMemoryHandle(*buffer_manager_.shm()), false), | |
577 total_size)); | |
578 | |
579 host()->SendReply(get_video_frames_reply_context_, | |
580 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply( | |
581 frame_count_, buffer_size_aligned, | |
582 PP_FromGfxSize(input_coded_size_))); | |
583 get_video_frames_reply_context_ = ppapi::host::ReplyMessageContext(); | |
584 } | |
585 | |
586 void PepperVideoEncoderHost::SendGetFramesErrorReply(int32_t error) { | |
587 DCHECK(get_video_frames_reply_context_.is_valid()); | |
588 | |
589 get_video_frames_reply_context_.params.set_result(error); | |
590 host()->SendReply( | |
591 get_video_frames_reply_context_, | |
592 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(0, 0, PP_MakeSize(0, 0))); | |
593 get_video_frames_reply_context_ = ppapi::host::ReplyMessageContext(); | |
594 } | |
595 | |
596 scoped_refptr<media::VideoFrame> PepperVideoEncoderHost::CreateVideoFrame( | |
597 uint32_t frame_id, | |
598 const ppapi::host::ReplyMessageContext& reply_context) { | |
599 ppapi::MediaStreamBuffer* buffer = buffer_manager_.GetBufferPointer(frame_id); | |
600 DCHECK(buffer); | |
601 uint32_t shm_offset = frame_id * buffer_manager_.buffer_size() + | |
602 sizeof(ppapi::MediaStreamBuffer::Video) - | |
603 sizeof(ppapi::MediaStreamBuffer::Video::data); | |
604 | |
605 return media::VideoFrame::WrapExternalPackedMemory( | |
606 media_input_format_, input_coded_size_, gfx::Rect(input_coded_size_), | |
607 input_coded_size_, static_cast<uint8*>(buffer->video.data), | |
608 buffer->video.data_size, buffer_manager_.shm()->handle(), shm_offset, | |
609 base::TimeDelta(), | |
610 base::Bind(&PepperVideoEncoderHost::FrameReleased, | |
611 weak_ptr_factory_.GetWeakPtr(), reply_context, frame_id)); | |
612 } | |
613 | |
614 void PepperVideoEncoderHost::FrameReleased( | |
615 const ppapi::host::ReplyMessageContext& reply_context, | |
616 uint32_t frame_id) { | |
617 DCHECK(RenderThreadImpl::current()); | |
618 | |
619 ppapi::host::ReplyMessageContext context = reply_context; | |
620 context.params.set_result(encoder_last_error_); | |
621 host()->SendReply(context, PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id)); | |
622 } | |
623 | |
624 void PepperVideoEncoderHost::NotifyPepperError(int32_t error) { | |
625 DCHECK(RenderThreadImpl::current()); | |
626 | |
627 encoder_last_error_ = error; | |
628 Close(); | |
629 host()->SendUnsolicitedReply( | |
630 pp_resource(), | |
631 PpapiPluginMsg_VideoEncoder_NotifyError(encoder_last_error_)); | |
632 } | |
633 | |
634 } // namespace content | |
OLD | NEW |