1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
media / mojo / common / mojo_decoder_buffer_converter.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/mojo/common/mojo_decoder_buffer_converter.h"
#include <memory>
#include <tuple>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "media/base/audio_buffer.h"
#include "media/base/cdm_context.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_util.h"
#include "media/mojo/common/media_type_converters.h"
#include "media/mojo/common/mojo_pipe_read_write_util.h"
using media::mojo_pipe_read_write_util::IsPipeReadWriteError;
namespace media {
// Creates mojo::DataPipe and sets `producer_handle` and `consumer_handle`.
// Returns true on success. Otherwise returns false and reset the handles.
bool CreateDataPipe(uint32_t capacity,
mojo::ScopedDataPipeProducerHandle* producer_handle,
mojo::ScopedDataPipeConsumerHandle* consumer_handle) {
MojoCreateDataPipeOptions options;
options.struct_size = sizeof(MojoCreateDataPipeOptions);
options.flags = MOJO_CREATE_DATA_PIPE_FLAG_NONE;
options.element_num_bytes = 1;
options.capacity_num_bytes = capacity;
auto result =
mojo::CreateDataPipe(&options, *producer_handle, *consumer_handle);
if (result != MOJO_RESULT_OK) {
DLOG(ERROR) << "DataPipe creation failed with " << result;
producer_handle->reset();
consumer_handle->reset();
return false;
}
return true;
}
uint32_t GetDefaultDecoderBufferConverterCapacity(DemuxerStream::Type type) {
uint32_t capacity = 0;
if (type == DemuxerStream::AUDIO) {
// TODO(timav): Consider capacity calculation based on AudioDecoderConfig.
capacity = 512 * 1024;
} else if (type == DemuxerStream::VIDEO) {
// Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in
// size; so allow for some head room.
// TODO(xhwang, sandersd): Provide a better way to customize this value.
capacity = 2 * (1024 * 1024);
} else {
NOTREACHED() << "Unsupported type: " << type;
}
return capacity;
}
// MojoDecoderBufferReader
// static
std::unique_ptr<MojoDecoderBufferReader> MojoDecoderBufferReader::Create(
uint32_t capacity,
mojo::ScopedDataPipeProducerHandle* producer_handle) {
DVLOG(1) << __func__;
DCHECK_GT(capacity, 0u);
// Create a MojoDecoderBufferReader even on the failure case and
// `ReadDecoderBuffer()` below will fail.
// TODO(xhwang): Update callers to handle failure so we can return null.
mojo::ScopedDataPipeConsumerHandle consumer_handle;
std::ignore = CreateDataPipe(capacity, producer_handle, &consumer_handle);
return std::make_unique<MojoDecoderBufferReader>(std::move(consumer_handle));
}
MojoDecoderBufferReader::MojoDecoderBufferReader(
mojo::ScopedDataPipeConsumerHandle consumer_handle)
: consumer_handle_(std::move(consumer_handle)),
pipe_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
base::SequencedTaskRunner::GetCurrentDefault()),
armed_(false),
bytes_read_(0) {
DVLOG(1) << __func__;
if (!consumer_handle_.is_valid()) {
DLOG(ERROR) << __func__ << ": Invalid consumer handle";
return;
}
MojoResult result = pipe_watcher_.Watch(
consumer_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE,
MOJO_WATCH_CONDITION_SATISFIED,
base::BindRepeating(&MojoDecoderBufferReader::OnPipeReadable,
base::Unretained(this)));
if (result != MOJO_RESULT_OK) {
DLOG(ERROR) << __func__
<< ": Failed to start watching the pipe. result=" << result;
consumer_handle_.reset();
}
}
MojoDecoderBufferReader::~MojoDecoderBufferReader() {
DVLOG(1) << __func__;
CancelAllPendingReadCBs();
if (flush_cb_)
std::move(flush_cb_).Run();
}
void MojoDecoderBufferReader::ReadDecoderBuffer(
mojom::DecoderBufferPtr mojo_buffer,
ReadCB read_cb) {
DVLOG(3) << __func__;
DCHECK(!flush_cb_);
if (!consumer_handle_.is_valid()) {
DCHECK(pending_read_cbs_.empty());
CancelReadCB(std::move(read_cb));
return;
}
scoped_refptr<DecoderBuffer> media_buffer(
mojo_buffer.To<scoped_refptr<DecoderBuffer>>());
DCHECK(media_buffer);
if (MediaTraceIsEnabled() && !media_buffer->end_of_stream()) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
"media,gpu", "MojoDecoderBufferReader::Read",
media_buffer->timestamp().InMicroseconds());
read_cb = base::BindOnce(
[](ReadCB read_cb, scoped_refptr<DecoderBuffer> buffer) {
TRACE_EVENT_NESTABLE_ASYNC_END2(
"media,gpu", "MojoDecoderBufferReader::Read",
buffer->timestamp().InMicroseconds(), "timestamp",
buffer->timestamp().InMicroseconds(), "read_bytes",
buffer->size());
std::move(read_cb).Run(std::move(buffer));
},
std::move(read_cb));
}
// We don't want reads to complete out of order, so we queue them even if they
// are zero-sized.
pending_read_cbs_.push_back(std::move(read_cb));
pending_buffers_.push_back(std::move(media_buffer));
// Do nothing if a read is already scheduled.
if (armed_)
return;
// To reduce latency, always process pending reads immediately.
ProcessPendingReads();
}
void MojoDecoderBufferReader::Flush(base::OnceClosure flush_cb) {
DVLOG(2) << __func__;
DCHECK(!flush_cb_);
if (pending_read_cbs_.empty()) {
std::move(flush_cb).Run();
return;
}
flush_cb_ = std::move(flush_cb);
}
bool MojoDecoderBufferReader::HasPendingReads() const {
return !pending_read_cbs_.empty();
}
void MojoDecoderBufferReader::CancelReadCB(ReadCB read_cb) {
DVLOG(1) << "Failed to read DecoderBuffer because the pipe is already closed";
std::move(read_cb).Run(nullptr);
}
void MojoDecoderBufferReader::CancelAllPendingReadCBs() {
while (!pending_read_cbs_.empty()) {
ReadCB read_cb = std::move(pending_read_cbs_.front());
pending_read_cbs_.pop_front();
// TODO(sandersd): Make sure there are no possible re-entrancy issues
// here. Perhaps these should be posted, or merged into a single error
// callback?
CancelReadCB(std::move(read_cb));
}
}
void MojoDecoderBufferReader::CompleteCurrentRead() {
DVLOG(4) << __func__;
DCHECK(!pending_read_cbs_.empty());
DCHECK_EQ(pending_read_cbs_.size(), pending_buffers_.size());
ReadCB read_cb = std::move(pending_read_cbs_.front());
pending_read_cbs_.pop_front();
scoped_refptr<DecoderBuffer> buffer = std::move(pending_buffers_.front());
pending_buffers_.pop_front();
DCHECK(buffer->end_of_stream() || buffer->size() == bytes_read_);
bytes_read_ = 0;
std::move(read_cb).Run(std::move(buffer));
if (pending_read_cbs_.empty() && flush_cb_) {
std::move(flush_cb_).Run();
}
}
void MojoDecoderBufferReader::ScheduleNextRead() {
DVLOG(4) << __func__;
DCHECK(!armed_);
DCHECK(!pending_buffers_.empty());
armed_ = true;
pipe_watcher_.ArmOrNotify();
}
void MojoDecoderBufferReader::OnPipeReadable(
MojoResult result,
const mojo::HandleSignalsState& state) {
DVLOG(4) << __func__ << "(" << result << ", " << state.readable() << ")";
// |MOJO_RESULT_CANCELLED| may be dispatched even while the SimpleWatcher
// is disarmed, and no further notifications will be dispatched after that.
DCHECK(armed_ || result == MOJO_RESULT_CANCELLED);
armed_ = false;
if (result != MOJO_RESULT_OK) {
OnPipeError(result);
return;
}
DCHECK(state.readable());
ProcessPendingReads();
}
void MojoDecoderBufferReader::ProcessPendingReads() {
DVLOG(4) << __func__;
DCHECK(!armed_);
DCHECK(!pending_buffers_.empty());
while (!pending_buffers_.empty()) {
DecoderBuffer* buffer = pending_buffers_.front().get();
size_t buffer_size = 0u;
if (!pending_buffers_.front()->end_of_stream()) {
buffer_size = buffer->size();
}
// Immediately complete empty reads.
// A non-EOS buffer can have zero size. See http://crbug.com/663438
if (buffer_size == 0) {
// TODO(sandersd): Make sure there are no possible re-entrancy issues
// here. Perhaps read callbacks should be posted?
CompleteCurrentRead();
continue;
}
size_t actually_read_bytes = 0;
MojoResult result = consumer_handle_->ReadData(
MOJO_WRITE_DATA_FLAG_NONE,
// We may be starting to read a new buffer (|bytes_read_| == 0), or
// recovering from a previous partial read (|bytes_read_| > 0).
buffer->writable_span().subspan(bytes_read_), actually_read_bytes);
if (IsPipeReadWriteError(result)) {
OnPipeError(result);
return;
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
ScheduleNextRead();
return;
}
DCHECK_EQ(result, MOJO_RESULT_OK);
DVLOG(4) << __func__ << ": " << actually_read_bytes << " bytes read.";
DCHECK_GT(actually_read_bytes, 0u);
bytes_read_ += actually_read_bytes;
// TODO(sandersd): Make sure there are no possible re-entrancy issues
// here.
if (bytes_read_ == buffer_size) {
CompleteCurrentRead();
}
// Since we can still read, try to read more.
}
}
void MojoDecoderBufferReader::OnPipeError(MojoResult result) {
DVLOG(1) << __func__ << "(" << result << ")";
DCHECK(IsPipeReadWriteError(result));
consumer_handle_.reset();
if (!pending_buffers_.empty()) {
DVLOG(1) << __func__ << ": reading from data pipe failed. result=" << result
<< ", buffer size=" << pending_buffers_.front()->size()
<< ", num_bytes(read)=" << bytes_read_;
bytes_read_ = 0;
pending_buffers_.clear();
CancelAllPendingReadCBs();
}
}
// MojoDecoderBufferWriter
// static
std::unique_ptr<MojoDecoderBufferWriter> MojoDecoderBufferWriter::Create(
uint32_t capacity,
mojo::ScopedDataPipeConsumerHandle* consumer_handle) {
DVLOG(1) << __func__;
DCHECK_GT(capacity, 0u);
// Create a MojoDecoderBufferWriter even on the failure case and
// `WriteDecoderBuffer()` below will fail.
// TODO(xhwang): Update callers to handle failure so we can return null.
mojo::ScopedDataPipeProducerHandle producer_handle;
std::ignore = CreateDataPipe(capacity, &producer_handle, consumer_handle);
return std::make_unique<MojoDecoderBufferWriter>(std::move(producer_handle));
}
MojoDecoderBufferWriter::MojoDecoderBufferWriter(
mojo::ScopedDataPipeProducerHandle producer_handle)
: producer_handle_(std::move(producer_handle)),
pipe_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
base::SequencedTaskRunner::GetCurrentDefault()),
armed_(false),
bytes_written_(0) {
DVLOG(1) << __func__;
if (!producer_handle_.is_valid()) {
DLOG(ERROR) << __func__ << ": Invalid producer handle";
return;
}
MojoResult result = pipe_watcher_.Watch(
producer_handle_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_WATCH_CONDITION_SATISFIED,
base::BindRepeating(&MojoDecoderBufferWriter::OnPipeWritable,
base::Unretained(this)));
if (result != MOJO_RESULT_OK) {
DLOG(ERROR) << __func__
<< ": Failed to start watching the pipe. result=" << result;
producer_handle_.reset();
}
}
MojoDecoderBufferWriter::~MojoDecoderBufferWriter() {
DVLOG(1) << __func__;
}
void MojoDecoderBufferWriter::ScheduleNextWrite() {
DVLOG(4) << __func__;
DCHECK(!armed_);
DCHECK(!pending_buffers_.empty());
armed_ = true;
pipe_watcher_.ArmOrNotify();
}
mojom::DecoderBufferPtr MojoDecoderBufferWriter::WriteDecoderBuffer(
scoped_refptr<DecoderBuffer> media_buffer) {
DVLOG(3) << __func__;
// DecoderBuffer cannot be written if the pipe is already closed.
if (!producer_handle_.is_valid()) {
DVLOG(1)
<< __func__
<< ": Failed to write DecoderBuffer because the pipe is already closed";
return nullptr;
}
mojom::DecoderBufferPtr mojo_buffer =
mojom::DecoderBuffer::From(*media_buffer);
// A non-EOS buffer can have zero size. See http://crbug.com/663438
if (media_buffer->end_of_stream() || media_buffer->empty()) {
return mojo_buffer;
}
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("media,gpu",
"MojoDecoderBufferWriter::Write",
media_buffer->timestamp().InMicroseconds());
// Queue writing the buffer's data into our DataPipe.
pending_buffers_.push_back(std::move(media_buffer));
// Do nothing if a write is already scheduled. Otherwise, to reduce latency,
// always try to write data to the pipe first.
if (!armed_)
ProcessPendingWrites();
return mojo_buffer;
}
void MojoDecoderBufferWriter::OnPipeWritable(
MojoResult result,
const mojo::HandleSignalsState& state) {
DVLOG(4) << __func__ << "(" << result << ", " << state.writable() << ")";
// |MOJO_RESULT_CANCELLED| may be dispatched even while the SimpleWatcher
// is disarmed, and no further notifications will be dispatched after that.
DCHECK(armed_ || result == MOJO_RESULT_CANCELLED);
armed_ = false;
if (result != MOJO_RESULT_OK) {
OnPipeError(result);
return;
}
DCHECK(state.writable());
ProcessPendingWrites();
}
void MojoDecoderBufferWriter::ProcessPendingWrites() {
DVLOG(4) << __func__;
DCHECK(!armed_);
DCHECK(!pending_buffers_.empty());
while (!pending_buffers_.empty()) {
DecoderBuffer* buffer = pending_buffers_.front().get();
base::span<const uint8_t> bytes_to_write(*buffer);
DCHECK_GT(bytes_to_write.size(), 0u) << "Unexpected EOS or empty buffer";
// We may be starting to write a new buffer (|bytes_written_| == 0), or
// recovering from a previous partial write (|bytes_written_| > 0).
bytes_to_write = bytes_to_write.subspan(bytes_written_);
DCHECK_GT(bytes_to_write.size(), 0u);
size_t actually_written_bytes = 0;
MojoResult result = producer_handle_->WriteData(
bytes_to_write, MOJO_WRITE_DATA_FLAG_NONE, actually_written_bytes);
if (IsPipeReadWriteError(result)) {
OnPipeError(result);
return;
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
ScheduleNextWrite();
return;
}
DCHECK_EQ(MOJO_RESULT_OK, result);
DVLOG(4) << __func__ << ": " << actually_written_bytes << " bytes written.";
DCHECK_GT(actually_written_bytes, 0u);
bytes_written_ += actually_written_bytes;
if (actually_written_bytes == bytes_to_write.size()) {
TRACE_EVENT_NESTABLE_ASYNC_END2(
"media,gpu", "MojoDecoderBufferWriter::Write",
buffer->timestamp().InMicroseconds(), "timestamp",
buffer->timestamp().InMicroseconds(), "write_bytes", bytes_written_);
pending_buffers_.pop_front();
bytes_written_ = 0;
}
// Since we can still write, try to write more.
}
}
void MojoDecoderBufferWriter::OnPipeError(MojoResult result) {
DVLOG(1) << __func__ << "(" << result << ")";
DCHECK(IsPipeReadWriteError(result));
producer_handle_.reset();
if (!pending_buffers_.empty()) {
DVLOG(1) << __func__ << ": writing to data pipe failed. result=" << result
<< ", buffer size=" << pending_buffers_.front()->size()
<< ", num_bytes(written)=" << bytes_written_;
if (MediaTraceIsEnabled()) {
for (const auto& buffer : pending_buffers_) {
TRACE_EVENT_NESTABLE_ASYNC_END2(
"media,gpu", "MojoDecoderBufferWriter::Write",
buffer->timestamp().InMicroseconds(), "timestamp",
buffer->timestamp().InMicroseconds(), "write_bytes",
bytes_written_);
}
}
pending_buffers_.clear();
bytes_written_ = 0;
}
}
} // namespace media