| 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/common/mojo_decoder_buffer_converter.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "media/base/audio_buffer.h" |
| 14 #include "media/base/cdm_context.h" |
| 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/mojo/common/media_type_converters.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 namespace { |
| 21 |
| 22 std::unique_ptr<mojo::DataPipe> CreateDataPipe(DemuxerStream::Type type) { |
| 23 MojoCreateDataPipeOptions options; |
| 24 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 25 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 26 options.element_num_bytes = 1; |
| 27 |
| 28 if (type == DemuxerStream::AUDIO) { |
| 29 // TODO(timav): Consider capacity calculation based on AudioDecoderConfig. |
| 30 options.capacity_num_bytes = 512 * 1024; |
| 31 } else if (type == DemuxerStream::VIDEO) { |
| 32 // Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in |
| 33 // size; so allow for 50% head room. |
| 34 options.capacity_num_bytes = 1.5 * (1024 * 1024); |
| 35 } else { |
| 36 NOTREACHED() << "Unsupported type: " << type; |
| 37 // Choose a random size. |
| 38 options.capacity_num_bytes = 512 * 1024; |
| 39 } |
| 40 |
| 41 return base::WrapUnique(new mojo::DataPipe(options)); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 // MojoDecoderBufferReader |
| 47 |
| 48 // static |
| 49 std::unique_ptr<MojoDecoderBufferReader> MojoDecoderBufferReader::Create( |
| 50 DemuxerStream::Type type, |
| 51 mojo::ScopedDataPipeProducerHandle* producer_handle) { |
| 52 DVLOG(1) << __FUNCTION__; |
| 53 std::unique_ptr<mojo::DataPipe> data_pipe = CreateDataPipe(type); |
| 54 *producer_handle = std::move(data_pipe->producer_handle); |
| 55 return base::WrapUnique( |
| 56 new MojoDecoderBufferReader(std::move(data_pipe->consumer_handle))); |
| 57 } |
| 58 |
| 59 MojoDecoderBufferReader::MojoDecoderBufferReader( |
| 60 mojo::ScopedDataPipeConsumerHandle consumer_handle) |
| 61 : consumer_handle_(std::move(consumer_handle)) { |
| 62 DVLOG(1) << __FUNCTION__; |
| 63 } |
| 64 |
| 65 MojoDecoderBufferReader::~MojoDecoderBufferReader() { |
| 66 DVLOG(1) << __FUNCTION__; |
| 67 } |
| 68 |
| 69 scoped_refptr<DecoderBuffer> MojoDecoderBufferReader::ConvertDecoderBuffer( |
| 70 const mojom::DecoderBufferPtr& buffer) { |
| 71 DVLOG(3) << __FUNCTION__; |
| 72 scoped_refptr<DecoderBuffer> media_buffer( |
| 73 buffer.To<scoped_refptr<DecoderBuffer>>()); |
| 74 DCHECK(media_buffer); |
| 75 |
| 76 if (media_buffer->end_of_stream()) |
| 77 return media_buffer; |
| 78 |
| 79 // Wait for the data to become available in the DataPipe. |
| 80 MojoHandleSignalsState state; |
| 81 MojoResult result = |
| 82 MojoWait(consumer_handle_.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 83 MOJO_DEADLINE_INDEFINITE, &state); |
| 84 |
| 85 if (result != MOJO_RESULT_OK) { |
| 86 DVLOG(1) << __FUNCTION__ << ": Peer closed the data pipe"; |
| 87 return nullptr; |
| 88 } |
| 89 |
| 90 // Read the inner data for the DecoderBuffer from our DataPipe. |
| 91 uint32_t data_size = static_cast<uint32_t>(media_buffer->data_size()); |
| 92 DCHECK_EQ(data_size, buffer->data_size); |
| 93 DCHECK_GT(data_size, 0u); |
| 94 |
| 95 uint32_t bytes_read = data_size; |
| 96 result = ReadDataRaw(consumer_handle_.get(), media_buffer->writable_data(), |
| 97 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE); |
| 98 if (result != MOJO_RESULT_OK || bytes_read != data_size) { |
| 99 DVLOG(1) << __FUNCTION__ << ": reading from pipe failed"; |
| 100 return nullptr; |
| 101 } |
| 102 |
| 103 return media_buffer; |
| 104 } |
| 105 |
| 106 // MojoDecoderBufferWriter |
| 107 |
| 108 // static |
| 109 std::unique_ptr<MojoDecoderBufferWriter> MojoDecoderBufferWriter::Create( |
| 110 DemuxerStream::Type type, |
| 111 mojo::ScopedDataPipeConsumerHandle* consumer_handle) { |
| 112 DVLOG(1) << __FUNCTION__; |
| 113 std::unique_ptr<mojo::DataPipe> data_pipe = CreateDataPipe(type); |
| 114 *consumer_handle = std::move(data_pipe->consumer_handle); |
| 115 return base::WrapUnique( |
| 116 new MojoDecoderBufferWriter(std::move(data_pipe->producer_handle))); |
| 117 } |
| 118 |
| 119 MojoDecoderBufferWriter::MojoDecoderBufferWriter( |
| 120 mojo::ScopedDataPipeProducerHandle producer_handle) |
| 121 : producer_handle_(std::move(producer_handle)) { |
| 122 DVLOG(1) << __FUNCTION__; |
| 123 } |
| 124 |
| 125 MojoDecoderBufferWriter::~MojoDecoderBufferWriter() { |
| 126 DVLOG(1) << __FUNCTION__; |
| 127 } |
| 128 |
| 129 mojom::DecoderBufferPtr MojoDecoderBufferWriter::ConvertDecoderBuffer( |
| 130 const scoped_refptr<DecoderBuffer>& media_buffer) { |
| 131 DVLOG(3) << __FUNCTION__; |
| 132 mojom::DecoderBufferPtr buffer = mojom::DecoderBuffer::From(media_buffer); |
| 133 |
| 134 if (media_buffer->end_of_stream()) |
| 135 return buffer; |
| 136 |
| 137 // Serialize the data section of the DecoderBuffer into our pipe. |
| 138 uint32_t num_bytes = base::checked_cast<uint32_t>(media_buffer->data_size()); |
| 139 DCHECK_GT(num_bytes, 0u); |
| 140 MojoResult result = |
| 141 WriteDataRaw(producer_handle_.get(), media_buffer->data(), &num_bytes, |
| 142 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 143 if (result != MOJO_RESULT_OK || num_bytes != media_buffer->data_size()) { |
| 144 DVLOG(1) << __FUNCTION__ << ": writing to data pipe failed"; |
| 145 return nullptr; |
| 146 } |
| 147 |
| 148 return buffer; |
| 149 } |
| 150 |
| 151 } // namespace media |
| OLD | NEW |