OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "media/mojo/services/mojo_video_decoder_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "media/base/decoder_buffer.h" |
| 12 #include "media/base/video_decoder.h" |
| 13 #include "media/base/video_decoder_config.h" |
| 14 #include "media/base/video_frame.h" |
| 15 #include "media/mojo/common/media_type_converters.h" |
| 16 #include "media/mojo/services/mojo_media_client.h" |
| 17 #include "mojo/public/c/system/types.h" |
| 18 #include "mojo/public/cpp/system/buffer.h" |
| 19 #include "mojo/public/cpp/system/handle.h" |
| 20 |
| 21 namespace media { |
| 22 |
| 23 MojoVideoDecoderService::MojoVideoDecoderService( |
| 24 mojo::InterfaceRequest<mojom::VideoDecoder> request, |
| 25 MojoMediaClient* mojo_media_client) |
| 26 : binding_(this, std::move(request)), |
| 27 mojo_media_client_(mojo_media_client), |
| 28 weak_factory_(this) { |
| 29 weak_this_ = weak_factory_.GetWeakPtr(); |
| 30 } |
| 31 |
| 32 MojoVideoDecoderService::~MojoVideoDecoderService() {} |
| 33 |
| 34 void MojoVideoDecoderService::Construct( |
| 35 mojom::VideoDecoderClientPtr client, |
| 36 mojo::ScopedDataPipeConsumerHandle decoder_buffer_pipe) { |
| 37 DVLOG(1) << __FUNCTION__; |
| 38 |
| 39 if (decoder_) |
| 40 return; |
| 41 |
| 42 // TODO(sandersd): Provide callback for requesting a stub. |
| 43 decoder_ = mojo_media_client_->CreateVideoDecoder( |
| 44 base::ThreadTaskRunnerHandle::Get()); |
| 45 |
| 46 client_ = std::move(client); |
| 47 decoder_buffer_pipe_ = std::move(decoder_buffer_pipe); |
| 48 } |
| 49 |
| 50 void MojoVideoDecoderService::Initialize(mojom::VideoDecoderConfigPtr config, |
| 51 bool low_delay, |
| 52 const InitializeCallback& callback) { |
| 53 DVLOG(1) << __FUNCTION__; |
| 54 |
| 55 if (!decoder_) { |
| 56 callback.Run(false); |
| 57 return; |
| 58 } |
| 59 |
| 60 decoder_->Initialize( |
| 61 config.To<VideoDecoderConfig>(), low_delay, nullptr, |
| 62 base::Bind(&MojoVideoDecoderService::OnDecoderInitialized, weak_this_, |
| 63 callback), |
| 64 base::Bind(&MojoVideoDecoderService::OnDecoderOutput, weak_this_)); |
| 65 } |
| 66 |
| 67 void MojoVideoDecoderService::OnDecoderInitialized( |
| 68 const InitializeCallback& callback, |
| 69 bool success) { |
| 70 DVLOG(1) << __FUNCTION__; |
| 71 callback.Run(success); |
| 72 } |
| 73 |
| 74 void MojoVideoDecoderService::OnDecoderOutput( |
| 75 const scoped_refptr<VideoFrame>& frame) { |
| 76 DVLOG(1) << __FUNCTION__; |
| 77 DCHECK(client_); |
| 78 client_->OnVideoFrameDecoded(mojom::VideoFrame::From(frame)); |
| 79 } |
| 80 |
| 81 void MojoVideoDecoderService::Decode(mojom::DecoderBufferPtr buffer, |
| 82 const DecodeCallback& callback) { |
| 83 DVLOG(1) << __FUNCTION__; |
| 84 |
| 85 if (!decoder_) { |
| 86 callback.Run(mojom::DecodeStatus::DECODE_ERROR); |
| 87 return; |
| 88 } |
| 89 |
| 90 // TODO(sandersd): After a decode error, we should enter an error state and |
| 91 // reject all future method calls. |
| 92 // TODO(sandersd): Extract and share with MojoAudioDecoderService. |
| 93 scoped_refptr<DecoderBuffer> media_buffer( |
| 94 buffer.To<scoped_refptr<DecoderBuffer>>()); |
| 95 if (!media_buffer->end_of_stream()) { |
| 96 MojoResult result; |
| 97 MojoHandleSignalsState state; |
| 98 |
| 99 // TODO(sandersd): Do not wait indefinitely. |
| 100 result = |
| 101 MojoWait(decoder_buffer_pipe_.get().value(), |
| 102 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, &state); |
| 103 if (result != MOJO_RESULT_OK || |
| 104 !(state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)) { |
| 105 callback.Run(mojom::DecodeStatus::DECODE_ERROR); |
| 106 return; |
| 107 } |
| 108 |
| 109 uint32_t data_size = buffer->data_size; |
| 110 uint32_t bytes_read = data_size; |
| 111 DCHECK_EQ(data_size, media_buffer->data_size()); |
| 112 result = |
| 113 ReadDataRaw(decoder_buffer_pipe_.get(), media_buffer->writable_data(), |
| 114 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE); |
| 115 if (result != MOJO_RESULT_OK || bytes_read != data_size) { |
| 116 callback.Run(mojom::DecodeStatus::DECODE_ERROR); |
| 117 return; |
| 118 } |
| 119 } |
| 120 |
| 121 decoder_->Decode(media_buffer, |
| 122 base::Bind(&MojoVideoDecoderService::OnDecoderDecoded, |
| 123 weak_this_, callback)); |
| 124 } |
| 125 |
| 126 void MojoVideoDecoderService::OnDecoderDecoded(const DecodeCallback& callback, |
| 127 DecodeStatus status) { |
| 128 DVLOG(1) << __FUNCTION__; |
| 129 callback.Run(static_cast<mojom::DecodeStatus>(status)); |
| 130 } |
| 131 |
| 132 void MojoVideoDecoderService::Reset(const ResetCallback& callback) { |
| 133 DVLOG(1) << __FUNCTION__; |
| 134 |
| 135 if (!decoder_) { |
| 136 callback.Run(); |
| 137 return; |
| 138 } |
| 139 |
| 140 decoder_->Reset(base::Bind(&MojoVideoDecoderService::OnDecoderReset, |
| 141 weak_this_, callback)); |
| 142 } |
| 143 |
| 144 void MojoVideoDecoderService::OnDecoderReset(const ResetCallback& callback) { |
| 145 DVLOG(1) << __FUNCTION__; |
| 146 callback.Run(); |
| 147 } |
| 148 |
| 149 } // namespace media |
OLD | NEW |