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_adapter.h"
8 #include "base/callback_helpers.h"
9 #include "media/base/decoder_buffer.h"
10 #include "media/mojo/services/media_type_converters.h"
14 MojoDemuxerStreamAdapter::MojoDemuxerStreamAdapter(
15 mojo::DemuxerStreamPtr demuxer_stream
,
16 const base::Closure
& stream_ready_cb
)
17 : demuxer_stream_(demuxer_stream
.Pass()),
18 stream_ready_cb_(stream_ready_cb
),
19 type_(DemuxerStream::UNKNOWN
),
21 DVLOG(1) << __FUNCTION__
;
22 demuxer_stream_
.set_client(this);
25 MojoDemuxerStreamAdapter::~MojoDemuxerStreamAdapter() {
26 DVLOG(1) << __FUNCTION__
;
29 void MojoDemuxerStreamAdapter::Read(const DemuxerStream::ReadCB
& read_cb
) {
30 DVLOG(3) << __FUNCTION__
;
31 // We shouldn't be holding on to a previous callback if a new Read() came in.
32 DCHECK(read_cb_
.is_null());
34 demuxer_stream_
->Read(base::Bind(&MojoDemuxerStreamAdapter::OnBufferReady
,
35 weak_factory_
.GetWeakPtr()));
38 AudioDecoderConfig
MojoDemuxerStreamAdapter::audio_decoder_config() {
39 DCHECK_EQ(type_
, DemuxerStream::AUDIO
);
40 DCHECK(!audio_config_queue_
.empty());
41 return audio_config_queue_
.front();
44 VideoDecoderConfig
MojoDemuxerStreamAdapter::video_decoder_config() {
45 DCHECK_EQ(type_
, DemuxerStream::VIDEO
);
46 DCHECK(!video_config_queue_
.empty());
47 return video_config_queue_
.front();
50 DemuxerStream::Type
MojoDemuxerStreamAdapter::type() {
54 void MojoDemuxerStreamAdapter::EnableBitstreamConverter() {
58 bool MojoDemuxerStreamAdapter::SupportsConfigChanges() {
62 VideoRotation
MojoDemuxerStreamAdapter::video_rotation() {
64 return VIDEO_ROTATION_0
;
67 void MojoDemuxerStreamAdapter::OnStreamReady(
68 mojo::ScopedDataPipeConsumerHandle pipe
) {
69 DVLOG(1) << __FUNCTION__
;
70 // TODO(tim): We don't support pipe streaming yet.
71 DCHECK(!pipe
.is_valid());
72 DCHECK_NE(type_
, DemuxerStream::UNKNOWN
);
73 stream_ready_cb_
.Run();
76 void MojoDemuxerStreamAdapter::OnAudioDecoderConfigChanged(
77 mojo::AudioDecoderConfigPtr config
) {
78 DCHECK(type_
== DemuxerStream::UNKNOWN
|| type_
== DemuxerStream::AUDIO
)
80 type_
= DemuxerStream::AUDIO
;
82 audio_config_queue_
.push(config
.To
<AudioDecoderConfig
>());
84 if (!read_cb_
.is_null()) {
85 read_cb_
.Run(DemuxerStream::Status::kConfigChanged
, NULL
);
90 void MojoDemuxerStreamAdapter::OnVideoDecoderConfigChanged(
91 mojo::VideoDecoderConfigPtr config
) {
92 DCHECK(type_
== DemuxerStream::UNKNOWN
|| type_
== DemuxerStream::VIDEO
)
94 type_
= DemuxerStream::VIDEO
;
96 video_config_queue_
.push(config
.To
<VideoDecoderConfig
>());
98 if (!read_cb_
.is_null()) {
99 read_cb_
.Run(DemuxerStream::Status::kConfigChanged
, NULL
);
104 void MojoDemuxerStreamAdapter::OnBufferReady(
105 mojo::DemuxerStream::Status status
,
106 mojo::MediaDecoderBufferPtr buffer
) {
107 DVLOG(3) << __FUNCTION__
;
108 DCHECK(!read_cb_
.is_null());
109 DCHECK_NE(type_
, DemuxerStream::UNKNOWN
);
111 DemuxerStream::Status
media_status(
112 static_cast<DemuxerStream::Status
>(status
));
113 scoped_refptr
<DecoderBuffer
> media_buffer(
114 buffer
.To
<scoped_refptr
<DecoderBuffer
>>());
116 if (status
== mojo::DemuxerStream::STATUS_CONFIG_CHANGED
) {
117 DCHECK(!media_buffer
.get());
119 // If the configuration queue is empty we need to wait for a config change
120 // event before invoking |read_cb_|.
122 if (type_
== DemuxerStream::AUDIO
) {
123 audio_config_queue_
.pop();
124 if (audio_config_queue_
.empty())
126 } else if (type_
== DemuxerStream::VIDEO
) {
127 video_config_queue_
.pop();
128 if (video_config_queue_
.empty())
133 read_cb_
.Run(media_status
, media_buffer
);