| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/mojo/services/mojo_audio_decoder_service.h" | 5 #include "media/mojo/services/mojo_audio_decoder_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/cdm_context.h" | 10 #include "media/base/cdm_context.h" |
| 11 #include "media/base/media_keys.h" | 11 #include "media/base/media_keys.h" |
| 12 #include "media/mojo/common/media_type_converters.h" | 12 #include "media/mojo/common/media_type_converters.h" |
| 13 #include "media/mojo/common/mojo_decoder_buffer_converter.h" |
| 13 #include "media/mojo/services/mojo_cdm_service_context.h" | 14 #include "media/mojo/services/mojo_cdm_service_context.h" |
| 14 | 15 |
| 15 namespace media { | 16 namespace media { |
| 16 | 17 |
| 17 MojoAudioDecoderService::MojoAudioDecoderService( | 18 MojoAudioDecoderService::MojoAudioDecoderService( |
| 18 base::WeakPtr<MojoCdmServiceContext> mojo_cdm_service_context, | 19 base::WeakPtr<MojoCdmServiceContext> mojo_cdm_service_context, |
| 19 std::unique_ptr<media::AudioDecoder> decoder, | 20 std::unique_ptr<media::AudioDecoder> decoder, |
| 20 mojo::InterfaceRequest<mojom::AudioDecoder> request) | 21 mojo::InterfaceRequest<mojom::AudioDecoder> request) |
| 21 : binding_(this, std::move(request)), | 22 : binding_(this, std::move(request)), |
| 22 mojo_cdm_service_context_(mojo_cdm_service_context), | 23 mojo_cdm_service_context_(mojo_cdm_service_context), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 decoder_->Initialize( | 65 decoder_->Initialize( |
| 65 config.To<media::AudioDecoderConfig>(), cdm_context, | 66 config.To<media::AudioDecoderConfig>(), cdm_context, |
| 66 base::Bind(&MojoAudioDecoderService::OnInitialized, weak_this_, callback, | 67 base::Bind(&MojoAudioDecoderService::OnInitialized, weak_this_, callback, |
| 67 cdm), | 68 cdm), |
| 68 base::Bind(&MojoAudioDecoderService::OnAudioBufferReady, weak_this_)); | 69 base::Bind(&MojoAudioDecoderService::OnAudioBufferReady, weak_this_)); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void MojoAudioDecoderService::SetDataSource( | 72 void MojoAudioDecoderService::SetDataSource( |
| 72 mojo::ScopedDataPipeConsumerHandle receive_pipe) { | 73 mojo::ScopedDataPipeConsumerHandle receive_pipe) { |
| 73 DVLOG(1) << __FUNCTION__; | 74 DVLOG(1) << __FUNCTION__; |
| 74 consumer_handle_ = std::move(receive_pipe); | 75 |
| 76 mojo_decoder_buffer_reader_.reset( |
| 77 new MojoDecoderBufferReader(std::move(receive_pipe))); |
| 75 } | 78 } |
| 76 | 79 |
| 77 void MojoAudioDecoderService::Decode(mojom::DecoderBufferPtr buffer, | 80 void MojoAudioDecoderService::Decode(mojom::DecoderBufferPtr buffer, |
| 78 const DecodeCallback& callback) { | 81 const DecodeCallback& callback) { |
| 79 DVLOG(3) << __FUNCTION__; | 82 DVLOG(3) << __FUNCTION__; |
| 80 | 83 |
| 81 scoped_refptr<DecoderBuffer> media_buffer = | 84 scoped_refptr<DecoderBuffer> media_buffer = |
| 82 ReadDecoderBuffer(std::move(buffer)); | 85 mojo_decoder_buffer_reader_->ConvertDecoderBuffer(buffer); |
| 83 if (!media_buffer) { | 86 if (!media_buffer) { |
| 84 callback.Run(mojom::DecodeStatus::DECODE_ERROR); | 87 callback.Run(mojom::DecodeStatus::DECODE_ERROR); |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 87 | 90 |
| 88 decoder_->Decode(media_buffer, | 91 decoder_->Decode(media_buffer, |
| 89 base::Bind(&MojoAudioDecoderService::OnDecodeStatus, | 92 base::Bind(&MojoAudioDecoderService::OnDecodeStatus, |
| 90 weak_this_, callback)); | 93 weak_this_, callback)); |
| 91 } | 94 } |
| 92 | 95 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 122 } | 125 } |
| 123 | 126 |
| 124 void MojoAudioDecoderService::OnAudioBufferReady( | 127 void MojoAudioDecoderService::OnAudioBufferReady( |
| 125 const scoped_refptr<AudioBuffer>& audio_buffer) { | 128 const scoped_refptr<AudioBuffer>& audio_buffer) { |
| 126 DVLOG(1) << __FUNCTION__; | 129 DVLOG(1) << __FUNCTION__; |
| 127 | 130 |
| 128 // TODO(timav): Use DataPipe. | 131 // TODO(timav): Use DataPipe. |
| 129 client_->OnBufferDecoded(mojom::AudioBuffer::From(audio_buffer)); | 132 client_->OnBufferDecoded(mojom::AudioBuffer::From(audio_buffer)); |
| 130 } | 133 } |
| 131 | 134 |
| 132 scoped_refptr<DecoderBuffer> MojoAudioDecoderService::ReadDecoderBuffer( | |
| 133 mojom::DecoderBufferPtr buffer) { | |
| 134 scoped_refptr<DecoderBuffer> media_buffer( | |
| 135 buffer.To<scoped_refptr<DecoderBuffer>>()); | |
| 136 | |
| 137 if (media_buffer->end_of_stream()) | |
| 138 return media_buffer; | |
| 139 | |
| 140 // Wait for the data to become available in the DataPipe. | |
| 141 MojoHandleSignalsState state; | |
| 142 MojoResult result = | |
| 143 MojoWait(consumer_handle_.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 144 MOJO_DEADLINE_INDEFINITE, &state); | |
| 145 | |
| 146 if (result != MOJO_RESULT_OK) { | |
| 147 DVLOG(1) << __FUNCTION__ << ": Peer closed the data pipe"; | |
| 148 return nullptr; | |
| 149 } | |
| 150 | |
| 151 // Read the inner data for the DecoderBuffer from our DataPipe. | |
| 152 uint32_t data_size = static_cast<uint32_t>(media_buffer->data_size()); | |
| 153 DCHECK_EQ(data_size, buffer->data_size); | |
| 154 DCHECK_GT(data_size, 0u); | |
| 155 | |
| 156 uint32_t bytes_read = data_size; | |
| 157 result = ReadDataRaw(consumer_handle_.get(), media_buffer->writable_data(), | |
| 158 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE); | |
| 159 if (result != MOJO_RESULT_OK || bytes_read != data_size) { | |
| 160 DVLOG(1) << __FUNCTION__ << ": reading from pipe failed"; | |
| 161 return nullptr; | |
| 162 } | |
| 163 | |
| 164 return media_buffer; | |
| 165 } | |
| 166 | |
| 167 } // namespace media | 135 } // namespace media |
| OLD | NEW |