Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: media/mojo/common/mojo_decoder_buffer_converter.cc

Issue 2096063003: media: Add MojoDecoderBuffer{Reader|Writer} (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments addressed Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 some head room.
34 // TODO(xhwang, sandersd): Provide a better way to customize this value.
35 options.capacity_num_bytes = 2 * (1024 * 1024);
36 } else {
37 NOTREACHED() << "Unsupported type: " << type;
38 // Choose an arbitrary size.
39 options.capacity_num_bytes = 512 * 1024;
40 }
41
42 return base::WrapUnique(new mojo::DataPipe(options));
43 }
44
45 } // namespace
46
47 // MojoDecoderBufferReader
48
49 // static
50 std::unique_ptr<MojoDecoderBufferReader> MojoDecoderBufferReader::Create(
51 DemuxerStream::Type type,
52 mojo::ScopedDataPipeProducerHandle* producer_handle) {
53 DVLOG(1) << __FUNCTION__;
54 std::unique_ptr<mojo::DataPipe> data_pipe = CreateDataPipe(type);
55 *producer_handle = std::move(data_pipe->producer_handle);
56 return base::WrapUnique(
57 new MojoDecoderBufferReader(std::move(data_pipe->consumer_handle)));
58 }
59
60 MojoDecoderBufferReader::MojoDecoderBufferReader(
61 mojo::ScopedDataPipeConsumerHandle consumer_handle)
62 : consumer_handle_(std::move(consumer_handle)) {
63 DVLOG(1) << __FUNCTION__;
64 }
65
66 MojoDecoderBufferReader::~MojoDecoderBufferReader() {
67 DVLOG(1) << __FUNCTION__;
68 }
69
70 scoped_refptr<DecoderBuffer> MojoDecoderBufferReader::ReadDecoderBuffer(
71 const mojom::DecoderBufferPtr& buffer) {
72 DVLOG(3) << __FUNCTION__;
73 scoped_refptr<DecoderBuffer> media_buffer(
74 buffer.To<scoped_refptr<DecoderBuffer>>());
75 DCHECK(media_buffer);
76
77 if (media_buffer->end_of_stream())
78 return media_buffer;
79
80 // Wait for the data to become available in the DataPipe.
81 // TODO(sandersd): Do not wait indefinitely.
82 MojoHandleSignalsState state;
83 MojoResult result =
84 MojoWait(consumer_handle_.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
85 MOJO_DEADLINE_INDEFINITE, &state);
86
87 if (result != MOJO_RESULT_OK) {
88 DVLOG(1) << __FUNCTION__ << ": Peer closed the data pipe";
89 return nullptr;
90 }
91
92 // Read the inner data for the DecoderBuffer from our DataPipe.
93 uint32_t data_size = static_cast<uint32_t>(media_buffer->data_size());
94 DCHECK_EQ(data_size, buffer->data_size);
95 DCHECK_GT(data_size, 0u);
96
97 uint32_t bytes_read = data_size;
98 result = ReadDataRaw(consumer_handle_.get(), media_buffer->writable_data(),
99 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE);
100 if (result != MOJO_RESULT_OK || bytes_read != data_size) {
101 DVLOG(1) << __FUNCTION__ << ": reading from pipe failed";
102 return nullptr;
103 }
104
105 return media_buffer;
106 }
107
108 // MojoDecoderBufferWriter
109
110 // static
111 std::unique_ptr<MojoDecoderBufferWriter> MojoDecoderBufferWriter::Create(
112 DemuxerStream::Type type,
113 mojo::ScopedDataPipeConsumerHandle* consumer_handle) {
114 DVLOG(1) << __FUNCTION__;
115 std::unique_ptr<mojo::DataPipe> data_pipe = CreateDataPipe(type);
116 *consumer_handle = std::move(data_pipe->consumer_handle);
117 return base::WrapUnique(
118 new MojoDecoderBufferWriter(std::move(data_pipe->producer_handle)));
119 }
120
121 MojoDecoderBufferWriter::MojoDecoderBufferWriter(
122 mojo::ScopedDataPipeProducerHandle producer_handle)
123 : producer_handle_(std::move(producer_handle)) {
124 DVLOG(1) << __FUNCTION__;
125 }
126
127 MojoDecoderBufferWriter::~MojoDecoderBufferWriter() {
128 DVLOG(1) << __FUNCTION__;
129 }
130
131 mojom::DecoderBufferPtr MojoDecoderBufferWriter::WriteDecoderBuffer(
132 const scoped_refptr<DecoderBuffer>& media_buffer) {
133 DVLOG(3) << __FUNCTION__;
134 mojom::DecoderBufferPtr buffer = mojom::DecoderBuffer::From(media_buffer);
135
136 if (media_buffer->end_of_stream())
137 return buffer;
138
139 // Serialize the data section of the DecoderBuffer into our pipe.
140 uint32_t num_bytes = base::checked_cast<uint32_t>(media_buffer->data_size());
141 DCHECK_GT(num_bytes, 0u);
142 MojoResult result =
143 WriteDataRaw(producer_handle_.get(), media_buffer->data(), &num_bytes,
144 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
145 if (result != MOJO_RESULT_OK || num_bytes != media_buffer->data_size()) {
146 DVLOG(1) << __FUNCTION__ << ": writing to data pipe failed";
147 return nullptr;
148 }
149
150 return buffer;
151 }
152
153 } // namespace media
OLDNEW
« no previous file with comments | « media/mojo/common/mojo_decoder_buffer_converter.h ('k') | media/mojo/common/mojo_decoder_buffer_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698