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.
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/common/gpu/media/gpu_video_accelerator_util.h"
10 #include "content/common/pepper_file_util.h"
11 #include "content/public/renderer/renderer_ppapi_host.h"
12 #include "content/renderer/pepper/gfx_conversion.h"
13 #include "content/renderer/pepper/host_globals.h"
14 #include "content/renderer/pepper/pepper_video_encoder_host.h"
15 #include "content/renderer/pepper/video_encoder_shim.h"
16 #include "content/renderer/render_thread_impl.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/video_frame.h"
19 #include "media/renderers/gpu_video_accelerator_factories.h"
20 #include "media/video/video_encode_accelerator.h"
21 #include "ppapi/c/pp_codecs.h"
22 #include "ppapi/c/pp_errors.h"
23 #include "ppapi/c/pp_graphics_3d.h"
24 #include "ppapi/host/dispatch_host_message.h"
25 #include "ppapi/host/ppapi_host.h"
26 #include "ppapi/proxy/ppapi_messages.h"
27 #include "ppapi/shared_impl/media_stream_buffer.h"
29 using ppapi::proxy::SerializedHandle
;
35 const uint32_t kDefaultNumberOfBitstreamBuffers
= 4;
37 int32_t PP_FromMediaEncodeAcceleratorError(
38 media::VideoEncodeAccelerator::Error error
) {
40 case media::VideoEncodeAccelerator::kInvalidArgumentError
:
41 return PP_ERROR_MALFORMED_INPUT
;
42 case media::VideoEncodeAccelerator::kIllegalStateError
:
43 case media::VideoEncodeAccelerator::kPlatformFailureError
:
44 return PP_ERROR_RESOURCE_FAILED
;
45 // No default case, to catch unhandled enum values.
47 return PP_ERROR_FAILED
;
50 // TODO(llandwerlin): move following to media_conversion.cc/h?
51 media::VideoCodecProfile
PP_ToMediaVideoProfile(PP_VideoProfile profile
) {
53 case PP_VIDEOPROFILE_H264BASELINE
:
54 return media::H264PROFILE_BASELINE
;
55 case PP_VIDEOPROFILE_H264MAIN
:
56 return media::H264PROFILE_MAIN
;
57 case PP_VIDEOPROFILE_H264EXTENDED
:
58 return media::H264PROFILE_EXTENDED
;
59 case PP_VIDEOPROFILE_H264HIGH
:
60 return media::H264PROFILE_HIGH
;
61 case PP_VIDEOPROFILE_H264HIGH10PROFILE
:
62 return media::H264PROFILE_HIGH10PROFILE
;
63 case PP_VIDEOPROFILE_H264HIGH422PROFILE
:
64 return media::H264PROFILE_HIGH422PROFILE
;
65 case PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE
:
66 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE
;
67 case PP_VIDEOPROFILE_H264SCALABLEBASELINE
:
68 return media::H264PROFILE_SCALABLEBASELINE
;
69 case PP_VIDEOPROFILE_H264SCALABLEHIGH
:
70 return media::H264PROFILE_SCALABLEHIGH
;
71 case PP_VIDEOPROFILE_H264STEREOHIGH
:
72 return media::H264PROFILE_STEREOHIGH
;
73 case PP_VIDEOPROFILE_H264MULTIVIEWHIGH
:
74 return media::H264PROFILE_MULTIVIEWHIGH
;
75 case PP_VIDEOPROFILE_VP8_ANY
:
76 return media::VP8PROFILE_ANY
;
77 case PP_VIDEOPROFILE_VP9_ANY
:
78 return media::VP9PROFILE_ANY
;
79 // No default case, to catch unhandled PP_VideoProfile values.
81 return media::VIDEO_CODEC_PROFILE_UNKNOWN
;
84 PP_VideoProfile
PP_FromMediaVideoProfile(media::VideoCodecProfile profile
) {
86 case media::H264PROFILE_BASELINE
:
87 return PP_VIDEOPROFILE_H264BASELINE
;
88 case media::H264PROFILE_MAIN
:
89 return PP_VIDEOPROFILE_H264MAIN
;
90 case media::H264PROFILE_EXTENDED
:
91 return PP_VIDEOPROFILE_H264EXTENDED
;
92 case media::H264PROFILE_HIGH
:
93 return PP_VIDEOPROFILE_H264HIGH
;
94 case media::H264PROFILE_HIGH10PROFILE
:
95 return PP_VIDEOPROFILE_H264HIGH10PROFILE
;
96 case media::H264PROFILE_HIGH422PROFILE
:
97 return PP_VIDEOPROFILE_H264HIGH422PROFILE
;
98 case media::H264PROFILE_HIGH444PREDICTIVEPROFILE
:
99 return PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE
;
100 case media::H264PROFILE_SCALABLEBASELINE
:
101 return PP_VIDEOPROFILE_H264SCALABLEBASELINE
;
102 case media::H264PROFILE_SCALABLEHIGH
:
103 return PP_VIDEOPROFILE_H264SCALABLEHIGH
;
104 case media::H264PROFILE_STEREOHIGH
:
105 return PP_VIDEOPROFILE_H264STEREOHIGH
;
106 case media::H264PROFILE_MULTIVIEWHIGH
:
107 return PP_VIDEOPROFILE_H264MULTIVIEWHIGH
;
108 case media::VP8PROFILE_ANY
:
109 return PP_VIDEOPROFILE_VP8_ANY
;
110 case media::VP9PROFILE_ANY
:
111 return PP_VIDEOPROFILE_VP9_ANY
;
114 return static_cast<PP_VideoProfile
>(-1);
118 media::VideoPixelFormat
PP_ToMediaVideoFormat(PP_VideoFrame_Format format
) {
120 case PP_VIDEOFRAME_FORMAT_UNKNOWN
:
121 return media::PIXEL_FORMAT_UNKNOWN
;
122 case PP_VIDEOFRAME_FORMAT_YV12
:
123 return media::PIXEL_FORMAT_YV12
;
124 case PP_VIDEOFRAME_FORMAT_I420
:
125 return media::PIXEL_FORMAT_I420
;
126 case PP_VIDEOFRAME_FORMAT_BGRA
:
127 return media::PIXEL_FORMAT_UNKNOWN
;
128 // No default case, to catch unhandled PP_VideoFrame_Format values.
130 return media::PIXEL_FORMAT_UNKNOWN
;
133 PP_VideoFrame_Format
PP_FromMediaVideoFormat(media::VideoPixelFormat format
) {
135 case media::PIXEL_FORMAT_UNKNOWN
:
136 return PP_VIDEOFRAME_FORMAT_UNKNOWN
;
137 case media::PIXEL_FORMAT_YV12
:
138 return PP_VIDEOFRAME_FORMAT_YV12
;
139 case media::PIXEL_FORMAT_I420
:
140 return PP_VIDEOFRAME_FORMAT_I420
;
142 return PP_VIDEOFRAME_FORMAT_UNKNOWN
;
146 PP_VideoProfileDescription
PP_FromVideoEncodeAcceleratorSupportedProfile(
147 media::VideoEncodeAccelerator::SupportedProfile profile
,
148 PP_Bool hardware_accelerated
) {
149 PP_VideoProfileDescription pp_profile
;
150 pp_profile
.profile
= PP_FromMediaVideoProfile(profile
.profile
);
151 pp_profile
.max_resolution
= PP_FromGfxSize(profile
.max_resolution
);
152 pp_profile
.max_framerate_numerator
= profile
.max_framerate_numerator
;
153 pp_profile
.max_framerate_denominator
= profile
.max_framerate_denominator
;
154 pp_profile
.hardware_accelerated
= hardware_accelerated
;
158 bool PP_HardwareAccelerationCompatible(bool accelerated
,
159 PP_HardwareAcceleration requested
) {
161 case PP_HARDWAREACCELERATION_ONLY
:
163 case PP_HARDWAREACCELERATION_NONE
:
165 case PP_HARDWAREACCELERATION_WITHFALLBACK
:
167 // No default case, to catch unhandled PP_HardwareAcceleration values.
174 PepperVideoEncoderHost::ShmBuffer::ShmBuffer(uint32_t id
,
175 scoped_ptr
<base::SharedMemory
> shm
)
176 : id(id
), shm(shm
.Pass()), in_use(true) {
180 PepperVideoEncoderHost::ShmBuffer::~ShmBuffer() {
183 media::BitstreamBuffer
PepperVideoEncoderHost::ShmBuffer::ToBitstreamBuffer() {
184 return media::BitstreamBuffer(id
, shm
->handle(), shm
->mapped_size());
187 PepperVideoEncoderHost::PepperVideoEncoderHost(RendererPpapiHost
* host
,
188 PP_Instance instance
,
189 PP_Resource resource
)
190 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
191 renderer_ppapi_host_(host
),
192 buffer_manager_(this),
194 encoder_last_error_(PP_ERROR_FAILED
),
196 media_input_format_(media::PIXEL_FORMAT_UNKNOWN
),
197 weak_ptr_factory_(this) {
200 PepperVideoEncoderHost::~PepperVideoEncoderHost() {
204 int32_t PepperVideoEncoderHost::OnResourceMessageReceived(
205 const IPC::Message
& msg
,
206 ppapi::host::HostMessageContext
* context
) {
207 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoEncoderHost
, msg
)
208 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
209 PpapiHostMsg_VideoEncoder_GetSupportedProfiles
,
210 OnHostMsgGetSupportedProfiles
)
211 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Initialize
,
213 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
214 PpapiHostMsg_VideoEncoder_GetVideoFrames
,
215 OnHostMsgGetVideoFrames
)
216 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoEncoder_Encode
,
218 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
219 PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer
,
220 OnHostMsgRecycleBitstreamBuffer
)
221 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
222 PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange
,
223 OnHostMsgRequestEncodingParametersChange
)
224 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoEncoder_Close
,
226 PPAPI_END_MESSAGE_MAP()
227 return PP_ERROR_FAILED
;
230 int32_t PepperVideoEncoderHost::OnHostMsgGetSupportedProfiles(
231 ppapi::host::HostMessageContext
* context
) {
232 std::vector
<PP_VideoProfileDescription
> pp_profiles
;
233 GetSupportedProfiles(&pp_profiles
);
236 context
->MakeReplyMessageContext(),
237 PpapiPluginMsg_VideoEncoder_GetSupportedProfilesReply(pp_profiles
));
239 return PP_OK_COMPLETIONPENDING
;
242 int32_t PepperVideoEncoderHost::OnHostMsgInitialize(
243 ppapi::host::HostMessageContext
* context
,
244 PP_VideoFrame_Format input_format
,
245 const PP_Size
& input_visible_size
,
246 PP_VideoProfile output_profile
,
247 uint32_t initial_bitrate
,
248 PP_HardwareAcceleration acceleration
) {
250 return PP_ERROR_FAILED
;
252 media_input_format_
= PP_ToMediaVideoFormat(input_format
);
253 if (media_input_format_
== media::PIXEL_FORMAT_UNKNOWN
)
254 return PP_ERROR_BADARGUMENT
;
256 media::VideoCodecProfile media_profile
=
257 PP_ToMediaVideoProfile(output_profile
);
258 if (media_profile
== media::VIDEO_CODEC_PROFILE_UNKNOWN
)
259 return PP_ERROR_BADARGUMENT
;
261 gfx::Size
input_size(input_visible_size
.width
, input_visible_size
.height
);
262 if (input_size
.IsEmpty())
263 return PP_ERROR_BADARGUMENT
;
265 if (!IsInitializationValid(input_visible_size
, output_profile
, acceleration
))
266 return PP_ERROR_NOTSUPPORTED
;
268 int32_t error
= PP_ERROR_NOTSUPPORTED
;
269 initialize_reply_context_
= context
->MakeReplyMessageContext();
271 if (acceleration
!= PP_HARDWAREACCELERATION_NONE
) {
272 if (InitializeHardware(media_input_format_
, input_size
, media_profile
,
274 return PP_OK_COMPLETIONPENDING
;
276 if (acceleration
== PP_HARDWAREACCELERATION_ONLY
)
277 error
= PP_ERROR_FAILED
;
280 #if defined(OS_ANDROID)
281 initialize_reply_context_
= ppapi::host::ReplyMessageContext();
285 if (acceleration
!= PP_HARDWAREACCELERATION_ONLY
) {
286 encoder_
.reset(new VideoEncoderShim(this));
287 if (encoder_
->Initialize(media_input_format_
, input_size
, media_profile
,
288 initial_bitrate
, this))
289 return PP_OK_COMPLETIONPENDING
;
290 error
= PP_ERROR_FAILED
;
293 initialize_reply_context_
= ppapi::host::ReplyMessageContext();
299 int32_t PepperVideoEncoderHost::OnHostMsgGetVideoFrames(
300 ppapi::host::HostMessageContext
* context
) {
301 if (encoder_last_error_
)
302 return encoder_last_error_
;
304 get_video_frames_reply_context_
= context
->MakeReplyMessageContext();
305 AllocateVideoFrames();
307 return PP_OK_COMPLETIONPENDING
;
310 int32_t PepperVideoEncoderHost::OnHostMsgEncode(
311 ppapi::host::HostMessageContext
* context
,
313 bool force_keyframe
) {
314 if (encoder_last_error_
)
315 return encoder_last_error_
;
317 if (frame_id
>= frame_count_
)
318 return PP_ERROR_FAILED
;
321 CreateVideoFrame(frame_id
, context
->MakeReplyMessageContext()),
324 return PP_OK_COMPLETIONPENDING
;
327 int32_t PepperVideoEncoderHost::OnHostMsgRecycleBitstreamBuffer(
328 ppapi::host::HostMessageContext
* context
,
329 uint32_t buffer_id
) {
330 if (encoder_last_error_
)
331 return encoder_last_error_
;
333 if (buffer_id
>= shm_buffers_
.size() || shm_buffers_
[buffer_id
]->in_use
)
334 return PP_ERROR_FAILED
;
336 shm_buffers_
[buffer_id
]->in_use
= true;
337 encoder_
->UseOutputBitstreamBuffer(
338 shm_buffers_
[buffer_id
]->ToBitstreamBuffer());
343 int32_t PepperVideoEncoderHost::OnHostMsgRequestEncodingParametersChange(
344 ppapi::host::HostMessageContext
* context
,
346 uint32_t framerate
) {
347 if (encoder_last_error_
)
348 return encoder_last_error_
;
350 encoder_
->RequestEncodingParametersChange(bitrate
, framerate
);
355 int32_t PepperVideoEncoderHost::OnHostMsgClose(
356 ppapi::host::HostMessageContext
* context
) {
357 encoder_last_error_
= PP_ERROR_FAILED
;
363 void PepperVideoEncoderHost::RequireBitstreamBuffers(
364 unsigned int frame_count
,
365 const gfx::Size
& input_coded_size
,
366 size_t output_buffer_size
) {
367 DCHECK(RenderThreadImpl::current());
368 // We assume RequireBitstreamBuffers is only called once.
369 DCHECK(!initialized_
);
371 input_coded_size_
= input_coded_size
;
372 frame_count_
= frame_count
;
374 for (uint32_t i
= 0; i
< kDefaultNumberOfBitstreamBuffers
; ++i
) {
375 scoped_ptr
<base::SharedMemory
> shm(
377 ->HostAllocateSharedMemoryBuffer(output_buffer_size
)
380 if (!shm
|| !shm
->Map(output_buffer_size
)) {
381 shm_buffers_
.clear();
385 shm_buffers_
.push_back(new ShmBuffer(i
, shm
.Pass()));
388 // Feed buffers to the encoder.
389 std::vector
<SerializedHandle
> handles
;
390 for (size_t i
= 0; i
< shm_buffers_
.size(); ++i
) {
391 encoder_
->UseOutputBitstreamBuffer(shm_buffers_
[i
]->ToBitstreamBuffer());
392 handles
.push_back(SerializedHandle(
393 renderer_ppapi_host_
->ShareSharedMemoryHandleWithRemote(
394 shm_buffers_
[i
]->shm
->handle()),
395 output_buffer_size
));
398 host()->SendUnsolicitedReplyWithHandles(
399 pp_resource(), PpapiPluginMsg_VideoEncoder_BitstreamBuffers(
400 static_cast<uint32_t>(output_buffer_size
)),
404 // Tell the plugin that initialization has been successful if we
407 encoder_last_error_
= PP_OK
;
408 host()->SendReply(initialize_reply_context_
,
409 PpapiPluginMsg_VideoEncoder_InitializeReply(
410 frame_count
, PP_FromGfxSize(input_coded_size
)));
413 if (shm_buffers_
.empty()) {
414 NotifyPepperError(PP_ERROR_NOMEMORY
);
418 // If the plugin already requested video frames, we can now answer
420 if (get_video_frames_reply_context_
.is_valid())
421 AllocateVideoFrames();
424 void PepperVideoEncoderHost::BitstreamBufferReady(int32 buffer_id
,
427 DCHECK(RenderThreadImpl::current());
428 DCHECK(shm_buffers_
[buffer_id
]->in_use
);
430 shm_buffers_
[buffer_id
]->in_use
= false;
431 host()->SendUnsolicitedReply(
433 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady(
434 buffer_id
, static_cast<uint32_t>(payload_size
), key_frame
));
437 void PepperVideoEncoderHost::NotifyError(
438 media::VideoEncodeAccelerator::Error error
) {
439 DCHECK(RenderThreadImpl::current());
440 NotifyPepperError(PP_FromMediaEncodeAcceleratorError(error
));
443 void PepperVideoEncoderHost::GetSupportedProfiles(
444 std::vector
<PP_VideoProfileDescription
>* pp_profiles
) {
445 DCHECK(RenderThreadImpl::current());
447 media::VideoEncodeAccelerator::SupportedProfiles profiles
;
449 if (EnsureGpuChannel()) {
450 profiles
= GpuVideoAcceleratorUtil::ConvertGpuToMediaEncodeProfiles(
451 channel_
->gpu_info().video_encode_accelerator_supported_profiles
);
452 for (media::VideoEncodeAccelerator::SupportedProfile profile
: profiles
) {
453 pp_profiles
->push_back(
454 PP_FromVideoEncodeAcceleratorSupportedProfile(profile
, PP_TRUE
));
458 #if !defined(OS_ANDROID)
459 VideoEncoderShim
software_encoder(this);
460 profiles
= software_encoder
.GetSupportedProfiles();
461 for (media::VideoEncodeAccelerator::SupportedProfile profile
: profiles
) {
462 pp_profiles
->push_back(
463 PP_FromVideoEncodeAcceleratorSupportedProfile(profile
, PP_FALSE
));
468 bool PepperVideoEncoderHost::IsInitializationValid(
469 const PP_Size
& input_size
,
470 PP_VideoProfile output_profile
,
471 PP_HardwareAcceleration acceleration
) {
472 DCHECK(RenderThreadImpl::current());
474 std::vector
<PP_VideoProfileDescription
> profiles
;
475 GetSupportedProfiles(&profiles
);
477 for (const PP_VideoProfileDescription
& profile
: profiles
) {
478 if (output_profile
== profile
.profile
&&
479 input_size
.width
<= profile
.max_resolution
.width
&&
480 input_size
.height
<= profile
.max_resolution
.height
&&
481 PP_HardwareAccelerationCompatible(
482 profile
.hardware_accelerated
== PP_TRUE
, acceleration
))
489 bool PepperVideoEncoderHost::EnsureGpuChannel() {
490 DCHECK(RenderThreadImpl::current());
495 // There is no guarantee that we have a 3D context to work with. So
496 // we create a dummy command buffer to communicate with the gpu process.
497 channel_
= RenderThreadImpl::current()->EstablishGpuChannelSync(
498 CAUSE_FOR_GPU_LAUNCH_PEPPERVIDEOENCODERACCELERATOR_INITIALIZE
);
502 std::vector
<int32
> attribs(1, PP_GRAPHICS3DATTRIB_NONE
);
503 command_buffer_
= channel_
->CreateOffscreenCommandBuffer(
504 gfx::Size(), nullptr, GpuChannelHost::kDefaultStreamId
,
505 GpuChannelHost::kDefaultStreamPriority
, attribs
, GURL::EmptyGURL(),
506 gfx::PreferIntegratedGpu
);
507 if (!command_buffer_
) {
512 command_buffer_
->SetContextLostCallback(media::BindToCurrentLoop(
513 base::Bind(&PepperVideoEncoderHost::NotifyPepperError
,
514 weak_ptr_factory_
.GetWeakPtr(), PP_ERROR_RESOURCE_FAILED
)));
515 if (!command_buffer_
->Initialize()) {
523 bool PepperVideoEncoderHost::InitializeHardware(
524 media::VideoPixelFormat input_format
,
525 const gfx::Size
& input_visible_size
,
526 media::VideoCodecProfile output_profile
,
527 uint32_t initial_bitrate
) {
528 DCHECK(RenderThreadImpl::current());
530 if (!EnsureGpuChannel())
533 encoder_
= command_buffer_
->CreateVideoEncoder();
535 !encoder_
->Initialize(input_format
, input_visible_size
, output_profile
,
536 initial_bitrate
, this))
542 void PepperVideoEncoderHost::Close() {
543 DCHECK(RenderThreadImpl::current());
546 command_buffer_
= nullptr;
549 void PepperVideoEncoderHost::AllocateVideoFrames() {
550 DCHECK(RenderThreadImpl::current());
551 DCHECK(get_video_frames_reply_context_
.is_valid());
553 // Frames have already been allocated.
554 if (buffer_manager_
.number_of_buffers() > 0) {
555 SendGetFramesErrorReply(PP_ERROR_FAILED
);
560 base::CheckedNumeric
<uint32_t> size
=
561 media::VideoFrame::AllocationSize(media_input_format_
, input_coded_size_
);
562 uint32_t frame_size
= size
.ValueOrDie();
563 size
+= sizeof(ppapi::MediaStreamBuffer::Video
);
564 uint32_t buffer_size
= size
.ValueOrDie();
565 // Make each buffer 4 byte aligned.
566 size
+= (4 - buffer_size
% 4);
567 uint32_t buffer_size_aligned
= size
.ValueOrDie();
568 size
*= frame_count_
;
569 uint32_t total_size
= size
.ValueOrDie();
571 scoped_ptr
<base::SharedMemory
> shm(
572 RenderThreadImpl::current()
573 ->HostAllocateSharedMemoryBuffer(total_size
)
576 !buffer_manager_
.SetBuffers(frame_count_
,
580 SendGetFramesErrorReply(PP_ERROR_NOMEMORY
);
584 VLOG(4) << " frame_count=" << frame_count_
<< " frame_size=" << frame_size
585 << " buffer_size=" << buffer_size_aligned
;
587 for (int32_t i
= 0; i
< buffer_manager_
.number_of_buffers(); ++i
) {
588 ppapi::MediaStreamBuffer::Video
* buffer
=
589 &(buffer_manager_
.GetBufferPointer(i
)->video
);
590 buffer
->header
.size
= buffer_manager_
.buffer_size();
591 buffer
->header
.type
= ppapi::MediaStreamBuffer::TYPE_VIDEO
;
592 buffer
->format
= PP_FromMediaVideoFormat(media_input_format_
);
593 buffer
->size
.width
= input_coded_size_
.width();
594 buffer
->size
.height
= input_coded_size_
.height();
595 buffer
->data_size
= frame_size
;
598 DCHECK(get_video_frames_reply_context_
.is_valid());
599 get_video_frames_reply_context_
.params
.AppendHandle(
600 SerializedHandle(renderer_ppapi_host_
->ShareSharedMemoryHandleWithRemote(
601 buffer_manager_
.shm()->handle()),
604 host()->SendReply(get_video_frames_reply_context_
,
605 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
606 frame_count_
, buffer_size_aligned
,
607 PP_FromGfxSize(input_coded_size_
)));
608 get_video_frames_reply_context_
= ppapi::host::ReplyMessageContext();
611 void PepperVideoEncoderHost::SendGetFramesErrorReply(int32_t error
) {
612 get_video_frames_reply_context_
.params
.set_result(error
);
614 get_video_frames_reply_context_
,
615 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(0, 0, PP_MakeSize(0, 0)));
616 get_video_frames_reply_context_
= ppapi::host::ReplyMessageContext();
619 scoped_refptr
<media::VideoFrame
> PepperVideoEncoderHost::CreateVideoFrame(
621 const ppapi::host::ReplyMessageContext
& reply_context
) {
622 DCHECK(RenderThreadImpl::current());
624 ppapi::MediaStreamBuffer
* buffer
= buffer_manager_
.GetBufferPointer(frame_id
);
626 uint32_t shm_offset
= static_cast<uint8
*>(buffer
->video
.data
) -
627 static_cast<uint8
*>(buffer_manager_
.shm()->memory());
629 scoped_refptr
<media::VideoFrame
> frame
=
630 media::VideoFrame::WrapExternalSharedMemory(
631 media_input_format_
, input_coded_size_
, gfx::Rect(input_coded_size_
),
632 input_coded_size_
, static_cast<uint8
*>(buffer
->video
.data
),
633 buffer
->video
.data_size
, buffer_manager_
.shm()->handle(), shm_offset
,
635 frame
->AddDestructionObserver(
636 base::Bind(&PepperVideoEncoderHost::FrameReleased
,
637 weak_ptr_factory_
.GetWeakPtr(), reply_context
, frame_id
));
641 void PepperVideoEncoderHost::FrameReleased(
642 const ppapi::host::ReplyMessageContext
& reply_context
,
644 DCHECK(RenderThreadImpl::current());
646 ppapi::host::ReplyMessageContext context
= reply_context
;
647 context
.params
.set_result(encoder_last_error_
);
648 host()->SendReply(context
, PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id
));
651 void PepperVideoEncoderHost::NotifyPepperError(int32_t error
) {
652 DCHECK(RenderThreadImpl::current());
654 encoder_last_error_
= error
;
656 host()->SendUnsolicitedReply(
658 PpapiPluginMsg_VideoEncoder_NotifyError(encoder_last_error_
));
661 uint8_t* PepperVideoEncoderHost::ShmHandleToAddress(int32 buffer_id
) {
662 DCHECK(RenderThreadImpl::current());
663 DCHECK_GE(buffer_id
, 0);
664 DCHECK_LT(buffer_id
, static_cast<int32
>(shm_buffers_
.size()));
665 return static_cast<uint8_t*>(shm_buffers_
[buffer_id
]->shm
->memory());
668 } // namespace content