| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "decoder_selector.h" | 5 #include "decoder_selector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "media/base/audio_decoder.h" | 11 #include "media/base/audio_decoder.h" |
| 12 #include "media/base/bind_to_current_loop.h" | 12 #include "media/base/bind_to_current_loop.h" |
| 13 #include "media/base/demuxer_stream.h" | 13 #include "media/base/demuxer_stream.h" |
| 14 #include "media/base/pipeline.h" | 14 #include "media/base/pipeline.h" |
| 15 #include "media/base/video_decoder.h" | 15 #include "media/base/video_decoder.h" |
| 16 #include "media/filters/decoder_stream_traits.h" |
| 16 #include "media/filters/decrypting_audio_decoder.h" | 17 #include "media/filters/decrypting_audio_decoder.h" |
| 17 #include "media/filters/decrypting_demuxer_stream.h" | 18 #include "media/filters/decrypting_demuxer_stream.h" |
| 18 #include "media/filters/decrypting_video_decoder.h" | 19 #include "media/filters/decrypting_video_decoder.h" |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 | 22 |
| 22 static bool HasValidStreamConfig(DemuxerStream* stream) { | 23 static bool HasValidStreamConfig(DemuxerStream* stream) { |
| 23 switch (stream->type()) { | 24 switch (stream->type()) { |
| 24 case DemuxerStream::AUDIO: | 25 case DemuxerStream::AUDIO: |
| 25 return stream->audio_decoder_config().IsValidConfig(); | 26 return stream->audio_decoder_config().IsValidConfig(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 61 |
| 61 template <DemuxerStream::Type StreamType> | 62 template <DemuxerStream::Type StreamType> |
| 62 DecoderSelector<StreamType>::~DecoderSelector() { | 63 DecoderSelector<StreamType>::~DecoderSelector() { |
| 63 DVLOG(2) << __FUNCTION__; | 64 DVLOG(2) << __FUNCTION__; |
| 64 DCHECK(select_decoder_cb_.is_null()); | 65 DCHECK(select_decoder_cb_.is_null()); |
| 65 } | 66 } |
| 66 | 67 |
| 67 template <DemuxerStream::Type StreamType> | 68 template <DemuxerStream::Type StreamType> |
| 68 void DecoderSelector<StreamType>::SelectDecoder( | 69 void DecoderSelector<StreamType>::SelectDecoder( |
| 69 DemuxerStream* stream, | 70 DemuxerStream* stream, |
| 70 StatisticsCB statistics_cb, | |
| 71 const SelectDecoderCB& select_decoder_cb) { | 71 const SelectDecoderCB& select_decoder_cb) { |
| 72 DVLOG(2) << __FUNCTION__; | 72 DVLOG(2) << __FUNCTION__; |
| 73 DCHECK(task_runner_->BelongsToCurrentThread()); | 73 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 74 DCHECK(stream); | 74 DCHECK(stream); |
| 75 | 75 |
| 76 // Make sure |select_decoder_cb| runs on a different execution stack. | 76 // Make sure |select_decoder_cb| runs on a different execution stack. |
| 77 select_decoder_cb_ = BindToCurrentLoop(select_decoder_cb); | 77 select_decoder_cb_ = BindToCurrentLoop(select_decoder_cb); |
| 78 | 78 |
| 79 statistics_cb_ = statistics_cb; | |
| 80 | |
| 81 if (!HasValidStreamConfig(stream)) { | 79 if (!HasValidStreamConfig(stream)) { |
| 82 DLOG(ERROR) << "Invalid stream config."; | 80 DLOG(ERROR) << "Invalid stream config."; |
| 83 ReturnNullDecoder(); | 81 ReturnNullDecoder(); |
| 84 return; | 82 return; |
| 85 } | 83 } |
| 86 | 84 |
| 87 input_stream_ = stream; | 85 input_stream_ = stream; |
| 88 | 86 |
| 89 if (!IsStreamEncrypted(input_stream_)) { | 87 if (!IsStreamEncrypted(input_stream_)) { |
| 90 InitializeDecoder(); | 88 InitializeDecoder(); |
| 91 return; | 89 return; |
| 92 } | 90 } |
| 93 | 91 |
| 94 // This could happen if Encrypted Media Extension (EME) is not enabled. | 92 // This could happen if Encrypted Media Extension (EME) is not enabled. |
| 95 if (set_decryptor_ready_cb_.is_null()) { | 93 if (set_decryptor_ready_cb_.is_null()) { |
| 96 ReturnNullDecoder(); | 94 ReturnNullDecoder(); |
| 97 return; | 95 return; |
| 98 } | 96 } |
| 99 | 97 |
| 100 decoder_.reset(new typename StreamTraits::DecryptingDecoderType( | 98 decoder_.reset(new typename StreamTraits::DecryptingDecoderType( |
| 101 task_runner_, set_decryptor_ready_cb_)); | 99 task_runner_, set_decryptor_ready_cb_)); |
| 102 | 100 |
| 103 DoInitializeDecoder( | 101 decoder_->Initialize( |
| 102 StreamTraits::GetDecoderConfig(*input_stream_), |
| 104 base::Bind(&DecoderSelector<StreamType>::DecryptingDecoderInitDone, | 103 base::Bind(&DecoderSelector<StreamType>::DecryptingDecoderInitDone, |
| 105 weak_ptr_factory_.GetWeakPtr())); | 104 weak_ptr_factory_.GetWeakPtr())); |
| 106 } | 105 } |
| 107 | 106 |
| 108 template <DemuxerStream::Type StreamType> | 107 template <DemuxerStream::Type StreamType> |
| 109 void DecoderSelector<StreamType>::Abort() { | 108 void DecoderSelector<StreamType>::Abort() { |
| 110 DVLOG(2) << __FUNCTION__; | 109 DVLOG(2) << __FUNCTION__; |
| 111 DCHECK(task_runner_->BelongsToCurrentThread()); | 110 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 112 | 111 |
| 113 // This could happen when SelectDecoder() was not called or when | 112 // This could happen when SelectDecoder() was not called or when |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 DCHECK(!decoder_); | 184 DCHECK(!decoder_); |
| 186 | 185 |
| 187 if (decoders_.empty()) { | 186 if (decoders_.empty()) { |
| 188 ReturnNullDecoder(); | 187 ReturnNullDecoder(); |
| 189 return; | 188 return; |
| 190 } | 189 } |
| 191 | 190 |
| 192 decoder_.reset(decoders_.front()); | 191 decoder_.reset(decoders_.front()); |
| 193 decoders_.weak_erase(decoders_.begin()); | 192 decoders_.weak_erase(decoders_.begin()); |
| 194 | 193 |
| 195 DoInitializeDecoder(base::Bind(&DecoderSelector<StreamType>::DecoderInitDone, | 194 decoder_->Initialize(StreamTraits::GetDecoderConfig(*input_stream_), |
| 196 weak_ptr_factory_.GetWeakPtr())); | 195 base::Bind(&DecoderSelector<StreamType>::DecoderInitDone, |
| 196 weak_ptr_factory_.GetWeakPtr())); |
| 197 } | 197 } |
| 198 | 198 |
| 199 template <DemuxerStream::Type StreamType> | 199 template <DemuxerStream::Type StreamType> |
| 200 void DecoderSelector<StreamType>::DecoderInitDone(PipelineStatus status) { | 200 void DecoderSelector<StreamType>::DecoderInitDone(PipelineStatus status) { |
| 201 DVLOG(2) << __FUNCTION__; | 201 DVLOG(2) << __FUNCTION__; |
| 202 DCHECK(task_runner_->BelongsToCurrentThread()); | 202 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 203 | 203 |
| 204 if (status != PIPELINE_OK) { | 204 if (status != PIPELINE_OK) { |
| 205 decoder_.reset(); | 205 decoder_.reset(); |
| 206 InitializeDecoder(); | 206 InitializeDecoder(); |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 base::ResetAndReturn(&select_decoder_cb_) | 210 base::ResetAndReturn(&select_decoder_cb_) |
| 211 .Run(decoder_.Pass(), decrypted_stream_.Pass()); | 211 .Run(decoder_.Pass(), decrypted_stream_.Pass()); |
| 212 } | 212 } |
| 213 | 213 |
| 214 template <DemuxerStream::Type StreamType> | 214 template <DemuxerStream::Type StreamType> |
| 215 void DecoderSelector<StreamType>::ReturnNullDecoder() { | 215 void DecoderSelector<StreamType>::ReturnNullDecoder() { |
| 216 DVLOG(2) << __FUNCTION__; | 216 DVLOG(2) << __FUNCTION__; |
| 217 DCHECK(task_runner_->BelongsToCurrentThread()); | 217 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 218 base::ResetAndReturn(&select_decoder_cb_) | 218 base::ResetAndReturn(&select_decoder_cb_) |
| 219 .Run(scoped_ptr<Decoder>(), | 219 .Run(scoped_ptr<Decoder>(), |
| 220 scoped_ptr<DecryptingDemuxerStream>()); | 220 scoped_ptr<DecryptingDemuxerStream>()); |
| 221 } | 221 } |
| 222 | 222 |
| 223 // TODO(rileya): Get rid of this and the specialization below once the Audio and | |
| 224 // Video Decoders' Initialize() interfaces match up (see crbug.com/338059). | |
| 225 template <DemuxerStream::Type StreamType> | |
| 226 void DecoderSelector<StreamType>::DoInitializeDecoder( | |
| 227 const PipelineStatusCB& status_cb) { | |
| 228 decoder_->Initialize(input_stream_->video_decoder_config(), status_cb); | |
| 229 } | |
| 230 | |
| 231 // Specialization for AudioDecoder (its Initialize() signature currently doesn't | |
| 232 // match that of VideoDecoder (eventually it will, see the TODO above). | |
| 233 template <> | |
| 234 void AudioDecoderSelector::DoInitializeDecoder( | |
| 235 const PipelineStatusCB& status_cb) { | |
| 236 decoder_->Initialize(input_stream_, status_cb, statistics_cb_); | |
| 237 } | |
| 238 | |
| 239 // These forward declarations tell the compiler that we will use | 223 // These forward declarations tell the compiler that we will use |
| 240 // DecoderSelector with these arguments, allowing us to keep these definitions | 224 // DecoderSelector with these arguments, allowing us to keep these definitions |
| 241 // in our .cc without causing linker errors. This also means if anyone tries to | 225 // in our .cc without causing linker errors. This also means if anyone tries to |
| 242 // instantiate a DecoderSelector with anything but these two specializations | 226 // instantiate a DecoderSelector with anything but these two specializations |
| 243 // they'll most likely get linker errors. | 227 // they'll most likely get linker errors. |
| 244 template class DecoderSelector<DemuxerStream::AUDIO>; | 228 template class DecoderSelector<DemuxerStream::AUDIO>; |
| 245 template class DecoderSelector<DemuxerStream::VIDEO>; | 229 template class DecoderSelector<DemuxerStream::VIDEO>; |
| 246 | 230 |
| 247 } // namespace media | 231 } // namespace media |
| OLD | NEW |