Index: media/mojo/services/mojo_video_decoder_service.cc |
diff --git a/media/mojo/services/mojo_video_decoder_service.cc b/media/mojo/services/mojo_video_decoder_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0cc2067cc051c1bd3899704a4c1daecc7fdf0d4e |
--- /dev/null |
+++ b/media/mojo/services/mojo_video_decoder_service.cc |
@@ -0,0 +1,134 @@ |
+// Copyright 2016 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 "media/mojo/services/mojo_video_decoder_service.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/logging.h" |
+#include "media/base/decoder_buffer.h" |
+#include "media/base/video_decoder.h" |
+#include "media/base/video_decoder_config.h" |
+#include "media/base/video_frame.h" |
+#include "media/mojo/common/media_type_converters.h" |
+#include "media/mojo/services/mojo_media_client.h" |
+#include "mojo/public/c/system/types.h" |
+#include "mojo/public/cpp/system/buffer.h" |
+#include "mojo/public/cpp/system/handle.h" |
+ |
+namespace media { |
+ |
+MojoVideoDecoderService::MojoVideoDecoderService( |
+ mojo::InterfaceRequest<interfaces::VideoDecoder> request, |
+ MojoMediaClient* mojo_media_client) |
+ : binding_(this, std::move(request)), |
+ mojo_media_client_(mojo_media_client), |
+ weak_factory_(this) { |
+ weak_this_ = weak_factory_.GetWeakPtr(); |
+} |
+ |
+MojoVideoDecoderService::~MojoVideoDecoderService() {} |
+ |
+void MojoVideoDecoderService::Initialize( |
+ interfaces::VideoDecoderClientPtr client, |
+ mojo::ScopedDataPipeConsumerHandle decoder_buffer_pipe) { |
+ DVLOG(1) << __FUNCTION__; |
+ CHECK(!decoder_); |
+ |
+ // TODO(sandersd): Provide callback for requesting a stub. |
+ decoder_ = mojo_media_client_->CreateVideoDecoder( |
+ base::ThreadTaskRunnerHandle::Get()); |
+ |
+ client_ = std::move(client); |
+ decoder_buffer_pipe_ = std::move(decoder_buffer_pipe); |
+} |
+ |
+void MojoVideoDecoderService::Configure( |
+ interfaces::VideoDecoderConfigPtr config, |
+ bool low_delay, |
+ const ConfigureCallback& callback) { |
+ DVLOG(1) << __FUNCTION__; |
+ |
+ if (!decoder_) { |
+ callback.Run(false); |
+ return; |
+ } |
+ |
+ decoder_->Initialize( |
+ config.To<VideoDecoderConfig>(), low_delay, nullptr, |
+ base::Bind(&MojoVideoDecoderService::OnDecoderInitialized, |
+ base::Unretained(this), callback), |
xhwang
2016/05/05 17:26:37
weak_this_?
|
+ base::Bind(&MojoVideoDecoderService::OnDecoderOutput, |
+ base::Unretained(this))); |
+} |
+ |
+void MojoVideoDecoderService::OnDecoderInitialized( |
+ const ConfigureCallback& callback, |
+ bool success) { |
+ DVLOG(1) << __FUNCTION__; |
+ callback.Run(success); |
+} |
+ |
+void MojoVideoDecoderService::OnDecoderOutput( |
+ const scoped_refptr<VideoFrame>& frame) { |
+ DVLOG(1) << __FUNCTION__; |
+ client_->OnVideoFrameDecoded(interfaces::VideoFrame::From(frame)); |
+} |
+ |
+void MojoVideoDecoderService::Decode(interfaces::DecoderBufferPtr buffer, |
+ const DecodeCallback& callback) { |
+ DVLOG(1) << __FUNCTION__; |
+ |
+ if (!decoder_) { |
+ callback.Run(interfaces::DecodeStatus::DECODE_ERROR); |
+ return; |
+ } |
+ |
+ scoped_refptr<DecoderBuffer> media_buffer( |
+ buffer.To<scoped_refptr<DecoderBuffer>>()); |
+ if (!media_buffer->end_of_stream()) { |
+ MojoHandleSignalsState state; |
+ CHECK_EQ( |
+ MojoWait(decoder_buffer_pipe_.get().value(), |
+ MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, &state), |
+ MOJO_RESULT_OK); |
+ CHECK_EQ(state.satisfied_signals, MOJO_HANDLE_SIGNAL_READABLE); |
+ uint32_t data_size = |
+ base::checked_cast<uint32_t>(media_buffer->data_size()); |
+ DCHECK_GT(data_size, 0u); |
+ uint32_t bytes_read = data_size; |
+ CHECK_EQ( |
+ ReadDataRaw(decoder_buffer_pipe_.get(), media_buffer->writable_data(), |
+ &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
+ MOJO_RESULT_OK); |
+ CHECK_EQ(bytes_read, data_size); |
+ } |
+ |
+ decoder_->Decode(media_buffer, |
+ base::Bind(&MojoVideoDecoderService::OnDecoderDecoded, |
+ base::Unretained(this), callback)); |
xhwang
2016/05/05 17:26:37
weak_this_?
sandersd (OOO until July 31)
2016/05/06 01:23:55
I totally forgot that I already set up a weak poin
|
+} |
+ |
+void MojoVideoDecoderService::OnDecoderDecoded(const DecodeCallback& callback, |
+ DecodeStatus status) { |
+ DVLOG(1) << __FUNCTION__; |
+ callback.Run(static_cast<interfaces::DecodeStatus>(status)); |
+} |
+ |
+void MojoVideoDecoderService::Reset(const ResetCallback& callback) { |
+ DVLOG(1) << __FUNCTION__; |
+ if (!decoder_) { |
+ callback.Run(); |
+ return; |
+ } |
+ decoder_->Reset(base::Bind(&MojoVideoDecoderService::OnDecoderReset, |
+ base::Unretained(this), callback)); |
+} |
+ |
+void MojoVideoDecoderService::OnDecoderReset(const ResetCallback& callback) { |
+ DVLOG(1) << __FUNCTION__; |
+ callback.Run(); |
+} |
+ |
+} // namespace media |