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
3 // found in the LICENSE file.
5 #include "media/mojo/services/mojo_demuxer_stream_impl.h"
8 #include "media/base/audio_decoder_config.h"
9 #include "media/base/decoder_buffer.h"
10 #include "media/base/video_decoder_config.h"
11 #include "media/mojo/services/media_type_converters.h"
12 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
16 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl(
17 media::DemuxerStream
* stream
,
18 mojo::InterfaceRequest
<interfaces::DemuxerStream
> request
)
19 : binding_(this, request
.Pass()), stream_(stream
), weak_factory_(this) {}
21 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() {
24 // This is called when our DemuxerStreamClient has connected itself and is
25 // ready to receive messages. Send an initial config and notify it that
26 // we are now ready for business.
27 void MojoDemuxerStreamImpl::Initialize(const InitializeCallback
& callback
) {
28 DVLOG(2) << __FUNCTION__
;
29 MojoCreateDataPipeOptions options
;
30 options
.struct_size
= sizeof(MojoCreateDataPipeOptions
);
31 options
.flags
= MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
;
32 options
.element_num_bytes
= 1;
34 // Allocate DataPipe sizes based on content type to reduce overhead. If this
35 // is still too burdensome we can adjust for sample rate or resolution.
36 if (stream_
->type() == media::DemuxerStream::VIDEO
) {
37 // Video can get quite large; at 4K, VP9 delivers packets which are ~1MB in
38 // size; so allow for 50% headroom.
39 options
.capacity_num_bytes
= 1.5 * (1024 * 1024);
41 // Other types don't require a lot of room, so use a smaller pipe.
42 options
.capacity_num_bytes
= 512 * 1024;
45 mojo::DataPipe
data_pipe(options
);
46 stream_pipe_
= data_pipe
.producer_handle
.Pass();
48 // Prepare the initial config.
49 interfaces::AudioDecoderConfigPtr audio_config
;
50 interfaces::VideoDecoderConfigPtr video_config
;
51 if (stream_
->type() == media::DemuxerStream::AUDIO
) {
53 interfaces::AudioDecoderConfig::From(stream_
->audio_decoder_config());
54 } else if (stream_
->type() == media::DemuxerStream::VIDEO
) {
56 interfaces::VideoDecoderConfig::From(stream_
->video_decoder_config());
58 NOTREACHED() << "Unsupported stream type: " << stream_
->type();
62 callback
.Run(static_cast<interfaces::DemuxerStream::Type
>(stream_
->type()),
63 data_pipe
.consumer_handle
.Pass(), audio_config
.Pass(),
67 void MojoDemuxerStreamImpl::Read(const ReadCallback
& callback
) {
68 stream_
->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady
,
69 weak_factory_
.GetWeakPtr(), callback
));
72 void MojoDemuxerStreamImpl::OnBufferReady(
73 const ReadCallback
& callback
,
74 media::DemuxerStream::Status status
,
75 const scoped_refptr
<media::DecoderBuffer
>& buffer
) {
76 interfaces::AudioDecoderConfigPtr audio_config
;
77 interfaces::VideoDecoderConfigPtr video_config
;
79 if (status
== media::DemuxerStream::kConfigChanged
) {
80 DVLOG(2) << __FUNCTION__
<< ": ConfigChange!";
81 // Send the config change so our client can read it once it parses the
82 // Status obtained via Run() below.
83 if (stream_
->type() == media::DemuxerStream::AUDIO
) {
85 interfaces::AudioDecoderConfig::From(stream_
->audio_decoder_config());
86 } else if (stream_
->type() == media::DemuxerStream::VIDEO
) {
88 interfaces::VideoDecoderConfig::From(stream_
->video_decoder_config());
90 NOTREACHED() << "Unsupported config change encountered for type: "
94 callback
.Run(interfaces::DemuxerStream::STATUS_CONFIG_CHANGED
,
95 interfaces::DecoderBufferPtr(), audio_config
.Pass(),
100 if (status
== media::DemuxerStream::kAborted
) {
101 callback
.Run(interfaces::DemuxerStream::STATUS_ABORTED
,
102 interfaces::DecoderBufferPtr(), audio_config
.Pass(),
103 video_config
.Pass());
107 DCHECK_EQ(status
, media::DemuxerStream::kOk
);
108 if (!buffer
->end_of_stream()) {
109 DCHECK_GT(buffer
->data_size(), 0);
110 // Serialize the data section of the DecoderBuffer into our pipe.
111 uint32_t num_bytes
= buffer
->data_size();
112 CHECK_EQ(WriteDataRaw(stream_pipe_
.get(), buffer
->data(), &num_bytes
,
113 MOJO_READ_DATA_FLAG_ALL_OR_NONE
),
115 CHECK_EQ(num_bytes
, static_cast<uint32_t>(buffer
->data_size()));
118 // TODO(dalecurtis): Once we can write framed data to the DataPipe, fill via
119 // the producer handle and then read more to keep the pipe full. Waiting for
120 // space can be accomplished using an AsyncWaiter.
121 callback
.Run(static_cast<interfaces::DemuxerStream::Status
>(status
),
122 interfaces::DecoderBuffer::From(buffer
), audio_config
.Pass(),
123 video_config
.Pass());