Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: content/renderer/pepper/pepper_video_encoder_host.cc

Issue 905023005: Pepper: PPB_VideoEncoder implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split error notification from error state Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(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 if (acceleration == PP_HARDWAREACCELERATION_ONLY ||
280 acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK) {
281 // There is no guarantee that we have a 3D context to work with. So
282 // we create a dummy command buffer to communicate with the gpu process.
283 channel_ = RenderThreadImpl::current()->EstablishGpuChannelSync(
284 CAUSE_FOR_GPU_LAUNCH_PEPPERVIDEOENCODERACCELERATOR_INITIALIZE);
285 if (!channel_.get())
286 return PP_ERROR_FAILED;
287
288 std::vector<int32> attribs(1, PP_GRAPHICS3DATTRIB_NONE);
289
290 command_buffer_ = channel_->CreateOffscreenCommandBuffer(
291 gfx::Size(), nullptr, attribs, GURL::EmptyGURL(),
292 gfx::PreferIntegratedGpu);
293 if (!command_buffer_)
294 return PP_ERROR_FAILED;
295
296 command_buffer_->SetChannelErrorCallback(media::BindToCurrentLoop(
297 base::Bind(&PepperVideoEncoderHost::NotifyPepperError,
298 weak_ptr_factory_.GetWeakPtr(), PP_ERROR_FAILED)));
299
300 if (!command_buffer_->Initialize())
301 return PP_ERROR_FAILED;
302
303 encoder_ = command_buffer_->CreateVideoEncoder();
304 if (encoder_ &&
305 encoder_->Initialize(media_input_format_, input_size, media_profile,
306 initial_bitrate, this)) {
307 initialized_ = true;
308 encoder_last_error_ = PP_OK;
309 host()->SendReply(context->MakeReplyMessageContext(),
310 PpapiPluginMsg_VideoEncoder_InitializeReply());
311 return PP_OK;
312 }
313
314 if (acceleration == PP_HARDWAREACCELERATION_ONLY) {
315 Close();
bbudge 2015/02/17 18:38:04 Close() should be outside the 'if'. As it stands,
llandwerlin-old 2015/02/18 13:01:44 Done.
316 return PP_ERROR_FAILED;
317 }
318 }
319
320 // TODO(llandwerlin): Software encoder.
321 return PP_ERROR_NOTSUPPORTED;
322 }
323
324 int32_t PepperVideoEncoderHost::OnHostMsgGetVideoFrames(
325 ppapi::host::HostMessageContext* context) {
326 if (encoder_last_error_)
327 return encoder_last_error_;
328
329 get_video_frames_context_ = context->MakeReplyMessageContext();
330
331 // If the encoder hasn't required bitstream buffers yet, we don't
332 // know the size of the video frames we need to allocate. Let's save
333 // the context and answer when we're ready.
334 if (!shm_buffers_.empty()) {
335 AllocateVideoFrames();
336 get_video_frames_context_ = ppapi::host::ReplyMessageContext();
337 }
338
339 return PP_OK_COMPLETIONPENDING;
340 }
341
342 int32_t PepperVideoEncoderHost::OnHostMsgEncode(
343 ppapi::host::HostMessageContext* context,
344 uint32_t frame_id,
345 bool force_keyframe) {
346 if (encoder_last_error_)
347 return encoder_last_error_;
348
349 if (frame_id >= frame_count_)
350 return PP_ERROR_FAILED;
351
352 encoder_->Encode(
353 CreateVideoFrame(frame_id, context->MakeReplyMessageContext()),
354 force_keyframe);
355
356 return PP_OK_COMPLETIONPENDING;
357 }
358
359 int32_t PepperVideoEncoderHost::OnHostMsgRecycleBitstreamBuffer(
360 ppapi::host::HostMessageContext* context,
361 uint32_t buffer_id) {
362 if (encoder_last_error_)
363 return encoder_last_error_;
364
365 if (buffer_id >= shm_buffers_.size() || shm_buffers_[buffer_id]->in_use)
366 return PP_ERROR_FAILED;
367
368 shm_buffers_[buffer_id]->in_use = true;
369 encoder_->UseOutputBitstreamBuffer(
370 shm_buffers_[buffer_id]->ToBitstreamBuffer());
371
372 return PP_OK;
373 }
374
375 int32_t PepperVideoEncoderHost::OnHostMsgRequestEncodingParametersChange(
376 ppapi::host::HostMessageContext* context,
377 uint32_t bitrate,
378 uint32_t framerate) {
379 if (encoder_last_error_)
380 return encoder_last_error_;
381
382 encoder_->RequestEncodingParametersChange(bitrate, framerate);
383
384 return PP_OK;
385 }
386
387 int32_t PepperVideoEncoderHost::OnHostMsgClose(
388 ppapi::host::HostMessageContext* context) {
389 encoder_last_error_ = PP_ERROR_FAILED;
390 Close();
391
392 return PP_OK;
393 }
394
395 void PepperVideoEncoderHost::RequireBitstreamBuffers(
396 unsigned int frame_count,
397 const gfx::Size& input_coded_size,
398 size_t output_buffer_size) {
399 DCHECK(RenderThreadImpl::current());
400
401 for (int32_t i = 0; i < kDefaultNumberOfBitstreamBuffers; ++i) {
402 scoped_ptr<base::SharedMemory> shm(
403 RenderThread::Get()
404 ->HostAllocateSharedMemoryBuffer(output_buffer_size)
405 .Pass());
406
407 if (!shm || !shm->Map(output_buffer_size)) {
408 shm_buffers_.clear();
409 break;
410 }
411
412 shm_buffers_.push_back(new ShmBuffer(i, shm.Pass()));
413 }
414
415 if (shm_buffers_.empty()) {
416 NotifyPepperError(PP_ERROR_NOMEMORY);
417 return;
418 }
419
420 // Feed buffers to the encoder.
421 std::vector<SerializedHandle> handles;
422 for (size_t i = 0; i < shm_buffers_.size(); ++i) {
423 encoder_->UseOutputBitstreamBuffer(shm_buffers_[i]->ToBitstreamBuffer());
424 handles.push_back(SerializedHandle(
425 renderer_ppapi_host_->ShareHandleWithRemote(
426 ConvertSharedMemoryHandle(*shm_buffers_[i]->shm), false),
427 output_buffer_size));
428 }
429
430 // Notify the plugin of the buffers and the encoding parameters.
431 PP_Size pp_input_coded_size =
432 PP_MakeSize(input_coded_size.width(), input_coded_size.height());
433 input_coded_size_ = input_coded_size;
434 frame_count_ = frame_count;
435
436 host()->SendUnsolicitedReplyWithHandles(
437 pp_resource(), PpapiPluginMsg_VideoEncoder_BitstreamBuffers(
438 static_cast<uint32_t>(output_buffer_size), frame_count,
439 pp_input_coded_size),
440 handles);
441
442 // If the plugin already ask for video frames, we can now answer
443 // that request.
444 if (get_video_frames_context_.is_valid()) {
445 AllocateVideoFrames();
446 get_video_frames_context_ = ppapi::host::ReplyMessageContext();
447 }
448 }
449
450 void PepperVideoEncoderHost::BitstreamBufferReady(int32 buffer_id,
451 size_t payload_size,
452 bool key_frame) {
453 DCHECK(RenderThreadImpl::current());
454 DCHECK(shm_buffers_[buffer_id]->in_use);
455
456 shm_buffers_[buffer_id]->in_use = false;
457 host()->SendUnsolicitedReply(
458 pp_resource(),
459 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady(
460 buffer_id, static_cast<uint32_t>(payload_size), key_frame));
461 }
462
463 void PepperVideoEncoderHost::NotifyError(
464 media::VideoEncodeAccelerator::Error error) {
465 DCHECK(RenderThreadImpl::current());
466 NotifyPepperError(PP_FromMediaEncodeAcceleratorError(error));
467 }
468
469 void PepperVideoEncoderHost::GetSupportedProfiles(
470 std::vector<PP_VideoProfileDescription>* pp_profiles) {
471 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles =
472 RenderThreadImpl::current()
473 ->GetGpuFactories()
474 ->GetVideoEncodeAcceleratorSupportedProfiles();
475 for (media::VideoEncodeAccelerator::SupportedProfile profile : profiles)
476 pp_profiles->push_back(PP_FromVideoEncodeAcceleratorSupportedProfile(
477 profile, PP_HARDWAREACCELERATION_ONLY));
478
479 // TODO(llandwerlin): add software supported profiles.
480 }
481
482 bool PepperVideoEncoderHost::IsInitializationValid(
483 const PP_Size& input_size,
484 PP_VideoProfile output_profile,
485 PP_HardwareAcceleration acceleration) {
486 std::vector<PP_VideoProfileDescription> profiles;
487 GetSupportedProfiles(&profiles);
488
489 for (const PP_VideoProfileDescription& profile : profiles) {
490 if (output_profile == profile.profile &&
491 input_size.width <= profile.max_resolution.width &&
492 input_size.height <= profile.max_resolution.height &&
493 PP_HardwareAccelerationCompatible(profile.acceleration, acceleration))
494 return true;
495 }
496
497 return false;
498 }
499
500 void PepperVideoEncoderHost::AllocateVideoFrames() {
501 // Frames have already been allocated.
502 if (buffer_manager_.number_of_buffers() > 0) {
503 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
504 host()->SendReply(get_video_frames_context_,
505 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
506 0, 0, PP_MakeSize(0, 0)));
507 return;
508 }
509
510 base::CheckedNumeric<uint32_t> size =
511 media::VideoFrame::AllocationSize(media_input_format_, input_coded_size_);
512 uint32_t frame_size = size.ValueOrDie();
513 size += sizeof(ppapi::MediaStreamBuffer::Video) -
514 sizeof(ppapi::MediaStreamBuffer::Video::data);
515 uint32_t buffer_size = size.ValueOrDie();
516 // Make each buffer 4 byte aligned.
517 size += (4 - buffer_size % 4);
518 base::CheckedNumeric<uint32_t> buffer_size_aligned = size.ValueOrDie();
519 size *= frame_count_;
520 base::CheckedNumeric<uint32_t> total_size = size.ValueOrDie();
521
522 if (!total_size.IsValid()) {
523 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
524 host()->SendReply(get_video_frames_context_,
525 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
526 0, 0, PP_MakeSize(0, 0)));
527 return;
528 }
529
530 scoped_ptr<base::SharedMemory> shm(
531 RenderThreadImpl::current()
532 ->HostAllocateSharedMemoryBuffer(total_size.ValueOrDie())
533 .Pass());
534 if (!shm) {
535 get_video_frames_context_.params.set_result(PP_ERROR_NOMEMORY);
536 host()->SendReply(get_video_frames_context_,
537 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
538 0, 0, PP_MakeSize(0, 0)));
539 return;
540 }
541
542 VLOG(4) << " frame_count=" << frame_count_ << " frame_size=" << frame_size
543 << " buffer_size=" << buffer_size_aligned.ValueOrDie();
544
545 if (!buffer_manager_.SetBuffers(
546 frame_count_, buffer_size_aligned.ValueOrDie(), shm.Pass(), true)) {
547 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
548 host()->SendReply(get_video_frames_context_,
549 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
550 0, 0, PP_MakeSize(0, 0)));
551 return;
552 }
553
554 for (int32_t i = 0; i < buffer_manager_.number_of_buffers(); ++i) {
555 ppapi::MediaStreamBuffer::Video* buffer =
556 &(buffer_manager_.GetBufferPointer(i)->video);
557 buffer->header.size = buffer_manager_.buffer_size();
558 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_VIDEO;
559 buffer->format = PP_FromMediaVideoFormat(media_input_format_);
560 buffer->size.width = input_coded_size_.width();
561 buffer->size.height = input_coded_size_.height();
562 buffer->data_size = frame_size;
563 }
564
565 get_video_frames_context_.params.AppendHandle(SerializedHandle(
566 renderer_ppapi_host_->ShareHandleWithRemote(
567 ConvertSharedMemoryHandle(*buffer_manager_.shm()), false),
568 total_size.ValueOrDie()));
569
570 host()->SendReply(get_video_frames_context_,
571 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
572 frame_count_, buffer_size_aligned.ValueOrDie(),
573 PP_FromGfxSize(input_coded_size_)));
574 }
575
576 scoped_refptr<media::VideoFrame> PepperVideoEncoderHost::CreateVideoFrame(
577 uint32_t frame_id,
578 const ppapi::host::ReplyMessageContext& reply_context) {
579 DCHECK_LT(frame_id, frame_count_);
580
581 ppapi::MediaStreamBuffer* buffer = buffer_manager_.GetBufferPointer(frame_id);
582 DCHECK(buffer);
583 uint32_t shm_offset = frame_id * buffer_manager_.buffer_size() +
584 sizeof(ppapi::MediaStreamBuffer::Video) -
585 sizeof(ppapi::MediaStreamBuffer::Video::data);
586
587 return media::VideoFrame::WrapExternalPackedMemory(
588 media_input_format_, input_coded_size_, gfx::Rect(input_coded_size_),
589 input_coded_size_, static_cast<uint8*>(buffer->video.data),
590 buffer->video.data_size, buffer_manager_.shm()->handle(), shm_offset,
591 base::TimeDelta(), base::Bind(
592 &PepperVideoEncoderHost::FrameReleased,
593 weak_ptr_factory_.GetWeakPtr(), reply_context, frame_id));
594 }
595
596 void PepperVideoEncoderHost::FrameReleased(
597 const ppapi::host::ReplyMessageContext& reply_context,
598 uint32_t frame_id) {
599 DCHECK(RenderThreadImpl::current());
600
601 ppapi::host::ReplyMessageContext context = reply_context;
602 context.params.set_result(encoder_last_error_);
603 host()->SendReply(reply_context,
604 PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id));
605 }
606
607 void PepperVideoEncoderHost::NotifyPepperError(int32_t error) {
608 DCHECK(RenderThreadImpl::current());
609
610 encoder_last_error_ = error;
611 Close();
612 host()->SendUnsolicitedReply(
613 pp_resource(),
614 PpapiPluginMsg_VideoEncoder_NotifyError(encoder_last_error_));
615 }
616
617 void PepperVideoEncoderHost::Close() {
618 DCHECK(RenderThreadImpl::current());
619
620 encoder_ = nullptr;
621 if (command_buffer_) {
622 DCHECK(channel_);
623 channel_->DestroyCommandBuffer(command_buffer_);
624 command_buffer_ = nullptr;
625 }
626 }
627
628 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698