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

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

Issue 2643743002: Mojify demuxers and allow running {Chunk/FFmpeg}Demuxer in a Utility Process (Closed)
Patch Set: Rebase and make sure to unbind mojom::DemuxerPtr on the bound thread during termination Created 3 years, 10 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
« no previous file with comments | « media/mojo/common/mojo_data_buffer_converter.h ('k') | media/mojo/interfaces/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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_data_buffer_converter.h"
6
7 #include <memory>
8
9 #include "base/logging.h"
10 #include "media/base/data_buffer.h"
11 #include "media/mojo/common/media_type_converters.h"
12
13 namespace media {
14
15 namespace {
16
17 std::unique_ptr<mojo::DataPipe> CreateDataPipe(uint32_t capacity_num_bytes) {
18 MojoCreateDataPipeOptions options;
19 options.struct_size = sizeof(MojoCreateDataPipeOptions);
20 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
21 options.element_num_bytes = 1;
22 options.capacity_num_bytes = capacity_num_bytes;
23 return base::MakeUnique<mojo::DataPipe>(options);
24 }
25
26 bool IsPipeReadWriteError(MojoResult result) {
27 return result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT;
28 }
29
30 } // namespace
31
32 // MojoDataBufferReader
33
34 // static
35 std::unique_ptr<MojoDataBufferReader> MojoDataBufferReader::Create(
36 uint32_t capacity_num_bytes,
37 mojo::ScopedDataPipeProducerHandle* producer_handle) {
38 DVLOG(1) << __func__;
39 std::unique_ptr<mojo::DataPipe> data_pipe =
40 CreateDataPipe(capacity_num_bytes);
41 *producer_handle = std::move(data_pipe->producer_handle);
42 return base::WrapUnique(
43 new MojoDataBufferReader(std::move(data_pipe->consumer_handle)));
44 }
45
46 MojoDataBufferReader::MojoDataBufferReader(
47 mojo::ScopedDataPipeConsumerHandle consumer_handle)
48 : consumer_handle_(std::move(consumer_handle)),
49 pipe_watcher_(FROM_HERE),
50 bytes_read_(0) {
51 DVLOG(1) << __func__;
52
53 MojoResult result =
54 pipe_watcher_.Start(consumer_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE,
55 base::Bind(&MojoDataBufferReader::OnPipeReadable,
56 base::Unretained(this)));
57 if (result != MOJO_RESULT_OK) {
58 DVLOG(1) << __func__
59 << ": Failed to start watching the pipe. result=" << result;
60 consumer_handle_.reset();
61 }
62 }
63
64 MojoDataBufferReader::~MojoDataBufferReader() {
65 DVLOG(1) << __func__;
66 }
67
68 void MojoDataBufferReader::ReadDataBuffer(mojom::DataBufferPtr mojo_buffer,
69 ReadCB read_cb) {
70 DVLOG(3) << __func__;
71
72 // DataBuffer cannot be read if the pipe is already closed.
73 if (!consumer_handle_.is_valid()) {
74 DVLOG(1)
75 << __func__
76 << ": Failed to read DataBuffer becuase the pipe is already closed";
77 std::move(read_cb).Run(nullptr);
78 return;
79 }
80
81 DCHECK(!read_cb_);
82 DCHECK(!media_buffer_);
83 DCHECK_EQ(bytes_read_, 0u);
84
85 scoped_refptr<DataBuffer> media_buffer(
86 mojo_buffer.To<scoped_refptr<DataBuffer>>());
87 DCHECK(media_buffer);
88
89 // A non-EOS buffer can have zero size. See http://crbug.com/663438
90 if (media_buffer->end_of_stream() || media_buffer->data_size() == 0) {
91 std::move(read_cb).Run(media_buffer);
92 return;
93 }
94
95 // Read the data section of |media_buffer| from the pipe.
96 read_cb_ = std::move(read_cb);
97 media_buffer_ = std::move(media_buffer);
98 ReadDataBufferData();
99 }
100
101 void MojoDataBufferReader::OnPipeError(MojoResult result) {
102 DVLOG(1) << __func__ << "(" << result << ")";
103 DCHECK(IsPipeReadWriteError(result));
104
105 if (media_buffer_) {
106 DVLOG(1) << __func__ << ": reading from data pipe failed. result=" << result
107 << ", buffer size=" << media_buffer_->data_size()
108 << ", num_bytes(read)=" << bytes_read_;
109 DCHECK(read_cb_);
110 bytes_read_ = 0;
111 media_buffer_ = nullptr;
112 std::move(read_cb_).Run(nullptr);
113 }
114 consumer_handle_.reset();
115 }
116
117 void MojoDataBufferReader::OnPipeReadable(MojoResult result) {
118 DVLOG(4) << __func__ << "(" << result << ")";
119
120 if (result != MOJO_RESULT_OK)
121 OnPipeError(result);
122 else if (media_buffer_)
123 ReadDataBufferData();
124 }
125
126 void MojoDataBufferReader::ReadDataBufferData() {
127 DVLOG(4) << __func__;
128 DCHECK(media_buffer_);
129
130 uint32_t buffer_size =
131 base::checked_cast<uint32_t>(media_buffer_->data_size());
132 DCHECK_GT(buffer_size, 0u);
133
134 uint32_t num_bytes = buffer_size - bytes_read_;
135 DCHECK_GT(num_bytes, 0u);
136
137 MojoResult result = ReadDataRaw(consumer_handle_.get(),
138 media_buffer_->writable_data() + bytes_read_,
139 &num_bytes, MOJO_WRITE_DATA_FLAG_NONE);
140
141 if (IsPipeReadWriteError(result)) {
142 OnPipeError(result);
143 } else if (result == MOJO_RESULT_OK) {
144 DCHECK_GT(num_bytes, 0u);
145 bytes_read_ += num_bytes;
146 if (bytes_read_ == buffer_size) {
147 DCHECK(read_cb_);
148 bytes_read_ = 0;
149 std::move(read_cb_).Run(std::move(media_buffer_));
150 }
151 }
152 }
153
154 // MojoDataBufferWriter
155
156 // static
157 std::unique_ptr<MojoDataBufferWriter> MojoDataBufferWriter::Create(
158 uint32_t capacity_num_bytes,
159 mojo::ScopedDataPipeConsumerHandle* consumer_handle) {
160 DVLOG(1) << __func__;
161 std::unique_ptr<mojo::DataPipe> data_pipe =
162 CreateDataPipe(capacity_num_bytes);
163 *consumer_handle = std::move(data_pipe->consumer_handle);
164 return base::WrapUnique(
165 new MojoDataBufferWriter(std::move(data_pipe->producer_handle)));
166 }
167
168 MojoDataBufferWriter::MojoDataBufferWriter(
169 mojo::ScopedDataPipeProducerHandle producer_handle)
170 : producer_handle_(std::move(producer_handle)),
171 pipe_watcher_(FROM_HERE),
172 bytes_written_(0) {
173 DVLOG(1) << __func__;
174
175 MojoResult result =
176 pipe_watcher_.Start(producer_handle_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
177 base::Bind(&MojoDataBufferWriter::OnPipeWritable,
178 base::Unretained(this)));
179 if (result != MOJO_RESULT_OK) {
180 DVLOG(1) << __func__
181 << ": Failed to start watching the pipe. result=" << result;
182 producer_handle_.reset();
183 }
184 }
185
186 MojoDataBufferWriter::~MojoDataBufferWriter() {
187 DVLOG(1) << __func__;
188 }
189
190 mojom::DataBufferPtr MojoDataBufferWriter::WriteDataBuffer(
191 const scoped_refptr<DataBuffer>& media_buffer) {
192 DVLOG(3) << __func__;
193
194 // DataBuffer cannot be written if the pipe is already closed.
195 if (!producer_handle_.is_valid()) {
196 DVLOG(1)
197 << __func__
198 << ": Failed to write DataBuffer becuase the pipe is already closed";
199 return nullptr;
200 }
201
202 DCHECK(!media_buffer_);
203 DCHECK_EQ(bytes_written_, 0u);
204
205 mojom::DataBufferPtr mojo_buffer = mojom::DataBuffer::From(media_buffer);
206
207 // A non-EOS buffer can have zero size. See http://crbug.com/663438
208 if (media_buffer->end_of_stream() || media_buffer->data_size() == 0)
209 return mojo_buffer;
210
211 // Serialize the data section of the DataBuffer into our pipe.
212 media_buffer_ = media_buffer;
213 MojoResult result = WriteDataBufferData();
214 return IsPipeReadWriteError(result) ? nullptr : std::move(mojo_buffer);
215 }
216
217 void MojoDataBufferWriter::OnPipeError(MojoResult result) {
218 DVLOG(1) << __func__ << "(" << result << ")";
219 DCHECK(IsPipeReadWriteError(result));
220
221 if (media_buffer_) {
222 DVLOG(1) << __func__ << ": writing to data pipe failed. result=" << result
223 << ", buffer size=" << media_buffer_->data_size()
224 << ", num_bytes(written)=" << bytes_written_;
225 media_buffer_ = nullptr;
226 bytes_written_ = 0;
227 }
228 producer_handle_.reset();
229 }
230
231 void MojoDataBufferWriter::OnPipeWritable(MojoResult result) {
232 DVLOG(4) << __func__ << "(" << result << ")";
233
234 if (result != MOJO_RESULT_OK)
235 OnPipeError(result);
236 else if (media_buffer_)
237 WriteDataBufferData();
238 }
239
240 MojoResult MojoDataBufferWriter::WriteDataBufferData() {
241 DVLOG(4) << __func__;
242 DCHECK(media_buffer_);
243
244 uint32_t buffer_size =
245 base::checked_cast<uint32_t>(media_buffer_->data_size());
246 DCHECK_GT(buffer_size, 0u);
247
248 uint32_t num_bytes = buffer_size - bytes_written_;
249 DCHECK_GT(num_bytes, 0u);
250
251 MojoResult result = WriteDataRaw(producer_handle_.get(),
252 media_buffer_->data() + bytes_written_,
253 &num_bytes, MOJO_WRITE_DATA_FLAG_NONE);
254
255 if (IsPipeReadWriteError(result)) {
256 OnPipeError(result);
257 } else if (result == MOJO_RESULT_OK) {
258 DCHECK_GT(num_bytes, 0u);
259 bytes_written_ += num_bytes;
260 if (bytes_written_ == buffer_size) {
261 media_buffer_ = nullptr;
262 bytes_written_ = 0;
263 }
264 }
265
266 return result;
267 }
268
269 } // namespace media
OLDNEW
« no previous file with comments | « media/mojo/common/mojo_data_buffer_converter.h ('k') | media/mojo/interfaces/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698