| 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_impl.h" | 5 #include "media/mojo/services/mojo_demuxer_stream_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "media/base/audio_decoder_config.h" | 9 #include "media/base/audio_decoder_config.h" |
| 10 #include "media/base/decoder_buffer.h" | 10 #include "media/base/decoder_buffer.h" |
| 11 #include "media/base/video_decoder_config.h" | 11 #include "media/base/video_decoder_config.h" |
| 12 #include "media/mojo/interfaces/demuxer_stream.mojom.h" | 12 #include "media/mojo/interfaces/demuxer_stream.mojom.h" |
| 13 #include "media/mojo/services/media_type_converters.h" | 13 #include "media/mojo/services/media_type_converters.h" |
| 14 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h" | 14 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h" |
| 15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" | 15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" |
| 16 | 16 |
| 17 namespace media { | 17 namespace media { |
| 18 | 18 |
| 19 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl(media::DemuxerStream* stream) | 19 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl(media::DemuxerStream* stream) |
| 20 : stream_(stream), weak_factory_(this) { | 20 : stream_(stream), weak_factory_(this) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() { | 23 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() { |
| 24 } | 24 } |
| 25 | 25 |
| 26 void MojoDemuxerStreamImpl::Read(const mojo::Callback< | 26 // This is called when our DemuxerStreamClient has connected itself and is |
| 27 void(mojo::DemuxerStream::Status, mojo::MediaDecoderBufferPtr)>& callback) { | 27 // ready to receive messages. Send an initial config and notify it that |
| 28 // we are now ready for business. |
| 29 void MojoDemuxerStreamImpl::Initialize(const InitializeCallback& callback) { |
| 30 DVLOG(2) << __FUNCTION__; |
| 31 MojoCreateDataPipeOptions options; |
| 32 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 33 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 34 options.element_num_bytes = 1; |
| 35 |
| 36 // Allocate DataPipe sizes based on content type to reduce overhead. If this |
| 37 // is still too burdensome we can adjust for sample rate or resolution. |
| 38 if (stream_->type() == media::DemuxerStream::VIDEO) { |
| 39 // Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in |
| 40 // size; so allow for 50% headroom. |
| 41 options.capacity_num_bytes = 1.5 * (1024 * 1024); |
| 42 } else { |
| 43 // Other types don't require a lot of room, so use a smaller pipe. |
| 44 options.capacity_num_bytes = 512 * 1024; |
| 45 } |
| 46 |
| 47 mojo::DataPipe data_pipe(options); |
| 48 stream_pipe_ = data_pipe.producer_handle.Pass(); |
| 49 |
| 50 // Prepare the initial config. |
| 51 mojo::AudioDecoderConfigPtr audio_config; |
| 52 mojo::VideoDecoderConfigPtr video_config; |
| 53 if (stream_->type() == media::DemuxerStream::AUDIO) { |
| 54 audio_config = |
| 55 mojo::AudioDecoderConfig::From(stream_->audio_decoder_config()); |
| 56 } else if (stream_->type() == media::DemuxerStream::VIDEO) { |
| 57 video_config = |
| 58 mojo::VideoDecoderConfig::From(stream_->video_decoder_config()); |
| 59 } else { |
| 60 NOTREACHED() << "Unsupported stream type: " << stream_->type(); |
| 61 return; |
| 62 } |
| 63 |
| 64 callback.Run(static_cast<mojo::DemuxerStream::Type>(stream_->type()), |
| 65 data_pipe.consumer_handle.Pass(), audio_config.Pass(), |
| 66 video_config.Pass()); |
| 67 } |
| 68 |
| 69 void MojoDemuxerStreamImpl::Read(const ReadCallback& callback) { |
| 28 stream_->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady, | 70 stream_->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady, |
| 29 weak_factory_.GetWeakPtr(), | 71 weak_factory_.GetWeakPtr(), callback)); |
| 30 callback)); | |
| 31 } | 72 } |
| 32 | 73 |
| 33 void MojoDemuxerStreamImpl::OnBufferReady( | 74 void MojoDemuxerStreamImpl::OnBufferReady( |
| 34 const BufferReadyCB& callback, | 75 const ReadCallback& callback, |
| 35 media::DemuxerStream::Status status, | 76 media::DemuxerStream::Status status, |
| 36 const scoped_refptr<media::DecoderBuffer>& buffer) { | 77 const scoped_refptr<media::DecoderBuffer>& buffer) { |
| 78 mojo::AudioDecoderConfigPtr audio_config; |
| 79 mojo::VideoDecoderConfigPtr video_config; |
| 80 |
| 37 if (status == media::DemuxerStream::kConfigChanged) { | 81 if (status == media::DemuxerStream::kConfigChanged) { |
| 82 DVLOG(2) << __FUNCTION__ << ": ConfigChange!"; |
| 38 // Send the config change so our client can read it once it parses the | 83 // Send the config change so our client can read it once it parses the |
| 39 // Status obtained via Run() below. | 84 // Status obtained via Run() below. |
| 40 if (stream_->type() == media::DemuxerStream::AUDIO) { | 85 if (stream_->type() == media::DemuxerStream::AUDIO) { |
| 41 observer_->OnAudioDecoderConfigChanged( | 86 audio_config = |
| 42 mojo::AudioDecoderConfig::From(stream_->audio_decoder_config())); | 87 mojo::AudioDecoderConfig::From(stream_->audio_decoder_config()); |
| 43 } else if (stream_->type() == media::DemuxerStream::VIDEO) { | 88 } else if (stream_->type() == media::DemuxerStream::VIDEO) { |
| 44 observer_->OnVideoDecoderConfigChanged( | 89 video_config = |
| 45 mojo::VideoDecoderConfig::From(stream_->video_decoder_config())); | 90 mojo::VideoDecoderConfig::From(stream_->video_decoder_config()); |
| 46 } else { | 91 } else { |
| 47 NOTREACHED() << "Unsupported config change encountered for type: " | 92 NOTREACHED() << "Unsupported config change encountered for type: " |
| 48 << stream_->type(); | 93 << stream_->type(); |
| 49 } | 94 } |
| 50 | 95 |
| 51 callback.Run(mojo::DemuxerStream::STATUS_CONFIG_CHANGED, | 96 callback.Run(mojo::DemuxerStream::STATUS_CONFIG_CHANGED, |
| 52 mojo::MediaDecoderBufferPtr()); | 97 mojo::MediaDecoderBufferPtr(), audio_config.Pass(), |
| 98 video_config.Pass()); |
| 53 return; | 99 return; |
| 54 } | 100 } |
| 55 | 101 |
| 56 if (status == media::DemuxerStream::kAborted) { | 102 if (status == media::DemuxerStream::kAborted) { |
| 57 callback.Run(mojo::DemuxerStream::STATUS_ABORTED, | 103 callback.Run(mojo::DemuxerStream::STATUS_ABORTED, |
| 58 mojo::MediaDecoderBufferPtr()); | 104 mojo::MediaDecoderBufferPtr(), audio_config.Pass(), |
| 105 video_config.Pass()); |
| 59 return; | 106 return; |
| 60 } | 107 } |
| 61 | 108 |
| 62 DCHECK_EQ(status, media::DemuxerStream::kOk); | 109 DCHECK_EQ(status, media::DemuxerStream::kOk); |
| 63 if (!buffer->end_of_stream()) { | 110 if (!buffer->end_of_stream()) { |
| 64 DCHECK_GT(buffer->data_size(), 0); | 111 DCHECK_GT(buffer->data_size(), 0); |
| 65 // Serialize the data section of the DecoderBuffer into our pipe. | 112 // Serialize the data section of the DecoderBuffer into our pipe. |
| 66 uint32_t num_bytes = buffer->data_size(); | 113 uint32_t num_bytes = buffer->data_size(); |
| 67 CHECK_EQ(WriteDataRaw(stream_pipe_.get(), buffer->data(), &num_bytes, | 114 CHECK_EQ(WriteDataRaw(stream_pipe_.get(), buffer->data(), &num_bytes, |
| 68 MOJO_READ_DATA_FLAG_ALL_OR_NONE), | 115 MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 69 MOJO_RESULT_OK); | 116 MOJO_RESULT_OK); |
| 70 CHECK_EQ(num_bytes, static_cast<uint32_t>(buffer->data_size())); | 117 CHECK_EQ(num_bytes, static_cast<uint32_t>(buffer->data_size())); |
| 71 } | 118 } |
| 72 | 119 |
| 73 // TODO(dalecurtis): Once we can write framed data to the DataPipe, fill via | 120 // TODO(dalecurtis): Once we can write framed data to the DataPipe, fill via |
| 74 // the producer handle and then read more to keep the pipe full. Waiting for | 121 // the producer handle and then read more to keep the pipe full. Waiting for |
| 75 // space can be accomplished using an AsyncWaiter. | 122 // space can be accomplished using an AsyncWaiter. |
| 76 callback.Run(static_cast<mojo::DemuxerStream::Status>(status), | 123 callback.Run(static_cast<mojo::DemuxerStream::Status>(status), |
| 77 mojo::MediaDecoderBuffer::From(buffer)); | 124 mojo::MediaDecoderBuffer::From(buffer), audio_config.Pass(), |
| 78 } | 125 video_config.Pass()); |
| 79 | |
| 80 void MojoDemuxerStreamImpl::Initialize( | |
| 81 mojo::DemuxerStreamObserverPtr observer, | |
| 82 const mojo::Callback<void(mojo::ScopedDataPipeConsumerHandle)>& callback) { | |
| 83 DCHECK(observer); | |
| 84 observer_ = observer.Pass(); | |
| 85 | |
| 86 // This is called when our DemuxerStreamClient has connected itself and is | |
| 87 // ready to receive messages. Send an initial config and notify it that | |
| 88 // we are now ready for business. | |
| 89 if (stream_->type() == media::DemuxerStream::AUDIO) { | |
| 90 observer_->OnAudioDecoderConfigChanged( | |
| 91 mojo::AudioDecoderConfig::From(stream_->audio_decoder_config())); | |
| 92 } else if (stream_->type() == media::DemuxerStream::VIDEO) { | |
| 93 observer_->OnVideoDecoderConfigChanged( | |
| 94 mojo::VideoDecoderConfig::From(stream_->video_decoder_config())); | |
| 95 } | |
| 96 | |
| 97 MojoCreateDataPipeOptions options; | |
| 98 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 99 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 100 options.element_num_bytes = 1; | |
| 101 | |
| 102 // Allocate DataPipe sizes based on content type to reduce overhead. If this | |
| 103 // is still too burdensome we can adjust for sample rate or resolution. | |
| 104 if (stream_->type() == media::DemuxerStream::VIDEO) { | |
| 105 // Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in | |
| 106 // size; so allow for 50% headroom. | |
| 107 options.capacity_num_bytes = 1.5 * (1024 * 1024); | |
| 108 } else { | |
| 109 // Other types don't require a lot of room, so use a smaller pipe. | |
| 110 options.capacity_num_bytes = 512 * 1024; | |
| 111 } | |
| 112 | |
| 113 mojo::DataPipe data_pipe(options); | |
| 114 stream_pipe_ = data_pipe.producer_handle.Pass(); | |
| 115 callback.Run(data_pipe.consumer_handle.Pass()); | |
| 116 } | 126 } |
| 117 | 127 |
| 118 } // namespace media | 128 } // namespace media |
| OLD | NEW |