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 "base/macros.h"
9 #include "media/base/audio_decoder_config.h"
10 #include "media/base/video_decoder_config.h"
11 #include "media/mojo/interfaces/demuxer_stream.mojom.h"
12 #include "media/mojo/services/media_type_converters.h"
13 #include "mojo/public/cpp/bindings/interface_impl.h"
14 #include "mojo/public/cpp/system/data_pipe.h"
18 MojoDemuxerStreamImpl::MojoDemuxerStreamImpl(media::DemuxerStream
* stream
)
19 : stream_(stream
), weak_factory_(this) {
22 MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() {
25 void MojoDemuxerStreamImpl::Read(const mojo::Callback
<
26 void(mojo::DemuxerStream::Status
, mojo::MediaDecoderBufferPtr
)>& callback
) {
27 stream_
->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady
,
28 weak_factory_
.GetWeakPtr(),
32 void MojoDemuxerStreamImpl::OnBufferReady(
33 const BufferReadyCB
& callback
,
34 media::DemuxerStream::Status status
,
35 const scoped_refptr
<media::DecoderBuffer
>& buffer
) {
36 if (status
== media::DemuxerStream::kConfigChanged
) {
37 // Send the config change so our client can read it once it parses the
38 // Status obtained via Run() below.
39 if (stream_
->type() == media::DemuxerStream::AUDIO
) {
40 client()->OnAudioDecoderConfigChanged(
41 mojo::AudioDecoderConfig::From(stream_
->audio_decoder_config()));
42 } else if (stream_
->type() == media::DemuxerStream::VIDEO
) {
43 client()->OnVideoDecoderConfigChanged(
44 mojo::VideoDecoderConfig::From(stream_
->video_decoder_config()));
48 // TODO(tim): Once using DataPipe, fill via the producer handle and then
49 // read more to keep the pipe full.
50 callback
.Run(static_cast<mojo::DemuxerStream::Status
>(status
),
51 mojo::MediaDecoderBuffer::From(buffer
));
54 void MojoDemuxerStreamImpl::OnConnectionEstablished() {
55 // This is called when our DemuxerStreamClient has connected itself and is
56 // ready to receive messages. Send an initial config and notify it that
57 // we are now ready for business.
58 if (stream_
->type() == media::DemuxerStream::AUDIO
) {
59 client()->OnAudioDecoderConfigChanged(
60 mojo::AudioDecoderConfig::From(stream_
->audio_decoder_config()));
61 } else if (stream_
->type() == media::DemuxerStream::VIDEO
) {
62 client()->OnVideoDecoderConfigChanged(
63 mojo::VideoDecoderConfig::From(stream_
->video_decoder_config()));
66 // TODO(tim): Create a DataPipe, hold the producer handle, and pass the
67 // consumer handle here.
68 client()->OnStreamReady(mojo::ScopedDataPipeConsumerHandle());