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 "decoder_selector.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "media/base/audio_decoder.h"
12 #include "media/base/bind_to_current_loop.h"
13 #include "media/base/demuxer_stream.h"
14 #include "media/base/pipeline.h"
15 #include "media/base/video_decoder.h"
16 #include "media/filters/decoder_stream_traits.h"
17 #include "media/filters/decrypting_audio_decoder.h"
18 #include "media/filters/decrypting_demuxer_stream.h"
19 #include "media/filters/decrypting_video_decoder.h"
23 static bool HasValidStreamConfig(DemuxerStream
* stream
) {
24 switch (stream
->type()) {
25 case DemuxerStream::AUDIO
:
26 return stream
->audio_decoder_config().IsValidConfig();
27 case DemuxerStream::VIDEO
:
28 return stream
->video_decoder_config().IsValidConfig();
29 case DemuxerStream::UNKNOWN
:
30 case DemuxerStream::TEXT
:
31 case DemuxerStream::NUM_TYPES
:
37 static bool IsStreamEncrypted(DemuxerStream
* stream
) {
38 switch (stream
->type()) {
39 case DemuxerStream::AUDIO
:
40 return stream
->audio_decoder_config().is_encrypted();
41 case DemuxerStream::VIDEO
:
42 return stream
->video_decoder_config().is_encrypted();
43 case DemuxerStream::UNKNOWN
:
44 case DemuxerStream::TEXT
:
45 case DemuxerStream::NUM_TYPES
:
51 template <DemuxerStream::Type StreamType
>
52 DecoderSelector
<StreamType
>::DecoderSelector(
53 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
54 ScopedVector
<Decoder
> decoders
)
55 : task_runner_(task_runner
),
56 decoders_(decoders
.Pass()),
58 weak_ptr_factory_(this) {
61 template <DemuxerStream::Type StreamType
>
62 DecoderSelector
<StreamType
>::~DecoderSelector() {
63 DVLOG(2) << __FUNCTION__
;
64 DCHECK(task_runner_
->BelongsToCurrentThread());
66 if (!select_decoder_cb_
.is_null())
70 decrypted_stream_
.reset();
73 template <DemuxerStream::Type StreamType
>
74 void DecoderSelector
<StreamType
>::SelectDecoder(
75 DemuxerStream
* stream
,
76 const SetDecryptorReadyCB
& set_decryptor_ready_cb
,
77 const SelectDecoderCB
& select_decoder_cb
,
78 const typename
Decoder::OutputCB
& output_cb
) {
79 DVLOG(2) << __FUNCTION__
;
80 DCHECK(task_runner_
->BelongsToCurrentThread());
82 DCHECK(select_decoder_cb_
.is_null());
84 set_decryptor_ready_cb_
= set_decryptor_ready_cb
;
86 // Make sure |select_decoder_cb| runs on a different execution stack.
87 select_decoder_cb_
= BindToCurrentLoop(select_decoder_cb
);
89 if (!HasValidStreamConfig(stream
)) {
90 DLOG(ERROR
) << "Invalid stream config.";
95 input_stream_
= stream
;
96 output_cb_
= output_cb
;
98 if (!IsStreamEncrypted(input_stream_
)) {
103 // This could be null if Encrypted Media Extension (EME) is not enabled.
104 if (set_decryptor_ready_cb_
.is_null()) {
109 decoder_
.reset(new typename
StreamTraits::DecryptingDecoderType(
110 task_runner_
, set_decryptor_ready_cb_
));
112 DecoderStreamTraits
<StreamType
>::InitializeDecoder(
113 decoder_
.get(), input_stream_
,
114 base::Bind(&DecoderSelector
<StreamType
>::DecryptingDecoderInitDone
,
115 weak_ptr_factory_
.GetWeakPtr()),
119 template <DemuxerStream::Type StreamType
>
120 void DecoderSelector
<StreamType
>::DecryptingDecoderInitDone(
121 PipelineStatus status
) {
122 DVLOG(2) << __FUNCTION__
;
123 DCHECK(task_runner_
->BelongsToCurrentThread());
125 if (status
== PIPELINE_OK
) {
126 base::ResetAndReturn(&select_decoder_cb_
)
127 .Run(decoder_
.Pass(), scoped_ptr
<DecryptingDemuxerStream
>());
133 decrypted_stream_
.reset(
134 new DecryptingDemuxerStream(task_runner_
, set_decryptor_ready_cb_
));
136 decrypted_stream_
->Initialize(
138 base::Bind(&DecoderSelector
<StreamType
>::DecryptingDemuxerStreamInitDone
,
139 weak_ptr_factory_
.GetWeakPtr()));
142 template <DemuxerStream::Type StreamType
>
143 void DecoderSelector
<StreamType
>::DecryptingDemuxerStreamInitDone(
144 PipelineStatus status
) {
145 DVLOG(2) << __FUNCTION__
;
146 DCHECK(task_runner_
->BelongsToCurrentThread());
148 if (status
!= PIPELINE_OK
) {
153 DCHECK(!IsStreamEncrypted(decrypted_stream_
.get()));
154 input_stream_
= decrypted_stream_
.get();
158 template <DemuxerStream::Type StreamType
>
159 void DecoderSelector
<StreamType
>::InitializeDecoder() {
160 DVLOG(2) << __FUNCTION__
;
161 DCHECK(task_runner_
->BelongsToCurrentThread());
164 if (decoders_
.empty()) {
169 decoder_
.reset(decoders_
.front());
170 decoders_
.weak_erase(decoders_
.begin());
172 DecoderStreamTraits
<StreamType
>::InitializeDecoder(
173 decoder_
.get(), input_stream_
,
174 base::Bind(&DecoderSelector
<StreamType
>::DecoderInitDone
,
175 weak_ptr_factory_
.GetWeakPtr()),
179 template <DemuxerStream::Type StreamType
>
180 void DecoderSelector
<StreamType
>::DecoderInitDone(PipelineStatus status
) {
181 DVLOG(2) << __FUNCTION__
;
182 DCHECK(task_runner_
->BelongsToCurrentThread());
184 if (status
!= PIPELINE_OK
) {
190 base::ResetAndReturn(&select_decoder_cb_
)
191 .Run(decoder_
.Pass(), decrypted_stream_
.Pass());
194 template <DemuxerStream::Type StreamType
>
195 void DecoderSelector
<StreamType
>::ReturnNullDecoder() {
196 DVLOG(2) << __FUNCTION__
;
197 DCHECK(task_runner_
->BelongsToCurrentThread());
198 base::ResetAndReturn(&select_decoder_cb_
)
199 .Run(scoped_ptr
<Decoder
>(),
200 scoped_ptr
<DecryptingDemuxerStream
>());
203 // These forward declarations tell the compiler that we will use
204 // DecoderSelector with these arguments, allowing us to keep these definitions
205 // in our .cc without causing linker errors. This also means if anyone tries to
206 // instantiate a DecoderSelector with anything but these two specializations
207 // they'll most likely get linker errors.
208 template class DecoderSelector
<DemuxerStream::AUDIO
>;
209 template class DecoderSelector
<DemuxerStream::VIDEO
>;