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.
4
5#include "media/mojo/services/mojo_demuxer_stream_impl.h"
6
7#include "base/bind.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"
14
15namespace media {
16
17MojoDemuxerStreamImpl::MojoDemuxerStreamImpl(media::DemuxerStream* stream)
18    : stream_(stream), weak_factory_(this) {
19}
20
21MojoDemuxerStreamImpl::~MojoDemuxerStreamImpl() {
22}
23
24void MojoDemuxerStreamImpl::Read(const mojo::Callback<
25    void(mojo::DemuxerStream::Status, mojo::MediaDecoderBufferPtr)>& callback) {
26  stream_->Read(base::Bind(&MojoDemuxerStreamImpl::OnBufferReady,
27                           weak_factory_.GetWeakPtr(),
28                           callback));
29}
30
31void 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()));
40  }
41
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));
46}
47
48void 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()));
54
55  // TODO(tim): Create a DataPipe, hold the producer handle, and pass the
56  // consumer handle here.
57  client()->OnStreamReady(mojo::ScopedDataPipeConsumerHandle());
58}
59
60}  // namespace media
61