| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_demuxer_stream_adapter.h" | 5 #include "media/mojo/services/mojo_demuxer_stream_adapter.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "media/base/decoder_buffer.h" | 13 #include "media/base/decoder_buffer.h" |
| 14 #include "media/mojo/common/media_type_converters.h" | 14 #include "media/mojo/common/media_type_converters.h" |
| 15 #include "media/mojo/common/mojo_decoder_buffer_converter.h" |
| 15 #include "mojo/public/cpp/system/data_pipe.h" | 16 #include "mojo/public/cpp/system/data_pipe.h" |
| 16 | 17 |
| 17 namespace media { | 18 namespace media { |
| 18 | 19 |
| 19 MojoDemuxerStreamAdapter::MojoDemuxerStreamAdapter( | 20 MojoDemuxerStreamAdapter::MojoDemuxerStreamAdapter( |
| 20 mojom::DemuxerStreamPtr demuxer_stream, | 21 mojom::DemuxerStreamPtr demuxer_stream, |
| 21 const base::Closure& stream_ready_cb) | 22 const base::Closure& stream_ready_cb) |
| 22 : demuxer_stream_(std::move(demuxer_stream)), | 23 : demuxer_stream_(std::move(demuxer_stream)), |
| 23 stream_ready_cb_(stream_ready_cb), | 24 stream_ready_cb_(stream_ready_cb), |
| 24 type_(DemuxerStream::UNKNOWN), | 25 type_(DemuxerStream::UNKNOWN), |
| 25 weak_factory_(this) { | 26 weak_factory_(this) { |
| 26 DVLOG(1) << __FUNCTION__; | 27 DVLOG(1) << __FUNCTION__; |
| 27 demuxer_stream_->Initialize(base::Bind( | 28 demuxer_stream_->Initialize(base::Bind( |
| 28 &MojoDemuxerStreamAdapter::OnStreamReady, weak_factory_.GetWeakPtr())); | 29 &MojoDemuxerStreamAdapter::OnStreamReady, weak_factory_.GetWeakPtr())); |
| 29 } | 30 } |
| 30 | 31 |
| 31 MojoDemuxerStreamAdapter::~MojoDemuxerStreamAdapter() { | 32 MojoDemuxerStreamAdapter::~MojoDemuxerStreamAdapter() { |
| 32 DVLOG(1) << __FUNCTION__; | 33 DVLOG(1) << __FUNCTION__; |
| 33 } | 34 } |
| 34 | 35 |
| 35 void MojoDemuxerStreamAdapter::Read(const DemuxerStream::ReadCB& read_cb) { | 36 void MojoDemuxerStreamAdapter::Read(const DemuxerStream::ReadCB& read_cb) { |
| 36 DVLOG(3) << __FUNCTION__; | 37 DVLOG(3) << __FUNCTION__; |
| 37 // We shouldn't be holding on to a previous callback if a new Read() came in. | 38 // We shouldn't be holding on to a previous callback if a new Read() came in. |
| 38 DCHECK(read_cb_.is_null()); | 39 DCHECK(read_cb_.is_null()); |
| 39 | 40 |
| 40 DCHECK(stream_pipe_.is_valid()); | |
| 41 read_cb_ = read_cb; | 41 read_cb_ = read_cb; |
| 42 demuxer_stream_->Read(base::Bind(&MojoDemuxerStreamAdapter::OnBufferReady, | 42 demuxer_stream_->Read(base::Bind(&MojoDemuxerStreamAdapter::OnBufferReady, |
| 43 weak_factory_.GetWeakPtr())); | 43 weak_factory_.GetWeakPtr())); |
| 44 } | 44 } |
| 45 | 45 |
| 46 AudioDecoderConfig MojoDemuxerStreamAdapter::audio_decoder_config() { | 46 AudioDecoderConfig MojoDemuxerStreamAdapter::audio_decoder_config() { |
| 47 DCHECK_EQ(type_, DemuxerStream::AUDIO); | 47 DCHECK_EQ(type_, DemuxerStream::AUDIO); |
| 48 return audio_config_; | 48 return audio_config_; |
| 49 } | 49 } |
| 50 | 50 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 66 } | 66 } |
| 67 | 67 |
| 68 VideoRotation MojoDemuxerStreamAdapter::video_rotation() { | 68 VideoRotation MojoDemuxerStreamAdapter::video_rotation() { |
| 69 NOTIMPLEMENTED(); | 69 NOTIMPLEMENTED(); |
| 70 return VIDEO_ROTATION_0; | 70 return VIDEO_ROTATION_0; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // TODO(xhwang): Pass liveness here. | 73 // TODO(xhwang): Pass liveness here. |
| 74 void MojoDemuxerStreamAdapter::OnStreamReady( | 74 void MojoDemuxerStreamAdapter::OnStreamReady( |
| 75 mojom::DemuxerStream::Type type, | 75 mojom::DemuxerStream::Type type, |
| 76 mojo::ScopedDataPipeConsumerHandle pipe, | 76 mojo::ScopedDataPipeConsumerHandle consumer_handle, |
| 77 mojom::AudioDecoderConfigPtr audio_config, | 77 mojom::AudioDecoderConfigPtr audio_config, |
| 78 mojom::VideoDecoderConfigPtr video_config) { | 78 mojom::VideoDecoderConfigPtr video_config) { |
| 79 DVLOG(1) << __FUNCTION__; | 79 DVLOG(1) << __FUNCTION__; |
| 80 DCHECK(pipe.is_valid()); | |
| 81 DCHECK_EQ(DemuxerStream::UNKNOWN, type_); | 80 DCHECK_EQ(DemuxerStream::UNKNOWN, type_); |
| 81 DCHECK(consumer_handle.is_valid()); |
| 82 | 82 |
| 83 type_ = static_cast<DemuxerStream::Type>(type); | 83 type_ = static_cast<DemuxerStream::Type>(type); |
| 84 stream_pipe_ = std::move(pipe); | 84 |
| 85 mojo_decoder_buffer_reader_.reset( |
| 86 new MojoDecoderBufferReader(std::move(consumer_handle))); |
| 87 |
| 85 UpdateConfig(std::move(audio_config), std::move(video_config)); | 88 UpdateConfig(std::move(audio_config), std::move(video_config)); |
| 86 | 89 |
| 87 stream_ready_cb_.Run(); | 90 stream_ready_cb_.Run(); |
| 88 } | 91 } |
| 89 | 92 |
| 90 void MojoDemuxerStreamAdapter::OnBufferReady( | 93 void MojoDemuxerStreamAdapter::OnBufferReady( |
| 91 mojom::DemuxerStream::Status status, | 94 mojom::DemuxerStream::Status status, |
| 92 mojom::DecoderBufferPtr buffer, | 95 mojom::DecoderBufferPtr buffer, |
| 93 mojom::AudioDecoderConfigPtr audio_config, | 96 mojom::AudioDecoderConfigPtr audio_config, |
| 94 mojom::VideoDecoderConfigPtr video_config) { | 97 mojom::VideoDecoderConfigPtr video_config) { |
| 95 DVLOG(3) << __FUNCTION__; | 98 DVLOG(3) << __FUNCTION__; |
| 96 DCHECK(!read_cb_.is_null()); | 99 DCHECK(!read_cb_.is_null()); |
| 97 DCHECK_NE(type_, DemuxerStream::UNKNOWN); | 100 DCHECK_NE(type_, DemuxerStream::UNKNOWN); |
| 98 DCHECK(stream_pipe_.is_valid()); | |
| 99 | 101 |
| 100 if (status == mojom::DemuxerStream::Status::CONFIG_CHANGED) { | 102 if (status == mojom::DemuxerStream::Status::CONFIG_CHANGED) { |
| 101 UpdateConfig(std::move(audio_config), std::move(video_config)); | 103 UpdateConfig(std::move(audio_config), std::move(video_config)); |
| 102 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kConfigChanged, nullptr); | 104 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kConfigChanged, nullptr); |
| 103 return; | 105 return; |
| 104 } | 106 } |
| 105 | 107 |
| 106 if (status == mojom::DemuxerStream::Status::ABORTED) { | 108 if (status == mojom::DemuxerStream::Status::ABORTED) { |
| 107 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, nullptr); | 109 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, nullptr); |
| 108 return; | 110 return; |
| 109 } | 111 } |
| 110 | 112 |
| 111 DCHECK_EQ(status, mojom::DemuxerStream::Status::OK); | 113 DCHECK_EQ(status, mojom::DemuxerStream::Status::OK); |
| 112 scoped_refptr<DecoderBuffer> media_buffer( | |
| 113 buffer.To<scoped_refptr<DecoderBuffer>>()); | |
| 114 | 114 |
| 115 if (media_buffer->end_of_stream()) { | 115 scoped_refptr<DecoderBuffer> media_buffer = |
| 116 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk, media_buffer); | 116 mojo_decoder_buffer_reader_->ReadDecoderBuffer(buffer); |
| 117 return; | 117 if (!media_buffer) { |
| 118 } | |
| 119 | |
| 120 DCHECK_GT(media_buffer->data_size(), 0u); | |
| 121 | |
| 122 // Wait for the data to become available in the DataPipe. | |
| 123 MojoHandleSignalsState state; | |
| 124 MojoResult result = | |
| 125 MojoWait(stream_pipe_.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 126 MOJO_DEADLINE_INDEFINITE, &state); | |
| 127 if (result != MOJO_RESULT_OK) { | |
| 128 DVLOG(1) << __FUNCTION__ << ": Peer closed the data pipe"; | |
| 129 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, nullptr); | 118 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, nullptr); |
| 130 return; | 119 return; |
| 131 } | 120 } |
| 132 | |
| 133 // Read the inner data for the DecoderBuffer from our DataPipe. | |
| 134 uint32_t bytes_to_read = | |
| 135 base::checked_cast<uint32_t>(media_buffer->data_size()); | |
| 136 uint32_t bytes_read = bytes_to_read; | |
| 137 result = ReadDataRaw(stream_pipe_.get(), media_buffer->writable_data(), | |
| 138 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE); | |
| 139 if (result != MOJO_RESULT_OK || bytes_read != bytes_to_read) { | |
| 140 DVLOG(1) << __FUNCTION__ << ": reading from pipe failed"; | |
| 141 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kAborted, nullptr); | |
| 142 return; | |
| 143 } | |
| 144 | 121 |
| 145 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk, media_buffer); | 122 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk, media_buffer); |
| 146 } | 123 } |
| 147 | 124 |
| 148 void MojoDemuxerStreamAdapter::UpdateConfig( | 125 void MojoDemuxerStreamAdapter::UpdateConfig( |
| 149 mojom::AudioDecoderConfigPtr audio_config, | 126 mojom::AudioDecoderConfigPtr audio_config, |
| 150 mojom::VideoDecoderConfigPtr video_config) { | 127 mojom::VideoDecoderConfigPtr video_config) { |
| 151 DCHECK_NE(type_, DemuxerStream::UNKNOWN); | 128 DCHECK_NE(type_, DemuxerStream::UNKNOWN); |
| 152 | 129 |
| 153 switch(type_) { | 130 switch(type_) { |
| 154 case DemuxerStream::AUDIO: | 131 case DemuxerStream::AUDIO: |
| 155 DCHECK(audio_config && !video_config); | 132 DCHECK(audio_config && !video_config); |
| 156 audio_config_ = audio_config.To<AudioDecoderConfig>(); | 133 audio_config_ = audio_config.To<AudioDecoderConfig>(); |
| 157 break; | 134 break; |
| 158 case DemuxerStream::VIDEO: | 135 case DemuxerStream::VIDEO: |
| 159 DCHECK(video_config && !audio_config); | 136 DCHECK(video_config && !audio_config); |
| 160 video_config_ = video_config.To<VideoDecoderConfig>(); | 137 video_config_ = video_config.To<VideoDecoderConfig>(); |
| 161 break; | 138 break; |
| 162 default: | 139 default: |
| 163 NOTREACHED(); | 140 NOTREACHED(); |
| 164 } | 141 } |
| 165 } | 142 } |
| 166 | 143 |
| 167 } // namespace media | 144 } // namespace media |
| OLD | NEW |