| 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/clients/mojo_demuxer_stream_impl.h" | 5 #include "media/mojo/clients/mojo_demuxer_stream_impl.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/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 12 #include "media/base/audio_decoder_config.h" | 12 #include "media/base/audio_decoder_config.h" |
| 13 #include "media/base/decoder_buffer.h" | 13 #include "media/base/decoder_buffer.h" |
| 14 #include "media/base/video_decoder_config.h" | 14 #include "media/base/video_decoder_config.h" |
| 15 #include "media/mojo/common/media_type_converters.h" | 15 #include "media/mojo/common/media_type_converters.h" |
| 16 #include "mojo/public/cpp/system/data_pipe.h" | 16 #include "media/mojo/common/mojo_decoder_buffer_converter.h" |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl( | 20 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl( |
| 21 media::DemuxerStream* stream, | 21 media::DemuxerStream* stream, |
| 22 mojo::InterfaceRequest<mojom::DemuxerStream> request) | 22 mojo::InterfaceRequest<mojom::DemuxerStream> request) |
| 23 : binding_(this, std::move(request)), | 23 : binding_(this, std::move(request)), |
| 24 stream_(stream), | 24 stream_(stream), |
| 25 weak_factory_(this) {} | 25 weak_factory_(this) {} |
| 26 | 26 |
| 27 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() {} | 27 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() {} |
| 28 | 28 |
| 29 // This is called when our DemuxerStreamClient has connected itself and is | 29 // This is called when our DemuxerStreamClient has connected itself and is |
| 30 // ready to receive messages. Send an initial config and notify it that | 30 // ready to receive messages. Send an initial config and notify it that |
| 31 // we are now ready for business. | 31 // we are now ready for business. |
| 32 void MojoDemuxerStreamImpl::Initialize(const InitializeCallback& callback) { | 32 void MojoDemuxerStreamImpl::Initialize(const InitializeCallback& callback) { |
| 33 DVLOG(2) << __FUNCTION__; | 33 DVLOG(2) << __FUNCTION__; |
| 34 MojoCreateDataPipeOptions options; | |
| 35 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 36 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 37 options.element_num_bytes = 1; | |
| 38 | |
| 39 // Allocate DataPipe sizes based on content type to reduce overhead. If this | |
| 40 // is still too burdensome we can adjust for sample rate or resolution. | |
| 41 if (stream_->type() == media::DemuxerStream::VIDEO) { | |
| 42 // Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in | |
| 43 // size; so allow for 50% headroom. | |
| 44 options.capacity_num_bytes = 1.5 * (1024 * 1024); | |
| 45 } else { | |
| 46 // Other types don't require a lot of room, so use a smaller pipe. | |
| 47 options.capacity_num_bytes = 512 * 1024; | |
| 48 } | |
| 49 | |
| 50 mojo::DataPipe data_pipe(options); | |
| 51 stream_pipe_ = std::move(data_pipe.producer_handle); | |
| 52 | 34 |
| 53 // Prepare the initial config. | 35 // Prepare the initial config. |
| 54 mojom::AudioDecoderConfigPtr audio_config; | 36 mojom::AudioDecoderConfigPtr audio_config; |
| 55 mojom::VideoDecoderConfigPtr video_config; | 37 mojom::VideoDecoderConfigPtr video_config; |
| 56 if (stream_->type() == media::DemuxerStream::AUDIO) { | 38 if (stream_->type() == media::DemuxerStream::AUDIO) { |
| 57 audio_config = | 39 audio_config = |
| 58 mojom::AudioDecoderConfig::From(stream_->audio_decoder_config()); | 40 mojom::AudioDecoderConfig::From(stream_->audio_decoder_config()); |
| 59 } else if (stream_->type() == media::DemuxerStream::VIDEO) { | 41 } else if (stream_->type() == media::DemuxerStream::VIDEO) { |
| 60 video_config = | 42 video_config = |
| 61 mojom::VideoDecoderConfig::From(stream_->video_decoder_config()); | 43 mojom::VideoDecoderConfig::From(stream_->video_decoder_config()); |
| 62 } else { | 44 } else { |
| 63 NOTREACHED() << "Unsupported stream type: " << stream_->type(); | 45 NOTREACHED() << "Unsupported stream type: " << stream_->type(); |
| 64 return; | 46 return; |
| 65 } | 47 } |
| 66 | 48 |
| 49 mojo::ScopedDataPipeConsumerHandle remote_consumer_handle; |
| 50 mojo_decoder_buffer_writer_ = |
| 51 MojoDecoderBufferWriter::Create(stream_->type(), &remote_consumer_handle); |
| 52 |
| 67 callback.Run(static_cast<mojom::DemuxerStream::Type>(stream_->type()), | 53 callback.Run(static_cast<mojom::DemuxerStream::Type>(stream_->type()), |
| 68 std::move(data_pipe.consumer_handle), std::move(audio_config), | 54 std::move(remote_consumer_handle), std::move(audio_config), |
| 69 std::move(video_config)); | 55 std::move(video_config)); |
| 70 } | 56 } |
| 71 | 57 |
| 72 void MojoDemuxerStreamImpl::Read(const ReadCallback& callback) { | 58 void MojoDemuxerStreamImpl::Read(const ReadCallback& callback) { |
| 73 stream_->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady, | 59 stream_->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady, |
| 74 weak_factory_.GetWeakPtr(), callback)); | 60 weak_factory_.GetWeakPtr(), callback)); |
| 75 } | 61 } |
| 76 | 62 |
| 77 void MojoDemuxerStreamImpl::EnableBitstreamConverter() { | 63 void MojoDemuxerStreamImpl::EnableBitstreamConverter() { |
| 78 stream_->EnableBitstreamConverter(); | 64 stream_->EnableBitstreamConverter(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 107 } | 93 } |
| 108 | 94 |
| 109 if (status == media::DemuxerStream::kAborted) { | 95 if (status == media::DemuxerStream::kAborted) { |
| 110 callback.Run(mojom::DemuxerStream::Status::ABORTED, | 96 callback.Run(mojom::DemuxerStream::Status::ABORTED, |
| 111 mojom::DecoderBufferPtr(), std::move(audio_config), | 97 mojom::DecoderBufferPtr(), std::move(audio_config), |
| 112 std::move(video_config)); | 98 std::move(video_config)); |
| 113 return; | 99 return; |
| 114 } | 100 } |
| 115 | 101 |
| 116 DCHECK_EQ(status, media::DemuxerStream::kOk); | 102 DCHECK_EQ(status, media::DemuxerStream::kOk); |
| 117 if (!buffer->end_of_stream()) { | 103 |
| 118 DCHECK_GT(buffer->data_size(), 0u); | 104 mojom::DecoderBufferPtr mojo_buffer = |
| 119 // Serialize the data section of the DecoderBuffer into our pipe. | 105 mojo_decoder_buffer_writer_->WriteDecoderBuffer(buffer); |
| 120 uint32_t bytes_to_write = base::checked_cast<uint32_t>(buffer->data_size()); | 106 if (!mojo_buffer) { |
| 121 uint32_t bytes_written = bytes_to_write; | 107 callback.Run(mojom::DemuxerStream::Status::ABORTED, |
| 122 CHECK_EQ(WriteDataRaw(stream_pipe_.get(), buffer->data(), &bytes_written, | 108 mojom::DecoderBufferPtr(), std::move(audio_config), |
| 123 MOJO_READ_DATA_FLAG_ALL_OR_NONE), | 109 std::move(video_config)); |
| 124 MOJO_RESULT_OK); | 110 return; |
| 125 CHECK_EQ(bytes_to_write, bytes_written); | |
| 126 } | 111 } |
| 127 | 112 |
| 128 // TODO(dalecurtis): Once we can write framed data to the DataPipe, fill via | 113 // TODO(dalecurtis): Once we can write framed data to the DataPipe, fill via |
| 129 // the producer handle and then read more to keep the pipe full. Waiting for | 114 // the producer handle and then read more to keep the pipe full. Waiting for |
| 130 // space can be accomplished using an AsyncWaiter. | 115 // space can be accomplished using an AsyncWaiter. |
| 131 callback.Run(static_cast<mojom::DemuxerStream::Status>(status), | 116 callback.Run(static_cast<mojom::DemuxerStream::Status>(status), |
| 132 mojom::DecoderBuffer::From(buffer), std::move(audio_config), | 117 std::move(mojo_buffer), std::move(audio_config), |
| 133 std::move(video_config)); | 118 std::move(video_config)); |
| 134 } | 119 } |
| 135 | 120 |
| 136 } // namespace media | 121 } // namespace media |
| OLD | NEW |