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