Index: content/renderer/pepper/video_decoder_proxy.cc |
diff --git a/content/renderer/pepper/video_decoder_proxy.cc b/content/renderer/pepper/video_decoder_proxy.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..05626fcda3e931e44ada1d17df0d3cdc603a5ab5 |
--- /dev/null |
+++ b/content/renderer/pepper/video_decoder_proxy.cc |
@@ -0,0 +1,456 @@ |
+// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <GLES2/gl2.h> |
+#include <GLES2/gl2ext.h> |
+#include <GLES2/gl2extchromium.h> |
+ |
+#include "content/renderer/pepper/video_decoder_proxy.h" |
+ |
+#include "base/bind.h" |
+#include "content/public/renderer/render_thread.h" |
+#include "content/renderer/pepper/pepper_video_decoder_host.h" |
+#include "content/renderer/render_thread_impl.h" |
+#include "gpu/command_buffer/client/gles2_implementation.h" |
+#include "media/base/decoder_buffer.h" |
+#include "media/filters/ffmpeg_video_decoder.h" |
+#include "media/video/picture.h" |
+#include "media/video/video_decode_accelerator.h" |
+#include "ppapi/c/pp_errors.h" |
+#include "third_party/libyuv/include/libyuv.h" |
+#include "webkit/common/gpu/context_provider_web_context.h" |
+ |
+namespace content { |
+ |
+VideoDecoderProxy::PendingDecode::PendingDecode( |
+ uint32_t decode_id, |
+ const scoped_refptr<media::DecoderBuffer>& buffer) |
+ : decode_id(decode_id), buffer(buffer) { |
+} |
+ |
+VideoDecoderProxy::PendingDecode::~PendingDecode() { |
+} |
+ |
+VideoDecoderProxy::PendingFrame::PendingFrame(uint32_t decode_id, |
+ const gfx::Size& size) |
+ : decode_id(decode_id), |
+ size(size), |
+ pixels(size.width() * size.height() * 4) { |
+} |
+ |
+VideoDecoderProxy::PendingFrame::~PendingFrame() { |
+} |
+ |
+VideoDecoderProxy::Delegate::Delegate(VideoDecoderProxy* proxy) |
+ : proxy_(proxy), main_message_loop_(base::MessageLoopProxy::current()) { |
+ DCHECK(proxy_); |
+} |
+ |
+VideoDecoderProxy::Delegate::~Delegate() { |
+ DCHECK(pending_decodes_.empty()); |
+} |
+ |
+void VideoDecoderProxy::Delegate::Initialize( |
+ media::VideoDecoderConfig config, |
+ scoped_ptr<media::VideoDecoder> decoder) { |
+ DCHECK(!decoder_); |
+ decoder_.reset(decoder.release()); |
+ decoder_->Initialize( |
+ config, |
+ true /* low_delay */, |
+ base::Bind(&VideoDecoderProxy::Delegate::OnPipelineStatus, |
+ base::Unretained(this))); |
+} |
+ |
+void VideoDecoderProxy::Delegate::OnPipelineStatus( |
+ media::PipelineStatus status) { |
+ main_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::OnPipelineStatus, |
+ base::Unretained(proxy_), |
+ status)); |
+} |
+ |
+void VideoDecoderProxy::Delegate::ReceiveBuffer( |
+ uint32_t decode_id, |
+ scoped_refptr<media::DecoderBuffer> buffer) { |
+ bool decoder_busy = !pending_decodes_.empty(); |
+ pending_decodes_.push(PendingDecode(decode_id, buffer)); |
+ if (!decoder_busy) |
+ Decode(); |
+} |
+ |
+void VideoDecoderProxy::Delegate::Decode() { |
+ DCHECK(!pending_decodes_.empty()); |
+ PendingDecode& next_decode = pending_decodes_.front(); |
+ decoder_->Decode(next_decode.buffer, |
+ base::Bind(&VideoDecoderProxy::Delegate::ConvertFrame, |
+ base::Unretained(this), |
+ next_decode.decode_id)); |
+ pending_decodes_.pop(); |
+} |
+ |
+void VideoDecoderProxy::Delegate::ConvertFrame( |
+ uint32_t decode_id, |
+ media::VideoDecoder::Status status, |
+ const scoped_refptr<media::VideoFrame>& frame) { |
+ scoped_ptr<PendingFrame> pending_frame( |
+ new PendingFrame(decode_id, gfx::Size())); |
+ if (frame) { |
+ pending_frame->size = frame->coded_size(); |
+ pending_frame->pixels.resize(frame->coded_size().width() * |
+ frame->coded_size().height() * 4); |
+ // Convert the decoded frame to ARGB pixels. |
+ libyuv::I420ToARGB(frame->data(media::VideoFrame::kYPlane), |
+ frame->stride(media::VideoFrame::kYPlane), |
+ frame->data(media::VideoFrame::kUPlane), |
+ frame->stride(media::VideoFrame::kUPlane), |
+ frame->data(media::VideoFrame::kVPlane), |
+ frame->stride(media::VideoFrame::kVPlane), |
+ &pending_frame->pixels.front(), |
+ frame->coded_size().width() * 4, |
+ frame->coded_size().width(), |
+ frame->coded_size().height()); |
+ } |
+ |
+ main_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::ReceiveFrame, |
+ base::Unretained(proxy_), |
+ status, |
+ base::Passed(&pending_frame))); |
+ |
+ if (!pending_decodes_.empty()) |
+ Decode(); |
+} |
+ |
+void VideoDecoderProxy::Delegate::Reset() { |
+ decoder_->Reset(base::Bind(&VideoDecoderProxy::Delegate::OnResetComplete, |
+ base::Unretained(this))); |
+} |
+ |
+void VideoDecoderProxy::Delegate::OnResetComplete() { |
+ // Cancel all remaining decodes, and notify the host so it can free the shm |
+ // buffers. We'll clear pending frames on the main thread. |
+ while (!pending_decodes_.empty()) { |
+ PendingDecode& next_decode = pending_decodes_.front(); |
+ scoped_ptr<PendingFrame> pending_frame( |
+ new PendingFrame(next_decode.decode_id, gfx::Size())); |
+ main_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::ReceiveFrame, |
+ base::Unretained(proxy_), |
+ media::VideoDecoder::kAborted, |
+ base::Passed(&pending_frame))); |
+ pending_decodes_.pop(); |
+ } |
+ main_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::OnResetComplete, |
+ base::Unretained(proxy_))); |
+} |
+ |
+void VideoDecoderProxy::Delegate::Destroy() { |
+ DCHECK(decoder_); |
+ decoder_->Stop(); |
+ // All callbacks have been called on the media thread, and thus all tasks |
+ // posted for the main thread. This is our last task for the main thread. |
+ main_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::OnDestroyComplete, |
+ base::Unretained(proxy_))); |
+} |
+ |
+VideoDecoderProxy::VideoDecoderProxy(PepperVideoDecoderHost* host) |
+ : delegate_(this), |
+ state_(UNINITIALIZED), |
+ host_(host), |
+ media_message_loop_( |
+ RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()), |
+ context_provider_( |
+ RenderThreadImpl::current()->SharedMainThreadContextProvider()), |
+ num_pending_decodes_(0) { |
+ DCHECK(host_); |
+ DCHECK(media_message_loop_); |
+ DCHECK(context_provider_); |
+} |
+ |
+VideoDecoderProxy::~VideoDecoderProxy() { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK(!host_); |
+ // Delete any remaining video frames. |
+ while (!pending_frames_.empty()) { |
+ delete pending_frames_.front(); |
+ pending_frames_.pop(); |
+ } |
+ // Delete any remaining textures. |
+ TextureIdMap::iterator it = texture_id_map_.begin(); |
+ for (; it != texture_id_map_.end(); ++it) |
+ DeleteTexture(it->second); |
+ |
+ FlushCommandBuffer(); |
+} |
+ |
+void VideoDecoderProxy::Initialize(media::VideoCodecProfile profile) { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK_EQ(state_, UNINITIALIZED); |
+ media::VideoCodec codec = media::kUnknownVideoCodec; |
+ if (profile <= media::H264PROFILE_MAX) |
+ codec = media::kCodecH264; |
+ else if (profile <= media::VP8PROFILE_MAX) |
+ codec = media::kCodecVP8; |
+ DCHECK_NE(codec, media::kUnknownVideoCodec); |
+ |
+ media::VideoDecoderConfig config( |
+ codec, |
+ profile, |
+ media::VideoFrame::YV12, |
+ gfx::Size(32, 24), // Small sizes that won't fail. |
+ gfx::Rect(32, 24), |
+ gfx::Size(32, 24), |
+ NULL /* extra_data */, |
+ 0 /* extra_data_size */, |
+ false /* decryption */); |
+ |
+ scoped_ptr<media::VideoDecoder> decoder( |
+ new media::FFmpegVideoDecoder(media_message_loop_)); |
+ |
+ media_message_loop_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::Delegate::Initialize, |
+ base::Unretained(&delegate_), |
+ config, |
+ base::Passed(&decoder))); |
+ state_ = DECODING; |
+} |
+ |
+void VideoDecoderProxy::Decode(uint32_t decode_id, |
+ const uint8_t* buffer, |
+ uint32_t size) { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK_EQ(state_, DECODING); |
+ |
+ num_pending_decodes_++; |
+ |
+ media_message_loop_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::Delegate::ReceiveBuffer, |
+ base::Unretained(&delegate_), |
+ decode_id, |
+ media::DecoderBuffer::CopyFrom(buffer, size))); |
+} |
+ |
+void VideoDecoderProxy::AssignTextures( |
+ const std::vector<uint32_t>& texture_ids) { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK_EQ(state_, DECODING); |
+ DCHECK(texture_ids.size()); |
+ DCHECK_EQ(texture_ids.size(), pending_texture_mailboxes_.size()); |
+ uint32_t num_textures = static_cast<GLuint>(texture_ids.size()); |
+ std::vector<uint32_t> local_texture_ids(num_textures); |
+ gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
+ gles2->GenTextures(num_textures, &local_texture_ids.front()); |
+ for (uint32_t i = 0; i < num_textures; i++) { |
+ gles2->ActiveTexture(GL_TEXTURE0); |
+ gles2->BindTexture(GL_TEXTURE_2D, local_texture_ids[i]); |
+ gles2->ConsumeTextureCHROMIUM(GL_TEXTURE_2D, |
+ pending_texture_mailboxes_[i].name); |
+ // Map the plugin texture id to the local texture id. |
+ texture_id_map_.insert( |
+ std::make_pair(texture_ids[i], local_texture_ids[i])); |
+ } |
+ pending_texture_mailboxes_.clear(); |
+ available_textures_.insert( |
+ available_textures_.end(), texture_ids.begin(), texture_ids.end()); |
+ SendPictures(); |
+} |
+ |
+void VideoDecoderProxy::RecycleTexture(uint32_t texture_id) { |
+ DCHECK(RenderThreadImpl::current()); |
+ if (textures_to_dismiss_.find(texture_id) != textures_to_dismiss_.end()) { |
+ DismissTexture(texture_id); |
+ } else if (texture_id_map_.find(texture_id) != texture_id_map_.end()) { |
+ available_textures_.push_back(texture_id); |
+ SendPictures(); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VideoDecoderProxy::Flush() { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK_EQ(state_, DECODING); |
+ state_ = FLUSHING; |
+} |
+ |
+void VideoDecoderProxy::Reset() { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK_EQ(state_, DECODING); |
+ state_ = RESETTING; |
+ media_message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::Delegate::Reset, |
+ base::Unretained(&delegate_))); |
+} |
+ |
+void VideoDecoderProxy::Destroy() { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK(host_); |
+ host_ = NULL; |
+ media_message_loop_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&VideoDecoderProxy::Delegate::Destroy, |
+ base::Unretained(&delegate_))); |
+} |
+ |
+void VideoDecoderProxy::OnPipelineStatus(media::PipelineStatus status) { |
+ DCHECK(RenderThreadImpl::current()); |
+ if (!host_) |
+ return; |
+ |
+ int32_t result; |
+ switch (status) { |
+ case media::PIPELINE_OK: |
+ result = PP_OK; |
+ break; |
+ case media::DECODER_ERROR_NOT_SUPPORTED: |
+ result = PP_ERROR_NOTSUPPORTED; |
+ break; |
+ default: |
+ result = PP_ERROR_FAILED; |
+ break; |
+ } |
+ host_->OnInitializeComplete(result); |
+} |
+ |
+void VideoDecoderProxy::ReceiveFrame(media::VideoDecoder::Status status, |
+ scoped_ptr<PendingFrame> frame) { |
+ DCHECK(RenderThreadImpl::current()); |
+ if (!host_) |
+ return; |
+ |
+ num_pending_decodes_--; |
+ |
+ if (frame->pixels.size()) { |
+ if (texture_size_ != frame->size) { |
+ // If the size has changed, all current textures must be dismissed. Add |
+ // all textures to |textures_to_dismiss_| and dismiss any that aren't in |
+ // use by the plugin. We dismiss the rest as they are recycled. |
+ for (TextureIdMap::const_iterator it = texture_id_map_.begin(); |
+ it != texture_id_map_.end(); |
+ ++it) { |
+ textures_to_dismiss_.insert(it->second); |
+ } |
+ for (std::vector<uint32_t>::const_iterator it = |
+ available_textures_.begin(); |
+ it != available_textures_.end(); |
+ ++it) { |
+ DismissTexture(*it); |
+ } |
+ available_textures_.clear(); |
+ FlushCommandBuffer(); |
+ |
+ DCHECK(pending_texture_mailboxes_.empty()); |
+ const uint32_t num_textures = 8; |
+ for (uint32_t i = 0; i < num_textures; i++) |
+ pending_texture_mailboxes_.push_back(gpu::Mailbox::Generate()); |
+ |
+ host_->RequestTextures( |
+ num_textures, frame->size, GL_TEXTURE_2D, pending_texture_mailboxes_); |
+ texture_size_ = frame->size; |
+ } |
+ |
+ pending_frames_.push(frame.release()); |
+ SendPictures(); |
+ } else { |
+ host_->NotifyEndOfBitstreamBuffer(frame->decode_id); |
+ } |
+ |
+ switch (status) { |
+ case media::VideoDecoder::kOk: |
+ case media::VideoDecoder::kAborted: |
+ // This is not necessarily an error. |
+ case media::VideoDecoder::kNotEnoughData: |
+ break; |
+ case media::VideoDecoder::kDecodeError: |
+ case media::VideoDecoder::kDecryptError: |
+ host_->NotifyError(PP_ERROR_RESOURCE_FAILED); |
+ break; |
+ // No default case, to catch unhandled status values. |
+ } |
+} |
+ |
+void VideoDecoderProxy::SendPictures() { |
+ DCHECK(RenderThreadImpl::current()); |
+ if (!host_) |
+ return; |
+ |
+ while (!pending_frames_.empty() && !available_textures_.empty()) { |
+ scoped_ptr<PendingFrame> frame(pending_frames_.front()); |
+ pending_frames_.pop(); |
+ |
+ uint32_t texture_id = available_textures_.back(); |
+ available_textures_.pop_back(); |
+ |
+ uint32_t local_texture_id = texture_id_map_[texture_id]; |
+ gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
+ gles2->ActiveTexture(GL_TEXTURE0); |
+ gles2->BindTexture(GL_TEXTURE_2D, local_texture_id); |
+ gles2->TexImage2D(GL_TEXTURE_2D, |
+ 0, |
+ GL_RGBA, |
+ texture_size_.width(), |
+ texture_size_.height(), |
+ 0, |
+ GL_RGBA, |
+ GL_UNSIGNED_BYTE, |
+ &frame->pixels.front()); |
+ |
+ host_->NotifyEndOfBitstreamBuffer(frame->decode_id); |
+ host_->PictureReady(media::Picture(texture_id, frame->decode_id)); |
+ } |
+ |
+ FlushCommandBuffer(); |
+ |
+ if (state_ == FLUSHING && !num_pending_decodes_ && pending_frames_.empty()) { |
+ state_ = DECODING; |
+ host_->NotifyFlushDone(); |
+ } |
+} |
+ |
+void VideoDecoderProxy::OnResetComplete() { |
+ DCHECK(RenderThreadImpl::current()); |
+ if (!host_) |
+ return; |
+ |
+ while (!pending_frames_.empty()) { |
+ scoped_ptr<PendingFrame> frame(pending_frames_.front()); |
+ host_->NotifyEndOfBitstreamBuffer(frame->decode_id); |
+ pending_frames_.pop(); |
+ } |
+ |
+ state_ = DECODING; |
+ host_->NotifyResetDone(); |
+} |
+ |
+void VideoDecoderProxy::OnDestroyComplete() { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK(!host_); |
+ delete this; |
+} |
+ |
+void VideoDecoderProxy::DismissTexture(uint32_t texture_id) { |
+ DCHECK(host_); |
+ textures_to_dismiss_.erase(texture_id); |
+ DCHECK(texture_id_map_.find(texture_id) != texture_id_map_.end()); |
+ DeleteTexture(texture_id_map_[texture_id]); |
+ texture_id_map_.erase(texture_id); |
+ host_->DismissPictureBuffer(texture_id); |
+} |
+ |
+void VideoDecoderProxy::DeleteTexture(uint32_t texture_id) { |
+ gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
+ gles2->DeleteTextures(1, &texture_id); |
+} |
+ |
+void VideoDecoderProxy::FlushCommandBuffer() { |
+ DCHECK(RenderThreadImpl::current()); |
+ context_provider_->ContextGL()->Flush(); |
+} |
+ |
+} // namespace content |