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

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: Send bitstream buffers in their own ppapi ipc message 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.
bbudge 2015/02/18 22:53:00 nit:indent
llandwerlin-old 2015/02/19 15:55:45 Done.
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);
bbudge 2015/02/18 22:53:00 I think shm here refers to the parameter. DCHECK(t
llandwerlin-old 2015/02/19 15:55:45 Done.
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;
bbudge 2015/02/18 22:53:01 We should Close() here before returning. It might
llandwerlin-old 2015/02/19 15:55:45 Done.
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;
bbudge 2015/02/18 22:53:00 Close() here too.
llandwerlin-old 2015/02/19 15:55:45 Done.
302
303 encoder_ = command_buffer_->CreateVideoEncoder();
304 if (encoder_ &&
305 encoder_->Initialize(media_input_format_, input_size, media_profile,
306 initial_bitrate, this)) {
307 initialize_context_ = context->MakeReplyMessageContext();
308 return PP_OK_COMPLETIONPENDING;
309 }
310
311 Close();
312 return PP_ERROR_FAILED;
313 }
314
315 // TODO(llandwerlin): Software encoder.
316 Close();
317 return PP_ERROR_NOTSUPPORTED;
318 }
319
320 int32_t PepperVideoEncoderHost::OnHostMsgGetVideoFrames(
321 ppapi::host::HostMessageContext* context) {
322 if (encoder_last_error_)
323 return encoder_last_error_;
324
325 get_video_frames_context_ = context->MakeReplyMessageContext();
326
327 // If the encoder hasn't required bitstream buffers yet, we don't
328 // know the size of the video frames we need to allocate. Let's save
329 // the context and answer when we're ready.
330 if (!shm_buffers_.empty()) {
331 AllocateVideoFrames();
332 get_video_frames_context_ = ppapi::host::ReplyMessageContext();
bbudge 2015/02/18 22:53:00 This should happen in AllocateVideoFrames.
llandwerlin-old 2015/02/19 15:55:45 Done.
333 }
334
335 return PP_OK_COMPLETIONPENDING;
336 }
337
338 int32_t PepperVideoEncoderHost::OnHostMsgEncode(
339 ppapi::host::HostMessageContext* context,
340 uint32_t frame_id,
341 bool force_keyframe) {
342 if (encoder_last_error_)
343 return encoder_last_error_;
344
345 if (frame_id >= frame_count_)
346 return PP_ERROR_FAILED;
347
348 encoder_->Encode(
349 CreateVideoFrame(frame_id, context->MakeReplyMessageContext()),
350 force_keyframe);
351
352 return PP_OK_COMPLETIONPENDING;
353 }
354
355 int32_t PepperVideoEncoderHost::OnHostMsgRecycleBitstreamBuffer(
356 ppapi::host::HostMessageContext* context,
357 uint32_t buffer_id) {
358 if (encoder_last_error_)
359 return encoder_last_error_;
360
361 if (buffer_id >= shm_buffers_.size() || shm_buffers_[buffer_id]->in_use)
362 return PP_ERROR_FAILED;
363
364 shm_buffers_[buffer_id]->in_use = true;
365 encoder_->UseOutputBitstreamBuffer(
366 shm_buffers_[buffer_id]->ToBitstreamBuffer());
367
368 return PP_OK;
369 }
370
371 int32_t PepperVideoEncoderHost::OnHostMsgRequestEncodingParametersChange(
372 ppapi::host::HostMessageContext* context,
373 uint32_t bitrate,
374 uint32_t framerate) {
375 if (encoder_last_error_)
376 return encoder_last_error_;
377
378 encoder_->RequestEncodingParametersChange(bitrate, framerate);
379
380 return PP_OK;
381 }
382
383 int32_t PepperVideoEncoderHost::OnHostMsgClose(
384 ppapi::host::HostMessageContext* context) {
385 encoder_last_error_ = PP_ERROR_FAILED;
386 Close();
387
388 return PP_OK;
389 }
390
391 void PepperVideoEncoderHost::RequireBitstreamBuffers(
392 unsigned int frame_count,
393 const gfx::Size& input_coded_size,
394 size_t output_buffer_size) {
395 DCHECK(RenderThreadImpl::current());
bbudge 2015/02/18 22:53:00 DCHECK(!initialized_) so we find out if/when Requi
llandwerlin-old 2015/02/19 15:55:45 Done.
396
397 input_coded_size_ = input_coded_size;
398 frame_count_ = frame_count;
399
400 if (!initialized_) {
401 // Tell the plugin that initialization has been successful if we
402 // haven't already.
403 initialized_ = true;
404 encoder_last_error_ = PP_OK;
405 host()->SendReply(initialize_context_,
406 PpapiPluginMsg_VideoEncoder_InitializeReply(
407 frame_count, PP_FromGfxSize(input_coded_size)));
408 }
bbudge 2015/02/18 22:53:00 It seems to me that the frame count and input code
llandwerlin-old 2015/02/19 15:55:45 It seems to me that input coded size is a hardware
bbudge 2015/02/19 20:30:54 I agree that RequireBitstreamBuffers is an odd par
409
410 for (int32_t i = 0; i < kDefaultNumberOfBitstreamBuffers; ++i) {
411 scoped_ptr<base::SharedMemory> shm(
412 RenderThread::Get()
413 ->HostAllocateSharedMemoryBuffer(output_buffer_size)
414 .Pass());
415
416 if (!shm || !shm->Map(output_buffer_size)) {
417 shm_buffers_.clear();
418 break;
419 }
420
421 shm_buffers_.push_back(new ShmBuffer(i, shm.Pass()));
422 }
423
424 if (shm_buffers_.empty()) {
425 NotifyPepperError(PP_ERROR_NOMEMORY);
426 return;
427 }
bbudge 2015/02/18 22:53:01 Do this check after replying to Initialize.
llandwerlin-old 2015/02/19 15:55:45 Done.
428
429 // Feed buffers to the encoder.
430 std::vector<SerializedHandle> handles;
431 for (size_t i = 0; i < shm_buffers_.size(); ++i) {
432 encoder_->UseOutputBitstreamBuffer(shm_buffers_[i]->ToBitstreamBuffer());
433 handles.push_back(SerializedHandle(
434 renderer_ppapi_host_->ShareHandleWithRemote(
435 ConvertSharedMemoryHandle(*shm_buffers_[i]->shm), false),
436 output_buffer_size));
437 }
438
439 host()->SendUnsolicitedReplyWithHandles(
440 pp_resource(), PpapiPluginMsg_VideoEncoder_BitstreamBuffers(
441 static_cast<uint32_t>(output_buffer_size)),
442 handles);
443
bbudge 2015/02/18 22:53:00 Initialization reply PP_OK here, then test for han
llandwerlin-old 2015/02/19 15:55:45 Done.
444 // If the plugin already ask for video frames, we can now answer
445 // that request.
446 if (get_video_frames_context_.is_valid()) {
447 AllocateVideoFrames();
448 get_video_frames_context_ = ppapi::host::ReplyMessageContext();
449 }
450 }
451
452 void PepperVideoEncoderHost::BitstreamBufferReady(int32 buffer_id,
453 size_t payload_size,
454 bool key_frame) {
455 DCHECK(RenderThreadImpl::current());
456 DCHECK(shm_buffers_[buffer_id]->in_use);
457
458 shm_buffers_[buffer_id]->in_use = false;
459 host()->SendUnsolicitedReply(
460 pp_resource(),
461 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady(
462 buffer_id, static_cast<uint32_t>(payload_size), key_frame));
463 }
464
465 void PepperVideoEncoderHost::NotifyError(
466 media::VideoEncodeAccelerator::Error error) {
467 DCHECK(RenderThreadImpl::current());
468 NotifyPepperError(PP_FromMediaEncodeAcceleratorError(error));
469 }
470
471 void PepperVideoEncoderHost::GetSupportedProfiles(
472 std::vector<PP_VideoProfileDescription>* pp_profiles) {
473 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles =
474 RenderThreadImpl::current()
475 ->GetGpuFactories()
476 ->GetVideoEncodeAcceleratorSupportedProfiles();
477 for (media::VideoEncodeAccelerator::SupportedProfile profile : profiles)
478 pp_profiles->push_back(PP_FromVideoEncodeAcceleratorSupportedProfile(
479 profile, PP_HARDWAREACCELERATION_ONLY));
480
481 // TODO(llandwerlin): add software supported profiles.
482 }
483
484 bool PepperVideoEncoderHost::IsInitializationValid(
485 const PP_Size& input_size,
486 PP_VideoProfile output_profile,
487 PP_HardwareAcceleration acceleration) {
488 std::vector<PP_VideoProfileDescription> profiles;
489 GetSupportedProfiles(&profiles);
490
491 for (const PP_VideoProfileDescription& profile : profiles) {
492 if (output_profile == profile.profile &&
493 input_size.width <= profile.max_resolution.width &&
494 input_size.height <= profile.max_resolution.height &&
495 PP_HardwareAccelerationCompatible(profile.acceleration, acceleration))
496 return true;
497 }
498
499 return false;
500 }
501
502 void PepperVideoEncoderHost::AllocateVideoFrames() {
503 // Frames have already been allocated.
504 if (buffer_manager_.number_of_buffers() > 0) {
505 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
506 host()->SendReply(get_video_frames_context_,
507 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
508 0, 0, PP_MakeSize(0, 0)));
bbudge 2015/02/18 22:53:00 This code is repeated 4 times in this method. I th
llandwerlin-old 2015/02/19 15:55:45 Done.
509 return;
510 }
511
512 base::CheckedNumeric<uint32_t> size =
513 media::VideoFrame::AllocationSize(media_input_format_, input_coded_size_);
514 uint32_t frame_size = size.ValueOrDie();
515 size += sizeof(ppapi::MediaStreamBuffer::Video) -
516 sizeof(ppapi::MediaStreamBuffer::Video::data);
517 uint32_t buffer_size = size.ValueOrDie();
518 // Make each buffer 4 byte aligned.
519 size += (4 - buffer_size % 4);
520 base::CheckedNumeric<uint32_t> buffer_size_aligned = size.ValueOrDie();
521 size *= frame_count_;
522 base::CheckedNumeric<uint32_t> total_size = size.ValueOrDie();
523
524 if (!total_size.IsValid()) {
525 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
526 host()->SendReply(get_video_frames_context_,
527 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
528 0, 0, PP_MakeSize(0, 0)));
529 return;
530 }
531
532 scoped_ptr<base::SharedMemory> shm(
533 RenderThreadImpl::current()
534 ->HostAllocateSharedMemoryBuffer(total_size.ValueOrDie())
535 .Pass());
536 if (!shm) {
537 get_video_frames_context_.params.set_result(PP_ERROR_NOMEMORY);
538 host()->SendReply(get_video_frames_context_,
539 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
540 0, 0, PP_MakeSize(0, 0)));
541 return;
542 }
543
544 VLOG(4) << " frame_count=" << frame_count_ << " frame_size=" << frame_size
545 << " buffer_size=" << buffer_size_aligned.ValueOrDie();
546
547 if (!buffer_manager_.SetBuffers(
548 frame_count_, buffer_size_aligned.ValueOrDie(), shm.Pass(), true)) {
549 get_video_frames_context_.params.set_result(PP_ERROR_FAILED);
550 host()->SendReply(get_video_frames_context_,
551 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
552 0, 0, PP_MakeSize(0, 0)));
553 return;
554 }
555
556 for (int32_t i = 0; i < buffer_manager_.number_of_buffers(); ++i) {
557 ppapi::MediaStreamBuffer::Video* buffer =
558 &(buffer_manager_.GetBufferPointer(i)->video);
559 buffer->header.size = buffer_manager_.buffer_size();
560 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_VIDEO;
561 buffer->format = PP_FromMediaVideoFormat(media_input_format_);
562 buffer->size.width = input_coded_size_.width();
563 buffer->size.height = input_coded_size_.height();
564 buffer->data_size = frame_size;
565 }
566
567 get_video_frames_context_.params.AppendHandle(SerializedHandle(
568 renderer_ppapi_host_->ShareHandleWithRemote(
569 ConvertSharedMemoryHandle(*buffer_manager_.shm()), false),
570 total_size.ValueOrDie()));
571
572 host()->SendReply(get_video_frames_context_,
573 PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
574 frame_count_, buffer_size_aligned.ValueOrDie(),
575 PP_FromGfxSize(input_coded_size_)));
bbudge 2015/02/18 22:53:00 Clear the reply context here: get_video_frames_con
llandwerlin-old 2015/02/19 15:55:45 Done.
576 }
577
578 scoped_refptr<media::VideoFrame> PepperVideoEncoderHost::CreateVideoFrame(
579 uint32_t frame_id,
580 const ppapi::host::ReplyMessageContext& reply_context) {
581 DCHECK_LT(frame_id, frame_count_);
bbudge 2015/02/18 22:53:01 nit: This check is repeated by MediaStrreamBufferM
llandwerlin-old 2015/02/19 15:55:45 Done.
582
583 ppapi::MediaStreamBuffer* buffer = buffer_manager_.GetBufferPointer(frame_id);
584 DCHECK(buffer);
585 uint32_t shm_offset = frame_id * buffer_manager_.buffer_size() +
586 sizeof(ppapi::MediaStreamBuffer::Video) -
587 sizeof(ppapi::MediaStreamBuffer::Video::data);
588
589 return media::VideoFrame::WrapExternalPackedMemory(
590 media_input_format_, input_coded_size_, gfx::Rect(input_coded_size_),
591 input_coded_size_, static_cast<uint8*>(buffer->video.data),
592 buffer->video.data_size, buffer_manager_.shm()->handle(), shm_offset,
593 base::TimeDelta(),
594 base::Bind(&PepperVideoEncoderHost::FrameReleased,
595 weak_ptr_factory_.GetWeakPtr(), reply_context, frame_id));
596 }
597
598 void PepperVideoEncoderHost::FrameReleased(
599 const ppapi::host::ReplyMessageContext& reply_context,
600 uint32_t frame_id) {
601 DCHECK(RenderThreadImpl::current());
602
603 ppapi::host::ReplyMessageContext context = reply_context;
604 context.params.set_result(encoder_last_error_);
605 host()->SendReply(reply_context,
606 PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id));
607 }
608
609 void PepperVideoEncoderHost::NotifyPepperError(int32_t error) {
610 DCHECK(RenderThreadImpl::current());
611
612 encoder_last_error_ = error;
613 Close();
614 host()->SendUnsolicitedReply(
615 pp_resource(),
616 PpapiPluginMsg_VideoEncoder_NotifyError(encoder_last_error_));
617 }
618
619 void PepperVideoEncoderHost::Close() {
620 DCHECK(RenderThreadImpl::current());
621
622 encoder_ = nullptr;
623 if (command_buffer_) {
624 DCHECK(channel_);
625 channel_->DestroyCommandBuffer(command_buffer_);
626 command_buffer_ = nullptr;
627 }
628 }
629
630 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698